Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: OpenGL Texture on height map

  1. #1
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    2

    OpenGL Texture on height map

    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!
    Last edited by michelone; 05-06-2012 at 06:26 AM.

  2. #2
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    433
    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)

  3. #3
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    2
    yes now i fixed the error... but i can only see white terrain without my jpg texture!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •