[NEHE Couse] 04.Rotating objects
没有什么新内容,注意glRotatef函数的使用就可以了,我在程序中为了效果,引入了glutIdleFunc函数的使用,具体用法大家可以google下。此外为了让三角面片旋转时不至于旋转出视线,我把glOrtho的参数也修改了。点击鼠标左键物体开始旋转,点击右键旋转停止。
程序如下:
1
/*
2
Introduction:Written by krisdy,finished at 2009.1.9
3
Apply to:NEHE Couse 04:Rotating objects
4
*/
5
#include <gl/glut.h>
6
#include <stdlib.h>
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
#define WinWidth 500 //the width of the window
9
#define WinHeight 500 //the height of the window
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
static GLint t;
12
static GLfloat spin_x,spin_y; //control the rotate range
13![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
void init(void)
15
{
16
glShadeModel(GL_SMOOTH);
17
glClearColor(0.0f,0.0f,0.0f,0.0f);
18
glClearDepth(1.0f);
19
glEnable(GL_DEPTH_TEST);
20
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
21
}
22
void display(void)
23
{
24
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
25
glMatrixMode(GL_MODELVIEW);
26
glLoadIdentity();
27
glPushMatrix();
28
glTranslatef(1.0f,1.0f,0.0f);
29
glRotatef(spin_x,1.0f,0.0f,0.0f);
30
//draw a triangle with smooth color
31
glBegin(GL_TRIANGLES);
32
glColor3f(1.0f,0.0f,0.0f);
33
glVertex3f(0.0f,0.0f,0.0f);
34
glColor3f(0.0f,1.0f,0.0f);
35
glVertex3f(3.0f,0.0f,0.0f);
36
glColor3f(0.0f,0.0f,1.0f);
37
glVertex3f(1.5f,4.0f,0.0f);
38
glEnd();
39
glPopMatrix();
40
glPushMatrix();
41
glTranslatef(6.0f,1.0f,0.0f);
42
glRotatef(spin_y,0.0f,1.0f,0.0f);
43
//draw a rectangle,without the help of glBegin&&glEnd
44
glColor3f(1.5f,0.5f,0.5f);
45
glRectf(0.0f,0.0f,3.0f,3.0f);
46
glPopMatrix();
47
glutSwapBuffers(); //apply to the moving objects,pay attention to it
48
}
49
void reshape(int w,int h)
50
{
51
GLfloat range = 10.0f;
52
if(h == 0)
53
h = 1;
54
glViewport(0,0,w,h);
55
glMatrixMode(GL_PROJECTION);
56
glLoadIdentity();
57
if(w < h)
58
glOrtho(-range,range,-GLfloat(range*h/w),GLfloat(range*h/w),-range,range);
59
else
60
glOrtho(-GLfloat(range*w/h),GLfloat(range*w/h),-range,range,-range,range);
61
glMatrixMode(GL_MODELVIEW);
62
glLoadIdentity();
63
}
64
void keyboard(unsigned char key,int x,int y)
65
{
66
switch(key)
67
{
68
//use the space key to decide whether the window will be full-screen displayed.
69
case 32:
70
if(t%2 == 0)
71
//requests that the current window be made full screen
72
glutFullScreen();
73
else
74
//requests a change to the size of the current window
75
glutReshapeWindow(WinWidth,WinHeight);
76
t++;
77
break;
78
//use the Esc key to quit the application
79
case 27:
80
exit(0);
81
break;
82
default:
83
break;
84
}
85
}
86
void spinDisplay(void)
87
{
88
spin_x = spin_x + 2.0f;
89
spin_y = spin_y + 2.0f;
90
if(spin_x > 360.0f)
91
spin_x = spin_x - 360.0f;
92
if(spin_y > 360.0f)
93
spin_y = spin_y - 360.0f;
94
glutPostRedisplay();
95
}
96
void mouse(int button,int state,int x,int y)
97
{
98
switch(button)
99
{
100
//click the left button to let the object move
101
case GLUT_LEFT_BUTTON:
102
if(state == GLUT_DOWN)
103
glutIdleFunc(spinDisplay);
104
break;
105
//click the right button to let the object stop moving
106
case GLUT_RIGHT_BUTTON:
107
if(state == GLUT_DOWN)
108
glutIdleFunc(NULL);
109
break;
110
default:
111
break;
112
}
113
}
114
int main(int argc,char *argv[])
115
{
116
glutInit(&argc,argv);
117
glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE);
118
glutInitWindowSize(WinWidth,WinHeight);
119
glutInitWindowPosition(100,100);
120
glutCreateWindow("Lesson 04");
121![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
122
init();
123
glutDisplayFunc(display);
124
glutReshapeFunc(reshape);
125
glutKeyboardFunc(keyboard);
126
glutMouseFunc(mouse);
127
glutMainLoop();
128
return 0;
129
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
113
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
114
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
115
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
116
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
119
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
120
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
121
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
122
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
124
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
126
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
128
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
效果图如下:
我没有什么雄心壮志,我只想给自己和关心自己的家人和朋友一个交代,仅此而已。