我正在为黑莓 10 操作系统上的导航系统开发自定义地图。到目前为止,我已经设置了级联以使用 OpenGL ES 1.1。目前,我可以在一个循环中绘制几个图块,如下所示。
void MapView::render() {
//Typical render pass
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//First render background and menu if it is enabled
glViewport(0, 0, (int) screenWidth, (int) screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0.0f, screenWidth / screenHeight, 0.0f, 1.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(1.0f / screenHeight, 1.0f / screenHeight, 1.0f);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
for (i = 0.0; i <= 100.0, i += 256.0){
for (j = 0.0; j <= 100.0, j += 256.0){
drawTile(image_texure, i, j, 0.0f, 256.0f, 1.0f, 1.0f);
}
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
//Use utility code to update the screen
swapBuffers();
}
void MapView::drawTile(GLuint tile_texture, float x, float y, float z,floatd,float tex_x, float tex_y)
{
//vertices for the grid.
GLfloat gridVertices[] = {x,y + d,z, x + d,y + d,z, x + d, y, z, x,y,z};
//coordinates for the tile testure.
GLfloat tex_coord[] = {0.0f, tex_y, 0.0f, 0.0f, tex_x, tex_y, tex_x, 0.0f};
glVertexPointer(3, GL_FLOAT, 0, gridVertices);
glTexCoordPointer(2, GL_FLOAT, 0, tex_coord);
glBindTexture(GL_TEXTURE_2D, tile_texture);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
在阅读了一些在线使用顶点缓冲区对象和索引以及缓冲纹理的文章后,我是 OpenGL 的新手,这是一个更好的解决方案,因为它大大提高了性能。请指导如何使用 VBO 进行网格平铺并将纹理映射到 C++ 中的网格。示例代码或任何教程将更好地说明。如果我帖子中的代码没有很好地排序,请多多包涵。
谢谢。
编辑
在阅读了 ios 文档和 VBO 之后——只是来自 OpenGL 的示例,我按照我将在此处发布的代码中的内容执行了以下操作。我现在遇到的问题是 VBO 不会渲染任何东西,因为顶点数组将按预期渲染。
下面是创建我的 VBO 的方法和用于绘制顶点和索引的结构的代码,createVertexBuffers() 在 initialize() 中调用,drawTileGrids() 在 render() 中调用。
typedef struct
{
float x, y, z; //vertex
} VertexStruct;
typedef struct
{
float tex_x, tex_y; //texture coordinates
} TexStruct;
typedef struct
{
VertexStruct position;
TexStruct texCoord;
} MeshVertex;
float d = 256.0;
MeshVertex meshVertices [] =
{
{{0.0, 0.0 + d, 0.0} , {0.0, 1.0}},
{{0.0, 0.0, 0.0} , {0.0, 0.0}},
{{0.0 + d, 0.0 + d, 0.0} , {1.0, 1.0}},
{{0.0 + d, 0.0, 0.0} , {1.0, 0.0}}
};
GLushort meshIndices [] =
{
0, 1, 2, 3
};
void MapView::createVertexBuffers()
{
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(meshVertices), meshVertices, GL_STATIC_DRAW);
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(meshIndices), meshIndices, GL_STATIC_DRAW);
qDebug() << "Vertex buffers created. ";
}
void MapView::drawTileGrids()
{
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexPointer(3, GL_FLOAT, sizeof(VertexStruct), BUFFER_OFFSET(0));
glClientActiveTexture(GL_TEXTURE0);
//The starting point of texcoords, 24 bytes away
glTexCoordPointer(2, GL_FLOAT, sizeof(VertexStruct), BUFFER_OFFSET(24));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBindTexture(GL_TEXTURE_2D, image_texure);
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));
//glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_BLEND);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//glDisable(GL_TEXTURE_2D);
}