【发布时间】:2014-03-19 02:42:36
【问题描述】:
我正在为我的应用程序开发一个 .obj 加载器,为此,我设置了一个索引数组和一个顶点数组并在运行时初始化它们。
这是定义结构的方式:
typedef struct {
float Position[3];
float Color[4];
float TexCoord[2];
} Vertex;
typedef struct {
GLuint v1;
GLuint v2;
GLuint v3;
} Face3D;
这就是数组在 .h 文件中的样子:
Vertex* VerticesArr;
Face3D* Faces;
但是当我初始化它们时,我的屏幕上什么也看不到,而不是使用这种方式:
const Vertex Vertices[] = {
{{1, -1, 0}, {1, 0, 0, 1},{0,0}},
{{1, 1, 0}, {0, 1, 0, 1},{0,0}},
{{-1, 1, 0}, {0, 0, 1, 1},{0,0}},
{{-1, -1, 0}, {0, 0, 0, 1},{0,0}}
};
const GLubyte Indices[] = {
0, 1, 2,
2, 3, 0
};
通过使用这些 const 数组,我可以在屏幕上绘制图形。
我的问题是,是否可以像我一样使用索引和顶点数组? 我相信是的,它似乎是错误的。
这就是我在前面介绍的两种方式上设置我的 VBO 的方式:
- (void)setupVBOs {
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(VerticesArr), VerticesArr, GL_STATIC_DRAW);
GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Faces), Faces, GL_STATIC_DRAW);
}
以及我使用绘图元素的方式:
glDrawElements(GL_POINTS, sizeof(Faces)/sizeof(Faces[0]), GL_UNSIGNED_INT, 0);
我确实检查了两个数组是否都按照应有的方式初始化,并且确实如此。
这就是我为他们分配空间的方式:
// Allocate space for the Vertices array
NSLog(@"Size of vertice is: %lu",sizeof(Vertex));
VerticesArr = (Vertex*)(malloc(sizeof(Vertex) * vertexCombinations.count));
// Allocate space for the Faces
NSLog(@"Size of face is: %lu",sizeof(Face3D));
Faces = (Face3D*)(malloc(sizeof(Face3D)*faceCount));
【问题讨论】:
标签: ios iphone opengl-es indices