OpenGL heightmap terrain render does not draw

657 views Asked by At

I have a bitmap map that created from a dted file. I want to use that bitmap and create a 3d terrain render. I am trying to do it with vertex vectors but I couldn't reach my goal at all. Here is my paintGL code:

void render::paintGL(){
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0,0,-100);
for(int z=0;z<dted.width()-1;z++){
    glBegin(GL_TRIANGLE_STRIP);
    for(int x = 0; x<dted.height()-1; x++){
        glColor3f(matrix[x][z], matrix[x][z], matrix[x][z]);
        glVertex3f(x, matrix[x][z], -z);
        //vertex0 ends here

        glColor3f(matrix[x+1][z], matrix[x+1][z], matrix[x+1][z]);
        glVertex3f(x+1, matrix[x+1][z], -z);
        //vertex1 ends here

        glColor3f(matrix[x][z+1], matrix[x][z+1], matrix[x][z+1]);
        glVertex3f(x, matrix[x][z+1], -z-1);
        //vertex2 ends here

        glColor3f(matrix[x+1][z+1], matrix[x+1][z+1], matrix[x+1][z+1]);
        glVertex3f(x+1, matrix[x+1][z+1], -z-1);
        //vertex3 ends here


    }
    glEnd();
}}

I had to write the code instead of copy-paste because of internet issues. So there may be things that I forgot but I did my best. Let me explain the variables and other things above:

dted is the dted image I created and I set boundaries from that images width and height values. matrix is a 2d matrix that holds the elevation values I get from the pre-created dted image pixels.

After all the code above I couldn't manage to render the terrain. I don't know if it renders the terrain but it just doesn't show me or it's not rendering at all. If you explain where is my mistake and suggest a solution for it, it would be great.

Sorry for the grammar mistakes if I did any, and I hope I did explain my problem well. If the code I provided is not sufficient, feel free to mention that and I can write the other parts down too.

Have a nice day.

Edit: Ok I got a fresh news over here. I have managed to draw a plain after getting over some control with mouse issues. However, when I try to get amplitude, it shows nothing. That because of the too much amplitude I guess, but when I divide it by a constant like 15 the same plain shows up again. Is this because of I used GL_TRIANGLE_STRIP ?

2

There are 2 answers

0
user3483948 On BEST ANSWER

For the other guys who are looking for solution at this: Make sure that your glFrustum's zNear parameter is far enough to see things. Mine were to close and when I increased it, the problem was solved.

5
Knowleech On

Your rendering code seems strange: You build your tri strip with degenerated triangles and might access data which isn't there. I guessed the rest of your approach and tried to fix your method. Try this:

void render::paintGL(){
  glLoadIdentity();
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glTranslatef(0,0,-100);
  for(int z = 0; z < dted.width() - 2; z++) {
    glBegin(GL_TRIANGLE_STRIP);
    for(int x = 0; x < dted.height() - 1; x++){
        glColor3f(matrix[x][z], matrix[x][z], matrix[x][z]);
        glVertex3f(x, matrix[x][z], -z);
        //vertex0 ends here

        glColor3f(matrix[x][z + 1], matrix[x][z + 1], matrix[x][z + 1]);
        glVertex3f(x, matrix[x][z + 1], -z - 1);
        //vertex1 ends here

    }
    glEnd();
}}

Other than this, triangle strips are fine. If you still don't see anything, follow the suggestion by Sga and use GL_POINTS. Then fix you camera/other rendering code until you see them.