From f0937f253d8aa5b4158b8aaad4d2a19290b1c6aa Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 20 Oct 2017 15:42:31 +0200 Subject: [PATCH] Implement rudimentary camera movement controls. --- src/glutils.cpp | 2 +- src/glutils.hpp | 2 +- src/main.cpp | 75 ++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/glutils.cpp b/src/glutils.cpp index 02d0b65..1ee54bd 100644 --- a/src/glutils.cpp +++ b/src/glutils.cpp @@ -30,7 +30,7 @@ static void show_info_log( LOG(INFO) << log.get() << endl; } -GLuint fmri::loadTexture(DType const *data, unsigned int width, unsigned int height) +GLuint fmri::loadTexture(DType const *data, int width, int height) { // Load and scale texture vector textureBuffer(data, data + (width * height)); diff --git a/src/glutils.hpp b/src/glutils.hpp index 6ee8b05..2b947b1 100644 --- a/src/glutils.hpp +++ b/src/glutils.hpp @@ -13,7 +13,7 @@ namespace fmri { * @param height * @return A texture reference. */ - GLuint loadTexture(DType const * data, unsigned int width, unsigned int height); + GLuint loadTexture(DType const * data, int width, int height); /** * diff --git a/src/main.cpp b/src/main.cpp index 8a7aada..dc5f6d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include "LayerData.hpp" #include "Options.hpp" @@ -14,27 +13,36 @@ using namespace std; using namespace fmri; -struct { +struct +{ optional> labels; vector> data; float angle = 0; map, GLuint> textureMap; + + struct + { + float pitch = 0; + float yaw = 0; + + float x = 0, y = 0, z = 0; + } camera; } rendererData; -static vector> getSimulationData(const Options& options) +static vector> getSimulationData(const Options &options) { vector> results; auto dumper = options.imageDumper(); Simulator simulator(options.model(), options.weights(), options.means()); - for (const auto& image : options.inputs()) { + for (const auto &image : options.inputs()) { results.emplace_back(simulator.simulate(image)); } CHECK_GT(results.size(), 0) << "Should have some results" << endl; if (dumper) { - for (auto& layer : *results.begin()) { + for (auto &layer : *results.begin()) { dumper->dump(layer); } } @@ -51,19 +59,19 @@ static void render() // Reset transformations glLoadIdentity(); // Set the camera - gluLookAt( 0.0f, 0.0f, 10.0f, - 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f); + gluLookAt(rendererData.camera.x, rendererData.camera.y, rendererData.camera.z - 10, + rendererData.camera.x, rendererData.camera.y, rendererData.camera.z, + 0.0f, 1.0f, 0.0f); glRotatef(rendererData.angle, 0.0f, 1.0f, 0.0f); glBegin(GL_TRIANGLES); - glVertex3f(-2.0f,-2.0f, 0.0f); - glVertex3f( 2.0f, 0.0f, 0.0); - glVertex3f( 0.0f, 2.0f, 0.0); + glVertex3f(-2.0f, -2.0f, 0.0f); + glVertex3f(2.0f, 0.0f, 0.0); + glVertex3f(0.0f, 2.0f, 0.0); glEnd(); - rendererData.angle+=0.1f; + rendererData.angle += 0.1f; glutSwapBuffers(); } @@ -71,15 +79,15 @@ static void render() static void reloadTextures(unsigned dataIndex) { // First, release any existing textures - for (auto& entry : rendererData.textureMap) { + for (auto &entry : rendererData.textureMap) { glDeleteTextures(0, &entry.second); } rendererData.textureMap.clear(); - const auto& dataSet = rendererData.data[dataIndex]; + const auto &dataSet = rendererData.data[dataIndex]; - for (auto& layer : dataSet) { + for (auto &layer : dataSet) { auto dimensions = layer.shape(); if (dimensions.size() != 4) { continue; @@ -100,9 +108,41 @@ 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; + } +} -int main(int argc, char * argv[]) { +int main(int argc, char *argv[]) +{ google::InitGoogleLogging(argv[0]); google::InstallFailureSignalHandler(); @@ -112,13 +152,14 @@ int main(int argc, char * argv[]) { // Prepare data for simulations Options options = Options::parse(argc, argv); - rendererData.labels = options.labels(); + rendererData.labels = options.labels(); rendererData.data = getSimulationData(options); // Register callbacks glutDisplayFunc(render); glutIdleFunc(render); glutReshapeFunc(changeWindowSize); + glutKeyboardFunc(handleKeys); glewInit(); if (!GLEW_VERSION_2_0) {