terrain rendering

Started by
15 comments, last by spek 15 years, 9 months ago
the rest of the code:

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (100, (GLfloat)w / (GLfloat)h, 0.01, 500.0);
glMatrixMode (GL_MODELVIEW);
}
void enable (void) {
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glShadeModel (GL_SMOOTH);
glEnable(GL_TEXTURE_2D);
}

void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
enable();
//glTranslatef(30, 0, 30);
glTranslatef(30,yTrans,zTrans);
//glRotatef(150, 0,0,0);
glRotatef(yRot,0,0.5,0);
gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
glScalef(2.0,1.0,2.0);
glColor3f(1,1,1);
//glBindTexture(GL_TEXTURE_2D, texture[0]);
DisplayHeightMap();
glutSwapBuffers();
}
Advertisement
"If after like 2m in front of your camera its cutting off, then make your far plane farther."

i would like to add that i am able to navigate in the front but if i move to the side (left or right) the terrain ends abruptly

glLoadIdentity();
enable();
//glTranslatef(30, 0, 30);
glTranslatef(30,yTrans,zTrans);
//glRotatef(150, 0,0,0);
glRotatef(yRot,0,0.5,0);
gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
glScalef(2.0,1.0,2.0);

There could be tons of problems, right now you are scaling, then setting up your camera, but then adding a translation after that. Your camera should be the last think you do. I'm not sure what your code is doing.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

ohh i am sorry thats a wrong code i was trying

below is the correct one:

glLoadIdentity();
enable();
glTranslatef(30,yTrans,zTrans);
glRotatef(yRot,0,0.5,0);
glColor3f(1,1,1);
DisplayHeightMap();
glutSwapBuffers();
Probably you go wrong somewhere with the camera and/or terrain scaling setup. I suggest to organize your code again, to see where it goes wrong.

About scaling/clipping distance; OpenGL (and any other API) does not have a specified unit for the size of your world. What I mean, if you render a quad '2 units' width, it does not have to be 2 metres, 2 centimetres. It's just 2 'units'. How big the quad looks on your screen depends on your camera setup, the (texture) detail on that quad, and other surrounding objects. In other words, the size is relative.

For simplicity, I always just tell 1 OpenGL unit is 1 meter, thus:
glBegin( GL_QUAD );  glVertex2f( 0,0 );  glVertex2f( 1,0 );  glVertex2f( 1,1 );  // 1 meter width/height  glVertex2f( 0,1 );glEnd;

To make it really look like 1 meter, I need to make sure all other objects have the same scaling as well. A character model for example should be 1.8 (180 cm) OpenGL units heigh. Also the texture detail is important. If I stretch my texture out very widely, it the quad will get a very low detail. That suggests you'll have to zoom out with the camera, which gives the illusion that the quad is much bigger.

And make sure your camera has the right clipping distance / Field of View settings. What you see on your screen, depends on the viewing frustum, see this picture:
http://wwweic.eri.u-tokyo.ac.jp/computer/manual/lx/SGI_Developer/books/Perf_GetStarted/sgi_html/figures/aspect.ratio.gif

In this case, you'll see the stuff between the 2 blue planes. From what I've seen, your 'gluPerspective' call seems to be ok, except that you might need to reduce the FoV to 50 or 60 instead of 100. 100 gives a little bit of a narrow tunnel vision result.
You render between 0.01 and 500 units in your case. Now make sure the terrain is scaled in such a way that it properly fits in this range. If you really don't see much, call glScalef with small factors right before you render the terrain. Just to test.

It's hard to tell where it exactly goes wrong. Be carefull with your transformations/rotations and scales, and make sure you always reset them:
glScalef( 2,2,2 );<render something>glScalef( 0.5, 0.5, 0.5 ); // scale back!

This is one way to do it. A mor easy way is to use the OpenGL glLoadIdentity and/or glPushMatrix/glPopMatrix. Right before you apply a scale/rotation/transformation, call glPusMatrix first. When you are done, you can reset back to the previous state with glPopMatrix. If you forget this, your scene might scale smaller/bigger and/or move endlessly.

Maybe you should first try to render the terrain without any scaling/moving/rotating at all. Just to make sure you'll see a proper terrain. Put the camera at position 0,0,0 and let it look straight forward. After that, you can enable your camera navigation functions one by one again, and eventually see where it goes wrong.

Greetings,
Rick
hi

gluPerspective and gluLookAt are both used for setting the camera, is it??

if i am using gluperspective then do i need to use glulookat??

and if i do gscalef what happens is that the terrain becomes flat but it does not become large..right now the terrain seems restricted on the left and right side ie it comes to an abrupt end when navigating..

Make a test object as a reference, a cube or sphere for example. If that object shows up normally, your terrain is scaled wrong, or just totally rendered wrong. If that primitive shape has problems too, your camera setup is wrong, the entire world is scaled wrong, and/or OpenGL is in the wrong state at the time you are rendering.

Don't forget that everything you'll do in OpenGL, applies from that moment on ("state machine"). Loading matrices, scaling, rotating, resetting (loadIdentity), transforming, etc. Be carefull not to use an unexpected transformation from the past. If your program is full of this, its likely it goes wrong somewhere there.

Havbe you already tried to simply disable all glTransforms, scales and rotations? Comment them, and don't move the camera either. Just render from the origin (camera at position 0,0,0 , no rotations afterwards). If its a real mess, its maybe better to start a new clean test application.

gluLookAt and gluPerspective are not the same things. GluPerspective sets your view frustum:
glMatrixMode( GL_PROJECTION );
gluPerspective( fieldOfView, ratio(width/height), near, far );
glMatrixMode( GL_MODELVIEW );

Make sure this function has proper parameters! FoV 50 or 60, ratio screenWidth / screenHeight, near = 0.1, far = 5000. Or something like that.

gluLookAt is used to set the camera rotation/orientation/position. So, you can use them both. However, some applications move/rotate the world around the camera, instead of rotating/moving the camera itself. Be sure you don't do both.

Greetings,
Rick

This topic is closed to new replies.

Advertisement