From b781960632024df2a7cefc5718a366ef75d9b60d Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 27 Oct 2017 12:05:53 +0200 Subject: [PATCH] Clean up camera code. --- src/camera.cpp | 47 ++++++++++++++++++++++++++++++----------------- src/camera.hpp | 17 ++++++++++++++--- src/main.cpp | 10 ++++++++-- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/src/camera.cpp b/src/camera.cpp index 132d25c..1f0377a 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -1,21 +1,20 @@ #include #include +#include #include "camera.hpp" using namespace fmri; using namespace std; -static float yaw, pitch; - -static float pos[3]; +static Camera& camera = Camera::instance(); static void handleMouseMove(int x, int y) { const float width = glutGet(GLUT_WINDOW_WIDTH) / 2; const float height = glutGet(GLUT_WINDOW_HEIGHT) / 2; - yaw = (x - width) / width * 180; - pitch = (y - height) / height * 90; + camera.angle[0] = (x - width) / width * 180; + camera.angle[1] = (y - height) / height * 90; } static void move(unsigned char key) @@ -38,7 +37,7 @@ static void move(unsigned char key) } for (unsigned int i = 0; i < 3; ++i) { - pos[i] += speed * dir[i]; + camera.pos[i] += speed * dir[i]; } } @@ -62,27 +61,41 @@ static void handleKeys(unsigned char key, int, int) } } -void fmri::registerCameraControls() +std::string Camera::infoLine() { - resetCamera(); - glutPassiveMotionFunc(handleMouseMove); - glutKeyboardFunc(handleKeys); + stringstream buffer; + buffer << "Pos(x,y,z) = (" << pos[0] << ", " << pos[1] << ", " << pos[2] << ")\n"; + buffer << "Angle(p,y) = (" << angle[0] << ", " << angle[1] << ")\n"; + return buffer.str(); } -void fmri::resetCamera() +void Camera::reset() { - pitch = 0; - yaw = 0; - pos[0] = 0; pos[1] = 0; pos[2] = 10; + + angle[0] = 0; + angle[1] = 0; } -void fmri::configureCamera() +void Camera::configureRenderingContext() { glLoadIdentity(); - glRotatef(yaw, 0, 1, 0); - glRotatef(pitch, 1, 0, 0); + glRotatef(angle[0], 0, 1, 0); + glRotatef(angle[1], 1, 0, 0); glTranslatef(-pos[0], -pos[1], -pos[2]); } + +Camera &Camera::instance() +{ + static Camera camera; + return camera; +} + +void Camera::registerControls() +{ + reset(); + glutPassiveMotionFunc(handleMouseMove); + glutKeyboardFunc(handleKeys); +} diff --git a/src/camera.hpp b/src/camera.hpp index b2e553e..936f27d 100644 --- a/src/camera.hpp +++ b/src/camera.hpp @@ -1,10 +1,21 @@ #pragma once +#include + namespace fmri { - void registerCameraControls(); + struct Camera { + float pos[3]; + float angle[2]; - void configureCamera(); + void reset(); + void configureRenderingContext(); + void registerControls(); + std::string infoLine(); - void resetCamera(); + static Camera& instance(); + + private: + Camera() = default; + }; } diff --git a/src/main.cpp b/src/main.cpp index 766e650..1f6cdac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,7 +48,9 @@ static void render() // Clear Color and Depth Buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - configureCamera(); + auto& camera = Camera::instance(); + + camera.configureRenderingContext(); glBegin(GL_TRIANGLES); glVertex3f(-2.0f, -2.0f, 0.0f); @@ -56,6 +58,9 @@ static void render() glVertex3f(0.0f, 2.0f, 0.0); glEnd(); + // Draw the status line on screen + glRasterPos2i(0, 0); + glutSwapBuffers(); } @@ -109,7 +114,8 @@ int main(int argc, char *argv[]) glutDisplayFunc(render); glutIdleFunc(render); glutReshapeFunc(changeWindowSize); - registerCameraControls(); + + Camera::instance().registerControls(); glewInit(); if (!GLEW_VERSION_2_0) {