glVertexAttribPointer() last at OpenGL 中的属性值可以是一个数组吗?
Could glVertexAttribPointer() last at attribute value in OpenGL be a array ?
现在我有两个自定义着色器,位置顶点着色器和颜色片段着色器。并且我定义了两个数组数据,位置数组和颜色数组。这样就可以做出iPhone中的渐变三角形了。
我没有为 VAO 调用 glBindBuffer() 和 glBufferData()。但是在 glVertexAttribPointer() 中,我将位置数组发送到它的最后一个属性和颜色数组。现在,我感到困惑,我发现大多数文档都解释了这个函数中的最后一个属性,它是顶点数据数组中的偏移量。现在我将它发送到数组数据并没有调用glBindBuffer()和glBufferData(),但是它生效了。
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,
]
这是关于 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
如果没有绑定顶点缓冲区对象,则最后一个参数被视为指向数据的指针。
见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.
见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. ...
这意味着,如果
glBindBuffer(GL_ARRAY_BUFFER, 0)
glVertexAttribPointer(...., pointer)
pointer
必须是指向顶点数组数据的指针 - 顶点数组数据的数组。
但万一
glBindBuffer(GL_ARRAY_BUFFER, vbo) // vbo != 0
glVertexAttribPointer(...., offset)
offset
必须是 vbo
的数据存储的偏移量。
现在我有两个自定义着色器,位置顶点着色器和颜色片段着色器。并且我定义了两个数组数据,位置数组和颜色数组。这样就可以做出iPhone中的渐变三角形了。 我没有为 VAO 调用 glBindBuffer() 和 glBufferData()。但是在 glVertexAttribPointer() 中,我将位置数组发送到它的最后一个属性和颜色数组。现在,我感到困惑,我发现大多数文档都解释了这个函数中的最后一个属性,它是顶点数据数组中的偏移量。现在我将它发送到数组数据并没有调用glBindBuffer()和glBufferData(),但是它生效了。
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,
]
这是关于 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
如果没有绑定顶点缓冲区对象,则最后一个参数被视为指向数据的指针。
见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.
见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. ...
这意味着,如果
glBindBuffer(GL_ARRAY_BUFFER, 0)
glVertexAttribPointer(...., pointer)
pointer
必须是指向顶点数组数据的指针 - 顶点数组数据的数组。
但万一
glBindBuffer(GL_ARRAY_BUFFER, vbo) // vbo != 0
glVertexAttribPointer(...., offset)
offset
必须是 vbo
的数据存储的偏移量。