# OpenGL GL_FOG

Discussion in 'iOS Programming' started by Kingbombs, Aug 19, 2009.

1. ### Kingbombs macrumors member

Joined:
Jun 24, 2009
#1
So i have been trying to implement fog into my opengl game

My game is a 2D game, so i was wondering if its possible to implement the fog in 2D games or does it have to be in 3D?

I have followed tutorials exactly (and quite a few tutorials which seem identical, but made for 3D objects) but nothing happens or appears, and i have come to the conclusion that i dont glEnable(DEPTH_TEST); even if i do it doesn't make a difference and im pretty sure its because the depth buffer isn't set (if i use the depth buffer i get no screen because its made for 2D view)

so i was just wondering what changes may have to be made to use Fog in a 2D program
IF it can even be used in 2D view?

2. ### lazydog macrumors 6502a

Joined:
Sep 3, 2005
Location:
Cramlington, UK
#2
Are you using exponential fog? If you are then you need to choose a value for the fog density that best matches the range in z-depth of your objects. If you use a density which is too high then all your objects will be obscured. Densities around 1, eg 0.25, 0.5, are good for a z-range of [0, 1].

b e n

3. ### Kingbombs thread starter macrumors member

Joined:
Jun 24, 2009
#3
Well thats what i was saying, its 2D so there is no Z axis, which is why i was wondering if you can even use it with any sort of result in 2D

4. ### lazydog macrumors 6502a

Joined:
Sep 3, 2005
Location:
Cramlington, UK
#4
When you use the 2-d version of glVertex() etc the z-coordinate is assigned a default value of 0.0. So even though your game is 2-d it's still running in 3-d. I'm not sure what you want to do with fog, but if you want to fade objects in and out of fog then you're going to have to assign them z-coordinates. Just use an appropriate orthographic projection to keep the game looking 2-d.

b e n

5. ### Kingbombs thread starter macrumors member

Joined:
Jun 24, 2009
#5
I was more curious to see what kind of effects it can give, i know now from doing some tutorials its not useful really in 2D,

problem is the depth bit buffer isn't enabled,
im just gonig to forget about it when i know its not going to work very well at all in 2D

thanks for the replys though

6. ### lazydog macrumors 6502a

Joined:
Sep 3, 2005
Location:
Cramlington, UK
#6
I'm not entirely sure, but I don't think you need the depth buffer enabled to use fog. Anyway I think it might be worth persevering with fog as it could be useful in say a 2-d scrolling game to add a bit of visual depth cuing to parallax scrolling planes.

Anyway good luck!

b e n

7. ### Kingbombs thread starter macrumors member

Joined:
Jun 24, 2009
#7
Im going to look into it a little bit more,

I was wondering though, lets say you have a person and you only want the fog to be covering the lower part of the body

How do you do that?
glFogf(GL_FOG_START, 1.0f);
glFogf(GL_FOG_END, 5.0f);
Now these are only for z axis arn't there?
So how can you get it to effect the X or Y axis? (the distance it covers in the X or Y axis, as these functions only effect the distance in the Z axis i think)

8. ### lazydog macrumors 6502a

Joined:
Sep 3, 2005
Location:
Cramlington, UK
#8
Is it a side on view of the person? If that's the case then using fog isn't the best way. It will be simpler, and nicer, to draw a layer of mist and use alpha transparency to get the effect you want.

b e n

9. ### Kingbombs thread starter macrumors member

Joined:
Jun 24, 2009
#9
Oh right ok, i see now
But does that mean if you use the mist you don't quite get the same effect when you enter and leave the mist?
With the fog you see objects slowly disappear as they enter etc, can you get the same effect with the mist?

10. ### lazydog macrumors 6502a

Joined:
Sep 3, 2005
Location:
Cramlington, UK
#10
Well, with fog the attenuation is based on distance from the eye/view point (this isn't necessarily the z-coordinate as the camera may be aligned along any axis or combination of axis). So if you have flat 'sprites' then you're not going to be able to have parts of the sprite in fog and some not, unless you split your sprite up. But this is getting more complicated!

It sounds more and more as though 'mist' is what you need... you can easily create mist with varying density  it's just a graphics with varying alpha.

b e n