Added basic camera functionality

This commit is contained in:
Robert 2021-01-22 19:02:14 +01:00
parent 6c55ba6339
commit 06614dd1b0
7 changed files with 200 additions and 17 deletions

68
src/camera.cpp Normal file
View file

@ -0,0 +1,68 @@
#include "camera.hpp"
#include <algorithm>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
namespace oglu
{
Camera::Camera() :
fov(45.0f), aspectRatio(0.0f), zNear(0.1f), zFar(100.0f), projection(new float[16]{ 0.0f })
{
memcpy(
projection,
glm::value_ptr(glm::perspective(glm::radians(fov), aspectRatio, zNear, zFar)),
16 * sizeof(float)
);
}
Camera::Camera(const Camera& other) :
fov(other.fov), aspectRatio(other.aspectRatio), zNear(other.zNear), zFar(other.zFar), projection(new float[16]{ 0.0f })
{
memcpy(
projection,
other.projection,
16 * sizeof(float)
);
}
Camera::Camera(float fov, float aspectRatio, float zNear, float zFar) :
fov(fov), aspectRatio(aspectRatio), zNear(zNear), zFar(zFar), projection(new float[16]{ 0.0f })
{
if (aspectRatio == 0.0f)
{
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
aspectRatio = (float)viewport[2] / (float)viewport[3];
}
memcpy(
projection,
glm::value_ptr(glm::perspective(glm::radians(fov), aspectRatio, zNear, zFar)),
16 * sizeof(float)
);
}
Camera::~Camera()
{
}
void Camera::LookAt(GLfloat x, GLfloat y, GLfloat z)
{
}
void Camera::LookAt(const GLfloat* target)
{
}
void Camera::LookAt(const Object& target)
{
}
const float* Camera::GetProjectionMatrix()
{
return projection;
}
}

View file

@ -9,7 +9,7 @@
namespace oglu
{
oglu::Transformable::Transformable() :
position(new float[3]{ 0.f }), rotation(new float[16]), scaling(new float[3]{ 1.f, 1.f, 1.f }), calculateMatrix(false)
position(new float[3]{ 0.f }), rotation(new float[16]), scaling(new float[3]{ 1.f, 1.f, 1.f }), transformation(new float[16]), calculateMatrix(false)
{
glm::mat4 identity(1.0f);
memcpy(
@ -17,10 +17,15 @@ namespace oglu
glm::value_ptr(identity),
16 * sizeof(float)
);
memcpy(
transformation,
glm::value_ptr(identity),
16 * sizeof(float)
);
}
Transformable::Transformable(const Transformable& other) :
position(new float[3]), rotation(new float[16]), scaling(new float[3]), calculateMatrix(true)
position(new float[3]), rotation(new float[16]), scaling(new float[3]), transformation(new float[16]), calculateMatrix(true)
{
memcpy(
this->position,
@ -232,15 +237,17 @@ namespace oglu
const float* Transformable::GetMatrix()
{
static glm::mat4 transformation(1.0f);
if (calculateMatrix)
{
transformation = glm::translate(glm::mat4(1.0f), glm::make_vec3(position)) * glm::make_mat4(rotation) * glm::scale(glm::mat4(1.0f), glm::make_vec3(scaling));
memcpy(
transformation,
glm::value_ptr(glm::translate(glm::mat4(1.0f), glm::make_vec3(position)) * glm::make_mat4(rotation) * glm::scale(glm::mat4(1.0f), glm::make_vec3(scaling))),
16 * sizeof(float)
);
calculateMatrix = false;
}
return glm::value_ptr(transformation);
return transformation;
}
const float* Transformable::GetPosition()