Advertisement
d10070dd

☆sample25-fps-test-拡大

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