简体   繁体   English

OpenGL ES:如何将 VAO 与 glVertexAttribPointer 一起使用?

[英]OpenGL ES : How to use VAOs together with glVertexAttribPointer?

I am trying to update the following code, using VAOs :我正在尝试使用 VAO 更新以下代码:

fun onSurfaceCreated() {
    // ...

    // Coordinates Handle
    this.meshCoordinatesHandle = GLES31.glGetAttribLocation(program, "mesh_coordinates")
    GLES31.glEnableVertexAttribArray(this.meshCoordinatesHandle)

    // Color Handle
    this.colorHandle = GLES31.glGetUniformLocation(program, "color")
    GLES31.glEnableVertexAttribArray(this.colorHandle)
}

fun onDrawFrame() {
    // ...

    // Mesh A
    GLES31.glVertexAttribPointer(this.meshCoordinatesHandle, 3, GLES31.GL_FLOAT, false, 3 * 4, this.meshACoordinatesBuffer)
    GLES31.glUniform4fv(this.colorHandle, 1, this.meshAColor, 0)
    GLES31.glDrawElements(GLES31.GL_LINES, this.meshALinesBuffer.capacity(), GLES31.GL_UNSIGNED_SHORT, this.meshALinesBuffer)

    // Mesh B
    GLES31.glVertexAttribPointer(this.meshCoordinatesHandle, 3, GLES31.GL_FLOAT, false, 3 * 4, this.meshBCoordinatesBuffer)
    GLES31.glUniform4fv(this.colorHandle, 1, this.meshBColor, 0)
    GLES31.glDrawElements(GLES31.GL_LINES, this.meshBLinesBuffer.capacity(), GLES31.GL_UNSIGNED_SHORT, this.meshBLinesBuffer)
}

I got this far, but i'm stuck here :我走了这么远,但我被困在这里:

fun onSurfaceCreated() {
    // ...

    GLES31.glGenVertexArrays(2, vaos, 0)

    // VAO A
    GLES31.glBindVertexArray(vaos[0])

    GLES31.glGetAttribLocation(program, "mesh_coordinates").also {
    GLES31.glEnableVertexAttribArray(it)
    GLES31.glVertexAttribPointer(it, 3, GLES31.GL_FLOAT, false, 3 * 4, this.meshACoordinatesBuffer)
    }

    GLES31.glGetUniformLocation(program, "color").also {
    GLES31.glEnableVertexAttribArray(it)
    GLES31.glUniform4fv(it, 1, this.meshAColor, 0)
    }

    // VAO B
    GLES31.glBindVertexArray(vaos[1])

    GLES31.glGetAttribLocation(program, "mesh_coordinates").also {
    GLES31.glEnableVertexAttribArray(it)
    GLES31.glVertexAttribPointer(it, 3, GLES31.GL_FLOAT, false, 3 * 4, this.meshBCoordinatesBuffer)
    }

    GLES31.glGetUniformLocation(program, "color").also {
    GLES31.glEnableVertexAttribArray(it)
    GLES31.glUniform4fv(it, 1, this.meshBColor, 0)
    }
}

fun onDrawFrame() {
    // ...

    // Mesh A
    GLES31.glBindVertexArray(vaos[0])
    GLES31.glDrawElements(GLES31.GL_LINES, this.meshALinesBuffer.capacity(), GLES31.GL_UNSIGNED_SHORT, this.meshALinesBuffer)

    // Mesh B
    GLES31.glBindVertexArray(vaos[1])
    GLES31.glDrawElements(GLES31.GL_LINES, this.meshBLinesBuffer.capacity(), GLES31.GL_UNSIGNED_SHORT, this.meshBLinesBuffer)
}

Any idea where the problem could come from ?知道问题可能来自哪里吗? Do I have to replace glVertexAttribPointer by glBindBuffer and glBufferData ?我必须用glBindBufferglBufferData替换glVertexAttribPointer吗?

Uniforms are stored in the default uniform block of the currently installed program.制服存储在当前安装程序的默认制服块中。 A uniforms is not a state of the VAO.制服不是VAO的状态。 Uniforms are part of the program, but not part of the attributes.制服是程序的一部分,但不是属性的一部分。 If you want to organize the uniforms you can use Uniform Buffer Object or Shader Storage Buffer Objects .如果要组织制服,可以使用Uniform Buffer ObjectShader Storage Buffer Objects

Set the uniform before drawing the mesh:在绘制网格之前设置均匀:

fun onSurfaceCreated() {
    // [...]

    GLES31.glGenVertexArrays(2, vaos, 0)

    // VAO A
    GLES31.glBindVertexArray(vaos[0])
    GLES31.glGetAttribLocation(program, "mesh_coordinates").also {
       GLES31.glEnableVertexAttribArray(it)
       GLES31.glVertexAttribPointer(it, 3, GLES31.GL_FLOAT, false, 3 * 4, this.meshACoordinatesBuffer)
    }

    // VAO B
    GLES31.glBindVertexArray(vaos[1])
    GLES31.glGetAttribLocation(program, "mesh_coordinates").also {
       GLES31.glEnableVertexAttribArray(it)
       GLES31.glVertexAttribPointer(it, 3, GLES31.GL_FLOAT, false, 3 * 4, this.meshBCoordinatesBuffer)
    }
}

fun onDrawFrame() {
    // [...]

    // Mesh A
    GLES31.glGetUniformLocation(program, "color").also {
       GLES31.glUniform4fv(it, 1, this.meshAColor, 0)
    }
    GLES31.glBindVertexArray(vaos[0])
    GLES31.glDrawElements(GLES31.GL_LINES, this.meshALinesBuffer.capacity(), GLES31.GL_UNSIGNED_SHORT, this.meshALinesBuffer)

    // Mesh B
    GLES31.glGetUniformLocation(program, "color").also {
       GLES31.glUniform4fv(it, 1, this.meshBColor, 0)
    }
    GLES31.glBindVertexArray(vaos[1])
    GLES31.glDrawElements(GLES31.GL_LINES, this.meshBLinesBuffer.capacity(), GLES31.GL_UNSIGNED_SHORT, this.meshBLinesBuffer)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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