正确使用 glVertexAttribPointer?
Correct Use of glVertexAttribPointer?
我最近决定开始学习 OpenGL,并给自己买了一本关于 OpenGL Core 3.3 的书。书一般都是讲C++的
所以,经过一番寻找,我找到了一个我更擅长的语言的库,它提供了几乎相同的功能:lwjgl.
我按照书中的步骤将 C++ 语法翻译成 java 语法,直到它开始实际绘制一些东西。
不管我对代码做了什么更改,JVM 一直崩溃。经过一些调试后,我发现当我调用 glVertexAttribPointer
或 glDrawArrays
.
时 JVM 崩溃了
我是 OpenGL 的新手,我假设这个问题对于更有经验的人来说一定听起来很愚蠢,但是:关于这段代码我需要更改什么?
float[] vertices = {
-0.5f, -0.5f, -0.0f,
0.5f, 0.5f, 0.0f,
0.0f,0.5f,0.0f
};
FloatBuffer b = BufferUtils.createFloatBuffer(9);
b.put(vertices);
int VBO = glGenBuffers();
int VAO = glGenVertexArrays();
log.info("VBO:" + VBO + "VAO: " + VAO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 12, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
glBindVertexArray(0);
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while (!glfwWindowShouldClose(window))
{
// input
// -----
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(shaderProgram);
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
我将非常感谢我能得到的任何帮助,如果您需要更多信息/需要查看更多我的代码,请告诉我。提前致谢
在指定顶点属性之前,您必须将顶点缓冲区对象绑定到目标 GL_ARRAY_BUFFER
:
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 12, 0);
当glVertexAttribPointer
is called, the buffer object currently bound to the ARRAY_BUFFER target is associated to the attribute (index) and a reference to the buffer object is stored in the state vector of the Vertex Array Object.
我最近决定开始学习 OpenGL,并给自己买了一本关于 OpenGL Core 3.3 的书。书一般都是讲C++的
所以,经过一番寻找,我找到了一个我更擅长的语言的库,它提供了几乎相同的功能:lwjgl.
我按照书中的步骤将 C++ 语法翻译成 java 语法,直到它开始实际绘制一些东西。
不管我对代码做了什么更改,JVM 一直崩溃。经过一些调试后,我发现当我调用 glVertexAttribPointer
或 glDrawArrays
.
我是 OpenGL 的新手,我假设这个问题对于更有经验的人来说一定听起来很愚蠢,但是:关于这段代码我需要更改什么?
float[] vertices = {
-0.5f, -0.5f, -0.0f,
0.5f, 0.5f, 0.0f,
0.0f,0.5f,0.0f
};
FloatBuffer b = BufferUtils.createFloatBuffer(9);
b.put(vertices);
int VBO = glGenBuffers();
int VAO = glGenVertexArrays();
log.info("VBO:" + VBO + "VAO: " + VAO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 12, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
glBindVertexArray(0);
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while (!glfwWindowShouldClose(window))
{
// input
// -----
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(shaderProgram);
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
我将非常感谢我能得到的任何帮助,如果您需要更多信息/需要查看更多我的代码,请告诉我。提前致谢
在指定顶点属性之前,您必须将顶点缓冲区对象绑定到目标 GL_ARRAY_BUFFER
:
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 12, 0);
当glVertexAttribPointer
is called, the buffer object currently bound to the ARRAY_BUFFER target is associated to the attribute (index) and a reference to the buffer object is stored in the state vector of the Vertex Array Object.