NeNe OpenGL Lesson41 - Volumetric Fog(体积雾) & IPicture Image Loading

lesson41_screenshot

     This samples shows us how to create some fog effect that does not fall off by the distance between the viewer and vertex position. With OpenGL command glFogCoordf, we could specify a fog density for each vertex.
    Of course, there are other ways to create the volumetric fog effect. To get better visual effect, we usually work with the render to texture & post-process.
1) method one, draw the back surface & front surface of a box separately to get two different depth map. These two depth map could be used to check whether some pixel in the fog or not. A volumetric fog post-process created to calculate how fog density will be for each pixel.
2) method two, we could create a volume texture to represent how the fog in the scene should be. In the post-process step, we could restore a 3D position by screen position, depth value and view matrix. We could sample the volume texture well with this 3D vertex position.

Volumetric fog with glFogCoordf

Set up fog with OpenGL commands, the code almost the same as usual fog set up process except the last line to switch on the fog coordinate feature:

glEnable(GL_FOG);    
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogfv(GL_FOG_COLOR, fogColor);
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);

Fog extension on windows, because windows does not support OpenGL advanced feature natively.(Windows already has D3D, no need for them update OpenGL)

// check "EXT_fog_coord" extension first
...
// Enable glFogCoordEXT
glFogCoordfEXT = (PFNGLFOGCOORDFEXTPROC) wglGetProcAddress("glFogCoordfEXT");

Draw with glFogCoordfEXT, specify a fog density for each vertex, a bit similar as specify a texture coordinates with glTexCoord2f function.

glBegin(GL_QUADS);                                                    // Back Wall
    glFogCoordfEXT( 1.0f);    glTexCoord2f(0.0f, 0.0f);    glVertex3f(-2.5f,-2.5f,-15.0f);
    glFogCoordfEXT( 1.0f);    glTexCoord2f(1.0f, 0.0f);    glVertex3f( 2.5f,-2.5f,-15.0f);
    glFogCoordfEXT( 1.0f);    glTexCoord2f(1.0f, 1.0f);    glVertex3f( 2.5f, 2.5f,-15.0f);
    glFogCoordfEXT( 1.0f);    glTexCoord2f(0.0f, 1.0f);    glVertex3f(-2.5f, 2.5f,-15.0f);
glEnd();


IPicture Image Load

Create a texture from temporary bitmap (get after OleLoadPicturePath() load the image content ), no matter from local disc or network. Well, I think no matter you write a small graphic program ,engine or game, we should need to design how texture should be found and loaded. We may even need to design a texture assets pipeline.


The full source code could be found here.

posted @ 2012-08-17 08:23  opencoder  阅读(983)  评论(0编辑  收藏  举报