Advertisement
d10070dd

new-sample25-edit

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