forked from Kiberchaika/Murka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMurCamera.h
115 lines (81 loc) · 2.55 KB
/
MurCamera.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#pragma once
#include "MurkaTypes.h"
#include "MurMatrix.h"
namespace murka {
#if defined(MURKA_OF)
class MurCamera {
public:
ofCamera camera;
MurCamera() {
camera.setNearClip(0.001);
camera.setFarClip(1000);
}
void setPosition(MurkaPoint3D p) {
camera.setPosition(p.x, p.y, p.z);
}
void lookAt(MurkaPoint3D p) {
camera.lookAt(ofVec3f(p.x, p.y, p.z));
}
ofMatrix4x4 getViewMatrix() {
return camera.getModelViewMatrix();
}
ofMatrix4x4 getProjectionMatrix(float aspect) {
return camera.getProjectionMatrix();
}
};
#elif defined(MURKA_JUCE)
class MurCamera {
MurMatrix<float> transformMatrix;
MurMatrix<float> projectionMatrix;
MurkaPoint3D position;
MurkaPoint3D target;
void updateTransformMatrix() {
MurMatrix<float> m;
MurkaPoint3D relPosition = (position - target);
float radius = relPosition.length();
if (radius > 0) {
float latitude = acos(relPosition.y / radius) - (float)( M_PI / 2);
float longitude = atan2(relPosition.x, relPosition.z);
juce::Quaternion<float> q = juce::Quaternion<float>::fromAngle(0.f, juce::Vector3D<float>(0, 0, 1));
q *= juce::Quaternion<float>::fromAngle(longitude, juce::Vector3D<float>(0, 1, 0));
q *= juce::Quaternion<float>::fromAngle(latitude, juce::Vector3D<float>(1, 0, 0));
m = MurMatrix<float>::fromQuaternion(q);
}
transformMatrix = MurMatrix<float>();
transformMatrix = transformMatrix.scaled(juce::Vector3D<float>(1, 1, 1));
transformMatrix = transformMatrix * m;
transformMatrix = transformMatrix * MurMatrix<float>::translation(juce::Vector3D<float>(position.x, position.y, position.z));
}
public:
MurCamera(){
}
void setPosition(MurkaPoint3D p) {
position = p;
updateTransformMatrix();
}
void lookAt(MurkaPoint3D p) {
target = p;
updateTransformMatrix();
}
MurMatrix<float> getViewMatrix() {
MurMatrix<float> m_inv = transformMatrix.inverted();
return transformMatrix.inverted();
}
MurMatrix<float> getProjectionMatrix(float aspect) {
float fov = 60;
float zFar = 1000;
float zNear = 0.001;
float tanHalfFovy = tan((fov * (float)(M_PI) / 180) / 2);
projectionMatrix = MurMatrix<float>();
projectionMatrix.mat[0] = 1 / (aspect * tanHalfFovy); // [0][0]
projectionMatrix.mat[5] = 1 / (tanHalfFovy); // [1][1]
projectionMatrix.mat[11] = -1; // [2][3] 14
projectionMatrix.mat[10] = -(zFar + zNear) / (zFar - zNear); // [2][2]
projectionMatrix.mat[14] = -(2 * zFar * zNear) / (zFar - zNear); // [3][2] 11
projectionMatrix.mat[15] = 0;
return projectionMatrix;
}
};
#else // Default version
#endif
}