Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (292)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  OpenGL fog mode not working for me  (Read 1159 times)
0 Members and 1 Guest are viewing this topic.
Offline ags1

Senior Member


Medals: 5
Projects: 1



« Posted 2012-06-02 20:19:42 »

I am trying to create a simple scene with fog. Forgive my basic trees! My problem is that the fog does not start out thin at the camera position and get denser as you go deeper into the scene. Instead my scene seems to consist of a bubble of clearness with thicker fog inside and outside the bubble. The only thing I know for sure is the zone of clearness passes through the glLoadIdentity() 0,0,0 point. What am I doing wrong?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   private void setCameraWithFog(GL2 gl, GLU glu, float x, float y, float z,
         float lookingAtX, float lookingAtY, float lookingAtZ) {
      gl.glTranslatef(x, y, z);
        gl.glEnable(GL2.GL_FOG);
        gl.glFogi(GL2.GL_FOG_MODE, GL2.GL_EXP2);
        float[] fogColor = {0.5f, 0.5f, 0.5f, 1.0f};
        gl.glFogfv(GL2.GL_FOG_COLOR, fogColor, 0);
        gl.glFogf(GL2.GL_FOG_DENSITY, 0.3f);
        gl.glHint(GL2.GL_FOG_HINT, GL.GL_DONT_CARE);
      gl.glFogf(GL2.GL_FOG_START, 0.0f);
       gl.glFogf(GL2.GL_FOG_END, 35.0f);
        gl.glLoadIdentity();
       setCamera(gl, glu, x, y, z, lookingAtX, lookingAtY, lookingAtZ);
     
   }


As you can see I try to set the 0,0,0 point to the eventual camera position before enabling fog but the fog still behaves strangely. Also, which parts of the fog code should be in my init function? I will try to upload a picture when i figure out how.


fogWrong by Agnes.Clarke.01, on Flickr
Offline lhkbob

JGO Knight


Medals: 32



« Reply #1 - Posted 2012-06-02 21:28:19 »

Depending on the fog hint, I believe opengl fog can be view dependent because it approximates distance using eye-space z coordinates.  You should try getting rid of your first call to glTranslate(), and glLoadIdentity() and move setCamera() to the first thing you do within that method.  That way you have your camera configured before setting up your fog.

However, I don't think the ordering of those commands should really affect things, so there's a chance that later on your rendering corrupts the modelview matrix so that fog sees the incorrect view transform.  This is all conjecture.

You could also try setting the fog hint to NICEST, since that should enable a view-independent fog path.

Offline theagentd
« Reply #2 - Posted 2012-06-02 21:54:05 »

glTranslate() and then you enable fog? Fog hasn't got anything to do with glTranslate(), so what's that call doing if you're resetting the matrix with glLoadIdentity a few lines down? =S

Myomyomyo.
Games published by our own members! Check 'em out!
Try the Free Demo of Droid Assault
Offline ags1

Senior Member


Medals: 5
Projects: 1



« Reply #3 - Posted 2012-06-02 22:25:54 »

The call to glTranslate does nothing... it was just an attempt to move the camera into the clear zone.
Offline ags1

Senior Member


Medals: 5
Projects: 1



« Reply #4 - Posted 2012-06-03 00:19:30 »

I think this code is the problem. the gluLookAt() is applied to the GL_PROJECTION matrix, which is apparently wrong (http://www.opengl.org/archives/resources/faq/technical/viewing.htm#view0030). But I'm struggling to get my lookAt code out of the projection matrix.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
   protected void setCamera(GL2 gl, GLU glu, float x, float y, float z, float lookingAtX, float lookingAtY, float lookingAtZ) {
        // Change to projection matrix.
     gl.glPushMatrix();
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
       
        // Perspective.
       float widthHeightRatio = 1280f / 720f;//(float) getWidth() / (float) getHeight();
       glu.gluPerspective(50, widthHeightRatio, 1, 1000);
        glu.gluLookAt(x, y, z, lookingAtX, lookingAtY, lookingAtZ, 0, 1, 0);
       
        // Change back to model view matrix.
       gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glPopMatrix();
        gl.glLoadIdentity();
   }
Offline lhkbob

JGO Knight


Medals: 32



« Reply #5 - Posted 2012-06-03 06:02:38 »

Do this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
protected void setCamera(GL2 gl, GLU glu, float x, float y, float z, float lookingAtX, float lookingAtY, float lookingAtZ) {
        // Change to projection matrix.
       gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
       
        // Perspective.
       float widthHeightRatio = 1280f / 720f;//(float) getWidth() / (float) getHeight();
       glu.gluPerspective(50, widthHeightRatio, 1, 1000);

        // Change back to model view matrix.
       gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
        glu.gluLookAt(x, y, z, lookingAtX, lookingAtY, lookingAtZ, 0, 1, 0);
   }


You don't need to use glPushMatrix and glPopMatrix here.  You should call glPushMatrix right before you set the transform for your model, and glPopMatrix right after you render it.  The pushing and popping are specific to the matrix mode you have activated.

Offline ags1

Senior Member


Medals: 5
Projects: 1



« Reply #6 - Posted 2012-06-03 18:26:54 »

This seems to be working! Correcting the setCamera() code seems to be giving me fog centered on the camera. I don't have access to my dev machine until later in the week, but I have tried this on my laptop and it works fine.
Offline davedes
« Reply #7 - Posted 2012-06-03 18:39:26 »

If you ever feel like 'going modern', GLSL fog seems relatively simple (and more flexible).
http://www.ozone3d.net/tutorials/glsl_fog/

Offline theagentd
« Reply #8 - Posted 2012-06-03 19:28:33 »

If you ever feel like 'going modern', GLSL fog seems relatively simple (and more flexible).
http://www.ozone3d.net/tutorials/glsl_fog/

One thing at least old games were plagued by was that they used the same depth value as the one to be stored in the depth buffer to calculate the fog. This is not even close to accurate. First of all, the value isn't linearly distributed. It's distributed so that you have the most precision up close and less far away, so a depth value of 0.5 is NOT the in the middle between the near and far plane. Secondly, this value does not even represent the exact distance to the pixel in the first place. This is blatantly obvious if you move the camera around. The fog should be equally thick for the same distance, but you actually have a longer "vision range" near the edges of the screen. The whole fog looks like it's a plane put over the camera (which it kind of is), not like you're in a sphere of fog. Kind of hard to explain, but I hope you get it.

A better way would be to either use the view space distance which is linear and correct. This distance can be calculated in two ways. The first is from the normal depth value by either multiplying the pixel + its depth by the inverse of the projection matrix to get the view space position and simply calculating the length of that vector or by converting the depth to linear depth and then finding finding where this finite ray would end up (faster than a matrix multiplication). This is the preferred method if you're doing fog as a postprocess (which is the fastest since you get 0 overdraw). The other one is to simply pass through the view space position from the vertex shader to the fragment shader and calculate the correct distance there and using it.

Myomyomyo.
Offline ags1

Senior Member


Medals: 5
Projects: 1



« Reply #9 - Posted 2012-06-04 22:34:47 »

The fix works! I'm very pleased and I have added an animation of walking through a foggy forest. Now to increase the poly count of my trees and get better grass. 
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (66 views)
2013-05-17 21:29:12

alaslipknot (75 views)
2013-05-16 21:24:48

gouessej (107 views)
2013-05-16 00:53:38

gouessej (103 views)
2013-05-16 00:17:58

theagentd (114 views)
2013-05-15 15:01:13

theagentd (103 views)
2013-05-15 15:00:54

StreetDoggy (148 views)
2013-05-14 15:56:26

kutucuk (171 views)
2013-05-12 17:10:36

kutucuk (169 views)
2013-05-12 15:36:09

UnluckyDevil (178 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.193 seconds with 20 queries.