Skip to content

Commit

Permalink
Merge pull request #91 from Christof/camera-node
Browse files Browse the repository at this point in the history
Add camera node which is stored in scene.
  • Loading branch information
Christof committed Jan 19, 2016
2 parents 6b97111 + bb55b70 commit 579db9f
Show file tree
Hide file tree
Showing 23 changed files with 336 additions and 94 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ set(SOURCES
src/volume_node.cpp
src/forces_visualizer_node.cpp
src/coordinate_system_node.cpp
src/camera_node.cpp
src/nodes.cpp
src/abstract_scene.h
src/scene.cpp
Expand Down
48 changes: 40 additions & 8 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@
#include <Eigen/Geometry>
#include <math.h>

#include <iostream>

Camera::Camera()
: origin(0, 0, 0), position(0, 0, -1), direction(0, 0, 1), up(0, 1, 0),
radius(1.0f), azimuth(-M_PI / 2.0f), declination(0),
fieldOfView(M_PI / 2.0f)
radius(1.0f), azimuth(-M_PI / 2.0f), declination(0)
{
projection = createProjection(fieldOfView, aspectRatio, near, far);
// projection = createOrthographicProjection(aspectRatio, near, far);

update();
}

Camera::Camera(Eigen::Matrix4f viewMatrix, Eigen::Matrix4f projectionMatrix,
Eigen::Vector3f origin)
: projection(projectionMatrix), view(viewMatrix), origin(origin)
{
position = -viewMatrix.inverse().col(3).head<3>();
direction = viewMatrix.col(2).head<3>();
up = viewMatrix.col(1).head<3>();

radius = (position - origin).norm();

Eigen::Vector3f diff = (position - origin) / radius;
declination = asin(diff.y());
azimuth = -acos(diff.x() / cos(declination));
}

Camera::~Camera()
Expand Down Expand Up @@ -47,18 +65,21 @@ Eigen::Matrix4f Camera::createOrthographicProjection(float aspectRatio,
void Camera::moveForward(float distance)
{
position += distance * direction;
update();
}

void Camera::moveBackward(float distance)
{
position -= distance * direction;
update();
}

void Camera::strafe(float distance)
{
auto right = direction.cross(up);
position += distance * right;
origin += distance * right;
update();
}

void Camera::strafeLeft(float distance)
Expand All @@ -75,6 +96,7 @@ void Camera::moveVertical(float distance)
{
position += distance * up;
origin += distance * up;
update();
}

void Camera::changeAzimuth(float deltaAngle)
Expand Down Expand Up @@ -107,32 +129,37 @@ void Camera::update()
float upDeclination = declination - M_PI / 2.0f;
up = -Eigen::Vector3f(cos(azimuth) * cos(upDeclination), sin(upDeclination),
sin(azimuth) * cos(upDeclination)).normalized();
}

Eigen::Matrix4f Camera::getViewMatrix()
{
auto n = direction.normalized();
auto u = up.cross(n).normalized();
auto v = n.cross(u);
auto e = position;

view << u.x(), u.y(), u.z(), u.dot(e), v.x(), v.y(), v.z(), v.dot(e), n.x(),
n.y(), n.z(), n.dot(e), 0, 0, 0, 1;
}

Eigen::Matrix4f Camera::getViewMatrix() const
{
return view;
}

Eigen::Matrix4f Camera::getProjectionMatrix()
Eigen::Matrix4f Camera::getProjectionMatrix() const
{
return projection;
}

Eigen::Vector3f Camera::getPosition()
Eigen::Vector3f Camera::getPosition() const
{
return position;
}

float Camera::getRadius()
Eigen::Vector3f Camera::getOrigin() const
{
return origin;
}

float Camera::getRadius() const
{
return (position - origin).norm();
}
Expand All @@ -151,3 +178,8 @@ void Camera::updateNearAndFarPlanes(float near, float far)
projection = createProjection(fieldOfView, aspectRatio, near, far);
}

bool Camera::needsResizing()
{
return aspectRatio == 0.0f;
}

19 changes: 12 additions & 7 deletions src/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ class Camera
{
public:
Camera();
Camera(Eigen::Matrix4f viewMatrix, Eigen::Matrix4f projectionMatrix,
Eigen::Vector3f origin);
virtual ~Camera();

Eigen::Matrix4f getProjectionMatrix();
Eigen::Matrix4f getViewMatrix();
Eigen::Vector3f getPosition();
float getRadius();
Eigen::Matrix4f getProjectionMatrix() const;
Eigen::Matrix4f getViewMatrix() const;
Eigen::Vector3f getPosition() const;
Eigen::Vector3f getOrigin() const;
float getRadius() const;

public:
void moveForward(float distance);
void moveBackward(float distance);
void strafeLeft(float distance);
Expand All @@ -35,6 +37,9 @@ class Camera
void resize(float width, float height);

void updateNearAndFarPlanes(float near, float far);

bool needsResizing();

private:
float near = 0.1f;
float far = 5.0f;
Expand All @@ -50,8 +55,8 @@ class Camera
float azimuth;
float declination;

float fieldOfView;
float aspectRatio = 16.0f / 9.0f;
float fieldOfView = 0.5 * M_PI;
float aspectRatio = 0.0f;

Eigen::Matrix4f createProjection(float fov, float aspectRatio,
float nearPlane, float farPlane);
Expand Down
27 changes: 13 additions & 14 deletions src/camera_controller.cpp
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
#include "./camera_controller.h"
#include <QDebug>
#include "./camera.h"

CameraController::CameraController(Camera &camera) : camera(camera)
CameraController::CameraController(std::shared_ptr<Camera> camera)
: camera(camera)
{
}

CameraController::~CameraController()
{
}

void CameraController::setFrameTime(double frameTime)
void CameraController::update(std::shared_ptr<Camera> camera, double frameTime)
{
this->camera = camera;
this->frameTime = frameTime;
}

void CameraController::moveForward()
{
camera.moveForward(frameTime * cameraSpeed);
camera->moveForward(frameTime * cameraSpeed);
}

void CameraController::moveBackward()
{
camera.moveBackward(frameTime * cameraSpeed);
camera->moveBackward(frameTime * cameraSpeed);
}

void CameraController::strafeLeft()
{
camera.strafeLeft(frameTime * cameraSpeed);
camera->strafeLeft(frameTime * cameraSpeed);
}

void CameraController::strafeRight()
{
camera.strafeRight(frameTime * cameraSpeed);
camera->strafeRight(frameTime * cameraSpeed);
}

void CameraController::azimuthLeft()
{
camera.changeAzimuth(frameTime);
camera->changeAzimuth(frameTime);
}

void CameraController::azimuthRight()
{
camera.changeAzimuth(-frameTime);
camera->changeAzimuth(-frameTime);
}

void CameraController::increaseDeclination()
{
camera.changeDeclination(-frameTime);
camera->changeDeclination(-frameTime);
}

void CameraController::decreaseDeclination()
{
camera.changeDeclination(frameTime);
camera->changeDeclination(frameTime);
}

11 changes: 6 additions & 5 deletions src/camera_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#define SRC_CAMERA_CONTROLLER_H_

#include <QObject>
#include "./camera.h"
#include <memory>

class Camera;

/**
* \brief Provides slots to control the given camera from
Expand All @@ -16,10 +18,9 @@ class CameraController : public QObject
{
Q_OBJECT
public:
explicit CameraController(Camera &camera);
virtual ~CameraController();
explicit CameraController(std::shared_ptr<Camera> camera);

void setFrameTime(double frameTime);
void update(std::shared_ptr<Camera> camera, double frameTime);

public slots:
void moveForward();
Expand All @@ -32,7 +33,7 @@ class CameraController : public QObject
void decreaseDeclination();

private:
Camera &camera;
std::shared_ptr<Camera> camera;
double cameraSpeed = 10.0f;
double frameTime;
};
Expand Down
13 changes: 7 additions & 6 deletions src/camera_controllers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include "./camera_move_controller.h"

CameraControllers::CameraControllers(
std::shared_ptr<InvokeManager> invokeManager, Camera &camera)
std::shared_ptr<InvokeManager> invokeManager,
std::shared_ptr<Camera> camera)
{
cameraController = std::make_shared<CameraController>(camera);
cameraRotationController = std::make_shared<CameraRotationController>(camera);
Expand All @@ -19,11 +20,11 @@ CameraControllers::CameraControllers(
invokeManager->addHandler("cameraMove", cameraMoveController.get());
}

void CameraControllers::update(double frameTime)
void CameraControllers::update(std::shared_ptr<Camera> camera, double frameTime)
{
cameraController->setFrameTime(frameTime);
cameraRotationController->setFrameTime(frameTime);
cameraZoomController->setFrameTime(frameTime);
cameraMoveController->setFrameTime(frameTime);
cameraController->update(camera, frameTime);
cameraRotationController->update(camera, frameTime);
cameraZoomController->update(camera, frameTime);
cameraMoveController->update(camera, frameTime);
}

4 changes: 2 additions & 2 deletions src/camera_controllers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class CameraControllers
{
public:
CameraControllers(std::shared_ptr<InvokeManager> invokeManager,
Camera &camera);
std::shared_ptr<Camera> camera);

void update(double frameTime);
void update(std::shared_ptr<Camera> camera, double frameTime);

private:
std::shared_ptr<CameraController> cameraController;
Expand Down
9 changes: 5 additions & 4 deletions src/camera_move_controller.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include "./camera_move_controller.h"
#include "./camera.h"

CameraMoveController::CameraMoveController(Camera &camera) : camera(camera)
CameraMoveController::CameraMoveController(std::shared_ptr<Camera> camera)
: MouseDraggingController(camera, 0.2)
{
}

void CameraMoveController::update(Eigen::Vector2f diff)
void CameraMoveController::updateFromDiff(Eigen::Vector2f diff)
{
Eigen::Vector2f delta = -frameTime * speedFactor * diff;

camera.strafe(delta.x());
camera.moveVertical(delta.y());
camera->strafe(delta.x());
camera->moveVertical(delta.y());
}

8 changes: 2 additions & 6 deletions src/camera_move_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ class Camera;
class CameraMoveController : public MouseDraggingController
{
public:
explicit CameraMoveController(Camera &camera);
explicit CameraMoveController(std::shared_ptr<Camera> camera);

protected:
virtual void update(Eigen::Vector2f diff);

private:
Camera &camera;
double speedFactor = 0.2;
virtual void updateFromDiff(Eigen::Vector2f diff);
};

#endif // SRC_CAMERA_MOVE_CONTROLLER_H_
22 changes: 22 additions & 0 deletions src/camera_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "./camera_node.h"

CameraNode::CameraNode()
{
camera = std::make_shared<Camera>();
}

CameraNode::CameraNode(std::shared_ptr<Camera> camera)
: camera(camera)
{
}

void CameraNode::render(Graphics::Gl *gl,
std::shared_ptr<Graphics::Managers> managers,
RenderData renderData)
{
}

std::shared_ptr<Camera> CameraNode::getCamera()
{
return camera;
}
Loading

0 comments on commit 579db9f

Please sign in to comment.