Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>//glut.hより先にインクルード
- #include <gl/glew.h>//glut.hより先にインクルード
- #include <gl/glut.h>
- #define M_PI 3.141592
- GLfloat *vertexAry;
- static
- int angle=0;
- float tx=0.0;
- float ty=0.0;
- float tz=0.0;
- float sx=1.0;
- float sy=1.0;
- float sz=1.0;
- double tx_p=0;
- double ty_p=0;
- double tz_p=0;
- double sx_p=0;
- double sy_p=0;
- double sz_p=0;
- //static unsigned int fps_start=0;
- //static unsigned int fps_frames=0;
- // The number of frames
- int frameCount = 0;
- float fps = 0;
- // currentTime - previousTime is the time elapsed
- // between every call of theIdle function
- int currentTime = 0, previousTime = 0;
- // printf prints to file. printw prints to window
- //void printw (float x, float y, float z, char* format, ...);
- void calculateFPS();
- //void drawFPS();
- //------- 頂点データ-----------//
- //「6」面、「4」頂点、1頂点はx,y,zの「3」要素
- /*
- GLfloat vertexAry[3][3] =
- {
- {0.0f,0.6f,0.0f},{-0.6f,0.0f,0.0f},
- {0.6f,0.0f,0.0f}
- };
- */
- //法線データ
- GLfloat normalAry[3][3] =
- {
- {0.0f,0.6f,0.0f},{-0.6f,0.0f,0.0f},
- {0.6f,0.0f,0.0f}
- };
- //色データ
- GLfloat colorAry[3][3] =
- {
- {0.0f,0.6f,0.0f},{-0.6f,0.0f,0.0f},
- {0.6f,0.0f,0.0f}
- };
- //インデックス
- GLint indexAry[]=
- {
- 0,1,2
- };
- // π/180の値
- const float PI_OVER_180 = 0.0174532925f;
- //VBO用ID
- GLuint VboId[3];//3つ分
- GLuint VboIndex;//インデックス
- //描画関数
- void drawAry(void)
- {
- GLfloat *clientPtr;//クライアント側用
- GLfloat tmp[3];
- int idloop;
- int loop;
- static float angle = 2*PI_OVER_180;
- //データの場所を知らせる
- //座標
- glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
- glVertexPointer(3, GL_FLOAT, 0, 0);
- //法線
- glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
- glNormalPointer(GL_FLOAT, 0, 0);
- //色
- glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
- glColorPointer(3,GL_FLOAT, 0, 0);
- //インデックスもバインド
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VboIndex);
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_NORMAL_ARRAY);
- glEnableClientState(GL_COLOR_ARRAY);
- //描画
- //glDrawElements(GL_TRIANGLES,685,GL_UNSIGNED_INT,0);
- glDrawArrays(GL_TRIANGLES,0,685);
- glDisableClientState(GL_COLOR_ARRAY);
- glDisableClientState(GL_NORMAL_ARRAY);
- glDisableClientState(GL_VERTEX_ARRAY);
- //座標と法線を回転させる
- //for(idloop = 0; idloop < 2;++idloop)
- {
- //idloop番目のバッファオブジェクトに注目
- //glBindBuffer(GL_ARRAY_BUFFER,VboId[idloop]);
- //対応付け
- clientPtr = (GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,GL_READ_WRITE);
- if(clientPtr != NULL)
- {
- //24頂点*3座標
- for(loop = 0; loop < 397*3;loop += 3) {
- //読み出し(READ)
- tmp[0] = clientPtr[loop];
- tmp[1] = clientPtr[loop+1];
- tmp[2] = clientPtr[loop+2];
- //書き込み(WRITE)
- clientPtr[loop] = cos(angle)*tmp[0]
- + sin(angle)*tmp[2];
- clientPtr[loop+1] = tmp[1];
- clientPtr[loop+2] = -sin(angle)*tmp[0]
- + cos(angle)*tmp[2];
- }
- glUnmapBuffer(GL_ARRAY_BUFFER);//対応付けの解放
- }
- }
- //クライアント側に戻す
- glBindBuffer(GL_ARRAY_BUFFER,0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- }
- //------- 各種コールバック----------//
- void display(void)
- {
- static int angle = 0;
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
- //gluLookAt(0.30, 0.40, 0.50, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10);
- glPushMatrix();
- calculateFPS();
- printf ("angle_x1_fps: %4.2f\n", fps);
- glRotatef(angle,1.0,0.0,0.0);//x軸回転
- glRotatef(angle+90,1.0,0.0,0.0);//x軸回転
- glRotatef(angle+180,1.0,0.0,0.0);//x軸回転
- calculateFPS();
- printf ("angle_x2_fps: %4.2f\n", fps);
- glRotatef(angle+270,1.0,0.0,0.0);//x軸回転
- glRotatef(angle+360,1.0,0.0,0.0);//x軸回転
- calculateFPS();
- printf ("angle_x3_fps: %4.2f\n", fps);
- calculateFPS();
- printf ("angle_y1_fps: %4.2f\n", fps);
- glRotatef(angle,0.0,1.0,0.0);
- glRotatef(angle+90,0.0,1.0,0.0);
- glRotatef(angle+180,0.0,1.0,0.0);
- calculateFPS();
- printf ("angle_y2_fps: %4.2f\n", fps);
- glRotatef(angle+270,0.0,1.0,0.0);
- glRotatef(angle+360,0.0,1.0,0.0);
- calculateFPS();
- printf ("angle_y3_fps: %4.2f\n", fps);
- calculateFPS();
- printf ("angle_z1_fps: %4.2f\n", fps);
- glRotatef(angle,0.0,0.0,1.0);
- glRotatef(angle+90,0.0,0.0,1.0);
- glRotatef(angle+180,0.0,0.0,1.0);
- calculateFPS();
- printf ("angle_z2_fps: %4.2f\n", fps);
- glRotatef(angle+270,0.0,0.0,1.0);
- glRotatef(angle+360,0.0,0.0,1.0);
- calculateFPS();
- printf ("angle_z3_fps: %4.2f\n", fps);
- calculateFPS();
- printf ("txyz1_fps: %4.2f\n", fps);
- glTranslatef(tx,ty,tz);
- calculateFPS();
- printf ("txyz2_fps: %4.2f\n", fps);
- glTranslatef(-1.0,-1.0,-1.0);
- calculateFPS();
- printf ("txyz3_fps: %4.2f\n", fps);
- calculateFPS();
- printf ("sxyz1_fps: %4.2f\n", fps);
- glScalef(sx, sy, sz);
- calculateFPS();
- printf ("sxyz2_fps: %4.2f\n", fps);
- glScalef(1.0, 1.0, 1.0);
- calculateFPS();
- printf ("sxyz3_fps: %4.2f\n", fps);
- drawAry();
- glPopMatrix();
- calculateFPS();
- //drawFPS();
- glutSwapBuffers();
- if(++angle >= 360) angle = 0;
- }
- void reshape(int w, int h)
- {
- glViewport(0,0,w,h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(30.0, double(w)/h, 0.1, 200.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- /*void idle(void)
- {
- glutPostRedisplay();
- }
- */
- //------ その他初期設定-------//
- void otherInit(void)
- {
- glClearColor(1.f, 1.f, 1.f, 1.f);
- glEnable(GL_DEPTH_TEST);
- glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_LIGHT0);
- glEnable(GL_LIGHTING);
- glEnable(GL_NORMALIZE);//法線の正規化
- }
- //---- VBOの作成----//
- void buildVBO(int a, int b, int c)
- {
- glGenBuffers(3,&VboId[0]);//座標、法線、色の3つ
- //頂点
- glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
- glBufferData(GL_ARRAY_BUFFER,sizeof(vertexAry)*a*b*c,
- vertexAry,GL_DYNAMIC_DRAW);//データ変更する
- //法線
- glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
- glBufferData(GL_ARRAY_BUFFER,sizeof(normalAry),
- normalAry,GL_DYNAMIC_DRAW);//データ変更あり
- //色
- glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
- glBufferData(GL_ARRAY_BUFFER,sizeof(colorAry),
- colorAry,GL_STREAM_DRAW);//データ変更なし
- //インデックス
- glGenBuffers(1,&VboIndex);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,VboIndex);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indexAry),
- indexAry,GL_STATIC_DRAW);//データ変更なし
- }
- static void
- idle()
- {
- angle=0;
- //tx=fmodf(tx+0.01,10);
- //ty=fmodf(ty+0.01,10);
- //tz=fmodf(tz+0.01,10);
- //sx=fmodf(sx+0.01,10);
- //sy=fmodf(sy+0.01,10);
- //sz=fmodf(sz+0.01,10);
- tx_p=(tx_p+0.5)*M_PI;
- tx=(float)sin(tx_p);
- //printf("%f\n",tx);
- ty_p=(ty_p+0.5)*M_PI;
- ty=(float)sin(ty_p);
- //printf("%f\n",ty);
- tz_p=(tz_p+0.5)*M_PI;
- tz=(float)sin(tz_p);
- //printf("%f\n",tz);
- sx=2.0;
- sy=2.0;
- sz=2.0;
- glutPostRedisplay();
- }
- //-------------------------------------------------------------------------
- // Calculates the frames per second
- //-------------------------------------------------------------------------
- void calculateFPS()
- {
- // Increase frame count
- frameCount++;
- // Get the number of milliseconds since glutInit called
- // (or first call to glutGet(GLUT ELAPSED TIME)).
- currentTime = glutGet(GLUT_ELAPSED_TIME);
- // Calculate time passed
- int timeInterval = currentTime - previousTime;
- if(timeInterval > 1000)
- {
- // calculate the number of frames per second
- fps = frameCount / (timeInterval / 1000.0f);
- // Set time
- previousTime = currentTime;
- // Reset frame count
- frameCount = 0;
- }
- //printf ("FPS: %4.2f\n", fps);
- }
- //-------------------------------------------------------------------------
- // Draw FPS
- //-------------------------------------------------------------------------
- /*
- void drawFPS()
- {
- // Load the identity matrix so that FPS string being drawn
- // won't get animates
- glLoadIdentity ();
- fps=glutGet(GLUT_ELAPSED_TIME);
- // Print the FPS to the window
- printf ("FPS: %4.2f\n", fps);
- }
- */
- //----------- メイン関数------------//
- int main(int argc, char *argv[])
- {
- FILE *fpi;
- int face,vertex,point;
- int i,j,k;
- float data;
- if(argc!=5)
- {
- fprintf(stderr,"Usage: %s 5m_mesh_data.lem,5m_mesh_data.raw\n",argv[0]);
- exit(1);
- }
- printf("OPEN FILE NAME:%s\n",argv[1]);
- if((fpi=fopen(argv[1],"rb"))==NULL)
- {
- fprintf(stderr,"Can not open\n");
- exit(1);
- }
- face=atoi(argv[2]);
- vertex=atoi(argv[3]);
- point=atoi(argv[4]);
- vertexAry=(GLfloat *)malloc(face*vertex*point*sizeof(GLfloat));
- fread((GLfloat *)vertexAry,sizeof(GLfloat),face*vertex*point,fpi);
- //GLUTの初期設定
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
- glutInitWindowSize(640,480);
- glutCreateWindow("VBO Sample");
- //GLEWの初期設定
- GLenum err = glewInit();
- if(err != GLEW_OK)
- {
- fprintf(stderr,"Err:%s\n",
- glewGetErrorString(err));
- return -1;
- }
- //拡張チェック
- if(!glewIsExtensionSupported(
- "GL_ARB_vertex_buffer_object")){
- puts("you Can't use VBO");
- return -1;
- }
- //コールバック
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutIdleFunc(idle);
- otherInit();
- buildVBO(face,vertex,point);//VBOの作成
- calculateFPS();
- //drawFPS();
- //fps=glutGet(GLUT_ELAPSED_TIME);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement