fog / gluPerspective problems

Hi

I’m having problems when tryng to use both of this functions on my rendering routine.

Only with glupespective works :

glMatrixMode(GL_PROJECTION);
gluPerspective( 67.f, float( 800 ) / float( 600 ), 1.f, 10000.f );

render_world();

but, if i use this :

glMatrixMode(GL_PROJECTION);
gluPerspective( 67.f, float( 800 ) / float( 600 ), 1.f, 10000.f );

glEnable(GL_FOG);
render_world();
glDisable(GL_FOG);

not working… the fog is incorrect , and again if i remove the 2 first lines , glmatrixmode and gluperspective my fog looks ok…

Can anyone see the problem here ?

thanks,

Bruno

You need to switch back to GL_MODELVIEW before calling render_world(). Otherwise, OpenGL will base its fog calculations, lighting and what not on the projection matrix, which is wrong.
http://www.3dgamedev.com/articles/projection_abuse.htm

  • Tom

Thanks Tom,
interesting paper, but actually it didn’t worked.
I placed the glMatrixMode(GL_MODELVIEW) right before by render_Word routine, and all i get is a blank screen.
I think the problem is gluPerspective…, because if i remove it everything is working fine…, but i need it to define my zFar…
any mode ideias ?

Bruno

Please post a more specific piece of code here or send it to me.

Hi
Try glLoadIdentity before gluPerspective.
From MSDN
The matrix generated by gluPerspective is multiplied by the current matrix, just as if glMultMatrix were called with the generated matrix. To load the perspective matrix onto the current matrix stack instead, precede the call to gluPerspective with a call to glLoadIdentity.

Michail.

Hi
I was already make glLoaditentity()

Here it is my code…

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glClearColor(0.3,0.4,0.3,1.0);
glMatrixMode(GL_PROJECTION);

// If i remove this gluPerspective, the
geometry will be drawn with correct fog.

gluPerspective( 67.f, float( 800 ) / float( 600 ), 1.f, 10000.f );
sceneroty = 360.0f - yrot;

glRotatef(lookupdown,1.0f,0,0);
glRotatef(sceneroty,0,1.0f,0);

glPushMatrix();

glTranslatef(look.x,look.y,look.z);
glBindTexture(GL_TEXTURE_2D,texture[1]);

glEnable(GL_LIGHTING);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);

ExtractFrustum();
// With this matrix mode here, the geometry will disapear
glMatrixMode(GL_MODELVIEW);
glEnable(GL_FOG);
Render_World();
glDisable(GL_FOG);

I know the problem is not the fog setup…, but here it is anyways

glFogi(GL_FOG_MODE, GL_EXP);
glFogfv(GL_FOG_COLOR, m_fFogColour);
glFogf(GL_FOG_DENSITY, 0.02);
glHint(GL_FOG_HINT, GL_DONT_CARE);
glFogf(GL_FOG_START, 0.0);
glFogf(GL_FOG_END, 40.0);
glEnable(GL_FOG);

thanks,

Bruno

http://www.3dgamedev.com/articles/projection_abuse.htm - read it again!

You are doing your camera positioning on the projection matrix, which is WRONG.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(...);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Camera positioning:
glRotatef(...);
glTranslatef(...);

glEnable(GL_FOG);
renderFoggedStuff();
glDisable(GL_FOG);
  • Tom

In addition to what Tom said, you were also NOT setting your projection matrix to the identity. You called glLoadIdentity before glMatrixMode(GL_PROJECTION), so you were infact setting the modelview matrix to the identity. Then when you call glMatrixMode(GL_PROJECTION) you switched to the projection matrix and you did gluPerspective(…) without resetting the projection matrix. glLoadIdentity only affects the current matrix (as selected by glMatrixMode).

Just follow toms code above and you should be all set.

Thanks guys…,
got it working

Bruno