Move camera logic to its own compilation unit.

Also remove unused shaders file.
This commit is contained in:
2017-10-25 16:30:04 +02:00
parent b459b22a97
commit f2c3708485
5 changed files with 86 additions and 67 deletions

74
src/camera.cpp Normal file
View File

@@ -0,0 +1,74 @@
#include <GL/glut.h>
#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);
}

10
src/camera.hpp Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
namespace fmri
{
void registerCameraControls();
void configureCamera();
void resetCamera();
}

View File

@@ -4,12 +4,12 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GL/glut.h> #include <GL/glut.h>
#include <map> #include <map>
#include <cmath>
#include "LayerData.hpp" #include "LayerData.hpp"
#include "Options.hpp" #include "Options.hpp"
#include "Simulator.hpp" #include "Simulator.hpp"
#include "glutils.hpp" #include "glutils.hpp"
#include "camera.hpp"
using namespace std; using namespace std;
using namespace fmri; using namespace fmri;
@@ -20,14 +20,6 @@ struct
vector<vector<LayerData>> data; vector<vector<LayerData>> data;
float angle = 0; float angle = 0;
map<tuple<string, int, int>, GLuint> textureMap; map<tuple<string, int, int>, GLuint> textureMap;
struct
{
float pitch = 0;
float yaw = 0;
float x = 0, y = 0, z = -10;
} camera;
} rendererData; } rendererData;
static vector<vector<LayerData>> getSimulationData(const Options &options) static vector<vector<LayerData>> getSimulationData(const Options &options)
@@ -51,14 +43,6 @@ static vector<vector<LayerData>> getSimulationData(const Options &options)
return results; 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() static void render()
{ {
// Clear Color and Depth Buffers // 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[]) int main(int argc, char *argv[])
{ {
google::InitGoogleLogging(argv[0]); google::InitGoogleLogging(argv[0]);
@@ -171,8 +113,7 @@ int main(int argc, char *argv[])
glutDisplayFunc(render); glutDisplayFunc(render);
glutIdleFunc(render); glutIdleFunc(render);
glutReshapeFunc(changeWindowSize); glutReshapeFunc(changeWindowSize);
glutKeyboardFunc(handleKeys); registerCameraControls();
glutPassiveMotionFunc(handleMouseMove);
glewInit(); glewInit();
if (!GLEW_VERSION_2_0) { if (!GLEW_VERSION_2_0) {

View File

@@ -1 +0,0 @@
#include "shaders.hpp"

View File

@@ -1,5 +0,0 @@
#pragma once
#include <GL/glut.h>
extern const char * GRAYSCALE_TEXTURE_SHADER;