gpt4 book ai didi

objective-c - 在 OpenGL es (Iphone) 中使用 Vertex Buffer Objects (VBO) 来提高性能

转载 作者:太空狗 更新时间:2023-10-30 03:52:13 24 4
gpt4 key购买 nike

我正在为显示旋转立方体的 iphone 编写一个简单的应用程序。我正在使用 glDrawElements (openGl es) 绘制立方体的三角形并旋转它。我注意到当我将立方体的大小增加到 100*100*100 体素时,显示性能会变差(澄清:我不画整个立方体,我只画它的轮廓(网格)。我通过在立方体上应用行进立方体算法得到网格的所有三角形......最终我得到像 120k 三角形的东西来绘制它们是由 40k 个顶点表示)...

为了绘制立方体,我保存了一个顶点数组、颜色数组和一个顶点索引数组。 indices 数组定义要绘制的顶点的三角形。它作为参数传递给 glDrawElements。

最近我发现了一种使用顶点缓冲区对象 (VBO) 绘制立方体的不同技术。我已经实现了它,但性能比以前的技术更差

这是我的代码,也许我犯了一个愚蠢的错误,任何改进建议都会受到欢迎:)

顺便说一句,我使用了以下文章作为引用:

http://playcontrol.net/ewing/jibberjabber/opengl_vertex_buffer_object.html http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-table-of.html

//all the 7 variables down are initialized by other function at the beginning 
GLushort* meshIndices; //array of indices (ushort)
MeshVertex* meshVertices; //array of vertices (floats)
Color3D* meshColors; //array of colors (floats)

int numberOfTriangles; //number of Triangle to draw the cube
int numberOfVertices; //number of all Vertices to draw the cube
int numberOfIndices; //number of all Indices to draw the cube, each 3 indices define 3 vertices which define 1 triangle
int numberOfColors; //number of colors used to draw the cube. each color is of tip Color3D

//in this function i initializing the VBOs
- (void) setupMeshVBOs {

glGenBuffers(1, &triangleVBO);
glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);

const GLsizeiptr vertex_size = numberOfVertices * sizeof(MeshVertex);
const GLsizeiptr color_size = numberOfColors * sizeof(Color3D);

glBufferData(GL_ARRAY_BUFFER, vertex_size + color_size, 0, GL_STATIC_DRAW);

GLvoid* vbo_buffer = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES);
memcpy(vbo_buffer, meshVertices, vertex_size);

GLbyte* temp = (GLbyte*)vbo_buffer;
temp += vertex_size;
memcpy((GLvoid*)temp, meshColors, color_size);

glUnmapBufferOES(GL_ARRAY_BUFFER);

glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)((char*)NULL));

glColorPointer(4, GL_FLOAT, 0, (GLvoid*)((char*)NULL+vertex_size));

glGenBuffers(1, &triangleIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangleIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numberOfIndices * sizeof(GLushort), meshIndices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

//this function is the one which draws the VBOs
- (void)drawView:(GLView*)view;
{

static GLfloat rot = 0.0;


glLoadIdentity();
glTranslatef(-1.0f,-2.0f,-20.0f);
glRotatef(rot,1.0f,1.0f,1.0f);
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangleIBO);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glDrawElements(GL_TRIANGLE_STRIP, numberOfIndices, GL_UNSIGNED_SHORT, (GLvoid*)((char*)NULL));

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

static NSTimeInterval lastDrawTime;
if (lastDrawTime)
{
NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
rot+=50 * timeSinceLastDraw;
}
lastDrawTime = [NSDate timeIntervalSinceReferenceDate];
}

最佳答案

首先,要绘制 100x100x100 的立方体 map ,您不应单独绘制每个立方体。如果有,比如说,连续六个盒子,那么你应该把它们画成一个长方体,总共有十二个三角形。任何被六边包围的立方体肯定不需要考虑。您应该应用类似的策略来显着减少几何数。

Apple 关于 GL 优化的建议是 here .摘要版本是您应该旨在使用具有对齐的交错数据的 VBO,使用最小的可接受类型。因此,隐含地,读取数据是一个瓶颈。使用两个单独的列表可能会使您的几何输入速率减半,而使用 float 可能会进一步减慢速度。

关于objective-c - 在 OpenGL es (Iphone) 中使用 Vertex Buffer Objects (VBO) 来提高性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7215309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com