106 lines
2.2 KiB
C++
106 lines
2.2 KiB
C++
|
|
#include <glad/glad.h>
|
||
|
|
#include <GLFW/glfw3.h>
|
||
|
|
#include <glm/glm.hpp>
|
||
|
|
#include <glm/gtc/matrix_transform.hpp>
|
||
|
|
#include <glm/gtc/type_ptr.hpp>
|
||
|
|
#include <stb_image.h>
|
||
|
|
|
||
|
|
#include <shader.hpp>
|
||
|
|
#include <object.hpp>
|
||
|
|
|
||
|
|
#include <iostream>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
using glm::vec2;
|
||
|
|
using glm::vec3;
|
||
|
|
using glm::mat4;
|
||
|
|
|
||
|
|
const float PI = 3.14159f;
|
||
|
|
|
||
|
|
enum Direction
|
||
|
|
{
|
||
|
|
FORWARD,
|
||
|
|
RIGHT,
|
||
|
|
BACKWARD,
|
||
|
|
LEFT
|
||
|
|
};
|
||
|
|
|
||
|
|
class Camera
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
vec3 position;
|
||
|
|
vec3 cameraFront;
|
||
|
|
vec3 cameraUp;
|
||
|
|
vec3 direction;
|
||
|
|
vec3 angles;
|
||
|
|
|
||
|
|
Camera()
|
||
|
|
{
|
||
|
|
position = vec3(0, 0, 10);
|
||
|
|
cameraFront = vec3(0, 0, -1);
|
||
|
|
cameraUp = vec3(0, 1, 0);
|
||
|
|
angles = vec3(0, -PI / 2, 0);
|
||
|
|
|
||
|
|
updateDirection();
|
||
|
|
}
|
||
|
|
|
||
|
|
mat4 viewMatrix()
|
||
|
|
{
|
||
|
|
return glm::lookAt(position, position + direction, vec3(0, 1, 0));
|
||
|
|
}
|
||
|
|
|
||
|
|
void setAngles(vec3 angles)
|
||
|
|
{
|
||
|
|
angles = angles;
|
||
|
|
updateDirection();
|
||
|
|
}
|
||
|
|
|
||
|
|
void updateAngles(float deltaX, float deltaY)
|
||
|
|
{
|
||
|
|
angles.x += deltaX;
|
||
|
|
angles.y += deltaY;
|
||
|
|
|
||
|
|
if (angles.x > (PI / 2) - 0.1f) angles.x = (PI / 2) - 0.1f;
|
||
|
|
if (angles.x < -(PI / 2) - 0.1f) angles.x = -((PI / 2) - 0.1f);
|
||
|
|
|
||
|
|
updateDirection();
|
||
|
|
}
|
||
|
|
|
||
|
|
void updateDirection()
|
||
|
|
{
|
||
|
|
direction.x = cos(angles.y) * cos(angles.x);
|
||
|
|
direction.y = sin(angles.x);
|
||
|
|
direction.z = sin(angles.y) * cos(angles.x);
|
||
|
|
direction = glm::normalize(direction);
|
||
|
|
}
|
||
|
|
|
||
|
|
void setPosition(vec3 p)
|
||
|
|
{
|
||
|
|
position = p;
|
||
|
|
}
|
||
|
|
|
||
|
|
void movePosition(vec3 delta)
|
||
|
|
{
|
||
|
|
position += delta;
|
||
|
|
}
|
||
|
|
|
||
|
|
void movePosition(Direction d, float speed)
|
||
|
|
{
|
||
|
|
switch (d)
|
||
|
|
{
|
||
|
|
case static_cast<int>(FORWARD):
|
||
|
|
movePosition(speed * direction);
|
||
|
|
break;
|
||
|
|
case static_cast<int>(BACKWARD):
|
||
|
|
movePosition(-speed * direction);
|
||
|
|
break;
|
||
|
|
case static_cast<int>(RIGHT):
|
||
|
|
movePosition(speed * glm::normalize(glm::cross(direction, cameraUp)));
|
||
|
|
break;
|
||
|
|
case static_cast<int>(LEFT):
|
||
|
|
movePosition(-speed * glm::normalize(glm::cross(direction, cameraUp)));
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|