Advertisement
hasegawa

sample25

Sep 20th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.37 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. //static unsigned int fps_start=0;
  8. //static unsigned int fps_frames=0;
  9. //  The number of frames
  10. int frameCount = 0;
  11. float fps = 0;
  12.  
  13. //  currentTime - previousTime is the time elapsed
  14. //  between every call of the Idle function
  15. int currentTime = 0, previousTime = 0;
  16.  
  17. //  printf prints to file. printw prints to window
  18. //void printw (float x, float y, float z, char* format, ...);
  19.  
  20. void calculateFPS();
  21.  
  22. //void drawFPS();
  23.  
  24.  
  25. //------- 頂点データ-----------//
  26. //「6」面、「4」頂点、1頂点はx,y,zの「3」要素
  27. GLfloat vertexAry[6][4][3] =
  28. {
  29.     //1つめの面(v0-v1-v2-v3)
  30.     {
  31.         {0.f,0.f,0.f},{1.f,0.f,0.f},
  32.         {1.f,1.f,0.f},{0.f,1.f,0.f}
  33.     },
  34.     //2つめの面(v1-v5-v6-v2)
  35.     {
  36.         {1.f,0.f,0.f},{1.f,0.f,-1.f},
  37.         {1.f,1.f,-1.f},{1.f,1.f,0.f}
  38.     },
  39.     //3つめの面(v4-v7-v6-v5)
  40.     {
  41.         {0.f,0.f,-1.f},{0.f,1.f,-1.f},
  42.         {1.f,1.f,-1.f},{1.f,0.f,-1.f}
  43.     },
  44.     //4つめの面(v4-v0-v3-v7)
  45.     {
  46.         {0.f,0.f,-1.f},{0.f,0.f,0.f},
  47.         {0.f,1.f,0.f},{0.f,1.f,-1.f}
  48.     },
  49.     //5つめの面(v3-v2-v6-v7)
  50.     {
  51.         {0.f,1.f,0.f},{1.f,1.f,0.f},
  52.         {1.f,1.f,-1.f},{0.f,1.f,-1.f}
  53.     },
  54.     //6つめの面(v0-v4-v5-v1)
  55.     {
  56.         {0.f,0.f,0.f},{0.f,0.f,-1.f},
  57.         {1.f,0.f,-1.f},{1.f,0.f,0.f}
  58.     }
  59. };
  60.  
  61. //法線データ
  62. GLfloat normalAry[6][4][3] =
  63. {
  64.     //1つめの面(v0-v1-v2-v3)
  65.     {
  66.         {0.f,0.f,1.f},{0.f,0.f,1.f},
  67.         {0.f,0.f,1.f},{0.f,0.f,1.f}
  68.     },
  69.     //2つめの面(v1-v5-v6-v2)
  70.     {
  71.         {1.f,0.f,0.f},{1.f,0.f,0.f},
  72.         {1.f,0.f,0.f},{1.f,0.f,0.f}
  73.     },
  74.     //3つめの面(v4-v7-v6-v5)
  75.     {
  76.         {0.f,0.f,-1.f},{0.f,0.f,-1.f},
  77.         {0.f,0.f,-1.f},{0.f,0.f,-1.f}
  78.     },
  79.     //4つめの面(v4-v0-v3-v7)
  80.     {
  81.         {-1.f,0.f,0.f},{-1.f,0.f,0.f},
  82.         {-1.f,0.f,0.f},{-1.f,0.f,0.f}
  83.     },
  84.     //5つめの面(v3-v2-v6-v7)
  85.     {
  86.         {0.f,1.f,0.f},{0.f,1.f,0.f},
  87.         {0.f,1.f,0.f},{0.f,1.f,0.f}
  88.     },
  89.     //6つめの面(v0-v4-v5-v1)
  90.     {
  91.         {0.f,-1.f,0.f},{0.f,-1.f,0.f},
  92.         {0.f,-1.f,0.f},{0.f,-1.f,0.f}
  93.     }
  94. };
  95.  
  96. //色データ
  97. GLfloat colorAry[6][4][3] =
  98. {
  99.     //1つめの面(v0-v1-v2-v3)
  100.     {
  101.         {0.f,0.f,1.f},{0.f,0.f,1.f},
  102.         {0.f,0.f,1.f},{0.f,0.f,1.f}
  103.     },
  104.     //2つめの面(v1-v5-v6-v2)
  105.     {
  106.         {1.f,0.f,0.f},{1.f,0.f,0.f},
  107.         {1.f,0.f,0.f},{1.f,0.f,0.f}
  108.     },
  109.     //3つめの面(v4-v7-v6-v5)
  110.     {
  111.         {0.f,1.f,1.f},{0.f,1.f,1.f},
  112.         {0.f,1.f,1.f},{0.f,1.f,1.f}
  113.     },
  114.     //4つめの面(v4-v0-v3-v7)
  115.     {
  116.         {1.f,1.f,0.f},{1.f,1.f,0.f},
  117.         {1.f,1.f,0.f},{1.f,1.f,0.f}
  118.     },
  119.     //5つめの面(v3-v2-v6-v7)
  120.     {
  121.         {0.f,1.f,0.f},{0.f,1.f,0.f},
  122.         {0.f,1.f,0.f},{0.f,1.f,0.f}
  123.     },
  124.     //6つめの面(v0-v4-v5-v1)
  125.     {
  126.         {1.f,0.f,1.f},{1.f,0.f,1.f},
  127.         {1.f,0.f,1.f},{1.f,0.f,1.f}
  128.     }
  129. };
  130. //インデックス
  131. GLubyte indexAry[]=
  132. {
  133.     0,1,2,3,
  134.     4,5,6,7,
  135.     8,9,10,11,
  136.     12,13,14,15,
  137.     16,17,18,19,
  138.     20,21,22,23
  139. };
  140.  
  141. // π/180の値
  142. const float PI_OVER_180 = 0.0174532925f;
  143.  
  144. //VBO用ID
  145. GLuint VboId[3];//3つ分
  146. GLuint VboIndex;//インデックス
  147.  
  148. //描画関数
  149. void drawAry(void)
  150. {
  151.     GLfloat *clientPtr;//クライアント側用
  152.     GLfloat tmp[3];
  153.     int idloop;
  154.     int loop;
  155.     static float angle = 2*PI_OVER_180;
  156.  
  157.     //データの場所を知らせる
  158.     //座標
  159.     glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
  160.     glVertexPointer(3, GL_FLOAT, 0, 0);
  161.     //法線
  162.     glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
  163.     glNormalPointer(GL_FLOAT, 0, 0);
  164.     //色
  165.     glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
  166.     glColorPointer(3,GL_FLOAT, 0, 0);
  167.     //インデックスもバインド
  168.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VboIndex);
  169.  
  170.     glEnableClientState(GL_VERTEX_ARRAY);
  171.     glEnableClientState(GL_NORMAL_ARRAY);
  172.     glEnableClientState(GL_COLOR_ARRAY);
  173.     //描画
  174.     glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,0);
  175.    
  176.     glDisableClientState(GL_COLOR_ARRAY);
  177.     glDisableClientState(GL_NORMAL_ARRAY);
  178.     glDisableClientState(GL_VERTEX_ARRAY);
  179.  
  180.     //座標と法線を回転させる
  181.     for(idloop = 0; idloop < 2;++idloop)
  182.     {
  183.         //idloop番目のバッファオブジェクトに注目
  184.         glBindBuffer(GL_ARRAY_BUFFER,VboId[idloop]);
  185.        
  186.         //対応付け
  187.         clientPtr = (GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,
  188.                                                 GL_READ_WRITE);
  189.         if(clientPtr != NULL)
  190.         {
  191.             //24頂点*3座標
  192.              for(loop = 0; loop < 24*3;loop += 3)  
  193.             {
  194.                 //読み出し(READ)
  195.                 tmp[0] = clientPtr[loop];
  196.                 tmp[1] = clientPtr[loop+1];
  197.                 tmp[2] = clientPtr[loop+2];
  198.                 //書き込み(WRITE)
  199.                 clientPtr[loop] = cos(angle)*tmp[0]
  200.                                     + sin(angle)*tmp[2];
  201.                 clientPtr[loop+1] = tmp[1];
  202.                 clientPtr[loop+2] = -sin(angle)*tmp[0]
  203.                                         + cos(angle)*tmp[2];
  204.              }
  205.             glUnmapBuffer(GL_ARRAY_BUFFER);//対応付けの解放
  206.         }
  207.     }
  208.  
  209.     //クライアント側に戻す
  210.     glBindBuffer(GL_ARRAY_BUFFER,0);
  211.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  212. }
  213. //------- 各種コールバック----------//
  214. void display(void)
  215. {
  216.     static int angle = 0;
  217.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  218.     glLoadIdentity();
  219.     gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  220.    
  221.     glPushMatrix();
  222.     //glRotatef(float(angle),1.f,0.f,0.f);//x軸回転
  223.     drawAry();
  224.     glPopMatrix();
  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. void keyboard(unsigned char key, int x, int y)
  246. {
  247.     switch(key)
  248.     {
  249.     case 'q':
  250.     case 'Q':
  251.     case '\033':
  252.         //終了処理
  253.         glDeleteBuffers(3,&VboId[0]);
  254.         glDeleteBuffers(1,&VboIndex);
  255.         exit(0);
  256.         break;
  257.     }
  258. }
  259. //------ その他初期設定-------//
  260. void otherInit(void)
  261. {
  262.     glClearColor(1.f, 1.f, 1.f, 1.f);
  263.     glEnable(GL_DEPTH_TEST);
  264.     glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  265.     glEnable(GL_COLOR_MATERIAL);
  266.     glEnable(GL_LIGHT0);
  267.     glEnable(GL_LIGHTING);
  268.     glEnable(GL_NORMALIZE);//法線の正規化
  269. }
  270.  
  271. //---- VBOの作成----//
  272. void buildVBO(void)
  273. {
  274.     glGenBuffers(3,&VboId[0]);//座標、法線、色の3つ
  275.    
  276.     //頂点
  277.     glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
  278.     glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*72,
  279.                 vertexAry,GL_DYNAMIC_DRAW);//データ変更する
  280.  
  281.     //法線
  282.     glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
  283.     glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*72,
  284.                 normalAry,GL_DYNAMIC_DRAW);//データ変更あり
  285.  
  286.     //色
  287.     glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
  288.     glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*72,
  289.                     colorAry,GL_STREAM_DRAW);//データ変更なし
  290.  
  291.     //インデックス
  292.     glGenBuffers(1,&VboIndex);
  293.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,VboIndex);
  294.     glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(GLubyte)*24,
  295.                     indexAry,GL_STATIC_DRAW);//データ変更なし
  296. }
  297.  
  298. //-------------------------------------------------------------------------
  299. // Calculates the frames per second
  300. //-------------------------------------------------------------------------
  301.  
  302. void calculateFPS()
  303. {
  304.     //  Increase frame count
  305.     frameCount++;
  306.    
  307.     //  Get the number of milliseconds since glutInit called
  308.     //  (or first call to glutGet(GLUT ELAPSED TIME)).
  309.     currentTime = glutGet(GLUT_ELAPSED_TIME);
  310.    
  311.     //  Calculate time passed
  312.     int timeInterval = currentTime - previousTime;
  313.    
  314.     if(timeInterval > 1000)
  315.     {
  316.         //  calculate the number of frames per second
  317.         fps = frameCount / (timeInterval / 1000.0f);
  318.        
  319.         //  Set time
  320.         previousTime = currentTime;
  321.        
  322.         //  Reset frame count
  323.         frameCount = 0;
  324.     }
  325.    
  326.     printf ("FPS: %4.2f\n", fps);
  327.    
  328. }
  329. //----------- メイン関数------------//
  330. int main(int argc, char *argv[])
  331. {
  332.     //GLUTの初期設定
  333.     glutInit(&argc, argv);
  334.     glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  335.     glutInitWindowSize(640,480);
  336.     glutCreateWindow("VBO Sample");
  337.     //GLEWの初期設定
  338.     GLenum err = glewInit();
  339.     if(err != GLEW_OK)
  340.     {
  341.         fprintf(stderr,"Err:%s\n",
  342.             glewGetErrorString(err));
  343.         return -1;
  344.     }
  345.     //拡張チェック
  346.     if(!glewIsExtensionSupported(
  347.                     "GL_ARB_vertex_buffer_object")){
  348.         puts("you Can't use VBO");
  349.         return -1;
  350.     }
  351.  
  352.     //コールバック
  353.     glutDisplayFunc(display);
  354.     glutReshapeFunc(reshape);
  355.     glutIdleFunc(idle);
  356.     glutKeyboardFunc(keyboard);
  357.  
  358.     otherInit();
  359.    
  360.     buildVBO();//VBOの作成
  361.  
  362.     calculateFPS();
  363.     //drawFPS();
  364.     //fps=glutGet(GLUT_ELAPSED_TIME);
  365.     glutMainLoop();
  366.    
  367.     return 0;
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement