Advertisement
d10070dd

sample25-triangle-binary-edit

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