diff --git a/basicmodule.h b/basicmodule.h index 4743821..51d7933 100644 --- a/basicmodule.h +++ b/basicmodule.h @@ -7,7 +7,6 @@ class Shader; class PhongEntity; -class Camera; class BasicModule : public Module { diff --git a/camera.cpp b/camera.cpp deleted file mode 100644 index 683e908..0000000 --- a/camera.cpp +++ /dev/null @@ -1,126 +0,0 @@ -#include "camera.h" - -#include -#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; -} diff --git a/camera.h b/camera.h index 0be22fa..c6492d5 100644 --- a/camera.h +++ b/camera.h @@ -5,46 +5,12 @@ #include #include -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 diff --git a/phongmaterial.cpp b/phongmaterial.cpp index 7b474e2..d71a2ba 100644 --- a/phongmaterial.cpp +++ b/phongmaterial.cpp @@ -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); diff --git a/sparrowRenderer.pro b/sparrowRenderer.pro index 8d2ae06..454380f 100644 --- a/sparrowRenderer.pro +++ b/sparrowRenderer.pro @@ -24,7 +24,6 @@ unix { } SOURCES += shader.cpp \ - camera.cpp \ gridmesh.cpp \ texture.cpp \ phongmaterial.cpp \ diff --git a/sparrowrenderer.cpp b/sparrowrenderer.cpp index 9de6874..3a0fa8c 100644 --- a/sparrowrenderer.cpp +++ b/sparrowrenderer.cpp @@ -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; } diff --git a/sparrowrenderer.h b/sparrowrenderer.h index 7c527c6..e634e89 100644 --- a/sparrowrenderer.h +++ b/sparrowrenderer.h @@ -3,8 +3,8 @@ #include #include -#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 modules; std::list::iterator getModuleNode(std::string name);