java - 如何设置索引缓冲区对象

标签 java opengl lwjgl index-buffer

我正在尝试使用 lwjgl(java OpenGL 绑定(bind))渲染基本模型。我试图尽可能利用我所记得的知识来完成这件事。我创建了一个像这样的vbo:

    int verticesVBO = GL15.glGenBuffers ( );
    vboIDs.add ( verticesVBO );
    FloatBuffer verticesData = bufferFromData ( vertices );// Custom Method
    GL15.glBindBuffer ( GL15.GL_ARRAY_BUFFER , verticesVBO );
    GL15.glBufferData ( GL15.GL_ARRAY_BUFFER , verticesData , GL15.GL_STATIC_DRAW );
    GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);// Binds the vbo to the bound vao
    if(( error = GL11.glGetError()) != GL11.GL_NO_ERROR) System.err.println(GLU.gluErrorString(error));

我已经了解了索引缓冲区对象:

    int indicesVBO = GL15.glGenBuffers ( );
    vboIDs.add ( verticesVBO );
    IntBuffer indicesData = bufferFromData ( indices );
    GL15.glBindBuffer ( GL15.GL_ELEMENT_ARRAY_BUFFER , indicesVBO );
    GL15.glBufferData ( GL15.GL_ELEMENT_ARRAY_BUFFER , indicesData , GL15.GL_STATIC_DRAW );
    //Problem Here
    if(( error = GL11.glGetError()) != GL11.GL_NO_ERROR) System.err.println(GLU.gluErrorString(error));

我遇到的问题是我不知道将索引缓冲区绑定(bind)到 vao 的方法。对于包含顶点数据的 vbo,我知道使用 GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);但我记得索引缓冲区的行为不同。这是一个学习过程,因此请提出建设性的批评。

最佳答案

您需要做的就是在绑定(bind) VAO 的同时绑定(bind)索引缓冲区。

参见https://www.opengl.org/wiki/Buffer_Object#General_use :

GL_ELEMENT_ARRAY_BUFFER

All rendering functions of the form gl*Draw*Elements*​ will use the pointer field as a byte offset from the beginning of the buffer object bound to this target. The indices used for indexed rendering will be taken from the buffer object. Note that this binding target is part of a Vertex Array Objects state, so a VAO must be bound before binding a buffer here.

关于java - 如何设置索引缓冲区对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31888404/

相关文章:

java - 将 java 游戏导出到 .jar 时出现问题

java - 如何在 lwjgl/slick2D 中使用系统鼠标光标(如 Cursor.TEXT_CURSOR)

java - 将自定义 HTTP header 添加到 BlazeDS 和 AMF 发送的请求

java - 将嵌套的 ArrayList 转换为 List Java

c++ - glfw openGL c++ 窗口背景和标题

c++ - 使用帧缓冲区进行高斯模糊 2 pass

java - 实体类可以在没有对表的所有列进行定义的情况下工作吗

java - 数据加载期间表中的 SQL 动态列处理

haskell - Haskell 程序中的 GLUT 错误

java - OpenGL VAO 多个 VBO 绑定(bind)问题