diff --git a/src/camera.cpp b/src/camera.cpp index 513bf08..0254857 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -21,6 +21,24 @@ static void handleMouseMove(int x, int y) glutPostRedisplay(); } +static float getFPS() +{ + static int frames = 0; + static float fps = 0; + static auto timeBase = glutGet(GLUT_ELAPSED_TIME); + + ++frames; + const auto time = glutGet(GLUT_ELAPSED_TIME); + if (time - timeBase > 2000) { + fps = frames * 1000.0f / (time - timeBase); + frames = 0; + timeBase = time; + } + + + return fps; +} + static void move(unsigned char key) { float speed = 0.5; @@ -80,6 +98,7 @@ std::string Camera::infoLine() stringstream buffer; buffer << "Pos(x,y,z) = (" << pos[0] << ", " << pos[1] << ", " << pos[2] << ")\n"; buffer << "Angle(p,y) = (" << angle[0] << ", " << angle[1] << ")\n"; + buffer << "FPS = " << getFPS() << "\n"; return buffer.str(); } diff --git a/src/glutils.cpp b/src/glutils.cpp index 8f4a931..7685a9f 100644 --- a/src/glutils.cpp +++ b/src/glutils.cpp @@ -83,12 +83,17 @@ void fmri::changeWindowSize(int w, int h) glMatrixMode(GL_MODELVIEW); } -void fmri::renderText(std::string_view text) +void fmri::renderText(std::string_view text, int x, int y) { constexpr auto font = GLUT_BITMAP_HELVETICA_10; - glRasterPos2i(0, 0); + glRasterPos2i(x, y); for (char c : text) { - glutBitmapCharacter(font, c); + if (c == '\n') { + y += 12; + glRasterPos2i(x, y); + } else { + glutBitmapCharacter(font, c); + } } } @@ -117,3 +122,32 @@ void fmri::throttleIdleFunc() lastCalled = now; } + +void fmri::restorePerspectiveProjection() { + + glMatrixMode(GL_PROJECTION); + // restore previous projection matrix + glPopMatrix(); + + // get back to modelview mode + glMatrixMode(GL_MODELVIEW); +} + +void fmri::setOrthographicProjection() { + + // switch to projection mode + glMatrixMode(GL_PROJECTION); + + // save previous matrix which contains the + //settings for the perspective projection + glPushMatrix(); + + // reset matrix + glLoadIdentity(); + + // set a 2D orthographic projection + gluOrtho2D(0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT), 0); + + // switch back to modelview mode + glMatrixMode(GL_MODELVIEW); +} diff --git a/src/glutils.hpp b/src/glutils.hpp index 739f74e..bdfbb53 100644 --- a/src/glutils.hpp +++ b/src/glutils.hpp @@ -34,7 +34,7 @@ namespace fmri { * * @param text The text to draw. */ - void renderText(std::string_view text); + void renderText(std::string_view text, int x = 0, int y = 0); /** * Check if there are OpenGL errors and report them. @@ -45,4 +45,8 @@ namespace fmri { * Slow down until the idle func is being called a reasonable amount of times. */ void throttleIdleFunc(); + + void setOrthographicProjection(); + + void restorePerspectiveProjection(); } diff --git a/src/main.cpp b/src/main.cpp index 0d2fc93..2e54067 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,6 +63,15 @@ static void loadSimulationData(const Options &options) static void renderLayerName(const LayerData &data); +static void renderDebugInfo() +{ + glLoadIdentity(); + setOrthographicProjection(); + glColor3f(1, 1, 0); + renderText(Camera::instance().infoLine(), 2, 10); + restorePerspectiveProjection(); +} + static void render() { // Clear Color and Depth Buffers @@ -92,6 +101,8 @@ static void render() glPopMatrix(); + renderDebugInfo(); + glutSwapBuffers(); }