Advertisement
d10070dd

sample25-triangle-edit

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