d10070dd

sample25-triangle-fps

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