这篇是在上一篇的基础上修改而来的,对应着Nehe教程的第8课,代码仍然采用MFC框架来实现。其中InitGL需要做些修改,加入混合操作所需要的代码:
BOOLCOpenGLDemoView::InitGL(GLvoid)
{
if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
{
return FALSE; // If Texture Didn't Load Return FALSE
}
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // 设置环境光
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // 设置漫射光
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // 设置光源位置
glEnable(GL_LIGHT1); // 启用一号光源
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
//COLORREF color = ::GetSysColor(COLOR_3DFACE);
//glClearColor((float)GetRValue(color)/255.0f,
// (float)GetGValue(color)/255.0f,
// (float)GetBValue(color)/255.0f,
// 1.0);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glColor4f(1.0f, 1.0f, 1.0f, 0.5); // Full Brightness. 50% Alpha
glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Set The Blending Function For Translucency
return TRUE; // Initialization Went OK
}
最后在void COpenGLDemoView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)中加入对B按键的事件处理代码:
case 'B':
{
if(bPressed==TRUE)
{//防止长时间按着'B'键而导致持续混合变化
bLighting = !bLighting;
if(bLighting)
{
glEnable(GL_BLEND); // Turn Blending On
glDisable(GL_DEPTH_TEST); // Turn Depth Testing Off
}
else
{
glDisable(GL_BLEND); // Turn Blending Off
glEnable(GL_DEPTH_TEST); // Turn Depth Testing On
}
}
bPressed = FALSE;
break;
}
最终效果如图所示:
本文转自Phinecos(洞庭散人)博客园博客,原文链接http://www.cnblogs.com/phinecos/archive/2007/08/09/849930.html,如需转载请自行联系原作者