gpt4 book ai didi

java - 尝试使用 LWJGL3 绘制基本形状

转载 作者:太空宇宙 更新时间:2023-11-04 10:18:47 25 4
gpt4 key购买 nike

所以我想使用 VAO 和 VBO 绘制一个形状,我认为我做的一切都是正确的,但每当我运行我的代码时,我只会得到带有清晰颜色的窗口。当我在调用创建功能之前尝试初始化三角形时,我遇到了一个问题,我是否缺少一些开始绘图的函数?

这是我的代码:

int vaoId, vboId, vertexCount;

float[] vertices = {
// Left bottom triangle
-0.5f, 0.5f,
-0.5f, -0.5f,
0.5f, -0.5f,};

private void init() {
if (!glfwInit()) {
throw new IllegalStateException("Failed to Initialize GLFW!");
}

int width = 1000;
int height = 1000;

glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
window = glfwCreateWindow(width, height, "App", NULL, NULL);

if (window == 0) {
throw new IllegalStateException("Failed to create Window!");
}

GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (videoMode.width() - width) / 2, (videoMode.height() - height) / 2);

// Make the OpenGL context current
glfwMakeContextCurrent(window);
// Enable v-sync
glfwSwapInterval(1);

glfwShowWindow(window);
}

private void loop() {
// This line is critical for LWJGL's interoperation with GLFW's
// OpenGL context, or any context that is managed externally.
// LWJGL detects the context that is current in the current thread,
// creates the GLCapabilities instance and makes the OpenGL
// bindings available for use.
GL.createCapabilities();

initTriangle();

// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT); // clear the framebuffer

glBindVertexArray(vaoId);
glEnableVertexAttribArray(0);

glDrawArrays(GL_TRIANGLES, 0, vertexCount);

glDisableVertexAttribArray(0);
glBindVertexArray(0);

glfwSwapBuffers(window); // swap the color buffers

// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();

}
}

private void initTriangle() {

FloatBuffer vertBuf = MemoryUtil.memAllocFloat(vertices.length);
vertBuf.put(vertices);
vertBuf.flip();

vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);

vboId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertBuf, GL_STATIC_DRAW);

glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}

希望大家帮忙,非常感谢。

最佳答案

I am not using shaders. Is that my problem? And is it a necessity

OpenGL 中最先进的渲染方式是使用 Shader

如果您不使用着色器,则必须通过 glVertexPointer 定义顶点数据数组

vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);

vboId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertBuf, GL_STATIC_DRAW);

glVertexPointer( 2, GL_FLOAT, 0, 0 ); // <---------------

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

并且您必须通过 glEnableClientState( GL_VERTEX_ARRAY ) 启用顶点坐标的客户端功能:

glBindVertexArray(vaoId);
glEnableClientState( GL_VERTEX_ARRAY ); // <---------------

glDrawArrays(GL_TRIANGLES, 0, vertexCount);

glDisableClientState( GL_VERTEX_ARRAY ); // <---------------
glBindVertexArray(0);
<小时/>

注意,客户端能力(或顶点属性数组)的此状态存储在 Vertex Array Object 中。因此,当指定顶点数组对象时,启用顶点坐标就足够了。绘制几何体时,可以省略顶点坐标的启用和禁用:

vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);

vboId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertBuf, GL_STATIC_DRAW);

glVertexPointer( 2, GL_FLOAT, 0, 0 );
glEnableClientState( GL_VERTEX_ARRAY ); // <---------------

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

glBindVertexArray(vaoId);

glDrawArrays(GL_TRIANGLES, 0, vertexCount);

glBindVertexArray(0);

关于java - 尝试使用 LWJGL3 绘制基本形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51390730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com