d10070dd

☆old-fps-test

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