camera is now an interface

This commit is contained in:
Anselme 2015-08-02 23:40:06 +02:00
parent 192413642c
commit b51c490eaa
7 changed files with 16 additions and 171 deletions

View File

@ -7,7 +7,6 @@
class Shader;
class PhongEntity;
class Camera;
class BasicModule : public Module
{

View File

@ -1,126 +0,0 @@
#include "camera.h"
#include <glm/ext.hpp>
#define M_PI 3.14159265358979323846
Camera::Camera(int width, int height, float fov_y, float near, float far, glm::vec3 pos) :
m_projectionHasChanged(true),
m_viewHasChanged(true),
m_fov(fov_y),
m_width(width),
m_height(height),
m_near(near),
m_far(far),
m_position(pos),
m_rotation(glm::vec2())
{
computeProjectionMatrix();
computeViewMatrix();
}
void Camera::computeProjectionMatrix()
{
m_projectionMatrix = glm::perspectiveFov(m_fov, (float)m_width, (float)m_height, m_near, m_far);
m_projectionHasChanged = false;
}
void Camera::computeViewMatrix()
{
m_viewMatrix = glm::rotate(glm::mat4(), m_rotation.y, glm::vec3(1, 0, 0));
m_viewMatrix = glm::rotate(m_viewMatrix, m_rotation.x, glm::vec3(0, 1, 0));
m_viewMatrix = glm::translate(m_viewMatrix, -m_position);
m_viewHasChanged = false;
}
void Camera::translate(glm::vec3 vector)
{
m_viewHasChanged = true;
m_position += vector;
}
void Camera::relativeTranslate(glm::vec3 vector)
{
m_viewHasChanged = true;
m_position += vector * glm::mat3(m_viewMatrix);
}
void Camera::rotate(glm::vec2 rad_vector)
{
m_viewHasChanged = true;
m_rotation += rad_vector;
if(m_rotation.y > M_PI/2)
m_rotation.y = M_PI/2;
if(m_rotation.y < -M_PI/2)
m_rotation.y = -M_PI/2;
}
void Camera::zoom(float new_fov_y)
{
m_projectionHasChanged = true;
m_fov = new_fov_y;
}
void Camera::resize(int width, int height)
{
m_projectionHasChanged = true;
m_width = width;
m_height = height;
}
void Camera::setNear(float near)
{
m_projectionHasChanged = true;
m_near = near;
}
void Camera::setFar(float far)
{
m_projectionHasChanged = true;
m_far = far;
}
void Camera::moveTo(glm::vec3 position)
{
m_viewHasChanged = true;
m_position = position;
}
void Camera::lookAt(glm::vec3 position)
{
// TODO : fix this method
m_viewHasChanged = true;
glm::vec3 delta = position - m_position;
glm::normalize(delta);
m_rotation.y = std::asin(delta.y);
m_rotation.x = std::atan2(delta.z, delta.x);
}
void Camera::lookAt(glm::vec2 rotation)
{
m_viewHasChanged = true;
m_rotation = rotation;
}
glm::mat4 Camera::getProjectionMatrix()
{
if(m_projectionHasChanged)
computeProjectionMatrix();
return m_projectionMatrix;
}
glm::mat4 Camera::getViewMatrix()
{
if(m_viewHasChanged)
computeViewMatrix();
return m_viewMatrix;
}
glm::vec2 Camera::getRotation()
{
return m_rotation;
}
glm::vec3 Camera::getPosition()
{
return m_position;
}

View File

@ -5,46 +5,12 @@
#include <glm/vec3.hpp>
#include <glm/vec2.hpp>
class Camera
{
bool m_projectionHasChanged;
bool m_viewHasChanged;
glm::mat4 m_projectionMatrix;
glm::mat4 m_viewMatrix;
float m_fov;
int m_width;
int m_height;
float m_near;
float m_far;
glm::vec3 m_position;
glm::vec2 m_rotation;
void computeProjectionMatrix();
void computeViewMatrix();
class Camera{
public:
//constructor
Camera(int width = 800, int height = 600, float fov_y = 45, float near = 0.1f, float far = 10.0f, glm::vec3 pos = glm::vec3());
virtual glm::mat4 getProjectionMatrix() = 0;
virtual glm::mat4 getViewMatrix() = 0;
// setters
// relative:
void translate(glm::vec3 vector);
void relativeTranslate(glm::vec3 vector);
void rotate(glm::vec2 rad_vector);
// absolute:
void zoom(float new_fov_y);
void resize(int width, int height);
void setNear(float near);
void setFar(float far);
void moveTo(glm::vec3 position);
void lookAt(glm::vec3 position);
void lookAt(glm::vec2 rotation);
// getters
glm::mat4 getProjectionMatrix();
glm::mat4 getViewMatrix();
glm::vec2 getRotation();
glm::vec3 getPosition();
virtual void resize(int width, int height) = 0;
};
#endif // CAMERA_H

View File

@ -5,6 +5,7 @@
void PhongMaterial::bindAttributes()
{
shader->bind();
shader->bindVec3(shader->getLocation("materialKd"), kd);
shader->bindVec3(shader->getLocation("materialKs"), ks);
shader->bindFloat(shader->getLocation("materialNs"), ns);

View File

@ -24,7 +24,6 @@ unix {
}
SOURCES += shader.cpp \
camera.cpp \
gridmesh.cpp \
texture.cpp \
phongmaterial.cpp \

View File

@ -41,7 +41,7 @@ void SparrowRenderer::destroyGL()
void SparrowRenderer::resizeGL(int width, int height)
{
glAssert(glViewport(0, 0, width, height));
camera.resize(width, height);
camera->resize(width, height);
}
void SparrowRenderer::renderGL()
@ -69,8 +69,13 @@ int SparrowRenderer::getNbModules()
// camera methods
Camera* SparrowRenderer::getCamera()
void SparrowRenderer::setCamera(Camera* myCamera)
{
return &camera;
camera = myCamera;
}
Camera* SparrowRenderer::getCamera()
{
return camera;
}

View File

@ -3,8 +3,8 @@
#include <list>
#include <string>
#include "camera.h"
class Camera;
class Module;
class SparrowRenderer
@ -21,6 +21,7 @@ public:
int getNbModules();
// camera methods
void setCamera(Camera* myCamera);
Camera* getCamera();
protected:
@ -32,7 +33,7 @@ protected:
s_moduleNode(Module* myModule, const std::string &myName) : module(myModule), name(myName), isEnabled(true) {}
} ModuleNode;
Camera camera;
Camera* camera;
std::list<ModuleNode> modules;
std::list<ModuleNode>::iterator getModuleNode(std::string name);