1
votes

Now I have two custom shader, the position vertex shader and the color fragment shader. And I define two array data, position Array and color Array. This can make the gradient triangel in the iPhone. I didn't call the glBindBuffer() and glBufferData() for the VAO. but in the glVertexAttribPointer(),I send the position array to its last attribute and the color array too. Now, I feel confused,I found most documents explain the last attribute in this function, it is offset in vertex data array. now I send it to the array data and did not call the glBindBuffer() and glBufferData(), but it take effect。

lazy var postions : [GLfloat] = [
    -0.5, -0.5, 0.0,
    0.5, -0.5, 0.0,
    0.0,  0.5, 0.0,
    ]

lazy var colors : [GLfloat] = [
    1, 0, 0,
    0, 1, 0,
    0, 0, 1,
    ]

and this is the code about glVertexAttribPointer

let posistionAtt : GLuint = GLuint(glGetAttribLocation(shaderProgram, "position"))
    glVertexAttribPointer(posistionAtt, 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(3 * MemoryLayout<GLfloat>.size), postions)
    glEnableVertexAttribArray(posistionAtt)

    let ptr = UnsafePointer<Int>.init(bitPattern: 3 * MemoryLayout<GLfloat>.size)
    let colorAtt : GLuint = GLuint(glGetAttribLocation(shaderProgram, "color"))
    glVertexAttribPointer(colorAtt, 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(3 * MemoryLayout<GLfloat>.size), colors)
    glEnableVertexAttribArray(colorAtt)

the result for the gradient triangel in the iPhone

1

1 Answers

1
votes

If no vertex buffer object is bound, then the last parameter is treated as a pointer to the data.

See OpenGL ES 2.0 specification; 2.8. VERTEX ARRAYS; 21:

void VertexAttribPointer( uint index, int size, enum type, 
                          boolean normalized,  sizei stride, 
                          const void *pointer );

... For each command, pointer specifies the location in memory of the first value of the first element of the array being specified.

See OpenGL ES 2.0 specification; 2.9. BUFFER OBJECTS; 25:

... When an array is sourced from a buffer object, the pointer value of that array is used to compute an offset, in basic machine units, into the data store of the buffer object. ...

This means, in case of

glBindBuffer(GL_ARRAY_BUFFER, 0)
glVertexAttribPointer(...., pointer)

pointer has to be a pointer to the vertex array data - the array of vertex array data.

But in case of

glBindBuffer(GL_ARRAY_BUFFER, vbo)  // vbo != 0
glVertexAttribPointer(...., offset)

offset has to be an offset, to the data store of vbo.