Advertisement
d10070dd

☆sample25-fps-test-固定

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