Advertisement
hasegawa

sample25-point-color

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