当前位置:主页   - 电脑 - 程序设计 - C/C++
《基于MFC的OpenGL编程》Part 8 Colors
来源:网络   作者:   更新时间:2011-01-27
收藏此页】    【字号    】    【打印】    【关闭

  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中加入如下旋转控制变量:

  GLfloat m_xRot, m_yRot;//绕x,y轴旋转的角度,随时间不断变化

  并在构造函数中初始化:

CCY457OpenGLView::CCY457OpenGLView()
{
  m_xRot = 0.0f;
  m_yRot = 0.0f;
}

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

void CCY457OpenGLView::OnTimer(UINT nIDEvent)
{
  m_xRot = m_xRot + 0.5f;
  m_yRot = m_yRot + 0.5f;
  InvalidateRect(NULL, FALSE);  
  CView::OnTimer(nIDEvent);
}

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

void CCY457OpenGLView::OnShadingmodelSmooth()
{
  glShadeModel(GL_SMOOTH);
  InvalidateRect(NULL,FALSE);
}
  
void CCY457OpenGLView::OnShadingmodelFlat()
{
  glShadeModel(GL_FLAT);
  InvalidateRect(NULL,FALSE);
}

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

void CCY457OpenGLView::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);
    //Front Face
    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);
    //Back Face
    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);
    //Left Face
    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);
    //Right Face
    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);
    //Top Face
    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);
    //Botton Face
    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();
}

其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明