NeNe OpenGL Lesson41 - Volumetric Fog(体积雾) & IPicture Image Loading
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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了