英文:
Trying to use VBOs & VAOs in JOGL (OpenGL) throws an exception wherein a buffer cannot be found
问题
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;
public int vVAO_ID;
public IntBuffer vVAO;
//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, 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) {
}
}
英文:
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) {
}
}
答案1
得分: 2
顶点数组对象(Vertex Array Object)和缓冲对象(Buffer Object)的名称分别包含在IntBuffer
对象vVAO
和vVBO
中。您忘记将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();
英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论