Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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 1926 times)
0 Members and 1 Guest are viewing this topic.
Offline ags1

JGO Ninja


Medals: 46
Projects: 2
Exp: 5 years


Make code not war!


« 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!
Legends of Yore - The Casual Retro Roguelike
Offline ags1

JGO Ninja


Medals: 46
Projects: 2
Exp: 5 years


Make code not war!


« 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

JGO Ninja


Medals: 46
Projects: 2
Exp: 5 years


Make code not war!


« 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

JGO Ninja


Medals: 46
Projects: 2
Exp: 5 years


Make code not war!


« 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

JGO Ninja


Medals: 46
Projects: 2
Exp: 5 years


Make code not war!


« 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.

 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (14 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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
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!