Advertisement
hasegawa

sample25-points

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