Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VI

下面代码以窗体模式启动.你可以在一堆给定的设置中选择用哪个进入游戏模式,也可以返回窗体模式.命令是用位图字体显示在屏幕上.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #ifdef __APPLE__
  6. #include <GLUT/glut.h>
  7. #else
  8. #include <GL/glut.h>
  9. #endif
  10.  
  11. // angle of rotation for the camera direction
  12. float angle = 0.0f;
  13.  
  14. // actual vector representing the camera's direction
  15. float lx=0.0f,lz=-1.0f;
  16.  
  17. // XZ position of the camera
  18. float x=0.0f, z=5.0f;
  19.  
  20. // the key states. These variables will be zero
  21. // when no key is being pressesed
  22. float deltaAngle = 0.0f;
  23. float deltaMove = ;
  24. int xOrigin = -;
  25.  
  26. // color for the snowman's nose
  27. float red = 1.0f, blue=0.5f, green=0.5f;
  28.  
  29. // scale of snowman
  30. float scale = 1.0f;
  31.  
  32. // default font
  33. void *font = GLUT_STROKE_ROMAN;
  34.  
  35. // width and height of the window
  36. int h,w;
  37.  
  38. // variables to compute frames per second
  39. int frame;
  40. long time, timebase;
  41. char s[];
  42. char currentMode[];
  43.  
  44. // this string keeps the last good setting
  45. // for the game mode
  46. char gameModeString[] = "640x480";
  47.  
  48. void init();
  49.  
  50. void changeSize(int ww, int hh) {
  51.  
  52. h = hh;
  53. w = ww;
  54. // Prevent a divide by zero, when window is too short
  55. // (you cant make a window of zero width).
  56. if (h == )
  57. h = ;
  58.  
  59. float ratio = w * 1.0 / h;
  60.  
  61. // Use the Projection Matrix
  62. glMatrixMode(GL_PROJECTION);
  63.  
  64. // Reset Matrix
  65. glLoadIdentity();
  66.  
  67. // Set the viewport to be the entire window
  68. glViewport(, , w, h);
  69.  
  70. // Set the correct perspective.
  71. gluPerspective(45.0f, ratio, 0.1f, 100.0f);
  72.  
  73. // Get Back to the Modelview
  74. glMatrixMode(GL_MODELVIEW);
  75. }
  76.  
  77. void drawSnowMan() {
  78.  
  79. glScalef(scale, scale, scale);
  80. glColor3f(1.0f, 1.0f, 1.0f);
  81.  
  82. // Draw Body
  83. glTranslatef(0.0f ,0.75f, 0.0f);
  84. glutSolidSphere(0.75f,,);
  85.  
  86. // Draw Head
  87. glTranslatef(0.0f, 1.0f, 0.0f);
  88. glutSolidSphere(0.25f,,);
  89.  
  90. // Draw Eyes
  91. glPushMatrix();
  92. glColor3f(0.0f,0.0f,0.0f);
  93. glTranslatef(0.05f, 0.10f, 0.18f);
  94. glutSolidSphere(0.05f,,);
  95. glTranslatef(-0.1f, 0.0f, 0.0f);
  96. glutSolidSphere(0.05f,,);
  97. glPopMatrix();
  98.  
  99. // Draw Nose
  100. glColor3f(red, green, blue);
  101. glRotatef(0.0f,1.0f, 0.0f, 0.0f);
  102. glutSolidCone(0.08f,0.5f,,);
  103.  
  104. glColor3f(1.0f, 1.0f, 1.0f);
  105. }
  106.  
  107. void renderBitmapString(
  108. float x,
  109. float y,
  110. float z,
  111. void *font,
  112. char *string) {
  113.  
  114. char *c;
  115. glRasterPos3f(x, y,z);
  116. for (c=string; *c != '\0'; c++) {
  117. glutBitmapCharacter(font, *c);
  118. }
  119. }
  120.  
  121. void renderStrokeFontString(
  122. float x,
  123. float y,
  124. float z,
  125. void *font,
  126. char *string) {
  127.  
  128. char *c;
  129. glPushMatrix();
  130. glTranslatef(x, y,z);
  131. glScalef(0.002f, 0.002f, 0.002f);
  132. for (c=string; *c != '\0'; c++) {
  133. glutStrokeCharacter(font, *c);
  134. }
  135. glPopMatrix();
  136. }
  137.  
  138. void restorePerspectiveProjection() {
  139.  
  140. glMatrixMode(GL_PROJECTION);
  141. // restore previous projection matrix
  142. glPopMatrix();
  143.  
  144. // get back to modelview mode
  145. glMatrixMode(GL_MODELVIEW);
  146. }
  147.  
  148. void setOrthographicProjection() {
  149.  
  150. // switch to projection mode
  151. glMatrixMode(GL_PROJECTION);
  152.  
  153. // save previous matrix which contains the
  154. //settings for the perspective projection
  155. glPushMatrix();
  156.  
  157. // reset matrix
  158. glLoadIdentity();
  159.  
  160. // set a 2D orthographic projection
  161. gluOrtho2D(, w, h, );
  162.  
  163. // switch back to modelview mode
  164. glMatrixMode(GL_MODELVIEW);
  165. }
  166.  
  167. void computePos(float deltaMove) {
  168.  
  169. x += deltaMove * lx * 0.1f;
  170. z += deltaMove * lz * 0.1f;
  171. }
  172.  
  173. void renderScene(void) {
  174.  
  175. if (deltaMove)
  176. computePos(deltaMove);
  177.  
  178. // Clear Color and Depth Buffers
  179. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  180.  
  181. // Reset transformations
  182. glLoadIdentity();
  183. // Set the camera
  184. gluLookAt( x, 1.0f, z,
  185. x+lx, 1.0f, z+lz,
  186. 0.0f, 1.0f, 0.0f);
  187.  
  188. // Draw ground
  189.  
  190. glColor3f(0.9f, 0.9f, 0.9f);
  191. glBegin(GL_QUADS);
  192. glVertex3f(-100.0f, 0.0f, -100.0f);
  193. glVertex3f(-100.0f, 0.0f, 100.0f);
  194. glVertex3f( 100.0f, 0.0f, 100.0f);
  195. glVertex3f( 100.0f, 0.0f, -100.0f);
  196. glEnd();
  197.  
  198. // Draw 36 SnowMen
  199. char number[];
  200. for(int i = -; i < ; i++)
  201. for(int j=-; j < ; j++) {
  202. glPushMatrix();
  203. glTranslatef(i*10.0f, 0.0f, j * 10.0f);
  204. drawSnowMan();
  205. sprintf(number,"%d",(i+)*+(j+));
  206. renderStrokeFontString(0.0f, 0.5f, 0.0f, (void *)font ,number);
  207. glPopMatrix();
  208. }
  209.  
  210. // Code to compute frames per second
  211. frame++;
  212.  
  213. time=glutGet(GLUT_ELAPSED_TIME);
  214. if (time - timebase > ) {
  215. sprintf(s,"Lighthouse3D - FPS:%4.2f",
  216. frame*1000.0/(time-timebase));
  217. timebase = time;
  218. frame = ;
  219. }
  220.  
  221. setOrthographicProjection();
  222. void *font= GLUT_BITMAP_8_BY_13;
  223. glPushMatrix();
  224. glLoadIdentity();
  225. renderBitmapString(,,,font,(char *)"GLUT Tutorial @ Lighthouse3D");
  226. renderBitmapString(,,,font,s);
  227. renderBitmapString(,,,font,(char *)"F1 - Game Mode 640x480 32 bits");
  228. renderBitmapString(,,,font,(char *)"F2 - Game Mode 800x600 32 bits");
  229. renderBitmapString(,,,font,(char *)"F3 - Game Mode 1024x768 32 bits");
  230. renderBitmapString(,,,font,(char *)"F4 - Game Mode 1280x1024 32 bits");
  231. renderBitmapString(,,,font,(char *)"F5 - Game Mode 1920x1200 32 bits");
  232. renderBitmapString(,,,font,(char *)"F6 - Window Mode");
  233. renderBitmapString(,,,font,(char *)"Esc - Quit");
  234. renderBitmapString(,,,font,currentMode);
  235. glPopMatrix();
  236.  
  237. restorePerspectiveProjection();
  238.  
  239. glutSwapBuffers();
  240. }
  241.  
  242. // -----------------------------------
  243. // KEYBOARD
  244. // -----------------------------------
  245.  
  246. void processNormalKeys(unsigned char key, int xx, int yy) {
  247.  
  248. switch (key) {
  249. case :
  250. if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE) != )
  251. glutLeaveGameMode();
  252. exit();
  253. break;
  254. }
  255. }
  256.  
  257. void pressKey(int key, int xx, int yy) {
  258.  
  259. switch (key) {
  260. case GLUT_KEY_UP : deltaMove = 0.5f; break;
  261. case GLUT_KEY_DOWN : deltaMove = -0.5f; break;
  262. case GLUT_KEY_F1:
  263. // define resolution, color depth
  264. glutGameModeString("640x480:32");
  265. // enter full screen
  266. if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
  267. glutEnterGameMode();
  268. sprintf(gameModeString,"640x480:32");
  269. // register callbacks again
  270. // and init OpenGL context
  271. init();
  272. }
  273. else
  274. glutGameModeString(gameModeString);
  275. break;
  276. case GLUT_KEY_F2:
  277. // define resolution, color depth
  278. glutGameModeString("800x600:32");
  279. // enter full screen
  280. if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
  281. glutEnterGameMode();
  282. sprintf(gameModeString,"800x600:32");
  283. // register callbacks again
  284. // and init OpenGL context
  285. init();
  286. }
  287. else
  288. glutGameModeString(gameModeString);
  289. break;
  290. case GLUT_KEY_F3:
  291. // define resolution, color depth
  292. glutGameModeString("1024x768:32");
  293. // enter full screen
  294. if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
  295. glutEnterGameMode();
  296. sprintf(gameModeString,"1024x768:32");
  297. // register callbacks again
  298. // and init OpenGL context
  299. init();
  300. }
  301. else
  302. glutGameModeString(gameModeString);
  303. break;
  304. case GLUT_KEY_F4:
  305. // define resolution, color depth
  306. glutGameModeString("1280x1024:32");
  307. // enter full screen
  308. if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
  309. glutEnterGameMode();
  310. sprintf(gameModeString,"1280x1024:32");
  311. // register callbacks again
  312. // and init OpenGL context
  313. init();
  314. }
  315. else
  316. glutGameModeString(gameModeString);
  317. break;
  318. case GLUT_KEY_F5:
  319. // define resolution, color depth
  320. glutGameModeString("1920x1200");
  321. // enter full screen
  322. if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
  323. glutEnterGameMode();
  324. sprintf(gameModeString,"1920x1200");
  325. // register callbacks again
  326. // and init OpenGL context
  327. init();
  328. }
  329. else
  330. glutGameModeString(gameModeString);
  331. break;
  332. case GLUT_KEY_F6:
  333. // return to default window
  334. w = ;h = ;
  335. if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE) != ) {
  336. glutLeaveGameMode();
  337. //init();
  338. }
  339. break;
  340. }
  341. if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE) == )
  342. sprintf(currentMode,"Current Mode: Window");
  343. else
  344. sprintf(currentMode,
  345. "Current Mode: Game Mode %dx%d at %d hertz, %d bpp",
  346. glutGameModeGet(GLUT_GAME_MODE_WIDTH),
  347. glutGameModeGet(GLUT_GAME_MODE_HEIGHT),
  348. glutGameModeGet(GLUT_GAME_MODE_REFRESH_RATE),
  349. glutGameModeGet(GLUT_GAME_MODE_PIXEL_DEPTH));
  350. }
  351.  
  352. void releaseKey(int key, int x, int y) {
  353.  
  354. switch (key) {
  355. case GLUT_KEY_UP :
  356. case GLUT_KEY_DOWN : deltaMove = ;break;
  357. }
  358. }
  359.  
  360. // -----------------------------------
  361. // MOUSE
  362. // -----------------------------------
  363.  
  364. void mouseMove(int x, int y) {
  365.  
  366. // this will only be true when the left button is down
  367. if (xOrigin >= ) {
  368.  
  369. // update deltaAngle
  370. deltaAngle = (x - xOrigin) * 0.001f;
  371.  
  372. // update camera's direction
  373. lx = sin(angle + deltaAngle);
  374. lz = -cos(angle + deltaAngle);
  375. }
  376. }
  377.  
  378. void mouseButton(int button, int state, int x, int y) {
  379.  
  380. // only start motion if the left button is pressed
  381. if (button == GLUT_LEFT_BUTTON) {
  382.  
  383. // when the button is released
  384. if (state == GLUT_UP) {
  385. angle += deltaAngle;
  386. xOrigin = -;
  387. }
  388. else {// state = GLUT_DOWN
  389. xOrigin = x;
  390. }
  391. }
  392. }
  393.  
  394. void init() {
  395.  
  396. // register callbacks
  397. glutDisplayFunc(renderScene);
  398. glutReshapeFunc(changeSize);
  399. glutIdleFunc(renderScene);
  400.  
  401. glutIgnoreKeyRepeat();
  402. glutKeyboardFunc(processNormalKeys);
  403. glutSpecialFunc(pressKey);
  404. glutSpecialUpFunc(releaseKey);
  405. glutMouseFunc(mouseButton);
  406. glutMotionFunc(mouseMove);
  407.  
  408. // OpenGL init
  409. glEnable(GL_DEPTH_TEST);
  410. glEnable(GL_CULL_FACE);
  411. }
  412.  
  413. // -----------------------------------
  414. // MAIN
  415. // -----------------------------------
  416.  
  417. int main(int argc, char **argv) {
  418.  
  419. // init GLUT and create window
  420. glutInit(&argc, argv);
  421. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  422. glutInitWindowPosition(,);
  423. glutInitWindowSize(,);
  424. glutCreateWindow("Lighthouse3D - GLUT Tutorial");
  425.  
  426. // register callbacks
  427. init();
  428.  
  429. // enter GLUT event processing cycle
  430. glutMainLoop();
  431.  
  432. return ;
  433. }

[译]GLUT教程 - 整合代码6的更多相关文章

  1. [译]GLUT教程 - 整合代码2

    Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far II 以下是前面几节的完整整合代码: # ...

  2. [译]GLUT教程 - 整合代码1

    Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far 以下是前面几节的完整整合代码: #inc ...

  3. [译]GLUT教程 - 整合代码8

    Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> The Code So Far VII ...

  4. [译]GLUT教程 - 整合代码7

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VII 以下是子窗体的最终版本代码. ...

  5. [译]GLUT教程 - 整合代码5

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far V 该代码与位图字体的代码类似.区别是 ...

  6. [译]GLUT教程 - 整合代码4

    Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far IV 以下代码使用了位图字 ...

  7. [译]GLUT教程 - 整合代码3

    Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> The Code So Far III 这里我们准备包含一 ...

  8. [译]GLUT教程(目录)

    http://www.lighthouse3d.com/tutorials/glut-tutorial/ GLUT是OpenGL Utility Toolkit的意思.作者Mark J. Kilgar ...

  9. [译]GLUT教程 - 游戏模式

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> Game Mode 根据GLUT官网的说明,GLUT的游戏模式是为开启 ...

随机推荐

  1. 【贪心】Mixing Milk

    题目描述 The Merry Milk Makers company buys milk from farmers, packages it into attractive 1- and 2-Unit ...

  2. 网页结构——head标签内

    之前写网页都很标准的格式,最近一个项目出现了页面闪动等一系列问题[项目不是前后端分离], 所以这边有后台的功劳,有部分后台是不管你页面结构的,在他们操作的时候可能会在,你的head内meta前加内联c ...

  3. lua的模式匹配

    模式: 字符类:(character classes) . all characters %a letters %c control characters %d digits %l lower -ca ...

  4. 每天一个liunx命令3之awk实现文本文件的抓取

    =============================================================================grep -h -s -E 'HUAWEI_9 ...

  5. 正规化方程Normal Equations解析

    如果需要代做算法,可以联系我...博客右侧有联系方式. 一.正规化方程概念 假设我们有m个样本.特征向量的维度为n.因此,可知样本为{(x(1),y(1)), (x(2),y(2)),... ..., ...

  6. Delphi7 实现窗体全屏方法

    设置要全屏的窗体的ALign 属性为ALcLient ,此法最快.当然对我来说,我并不知道这个,所以走了远路,等后来在实现窗体禁止移动的时候才想到了这里,汗.注意:这种全屏方式不会挡了系统的任务栏.. ...

  7. webpack2.x基础属性讲解

    webpack作为构建工具平时作为前端作为优化.模块编程.和分片打包的重要组成部分,大家可能并不陌生,如果没有时刻的去关注文档,那么大家可能不太清楚webpack已经默默然的升级到2.x了,对比1.x ...

  8. java程序监控tomcat中部署的项目的状态以及控制某些项目的启动停止

    原文:http://blog.csdn.net/liuyuqin1991/article/details/49280777 步骤如下: ①:首先授权用户使获得这些权限 You can find the ...

  9. 发布Android开源库,看这个文章就够了!

    最近在Flipboard实习期间写了一个轮播工具,技术上没什么难点,不过动画效果还是不错的,决定改改代码写个库开源出去.项目地址:http://github.com/chengdazhi/Decent ...

  10. Git:fatal: The remote end hung up unexpectedly

    一.配置公共密钥 https://help.github.com/articles/generating-ssh-keys/ 二.设置缓冲值(push文件较大时导致错误) \.git\config [ ...