Advertisement
d10070dd

sample25-fps

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