Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm wondering if its possible to setup a fog to a player's coordinates (where there is fog around the player to make it more third-person) the main reason I ask this is because I have my player more towards the top right corner of the screen

Here's some code that can give you a guide on what im doing:

glEnable(GL_FOG) GLfloat FogColor[] = {0.8,0.8,0.8,1.0};

and I want the fog to follow a player with a position as said in


If anyone can turn this into some example code that would be very helpful, thanks

If this is not possible then how would I setup the fog to be around a certain position of the camera, like show on the top right corner of the camera instead of the center?

Here's a picture of the game screen so you can see what im talking about:

enter image description here

share|improve this question
up vote 1 down vote accepted

I think this would be most easily accomplished using shaders. If you don't have the ability to use shaders yet I'd recommend figuring them out regardless because they're super useful for lots of things like this. Operating under the assumption you can use them, here is a vertex and fragment shader that will create a grey radius fog around your character's location (with simple texturing).

Vertex Shader (fog.vert):

uniform vec3 playerPos;
uniform sampler2D colorMap;

varying vec3 vertexToPlayer;

void main(void)
    vec3 vertexPos = vec3(gl_Vertex[0], gl_Vertex[1], gl_Vertex[2]);

    vertexToPlayer = playerPos - vertexPos;

    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

Fragment Shader (fog.frag):

uniform vec3 playerPos;
uniform sampler2D colorMap;

varying vec3 vertexToPlayer;

void main(void)
    const float radius = 1024.0;

    float dist = length(vertexToPlayer);
    float attenuation = clamp((radius - dist) / radius, 0.0, 1.0);

    vec4 diffuseTexel = texture2D(colorMap, gl_TexCoord[0].st);
    vec4 diffuseTerm  = diffuseTexel * attenuation + (.8 * (1.0-attenuation));
    diffuseTerm.a = diffuseTexel.a; /* preserve alpha */

    gl_FragColor = diffuseTerm;

Then, somewhere in your code, pass in your values for the player position with:

GLint playerPos = glGetUniformLocation(prog, "playerPos");
glUniform3f(playerPos, xPos, yPos, zPos);

Hope this is helpful!

share|improve this answer
+1. This is the best way to do it. Shaders rule! (Aaand they're all you can hope for in the future, and on mobile devices...) – Macke May 10 '11 at 20:54

Use fog coordinates.

Normally, the fog value is computed using a distance factor from eye to the pixel 'c'. In effect, the fog density is a function of distance.

f = exp(-d*c) -- GL_EXP2
f = exp(-(d*c)^2) -- GL_EXP
f = (e-c)/(e-s) -- GL_LINEAR

Fog coordinates allow you to set the 'c' value of the equation and specify values directly for this. If you want to set up fogging so that 0 -> no fog, 1.0 -> most fog, then do:

glFogf(GL_FOG_START, 0.0f);
glFogf(GL_FOG_END, 1.0f);

//This actually enables the "fog coordinate" mode

Then, when you render your person, use glFogCoordfEXT()

//Show fog (some value between 0.0 - 1.0f)


//No fog

Edit: An extensionless alternative is to disable fog when rendering everything, then enable fog with appropriate start/end distances when rendering your character.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.