-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlane.cpp
More file actions
152 lines (109 loc) · 4.92 KB
/
Plane.cpp
File metadata and controls
152 lines (109 loc) · 4.92 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
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "Plane.h"
#include "vertexArray.h"
#include "texture.h"
#include "shaderProgram.h"
#include "Propellor.h"
#include "camera.h"
#include "input.h"
#include "orbitLight.h"
#include "GL/glew.h"
#include "glm/ext.hpp"
#include "SDL2/SDL.h"
#include <iostream>
Plane::Plane()
{
model = std::make_shared<vertexArray>("Models/plane/plane.obj");
modelTexture = std::make_shared<texture>("Models/plane/plane.png");
program = std::make_shared<shaderProgram>(shaderProgram::SHADERTYPE::SHADER_LIGHT);
position = glm::vec3(0.0f, 0.0f, 0.0f);
rotation = glm::vec3(0.0f, 0.0f, 0.0f);
modelMatrix = glm::translate(modelMatrix, position);
modelMatrix = glm::rotate(modelMatrix, glm::radians(rotation.x), glm::vec3(0, 1, 0));
modelMatrix = glm::rotate(modelMatrix, glm::radians(rotation.y), glm::vec3(1, 0, 0));
modelMatrix = glm::rotate(modelMatrix, glm::radians(rotation.z), glm::vec3(0, 0, 1));
projection = glm::perspective(glm::radians(60.0f), ((1.0f * 1920) / (1.0f * 1080)), 0.1f, 1000.0f);
propellor = std::make_shared<Propellor>();
cockpitCamera = std::make_shared<camera>();
rearCamera = std::make_shared<camera>();
throttle = 0.5f;
viewMatrix = cockpitCamera->getMatrix();
}
void Plane::draw(std::shared_ptr<orbitLight> _light, float _deltaTime,int currentCamera)
{
if (currentCamera == 0)
{
program->update(projection, cockpitCamera->getMatrix(), modelMatrix, model, modelTexture->getId());
}
else
{
program->update(projection, rearCamera->getMatrix(), modelMatrix, model, modelTexture->getId());
}
//program->update(projection, cockpitCamera->getMatrix(), modelMatrix, model, modelTexture->getId(), _light);
propellor->draw(_light);
}
void Plane::update(glm::vec3 _translation, glm::vec3 _rotation, glm::mat4 _viewMatrix)
{
modelMatrix = glm::mat4(1.0f);
position += _translation;
rotation += _rotation;
modelMatrix = glm::translate(modelMatrix, position);
modelMatrix = glm::rotate(modelMatrix, glm::radians(rotation.x), glm::vec3(0, 1, 0));
modelMatrix = glm::rotate(modelMatrix, glm::radians(rotation.y), glm::vec3(1, 0, 0));
modelMatrix = glm::rotate(modelMatrix, glm::radians(rotation.z), glm::vec3(0, 0, 1));
front = glm::vec3(modelMatrix * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f));
right = glm::vec3(modelMatrix * glm::vec4(1.0f, 0.0f, 0.0f, 1.0f));
up = glm::vec3(modelMatrix * glm::vec4(0.0f, 1.0f, 0.0f, 1.0f));
viewMatrix = _viewMatrix;
glm::vec3 propTransform = front + 2.6f;
propellor->update(front, _rotation, _viewMatrix);
}
void Plane::update(input inp, glm::mat4 _viewMatrix, float deltaTime)
{
float mouseSensitivity = 0.6f;
float xoff = -inp.getMouseDelta().x * mouseSensitivity * deltaTime;
float yoff = -inp.getMouseDelta().y * mouseSensitivity * deltaTime;
float zoff = 0.0f;
if (inp.getKey_e()) { zoff -= 40.0f * mouseSensitivity * deltaTime; }
if (inp.getKey_q()) { zoff += 40.0f * mouseSensitivity * deltaTime; }
rotation.x += xoff;// yaw
rotation.z += yoff;// pitch
rotation.y += zoff;// roll
glm::mat4 Model(1.0f);
Model = glm::rotate(Model, glm::radians(rotation.x), glm::vec3(0, 1, 0));
Model = glm::rotate(Model, glm::radians(rotation.y), glm::vec3(1, 0, 0));
Model = glm::rotate(Model, glm::radians(rotation.z), glm::vec3(0, 0, 1));
front = glm::vec3(Model * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f));
right = glm::vec3(Model * glm::vec4(1.0f, 0.0f, 0.0f, 1.0f));
up = glm::vec3(Model * glm::vec4(0.0f, 1.0f, 0.0f, 1.0f));
if (inp.getKey_w() && throttle <= 1.0f)
{
throttle += 0.1f * deltaTime;
//position += -(right * 1.0f) * deltaTime;
}
if (inp.getKey_s() && throttle >= 0.0f)
{
throttle -= 0.1f * deltaTime;
//position += right * 1.0f * deltaTime;
}
if (throttle < 0.1f)
{
position -= (9.8f * up * deltaTime);
}
float currentSpeed = minSpeed + ((throttle / 1.0f) * (maxSpeed - minSpeed));
position += -(right * currentSpeed) * deltaTime;
modelMatrix = glm::mat4(1.0f);
modelMatrix = glm::translate(modelMatrix, position);
modelMatrix = glm::rotate(modelMatrix, glm::radians(rotation.x), glm::vec3(0, 1, 0));
modelMatrix = glm::rotate(modelMatrix, glm::radians(rotation.y), glm::vec3(1, 0, 0));
modelMatrix = glm::rotate(modelMatrix, glm::radians(rotation.z), glm::vec3(0, 0, 1));
glm::vec3 propTransform = position + (glm::normalize(right) * 2.6f);
//propellor->update(propTransform, rotation, _viewMatrix);
propellor->setTransform(propTransform, rotation, _viewMatrix, throttle);
glm::vec3 cockpitCameraPosition = position + (glm::normalize(up) * 2.0f) + (glm::normalize(right) * -2.4f);
glm::vec3 cockpitRotation = glm::vec3(rotation.x +90, -rotation.z, rotation.y);// +glm::vec3(0, 0, 90);
cockpitCamera->setTransform(cockpitCameraPosition, cockpitRotation);
glm::vec3 rearCameraPosition = position + (glm::normalize(up) * 5.0f) + (glm::normalize(right) * 15.0f);
glm::vec3 rearCameraRotation = glm::vec3(rotation.x + 90, -rotation.z, rotation.y);// rotation.x);
rearCamera->setTransform(rearCameraPosition, rearCameraRotation);
viewMatrix = _viewMatrix;
}