Add debug info in the top left corner.

This commit is contained in:
2018-02-26 15:39:53 +01:00
parent 604d302627
commit 2dec364b1b
4 changed files with 72 additions and 4 deletions

View File

@@ -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();
}

View File

@@ -83,14 +83,19 @@ 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) {
if (c == '\n') {
y += 12;
glRasterPos2i(x, y);
} else {
glutBitmapCharacter(font, c);
}
}
}
void fmri::checkGLErrors()
{
@@ -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);
}

View File

@@ -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();
}

View File

@@ -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();
}