[NEHE Couse] 02.My first polygon
这节比较简单但却很重要,因为几乎所有复杂的物体都是有基本的图形面片组成的(这点会随着你对图形学的深入而变的明显起来),我在教程原来的基础上,画了一个三角面片,长方体面片和一条直线,都是OpenGL里简单却基础的知识,注意下glPushMatrix()和glPopMatrix()的使用。
代码如下:
1
/*
2
Introduction:Written by krisdy,finished at 2009.1.9
3
Apply to:NEHE Couse 02:My First Polygon
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
glColor3f(1.0f,0.0f,0.0f);
25
glMatrixMode(GL_MODELVIEW);
26
glLoadIdentity();
27
glPushMatrix();
28
glTranslatef(1.0f,1.0f,0.0f);
29
//draw a triangle
30
glBegin(GL_TRIANGLES);
31
glVertex3f(0.0f,0.0f,0.0f);
32
glVertex3f(3.0f,0.0f,0.0f);
33
glVertex3f(1.5f,4.0f,0.0f);
34
glEnd();
35
//draw a line
36
glLineWidth(3.0);
37
glBegin(GL_LINES);
38
glVertex3f(1.5f,7.0f,0.0f);
39
glVertex3f(8.0f,7.0f,0.0f);
40
glEnd();
41
glPopMatrix();
42
glPushMatrix();
43
glTranslatef(6.0f,1.0f,0.0f);
44
//draw a rectangle,without the help of glBegin&&glEnd
45
glRectf(0.0f,0.0f,3.0f,3.0f);
46
glPopMatrix();
47
glFlush();
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(0.0f,range,0.0f,GLfloat(range*h/w),-1.0f,1.0f);
59
else
60
glOrtho(0.0f,GLfloat(range*w/h),0.0f,range,-1.0f,1.0f);
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
int main(int argc,char *argv[])
87
{
88
glutInit(&argc,argv);
89
glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_SINGLE);
90
glutInitWindowSize(WinWidth,WinHeight);
91
glutInitWindowPosition(100,100);
92
glutCreateWindow("Lesson 02");
93
94
init();
95
glutDisplayFunc(display);
96
glutReshapeFunc(reshape);
97
glutKeyboardFunc(keyboard);
98
glutMainLoop();
99
return 0;
100
}
/*2
Introduction:Written by krisdy,finished at 2009.1.93
Apply to:NEHE Couse 02:My First Polygon4
*/5
#include <gl/glut.h>6
#include <stdlib.h>7

8
#define WinWidth 500 //the width of the window9
#define WinHeight 500 //the height of the window10

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
glColor3f(1.0f,0.0f,0.0f);25
glMatrixMode(GL_MODELVIEW);26
glLoadIdentity();27
glPushMatrix();28
glTranslatef(1.0f,1.0f,0.0f);29
//draw a triangle30
glBegin(GL_TRIANGLES);31
glVertex3f(0.0f,0.0f,0.0f);32
glVertex3f(3.0f,0.0f,0.0f);33
glVertex3f(1.5f,4.0f,0.0f);34
glEnd();35
//draw a line36
glLineWidth(3.0);37
glBegin(GL_LINES);38
glVertex3f(1.5f,7.0f,0.0f);39
glVertex3f(8.0f,7.0f,0.0f);40
glEnd();41
glPopMatrix();42
glPushMatrix();43
glTranslatef(6.0f,1.0f,0.0f);44
//draw a rectangle,without the help of glBegin&&glEnd45
glRectf(0.0f,0.0f,3.0f,3.0f);46
glPopMatrix();47
glFlush();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(0.0f,range,0.0f,GLfloat(range*h/w),-1.0f,1.0f);59
else60
glOrtho(0.0f,GLfloat(range*w/h),0.0f,range,-1.0f,1.0f);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 screen72
glutFullScreen(); 73
else74
//requests a change to the size of the current window75
glutReshapeWindow(WinWidth,WinHeight); 76
t++;77
break;78
//use the Esc key to quit the application79
case 27: 80
exit(0);81
break;82
default:83
break;84
}85
}86
int main(int argc,char *argv[])87
{88
glutInit(&argc,argv);89
glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_SINGLE);90
glutInitWindowSize(WinWidth,WinHeight);91
glutInitWindowPosition(100,100);92
glutCreateWindow("Lesson 02");93

94
init();95
glutDisplayFunc(display);96
glutReshapeFunc(reshape);97
glutKeyboardFunc(keyboard);98
glutMainLoop();99
return 0;100
}
程序效果如下:
我没有什么雄心壮志,我只想给自己和关心自己的家人和朋友一个交代,仅此而已。


浙公网安备 33010602011771号