Advertisement
d10070dd

sample25-triangle-binary

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