diff --git a/src/camera.cpp b/src/camera.cpp new file mode 100644 index 0000000..d58c79e --- /dev/null +++ b/src/camera.cpp @@ -0,0 +1,74 @@ +#include +#include "camera.hpp" + +static float yaw, pitch; + +static struct { + float x, y, z; +} camera; + +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; +} + +static void handleKeys(unsigned char key, int, int) +{ + constexpr float rotationScaling = 0.2f; + constexpr float movementScaling = 0.2f; + switch (key) { + case 'a': + // TODO: handle rotations + camera.x += movementScaling; + break; + + case 'd': + camera.x -= rotationScaling; + break; + + case 'w': + camera.z += movementScaling; + break; + + case 's': + camera.z -= movementScaling; + break; + + case 'q': + // Utility quit function. + exit(0); + + default: + // Do nothing. + break; + } +} + +void fmri::registerCameraControls() +{ + resetCamera(); + glutPassiveMotionFunc(handleMouseMove); + glutKeyboardFunc(handleKeys); +} + +void fmri::resetCamera() +{ + camera.x = 0; + camera.y = 0; + camera.z = -10; + + pitch = 0; + yaw = 0; +} + +void fmri::configureCamera() +{ + glLoadIdentity(); + glRotatef(yaw, 0, 1, 0); + glRotatef(pitch, 1, 0, 0); + glTranslatef(camera.x, camera.y, camera.z); +} diff --git a/src/camera.hpp b/src/camera.hpp new file mode 100644 index 0000000..b2e553e --- /dev/null +++ b/src/camera.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace fmri +{ + void registerCameraControls(); + + void configureCamera(); + + void resetCamera(); +} diff --git a/src/main.cpp b/src/main.cpp index 5c28483..5752bd1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,12 +4,12 @@ #include #include #include -#include #include "LayerData.hpp" #include "Options.hpp" #include "Simulator.hpp" #include "glutils.hpp" +#include "camera.hpp" using namespace std; using namespace fmri; @@ -20,14 +20,6 @@ struct vector> data; float angle = 0; map, GLuint> textureMap; - - struct - { - float pitch = 0; - float yaw = 0; - - float x = 0, y = 0, z = -10; - } camera; } rendererData; static vector> getSimulationData(const Options &options) @@ -51,14 +43,6 @@ static vector> getSimulationData(const Options &options) return results; } -static void configureCamera() -{ - glLoadIdentity(); - glRotatef(rendererData.camera.yaw, 0, 1, 0); - glRotatef(rendererData.camera.pitch, 1, 0, 0); - glTranslatef(rendererData.camera.x, rendererData.camera.y, rendererData.camera.z); -} - static void render() { // Clear Color and Depth Buffers @@ -111,48 +95,6 @@ static void reloadTextures(unsigned dataIndex) } } -static void handleKeys(unsigned char key, int, int) -{ - constexpr float rotationScaling = 0.2f; - constexpr float movementScaling = 0.2f; - switch (key) { - case 'a': - // TODO: handle rotations - rendererData.camera.x += movementScaling; - break; - - case 'd': - rendererData.camera.x -= rotationScaling; - break; - - case 'w': - rendererData.camera.z += movementScaling; - break; - - case 's': - rendererData.camera.z -= movementScaling; - break; - - case 'q': - // Utility quit function. - exit(0); - - default: - LOG(INFO) << "Received key: " << key << endl; - break; - } -} - -static void handleMouseMove(int x, int y) -{ - const float width = glutGet(GLUT_WINDOW_WIDTH) / 2; - const float height = glutGet(GLUT_WINDOW_HEIGHT) / 2; - - rendererData.camera.yaw = (x - width) / width * 180; - rendererData.camera.pitch = (y - height) / height * 90; -} - - int main(int argc, char *argv[]) { google::InitGoogleLogging(argv[0]); @@ -171,8 +113,7 @@ int main(int argc, char *argv[]) glutDisplayFunc(render); glutIdleFunc(render); glutReshapeFunc(changeWindowSize); - glutKeyboardFunc(handleKeys); - glutPassiveMotionFunc(handleMouseMove); + registerCameraControls(); glewInit(); if (!GLEW_VERSION_2_0) { diff --git a/src/shaders.cpp b/src/shaders.cpp deleted file mode 100644 index 2ded97d..0000000 --- a/src/shaders.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "shaders.hpp" diff --git a/src/shaders.hpp b/src/shaders.hpp deleted file mode 100644 index a34892a..0000000 --- a/src/shaders.hpp +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#include - -extern const char * GRAYSCALE_TEXTURE_SHADER;