Fog(gy)

I’ve got a little problem w/the fog implementation with OpenGL(Yes, I’m a newbie
)
I’ve got a rotating cube, that I’m trying to cover with (red) fog, so(using GLUT), I create the window, in my init. function I call glEnable(GL_FOG), then I call the three fog drawing functions to generate the fog.
I get 1 frame of fog, then it vanishes!
Anyone got any idea?(No, the fog SHOULD be redrawn, I’ve tried drawing it in the idle, display & reshape callbacks: it dosen’t work )
Thanks!

Sounds as if you are using the projection matrix to rotate the cube. If so use the modelview matrix to transform it.

No, I’m using the Modelview matrix all the way through(except when I’m in the reshape callback): Take a look at the code…

#include <stdlib.h>
#include <Gl/glut.h>
#include <float.h>
//---------------------------------------------------------------------------
/*
Global var’s
*/
//data remioved for brevity

static GLfloat theta[] = {0.0, 0.0, 0.0};
static GLint axis = 2;
//---------------------------------------------------------------------------
void init(void)
{
glEnable(GL_FOG);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 0.5);
}

void polygon(int a, int b, int c, int d)
{
//polygon drawing code removed for brevity
}
/*
Map vertices to face.
*/
void colorcube(void)
{
polygon(0, 3, 2, 1);
polygon(2, 3, 7, 6);
polygon(0, 4, 7, 3);
polygon(1, 2, 6, 5);
polygon(4, 5, 6, 7);
polygon(0, 1, 5, 4);
}
//---------------------------------------------------------------------------
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glFogf(GL_FOG_MODE, GL_EXP);
glFogf(GL_FOG_DENSITY, 0.25);
glFogfv(GL_FOG_COLOR, fogcol);
glFlush();
glutSwapBuffers();
}

void spinCube()
{
theta[axis] += 2.0;
if(theta[axis] > 360.0) theta[axis] -= 360.0;
display();
}

void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv)
{

}

Funnily, when I enable GL_LIGHTING, then I get an almost fog: the cube goes dark and grey, then the most forward-facing corner gets a vauge red shroud. Strange.

TiA

You enable fog for the first image with the OpenGL defaults, and set the fog parameters after you have drawn the first cube!
Your second image is what you wanted to program.

Try this instead:

void init(void)
{
glFogf(GL_FOG_MODE, GL_EXP);
glFogf(GL_FOG_DENSITY, 0.25);
glFogfv(GL_FOG_COLOR, fogcol);
glEnable(GL_FOG);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 0.5);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
// No need to flush for double buffering.
glutSwapBuffers();
}

Play with the densities and try linear fog with start and end values, too.

okay, thanks!