I have been having some trouble incorporating fog into a game I am developing... the games graphics are similar to old school doom in that all the graphics are 2 dimensional sprites drawn onto 2 dimensional planes... as the user you have a third person view of your character which is set up as follows..

for all objects that get rendered to the scene I use the modelview matrix and translate to the objects absolute position and then render it...currently when I enable fog the fog seems to be kind of random in where it appears, I have read that part of the issue may have to do with calling glulookat on the projection matrix stack instead of the modelview, but when I call gluperspective on the modelview stack it throws off all my translations for the game objects(user, npc's, etc). could anyone offer any insight on how I should change my setup so I can enable fog? Thanks

You want to call gluPerspective on the PROJECTION matrix, which you're doing. However, you should be calling gluLookAt on the MODELVIEW and not the PROJECTION matrix. This is because gluLookAt configures the VIEW part of the MODELVIEW matrix.

You can use glPushMatrix() and glPopMatrix() to easily reset your MODELVIEW back to what was set with gluLookAt:

Thanks for the speedy respone... I changed it around a bit and my user is now rendered correctly on the floor, but all the other game objects are gone. just so I know I'm understanding this correctly could you confirm the following?

then for every game objectgl.glPushMatrix();gl.glLoadIdentity();gl.glTranslatef(world.user.pos.x, world.user.pos.y, world.user.pos.z);//any other necesary transforms/rotations etcgl.glPopMatrix();renderObject();

glPushMatrix() saves the current state before modifications by pushing it onto a stack. glPopMatrix() resets the state to the last call of glPushMatrix() by popping off a matrix from the stack. In this case, you save the view with glPushMatrix(), modify it to take into account the object's translation, then you render it, then you pop it off to reset for the next object.

Lastly, the reason why your original problem caused issues with fog rendering is that OpenGL uses view-space fog. Since your view matrix was included in the projection, the fog calculations were seeing strange transformations.

I am still having issues figuring this all out, so I am going to post up a bunch of code and hopefully, with a little luck, and a wee bit of magic, somone can figure this out.

ok so I changed the project setup and now the camera seems to be fixed at a point in space instead of following the user.just to recap, previously I had the glulook at on the projectionmatrix and that rendered everything correctly but did not allow me to incorporate fog which I desperately want to incorporate...

gl.glMatrixMode(GL10.GL_MODELVIEW);gl.glLoadIdentity();GLU.gluLookAt(gl, world.user.cam.pos.x,world.user.cam.pos.y, world.user.cam.pos.z, world.user.cam.lookat.x, world.user.cam.lookat.y, world.user.cam.lookat.z, 0, 1, 0);//the cameras position is determined by rotating the user look vector 180 degrees, scaling it a bit and then moving it up in the y//the lookat is just a copy of the users position// the idea is to create a third person view looking from above and behind the user

gl.glPushMatrix();gl.glLoadIdentity();gl.glTranslatef(world.user.pos.x, world.user.pos.y, world.user.pos.z); // translate to the users position which is the same as the cameras lookat//gl.glRotatef(world.user.look2.angle() + 90, 0, 1, 0); // rotate the 2d plane 90 from the angle of the look so it appears perpendicular to camera//The batcher.begin binds the texture//batcher.drawsprite(x,y,width,height,textureRegion) draws a 2d plane at soecified x,y and width height and applies specified texture region

Don't call glLoadIdentity() after glPushMatrix(). glTranslate() multiplies the current state of the matrix (i.e. the view configured by gluLookAt()) with a translation matrix. This means the MODELVIEW would translate the object to its world position, then transform the world position into view or camera space.

If you have that 2nd glLoadIdentity(), you just translate into world space and never complete it. Otherwise, your glPushMatrix() and glPopMatrix() and object rendering are all ordered correctly with respect to gluLookAt() and glTranslate()

ok so everything is being drawn in their respected places again and glulookat is being called on the modelview matrix, but my fog is still kind of random... for all my characters, user and npc's it works, but the floor and skybox seem to randomly partially or completely disappear or come into full view with no fog as I rotate the viewing angle of the camera.... I have the fog set up as follows, right now this is being called before every frame:

Then I render the floor currently represented by a single large plane composed of 4 vertices rendered with six indices, followed by the skybox , then the characters and effects(which the fog is working on)... I thought the issue may have to do with per vertex fog calculations but it is my understanding that setting the fog mode to nicest results in per pixel calculations... any ideas?

My only thought is that there is some discrepancy with how you configure your MODELVIEW matrix for when you render your skybox and floor. If you could boil it down into a standalone class, I'd be happy to run it and fiddle around to see where the problem is.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org