-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
executable file
·217 lines (176 loc) · 5.44 KB
/
Camera.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// Camera.cpp
// RealtimeRending
//
// Created by Philipp Lensing on 22.10.14.
// Copyright (c) 2014 Philipp Lensing. All rights reserved.
//
#include "Camera.h"
#define _USE_MATH_DEFINES
#include <math.h>
Camera::Camera(GLFWwindow* pWin) : m_Position(0.0f,5.0f,5.0f), m_Target(0.0f,0.0f,0.0f), m_Up(0.0f,1.0f,0.0f), m_LastMouseX(-1), m_LastMouseY(-1), m_Panning(0,0,0), m_Zoom(0,0,0), m_Rotation(0,0,0), WindowWidth(640), WindowHeight(480), pWindow(pWin)
{
if(pWindow)
glfwGetWindowSize(pWindow, &WindowWidth, &WindowHeight);
m_ViewMatrix.identity();
m_ProjMatrix.perspective((float)M_PI*65.0f/180.0f, (float)WindowWidth/(float)WindowHeight, 0.045f, 1000.0f);
}
Vector Camera::position() const
{
return m_Position + m_Panning + m_Zoom + m_Rotation;
}
Vector Camera::target() const
{
return m_Target + m_Panning;
}
Vector Camera::up() const
{
return m_Up;
}
void Camera::setPosition( const Vector& Pos)
{
m_Position = Pos;
m_Panning = m_Rotation = m_Zoom = Vector(0,0,0);
}
void Camera::setTarget( const Vector& Target)
{
m_Target = Target;
m_Panning = Vector(0,0,0);
}
void Camera::setUp( const Vector& Up)
{
m_Up = Up;
}
void Camera::mouseInput( int x, int y, int Button, int State)
{
if(State == GLFW_PRESS)
{
if(m_LastMouseX==-1) m_LastMouseX = x;
if(m_LastMouseY==-1) m_LastMouseY = y;
if( Button == GLFW_MOUSE_BUTTON_LEFT )
{
rotate((float)x, (float)y );
}
else if( Button == GLFW_MOUSE_BUTTON_RIGHT)
{
pan( (float)(m_LastMouseX-x)*0.01f, (float)(m_LastMouseY-y)*0.01f );
}
else if( Button == GLFW_MOUSE_BUTTON_MIDDLE)
{
zoom( (float)(m_LastMouseY-y)*0.01f );
}
}
else
{
m_Position += m_Panning + m_Zoom + m_Rotation;
m_Target += m_Panning;
m_Panning = Vector(0,0,0);
m_Zoom = Vector(0,0,0);
m_Rotation = Vector(0,0,0);
m_LastMouseX = -1;
m_LastMouseY = -1;
}
}
void Camera::pan( float dx, float dy)
{
// calculate panning-plane
Vector aDir = m_Target-m_Position;
aDir.normalize();
Vector aRight = aDir.cross(m_Up);
aRight.normalize();
Vector aUp = aDir.cross(aRight);
m_Panning = aRight * dx + aUp * dy;
}
void Camera::zoom( float dz)
{
Vector aDir = m_Target-m_Position;
float Dist = aDir.length();
aDir.normalize();
if( Dist-dz <= 1.0f)
{
m_Zoom = aDir * (Dist-1.0f);
return;
}
m_Zoom = aDir * dz;
}
void Camera::rotate( float x, float y )
{
Vector po = getVSpherePos((float) m_LastMouseX, (float)m_LastMouseY);
Vector pn = getVSpherePos(x, y);
if((po-pn).lengthSquared() < 0.0001f )
return;
float cosangle = po.dot(pn);
if(cosangle>1.0f) cosangle=1.0f;
if(cosangle<-1.0f) cosangle=-1.0f;
const float angle = acos(cosangle);
Vector RotAxis = pn.cross(po);
RotAxis.normalize();
//Vector Diff = m_Position-m_Target;
Vector Diff(0,0,(m_Position-m_Target).length());
Vector RotDiff = rotateAxisAngle(Diff, RotAxis, angle);
Vector cdir = m_Target-m_Position;
cdir.normalize();
Vector cup = m_Up;
Vector cright = cdir.cross(cup);
cright.normalize();
cup = cright.cross(cdir);
Vector RotDiffW;
RotDiffW.X = cright.X * RotDiff.X + cup.X * RotDiff.Y + -cdir.X * RotDiff.Z;
RotDiffW.Y = cright.Y * RotDiff.X + cup.Y * RotDiff.Y + -cdir.Y * RotDiff.Z;
RotDiffW.Z = cright.Z * RotDiff.X + cup.Z * RotDiff.Y + -cdir.Z * RotDiff.Z;
m_Rotation = RotDiffW - (m_Position-m_Target);
}
Vector Camera::rotateAxisAngle( Vector v, Vector n, float a)
{
float co = cos(a);
float si = sin(a);
Vector o;
Vector mx( n.X*n.X*(1.0f-co)+co, n.X*n.Y*(1.0f-co)-n.Z*si,n.X*n.Z*(1.0f-co)+n.Y*si );
Vector my( n.X*n.Y*(1.0f-co)+n.Z*si, n.Y*n.Y*(1.0f-co)+co, n.Y*n.Z*(1.0f-co)-n.X*si );
Vector mz( n.X*n.Z*(1.0f-co)-n.Y*si, n.Z*n.Y*(1.0f-co)+n.X*si, n.Z*n.Z*(1.0f-co)+co);
o.X = mx.dot(v);
o.Y = my.dot(v);
o.Z = mz.dot(v);
return o;
}
const Matrix& Camera::getViewMatrix() const
{
return m_ViewMatrix;
}
const Matrix& Camera::getProjectionMatrix() const
{
return m_ProjMatrix;
}
Vector Camera::getVSpherePos( float x, float y)
{
Vector p( 1.0f*x/(float)WindowWidth*2.0f - 1.0f, 1.0f*y/(float)WindowHeight*2.0f -1.0f, 0);
p.Y = -p.Y;
float sqrLen = p.lengthSquared();
if( sqrLen <= 1.0f)
{
p.Z = sqrt( 1- sqrLen);
}
else
p.normalize();
return p;
}
void Camera::updateMouseInput()
{
double xpos, ypos;
glfwGetCursorPos(pWindow, &xpos, &ypos);
if( glfwGetMouseButton(pWindow, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
mouseInput((int)xpos, (int)ypos, GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS);
else if (glfwGetMouseButton(pWindow, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
mouseInput((int)xpos, (int)ypos, GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS);
else if (glfwGetMouseButton(pWindow, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS)
mouseInput((int)xpos, (int)ypos, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_PRESS);
else
mouseInput((int)xpos, (int)ypos, GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE);
}
void Camera::update()
{
updateMouseInput();
Vector Pos = position(); //m_Position + m_Panning + m_Zoom + m_Rotation;
Vector Target = target(); //m_Target + m_Panning;
m_ViewMatrix.lookAt(Target, m_Up, Pos);
}