openGL+VS2010的例程--旋转变色立方体(三维)

 

效果图如上。

步骤:首先,变换模型视角;然后,改变颜色;最后,利用顶点数组绘制立方体。

源代码如下:

 
#include <GL/glut.h> 
 
// 绘制立方体

// 将立方体的八个顶点保存到一个数组里面

static const float vertex_list[][3] = 
{
    -0.5f, -0.5f, -0.5f, 
    0.5f, -0.5f, -0.5f, 
    -0.5f, 0.5f, -0.5f, 
    0.5f, 0.5f, -0.5f, 
    -0.5f, -0.5f, 0.5f, 
    0.5f, -0.5f, 0.5f, 
    -0.5f, 0.5f, 0.5f, 
    0.5f, 0.5f, 0.5f,
};

// 将要使用的顶点的序号保存到一个数组里面 

static const GLint index_list[][2] = 
{ 
    {0, 1},    
    {2, 3},    
    {4, 5},    
    {6, 7},    
    {0, 2},    
    {1, 3},    
    {4, 6},    
    {5, 7},
    {0, 4},
    {1, 5},
    {7, 3},
    {2, 6}
}; 

// 绘制立方体

void DrawCube(void)
{
    int i,j;
    
    glBegin(GL_LINES); 
    for(i=0; i<12; ++i) // 12 条线段
    {
        for(j=0; j<2; ++j) // 每条线段 2个顶点
        {
            glVertex3fv(vertex_list[index_list[i][j]]);     
        }
    }
    glEnd();
}

static float rotate = 0;
static int times = 0;

void renderScene(void) 
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清理颜色缓冲和深度缓冲
    
    glMatrixMode(GL_MODELVIEW); // 对模型视景的操作
    glLoadIdentity(); // 重置当前指定的矩阵为单位矩阵
    glPushMatrix(); // 压栈

    //glTranslatef(-0.2, 0, 0); // 平移
    //glScalef(1, 1, 1);    // 缩放

    times++;
    if(times > 100)
    {
        times = 0;
    }

    if(times % 100 == 0) // [0, 100)
    {
        rotate += 0.5; // [0, 20)
    }
    
    glRotatef(rotate, 0, 1, 0); // 旋转
    glRotatef(rotate, 1, 0, 0);
        
    // 动态颜色变换--红->绿->蓝->红
    if(rotate == 0)
        glColor3f(1, 0, 0);
    if(rotate ==90)
        glColor3f(0, 1, 0);
    if(rotate ==180)
        glColor3f(0, 0, 1); 
    if(rotate ==270)
        glColor3f(1, 1, 0);
    if(rotate ==360) 
        rotate = 0; 

    DrawCube(); // 绘制立方体

    glPopMatrix();// 出栈
    glutSwapBuffers();
} 

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);  // 初始化GLUT
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);  
    glutInitWindowPosition(100, 100);  // 显示窗口在屏幕的相对位置
    glutInitWindowSize(500, 500); // 设置显示窗口大小
    glutCreateWindow(argv[0]); // 创建窗口,附带标题
     
    glutDisplayFunc(renderScene); // 注册显示用的函数
    glutIdleFunc(renderScene); // 注册空闲用的函数

    glutMainLoop(); // GLUT 状态机

    return 0; 

}

 

转载于:https://www.cnblogs.com/yuwl26/p/4218471.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++语言画矩形 "_AFXDLL" "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\Test.rc"" Creating temporary file "C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8A.tmp" with contents [ /nologo /MDd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /Fp"Debug/Test.pch" /Yu"stdafx.h" /Fo"Debug/" /Fd"Debug/" /FD /GZ /c "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\MainFrm.cpp" "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\Picdlg.cpp" "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\Test.cpp" "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\TestDoc.cpp" "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\TestView.cpp" ] Creating command line "cl.exe @C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8A.tmp" Creating temporary file "C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8B.tmp" with contents [ /nologo /MDd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /Fp"Debug/Test.pch" /Yc"stdafx.h" /Fo"Debug/" /Fd"Debug/" /FD /GZ /c "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\StdAfx.cpp" ] Creating command line "cl.exe @C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8B.tmp" Creating temporary file "C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8C.tmp" with contents [ /nologo /subsystem:windows /incremental:yes /pdb:"Debug/Test.pdb" /debug /machine:I386 /out:"Debug/Test.exe" /pdbtype:sept ".\Debug\MainFrm.obj" ".\Debug\Picdlg.obj" ".\Debug\StdAfx.obj" ".\Debug\Test.obj" ".\Debug\TestDoc.obj" ".\Debug\TestView.obj" ".\Debug\Test.res" ] Creating command line "link.exe @C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8C.tmp" <h3>Output Window</h3> Compiling resources... Compiling... StdAfx.cpp Compiling... MainFrm.cpp Picdlg.cpp Test.cpp TestDoc.cpp TestView.cpp Generating Code... Linking...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值