I am developing a custom map for navigation systems on blackberry 10 OS. So far I have setup cascade to work with OpenGL ES 1.1. At the Moment, I can draw a couple of tiles in a loop as shown below.
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);
}
I am newbie to OpenGL after reading some articles online working with Vertex Buffer Objects and Indices as well as buffering Textures is a far better solution as it greatly improves performance. Please kindly guide on how to use VBO for grid tiling and mapping textures to grids in C++. A sample code or any tutorial will illustrate better. Please bear with me if the codes in my post is not well ordered.
Thanks.
Edit
After reading through ios documentattion and VBO - just examples from OpenGL I did the following as in the code i will post here. The problem I have now is that VBO does not render anything as oppose to vertex array will render as expected.
Below are the methods for creating my VBOs and code for drawing and structs for vertices and indices as well, createVertexBuffers() is called in initialize() and drawTileGrids() is called in 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);
}