Advertisement
d10070dd

old-triangle-fps

Dec 26th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.30 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>//glut.hより先にインクルード
  4. #include <gl/glew.h>//glut.hより先にインクルード
  5. #include <gl/glut.h>
  6.  
  7. #define M_PI 3.141592
  8.  
  9. static
  10. int angle=0;
  11. float tx=0.0;
  12. float ty=0.0;
  13. float tz=0.0;
  14. float sx=1.0;
  15. float sy=1.0;
  16. float sz=1.0;
  17. double tx_p=0;
  18. double ty_p=0;
  19. double tz_p=0;
  20. double sx_p=0;
  21. double sy_p=0;
  22. double sz_p=0;
  23.  
  24. //static unsigned int fps_start=0;
  25. //static unsigned int fps_frames=0;
  26. //  The number of frames
  27. int frameCount = 0;
  28. float fps = 0;
  29.  
  30. //  currentTime - previousTime is the time elapsed
  31. //  between every call of theIdle function
  32. int currentTime = 0, previousTime = 0;
  33.  
  34. //  printf prints to file. printw prints to window
  35. //void printw (float x, float y, float z, char* format, ...);
  36.  
  37. void calculateFPS();
  38.  
  39. //void drawFPS();
  40.  
  41. //------- 頂点データ-----------//
  42. //「6」面、「4」頂点、1頂点はx,y,zの「3」要素
  43. /*
  44. GLfloat vertexAry[3][3] =
  45. {
  46.     {0.0f,0.6f,0.0f},{-0.6f,0.0f,0.0f},
  47.     {0.6f,0.0f,0.0f}
  48. };
  49.  
  50. //法線データ
  51. GLfloat normalAry[3][3] =
  52. {
  53.     {0.0f,0.6f,0.0f},{-0.6f,0.0f,0.0f},
  54.     {0.6f,0.0f,0.0f}
  55. };
  56.  
  57. //色データ
  58. GLfloat colorAry[3][3] =
  59. {
  60.     {0.0f,0.6f,0.0f},{-0.6f,0.0f,0.0f},
  61.     {0.6f,0.0f,0.0f}
  62. };
  63. //インデックス
  64. GLint indexAry[]=
  65. {
  66.     0,1,2
  67. };
  68. */
  69.  
  70. // π/180の値
  71. const float PI_OVER_180 = 0.0174532925f;
  72.  
  73. //VBO用ID
  74. /*
  75. GLuint VboId[3];//3つ分
  76. GLuint VboIndex;//インデックス
  77. */
  78.  
  79. //描画関数
  80. /*
  81. void drawAry(void)
  82. {
  83.     GLfloat *clientPtr;//クライアント側用
  84.     GLfloat tmp[3];
  85.     int idloop;
  86.     int loop;
  87.     static float angle = 2*PI_OVER_180;
  88.  
  89.     //データの場所を知らせる
  90.     //座標
  91.     glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
  92.     glVertexPointer(3, GL_FLOAT, 0, 0);
  93.     //法線
  94.     glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
  95.     glNormalPointer(GL_FLOAT, 0, 0);
  96.     //色
  97.     glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
  98.     glColorPointer(3,GL_FLOAT, 0, 0);
  99.     //インデックスもバインド
  100.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VboIndex);
  101.  
  102.     glEnableClientState(GL_VERTEX_ARRAY);
  103.     glEnableClientState(GL_NORMAL_ARRAY);
  104.     glEnableClientState(GL_COLOR_ARRAY);
  105.     //描画
  106.     glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0);
  107.    
  108.     glDisableClientState(GL_COLOR_ARRAY);
  109.     glDisableClientState(GL_NORMAL_ARRAY);
  110.     glDisableClientState(GL_VERTEX_ARRAY);
  111.  
  112.     //座標と法線を回転させる
  113.     //for(idloop = 0; idloop < 2;++idloop)
  114.     {
  115.         //idloop番目のバッファオブジェクトに注目
  116.         //glBindBuffer(GL_ARRAY_BUFFER,VboId[idloop]);
  117.        
  118.         //対応付け
  119.         clientPtr = (GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,GL_READ_WRITE);
  120.  
  121.         if(clientPtr != NULL)
  122.         {
  123.             //24頂点*3座標
  124.              for(loop = 0; loop < 3*3;loop += 3)    {
  125.                 //読み出し(READ)
  126.                 tmp[0] = clientPtr[loop];
  127.                 tmp[1] = clientPtr[loop+1];
  128.                 tmp[2] = clientPtr[loop+2];
  129.                 //書き込み(WRITE)
  130.                 clientPtr[loop] = cos(angle)*tmp[0]
  131.                                     + sin(angle)*tmp[2];
  132.                 clientPtr[loop+1] = tmp[1];
  133.                 clientPtr[loop+2] = -sin(angle)*tmp[0]
  134.                                         + cos(angle)*tmp[2];
  135.              }
  136.             glUnmapBuffer(GL_ARRAY_BUFFER);//対応付けの解放
  137.         }
  138.     }
  139.     //クライアント側に戻す
  140.     glBindBuffer(GL_ARRAY_BUFFER,0);
  141.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  142. }
  143. */
  144.  
  145. void drawTriangle(void)
  146. {
  147.     glBegin(GL_TRIANGLES);
  148.     glColor3f(0.0f,1.0f,0.0f);
  149.     glVertex3f(0.0f,0.6f,0.0f);
  150.     glVertex3f(-0.6f,0.0f,0.0f);
  151.     glVertex3f(0.6f,-0.0f,0.0f);
  152.     glEnd();
  153. }
  154.  
  155. //------- 各種コールバック----------//
  156. void display(void)
  157. {
  158.     static int angle = 0;
  159.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  160.     glLoadIdentity();
  161.     gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  162.    
  163.     glPushMatrix();
  164.     calculateFPS();
  165.     printf ("angle_x1_fps: %4.2f\n", fps);
  166.     glRotatef(angle,1.0,0.0,0.0);//x軸回転
  167.     glRotatef(angle+90,1.0,0.0,0.0);//x軸回転
  168.     glRotatef(angle+180,1.0,0.0,0.0);//x軸回転
  169.     calculateFPS();
  170.     printf ("angle_x2_fps: %4.2f\n", fps);
  171.     glRotatef(angle+270,1.0,0.0,0.0);//x軸回転
  172.     glRotatef(angle+360,1.0,0.0,0.0);//x軸回転
  173.     calculateFPS();
  174.     printf ("angle_x3_fps: %4.2f\n", fps);
  175.     calculateFPS();
  176.     printf ("angle_y1_fps: %4.2f\n", fps);
  177.     glRotatef(angle,0.0,1.0,0.0);
  178.     glRotatef(angle+90,0.0,1.0,0.0);
  179.     glRotatef(angle+180,0.0,1.0,0.0);
  180.     calculateFPS();
  181.     printf ("angle_y2_fps: %4.2f\n", fps);
  182.     glRotatef(angle+270,0.0,1.0,0.0);
  183.     glRotatef(angle+360,0.0,1.0,0.0);
  184.     calculateFPS();
  185.     printf ("angle_y3_fps: %4.2f\n", fps);
  186.     calculateFPS();
  187.     printf ("angle_z1_fps: %4.2f\n", fps);
  188.     glRotatef(angle,0.0,0.0,1.0);
  189.     glRotatef(angle+90,0.0,0.0,1.0);
  190.     glRotatef(angle+180,0.0,0.0,1.0);
  191.     calculateFPS();
  192.     printf ("angle_z2_fps: %4.2f\n", fps);
  193.     glRotatef(angle+270,0.0,0.0,1.0);
  194.     glRotatef(angle+360,0.0,0.0,1.0);
  195.     calculateFPS();
  196.     printf ("angle_z3_fps: %4.2f\n", fps);
  197.     calculateFPS();
  198.     printf ("txyz1_fps: %4.2f\n", fps);
  199.     glTranslatef(tx,ty,tz);
  200.     calculateFPS();
  201.     printf ("txyz2_fps: %4.2f\n", fps);
  202.     glTranslatef(-1.0,-1.0,-1.0);
  203.     calculateFPS();
  204.     printf ("txyz3_fps: %4.2f\n", fps);
  205.     calculateFPS();
  206.     printf ("sxyz1_fps: %4.2f\n", fps);
  207.     glScalef(sx, sy, sz);
  208.     calculateFPS();
  209.     printf ("sxyz2_fps: %4.2f\n", fps);
  210.     glScalef(1.0, 1.0, 1.0);
  211.     calculateFPS();
  212.     printf ("sxyz3_fps: %4.2f\n", fps);
  213.     drawTriangle();
  214.     glPopMatrix();
  215.  
  216.      calculateFPS();
  217.      //drawFPS();
  218.  
  219.     glutSwapBuffers();
  220.     if(++angle >= 360) angle = 0;
  221. }
  222.  
  223. void reshape(int w, int h)
  224. {
  225.     glViewport(0,0,w,h);
  226.     glMatrixMode(GL_PROJECTION);
  227.     glLoadIdentity();
  228.     gluPerspective(30.0, double(w)/h, 0.1, 200.0);
  229.     glMatrixMode(GL_MODELVIEW);
  230.     glLoadIdentity();
  231. }
  232.  
  233. /*void idle(void)
  234. {
  235.     glutPostRedisplay();
  236. }
  237. */
  238. //------ その他初期設定-------//
  239. void otherInit(void)
  240. {
  241.     glClearColor(1.f, 1.f, 1.f, 1.f);
  242.     glEnable(GL_DEPTH_TEST);
  243.     glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  244.     glEnable(GL_COLOR_MATERIAL);
  245.     glEnable(GL_LIGHT0);
  246.     glEnable(GL_LIGHTING);
  247.     glEnable(GL_NORMALIZE);//法線の正規化
  248. }
  249.  
  250. //---- VBOの作成----//
  251. /*
  252. void buildVBO(void)
  253. {
  254.     glGenBuffers(3,&VboId[0]);//座標、法線、色の3つ
  255.    
  256.     //頂点
  257.     glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
  258.     glBufferData(GL_ARRAY_BUFFER,sizeof(vertexAry),
  259.                 vertexAry,GL_DYNAMIC_DRAW);//データ変更する
  260.  
  261.     //法線
  262.     glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
  263.     glBufferData(GL_ARRAY_BUFFER,sizeof(normalAry),
  264.                 normalAry,GL_DYNAMIC_DRAW);//データ変更あり
  265.  
  266.     //色
  267.     glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
  268.     glBufferData(GL_ARRAY_BUFFER,sizeof(colorAry),
  269.                     colorAry,GL_STREAM_DRAW);//データ変更なし
  270.  
  271.     //インデックス
  272.     glGenBuffers(1,&VboIndex);
  273.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,VboIndex);
  274.     glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indexAry),
  275.                     indexAry,GL_STATIC_DRAW);//データ変更なし
  276. }
  277. */
  278.  
  279. static void
  280. idle()
  281. {
  282.     angle=0;
  283.     //tx=fmodf(tx+0.01,10);
  284.     //ty=fmodf(ty+0.01,10);
  285.     //tz=fmodf(tz+0.01,10);
  286.     //sx=fmodf(sx+0.01,10);
  287.     //sy=fmodf(sy+0.01,10);
  288.     //sz=fmodf(sz+0.01,10);
  289.     tx_p=(tx_p+0.5)*M_PI;
  290.     tx=(float)sin(tx_p);
  291.     //printf("%f\n",tx);
  292.     ty_p=(ty_p+0.5)*M_PI;
  293.     ty=(float)sin(ty_p);
  294.     //printf("%f\n",ty);
  295.     tz_p=(tz_p+0.5)*M_PI;
  296.     tz=(float)sin(tz_p);
  297.     //printf("%f\n",tz);
  298.     sx=2.0;
  299.     sy=2.0;
  300.     sz=2.0;
  301.     glutPostRedisplay();
  302. }
  303.  
  304. //-------------------------------------------------------------------------
  305. // Calculates the frames per second
  306. //-------------------------------------------------------------------------
  307.  
  308. void calculateFPS()
  309. {
  310.         //  Increase frame count
  311.         frameCount++;
  312.        
  313.         //  Get the number of milliseconds since glutInit called
  314.         //  (or first call to glutGet(GLUT ELAPSED TIME)).
  315.         currentTime = glutGet(GLUT_ELAPSED_TIME);
  316.        
  317.         //  Calculate time passed
  318.         int timeInterval = currentTime - previousTime;
  319.        
  320.         if(timeInterval > 1000)
  321.         {
  322.                 //  calculate the number of frames per second
  323.  
  324.             fps = frameCount / (timeInterval / 1000.0f);
  325.                 //  Set time
  326.                 previousTime = currentTime;
  327.                
  328.                 //  Reset frame count
  329.                 frameCount = 0;
  330.         }
  331.    
  332.         //printf ("FPS: %4.2f\n", fps);
  333.    
  334. }
  335.  
  336.  
  337. //-------------------------------------------------------------------------
  338. //  Draw FPS
  339. //-------------------------------------------------------------------------
  340. /*
  341. void drawFPS()
  342. {
  343.         //  Load the identity matrix so that FPS string being drawn
  344.         //  won't get animates
  345.         glLoadIdentity ();
  346.  
  347.         fps=glutGet(GLUT_ELAPSED_TIME);
  348.  
  349.         //  Print the FPS to the window
  350.         printf ("FPS: %4.2f\n", fps);
  351. }
  352.  */
  353.  
  354. //----------- メイン関数------------//
  355. int main(int argc, char *argv[])
  356. {
  357.     //GLUTの初期設定
  358.     glutInit(&argc, argv);
  359.     glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  360.     glutInitWindowSize(640,480);
  361.     glutCreateWindow("Triangle Sample");
  362.     //GLEWの初期設定
  363.     GLenum err = glewInit();
  364.     if(err != GLEW_OK)
  365.     {
  366.         fprintf(stderr,"Err:%s\n",
  367.             glewGetErrorString(err));
  368.         return -1;
  369.     }
  370.  
  371.     //拡張チェック
  372.     if(!glewIsExtensionSupported(
  373.                     "GL_ARB_vertex_buffer_object")){
  374.         puts("you Can't use VBO");
  375.         return -1;
  376.     }
  377.  
  378.     //コールバック
  379.     glutDisplayFunc(display);
  380.  
  381.     glutReshapeFunc(reshape);
  382.     glutIdleFunc(idle);
  383.  
  384.     otherInit();
  385.     //buildVBO();//VBOの作成
  386.  
  387.     calculateFPS();
  388.     //drawFPS();
  389.     //fps=glutGet(GLUT_ELAPSED_TIME);
  390.  
  391.     glutMainLoop();
  392.  
  393.     return 0;
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement