利用OpenGL创建一个3D空间,便于观察三维的曲面。程序实现:
1. 设置投影使用 glOrtho(),而不是gluOrtho2D();
2. 增加光照效果;
3. 画出三维坐标轴,且可通过方向键来旋转视图;
源程序有三个文件:Main.cpp、CordinateAxis.h、CordinateAxis.cpp;其中类CCordinateAxis用来绘坐标轴。
程序代码如下:
1: // Main.cpp
2: // OpenGL 3D Environment
3:
4: #include <gl\glut.h>
5: #include <iostream>
6: #include "CordinateAxis.h"
7: using namespace std;
8:
9: void Initialize(void);
10: void DrawScene(void);
11: void myReshape(GLsizei w, GLsizei h);
12: void myKeyFunc(GLubyte key, GLint x, GLint y);
13: void mySpecFunc(GLint key, GLint x, GLint y);
14:
15: GLint leftRight = 0;
16: GLint upDown = 0;
17:
18: void main(int argc, char* argv[]) {
19: glutInit(&argc, argv); // Initialize GLUT
20: glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); // Set display mode
21: glutInitWindowPosition(50,100); // Set top-left display window position
22: glutInitWindowSize(400, 300); // set display window width and height
23: glutCreateWindow("OpenGL 3D"); // Create display window
24:
25: Initialize(); // Execute initialization procedure
26: glutDisplayFunc(DrawScene); // Send graphics to display window
27: glutReshapeFunc(myReshape); //
28: glutKeyboardFunc(myKeyFunc);
29: glutSpecialFunc(mySpecFunc);
30:
31: glutMainLoop(); // Display everything and wait
32: }
33:
34: void Initialize(void) {
35: glClearColor(0.0, 0.0, 0.0, 0.0); // Set Display-window color to white
36: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
37: glMatrixMode(GL_PROJECTION); // Set projection parameters
38: glLoadIdentity();
39:
40: // Set light
41: GLfloat ambientLight[] = {1.0f, 1.0f, 1.0f, 1.0f};
42: GLfloat diffuseLight[] = {1.0f, 0.2f, 1.0f, 1.0f};
43: GLfloat specularLight[] = {1.0f, 1.0f, 1.0f, 1.0f};
44: GLfloat shininess[] = {90.0f};
45: GLfloat lightPos[] = {800.0f, 800.0f, 900.0f, 1.0f};
46:
47: // Setup and enable light 0
48: // glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
49: // glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
50: // glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
51:
52: glMaterialfv(GL_FRONT, GL_AMBIENT, ambientLight);
53: glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseLight);
54: glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight);
55: glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
56: glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
57:
58: // Enable lighting
59: glEnable(GL_LIGHTING);
60: glEnable(GL_LIGHT0);
61: glDepthFunc(GL_LESS);
62: glEnable(GL_DEPTH_TEST);
63:
64: // Enable color tracking
65: glEnable(GL_COLOR_MATERIAL);
66:
67: //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
68: } // Initialize
69:
70: void myReshape(GLsizei w, GLsizei h) {
71: // Reset viewport and projection parameter
72: glViewport(0, 0, w, h);
73: glMatrixMode(GL_PROJECTION);
74: glLoadIdentity();
75: glOrtho(0, w, 0, h, -500, 500);
76: glMatrixMode(GL_MODELVIEW);
77: } // myReshape
78:
79: void myKeyFunc(GLubyte key, GLint x, GLint y) {
80: cout<<key<<x<<y<<endl;
81: } // myKeyFunc
82:
83: void mySpecFunc(GLint key, GLint x, GLint y) {
84:
85: switch(key) {
86: case GLUT_KEY_LEFT:
87: leftRight -= 1;
88: break;
89:
90: case GLUT_KEY_RIGHT:
91: leftRight += 1;
92: break;
93:
94: case GLUT_KEY_UP:
95: upDown -= 1;
96: break;
97:
98: case GLUT_KEY_DOWN:
99: upDown += 1;
100: break;
101:
102: default:
103: glutPostRedisplay();
104: }
105:
106: glutPostRedisplay();
107: } // mySpecFunc
108:
109: void DrawScene(void) {
110: glClear(GL_COLOR_BUFFER_BIT); // Clear display window
111: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
112: glEnable(GL_AUTO_NORMAL);
113:
114: glPushMatrix();
115:
116: glRotatef((GLfloat)leftRight, 0.0, 1.0, 0.0);
117: glRotatef((GLfloat)upDown, 1.0, 0.0, 0.0);
118:
119: //
120: glTranslatef(50.0, 50.0, 0.0);
121: CCordinateAxis axis;
122: axis.SetAxisPosition(0, 0, 0);
123: axis.SetAxisSize(100);
124: axis.Show();
125:
126: glTranslatef(100.0f, 100.0f, 100.0f);
127: glColor3f(1.0f, 0.8f, 0.0f);
128: //glutWireCube(100.0);
129: glutSolidTeapot(80);
130:
131: glPopMatrix();
132:
133: glFlush(); // Process all OpenGL routines as quickly possible
134: } // DrawScene
1: // CordinateAxis.h: interface for the CCordinateAxis class.
2: //
3: //////////////////////////////////////////////////////////////////////
4:
5: #ifndef _CORDINATEAXIS_H_
6: #define _CORDINATEAXIS_H_
7:
8: #pragma once
9:
10: #include <GL/glut.h>
11: #include <iostream>
12: using namespace std;
13:
14: class CCordinateAxis
15: {
16: public:
17: CCordinateAxis();
18: CCordinateAxis(double x, double y, double z);
19: virtual ~CCordinateAxis();
20:
21: void SetAxisPosition(double x, double y, double z);
22: void SetAxisSize(double size);
23: void Show(void);
24:
25: private:
26: double m_xPos;
27: double m_yPos;
28: double m_zPos;
29:
30: double m_Size;
31: };
32:
33: #endif // _CORDINATEAXIS_H_
1: // CordinateAxis.cpp: implementation of the CCordinateAxis class.
2: //
3: //////////////////////////////////////////////////////////////////////
4:
5: #include "CordinateAxis.h"
6:
7: CCordinateAxis::CCordinateAxis()
8: {
9: m_xPos = 0;
10: m_yPos = 0;
11: m_zPos = 0;
12:
13: m_Size = 80;
14: }
15:
16: CCordinateAxis::CCordinateAxis( double x, double y, double z )
17: {
18: m_xPos = x;
19: m_yPos = y;
20: m_zPos = z;
21: }
22:
23: CCordinateAxis::~CCordinateAxis()
24: {
25:
26: }
27:
28: void CCordinateAxis::SetAxisPosition( double x, double y, double z )
29: {
30: m_xPos = x;
31: m_yPos = y;
32: m_zPos = z;
33: }
34:
35: void CCordinateAxis::SetAxisSize( double size )
36: {
37: m_Size = size;
38: }
39:
40: void CCordinateAxis::Show( void )
41: {
42: GLdouble radius = m_Size / 12;
43: GLdouble height = radius * 2.2;
44:
45: glPushMatrix();
46: glTranslatef(m_xPos, m_yPos, m_zPos);
47:
48: glBegin(GL_LINES);
49: glVertex3f(0, 0, 0);
50: glVertex3f(m_Size, 0, 0);
51:
52: glVertex3f(0, 0, 0);
53: glVertex3f(0, m_Size, 0);
54:
55: glVertex3f(0, 0, 0);
56: glVertex3f(0, 0, m_Size);
57: glEnd();
58:
59: glutSolidSphere(radius/2, 10, 10);
60:
61: // x axis arrow
62: glTranslatef(m_Size, 0, 0);
63: glRotatef(90, 0, 1, 0);
64: glColor3f(1, 0, 0);
65: glutSolidCone(radius, height, 10, 10);
66:
67: // y axis arrow
68: glTranslatef(0, m_Size, -m_Size);
69: glRotatef(-90, 1, 0, 0);
70: glColor3f(0, 1, 0);
71: glutSolidCone(radius, height, 10, 10);
72:
73: // z axis arrow
74: glTranslatef(-m_Size, 0, -m_Size);
75: glRotatef(-90, 0, 1, 0);
76: glColor3f(0, 0, 1);
77: glutSolidCone(radius, height, 10, 10);
78:
79: glPopMatrix();
80: }
程序效果图