简体   繁体   中英

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

I am completely stuck here, so any help would be appreciated:/

I'm trying to learn how to use OpenGL 4.0 using it's Java Bindings, JOGL. It seems like now it's better to use VBO/VAOs as opposed to glBegin/End.

I'm following various sources, but for this specific aspect I used this: https://learnopengl.com/Getting-started/Hello-Triangle (C++)

Unfortunately, when I run my program, it throws the following exception (@ Line 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 "

Here's my full GLEventListener implementing class (Renderer):

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) {

    }
}

The name of the vertex array object and the buffer object is contained in the IntBuffer objects vVAO respectively vVBO . You missed to assign the content of the IntBuffer s to the attributes vVAO_ID respectively 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM