d10070dd

☆legacy-time

Feb 2nd, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.17 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. #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. float start,end;
  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.     end=clock();
  161.    
  162.     printf("start  %f /sec \n " ,start/1000);
  163.     printf("end  %f /sec \n " ,end/1000);
  164.     printf("star to end  %f /sec \n " ,(end-start)/1000);
  165.     glEnd();
  166. }
  167. //------- 各種コールバック----------//
  168. void display(void)
  169. {
  170.     static int angle = 0;
  171.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  172.     glLoadIdentity();
  173.     //gluLookAt(400.0, 750.0, 20.0, 230.0, 550.0, 15.0, 0.0, 1.0, 0.0);
  174.     //gluLookAt(-40000.0, -90000.0, 45.0, -35000.0, -81750.0, 30.0, 0.0, 1.0, 0.0);
  175.     //gluLookAt(-30000.0, -80000.0, 45.0, -35000.0, -81750.0, 30.0, 0.0, 0.0, 1.0);
  176.     gluLookAt(-39000.0, -86000.0, 45.0, -34000.0, -82500.0, 30.0, 0.0, 0.0, 1.0);
  177.     //gluLookAt(30.0, 40.0, 50.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  178.     //gluLookAt(0.30, 0.40, 0.50, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10);
  179.     glPushMatrix();
  180.     calculateFPS();
  181.     //printf ("angle_x1_fps: %4.2f\n", fps);
  182.     //glRotatef(angle,-35000,-81750.0,30.0);//x軸回転
  183.     glRotatef(angle,-34000,-82500.0,30.0);//x軸回転
  184.     //glRotatef(angle,1.0,0.0,0.0);//x軸回転
  185.     //glRotatef(angle+90,1.0,0.0,0.0);//x軸回転
  186.     //glRotatef(angle+180,1.0,0.0,0.0);//x軸回転
  187.     calculateFPS();
  188.     //printf ("angle_x2_fps: %4.2f\n", fps);
  189.     //glRotatef(angle+270,1.0,0.0,0.0);//x軸回転
  190.     //glRotatef(angle+360,1.0,0.0,0.0);//x軸回転
  191.     calculateFPS();
  192.     //printf ("angle_x3_fps: %4.2f\n", fps);
  193.     calculateFPS();
  194.     //printf ("angle_y1_fps: %4.2f\n", fps);
  195.     //glRotatef(angle,0.0,1.0,0.0);
  196.     //glRotatef(angle+90,0.0,1.0,0.0);
  197.     //glRotatef(angle+180,0.0,1.0,0.0);
  198.     calculateFPS();
  199.     //printf ("angle_y2_fps: %4.2f\n", fps);
  200.     //glRotatef(angle+270,0.0,1.0,0.0);
  201.     //glRotatef(angle+360,0.0,1.0,0.0);
  202.     calculateFPS();
  203.     //printf ("angle_y3_fps: %4.2f\n", fps);
  204.     calculateFPS();
  205.     //printf ("angle_z1_fps: %4.2f\n", fps);
  206.     //glRotatef(angle,0.0,0.0,1.0);
  207.     //glRotatef(angle+90,0.0,0.0,1.0);
  208.     //glRotatef(angle+180,0.0,0.0,1.0);
  209.     calculateFPS();
  210.     //printf ("angle_z2_fps: %4.2f\n", fps);
  211.     //glRotatef(angle+270,0.0,0.0,1.0);
  212.     //glRotatef(angle+360,0.0,0.0,1.0);
  213.     calculateFPS();
  214.     //printf ("angle_z3_fps: %4.2f\n", fps);
  215.     calculateFPS();
  216.     //printf ("txyz1_fps: %4.2f\n", fps);
  217.     //glTranslatef(tx,ty,tz);
  218.     calculateFPS();
  219.     //printf ("txyz2_fps: %4.2f\n", fps);
  220.     //glTranslatef(-1.0,-1.0,-1.0);
  221.     calculateFPS();
  222.     //printf ("txyz3_fps: %4.2f\n", fps);
  223.     calculateFPS();
  224.     //printf ("sxyz1_fps: %4.2f\n", fps);
  225.     //glScalef(sx, sy, sz);
  226.     calculateFPS();
  227.     //printf ("sxyz2_fps: %4.2f\n", fps);
  228.     //glScalef(1.0, 1.0, 1.0);
  229.     calculateFPS();
  230.     //printf ("sxyz3_fps: %4.2f\n", fps);
  231.     drawTriangle();
  232.     glPopMatrix();
  233.  
  234.      calculateFPS();
  235.      //drawFPS();
  236.  
  237.     glutSwapBuffers();
  238.     if(++angle >= 360) angle = 0;
  239. }
  240.  
  241. void reshape(int w, int h)
  242. {
  243.     glViewport(0,0,w,h);
  244.     glMatrixMode(GL_PROJECTION);
  245.     glLoadIdentity();
  246.     //gluPerspective(30.0, double(w)/h, 10.0, 100000.0);
  247.     gluPerspective(45.0, double(w)/h, 10.0, 100000.0);
  248.     //gluPerspective(15.0, double(w)/h, 10.0, 100000.0);
  249.     //gluPerspective(10.0, double(w)/h, 10.0, 1000.0);
  250.     //gluPerspective(90.0, double(w)/h, 100.0, 2000.0);
  251.     glMatrixMode(GL_MODELVIEW);
  252.     glLoadIdentity();
  253. }
  254.  
  255. /*void idle(void)
  256. {
  257.     glutPostRedisplay();
  258. }
  259. */
  260. //------ その他初期設定-------//
  261. void otherInit(void)
  262. {
  263.     glClearColor(1.f, 1.f, 1.f, 1.f);
  264.     glEnable(GL_DEPTH_TEST);
  265.     glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  266.     glEnable(GL_COLOR_MATERIAL);
  267.     glEnable(GL_LIGHT0);
  268.     glEnable(GL_LIGHTING);
  269.     glEnable(GL_NORMALIZE);//法線の正規化
  270. }
  271.  
  272. //---- VBOの作成----//
  273. /*
  274. void buildVBO(void)
  275. {
  276.     glGenBuffers(3,&VboId[0]);//座標、法線、色の3つ
  277.    
  278.     //頂点
  279.     glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
  280.     glBufferData(GL_ARRAY_BUFFER,sizeof(vertexAry),
  281.                 vertexAry,GL_DYNAMIC_DRAW);//データ変更する
  282.  
  283.     //法線
  284.     glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
  285.     glBufferData(GL_ARRAY_BUFFER,sizeof(normalAry),
  286.                 normalAry,GL_DYNAMIC_DRAW);//データ変更あり
  287.  
  288.     //色
  289.     glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
  290.     glBufferData(GL_ARRAY_BUFFER,sizeof(colorAry),
  291.                     colorAry,GL_STREAM_DRAW);//データ変更なし
  292.  
  293.     //インデックス
  294.     glGenBuffers(1,&VboIndex);
  295.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,VboIndex);
  296.     glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indexAry),
  297.                     indexAry,GL_STATIC_DRAW);//データ変更なし
  298. }
  299. */
  300.  
  301. static void
  302. idle()
  303. {
  304.     angle=0;
  305.     //tx=fmodf(tx+0.01,10);
  306.     //ty=fmodf(ty+0.01,10);
  307.     //tz=fmodf(tz+0.01,10);
  308.     //sx=fmodf(sx+0.01,10);
  309.     //sy=fmodf(sy+0.01,10);
  310.     //sz=fmodf(sz+0.01,10);
  311.     tx_p=(tx_p+0.5)*M_PI;
  312.     tx=(float)sin(tx_p);
  313.     //printf("%f\n",tx);
  314.     ty_p=(ty_p+0.5)*M_PI;
  315.     ty=(float)sin(ty_p);
  316.     //printf("%f\n",ty);
  317.     tz_p=(tz_p+0.5)*M_PI;
  318.     tz=(float)sin(tz_p);
  319.     //printf("%f\n",tz);
  320.     sx=2.0;
  321.     sy=2.0;
  322.     sz=2.0;
  323.     glutPostRedisplay();
  324. }
  325.  
  326. //-------------------------------------------------------------------------
  327. // Calculates the frames per second
  328. //-------------------------------------------------------------------------
  329.  
  330. void calculateFPS()
  331. {
  332.         //  Increase frame count
  333.         frameCount++;
  334.        
  335.         //  Get the number of milliseconds since glutInit called
  336.         //  (or first call to glutGet(GLUT ELAPSED TIME)).
  337.         currentTime = glutGet(GLUT_ELAPSED_TIME);
  338.        
  339.         //  Calculate time passed
  340.         int timeInterval = currentTime - previousTime;
  341.        
  342.         if(timeInterval > 1000)
  343.         {
  344.                 //  calculate the number of frames per second
  345.  
  346.             fps = frameCount / (timeInterval / 1000.0f);
  347.                 //  Set time
  348.                 previousTime = currentTime;
  349.                
  350.                 //  Reset frame count
  351.                 frameCount = 0;
  352.         }
  353.    
  354.         //printf ("FPS: %4.2f\n", fps);
  355.    
  356. }
  357.  
  358.  
  359. //-------------------------------------------------------------------------
  360. //  Draw FPS
  361. //-------------------------------------------------------------------------
  362. /*
  363. void drawFPS()
  364. {
  365.         //  Load the identity matrix so that FPS string being drawn
  366.         //  won't get animates
  367.         glLoadIdentity ();
  368.  
  369.         fps=glutGet(GLUT_ELAPSED_TIME);
  370.  
  371.         //  Print the FPS to the window
  372.         printf ("FPS: %4.2f\n", fps);
  373. }
  374.  */
  375.  
  376. //----------- メイン関数------------//
  377. int main(int argc, char *argv[])
  378. {
  379.         FILE *fpi;
  380.         //int face,vertex,point;
  381.         int i,j,k;
  382.         float data;
  383.         start =clock();
  384.         if(argc!=5)
  385.         {
  386.                 fprintf(stderr,"Usage: %s 5m_mesh_data.lem,5m_mesh_data.raw\n",argv[0]);
  387.                 exit(1);
  388.         }
  389.         printf("OPEN FILE NAME:%s\n",argv[1]);
  390.  
  391.         if((fpi=fopen(argv[1],"rb"))==NULL)
  392.         {
  393.                 fprintf(stderr,"Can not open\n");
  394.                 exit(1);
  395.         }
  396.  
  397.         face=atoi(argv[2]);
  398.         vertex=atoi(argv[3]);
  399.         point=atoi(argv[4]);
  400.         a=face*vertex;
  401.         vertexAry=(GLfloat *)malloc(face*vertex*point*sizeof(GLfloat));
  402.         fread((GLfloat *)vertexAry,sizeof(GLfloat),face*vertex*point,fpi);
  403.        
  404.     //GLUTの初期設定
  405.     glutInit(&argc, argv);
  406.     glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  407.     glutInitWindowSize(640,480);
  408.     glutCreateWindow("Triangle Sample");
  409.     //GLEWの初期設定
  410.     GLenum err = glewInit();
  411.     if(err != GLEW_OK)
  412.     {
  413.         fprintf(stderr,"Err:%s\n",
  414.             glewGetErrorString(err));
  415.         return -1;
  416.     }
  417.  
  418.     //拡張チェック
  419.     if(!glewIsExtensionSupported(
  420.                     "GL_ARB_vertex_buffer_object")){
  421.         puts("you Can't use VBO");
  422.         return -1;
  423.     }
  424.  
  425.     //コールバック
  426.     glutDisplayFunc(display);
  427.  
  428.     glutReshapeFunc(reshape);
  429.     glutIdleFunc(idle);
  430.  
  431.     otherInit();
  432.     //buildVBO(face,vertex,point);//VBOの作成
  433.  
  434.     calculateFPS();
  435.     //drawFPS();
  436.     //fps=glutGet(GLUT_ELAPSED_TIME);
  437.  
  438.     glutMainLoop();
  439.  
  440.     return 0;
  441. }
Advertisement
Add Comment
Please, Sign In to add comment