PeriDyno 1.0.0
Loading...
Searching...
No Matches
TrackballCamera.cpp
Go to the documentation of this file.
1#include "TrackballCamera.h"
2
3#include <iostream>
4#include <math.h>
5
6#include <glm/gtc/matrix_transform.hpp>
7#include <glm/gtx/rotate_vector.hpp>
8
9#include <Vector.h>
10
11namespace dyno
12{
14 : Camera()
15 {
16 mCameraPos = Vec3f(0.0f, 0.0f, 3.0f);
17 mCameraTarget = Vec3f(0, 0, 0);
18 mCameraUp = Vec3f(0, 1, 0);
19
20 mFov = glm::radians(45.0f);
21 }
22
24 {
25 // TODO: reset to fit scene
26 }
27
29 {
30 return glm::lookAt(mCameraPos.data_, mCameraTarget.data_, mCameraUp.data_);
31 }
32
34 {
35 float aspect = std::max(float(mViewportWidth), 1.0f) / std::max(float(mViewportHeight), 1.0f);
36
37 glm::mat4 projection;
38
40 {
41 projection = glm::perspective(mFov, aspect, mNear * mUnitScale, mFar * mUnitScale);
42 }
43 else
44 {
45 float half_depth = (mCameraPos - mCameraTarget).norm() * mUnitScale;
46 projection = glm::ortho(-half_depth * aspect, half_depth * aspect, -half_depth, half_depth, -5.0f * half_depth, 5.0f * half_depth);
47 }
48
49 return projection;
50 }
51
52 void TrackballCamera::zoom(float amount)
53 {
54 mFov += amount / 10;
55 mFov = std::max(mFov, 0.05f);
56
57 // maybe we want to move forward/backward the camera...
58 Vec3f viewDir = mCameraPos - mCameraTarget;
59 Vec3f t = viewDir * (amount / 10.0);
60 mCameraPos += t;
61 mCameraTarget += t;
62 }
63
64 void TrackballCamera::registerPoint(float xpos, float ypos) {
65 mRegX = float(xpos) / float(mViewportWidth) - 0.5f;
66 mRegY = float(mViewportHeight - ypos) / float(mViewportHeight) - 0.5f;
67 }
68
69 void TrackballCamera::rotateToPoint(float xpos, float ypos) {
70 float x = float(xpos) / float(mViewportWidth) - 0.5f;
71 float y = float(mViewportHeight - ypos) / float(mViewportHeight) - 0.5f;
72
73 float dx = x - mRegX;
74 float dy = y - mRegY;
75
76 Vec3f viewDir = mCameraPos - mCameraTarget;
77 Vec3f rightDir = mCameraUp.cross(viewDir).normalize();
78 Vec3f upDir = viewDir.cross(rightDir).normalize();
79
80 viewDir.data_ = glm::rotate(viewDir.data_, dy, rightDir.data_);
81 viewDir.data_ = glm::rotate(viewDir.data_, -dx, upDir.data_);
82 mCameraPos = mCameraTarget + viewDir;
83
84 mCameraUp.data_ = glm::rotate(upDir.data_, dy, rightDir.data_);
85 mCameraUp.data_ = glm::rotate(upDir.data_, -dx, upDir.data_);
86
87 registerPoint(xpos, ypos);
88 }
89
90 void TrackballCamera::translateToPoint(float xpos, float ypos) {
91
92 float x = float(xpos) / float(mViewportWidth) - 0.5f;
93 float y = float(mViewportHeight - ypos) / float(mViewportHeight) - 0.5f;
94
95 float dx = x - mRegX;
96 float dy = y - mRegY;
97
98 Vec3f viewDir = mCameraPos - mCameraTarget;
99 Vec3f rightDir = mCameraUp.cross(viewDir).normalize();
100 Vec3f upDir = viewDir.cross(rightDir).normalize();
101
102 Vec3f t = upDir * -dy + rightDir * -dx;
103 mCameraPos += t;
104 mCameraTarget += t;
105
106 registerPoint(xpos, ypos);
107 }
108}
ProjectionType mProjectionType
Definition Camera.h:70
int mViewportHeight
Definition Camera.h:68
float mNear
Definition Camera.h:63
float mFar
Definition Camera.h:64
float mUnitScale
Definition Camera.h:73
int mViewportWidth
Definition Camera.h:67
@ Perspective
Definition Camera.h:22
float mFov
Definition Camera.h:65
glm::mat4 getViewMat() override
void zoom(float amount) override
void translateToPoint(float x, float y) override
void registerPoint(float x, float y) override
void rotateToPoint(float x, float y) override
glm::mat4 getProjMat() override
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Vector< float, 3 > Vec3f
Definition Vector3D.h:93