Advertisement
d10070dd

new-sample25

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