glVertexAttribPointer使用错误致JVM崩溃,求代码修改方案
OpenGL Core 3.3 (LWJGL) Crash on glVertexAttribPointer/glDrawArrays - Fixes
Hey there, let's break down why your code is crashing and fix it step by step. Your JVM crashes because of two critical mistakes in OpenGL state setup and buffer handling:
Key Issues in Your Code
- FloatBuffer not flipped: After filling the buffer with
b.put(vertices), the buffer's position is at the end of the data. OpenGL will read from this position (which is beyond your actual vertex data) if you don't reset it withflip(). - Incorrect OpenGL state order:
glVertexAttribPointerrelies on the currently boundGL_ARRAY_BUFFERto associate the vertex data with the attribute. You're calling this function before binding and filling your VBO, so the attribute ends up linked to whatever (invalid) buffer was bound previously (or none at all), leading to invalid memory access when drawing.
Fixed Code
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); b.flip(); // Critical: Reset buffer position to start int VBO = glGenBuffers(); int VAO = glGenVertexArrays(); log.info("VBO:" + VBO + " VAO: " + VAO); // Correct order: Bind VAO first, then bind VBO, fill VBO, then configure vertex attributes glBindVertexArray(VAO); // Bind VBO first so glVertexAttribPointer links to it glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, b, GL_STATIC_DRAW); // Use the flipped buffer here // Now configure the vertex attribute (linked to the bound VBO) glVertexAttribPointer(0, 3, GL_FLOAT, false, 12, 0); glEnableVertexAttribArray(0); // Unbind VBO and VAO (optional but good practice) glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); // Rendering loop remains mostly the same while (!glfwWindowShouldClose(window)) { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(shaderProgram); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); glfwSwapBuffers(window); glfwPollEvents(); }
Detailed Explanations of Changes
- Added
b.flip(): This moves the buffer's read/write position back to index 0 and sets the limit to the number of elements you put into the buffer. Without this,glBufferDatawould read zero bytes (or garbage beyond your data) from the buffer. - Reordered OpenGL calls:
- After binding the VAO, we first bind the VBO and upload the vertex data with
glBufferData. - Only then do we call
glVertexAttribPointer— this ensures the attribute index 0 is associated with our newly created VBO. - We also unbind the VBO after setting up the attribute (since the VAO stores this association, we don't need to keep the VBO bound).
- After binding the VAO, we first bind the VBO and upload the vertex data with
Give this a try — it should resolve the JVM crashes during drawing. If you still run into issues, feel free to share your shader code or any additional error logs!
内容的提问来源于stack exchange,提问作者PhoenixXKN
相关产品推荐
相关产品推荐

