d10070dd

☆modern-time

Feb 2nd, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.46 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. #include <time.h>
  7.  
  8. #define M_PI 3.141592
  9.  
  10. GLfloat *vertexAry;
  11.  
  12. static
  13. int angle=0;
  14. float tx=0.0;
  15. float ty=0.0;
  16. float tz=0.0;
  17. float sx=1.0;
  18. float sy=1.0;
  19. float sz=1.0;
  20. double tx_p=0;
  21. double ty_p=0;
  22. double tz_p=0;
  23. double sx_p=0;
  24. double sy_p=0;
  25. double sz_p=0;
  26. int face,vertex,point;
  27. //static unsigned int fps_start=0;
  28. //static unsigned int fps_frames=0;
  29. //  The number of frames
  30. int frameCount = 0;
  31. float fps = 0;
  32. float start,end;
  33. //  currentTime - previousTime is the time elapsed
  34. //  between every call of theIdle function
  35. int currentTime = 0, previousTime = 0;
  36.  
  37. //  printf prints to file. printw prints to window
  38. //void printw (float x, float y, float z, char* format, ...);
  39.  
  40. void calculateFPS();
  41.  
  42. //void drawFPS();
  43.  
  44. //------- 頂点データ-----------//
  45. //「6」面、「4」頂点、1頂点はx,y,zの「3」要素
  46. /*
  47. GLfloat vertexAry[3][3] =
  48. {
  49.     {0.0f,0.6f,0.0f},{-0.6f,0.0f,0.0f},
  50.     {0.6f,0.0f,0.0f}
  51. };
  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. // π/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(-39000.0, -86000.0, 45.0, -34000.0, -82500.0, 30.0, 0.0, 0.0, 1.0);
  152.     glPushMatrix();
  153.     calculateFPS();
  154.     //printf ("angle_x1_fps: %4.2f\n", fps);
  155.     glRotatef(angle,-34000,-82500.0,30.0);//x軸回転
  156.     //glRotatef(angle+90,1.0,0.0,0.0);//x軸回転
  157.     //glRotatef(angle+180,1.0,0.0,0.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(angle,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(-1.0,-1.0,-1.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.1, 1.1, 1.1);
  200.     calculateFPS();
  201.     //printf ("sxyz3_fps: %4.2f\n", fps);
  202.     drawAry();
  203.     end=clock();
  204.    
  205.     printf("start  %f /sec \n " ,start/1000);
  206.     printf("end  %f /sec \n " ,end/1000);
  207.     printf("star to end  %f /sec \n " ,(end-start)/1000);
  208.     glPopMatrix();
  209.  
  210.      calculateFPS();
  211.      //drawFPS();
  212.  
  213.     glutSwapBuffers();
  214.     if(++angle >= 360) angle = 0;
  215. }
  216.  
  217. void reshape(int w, int h)
  218. {
  219.     glViewport(0,0,w,h);
  220.     glMatrixMode(GL_PROJECTION);
  221.     glLoadIdentity();
  222.     gluPerspective(45.0, double(w)/h, 10.0, 100000.0);
  223.     //gluPerspective(60.0, double(w)/h, 0.1, 200.0);
  224.     glMatrixMode(GL_MODELVIEW);
  225.     glLoadIdentity();
  226. }
  227.  
  228. /*void idle(void)
  229. {
  230.     glutPostRedisplay();
  231. }
  232. */
  233. //------ その他初期設定-------//
  234. void otherInit(void)
  235. {
  236.     glClearColor(1.f, 1.f, 1.f, 1.f);
  237.     glEnable(GL_DEPTH_TEST);
  238.     glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  239.     glEnable(GL_COLOR_MATERIAL);
  240.     glEnable(GL_LIGHT0);
  241.     glEnable(GL_LIGHTING);
  242.     glEnable(GL_NORMALIZE);//法線の正規化
  243. }
  244.  
  245. //---- VBOの作成----//
  246. void buildVBO(int a, int b, int c)
  247. {
  248.     glGenBuffers(3,&VboId[0]);//座標、法線、色の3つ
  249.    
  250.     //頂点
  251.     glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
  252.     glBufferData(GL_ARRAY_BUFFER,sizeof(vertexAry)*a*b*c,
  253.                 vertexAry,GL_STATIC_DRAW);//データ変更する
  254.  
  255.     //法線
  256.     //glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
  257.     //glBufferData(GL_ARRAY_BUFFER,sizeof(normalAry),
  258.     //          normalAry,GL_DYNAMIC_DRAW);//データ変更あり
  259.  
  260.     //色
  261.     //glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
  262.     //glBufferData(GL_ARRAY_BUFFER,sizeof(colorAry),
  263.     //              colorAry,GL_STREAM_DRAW);//データ変更なし
  264.  
  265.     //インデックス
  266.     //glGenBuffers(1,&VboIndex);
  267.     //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,VboIndex);
  268.     //glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indexAry),
  269.     //              indexAry,GL_STATIC_DRAW);//データ変更なし
  270. }
  271.  
  272. static void
  273. idle()
  274. {
  275.     angle=0;
  276.     //tx=fmodf(tx+0.01,10);
  277.     //ty=fmodf(ty+0.01,10);
  278.     //tz=fmodf(tz+0.01,10);
  279.     //sx=fmodf(sx+0.01,10);
  280.     //sy=fmodf(sy+0.01,10);
  281.     //sz=fmodf(sz+0.01,10);
  282.     tx_p=(tx_p+0.5)*M_PI;
  283.     tx=(float)sin(tx_p);
  284.     //printf("%f\n",tx);
  285.     ty_p=(ty_p+0.5)*M_PI;
  286.     ty=(float)sin(ty_p);
  287.     //printf("%f\n",ty);
  288.     tz_p=(tz_p+0.5)*M_PI;
  289.     tz=(float)sin(tz_p);
  290.     //printf("%f\n",tz);
  291.     sx=2.0;
  292.     sy=2.0;
  293.     sz=2.0;
  294.     glutPostRedisplay();
  295. }
  296.  
  297. //-------------------------------------------------------------------------
  298. // Calculates the frames per second
  299. //-------------------------------------------------------------------------
  300.  
  301. void calculateFPS()
  302. {
  303.         //  Increase frame count
  304.         frameCount++;
  305.        
  306.         //  Get the number of milliseconds since glutInit called
  307.         //  (or first call to glutGet(GLUT ELAPSED TIME)).
  308.         currentTime = glutGet(GLUT_ELAPSED_TIME);
  309.        
  310.         //  Calculate time passed
  311.         int timeInterval = currentTime - previousTime;
  312.        
  313.         if(timeInterval > 1000)
  314.         {
  315.                 //  calculate the number of frames per second
  316.  
  317.             fps = frameCount / (timeInterval / 1000.0f);
  318.                 //  Set time
  319.                 previousTime = currentTime;
  320.                
  321.                 //  Reset frame count
  322.                 frameCount = 0;
  323.         }
  324.    
  325.         //printf ("FPS: %4.2f\n", fps);
  326.    
  327. }
  328.  
  329.  
  330. //-------------------------------------------------------------------------
  331. //  Draw FPS
  332. //-------------------------------------------------------------------------
  333. /*
  334. void drawFPS()
  335. {
  336.         //  Load the identity matrix so that FPS string being drawn
  337.         //  won't get animates
  338.         glLoadIdentity ();
  339.  
  340.         fps=glutGet(GLUT_ELAPSED_TIME);
  341.  
  342.         //  Print the FPS to the window
  343.         printf ("FPS: %4.2f\n", fps);
  344. }
  345.  */
  346.  
  347. //----------- メイン関数------------//
  348. int main(int argc, char *argv[])
  349. {
  350.     FILE *fpi;
  351.         //int face,vertex,point;
  352.         int i,j,k;
  353.         float data;
  354.         start =clock();
  355.         if(argc!=5)
  356.         {
  357.                 fprintf(stderr,"Usage: %s 5m_mesh_data.lem,5m_mesh_data.raw\n",argv[0]);
  358.                 exit(1);
  359.         }
  360.         printf("OPEN FILE NAME:%s\n",argv[1]);
  361.  
  362.         if((fpi=fopen(argv[1],"rb"))==NULL)
  363.         {
  364.                 fprintf(stderr,"Can not open\n");
  365.                 exit(1);
  366.         }
  367.  
  368.         face=atoi(argv[2]);
  369.         vertex=atoi(argv[3]);
  370.         point=atoi(argv[4]);
  371.         //printf("check1\n");
  372.         vertexAry=(GLfloat *)malloc(face*vertex*point*sizeof(GLfloat));
  373.         //printf("check2\n");
  374.         fread((GLfloat *)vertexAry,sizeof(GLfloat),face*vertex*point,fpi);
  375.         //printf("check3\n");
  376.     //GLUTの初期設定
  377.     glutInit(&argc, argv);
  378.     glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  379.     glutInitWindowSize(640,480);
  380.     glutCreateWindow("VBO Sample");
  381.     //GLEWの初期設定
  382.     GLenum err = glewInit();
  383.     if(err != GLEW_OK)
  384.     {
  385.         fprintf(stderr,"Err:%s\n",
  386.             glewGetErrorString(err));
  387.         return -1;
  388.     }
  389.  
  390.     //拡張チェック
  391.     if(!glewIsExtensionSupported(
  392.                     "GL_ARB_vertex_buffer_object")){
  393.         puts("you Can't use VBO");
  394.         return -1;
  395.     }
  396.  
  397.     //コールバック
  398.     glutDisplayFunc(display);
  399.  
  400.     glutReshapeFunc(reshape);
  401.     glutIdleFunc(idle);
  402.  
  403.     otherInit();
  404.     buildVBO(face,vertex,point);//VBOの作成
  405.  
  406.     calculateFPS();
  407.     //drawFPS();
  408.     //fps=glutGet(GLUT_ELAPSED_TIME);
  409.  
  410.     glutMainLoop();
  411.  
  412.     return 0;
  413. }
Advertisement
Add Comment
Please, Sign In to add comment