`
dato0123
  • 浏览: 915628 次
文章分类
社区版块
存档分类
最新评论

《基于MFC的OpenGL编程》Part 8 Colors

 
阅读更多
OpenGL支持两种颜色模式:RGBA和颜色索引模式,本文关注于前者。

Smooth Shading and Flat Shading

When Smooth Shading is specified, the color values are interpolated between vertices. If Flat Shading is specified, one vertex is selected as being representative of all the vertices, thus the entire primitive is displayed using one single color.

一个简单Demo

1,CCY457OpenGLView.h中加入如下旋转控制变量:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->GLfloatm_xRot,m_yRot;//绕x,y轴旋转的角度,随时间不断变化

并在构造函数中初始化:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->CCY457OpenGLView::CCY457OpenGLView()
{
m_xRot
=0.0f;
m_yRot
=0.0f;
}

2,在OnTimer函数中,修改绕x,y轴旋转的角度值

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::OnTimer(UINTnIDEvent)
{
m_xRot
=m_xRot+0.5f;
m_yRot
=m_yRot+0.5f;
InvalidateRect(NULL,FALSE);
CView::OnTimer(nIDEvent);
}

3,加入两个菜单项,控制OpenGL的渲染模式

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::OnShadingmodelSmooth()
{
glShadeModel(GL_SMOOTH);
InvalidateRect(NULL,FALSE);
}

voidCCY457OpenGLView::OnShadingmodelFlat()
{
glShadeModel(GL_FLAT);
InvalidateRect(NULL,FALSE);
}

4,在RenderScene中加入绘制代码:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCY457OpenGLView::RenderScene()
{
//绘制函数
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(m_xRot,
1.0f,0.0f,0.0f);
glRotatef(m_yRot,
0.0f,1.0f,0.0f);
//FrontFace
glBegin(GL_POLYGON);
glColor3f(
1.0f,0.0f,0.0f);
glVertex3f(
-1.0f,-1.0f,0.0f);
glColor3f(
1.0f,1.0f,0.0f);
glVertex3f(
1.0f,-1.0f,0.0f);
glColor3f(
1.0f,0.0f,1.0f);
glVertex3f(
1.0f,1.0f,0.0f);
glColor3f(
1.0f,1.0f,1.0f);
glVertex3f(
-1.0f,1.0f,0.0f);
glEnd();
glColor3f(
1.0f,1.0f,0.0f);
//BackFace
glBegin(GL_POLYGON);
glVertex3f(
-1.0f,-1.0f,-1.0f);
glVertex3f(
-1.0f,1.0f,-1.0f);
glVertex3f(
1.0f,1.0f,-1.0f);
glVertex3f(
1.0f,-1.0f,-1.0f);
glEnd();
glColor3f(
1.0f,0.0f,1.0f);
//LeftFace
glBegin(GL_POLYGON);
glVertex3f(
-1.0f,-1.0f,0.0f);
glVertex3f(
-1.0f,1.0f,0.0f);
glVertex3f(
-1.0f,1.0f,-1.0f);
glVertex3f(
-1.0f,-1.0f,-1.0f);
glEnd();
glColor3f(
0.0f,1.0f,0.0f);
//RightFace
glBegin(GL_POLYGON);
glVertex3f(
1.0f,-1.0f,0.0f);
glVertex3f(
1.0f,-1.0f,-1.0f);
glVertex3f(
1.0f,1.0f,-1.0f);
glVertex3f(
1.0f,1.0f,0.0f);
glEnd();
glColor3f(
0.0f,1.0f,1.0f);
//TopFace
glBegin(GL_POLYGON);
glVertex3f(
-1.0f,1.0f,0.0f);
glVertex3f(
1.0f,1.0f,0.0f);
glVertex3f(
1.0f,1.0f,-1.0f);
glVertex3f(
-1.0f,1.0f,-1.0f);
glEnd();
glColor3f(
0.0f,0.0f,1.0f);
//BottonFace
glBegin(GL_POLYGON);
glVertex3f(
-1.0f,-1.0f,0.0f);
glVertex3f(
-1.0f,-1.0f,-1.0f);
glVertex3f(
1.0f,-1.0f,-1.0f);
glVertex3f(
1.0f,-1.0f,0.0f);
glEnd();
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics