[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![](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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
init();
99
glutDisplayFunc(display);
100
glutReshapeFunc(reshape);
101
glutKeyboardFunc(keyboard);
102
glutMainLoop();
103
return 0;
104
}
![](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/ExpandedBlockStart.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.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/ExpandedBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.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/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.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/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.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/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.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/InBlock.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.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/ExpandedBlockEnd.gif)
程序效果:
我没有什么雄心壮志,我只想给自己和关心自己的家人和朋友一个交代,仅此而已。