ait-survey

sample25-mac

Oct 1st, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.23 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>//glut.hより先にインクルード
  4. #include "glew.h"
  5. //#include <GL/glew.h>//glut.hより先にインクルード
  6. #include <glut/glut.h>
  7.  
  8. //------- 頂点データ-----------//
  9. //「6」面、「4」頂点、1頂点はx,y,zの「3」要素
  10. GLfloat vertexAry[6][4][3] =
  11. {
  12.     //1つめの面(v0-v1-v2-v3)
  13.     {
  14.         {0.f,0.f,0.f},{1.f,0.f,0.f},
  15.         {1.f,1.f,0.f},{0.f,1.f,0.f}
  16.     },
  17.     //2つめの面(v1-v5-v6-v2)
  18.     {
  19.         {1.f,0.f,0.f},{1.f,0.f,-1.f},
  20.         {1.f,1.f,-1.f},{1.f,1.f,0.f}
  21.     },
  22.     //3つめの面(v4-v7-v6-v5)
  23.     {
  24.         {0.f,0.f,-1.f},{0.f,1.f,-1.f},
  25.         {1.f,1.f,-1.f},{1.f,0.f,-1.f}
  26.     },
  27.     //4つめの面(v4-v0-v3-v7)
  28.     {
  29.         {0.f,0.f,-1.f},{0.f,0.f,0.f},
  30.         {0.f,1.f,0.f},{0.f,1.f,-1.f}
  31.     },
  32.     //5つめの面(v3-v2-v6-v7)
  33.     {
  34.         {0.f,1.f,0.f},{1.f,1.f,0.f},
  35.         {1.f,1.f,-1.f},{0.f,1.f,-1.f}
  36.     },
  37.     //6つめの面(v0-v4-v5-v1)
  38.     {
  39.         {0.f,0.f,0.f},{0.f,0.f,-1.f},
  40.         {1.f,0.f,-1.f},{1.f,0.f,0.f}
  41.     }
  42. };
  43.  
  44. //法線データ
  45. GLfloat normalAry[6][4][3] =
  46. {
  47.     //1つめの面(v0-v1-v2-v3)
  48.     {
  49.         {0.f,0.f,1.f},{0.f,0.f,1.f},
  50.         {0.f,0.f,1.f},{0.f,0.f,1.f}
  51.     },
  52.     //2つめの面(v1-v5-v6-v2)
  53.     {
  54.         {1.f,0.f,0.f},{1.f,0.f,0.f},
  55.         {1.f,0.f,0.f},{1.f,0.f,0.f}
  56.     },
  57.     //3つめの面(v4-v7-v6-v5)
  58.     {
  59.         {0.f,0.f,-1.f},{0.f,0.f,-1.f},
  60.         {0.f,0.f,-1.f},{0.f,0.f,-1.f}
  61.     },
  62.     //4つめの面(v4-v0-v3-v7)
  63.     {
  64.         {-1.f,0.f,0.f},{-1.f,0.f,0.f},
  65.         {-1.f,0.f,0.f},{-1.f,0.f,0.f}
  66.     },
  67.     //5つめの面(v3-v2-v6-v7)
  68.     {
  69.         {0.f,1.f,0.f},{0.f,1.f,0.f},
  70.         {0.f,1.f,0.f},{0.f,1.f,0.f}
  71.     },
  72.     //6つめの面(v0-v4-v5-v1)
  73.     {
  74.         {0.f,-1.f,0.f},{0.f,-1.f,0.f},
  75.         {0.f,-1.f,0.f},{0.f,-1.f,0.f}
  76.     }
  77. };
  78.  
  79. //色データ
  80. GLfloat colorAry[6][4][3] =
  81. {
  82.     //1つめの面(v0-v1-v2-v3)
  83.     {
  84.         {0.f,0.f,1.f},{0.f,0.f,1.f},
  85.         {0.f,0.f,1.f},{0.f,0.f,1.f}
  86.     },
  87.     //2つめの面(v1-v5-v6-v2)
  88.     {
  89.         {1.f,0.f,0.f},{1.f,0.f,0.f},
  90.         {1.f,0.f,0.f},{1.f,0.f,0.f}
  91.     },
  92.     //3つめの面(v4-v7-v6-v5)
  93.     {
  94.         {0.f,1.f,1.f},{0.f,1.f,1.f},
  95.         {0.f,1.f,1.f},{0.f,1.f,1.f}
  96.     },
  97.     //4つめの面(v4-v0-v3-v7)
  98.     {
  99.         {1.f,1.f,0.f},{1.f,1.f,0.f},
  100.         {1.f,1.f,0.f},{1.f,1.f,0.f}
  101.     },
  102.     //5つめの面(v3-v2-v6-v7)
  103.     {
  104.         {0.f,1.f,0.f},{0.f,1.f,0.f},
  105.         {0.f,1.f,0.f},{0.f,1.f,0.f}
  106.     },
  107.     //6つめの面(v0-v4-v5-v1)
  108.     {
  109.         {1.f,0.f,1.f},{1.f,0.f,1.f},
  110.         {1.f,0.f,1.f},{1.f,0.f,1.f}
  111.     }
  112. };
  113. //インデックス
  114. GLubyte indexAry[]=
  115. {
  116.     0,1,2,3,
  117.     4,5,6,7,
  118.     8,9,10,11,
  119.     12,13,14,15,
  120.     16,17,18,19,
  121.     20,21,22,23
  122. };
  123.  
  124. // π/180の値
  125. const float PI_OVER_180 = 0.0174532925f;
  126.  
  127. //VBO用ID
  128. GLuint VboId[3];//3つ分
  129. GLuint VboIndex;//インデックス
  130.  
  131. //描画関数
  132. void drawAry(void)
  133. {
  134.     GLfloat *clientPtr;//クライアント側用
  135.     GLfloat tmp[3];
  136.     int idloop;
  137.     int loop;
  138.     static float angle = 2*PI_OVER_180;
  139.    
  140.     //データの場所を知らせる
  141.     //座標
  142.     glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
  143.     glVertexPointer(3, GL_FLOAT, 0, 0);
  144.     //法線
  145.     glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
  146.     glNormalPointer(GL_FLOAT, 0, 0);
  147.     //色
  148.     glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
  149.     glColorPointer(3,GL_FLOAT, 0, 0);
  150.     //インデックスもバインド
  151.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VboIndex);
  152.    
  153.     glEnableClientState(GL_VERTEX_ARRAY);
  154.     glEnableClientState(GL_NORMAL_ARRAY);
  155.     glEnableClientState(GL_COLOR_ARRAY);
  156.     //描画
  157.     glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,0);
  158.    
  159.     glDisableClientState(GL_COLOR_ARRAY);
  160.     glDisableClientState(GL_NORMAL_ARRAY);
  161.     glDisableClientState(GL_VERTEX_ARRAY);
  162.    
  163.     //座標と法線を回転させる
  164.     for(idloop = 0; idloop < 2;++idloop)
  165.     {
  166.         //idloop番目のバッファオブジェクトに注目
  167.         glBindBuffer(GL_ARRAY_BUFFER,VboId[idloop]);
  168.        
  169.         //対応付け
  170.         clientPtr = (GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,
  171.                                            GL_READ_WRITE);
  172.         if(clientPtr != NULL)
  173.         {
  174.             //24頂点*3座標
  175.             for(loop = 0; loop < 24*3;loop += 3)    {
  176.                 //読み出し(READ)
  177.                 tmp[0] = clientPtr[loop];
  178.                 tmp[1] = clientPtr[loop+1];
  179.                 tmp[2] = clientPtr[loop+2];
  180.                 //書き込み(WRITE)
  181.                 clientPtr[loop] = cos(angle)*tmp[0]
  182.                 + sin(angle)*tmp[2];
  183.                 clientPtr[loop+1] = tmp[1];
  184.                 clientPtr[loop+2] = -sin(angle)*tmp[0]
  185.                 + cos(angle)*tmp[2];
  186.             }
  187.             glUnmapBuffer(GL_ARRAY_BUFFER);//対応付けの解放
  188.         }
  189.     }
  190.     //クライアント側に戻す
  191.     glBindBuffer(GL_ARRAY_BUFFER,0);
  192.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  193. }
  194. //------- 各種コールバック----------//
  195. void display(void)
  196. {
  197.     static int angle = 0;
  198.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  199.     glLoadIdentity();
  200.     gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  201.    
  202.     glPushMatrix();
  203.     glRotatef(float(angle),1.f,0.f,0.f);//x軸回転
  204.     drawAry();
  205.     glPopMatrix();
  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(30.0, double(w)/h, 0.1, 200.0);
  217.     glMatrixMode(GL_MODELVIEW);
  218.     glLoadIdentity();
  219. }
  220.  
  221. void idle(void)
  222. {
  223.     glutPostRedisplay();
  224. }
  225.  
  226. void keyboard(unsigned char key, int x, int y)
  227. {
  228.     switch(key)
  229.     {
  230.         case 'q':
  231.         case 'Q':
  232.         case '\033':
  233.             //終了処理
  234.             glDeleteBuffers(3,&VboId[0]);
  235.             glDeleteBuffers(1,&VboIndex);
  236.             exit(0);
  237.             break;
  238.     }
  239. }
  240. //------ その他初期設定-------//
  241. void otherInit(void)
  242. {
  243.     glClearColor(1.f, 1.f, 1.f, 1.f);
  244.     glEnable(GL_DEPTH_TEST);
  245.     glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  246.     glEnable(GL_COLOR_MATERIAL);
  247.     glEnable(GL_LIGHT0);
  248.     glEnable(GL_LIGHTING);
  249.     glEnable(GL_NORMALIZE);//法線の正規化
  250. }
  251.  
  252. //---- VBOの作成----//
  253. void buildVBO(void)
  254. {
  255.     glGenBuffers(3,&VboId[0]);//座標、法線、色の3つ
  256.    
  257.     //頂点
  258.     glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
  259.     glBufferData(GL_ARRAY_BUFFER,sizeof(vertexAry),
  260.                  vertexAry,GL_DYNAMIC_DRAW);//データ変更する
  261.    
  262.     //法線
  263.     glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
  264.     glBufferData(GL_ARRAY_BUFFER,sizeof(normalAry),
  265.                  normalAry,GL_DYNAMIC_DRAW);//データ変更あり
  266.    
  267.     //色
  268.     glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
  269.     glBufferData(GL_ARRAY_BUFFER,sizeof(colorAry),
  270.                  colorAry,GL_STREAM_DRAW);//データ変更なし
  271.    
  272.     //インデックス
  273.     glGenBuffers(1,&VboIndex);
  274.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,VboIndex);
  275.     glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indexAry),
  276.                  indexAry,GL_STATIC_DRAW);//データ変更なし
  277. }
  278. //----------- メイン関数------------//
  279. int main(int argc, char *argv[])
  280. {
  281.     //GLUTの初期設定
  282.     glutInit(&argc, argv);
  283.     glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  284.     glutInitWindowSize(640,480);
  285.     glutCreateWindow("VBO Sample");
  286.     //GLEWの初期設定
  287.     GLenum err = glewInit();
  288.     if(err != GLEW_OK)
  289.     {
  290.         fprintf(stderr,"Err:%s\n",
  291.                 glewGetErrorString(err));
  292.         return -1;
  293.     }
  294.     //拡張チェック
  295.     if(!glewIsExtensionSupported(
  296.                                  "GL_ARB_vertex_buffer_object")){
  297.         puts("you Can't use VBO");
  298.         return -1;
  299.     }
  300.    
  301.     //コールバック
  302.     glutDisplayFunc(display);
  303.     glutReshapeFunc(reshape);
  304.     glutIdleFunc(idle);
  305.     glutKeyboardFunc(keyboard);
  306.    
  307.     otherInit();
  308.    
  309.     buildVBO();//VBOの作成
  310.     glutMainLoop();
  311.    
  312.     return 0;
  313. }
Advertisement
Add Comment
Please, Sign In to add comment