Advertisement
d10070dd

old-triangle-binary

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