d10070dd

sample25-07MD711-714-add

Jan 25th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.21 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. int face,vertex,point;
  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,face*vertex);
  107.     //glDrawArrays(GL_TRIANGLES,0,2055);
  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[0]);
  117.        
  118.         //対応付け
  119.         //clientPtr = (GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,GL_READ_ONLY);
  120.    
  121.         /*if(clientPtr != NULL)
  122.         {
  123.             //24頂点*3座標
  124.              for(loop = 0; loop < 685*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.         }*/
  137.     //}
  138.     glUnmapBuffer(GL_ARRAY_BUFFER);//対応付けの解放
  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(-39000.0, -86000.0, 45.0, -34000.0, -82500.0, 30.0, 0.0, 0.0, 1.0);
  151.     glPushMatrix();
  152.     calculateFPS();
  153.     printf ("angle_x1_fps: %4.2f\n", fps);
  154.     glRotatef(angle,-34000,-82500.0,30.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.1, 1.1, 1.1);
  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(45.0, double(w)/h, 10.0, 100000.0);
  217.     //gluPerspective(60.0, double(w)/h, 0.1, 200.0);
  218.     glMatrixMode(GL_MODELVIEW);
  219.     glLoadIdentity();
  220. }
  221.  
  222. /*void idle(void)
  223. {
  224.     glutPostRedisplay();
  225. }
  226. */
  227. //------ その他初期設定-------//
  228. void otherInit(void)
  229. {
  230.     glClearColor(1.f, 1.f, 1.f, 1.f);
  231.     glEnable(GL_DEPTH_TEST);
  232.     glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  233.     glEnable(GL_COLOR_MATERIAL);
  234.     glEnable(GL_LIGHT0);
  235.     glEnable(GL_LIGHTING);
  236.     glEnable(GL_NORMALIZE);//法線の正規化
  237. }
  238.  
  239. //---- VBOの作成----//
  240. void buildVBO(int a, int b, int c)
  241. {
  242.     glGenBuffers(3,&VboId[0]);//座標、法線、色の3つ
  243.    
  244.     //頂点
  245.     glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
  246.     glBufferData(GL_ARRAY_BUFFER,sizeof(vertexAry)*a*b*c,
  247.                 vertexAry,GL_STATIC_DRAW);//データ変更する
  248.  
  249.     //法線
  250.     //glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
  251.     //glBufferData(GL_ARRAY_BUFFER,sizeof(normalAry),
  252.     //          normalAry,GL_DYNAMIC_DRAW);//データ変更あり
  253.  
  254.     //色
  255.     //glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
  256.     //glBufferData(GL_ARRAY_BUFFER,sizeof(colorAry),
  257.     //              colorAry,GL_STREAM_DRAW);//データ変更なし
  258.  
  259.     //インデックス
  260.     //glGenBuffers(1,&VboIndex);
  261.     //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,VboIndex);
  262.     //glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indexAry),
  263.     //              indexAry,GL_STATIC_DRAW);//データ変更なし
  264. }
  265.  
  266. static void
  267. idle()
  268. {
  269.     angle=0;
  270.     //tx=fmodf(tx+0.01,10);
  271.     //ty=fmodf(ty+0.01,10);
  272.     //tz=fmodf(tz+0.01,10);
  273.     //sx=fmodf(sx+0.01,10);
  274.     //sy=fmodf(sy+0.01,10);
  275.     //sz=fmodf(sz+0.01,10);
  276.     tx_p=(tx_p+0.5)*M_PI;
  277.     tx=(float)sin(tx_p);
  278.     //printf("%f\n",tx);
  279.     ty_p=(ty_p+0.5)*M_PI;
  280.     ty=(float)sin(ty_p);
  281.     //printf("%f\n",ty);
  282.     tz_p=(tz_p+0.5)*M_PI;
  283.     tz=(float)sin(tz_p);
  284.     //printf("%f\n",tz);
  285.     sx=2.0;
  286.     sy=2.0;
  287.     sz=2.0;
  288.     glutPostRedisplay();
  289. }
  290.  
  291. //-------------------------------------------------------------------------
  292. // Calculates the frames per second
  293. //-------------------------------------------------------------------------
  294.  
  295. void calculateFPS()
  296. {
  297.         //  Increase frame count
  298.         frameCount++;
  299.        
  300.         //  Get the number of milliseconds since glutInit called
  301.         //  (or first call to glutGet(GLUT ELAPSED TIME)).
  302.         currentTime = glutGet(GLUT_ELAPSED_TIME);
  303.        
  304.         //  Calculate time passed
  305.         int timeInterval = currentTime - previousTime;
  306.        
  307.         if(timeInterval > 1000)
  308.         {
  309.                 //  calculate the number of frames per second
  310.  
  311.             fps = frameCount / (timeInterval / 1000.0f);
  312.                 //  Set time
  313.                 previousTime = currentTime;
  314.                
  315.                 //  Reset frame count
  316.                 frameCount = 0;
  317.         }
  318.    
  319.         //printf ("FPS: %4.2f\n", fps);
  320.    
  321. }
  322.  
  323.  
  324. //-------------------------------------------------------------------------
  325. //  Draw FPS
  326. //-------------------------------------------------------------------------
  327. /*
  328. void drawFPS()
  329. {
  330.         //  Load the identity matrix so that FPS string being drawn
  331.         //  won't get animates
  332.         glLoadIdentity ();
  333.  
  334.         fps=glutGet(GLUT_ELAPSED_TIME);
  335.  
  336.         //  Print the FPS to the window
  337.         printf ("FPS: %4.2f\n", fps);
  338. }
  339.  */
  340.  
  341. //----------- メイン関数------------//
  342. int main(int argc, char *argv[])
  343. {
  344.     FILE *fpi;
  345.         //int face,vertex,point;
  346.         int i,j,k;
  347.         float data;
  348.  
  349.         if(argc!=5)
  350.         {
  351.                 fprintf(stderr,"Usage: %s 5m_mesh_data.lem,5m_mesh_data.raw\n",argv[0]);
  352.                 exit(1);
  353.         }
  354.         printf("OPEN FILE NAME:%s\n",argv[1]);
  355.  
  356.         if((fpi=fopen(argv[1],"rb"))==NULL)
  357.         {
  358.                 fprintf(stderr,"Can not open\n");
  359.                 exit(1);
  360.         }
  361.  
  362.         face=atoi(argv[2]);
  363.         vertex=atoi(argv[3]);
  364.         point=atoi(argv[4]);
  365.         //printf("check1\n");
  366.         vertexAry=(GLfloat *)malloc(face*vertex*point*sizeof(GLfloat));
  367.         //printf("check2\n");
  368.         fread((GLfloat *)vertexAry,sizeof(GLfloat),face*vertex*point,fpi);
  369.         //printf("check3\n");
  370.     //GLUTの初期設定
  371.     glutInit(&argc, argv);
  372.     glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  373.     glutInitWindowSize(640,480);
  374.     glutCreateWindow("VBO Sample");
  375.     //GLEWの初期設定
  376.     GLenum err = glewInit();
  377.     if(err != GLEW_OK)
  378.     {
  379.         fprintf(stderr,"Err:%s\n",
  380.             glewGetErrorString(err));
  381.         return -1;
  382.     }
  383.  
  384.     //拡張チェック
  385.     if(!glewIsExtensionSupported(
  386.                     "GL_ARB_vertex_buffer_object")){
  387.         puts("you Can't use VBO");
  388.         return -1;
  389.     }
  390.  
  391.     //コールバック
  392.     glutDisplayFunc(display);
  393.  
  394.     glutReshapeFunc(reshape);
  395.     glutIdleFunc(idle);
  396.  
  397.     otherInit();
  398.     buildVBO(face,vertex,point);//VBOの作成
  399.  
  400.     calculateFPS();
  401.     //drawFPS();
  402.     //fps=glutGet(GLUT_ELAPSED_TIME);
  403.  
  404.     glutMainLoop();
  405.  
  406.     return 0;
  407. }
Add Comment
Please, Sign In to add comment