我可以使用着色器,glVertexAttribPointer和glDrawArrays显示纹理,如下所示:
初始化
const GLfloat squareVertices[] = {
-0.5f, -0.33f,
0.5f, -0.33f,
-0.5f, 0.33f,
0.5f, 0.33f
};
const GLfloat squareTex[] = {
0, 0,
1, 0,
0, 1,
1, 1
};
glEnableVertexAttribArray(PositionTag);
glEnableVertexAttribArray(TexCoord0Tag);
glVertexAttribPointer(PositionTag, 2, GL_FLOAT, GL_FALSE, 0, squareVertices);
glVertexAttribPointer(TexCoord0Tag, 2, GL_FLOAT, GL_FALSE, 0, squareTex);
和抽签
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
但是我很难转换为VBO,着色器和glDrawElements。这是我到目前为止的代码,但没有显示:
标题
typedef struct MyVertex
{
float x, y, z; //Vertex
float nx, ny, nz; //Normal
float s0, t0; //Texcoord0
} MyVertex;
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
初始化
glGenBuffers(1, &VertexVBOID);
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
MyVertex pvertices[4];
//Fill the pvertices array
pvertices[0].x = -0.5f;
pvertices[0].y = -0.33f;
pvertices[0].z = 0.0;
pvertices[0].nx = 0.0;
pvertices[0].ny = 0.0;
pvertices[0].nz = 1.0;
pvertices[0].s0 = 0.0;
pvertices[0].t0 = 0.0;
pvertices[1].x = 0.5f;
pvertices[1].y = -0.33f;
pvertices[1].z = 0.0;
pvertices[1].nx = 0.0;
pvertices[1].ny = 0.0;
pvertices[1].nz = 1.0;
pvertices[1].s0 = 1.0;
pvertices[1].t0 = 0.0;
pvertices[2].x = -0.5f;
pvertices[2].y = 0.33f;
pvertices[2].z = 0.0;
pvertices[2].nx = 0.0;
pvertices[2].ny = 0.0;
pvertices[2].nz = 1.0;
pvertices[2].s0 = 0.0;
pvertices[2].t0 = 1.0;
pvertices[3].x = 0.5f;
pvertices[3].y = 0.33f;
pvertices[3].z = 0.0;
pvertices[3].nx = 0.0;
pvertices[3].ny = 0.0;
pvertices[3].nz = 1.0;
pvertices[3].s0 = 1.0;
pvertices[3].t0 = 1.0;
glBufferData(GL_ARRAY_BUFFER, sizeof(MyVertex)*4, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(MyVertex)*4, pvertices);
glGenBuffers(1, &IndexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
int pindices[6];
pindices[0]=0;
pindices[1]=1;
pindices[2]=2;
pindices[3]=2;
pindices[4]=1;
pindices[5]=3;
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*6, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(int)*6, pindices);
绘制
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
glEnableVertexAttribArray(PositionTag);
glEnableVertexAttribArray(NormalTag);
glEnableVertexAttribArray(TexCoord0Tag);
glVertexAttribPointer(PositionTag, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(0));
glVertexAttribPointer(NormalTag, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(12));
glVertexAttribPointer(TexCoord0Tag, 2, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(24));
// glDrawRangeElements(GL_TRIANGLES, x, y, z, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
glDrawElements(GL_TRIANGLES, 3, GL_INT, 0);
答案 0 :(得分:4)
根据here,GL_INT
不是glDrawElements
中用于索引的有效类型。尝试使用unsigned int
s作为索引(当然GL_UNSIGNED_INT
中的glDrawElements
)。您仍然可以使用int
数据作为索引,但由于glDrawElements
需要GL_UNSIGNED_INT
,因此制作数组unsigned int
会更加一致。
编辑:在查看规范后(根据您的标签,我采用了ES 2.0规范),它们似乎进一步将其限制为无符号字节和无符号短路。我不知道它是否在iOS中有限,但我们可以得出结论,数据至少是无符号的。另一方面,我没有找到关于错误类型参数引发的可能GL_INVALID_ENUM
错误的任何声明,但得到一个是合理的。
答案 1 :(得分:1)
你的代码看起来并不严重,所以这一次魔鬼在细节中的某个地方。我的猜测是,你使用结构及其数据字段的对齐方式与传递给OpenGL的偏移量不匹配。
我建议您使用 stddef.h 中的offsetof()
宏来轻松获取数据字段的偏移量。