我们将从设置保存雾的信息的变量开始。变量fogMode用来保存三种类型的雾:GL_EXP,GL_EXP2和GL_LINEAR。变量fogfilter将用来表示我们使用的是哪种雾类型。变量fogColor保存雾的颜色。还加了一个布尔类型的变量gPressed用来记录'g'键是否被按下。
bool
gPressed;
![None.gif](/Images/OutliningIndicators/None.gif)
GLuintfogMode[
3
];
//
StorageForThreeTypesOfFog
GLuintfogfilter;
//
WhichFogModeToUse
GLfloatfogColor[
4
];
//
FogColor
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
BOOLCOpenGLDemoView::InitGL(GLvoid)
//
AllSetupForOpenGLGoesHere
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(!LoadGLTextures())//JumpToTextureLoadingRoutine(NEW)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
returnFALSE;//IfTextureDidn'tLoadReturnFALSE
}
glEnable(GL_TEXTURE_2D);//EnableTextureMapping(NEW)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
glLightfv(GL_LIGHT1,GL_AMBIENT,LightAmbient);//设置环境光
glLightfv(GL_LIGHT1,GL_DIFFUSE,LightDiffuse);//设置漫射光
glLightfv(GL_LIGHT1,GL_POSITION,LightPosition);//设置光源位置
glEnable(GL_LIGHT1);//启用一号光源
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
glShadeModel(GL_SMOOTH);//EnableSmoothShading
COLORREFcolor=::GetSysColor(COLOR_3DFACE);
//glClearColor((float)GetRValue(color)/255.0f,
//(float)GetGValue(color)/255.0f,
//(float)GetBValue(color)/255.0f,
//1.0);
glClearColor(0.5f,0.5f,0.5f,1.0f);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
glClearDepth(1.0f);//DepthBufferSetup
glEnable(GL_DEPTH_TEST);//EnablesDepthTesting
glDepthFunc(GL_LEQUAL);//TheTypeOfDepthTestingToDo
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//ReallyNicePerspectiveCalculations
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
glFogi(GL_FOG_MODE,fogMode[fogfilter]);//FogMode
glFogfv(GL_FOG_COLOR,fogColor);//SetFogColor
glFogf(GL_FOG_DENSITY,0.35f);//HowDenseWillTheFogBe
glHint(GL_FOG_HINT,GL_DONT_CARE);//FogHintValue
glFogf(GL_FOG_START,1.0f);//FogStartDepth
glFogf(GL_FOG_END,5.0f);//FogEndDepth
glEnable(GL_FOG);//EnablesGL_FOG
returnTRUE;//InitializationWentOK
}
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
void
COpenGLDemoView::OnKeyUp(UINTnChar,UINTnRepCnt,UINTnFlags)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//TODO:Addyourmessagehandlercodehereand/orcalldefault
switch(nChar)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
case'G':
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(gPressed==TRUE)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
fogfilter=(fogfilter+1)%3;
gPressed=FALSE;
glFogi(GL_FOG_MODE,fogMode[fogfilter]);//FogMode
}
break;
}
default:
break;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
CView::OnKeyUp(nChar,nRepCnt,nFlags);
}
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
void
COpenGLDemoView::OnKeyDown(UINTnChar,UINTnRepCnt,UINTnFlags)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//TODO:Addyourmessagehandlercodehereand/orcalldefault
switch(nChar)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
case'G':
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
gPressed=TRUE;
break;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
default:
break;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
CView::OnKeyDown(nChar,nRepCnt,nFlags);
}
![None.gif](/Images/OutliningIndicators/None.gif)
COpenGLDemoView::COpenGLDemoView()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this->gPressed=FALSE;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
this->fogMode[0]=GL_EXP;
this->fogMode[1]=GL_EXP2;
this->fogMode[2]=GL_LINEAR;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
this->fogfilter=0;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
this->fogColor[0]=0.5f;
this->fogColor[1]=0.5f;
this->fogColor[2]=0.5f;
this->fogColor[3]=1.0f;
}
第一行glEnable(GL_FOG);是初始化雾效。
第二行glFogi(GL_FOG_MODE, fogMode[fogfilter]);建立雾的过滤模式。之前我们声明了数组fogMode,它保存了值GL_EXP, GL_EXP2, and GL_LINEAR。(是计算雾效混合因子的三种方式):
GL_EXP - 充满整个屏幕的基本渲染的雾。它能在较老的PC上工作,因此并不是特别像雾。
GL_EXP2 - 比GL_EXP更进一步。它也是充满整个屏幕,但它使屏幕看起来更有深度。
GL_LINEAR - 最好的渲染模式。物体淡入淡出的效果更自然。
第三行glFogfv(GL_FOG_COLOR, fogcolor);设置雾的颜色。之前我们已将变量fogcolor设为(<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue=".5" unitname="F">0.5f</chmetcnv>,<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue=".5" unitname="F">0.5f</chmetcnv>,<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue=".5" unitname="F">0.5f</chmetcnv>,<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="1" unitname="F">1.0f</chmetcnv>),这是一个很棒的灰色。
接下来我们看看最后的四行。glFogf(GL_FOG_DENSITY, <chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue=".35" unitname="F">0.35f</chmetcnv>);这行设置雾的密度。增加数字会让雾更密,减少它则雾更稀。
glHint (GL_FOG_HINT, GL_DONT_CARE); 设置修正。我使用了GL_DONT_CARE因为我不关心它的值。
gl_dont_care - 由OpenGL决定使用何种雾效(对每个顶点还是每个像素执行雾效计算)和一个未知的公式
gl_nicest - 对每个像素执行雾效计算(效果好)
gl_fastest - 对每个顶点执行雾效计算 (更快,但效果不如上面的好)
下一行glFogf(GL_FOG_START, <chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="1" unitname="F">1.0f</chmetcnv>);设定雾效距屏幕多近开始。你可以根据你的需要随意改变这个值。下一行类似,glFogf(GL_FOG_END, <chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="5" unitname="F">5.0f</chmetcnv>);告诉OpenGL程序雾效持续到距屏幕多远。
![200782703.jpg](https://i-blog.csdnimg.cn/blog_migrate/a40a65e2c19c07ce828f00b574f8320e.jpeg)
![200782704.jpg](https://i-blog.csdnimg.cn/blog_migrate/e13db4434a8b4b1138f6be2a19959c9c.jpeg)