Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Texture height map

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    26

    Texture height map

    I'm trying to load a texture over a height map and struggling. I've got my texture stored as a jpg and have loaded it. This is working as I've applied the texture to something else as a test, however I can't get it to work properly on my height map
    Code :
    public void renderHeightMap(GL gl, byte[] pheightMap) {
            gl.glBindTexture(GL.GL_TEXTURE_2D, land[0]);
            gl.glBegin(renderType);
            for(int X = 0; X < (MAP_SIZE - STEP_SIZE); X += STEP_SIZE) {
                for(int Y = 0; Y < (MAP_SIZE - STEP_SIZE); Y += STEP_SIZE) {
                    int x = X;
                    int y = height(pheightMap, X, Y);
                    int z = Y;
     
    //                setVertexColor(gl, pheightMap, x, z);
                    gl.glTexCoord2i(0,0);
                    gl.glVertex3i(x, y, z);
     
                    x = X;
                    y = height(pheightMap, X, Y + STEP_SIZE);
                    z = Y + STEP_SIZE;
     
    //                setVertexColor(gl, pheightMap, x, z);
                    gl.glTexCoord2i(1,0);
                    gl.glVertex3i(x, y, z);
     
                    x = X + STEP_SIZE;
                    y = height(pheightMap, X + STEP_SIZE, Y + STEP_SIZE);
                    z = Y + STEP_SIZE;
     
    //                setVertexColor(gl, pheightMap, x, z);
                    gl.glTexCoord2i(1,1);
                    gl.glVertex3i(x, y, z);
     
                    x = X + STEP_SIZE;
                    y = height(pheightMap, X + STEP_SIZE, Y);
                    z = Y;
     
    //                setVertexColor(gl, pheightMap, x, z);
                    gl.glTexCoord2i(0,1);
                    gl.glVertex3i(x, y, z);
                }
            }
            gl.glEnd();
            gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
        }

    That is the code used for rendering my height map. I've added the TexCoord commands and the texture is being applied but for every single square being drawn. I want to stretch the texture over the entire map.

    How?
    Thanks

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Texture height map

    The problem is the drawing loop is setting the same texture coordinates for each of the 4 corners of the square, on each iteration of the loop. Therefore each square is sending 0,1 as texture coords each time.
    You need to send in a fraction of the texture width/height on each iteration, for each square.

    Notice the use of floats for the texture coords:


    public void renderHeightMap(GL gl, byte[] pheightMap) {
    gl.glBindTexture(GL.GL_TEXTURE_2D, land[0]);
    gl.glBegin(renderType);
    for(int X = 0; X < (MAP_SIZE - STEP_SIZE); X += STEP_SIZE) {
    for(int Y = 0; Y < (MAP_SIZE - STEP_SIZE); Y += STEP_SIZE) {
    int x = X;
    int y = height(pheightMap, X, Y);
    int z = Y;

    // setVertexColor(gl, pheightMap, x, z);
    gl.glTexCoord2f(x/MAP_SIZE, z/MAP_SIZE);
    gl.glVertex3i(x, y, z);

    x = X;
    y = height(pheightMap, X, Y + STEP_SIZE);
    z = Y + STEP_SIZE;

    // setVertexColor(gl, pheightMap, x, z);
    gl.glTexCoord2f(x/MAP_SIZE, z/MAP_SIZE);
    gl.glVertex3i(x, y, z);

    x = X + STEP_SIZE;
    y = height(pheightMap, X + STEP_SIZE, Y + STEP_SIZE);
    z = Y + STEP_SIZE;

    // setVertexColor(gl, pheightMap, x, z);
    gl.glTexCoord2f(x/MAP_SIZE, z/MAP_SIZE);
    gl.glVertex3i(x, y, z);

    x = X + STEP_SIZE;
    y = height(pheightMap, X + STEP_SIZE, Y);
    z = Y;

    // setVertexColor(gl, pheightMap, x, z);
    gl.glTexCoord2f(x/MAP_SIZE, z/MAP_SIZE);
    gl.glVertex3i(x, y, z);
    }
    }
    gl.glEnd();
    gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    }

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    26

    Re: Texture height map

    Cheers. Now however it's not rendering any texture? The map just comes out a slightly greyish colour?

  4. #4
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Texture height map

    It may be the winding order of the polygons. Try disabling cullface or change the winding order to test this.

    gldisable (GL_CULL_FACE)

    If this is the case, you'll need to reorder the texture coordinates to match the winding order.

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    26

    Re: Texture height map

    Still nothing. Just a blank height map

  6. #6
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Texture height map

    Have you changed any GL state since the change to the texture coordinates?
    eg Disabled GL_TEXTURE_2D, or enabled GL_TEXTURE_3D
    (You could always put back the original code and check).

    What's your graphic h/w ?
    What colour are you expecting for the texture? Don't forget that the texture will appear very S T R E A T C H E D !!

  7. #7
    Junior Member Newbie
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    26

    Re: Texture height map

    I've enabled texture in the init method so that should be fine. I've got a skybox around everything and the textures are loading into that without any issue.

    The graphics chip is the NVidia 8600M GT so should be more than capable of handling it.

    I made the height map in Terragen and created a texture on there which I've taken and am trying to overlay, so it should be a birds eye view image. I've tested it by loading it to the roof the the skybox which works fine but then this won't load. It simply changes the colour of the height map but not an image

  8. #8
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Texture height map

    Quote Originally Posted by Adrian Hooper
    I've tested it by loading it to the roof the the skybox which works fine but then this won't load. It simply changes the colour of the height map but not an image
    I don't understand what you're saying here.
    What happens if you put back your 0 1 texture coords ?
    All I have done is replace integer tex coords for float coords in the range 0..1. Your GeForce is way more than capable...

  9. #9
    Junior Member Newbie
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    26

    Re: Texture height map

    That's why I'm confused. When I had the 0 and 1 coords, every little rectangle being produced had the entire texture in rather than stretching over the entire thing. When the coords were changed to the float values it seems to do nothing.

    The skybox is made up of 6 textures. If I replace one of these textures for the one I'm trying to use for the height map, it loads and can be seen. But if I try it on this height map it doesn't work.

  10. #10
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Texture height map

    Ok so we have established there is no probs displaying the texture...so it has to be the generated texture coordinates for each square.

    Change each of the vertex from
    gl.glVertex3i(x, y, z);

    to

    gl.glVertex3f(x, y, z);

    and see what happens.

Posting Permissions

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