views:

293

answers:

2

Good day. I am trying to make a volumetric fog in OpenGL using glFogCoordfEXT. Why does a fog affect to all object of my scene, even if they're not in fog's volume? And these objects become evenly gray as a fog itself. Here is a pic alt text

Code:

void CFog::init()
    {           

        glEnable(GL_FOG);                
        glFogi(GL_FOG_MODE, GL_LINEAR); 
        glFogfv(GL_FOG_COLOR, this->color); 
        glFogf(GL_FOG_START,  0.0f); 
        glFogf(GL_FOG_END,    1.0f);   
        glHint(GL_FOG_HINT, GL_NICEST); 

        glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
    }

void CFog::draw()
{
    glBlendFunc(GL_SRC_ALPHA, GL_SRC_ALPHA);
    glEnable(GL_BLEND);
    glPushMatrix();
    glTranslatef(this->coords[0], this->coords[1], this->coords[2]);
    if(this->angle[0] != 0.0f)
        glRotatef(this->angle[0], 1.0f, 0.0f, 0.0f);
    if(this->angle[1] != 0.0f)
        glRotatef(this->angle[1], 0.0f, 1.0f, 0.0f);
    if(this->angle[2] != 0.0f)
        glRotatef(this->angle[2], 0.0f, 0.0f, 1.0f);
    glScalef(this->size, this->size, this->size);
    GLfloat one = 1.0f;
    GLfloat zero = 0.0f;
    glColor4f(0.0, 0.0, 0.0, 0.5);
    glBegin(GL_QUADS);                                                                                                  // Back Wall
        glFogCoordfEXT( one);           glVertex3f(-2.5f,-2.5f,-15.0f);
        glFogCoordfEXT( one);           glVertex3f( 2.5f,-2.5f,-15.0f);
        glFogCoordfEXT( one);           glVertex3f( 2.5f, 2.5f,-15.0f);
        glFogCoordfEXT( one);           glVertex3f(-2.5f, 2.5f,-15.0f);
    glEnd();
    GLenum err;
    if((err = glGetError()) != GL_NO_ERROR)
        {
                char * str = (char *)glGetString(err);
        }
    glBegin(GL_QUADS);                                                                                                  // Floor
        glFogCoordfEXT( one);           glVertex3f(-2.5f,-2.5f,-15.0f);
        glFogCoordfEXT( one);           glVertex3f( 2.5f,-2.5f,-15.0f);
        glFogCoordfEXT( zero);          glVertex3f( 2.5f,-2.5f, 15.0f);
        glFogCoordfEXT( zero);          glVertex3f(-2.5f,-2.5f, 15.0f);
    glEnd();

    glBegin(GL_QUADS);                                                                                                  // Roof
        glFogCoordfEXT( one);           glVertex3f(-2.5f, 2.5f,-15.0f);
        glFogCoordfEXT( one);           glVertex3f( 2.5f, 2.5f,-15.0f);
        glFogCoordfEXT( zero);          glVertex3f( 2.5f, 2.5f, 15.0f);
        glFogCoordfEXT( zero);  glVertex3f(-2.5f, 2.5f, 15.0f);
    glEnd();

    glBegin(GL_QUADS);                                                                                                  // Right Wall
        glFogCoordfEXT( zero);  glVertex3f( 2.5f,-2.5f, 15.0f);
        glFogCoordfEXT( zero);          glVertex3f( 2.5f, 2.5f, 15.0f);
        glFogCoordfEXT( one);           glVertex3f( 2.5f, 2.5f,-15.0f);
        glFogCoordfEXT( one);           glVertex3f( 2.5f,-2.5f,-15.0f);
    glEnd();

    glBegin(GL_QUADS);                                                                                                  // Left Wall
        glFogCoordfEXT( zero);          glVertex3f(-2.5f,-2.5f, 15.0f);
        glFogCoordfEXT( zero);  glVertex3f(-2.5f, 2.5f, 15.0f);
        glFogCoordfEXT( one);   glVertex3f(-2.5f, 2.5f,-15.0f);
        glFogCoordfEXT( one);           glVertex3f(-2.5f,-2.5f,-15.0f);
    glEnd();
    glPopMatrix();
    glDisable(GL_BLEND);
    //glDisable(GL_FOG);
}
A: 

Maybe there's no lighting in the scene?
If all light sources are disabled, all objects are going to appear in a flat ambient color.
Check to see what happens if you disable fog altogether. Does this still happen?

shoosh
No, lighting is there, if I comment fog draw and init in my scene render functon, all is fine
Irene
+1  A: 

You seem to not be clear on how GL_fog_coord_EXT works.

You're saying that an object is "outside the fog volume" but OpenGL does not have any notion of a fog volume. At any point, either Fog is completely off, or it's on, in which case the fog equation will be applied with a fog coefficient that depends both on the fog mode (LINEAR in your case) and the fog coordinate.

Regarding the fog coordinate. when using

    glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);

You're telling OpenGL that every time you'll provide a vertex, you'll also provide which fog coordinate to use through glFogCoordfEXT

So, what does it mean in your case ? Assuming you're not calling glFogCoordfEXT in your teapot drawing code, you'll end up with the value of your last call to glFogCoordfEXT, which looks like a glFogCoordf(one). So everything drawn in that case will be fully in fog, which is what you observe.

Now, I'm not sure exactly what you're trying to achieve, so I don't know how to help you solve the issue, exactly. However, if the goal is to use your quads to mimic fog, simply turn fog off when drawing the scene, and turn it on only when drawing the cube (I'm pretty sure it won't look like nice fog though).

Bahbar
Thank you, Bahbar!! this: "you'll end up with the value of your last call to glFogCoordfEXT, which looks like a glFogCoordf(one). "helped! I just changed a drawing order, and everything became ok:))
Irene