Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <stdio.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();
- #if defined(WIN32)
- //# pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
- # include "glut.h"
- # include "glext.h"
- PFNGLGENBUFFERSPROC glGenBuffers;
- PFNGLISBUFFERPROC glIsBuffer;
- PFNGLBINDBUFFERPROC glBindBuffer;
- PFNGLBUFFERDATAPROC glBufferData;
- PFNGLBUFFERSUBDATAPROC glBufferSubData;
- PFNGLMAPBUFFERPROC glMapBuffer;
- PFNGLUNMAPBUFFERPROC glUnmapBuffer;
- PFNGLDELETEBUFFERSPROC glDeleteBuffers;
- #elif defined(__APPLE__) || defined(MACOSX)
- # include <GLUT/glut.h>
- #else
- # include <GL/glut.h>
- #endif
- /*
- ** トラックボール処理用関数の宣言
- */
- #include "trackball.h"
- /*
- ** 形状データ
- */
- #include "Obj.h"
- Obj *data;
- /*
- ** 視点の距離
- */
- static double offset = -200.0;
- /*
- ** 光源
- */
- static const GLfloat lightpos[] = { 0.0, 0.0, 1.0, 0.0 }; /* 位置 */
- /*
- ** 初期化
- */
- static void init(void)
- {
- #if defined(WIN32)
- glGenBuffers =
- (PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffers");
- glIsBuffer =
- (PFNGLISBUFFERPROC)wglGetProcAddress("glIsBuffer");
- glBindBuffer =
- (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
- glBufferData =
- (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
- glBufferSubData =
- (PFNGLBUFFERSUBDATAPROC)wglGetProcAddress("glBufferSubData");
- glMapBuffer =
- (PFNGLMAPBUFFERPROC)wglGetProcAddress("glMapBuffer");
- glUnmapBuffer =
- (PFNGLUNMAPBUFFERPROC)wglGetProcAddress("glUnmapBuffer");
- glDeleteBuffers =
- (PFNGLDELETEBUFFERSPROC)wglGetProcAddress("glDeleteBuffers");
- #endif
- /* 初期設定 */
- glClearColor(0.3, 0.3, 1.0, 1.0);
- glEnable(GL_DEPTH_TEST);
- glDisable(GL_CULL_FACE);
- /* 光源の初期設定 */
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- /* 形状データオブジェクトの作成 */
- data = new Obj("bunny.obj");
- }
- /*
- ** シーンの描画
- */
- void scene(void)
- {
- data->draw();
- }
- /****************************
- ** GLUT のコールバック関数 **
- ****************************/
- static void display(void)
- {
- /* モデルビュー変換行列の設定 */
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- /* 視点の移動(物体の方を奥に移動)*/
- glTranslated(0.0, 0.0, offset);
- /* 光源の位置を設定 */
- glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
- /* トラックボール処理による回転 */
- glMultMatrixd(trackballRotation());
- /* 画面クリア */
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- /* シーンの描画 */
- scene();
- /* ダブルバッファリング */
- glutSwapBuffers();
- }
- static void resize(int w, int h)
- {
- /* トラックボールする範囲 */
- trackballRegion(w, h);
- /* ウィンドウ全体をビューポートにする */
- glViewport(0, 0, w, h);
- /* 透視変換行列の指定 */
- glMatrixMode(GL_PROJECTION);
- /* 透視変換行列の初期化 */
- glLoadIdentity();
- gluPerspective(60.0, (double)w / (double)h, 1.0, 500.0);
- }
- static void idle(void)
- {
- /* 画面の描き替え */
- glutPostRedisplay();
- }
- static void mouse(int button, int state, int x, int y)
- {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- switch (state) {
- case GLUT_DOWN:
- /* トラックボール開始 */
- trackballStart(x, y);
- glutIdleFunc(idle);
- break;
- case GLUT_UP:
- /* トラックボール停止 */
- glutIdleFunc(0);
- trackballStop(x, y);
- break;
- default:
- break;
- }
- break;
- default:
- break;
- }
- }
- static void motion(int x, int y)
- {
- /* トラックボール移動 */
- trackballMotion(x, y);
- }
- static void keyboard(unsigned char key, int x, int y)
- {
- switch (key) {
- case '+':
- offset += 10.0;
- glutPostRedisplay();
- break;
- case '-':
- offset -= 10.0;
- glutPostRedisplay();
- break;
- case 'q':
- case 'Q':
- case '\033':
- /* ESC か q か Q をタイプしたら終了 */
- exit(0);
- default:
- break;
- }
- }
- //-------------------------------------------------------------------------
- // 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;
- }
- }
- //-------------------------------------------------------------------------
- // 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[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
- glutCreateWindow(argv[0]);
- glutDisplayFunc(display);
- glutReshapeFunc(resize);
- glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutKeyboardFunc(keyboard);
- init();
- calculateFPS();
- drawFPS();
- fps=glutGet(GLUT_ELAPSED_TIME);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement