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

本文介绍了一个使用OpenGL实现立方体绘制与旋转的基础教程。通过设置模型视角、使用默认立方体绘制函数,并在空闲时调整模型角度来完成立方体的绘制及旋转效果。文章详细展示了如何设置投影矩阵、视口大小、以及模型视图变换。

效果图如上:

步骤:首先,设置模型视角往后退,再旋转视角;然后,用默认绘制立方体函数绘制;最后,利用空闲对模型做角度微调。

实现代码如下:

 1 #include <GL\glut.h>
 2 
 3 GLfloat xRotated, yRotated, zRotated;
 4 
 5 void Display(void)
 6 {
 7     glClear(GL_COLOR_BUFFER_BIT);
 8     glLoadIdentity();
 9     glTranslatef(0.0,0.0,-4.0);
10     glRotatef(xRotated,1.0,0.0,0.0);
11     glRotatef(yRotated,0.0,1.0,0.0);
12     glRotatef(zRotated,0.0,0.0,1.0);
13     //glScalef(2.0,1.0,1.0);
14     glutWireCube(1.5);
15     glFlush();            //Finish rendering
16     glutSwapBuffers();
17 }
18 
19 void Reshape(int x, int y)
20 {
21     if (y == 0 || x == 0) return;  //Nothing is visible then, so return
22     //Set a new projection matrix
23     glMatrixMode(GL_PROJECTION);  
24     glLoadIdentity();
25     //Angle of view:40 degrees
26     //Near clipping plane distance: 0.5
27     //Far clipping plane distance: 20.0
28     gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
29     glMatrixMode(GL_MODELVIEW);
30     glViewport(0,0,x,y);  //Use the whole window for rendering
31 }
32 static int times = 0;
33 void Idle(void)
34 {
35     times++;
36     if(times >30000)
37         times = 0;
38 
39     if(times %30000 == 0)
40     {
41         xRotated += 0.3;
42         yRotated += 0.1;
43         zRotated += -0.4;
44         Display();
45     }
46 }
47 
48 
49 int main (int argc, char **argv)
50 {
51     //Initialize GLUT
52     glutInit(&argc, argv);
53     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);  //For animations you should use double buffering
54     glutInitWindowSize(300,300);
55     //Create a window with rendering context and everything else we need
56     glutCreateWindow("Cube example");
57     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
58     xRotated = yRotated = zRotated = 0.0;
59     glClearColor(0.0,0.0,0.0,0.0);
60     //Assign the two used Msg-routines
61     glutDisplayFunc(Display);
62     glutReshapeFunc(Reshape);
63     glutIdleFunc(Idle);
64     //Let GLUT get the msgs
65     glutMainLoop();
66     return 0;
67 }

 

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

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、付费专栏及课程。

余额充值