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>
- //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 the Idle 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[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_QUADS,24,GL_UNSIGNED_BYTE,0);
- 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 < 24*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, 1.0, 0.0);
- glPushMatrix();
- //glRotatef(float(angle),1.f,0.f,0.f);//x軸回転
- drawAry();
- glPopMatrix();
- 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 keyboard(unsigned char key, int x, int y)
- {
- switch(key)
- {
- case 'q':
- case 'Q':
- case '\033':
- //終了処理
- glDeleteBuffers(3,&VboId[0]);
- glDeleteBuffers(1,&VboIndex);
- exit(0);
- break;
- }
- }
- //------ その他初期設定-------//
- 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(void)
- {
- glGenBuffers(3,&VboId[0]);//座標、法線、色の3つ
- //頂点
- glBindBuffer(GL_ARRAY_BUFFER,VboId[0]);
- glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*72,
- vertexAry,GL_DYNAMIC_DRAW);//データ変更する
- //法線
- glBindBuffer(GL_ARRAY_BUFFER,VboId[1]);
- glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*72,
- normalAry,GL_DYNAMIC_DRAW);//データ変更あり
- //色
- glBindBuffer(GL_ARRAY_BUFFER,VboId[2]);
- glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*72,
- colorAry,GL_STREAM_DRAW);//データ変更なし
- //インデックス
- glGenBuffers(1,&VboIndex);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,VboIndex);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(GLubyte)*24,
- 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[])
- {
- //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);
- otherInit();
- buildVBO();//VBOの作成
- calculateFPS();
- //drawFPS();
- //fps=glutGet(GLUT_ELAPSED_TIME);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement