Glut框架示例

Glut框架示例

本文遵循“署名-非商业用途-保持一致”创作公用协议

有了glut省事不少,不用去管烦琐的窗口创建,消息循环什么的,整了一个glut框架,作为以后demo的框架。

 
上图:
 
贴代码:

  1 //=============================================================
  2 // 包含头文件
  3 //=============================================================
  4 #include <gl/glew.h>    // opengl 扩展封装头文件
  5 #include <gl/glut.h>    // opengl 实用工具头文件
  6 
  7 //=============================================================
  8 // 导入lib
  9 //=============================================================
 10 #pragma comment(lib, "glew32.lib")
 11 
 12 //=============================================================
 13 // 函数声明
 14 //=============================================================
 15 void Initialize();        // 初始化应用程序
 16 void Uninitialize();    // 清理应用程序
 17 
 18 void DrawFunc();                                    // glut描绘回调函数
 19 void ReshapeFunc(int width, int height);            // glut窗口重置回调函数
 20 void KeyboardFunc(unsigned char key, int x, int y);    // glut键盘回调函数
 21 void MouseFunc(int button, int state, int x, int y);// glut鼠标按下与释放回调函数
 22 void MotionFunc(int x, int y);                        // glut鼠标移动回调函数
 23 void IdleFunc();                                    // glut空闲处理回调函数
 24 
 25 void DrawText(const char* text, float x, float y);    // 在屏幕上显示文本
 26 
 27 //=============================================================
 28 // 全局变量
 29 //=============================================================
 30 const static float EPSILON = 0.000001f;
 31 const static float PI = 3.1415926f;
 32 const static float PI_2 = 6.2831852f;
 33 
 34 // 窗口相关
 35 const static int g_WindowPosX = 240;
 36 const static int g_WindowPosY = 200;
 37 int g_WindowWidth = 512;
 38 int g_WindowHeight = 384;
 39 const char* g_WindowTitle = "OpenGL 飘飘白云(www.cnblogs.com/kesalin)";
 40 
 41 // 显示模式:双缓冲,RGBA,深度缓存
 42 const static int g_DisplayMode = (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);    // GLUT_STENCIL GLUT_ACCUM
 43 const static float g_fov = 60.0f;
 44 
 45 float g_rotateY = 0.0f;        // 绕Y轴旋转变量
 46 float g_posX = 0.0f;
 47 
 48 //=============================================================
 49 // main 函数
 50 //=============================================================
 51 int main(int argc, char** argv)
 52 {
 53     // 初始化opengl, glut, glew
 54     glutInit(&argc, argv);
 55     glutInitDisplayMode(g_DisplayMode);   
 56     glewInit();
 57 
 58     // 创建window
 59     glutInitWindowPosition(g_WindowPosX, g_WindowPosY);
 60     glutInitWindowSize(g_WindowWidth, g_WindowHeight);
 61     glutCreateWindow(g_WindowTitle);
 62     
 63     // 初始化应用程序
 64     Initialize() ;
 65     
 66     // 设定glut回调函数
 67     glutDisplayFunc(DrawFunc);
 68     glutReshapeFunc(ReshapeFunc);
 69     glutKeyboardFunc(KeyboardFunc);
 70     glutIdleFunc(IdleFunc);
 71 
 72     // 进入glut事件处理循环
 73     glutMainLoop();
 74 
 75     // 清理应用程序
 76     Uninitialize();
 77     
 78     return 0;
 79 }
 80 
 81 //=============================================================
 82 // 函数定义
 83 //=============================================================
 84 
 85 // 在屏幕上显示文本,x 和 y 为屏幕坐标
 86 void DrawText(const char* text, float x, float y)
 87 {
 88     // 检查OpenGL状态
 89     bool isDepthOpen = false;
 90     bool isStencilOpen = false;
 91     bool isLightOpen = false;
 92     bool isFogOpen = false;
 93     
 94     if(glIsEnabled(GL_DEPTH_TEST)) 
 95     {
 96         isDepthOpen = true;
 97         glDisable(GL_DEPTH_TEST);
 98     }
 99     if(glIsEnabled(GL_STENCIL_TEST))
100     {
101         isStencilOpen = true;
102         glDisable(GL_STENCIL_TEST);
103     }
104     if(glIsEnabled(GL_LIGHTING)) 
105     {
106         isLightOpen = true;
107         glDisable(GL_LIGHTING);
108     }
109     if(glIsEnabled(GL_FOG))
110     {
111         isFogOpen = true;
112         glDisable(GL_FOG);
113     }
114     
115     int font = (int)GLUT_BITMAP_8_BY_13;
116 
117     // 设置字体颜色
118     glColor3f(1.01.00.0);
119     
120     /*
121      * 设置正投影
122      */
123 
124     glMatrixMode(GL_PROJECTION);
125 
126     // 保存当前投影矩阵
127     glPushMatrix();
128 
129         glLoadIdentity();
130         gluOrtho2D( 0, g_WindowWidth, 0, g_WindowHeight );
131 
132         // 反转Y轴(朝下为正方向)(与窗口坐标一致)
133         glScalef(1-11);
134         // 将原点移动到屏幕左上方(与窗口坐标一致)
135         glTranslatef(0-g_WindowHeight, 0);
136         glMatrixMode(GL_MODELVIEW);
137         
138         // 保存当前模型视图矩阵
139         glPushMatrix();
140             glLoadIdentity();
141             
142             // 设置文字位置
143             glRasterPos2f( x, y );
144             
145             // 依次描绘所有字符(使用显示列表)
146             for(const char* c = text; *!= '\0'; c++)
147             {
148                 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *c);
149             }
150 
151         // 恢复之前保存的模型视图矩阵
152         glPopMatrix();
153         
154         glMatrixMode(GL_PROJECTION);
155     
156     // 恢复之前保存的投影矩阵
157     glPopMatrix();
158 
159     // 返回模型视图矩阵状态
160     glMatrixMode(GL_MODELVIEW);
161     
162     // 恢复OpenGL状态
163     if(isDepthOpen) 
164     {
165         glEnable(GL_DEPTH_TEST);
166     }
167     if(isStencilOpen)
168     {
169         glEnable(GL_STENCIL_TEST);
170     }
171     if(isLightOpen) 
172     {
173         glEnable(GL_LIGHTING);
174     }
175     if(isFogOpen)
176     {
177         glEnable(GL_FOG);
178     }
179 }
180 
181 // 初始化应用程序
182 void Initialize()
183 {
184     // 设置清屏颜色
185     glClearColor (0.00.00.00.0);
186 
187     glShadeModel(GL_SMOOTH);
188     glEnable(GL_DEPTH_TEST);
189     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
190 
191     // 设置视口,投影信息
192     glMatrixMode(GL_PROJECTION);
193     glLoadIdentity();
194     gluPerspective(g_fov, (GLfloat)g_WindowWidth/g_WindowHeight, 1.030.0);
195 }
196 
197 // 清理应用程序
198 void Uninitialize()
199 {
200     // nothing
201 }
202 
203 // 空闲处理
204 void IdleFunc()
205 {
206     g_rotateY += 0.2f;
207 
208     // 请求重绘
209     glutPostRedisplay();
210 }
211 
212 // 描绘函数
213 void DrawFunc()
214 {
215     // 清屏
216     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
217 
218     glPushMatrix();
219         glTranslatef(g_posX, 0.0f-5.0f);
220 
221         // 描绘圆环
222         glPushMatrix();
223             glColor3f (0.0f0.0f1.0f);
224             glRotatef(g_rotateY, 0.0f1.0f0.0f);
225             glutSolidTorus(0.2f ,1.0f2040);
226         glPopMatrix();
227 
228         // 描绘矩形
229         glPushMatrix();
230             glColor3f (0.0f1.0f1.0f);
231             glBegin(GL_QUADS);
232                 glVertex3f (2.0f-1.5f2.0f);
233                 glVertex3f (-2.0f-1.5f2.0);
234                 glVertex3f (-2.0f-1.5f-2.0);
235                 glVertex3f (2.0f-1.50f-2.0);
236             glEnd();
237         glPopMatrix();
238 
239     glPopMatrix();
240 
241     // 显示文本信息
242     float x = 10.0f;
243     float y = 20.0f;
244     DrawText("Author: kesalin@gmail.com", x, y);
245     DrawText("Push \'A\' or \'D\' to move the torus.", x, y + 20.0f);
246 
247     // 交换显示缓冲区
248     glutSwapBuffers() ;
249 }
250 
251 // 响应窗口重置事件
252 void ReshapeFunc(int width, int height)
253 {
254     g_WindowWidth = width;
255     g_WindowHeight = height;
256 
257     glViewport(00, g_WindowWidth, g_WindowHeight);
258     glMatrixMode(GL_PROJECTION);
259     glLoadIdentity();
260     gluPerspective(g_fov, (GLfloat)g_WindowWidth/g_WindowHeight, 1.030.0);
261 
262     glMatrixMode(GL_MODELVIEW);
263     glLoadIdentity();
264 }
265 
266 // 响应按键事件
267 void KeyboardFunc(unsigned char key, int x, int y)
268 {
269     switch (key)
270     {
271         case 'A':
272         case 'a':
273             // 向左移动
274             g_posX -= 0.05f;
275             if( g_posX < -4.0f )
276             {
277                 g_posX = -4.0f;
278             }
279             break;
280 
281         case 'D':
282         case 'd':
283             // 向右移动
284             g_posX += 0.05f;
285             if( g_posX > 4.0f )
286             {
287                 g_posX = 4.0f;
288             }
289             break;
290 
291         default:
292             break;
293     }
294 }
295 
296 // 响应鼠标按下与释放事件
297 void MouseFunc(int button, int state, int x, int y)
298 {
299 #if 0
300     switch( button )
301     {
302         case GLUT_LEFT_BUTTON:
303         {
304             if( state == GLUT_UP )
305             {
306             }
307             else if( state == GLUT_DOWN )
308             {
309             }
310         }
311         break;
312 
313         case GLUT_RIGHT_BUTTON:
314         {
315             if( state == GLUT_UP )
316             {
317             }
318             else if( state == GLUT_DOWN )
319             {
320             }
321         }
322         break;
323     
324         case GLUT_MIDDLE_BUTTON:
325         {
326             if( state == GLUT_UP )
327             {
328             }
329             else if( state == GLUT_DOWN )
330             {
331             }
332         }
333         break;
334     }
335 #endif
336 }
337 
338 // 响应鼠标移动事件
339 void MotionFunc(int x, int y)
340 {
341     // nothing
342 }
343 
posted @ 2009-09-11 16:55  飘飘白云  阅读(2931)  评论(0编辑  收藏  举报
本博客遵循 Creative Commons License “署名-非商业用途-保持一致”创作共用协议。 与我联系