Clean up camera code.
This commit is contained in:
@@ -1,21 +1,20 @@
|
|||||||
#include <GL/glut.h>
|
#include <GL/glut.h>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <sstream>
|
||||||
#include "camera.hpp"
|
#include "camera.hpp"
|
||||||
|
|
||||||
using namespace fmri;
|
using namespace fmri;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
static float yaw, pitch;
|
static Camera& camera = Camera::instance();
|
||||||
|
|
||||||
static float pos[3];
|
|
||||||
|
|
||||||
static void handleMouseMove(int x, int y)
|
static void handleMouseMove(int x, int y)
|
||||||
{
|
{
|
||||||
const float width = glutGet(GLUT_WINDOW_WIDTH) / 2;
|
const float width = glutGet(GLUT_WINDOW_WIDTH) / 2;
|
||||||
const float height = glutGet(GLUT_WINDOW_HEIGHT) / 2;
|
const float height = glutGet(GLUT_WINDOW_HEIGHT) / 2;
|
||||||
|
|
||||||
yaw = (x - width) / width * 180;
|
camera.angle[0] = (x - width) / width * 180;
|
||||||
pitch = (y - height) / height * 90;
|
camera.angle[1] = (y - height) / height * 90;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void move(unsigned char key)
|
static void move(unsigned char key)
|
||||||
@@ -38,7 +37,7 @@ static void move(unsigned char key)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < 3; ++i) {
|
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();
|
stringstream buffer;
|
||||||
glutPassiveMotionFunc(handleMouseMove);
|
buffer << "Pos(x,y,z) = (" << pos[0] << ", " << pos[1] << ", " << pos[2] << ")\n";
|
||||||
glutKeyboardFunc(handleKeys);
|
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[0] = 0;
|
||||||
pos[1] = 0;
|
pos[1] = 0;
|
||||||
pos[2] = 10;
|
pos[2] = 10;
|
||||||
|
|
||||||
|
angle[0] = 0;
|
||||||
|
angle[1] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fmri::configureCamera()
|
void Camera::configureRenderingContext()
|
||||||
{
|
{
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glRotatef(yaw, 0, 1, 0);
|
glRotatef(angle[0], 0, 1, 0);
|
||||||
glRotatef(pitch, 1, 0, 0);
|
glRotatef(angle[1], 1, 0, 0);
|
||||||
glTranslatef(-pos[0], -pos[1], -pos[2]);
|
glTranslatef(-pos[0], -pos[1], -pos[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Camera &Camera::instance()
|
||||||
|
{
|
||||||
|
static Camera camera;
|
||||||
|
return camera;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::registerControls()
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
glutPassiveMotionFunc(handleMouseMove);
|
||||||
|
glutKeyboardFunc(handleKeys);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace fmri
|
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;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/main.cpp
10
src/main.cpp
@@ -48,7 +48,9 @@ static void render()
|
|||||||
// Clear Color and Depth Buffers
|
// Clear Color and Depth Buffers
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
configureCamera();
|
auto& camera = Camera::instance();
|
||||||
|
|
||||||
|
camera.configureRenderingContext();
|
||||||
|
|
||||||
glBegin(GL_TRIANGLES);
|
glBegin(GL_TRIANGLES);
|
||||||
glVertex3f(-2.0f, -2.0f, 0.0f);
|
glVertex3f(-2.0f, -2.0f, 0.0f);
|
||||||
@@ -56,6 +58,9 @@ static void render()
|
|||||||
glVertex3f(0.0f, 2.0f, 0.0);
|
glVertex3f(0.0f, 2.0f, 0.0);
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
|
// Draw the status line on screen
|
||||||
|
glRasterPos2i(0, 0);
|
||||||
|
|
||||||
glutSwapBuffers();
|
glutSwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +114,8 @@ int main(int argc, char *argv[])
|
|||||||
glutDisplayFunc(render);
|
glutDisplayFunc(render);
|
||||||
glutIdleFunc(render);
|
glutIdleFunc(render);
|
||||||
glutReshapeFunc(changeWindowSize);
|
glutReshapeFunc(changeWindowSize);
|
||||||
registerCameraControls();
|
|
||||||
|
Camera::instance().registerControls();
|
||||||
|
|
||||||
glewInit();
|
glewInit();
|
||||||
if (!GLEW_VERSION_2_0) {
|
if (!GLEW_VERSION_2_0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user