[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
8
#define WinWidth 500 //the width of the window
9
#define WinHeight 500 //the height of the window
10
11
static GLint t;
12
static GLfloat spin_x,spin_y; //control the rotate range
13
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
122
init();
123
glutDisplayFunc(display);
124
glutReshapeFunc(reshape);
125
glutKeyboardFunc(keyboard);
126
glutMouseFunc(mouse);
127
glutMainLoop();
128
return 0;
129
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

效果图如下:
我没有什么雄心壮志,我只想给自己和关心自己的家人和朋友一个交代,仅此而已。