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>
- GLfloat *vertexAry;
- int tx,ty,tz;
- int txyz;
- int l;
- int sx,sy,sz;
- int sxyz;
- int m;
- int frameCount = 0;
- float fps = 0;
- int currentTime = 0, previousTime = 0;
- void calculateFPS();
- //affine変換用変数
- float pos[] = { 0.0, 0.0, 0.0 };//図形中心のxyz座標
- float scale[] = { 1.0, 1.0, 1.0 };//大きさ(倍率)
- float angle[] = { 0.0, 0.0, 0.0 };//回転角度
- //アフィン変換フラグ
- bool flagTranslate = true;
- bool flagRotate = false;
- bool flagScale = false;
- //視野角
- double fovY = 30.0;//追加
- //------- 頂点データ-----------//
- //「6」面、「4」頂点、1頂点はx,y,zの「3」要素
- /*
- GLfloat vertexAry[6][4][3] =
- {
- //1つめの面(v0-v1-v2-v3)
- {
- {0.f,0.f,0.f},{1.f,0.f,0.f},
- {1.f,1.f,0.f},{0.f,1.f,0.f}
- },
- //2つめの面(v1-v5-v6-v2)
- {
- {1.f,0.f,0.f},{1.f,0.f,-1.f},
- {1.f,1.f,-1.f},{1.f,1.f,0.f}
- },
- //3つめの面(v4-v7-v6-v5)
- {
- {0.f,0.f,-1.f},{0.f,1.f,-1.f},
- {1.f,1.f,-1.f},{1.f,0.f,-1.f}
- },
- //4つめの面(v4-v0-v3-v7)
- {
- {0.f,0.f,-1.f},{0.f,0.f,0.f},
- {0.f,1.f,0.f},{0.f,1.f,-1.f}
- },
- //5つめの面(v3-v2-v6-v7)
- {
- {0.f,1.f,0.f},{1.f,1.f,0.f},
- {1.f,1.f,-1.f},{0.f,1.f,-1.f}
- },
- //6つめの面(v0-v4-v5-v1)
- {
- {0.f,0.f,0.f},{0.f,0.f,-1.f},
- {1.f,0.f,-1.f},{1.f,0.f,0.f}
- }
- };
- */
- //法線データ
- GLfloat normalAry[6][4][3] =
- {
- //1つめの面(v0-v1-v2-v3)
- {
- {0.f,0.f,1.f},{0.f,0.f,1.f},
- {0.f,0.f,1.f},{0.f,0.f,1.f}
- },
- //2つめの面(v1-v5-v6-v2)
- {
- {1.f,0.f,0.f},{1.f,0.f,0.f},
- {1.f,0.f,0.f},{1.f,0.f,0.f}
- },
- //3つめの面(v4-v7-v6-v5)
- {
- {0.f,0.f,-1.f},{0.f,0.f,-1.f},
- {0.f,0.f,-1.f},{0.f,0.f,-1.f}
- },
- //4つめの面(v4-v0-v3-v7)
- {
- {-1.f,0.f,0.f},{-1.f,0.f,0.f},
- {-1.f,0.f,0.f},{-1.f,0.f,0.f}
- },
- //5つめの面(v3-v2-v6-v7)
- {
- {0.f,1.f,0.f},{0.f,1.f,0.f},
- {0.f,1.f,0.f},{0.f,1.f,0.f}
- },
- //6つめの面(v0-v4-v5-v1)
- {
- {0.f,-1.f,0.f},{0.f,-1.f,0.f},
- {0.f,-1.f,0.f},{0.f,-1.f,0.f}
- }
- };
- //色データ
- GLfloat colorAry[6][4][3] =
- {
- //1つめの面(v0-v1-v2-v3)
- {
- {0.f,0.f,1.f},{0.f,0.f,1.f},
- {0.f,0.f,1.f},{0.f,0.f,1.f}
- },
- //2つめの面(v1-v5-v6-v2)
- {
- {1.f,0.f,0.f},{1.f,0.f,0.f},
- {1.f,0.f,0.f},{1.f,0.f,0.f}
- },
- //3つめの面(v4-v7-v6-v5)
- {
- {0.f,1.f,1.f},{0.f,1.f,1.f},
- {0.f,1.f,1.f},{0.f,1.f,1.f}
- },
- //4つめの面(v4-v0-v3-v7)
- {
- {1.f,1.f,0.f},{1.f,1.f,0.f},
- {1.f,1.f,0.f},{1.f,1.f,0.f}
- },
- //5つめの面(v3-v2-v6-v7)
- {
- {0.f,1.f,0.f},{0.f,1.f,0.f},
- {0.f,1.f,0.f},{0.f,1.f,0.f}
- },
- //6つめの面(v0-v4-v5-v1)
- {
- {1.f,0.f,1.f},{1.f,0.f,1.f},
- {1.f,0.f,1.f},{1.f,0.f,1.f}
- }
- };
- //インデックス
- GLubyte indexAry[]=
- {
- 0,1,2,3,
- 4,5,6,7,
- 8,9,10,11,
- 12,13,14,15,
- 16,17,18,19,
- 20,21,22,23
- };
- // π/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,3,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);//対応付けの解放
- }
- }
- //glTranslated
- GLint txyz[13][3]=
- {
- {0,0,0},
- {10,0,0},
- {0,0,0},
- {-10,0,0},
- {0,0,0},
- {0,10,0},
- {0,0,0},
- {0,-10,0},
- {0,0,0},
- {0,0,10},
- {0,0,0},
- {0,0,-10},
- {0,0,0}
- };
- //Scale
- for(l=0;l<13;l++)
- {
- tx=txyz[l][0]; //move x
- ty=txyz[l][1]; //move y
- tz=txyz[l][2]; //move z
- }
- GLint sxyz[9][3]=
- {
- {1,1,1},
- {2,2,2},
- {3,3,3},
- {4,4,4},
- {5,5,5},
- {4,4,4},
- {3,3,3},
- {2,2,2},
- {1,1,1},
- };
- for(m=0;m<9;m++)
- {
- sx=sxyz[m][0];
- sy=sxyz[m][1];
- sz=sxyz[m][2];
- }
- //クライアント側に戻す
- 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(0.30, 0.40, 0.50, 0.0, 0.0, 0.0, 0.0, 0.10, 0.0);
- glPushMatrix();
- glTranslated(tx,ty,tz);
- glRotatef(float(angle),0.f,0.f,1.f);//z軸回転
- glRotatef(float(angle),0.f,1.f,0.f);//y軸回転
- glRotatef(float(angle),1.f,0.f,0.f);//x軸回転
- glScaled(sx, sy, sz);
- //drawSphere(0.7,10,10);
- drawAry();
- glPopMatrix();
- calculateFPS();
- 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 reshape(int w, int h)
- {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(fovY, (double)w/(double)h, 0.1, 100.0);
- glMatrixMode(GL_MODELVIEW);
- }
- */
- 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);//データ変更なし
- }
- //-------------------------------------------------------------------------
- // 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);
- }
- //----------- メイン関数------------//
- int main(int argc, char *argv[])
- {
- FILE *fpi;
- int face,vertex,point;
- int i,j,k;
- float data;
- tx=atoi(argv[5]);
- ty=atoi(argv[6]);
- tz=atoi(argv[7]);
- sx=atoi(argv[8]);
- sy=atoi(argv[9]);
- sz=atoi(argv[10]);
- if(argc!=11)
- {
- 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);
- //glutKeyboardFunc(keyboard);
- //glutSpecialFunc(special);
- otherInit();
- buildVBO(face,vertex,point);//VBOの作成
- calculateFPS();
- glutMainLoop();
- fclose(fpi);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement