OpenGL-Practice/Camera.h

93 lines
2.4 KiB
C++

#ifndef CAMERA_H
#define CAMERA_H
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
class Camera
{
public:
glm::vec3 position;
glm::vec3 target;
glm::vec3 direction;
glm::vec3 up;
// Movement
glm::vec3 horizontal_velocity;
glm::vec3 forward_velocity;
glm::vec3 vertical_velocity;
float speed;
glm::vec3 worldUp;
Camera(float speed, glm::vec3 pos)
{
this->speed = speed;
this->vertical_velocity = glm::vec3(0.0f);
this->horizontal_velocity = glm::vec3(0.0f);
this->forward_velocity = glm::vec3(0.0f);
this->position = pos;
this->target = glm::vec3(0.0f, 0.0f, -1.0f);
this->direction = glm::normalize(this->position);
this->worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 right = glm::normalize(glm::cross(up, this->direction));
this->up = glm::cross(this->direction, right);
}
Camera(glm::vec3 pos, glm::vec3 target, float speed)
{
this->speed = speed;
this->vertical_velocity = glm::vec3(0.0f);
this->horizontal_velocity = glm::vec3(0.0f);
this->forward_velocity = glm::vec3(0.0f);
this->position = pos;
this->target = target;
this->direction = glm::normalize(this->position - this->target);
this->worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 right = glm::normalize(glm::cross(up, this->direction));
this->up = glm::cross(this->direction, right);
}
glm::mat4 getView()
{
return glm::lookAt(this->position,
this->target + this->position,
this->worldUp);
}
void move(float time) // time passed in seconds since last move
{
if (this->position.y > 0)
this->vertical_velocity.y -= (9.8 * time); // gravity is 9.8m/s^2 no way! physics reference?
std::cout << "pos: " << this->position.x << " " << this->position.y << " " << this->position.z << std::endl;
glm::vec3 velocity = this->forward_velocity + this->horizontal_velocity + this->vertical_velocity;
glm::vec3 movement = (velocity * time);
this->position += movement;
// Don't want to clip into the ground right
if (this->position.y < 0)
{
this->position.y = 0;
this->vertical_velocity.y = 0;
}
}
};
#endif