You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 with flip().
  • Incorrect OpenGL state order: glVertexAttribPointer relies on the currently bound GL_ARRAY_BUFFER to 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

  1. 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, glBufferData would read zero bytes (or garbage beyond your data) from the buffer.
  2. 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).

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:22:39