added basic camera (default camera implementation)

This commit is contained in:
Anselme 2016-06-21 17:15:38 +02:00
parent bb27fb0965
commit 116a5e3d1a
3 changed files with 136 additions and 1 deletions

View File

@ -2,8 +2,10 @@
#define CAMERA_H
#include <glm/mat4x4.hpp>
#include <glm/ext.hpp>
class Camera{
class Camera
{
public:
virtual glm::mat4 getProjectionMatrix() = 0;
virtual glm::mat4 getViewMatrix() = 0;
@ -11,4 +13,45 @@ class Camera{
virtual ~Camera() {}
};
class BasicCamera : public Camera
{
protected:
glm::mat4 m_view;
glm::mat4 m_projection;
float m_fov;
float m_near;
float m_far;
float m_ratio;
void computeProj() { m_projection = glm::perspective(m_fov, m_ratio, m_near, m_far); }
public:
BasicCamera(float myFov = 70.f, float myNear = 0.1f, float myFar = 100.f) :
m_fov(myFov), m_near(myNear), m_far(myFar), m_ratio(1.f)
{
computeProj();
setView(glm::vec3(0), glm::vec3(0, 0, 1));
}
virtual glm::mat4 getProjectionMatrix() { return m_projection; }
virtual glm::mat4 getViewMatrix() { return m_view; }
virtual void resize(int width, int height)
{
if(width > 0 && height > 0)
{
m_ratio = float(width)/float(height);
computeProj();
}
}
void setFov(float fov = 70.f) { m_fov = fov; computeProj(); }
void setNear(float myNear = 0.1f) { m_near = myNear; computeProj(); }
void setFar(float myFar = 100.f) { m_far = myFar; computeProj(); }
void setView(glm::vec3 cameraPos, glm::vec3 target, glm::vec3 upVector = glm::vec3(0, 1, 0))
{
m_view = glm::lookAt(cameraPos, target, upVector);
}
};
#endif // CAMERA_H

65
src/trackballcamera.cpp Normal file
View File

@ -0,0 +1,65 @@
#include "trackballcamera.h"
#include <glm/ext.hpp>
#define DEFAULT_SCROLL_SPEED 0.998f
#define DEFAULT_ROTATION_SPEED 0.01f
#define DEFAULT_MOVE_SPEED 0.002f
TrackBallCamera::TrackBallCamera(float myFov, float myNear, float myFar) :
BasicCamera(myFov, myNear, myFar)
{
reset();
}
void TrackBallCamera::rotateCamera(float dx, float dy)
{
m_rotation.x += dx*DEFAULT_ROTATION_SPEED;
m_rotation.y += dy*DEFAULT_ROTATION_SPEED;
if(m_rotation.y > 1.57f)
m_rotation.y = 1.57f;
if(m_rotation.y < -1.57f)
m_rotation.y = -1.57f;
computeView();
}
void TrackBallCamera::moveCamera(float dx, float dy)
{
glm::mat4 inverseMVP = glm::inverse(m_projection * m_view);
glm::vec4 diff = inverseMVP * glm::vec4(dx*m_dist*DEFAULT_MOVE_SPEED, dy*m_dist*DEFAULT_MOVE_SPEED, 0, 1);
m_center += glm::vec3(diff);
computeView();
}
void TrackBallCamera::reset()
{
m_center = glm::vec3(0, 4, 0);
m_rotation = glm::vec2(0, 1);
m_dist = 20;
computeView();
}
void TrackBallCamera::computeView()
{
m_view = glm::translate(glm::mat4(), glm::vec3(0, 0, -m_dist));
m_view = glm::rotate(m_view, m_rotation.y, glm::vec3(1, 0, 0));
m_view = glm::rotate(m_view, m_rotation.x, glm::vec3(0, 1, 0));
m_view = glm::translate(m_view, -m_center);
}
void TrackBallCamera::zoom(int nbUnits)
{
while(nbUnits != 0)
{
if(nbUnits > 0)
{
m_dist *= DEFAULT_SCROLL_SPEED;
--nbUnits;
}
else
{
m_dist /= DEFAULT_SCROLL_SPEED;
++nbUnits;
}
}
computeView();
}

27
src/trackballcamera.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef TRACKBALLCAMERA_H
#define TRACKBALLCAMERA_H
#include "camera.h"
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
class TrackBallCamera : public BasicCamera
{
private:
// camera position
glm::vec3 m_center;
glm::vec2 m_rotation;
float m_dist;
void computeView();
public:
TrackBallCamera(float myFov = 70.f, float myNear = 0.1f, float myFar = 100.f);
void rotateCamera(float dx, float dy);
void moveCamera(float dx, float dy);
void zoom(int nbScrolls);
void reset();
};
#endif // TRACKBALLCAMERA_H