-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
53 lines (43 loc) · 1.96 KB
/
camera.cpp
File metadata and controls
53 lines (43 loc) · 1.96 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
#include "camera.h"
#include <iostream>
#include "mygl.h"
Camera::Camera(InputBundle* input) : input(input), prevMousePos(input->mousePos), lookSens(vec2(0.005f)), upBarrier(0.1f), fovY(PI * 0.5f), moveSpeed(2.0f), polarCoords(0.0f), nearFar(0.1f, 1000.0f) {}
/// <summary>
/// Call this even when not changing transform since camera needs to do maintenance stuff per frame.
/// </summary>
/// <param name="dt"></param>
/// <param name="cameraEnabled"></param>
void Camera::update(float dt, bool cameraEnabled) {
// Orientation
vec2 dm = (input->mousePos - prevMousePos) * vec2(1.0f, -1.0f);
if (prevMousePos.x == -1.0f) dm *= 0.0f; // Ignore first mouse callback
prevMousePos = input->mousePos;
//
if (!cameraEnabled) return;
polarCoords += dm * lookSens;
if(abs(polarCoords.y) > 0.0f) polarCoords.y = sign(polarCoords.y) * glm::min(abs(polarCoords.y), PI * 0.5f - upBarrier);
//
fo = cos(polarCoords.y) * vec3(sin(polarCoords.x), 0.0f, cos(polarCoords.x)) + sin(polarCoords.y) * vec3(0.0f, 1.0f, 0.0f);
up = normalize(vec3(0.0f, 1.0f, 0.0f) - fo * fo.y);
ri = cross(up, fo);
//
vec4 move = vec4(vec3(int(input->d) - int(input->a), int(input->e) - int(input->q), int(input->w) - int(input->s)) * moveSpeed * dt, 0.0f);
position += vec3(getTransform() * move);
}
void Camera::setInitialUniforms(Shader* shader) {
shader->use();
shader->uniformFloat("u_fovY", fovY);
updateUniforms(shader);
}
void Camera::updateUniforms(Shader* shader) {
shader->use();
shader->uniformVec3("u_CamPos", position);
shader->uniformVec3("u_CamRi", ri);
shader->uniformVec3("u_CamUp", up);
shader->uniformVec3("u_CamFo", fo);
}
mat4 Camera::getProjectionMatrix() const {
float aspect = (1.0f * MyGL::screenDimensions.x) / MyGL::screenDimensions.y;
float mi = 1.0f / tan(fovY * 0.5f);
return mat4(vec4(mi / aspect, 0.0f, 0.0f, 0.0f), vec4(0.0f, mi, 0.0f, 0.0f), vec4(0.0f, 0.0f, nearFar.y/(nearFar.y-nearFar.x), 1.0f), vec4(0.0f, 0.0f, -nearFar.y*nearFar.x/(nearFar.y-nearFar.x), 0.0f));
}