d10070dd

sample22

Sep 14th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. #include <gl/glut.h>
  2.  
  3. //------- 頂点データ-----------//
  4. //「6」面、「4」頂点、1頂点はx,y,zの「3」要素
  5. GLfloat vertexAry[6][4][3] =
  6. {
  7.     //1つめの面(v0-v1-v2-v3)
  8.     {
  9.         {0.f,0.f,0.f},{1.f,0.f,0.f},
  10.         {1.f,1.f,0.f},{0.f,1.f,0.f}
  11.     },
  12.     //2つめの面(v1-v5-v6-v2)
  13.     {
  14.         {1.f,0.f,0.f},{1.f,0.f,-1.f},
  15.         {1.f,1.f,-1.f},{1.f,1.f,0.f}
  16.     },
  17.     //3つめの面(v4-v7-v6-v5)
  18.     {
  19.         {0.f,0.f,-1.f},{0.f,1.f,-1.f},
  20.         {1.f,1.f,-1.f},{1.f,0.f,-1.f}
  21.     },
  22.     //4つめの面(v4-v0-v3-v7)
  23.     {
  24.         {0.f,0.f,-1.f},{0.f,0.f,0.f},
  25.         {0.f,1.f,0.f},{0.f,1.f,-1.f}
  26.     },
  27.     //5つめの面(v3-v2-v6-v7)
  28.     {
  29.         {0.f,1.f,0.f},{1.f,1.f,0.f},
  30.         {1.f,1.f,-1.f},{0.f,1.f,-1.f}
  31.     },
  32.     //6つめの面(v0-v4-v5-v1)
  33.     {
  34.         {0.f,0.f,0.f},{0.f,0.f,-1.f},
  35.         {1.f,0.f,-1.f},{1.f,0.f,0.f}
  36.     }
  37. };
  38.  
  39. //法線データ
  40. GLfloat normalAry[6][4][3] =
  41. {
  42.     //1つめの面(v0-v1-v2-v3)
  43.     {
  44.         {0.f,0.f,1.f},{0.f,0.f,1.f},
  45.         {0.f,0.f,1.f},{0.f,0.f,1.f}
  46.     },
  47.     //2つめの面(v1-v5-v6-v2)
  48.     {
  49.         {1.f,0.f,0.f},{1.f,0.f,0.f},
  50.         {1.f,0.f,0.f},{1.f,0.f,0.f}
  51.     },
  52.     //3つめの面(v4-v7-v6-v5)
  53.     {
  54.         {0.f,0.f,-1.f},{0.f,0.f,-1.f},
  55.         {0.f,0.f,-1.f},{0.f,0.f,-1.f}
  56.     },
  57.     //4つめの面(v4-v0-v3-v7)
  58.     {
  59.         {-1.f,0.f,0.f},{-1.f,0.f,0.f},
  60.         {-1.f,0.f,0.f},{-1.f,0.f,0.f}
  61.     },
  62.     //5つめの面(v3-v2-v6-v7)
  63.     {
  64.         {0.f,1.f,0.f},{0.f,1.f,0.f},
  65.         {0.f,1.f,0.f},{0.f,1.f,0.f}
  66.     },
  67.     //6つめの面(v0-v4-v5-v1)
  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. };
  73.  
  74. //色データ
  75. GLfloat colorAry[6][4][3] =
  76. {
  77.     //1つめの面(v0-v1-v2-v3)
  78.     {
  79.         {0.f,0.f,1.f},{0.f,0.f,1.f},
  80.         {0.f,0.f,1.f},{0.f,0.f,1.f}
  81.     },
  82.     //2つめの面(v1-v5-v6-v2)
  83.     {
  84.         {1.f,0.f,0.f},{1.f,0.f,0.f},
  85.         {1.f,0.f,0.f},{1.f,0.f,0.f}
  86.     },
  87.     //3つめの面(v4-v7-v6-v5)
  88.     {
  89.         {0.f,1.f,1.f},{0.f,1.f,1.f},
  90.         {0.f,1.f,1.f},{0.f,1.f,1.f}
  91.     },
  92.     //4つめの面(v4-v0-v3-v7)
  93.     {
  94.         {1.f,1.f,0.f},{1.f,1.f,0.f},
  95.         {1.f,1.f,0.f},{1.f,1.f,0.f}
  96.     },
  97.     //5つめの面(v3-v2-v6-v7)
  98.     {
  99.         {0.f,1.f,0.f},{0.f,1.f,0.f},
  100.         {0.f,1.f,0.f},{0.f,1.f,0.f}
  101.     },
  102.     //6つめの面(v0-v4-v5-v1)
  103.     {
  104.         {1.f,0.f,1.f},{1.f,0.f,1.f},
  105.         {1.f,0.f,1.f},{1.f,0.f,1.f}
  106.     }
  107. };
  108.  
  109. void drawAry(void)
  110. {
  111.     //有効化
  112.     glEnableClientState(GL_VERTEX_ARRAY);
  113.     glEnableClientState(GL_NORMAL_ARRAY);
  114.     glEnableClientState(GL_COLOR_ARRAY);
  115.  
  116.     //データの関連付け
  117.     glVertexPointer(3, GL_FLOAT, 0, vertexAry);//座標
  118.     glNormalPointer(GL_FLOAT, 0, normalAry);//法線
  119.     glColorPointer(3,GL_FLOAT, 0, colorAry);//色
  120.  
  121.     glDrawArrays(GL_QUADS,0,4*6); //描画(4頂点*6面)
  122.  
  123.     //無効化
  124.     glDisableClientState(GL_COLOR_ARRAY);
  125.     glDisableClientState(GL_NORMAL_ARRAY);
  126.     glDisableClientState(GL_VERTEX_ARRAY);
  127. }
  128. //------- 各種コールバック----------//
  129. void display(void)
  130. {
  131.     static int angle = 0;
  132.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  133.     glLoadIdentity();
  134.     gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  135.    
  136.     glPushMatrix();
  137.     glRotatef(float(angle),1.f,0.f,0.f);
  138.     glRotatef(float(angle+45),0.f,1.f,0.f);
  139.     drawAry();
  140.     glPopMatrix();
  141.  
  142.     glutSwapBuffers();
  143.     if(++angle >= 360) angle = 0;
  144. }
  145.  
  146. void reshape(int w, int h)
  147. {
  148.     glViewport(0,0,w,h);
  149.     glMatrixMode(GL_PROJECTION);
  150.     glLoadIdentity();
  151.     gluPerspective(30.0, double(w)/h, 0.1, 200.0);
  152.     glMatrixMode(GL_MODELVIEW);
  153.     glLoadIdentity();
  154. }
  155.  
  156. void idle(void)
  157. {
  158.     glutPostRedisplay();
  159. }
  160. //------ その他初期設定-------//
  161. void otherInit(void)
  162. {
  163.     glClearColor(1.f, 1.f, 1.f, 1.f);
  164.     glEnable(GL_DEPTH_TEST);
  165.     glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  166.     glEnable(GL_COLOR_MATERIAL);
  167.     glEnable(GL_LIGHT0);
  168.     glEnable(GL_LIGHTING);
  169.     glEnable(GL_NORMALIZE);//法線の正規化
  170. }
  171. //----------- メイン関数------------//
  172. int main(int argc, char *argv[])
  173. {
  174.     //GLUTの初期設定
  175.     glutInit(&argc, argv);
  176.     glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  177.     glutInitWindowSize(640,480);
  178.     glutCreateWindow("glDrawArray");
  179.  
  180.     //コールバック
  181.     glutDisplayFunc(display);
  182.     glutReshapeFunc(reshape);
  183.     glutIdleFunc(idle);
  184.  
  185.     otherInit();//その他初期設定
  186.     glutMainLoop();
  187.  
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment