void Terrain::Init()
{
// used to track current entry in the index array
int index = 0;
int currentVertex;
// loop over all vertices in the terrain map
for (int z = 0; z < MAP_Z; z++)
{
for (int x = 0; x < MAP_X; x++)
{
// vertices are numbered left to right, top to bottom
currentVertex = z * MAP_X + x;
// set the values in the color array
g_color[currentVertex][0] = g_color[currentVertex][1] =
g_color[currentVertex][2] = g_terrain[x + MAP_X * z][1]/255.0f;
// set the values in the texture coordinate array. since the texture
// is tiled over each "square", we can use texture wrapping
g_texcoord[currentVertex][0] = (float) x;
g_texcoord[currentVertex][1] = (float) z;
}
}
for (z = 0; z < MAP_Z - 1; z++)
{
for (int x = 0; x < MAP_X; x++)
{
currentVertex = z * MAP_X + x;
g_index[index++] = currentVertex + MAP_X;
g_index[index++] = currentVertex;
}
}
// enable the vertex arrays being used
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// pass the pointers to OpenGL
glVertexPointer(3, GL_FLOAT, 0, g_terrain);
glColorPointer(3, GL_FLOAT, 0, g_color);
glTexCoordPointer(2, GL_FLOAT, 0, g_texcoord);
}
void Terrain::Render()
{
// loop through all the triangle strips
for (int z = 0; z < MAP_Z-1; z++)
{
// draw the triangles in this strip
glDrawElements(GL_TRIANGLE_STRIP, MAP_X * 2, GL_UNSIGNED_INT, &g_index[z * MAP_X * 2]);
}
}
vertex array question
when i have the arrays containing the vertices, colors and texture coordinates, indices as a global everything works.
Now i try to move everything in a class and it doesn''t work anymore. I can''t figure out why.
this is the code:
Thanks for your help,
airo
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement