PDA

View Full Version : OpenGL Texture on height map



michelone
05-06-2012, 06:17 AM
i'm working on the rendering of an height map terrain and everything works fine except the texture. I have to stretch a jpg file over the whole terrain (using GLM) but it doesn't work! This is part of my code, just to be clear (Terreain.cpp):

GLfloat height, width;

int CreateTerrain::getTextureID(){
GLuint textID = glmLoadTexture("terrain.png",GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE,&width, &height);
return textID;
}

float* CreateTerrain::getTexture(){
int j = 0;
float iF = (width/mapCols)/width;
float jF = (height/mapRows)/height;
for ( int row = 0; row < mapRows; row++ ) {
for ( int col = 0; col < mapCols; col++ ) {
terrainTexture[j++] = (float) row*iF;
terrainTexture[j++] = (float) col*jF;
}
}
return terrainTexture;
}
and this is the file main.cpp:


GLuint textID = terrain.getTextureID();
float* texture = terrain.getTexture();

void RenderTerrain(){
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textID);
glEnable(GL_COLOR_MATERIAL);
glTranslatef( -(MAP_COLS*CELL_SIZE)/2, 0, -(MAP_ROWS*CELL_SIZE)/2);
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer( 3, GL_FLOAT, 0, vertices );
glNormalPointer(GL_FLOAT, 0, normals );
glTexCoordPointer(2,GL_FLOAT,0,texture);
for ( int row = 0; row < MAP_ROWS - 1; row++ ) {
int i = 0;
for ( int col = 0; col < MAP_COLS; col++ ) {
indices[i++] = col + (row * MAP_COLS);
indices[i++] = col + (row * MAP_COLS) + MAP_COLS;
}
glPushMatrix();
glDrawElements( GL_TRIANGLE_STRIP, i, GL_UNSIGNED_INT, indices );
glPopMatrix();
}
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState (GL_TEXTURE_COORD_ARRAY);}

I read a lot of tutorial but i didn't find the solution. Every tip is really appreciated! Thanks in advance!

Dan Bartlett
05-06-2012, 06:22 AM
I don't know whether you've just made a copy-paste error in this post, but you never assign a value to _textureId (only to textID)

michelone
05-06-2012, 06:25 AM
yes now i fixed the error... but i can only see white terrain without my jpg texture!