Clean up camera code.

This commit is contained in:
2017-10-27 12:05:53 +02:00
parent 33af59b8a5
commit b781960632
3 changed files with 52 additions and 22 deletions

View File

@@ -1,21 +1,20 @@
#include <GL/glut.h>
#include <cmath>
#include <sstream>
#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);
}

View File

@@ -1,10 +1,21 @@
#pragma once
#include <string>
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;
};
}

View File

@@ -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) {