aboutsummaryrefslogtreecommitdiffstats
path: root/dev/MinGfx/src/craft_cam.cc
blob: 425f524a0321396b20bba3678ecf35f0468dc26d (plain) (blame)
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
/*
 Copyright (c) 2017,2018 Regents of the University of Minnesota.
 All Rights Reserved.
 See corresponding header file for details.
 */

#include "craft_cam.h"

namespace mingfx {
    

CraftCam::CraftCam() : t_scale_(1.0), r_scale_(1.0), l_scale_(1.0), yaw_(0.0), pitch_(0.0) {
}

CraftCam::CraftCam(const Matrix4 &initialViewMatrix) :
    t_scale_(1.0), r_scale_(1.0), l_scale_(1.0), yaw_(0.0), pitch_(0.0)
{
}

CraftCam::~CraftCam()
{
}

    
void CraftCam::UpdateSimulation(double dt, GLFWwindow *window_ptr) {
    if ((glfwGetKey(window_ptr, GLFW_KEY_UP) == GLFW_PRESS) ||
        (glfwGetKey(window_ptr, GLFW_KEY_W) == GLFW_PRESS)) {
        WalkForward(dt);
    }
    if ((glfwGetKey(window_ptr, GLFW_KEY_DOWN) == GLFW_PRESS) ||
        (glfwGetKey(window_ptr, GLFW_KEY_Z) == GLFW_PRESS)) {
        WalkBackward(dt);
    }
    if ((glfwGetKey(window_ptr, GLFW_KEY_LEFT) == GLFW_PRESS) ||
        (glfwGetKey(window_ptr, GLFW_KEY_A) == GLFW_PRESS)) {
        RotateLeft(dt);
    }
    if ((glfwGetKey(window_ptr, GLFW_KEY_RIGHT) == GLFW_PRESS) ||
        (glfwGetKey(window_ptr, GLFW_KEY_S) == GLFW_PRESS)) {
        RotateRight(dt);
    }
}

    
void CraftCam::OnMouseMove(const Vector2 &normalized_mouse_delta) {
    LookWithMouse(normalized_mouse_delta);
}



void CraftCam::WalkForward(double dt) {
    base_head_ = Matrix4::Translation(3.0f * (float)dt * t_scale_ * Vector3(0,0,1)) * base_head_;
}

void CraftCam::WalkBackward(double dt) {
    base_head_ = Matrix4::Translation(3.0f * (float)dt * t_scale_ * Vector3(0,0,-1)) * base_head_;
}

void CraftCam::RotateLeft(double dt) {
    base_head_ = Matrix4::RotationY(-0.75f * (float)dt * r_scale_) * base_head_;
}

void CraftCam::RotateRight(double dt) {
    base_head_ = Matrix4::RotationY(0.75f * (float)dt * r_scale_) * base_head_;
}

void CraftCam::LookWithMouse(const Vector2 &mouse_delta) {
    yaw_ += l_scale_ * mouse_delta[0];
    pitch_ += l_scale_ * mouse_delta[1];
    added_rot_ = Matrix4::RotationX(-pitch_) * Matrix4::RotationY(yaw_);
}


Matrix4 CraftCam::view_matrix() {
    return added_rot_ * base_head_;
}

void CraftCam::set_view_matrix(Matrix4 view_matrix) {
    base_head_ = view_matrix;
    added_rot_ = Matrix4();
}

Point3 CraftCam::eye() {
    Matrix4 camMat = view_matrix().Inverse();
    return camMat.ColumnToPoint3(3);
}

Vector3 CraftCam::look() {
    Matrix4 camMat = view_matrix().Inverse();
    return -camMat.ColumnToVector3(2);
}


void CraftCam::set_translation_scale(float s) {
    t_scale_ = s;
}

void CraftCam::set_rotation_scale(float s) {
    r_scale_ = s;
}

void CraftCam::set_look_scale(float s) {
    l_scale_ = s;
}

    
float CraftCam::translation_scale() {
    return t_scale_;
}

float CraftCam::rotation_scale() {
    return r_scale_;
}

float CraftCam::look_scale() {
    return l_scale_;
}

void CraftCam::UpdateHeight(float new_y_value) {
    Vector3 offset = Vector3(0, new_y_value - eye()[1], 0);    
    base_head_ = Matrix4::Translation(-offset) * base_head_;
}

    
    
} // end namespace