尝试在 JOGL (OpenGL) 中使用 VBO 和 VAO 会引发异常,其中无法找到缓冲区

Trying to use VBOs & VAOs in JOGL (OpenGL) throws an exception wherein a buffer cannot be found

我完全被困在这里,所以任何帮助将不胜感激:/

我正在尝试学习如何使用 OpenGL 4.0,使用它的 Java 绑定,JOGL。看起来现在最好使用 VBO/VAOs 而不是 glBegin/End.

我正在关注各种来源,但对于这个特定方面,我使用了这个:https://learnopengl.com/Getting-started/Hello-Triangle (C++)

不幸的是,当我运行我的程序时,它抛出以下异常(@第41行): “Exception in thread "AWT-EventQueue-0" com.jogamp.opengl.GLException: Caught GLException: GL_INVALID_OPERATION: Buffer for target 0x8892 not bound on thread AWT-EventQueue-0

这是我实现 class(渲染器)的完整 GLEventListener:

package pRendering;

import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL4;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

public class Renderer implements GLEventListener {

    public int vShaderProgramID;
    public int vVertShaderID;
    public int vFragShaderID;

    public int vVBO_ID;
    public IntBuffer vVBO; //uint pointer?

    public int vVAO_ID;
    public IntBuffer vVAO; //uint pointer?

    //region tmp
    float vertices[] = {
            -0.5f, -0.5f, 0.0f,
            0.5f, -0.5f, 0.0f,
            0.0f,  0.5f, 0.0f
    };
    //endregion

    private void TriangleVBO_VAO(GL4 vGL){
        FloatBuffer vVertFloatBuffer = Buffers.newDirectFloatBuffer(vertices);

        vGL.glGenVertexArrays(1, vVAO);
        vGL.glGenBuffers(1, vVBO);

        vGL.glBindVertexArray(vVAO_ID);

        vGL.glBindBuffer(GL.GL_ARRAY_BUFFER, vVBO_ID);
        vGL.glBufferData(GL.GL_ARRAY_BUFFER, vVertFloatBuffer.capacity() * Buffers.SIZEOF_FLOAT, vVertFloatBuffer, GL.GL_STATIC_DRAW); // <---

        vGL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, false, 3 * 4 /*sizeof(float)*/, 0);
        vGL.glEnableVertexAttribArray(0);

        vGL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

        vGL.glBindVertexArray(0);
    }

    @Override
    public void init(GLAutoDrawable glAutoDrawable) {
        GL4 vGL = glAutoDrawable.getGL().getGL4();

        vShaderProgramID = vGL.glCreateProgram();
        try {
            vVertShaderID = Shaders.CreateShader(vGL, vShaderProgramID, GL4.GL_VERTEX_SHADER, "C:\Users\quent\Desktop\def.vshd");
            vFragShaderID = Shaders.CreateShader(vGL, vShaderProgramID, GL4.GL_FRAGMENT_SHADER, "C:\Users\quent\Desktop\def.fshd");

            Shaders.Link(vGL, vShaderProgramID);
        } catch (Exception EX) {
            EX.toString();
        }

        TriangleVBO_VAO(vGL);
    }

    @Override
    public void display(GLAutoDrawable glAutoDrawable) {
        GL4 vGL = glAutoDrawable.getGL().getGL4();

        vGL.glClear(GL4.GL_COLOR_BUFFER_BIT | GL4.GL_DEPTH_BUFFER_BIT);

        vGL.glUseProgram(vShaderProgramID);

        vGL.glBindVertexArray(vVAO_ID);

        vGL.glDrawArrays(GL.GL_TRIANGLES, 0, 3);
    }

    @Override
    public void dispose(GLAutoDrawable glAutoDrawable) {
        GL4 vGL = glAutoDrawable.getGL().getGL4();

        vGL.glDetachShader( vShaderProgramID, vVertShaderID);
        vGL.glDetachShader( vShaderProgramID, vFragShaderID);
        vGL.glDeleteProgram(vShaderProgramID);
    }

    @Override
    public void reshape(GLAutoDrawable glAutoDrawable, int i, int i1, int i2, int i3) {

    }
}

顶点数组对象和缓冲区对象的名称分别包含在IntBuffer个对象vVAOvVBO。您错过了将 IntBuffer 的内容分别分配给属性 vVAO_ID vVBO_ID:

vVAO = GLBuffers.newDirectIntBuffer(1);
vVBO = GLBuffers.newDirectIntBuffer(1);

vGL.glGenVertexArrays(1, vVAO);
vVAO_ID = vVAO.get();

vGL.glGenBuffers(1, vVBO);
vVBO_ID = vVBO.get();