OpenGL ES级联2D网格平铺与纹理

时间:2013-07-09 18:58:56

标签: textures blackberry-10 cascade vbo opengl-es-1.1

我正在为黑莓10 OS开发导航系统的自定义地图。到目前为止,我已经设置了级联来使用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);

   }

在阅读一些使用Vertex缓冲区对象和索引的文章以及缓冲纹理之后,我是OpenGL的新手,因为它大大提高了性能,因此是一个更好的解决方案。请指导如何使用VBO进行网格平铺并将纹理映射到C ++中的网格。示例代码或任何教程将更好地说明。如果我的帖子中的代码订购不当,请耐心等待。

感谢。

修改

通过阅读ios documentattion和VBO - 只是来自OpenGL的例子,我做了以下内容,就像我将在这里发布的代码一样。我现在遇到的问题是VBO不会渲染任何与顶点数组相反的东西会按预期渲染。

下面是创建我的VBO的方法和用于绘制顶点和索引的结构的代码,在initialize()中调用createVertexBuffers(),在render()中调用drawTileGrids()。

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);
   }

0 个答案:

没有答案