[NEHE Couse] 03.My first polygon with various color
这一节跟第二节没什么大的区别,就是在原来简单面片上加了颜色,注意glColor3f()的使用以及glShadeModel(GL_SMOOTH)和glShadeModel(GL_FLAT)对颜色的控制,后面出现的glColor3f函数会覆盖前面glColor3f函数的功能。
程序代码:
1
/*
2
Introduction:Written by krisdy,finished at 2009.1.9
3
Apply to:NEHE Couse 03:My First Polygon with various color
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
13
void init(void)
14
{
15
glShadeModel(GL_SMOOTH);
16
glClearColor(0.0f,0.0f,0.0f,0.0f);
17
glClearDepth(1.0f);
18
glEnable(GL_DEPTH_TEST);
19
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
20
}
21
void display(void)
22
{
23
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
24
glMatrixMode(GL_MODELVIEW);
25
glLoadIdentity();
26
glPushMatrix();
27
glTranslatef(1.0f,1.0f,0.0f);
28
//draw a triangle with smooth color
29
glBegin(GL_TRIANGLES);
30
glColor3f(1.0f,0.0f,0.0f);
31
glVertex3f(0.0f,0.0f,0.0f);
32
glColor3f(0.0f,1.0f,0.0f);
33
glVertex3f(3.0f,0.0f,0.0f);
34
glColor3f(0.0f,0.0f,1.0f);
35
glVertex3f(1.5f,4.0f,0.0f);
36
glEnd();
37
//draw a line
38
glLineWidth(3.0);
39
glColor3f(0.5f,0.5f,1.0f);
40
glBegin(GL_LINES);
41
glVertex3f(1.5f,7.0f,0.0f);
42
glVertex3f(8.0f,7.0f,0.0f);
43
glEnd();
44
glPopMatrix();
45
glPushMatrix();
46
glTranslatef(6.0f,1.0f,0.0f);
47
//draw a rectangle,without the help of glBegin&&glEnd
48
glColor3f(1.5f,0.5f,0.5f);
49
glRectf(0.0f,0.0f,3.0f,3.0f);
50
glPopMatrix();
51
glFlush();
52
}
53
void reshape(int w,int h)
54
{
55
GLfloat range = 10.0f;
56
if(h == 0)
57
h = 1;
58
glViewport(0,0,w,h);
59
glMatrixMode(GL_PROJECTION);
60
glLoadIdentity();
61
if(w < h)
62
glOrtho(0.0f,range,0.0f,GLfloat(range*h/w),-1.0f,1.0f);
63
else
64
glOrtho(0.0f,GLfloat(range*w/h),0.0f,range,-1.0f,1.0f);
65
glMatrixMode(GL_MODELVIEW);
66
glLoadIdentity();
67
}
68
void keyboard(unsigned char key,int x,int y)
69
{
70
switch(key)
71
{
72
//use the space key to decide whether the window will be full-screen displayed.
73
case 32:
74
if(t%2 == 0)
75
//requests that the current window be made full screen
76
glutFullScreen();
77
else
78
//requests a change to the size of the current window
79
glutReshapeWindow(WinWidth,WinHeight);
80
t++;
81
break;
82
//use the Esc key to quit the application
83
case 27:
84
exit(0);
85
break;
86
default:
87
break;
88
}
89
}
90
int main(int argc,char *argv[])
91
{
92
glutInit(&argc,argv);
93
glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_SINGLE);
94
glutInitWindowSize(WinWidth,WinHeight);
95
glutInitWindowPosition(100,100);
96
glutCreateWindow("Lesson 03");
97
98
init();
99
glutDisplayFunc(display);
100
glutReshapeFunc(reshape);
101
glutKeyboardFunc(keyboard);
102
glutMainLoop();
103
return 0;
104
}

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

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