简体   繁体   中英

OpenGL ES : How to use VAOs together with glVertexAttribPointer?

I am trying to update the following code, using VAOs :

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 ?

Uniforms are stored in the default uniform block of the currently installed program. A uniforms is not a state of the 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 .

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

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