ait-survey

sample25-win

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