-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCamera.cpp
More file actions
139 lines (112 loc) · 2.83 KB
/
Copy pathCamera.cpp
File metadata and controls
139 lines (112 loc) · 2.83 KB
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
#include <GL/freeglut.h>
#include "Camera.h"
#include <iostream>
#include <glm/gtc/matrix_transform.hpp>
#ifndef PI
#define PI 3.141592f
#endif
#define CAMERA_ROTATE 1
#define CAMERA_MOVE 2
using namespace glm;
Camera::Camera(float ratio, vec3 camPos, float waterheight)
{
mMotionState = CAMERA_ROTATE;
mTheta = 2.0;
mPhi = 4.0;
mSpeed = 0.0f;
mMotionStep = 0.001f; // sens of camera controls
mThetaStep = 0.001f;
mPhiStep = 0.003f;
mPosition = camPos;
mRatio = ratio;
mNear = 0.01f;
mFar = 100000.0f;
mUp = vec3(0.0f, 1.0f, 0.0f);
mApertureAngle = 60.0f;
mWaterHeight = waterheight;
mProjectionMatrix = perspective(mApertureAngle, mRatio, mNear, mFar);
}
Camera::~Camera()
{
}
void Camera::mouseButton(int button, int state, int x, int y)
{
mMove = true;
mOldX = x;
mOldY = y;
// Left mouse button: turn camera
if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
mMotionState = CAMERA_ROTATE;
}
// right mouse button: move camera
else if (button == GLUT_RIGHT_BUTTON)
{
if (state == GLUT_DOWN)
mMotionState = CAMERA_MOVE;
}
}
void Camera::mouseMove(int x, int y)
{
int deltaX = x - mOldX;
int deltaY = y - mOldY;
if (mMotionState == CAMERA_ROTATE)
{
mTheta += mThetaStep * static_cast<float>(deltaY);
if (mTheta < mThetaStep)
mTheta = mThetaStep;
else if (mTheta > PI - mThetaStep)
mTheta = PI - mThetaStep;
mPhi += mPhiStep * static_cast<float>(deltaX);
if (mPhi < 0.0f)
mPhi += 2.0f*PI;
else if (mPhi > 2.0f*PI)
mPhi -= 2.0f*PI;
}
else if (mMotionState == CAMERA_MOVE)
mSpeed -= mMotionStep * static_cast<float>(deltaY);
mOldX = x;
mOldY = y;
}
void Camera::update()
{
mDirection.x = sin(mTheta) * cos(mPhi);
mDirection.y = cos(mTheta);
mDirection.z = sin(mTheta) * sin(mPhi);
mPosition += mSpeed * mDirection;
mViewMatrix = lookAt(mPosition, mPosition + mDirection, mUp);
updateReflectedViewMatrix();
}
const glm::mat4& Camera::getViewMatrix() const
{
if (reflected)
return mReflectedViewMatrix;
else
return mViewMatrix;
}
const glm::mat4& Camera::getProjectionMatrix() const
{
return mProjectionMatrix;
}
void Camera::updateProjection(float ratio)
{
mRatio = ratio;
mProjectionMatrix = perspective(mApertureAngle, mRatio, mNear, mFar);
}
void Camera::setViewDir(glm::fvec3 dir)
{
mViewMatrix = lookAt(mPosition, mPosition + dir, mUp);
}
void Camera::updateReflectedViewMatrix()
{
vec3 newPosition = mPosition;
newPosition.y = mPosition.y - (2 * (mPosition.y - mWaterHeight));
vec3 newForward = glm::reflect(mDirection, vec3(0.0, 1.0, 0.0));
vec3 newUp = mUp * -1.0f;
mReflectedViewMatrix = lookAt(newPosition, newPosition+newForward, mUp);
}
void Camera::reflect()
{
reflected = !reflected;
}