Advertisement
hasegawa

bunny

Sep 20th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.13 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <stdio.h>
  3.  
  4. //static unsigned int fps_start=0;
  5. //static unsigned int fps_frames=0;
  6. //  The number of frames
  7. int frameCount = 0;
  8. float fps = 0;
  9.  
  10. //  currentTime - previousTime is the time elapsed
  11. //  between every call of the Idle function
  12. int currentTime = 0, previousTime = 0;
  13.  
  14. //  printf prints to file. printw prints to window
  15. //void printw (float x, float y, float z, char* format, ...);
  16.  
  17. void calculateFPS();
  18.  
  19. void drawFPS();
  20.  
  21. #if defined(WIN32)
  22. //#  pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
  23. #  include "glut.h"
  24. #  include "glext.h"
  25. PFNGLGENBUFFERSPROC glGenBuffers;
  26. PFNGLISBUFFERPROC glIsBuffer;
  27. PFNGLBINDBUFFERPROC glBindBuffer;
  28. PFNGLBUFFERDATAPROC glBufferData;
  29. PFNGLBUFFERSUBDATAPROC glBufferSubData;
  30. PFNGLMAPBUFFERPROC glMapBuffer;
  31. PFNGLUNMAPBUFFERPROC glUnmapBuffer;
  32. PFNGLDELETEBUFFERSPROC glDeleteBuffers;
  33. #elif defined(__APPLE__) || defined(MACOSX)
  34. #  include <GLUT/glut.h>
  35. #else
  36. #  include <GL/glut.h>
  37. #endif
  38.  
  39. /*
  40. ** トラックボール処理用関数の宣言
  41. */
  42. #include "trackball.h"
  43.  
  44. /*
  45. ** 形状データ
  46. */
  47. #include "Obj.h"
  48. Obj *data;
  49.  
  50. /*
  51. ** 視点の距離
  52. */
  53. static double offset = -200.0;
  54.  
  55. /*
  56. ** 光源
  57. */
  58. static const GLfloat lightpos[] = { 0.0, 0.0, 1.0, 0.0 };  /* 位置 */
  59.  
  60. /*
  61. ** 初期化
  62. */
  63. static void init(void)
  64. {
  65. #if defined(WIN32)
  66.   glGenBuffers =
  67.     (PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffers");
  68.   glIsBuffer =
  69.     (PFNGLISBUFFERPROC)wglGetProcAddress("glIsBuffer");
  70.   glBindBuffer =
  71.     (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
  72.   glBufferData =
  73.     (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
  74.   glBufferSubData =
  75.     (PFNGLBUFFERSUBDATAPROC)wglGetProcAddress("glBufferSubData");
  76.   glMapBuffer =
  77.     (PFNGLMAPBUFFERPROC)wglGetProcAddress("glMapBuffer");
  78.   glUnmapBuffer =
  79.     (PFNGLUNMAPBUFFERPROC)wglGetProcAddress("glUnmapBuffer");
  80.   glDeleteBuffers =
  81.     (PFNGLDELETEBUFFERSPROC)wglGetProcAddress("glDeleteBuffers");
  82. #endif
  83.  
  84.   /* 初期設定 */
  85.   glClearColor(0.3, 0.3, 1.0, 1.0);
  86.   glEnable(GL_DEPTH_TEST);
  87.   glDisable(GL_CULL_FACE);
  88.  
  89.   /* 光源の初期設定 */
  90.   glEnable(GL_LIGHTING);
  91.   glEnable(GL_LIGHT0);
  92.  
  93.   /* 形状データオブジェクトの作成 */
  94.   data = new Obj("bunny.obj");
  95. }
  96.  
  97. /*
  98. ** シーンの描画
  99. */
  100. void scene(void)
  101. {
  102.   data->draw();
  103. }
  104.  
  105. /****************************
  106. ** GLUT のコールバック関数 **
  107. ****************************/
  108.  
  109. static void display(void)
  110. {
  111.   /* モデルビュー変換行列の設定 */
  112.   glMatrixMode(GL_MODELVIEW);
  113.   glLoadIdentity();
  114.  
  115.   /* 視点の移動(物体の方を奥に移動)*/
  116.   glTranslated(0.0, 0.0, offset);
  117.  
  118.   /* 光源の位置を設定 */
  119.   glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
  120.  
  121.   /* トラックボール処理による回転 */
  122.   glMultMatrixd(trackballRotation());
  123.  
  124.   /* 画面クリア */
  125.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  126.  
  127.   /* シーンの描画 */
  128.   scene();
  129.  
  130.   /* ダブルバッファリング */
  131.   glutSwapBuffers();
  132. }
  133.  
  134. static void resize(int w, int h)
  135. {
  136.   /* トラックボールする範囲 */
  137.   trackballRegion(w, h);
  138.  
  139.   /* ウィンドウ全体をビューポートにする */
  140.   glViewport(0, 0, w, h);
  141.  
  142.   /* 透視変換行列の指定 */
  143.   glMatrixMode(GL_PROJECTION);
  144.  
  145.   /* 透視変換行列の初期化 */
  146.   glLoadIdentity();
  147.   gluPerspective(60.0, (double)w / (double)h, 1.0, 500.0);
  148. }
  149.  
  150. static void idle(void)
  151. {
  152.   /* 画面の描き替え */
  153.   glutPostRedisplay();
  154. }
  155.  
  156. static void mouse(int button, int state, int x, int y)
  157. {
  158.   switch (button) {
  159.   case GLUT_LEFT_BUTTON:
  160.     switch (state) {
  161.     case GLUT_DOWN:
  162.       /* トラックボール開始 */
  163.       trackballStart(x, y);
  164.       glutIdleFunc(idle);
  165.       break;
  166.     case GLUT_UP:
  167.       /* トラックボール停止 */
  168.       glutIdleFunc(0);
  169.       trackballStop(x, y);
  170.       break;
  171.     default:
  172.       break;
  173.     }
  174.     break;
  175.     default:
  176.       break;
  177.   }
  178. }
  179.  
  180. static void motion(int x, int y)
  181. {
  182.   /* トラックボール移動 */
  183.   trackballMotion(x, y);
  184. }
  185.  
  186. static void keyboard(unsigned char key, int x, int y)
  187. {
  188.   switch (key) {
  189.   case '+':
  190.     offset += 10.0;
  191.     glutPostRedisplay();
  192.     break;
  193.   case '-':
  194.     offset -= 10.0;
  195.     glutPostRedisplay();
  196.     break;
  197.   case 'q':
  198.   case 'Q':
  199.   case '\033':
  200.     /* ESC か q か Q をタイプしたら終了 */
  201.     exit(0);
  202.   default:
  203.     break;
  204.   }
  205. }
  206.  
  207. //-------------------------------------------------------------------------
  208. // Calculates the frames per second
  209. //-------------------------------------------------------------------------
  210.  
  211. void calculateFPS()
  212. {
  213.     //  Increase frame count
  214.     frameCount++;
  215.    
  216.     //  Get the number of milliseconds since glutInit called
  217.     //  (or first call to glutGet(GLUT ELAPSED TIME)).
  218.     currentTime = glutGet(GLUT_ELAPSED_TIME);
  219.    
  220.     //  Calculate time passed
  221.     int timeInterval = currentTime - previousTime;
  222.    
  223.     if(timeInterval > 1000)
  224.     {
  225.         //  calculate the number of frames per second
  226.         fps = frameCount / (timeInterval / 1000.0f);
  227.        
  228.         //  Set time
  229.         previousTime = currentTime;
  230.        
  231.         //  Reset frame count
  232.         frameCount = 0;
  233.     }
  234.    
  235.      
  236.    
  237. }
  238. //-------------------------------------------------------------------------
  239. //  Draw FPS
  240. //-------------------------------------------------------------------------
  241.  
  242. void drawFPS()
  243. {
  244.     //  Load the identity matrix so that FPS string being drawn
  245.     //  won't get animates
  246.     glLoadIdentity ();
  247.    
  248.      fps=glutGet(GLUT_ELAPSED_TIME);
  249.    
  250.     //  Print the FPS to the window
  251.     printf ("FPS: %4.2f\n", fps);
  252. }
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259. /*
  260. ** メインプログラム
  261. */
  262. int main(int argc, char *argv[])
  263. {
  264.   glutInit(&argc, argv);
  265.   glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
  266.   glutCreateWindow(argv[0]);
  267.   glutDisplayFunc(display);
  268.   glutReshapeFunc(resize);
  269.   glutMouseFunc(mouse);
  270.   glutMotionFunc(motion);
  271.   glutKeyboardFunc(keyboard);
  272.   init();
  273.    calculateFPS();
  274.   drawFPS();
  275.     fps=glutGet(GLUT_ELAPSED_TIME);
  276.   glutMainLoop();
  277.   return 0;
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement