我想在我的Open GL ES 1.1游戏中使用顶点缓冲对象(VBOs)来改进我的复杂模型渲染。在阅读了SO和这篇(http://playcontrol.net/ewing/jibberjabber/opengl_vertex_buffer_object.html)教程的几篇文章之后,我仍然无法理解VBO以及如何在我的Cheetah 3D导出模型格式下实现它们。有人能给我举一个实现VBO的例子,并用它来绘制给定数据结构的顶点并解释语法吗?我非常感谢任何帮助!iPhone Cheetah 3D OpenGL ES顶点缓冲区对象(VBO)示例
#define body_vertexcount 434
#define body_polygoncount 780
// The vertex data is saved in the following format:
// u0,v0,normalx0,normaly0,normalz0,x0,y0,z0
float body_vertex[body_vertexcount][8]={
{0.03333, 0.00000, -0.68652, -0.51763, 0.51063, 0.40972, -0.25028, -1.31418},
{...},
{...}
}
GLushort body_index[body_polygoncount][3]={
{0, 1, 2},
{2, 3, 0}
}
我在Pro OpenGL ES(Appress)的第9章的帮助下编写了下面的代码。我得到EXE_BAD_ACCESS与DrawElements命令,我不知道为什么。有人可以摆脱一些光线吗?谢谢 -
// First thing we do is create/setup the index buffer
glGenBuffers(1, &bodyIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bodyIBO);
// For constrast, instead of glBufferSubData and glMapBuffer,
// we can directly supply the data in one-shot
glBufferData(GL_ELEMENT_ARRAY_BUFFER, body_polygoncount*sizeof(GLubyte), body_index, GL_STATIC_DRAW);
// Define our data structure
int numXYZElements = 3;
int numNormalElements = 3;
int numTextureCoordElements = 2;
long totalXYZBytes;
long totalNormalBytes;
long totalTexCoordinateBytes;
int numBytesPerVertex;
// Allocate a new buffer
glGenBuffers(1, &bodyVBO);
// Bind the buffer object to use
glBindBuffer(GL_ARRAY_BUFFER, bodyVBO);
// Tally up the size of the data components
numBytesPerVertex = numXYZElements;
numBytesPerVertex += numNormalElements;
numBytesPerVertex += numTextureCoordElements;
numBytesPerVertex *= sizeof(GLfloat);
// Actually allocate memory on the GPU (Data is static here)
glBufferData(GL_ARRAY_BUFFER, numBytesPerVertex * body_vertexcount, 0, GL_STATIC_DRAW);
// Upload data to the cache (memory mapping)
GLubyte *vboBuffer = (GLubyte *)glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES);
// Caclulate the total number of bytes for each data type
totalXYZBytes = numXYZElements * body_vertexcount * sizeof(GLfloat);
totalNormalBytes = numNormalElements * body_vertexcount * sizeof(GLfloat);
totalTexCoordinateBytes = numTextureCoordElements * body_vertexcount * sizeof(GLfloat);
// Set the total bytes property for the body
self.bodyTotalBytes = totalXYZBytes + totalNormalBytes + totalTexCoordinateBytes;
// Setup the copy of the buffer(s) using memcpy()
memcpy(vboBuffer, body_vertex, self.bodyTotalBytes);
// Perform the actual copy
glUnmapBufferOES(GL_ARRAY_BUFFER);
这里是哪里,我发现了异常的绘图命令:
// Activate the VBOs to draw
glBindBuffer(GL_ARRAY_BUFFER, bodyVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bodyIBO);
// Setup drawing
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glClientActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,lightGreyInt);
// Setup pointers
glVertexPointer(3, GL_FLOAT, sizeof(vertexStruct), (char *)NULL + 0);
glTexCoordPointer(2, GL_FLOAT, sizeof(vertexStruct), (char *)NULL + 12);
glNormalPointer(GL_FLOAT, sizeof(vertexStruct), (char *)NULL + 24);
// Now draw the body
glDrawElements(GL_TRIANGLES, body_polygoncount,GL_UNSIGNED_SHORT, (GLvoid*)((char*)NULL));
//glDrawElements(GL_TRIANGLES, body_polygoncount, GL_UNSIGNED_SHORT, nil);
//glDrawElements(GL_TRIANGLES,body_polygoncount*3,GL_UNSIGNED_SHORT,body_index);
我不能相信这是说明这个问题非常好给你,假设你已经知道如何顶点数组和OpenGL的其余产品(其中你的** **你的OpenGL ES 1.1的游戏建议提被发现没有学习资源)。你也可以看[这里](http://www.songho.ca/opengl/gl_vbo.html),但是从你的问题链接的教程看起来是合理的(我只浏览了它,虽然)。 – 2012-03-18 15:24:36
你有什么麻烦?我们不只是为你编写代码。数据结构和语法怎么样,你不明白吗?只要尽可能明确,我们就可以制定出合理的答案。 – user1118321 2012-03-18 15:52:02
我贴我的代码,我特别有麻烦呈现交织的数据与glDrawElements调用。我得到EXC_BAD_ACCESS,我会很感激一些帮助 - – PhilBot 2012-03-18 19:09:24