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

     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();
}
复制代码


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/11/05/1327571.html,如需转载请自行联系原作者
目录
相关文章
|
Android开发 异构计算
Android OpenGL ES(八)----纹理编程框架(二)
Android OpenGL ES(八)----纹理编程框架(二)
190 0
Android OpenGL ES(八)----纹理编程框架(二)
|
存储 Java API
Android OpenGL ES(八)----纹理编程框架(一)
Android OpenGL ES(八)----纹理编程框架(一)
320 0
Android OpenGL ES(八)----纹理编程框架(一)
|
Android开发
Android OpenGL ES(三)----编程框架(二)
Android OpenGL ES(三)----编程框架(二)
123 0
Android OpenGL ES(三)----编程框架(二)
|
Java API Android开发
Android OpenGL ES(三)----编程框架(一)
Android OpenGL ES(三)----编程框架(一)
128 0
|
iOS开发 异构计算
了解 OpenGL ES实现自定义编程粒子效果 思路
本案例旨在于了解OpenGL ES中自定义编程粒子效果的整体实现思路。
200 0
了解 OpenGL ES实现自定义编程粒子效果 思路