I think transformations should somewhat work now
This commit is contained in:
parent
a9f169deba
commit
83b5453088
|
@ -70,6 +70,8 @@ int main(int argc, char** argv)
|
||||||
square.Move(-0.6f, 0.0f, 0.0f);
|
square.Move(-0.6f, 0.0f, 0.0f);
|
||||||
square2.Move(0.6f, 0.0f, 0.0f);
|
square2.Move(0.6f, 0.0f, 0.0f);
|
||||||
|
|
||||||
|
square.Rotate(0.0f, 0.0f, 45.0f);
|
||||||
|
|
||||||
// Create a shader
|
// Create a shader
|
||||||
oglu::Shader shader;
|
oglu::Shader shader;
|
||||||
try
|
try
|
||||||
|
@ -86,10 +88,14 @@ int main(int argc, char** argv)
|
||||||
oglu::Texture crate = oglu::MakeTexture("assets/crate.jpg");
|
oglu::Texture crate = oglu::MakeTexture("assets/crate.jpg");
|
||||||
oglu::Texture opengl = oglu::MakeTexture("assets/opengl.png");
|
oglu::Texture opengl = oglu::MakeTexture("assets/opengl.png");
|
||||||
|
|
||||||
oglu::Camera camera;
|
glm::mat4 view(1.0f);
|
||||||
camera.Move(0.0f, 0.0f, -5.0f);
|
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -5.0f));
|
||||||
|
|
||||||
|
glm::mat4 projection(1.0f);
|
||||||
|
projection = glm::perspective(45.0f, 1.0f, 0.1f, 100.0f);
|
||||||
|
|
||||||
// Window loop
|
// Window loop
|
||||||
|
oglu::Enable(GL_DEPTH_TEST);
|
||||||
float t = 0.0f;
|
float t = 0.0f;
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window))
|
||||||
{
|
{
|
||||||
|
@ -97,16 +103,17 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
oglu::ClearScreen(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f));
|
oglu::ClearScreen(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f));
|
||||||
|
|
||||||
square.Rotate(6.0f, 0.0f, 0.0f);
|
view = glm::rotate(view, glm::radians(1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
square2.Rotate(-6.0f, 0.0f, 0.0f);
|
|
||||||
camera.Rotate(0.0f, 1.0f, 0.0f);
|
// camera.Rotate(0.0f, 1.0f, 0.0f);
|
||||||
|
// camera.Pan(1.f);
|
||||||
|
|
||||||
shader->Use();
|
shader->Use();
|
||||||
shader->SetUniform("texture1", crate, 0);
|
shader->SetUniform("texture1", crate, 0);
|
||||||
shader->SetUniform("texture2", opengl, 1);
|
shader->SetUniform("texture2", opengl, 1);
|
||||||
shader->SetUniform("model", square);
|
shader->SetUniform("model", square);
|
||||||
shader->SetUniform("view", camera);
|
shader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(view));
|
||||||
shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, camera.GetProjectionMatrix());
|
shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(projection));
|
||||||
|
|
||||||
square.Render();
|
square.Render();
|
||||||
|
|
||||||
|
|
|
@ -71,9 +71,9 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
oglu::Enable(GL_DEPTH_TEST);
|
oglu::Enable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
oglu::Camera camera(45.0f, 1.0f, 0.1f, 100.0f);
|
// oglu::Camera camera(45.0f, 1.0f, 0.1f, 100.0f);
|
||||||
camera.Move(0.0f, -5.0f, -10.0f);
|
//camera.Move(0.0f, -5.0f, -10.0f);
|
||||||
camera.LookAt(glm::value_ptr(glm::make_vec3(utah.GetPosition()) + glm::vec3(0.0f, 2.0f, 0.0f)));
|
//camera.LookAt(glm::value_ptr(glm::make_vec3(utah.GetPosition()) + glm::vec3(0.0f, 2.0f, 0.0f)));
|
||||||
|
|
||||||
float t = 0.0f;
|
float t = 0.0f;
|
||||||
|
|
||||||
|
@ -88,8 +88,8 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
shader->Use();
|
shader->Use();
|
||||||
shader->SetUniform("model", utah);
|
shader->SetUniform("model", utah);
|
||||||
shader->SetUniform("view", camera);
|
//shader->SetUniform("view", camera);
|
||||||
shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, camera.GetProjectionMatrix());
|
//shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, camera.GetProjectionMatrix());
|
||||||
|
|
||||||
oglu::PolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
oglu::PolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
utah.Render();
|
utah.Render();
|
||||||
|
|
|
@ -1,118 +0,0 @@
|
||||||
/*****************************************************************//**
|
|
||||||
* \file camera.hpp
|
|
||||||
* \brief Contains anything relevant to cameras
|
|
||||||
*
|
|
||||||
* \author Lauchmelder
|
|
||||||
* \date January 2021
|
|
||||||
*********************************************************************/
|
|
||||||
#ifndef CAMERA_HPP
|
|
||||||
#define CAMERA_HPP
|
|
||||||
|
|
||||||
#include <core.hpp>
|
|
||||||
#include <transformable.hpp>
|
|
||||||
|
|
||||||
namespace oglu
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @brief A camera object in 3D space.
|
|
||||||
*
|
|
||||||
* This class unites a world- and projection matrix.
|
|
||||||
*/
|
|
||||||
class OGLU_API Camera : public Transformable
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* @brief Create a default camera.
|
|
||||||
*
|
|
||||||
* Sits at the origin, looks in negative z-direction.
|
|
||||||
* FOV: 45.0°
|
|
||||||
* Aspect ratio: Matches the viewport
|
|
||||||
* zNear: 0.1f
|
|
||||||
* zFar: 100.0f
|
|
||||||
*/
|
|
||||||
Camera();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Copy another camera.
|
|
||||||
*
|
|
||||||
* Every property of @p other is copied.
|
|
||||||
*
|
|
||||||
* @param[in] other The camera to copy from
|
|
||||||
*/
|
|
||||||
Camera(const Camera& other);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Create a new camera.
|
|
||||||
*
|
|
||||||
* Sits at the origin, looks in negative z-direction.
|
|
||||||
* The other properties are set from the parameters
|
|
||||||
*
|
|
||||||
* @param[in] fov The FOV (field of view) of the camera
|
|
||||||
* @param[in] aspectRatio The aspect ratio of the camera. (Setting this to 0.f will use your viewport's aspect ratio)
|
|
||||||
* @param[in] zNear Near z clipping plane
|
|
||||||
* @param[in] zNear Far z clipping plane
|
|
||||||
*/
|
|
||||||
Camera(float fov, float aspectRatio, float zNear, float zFar);
|
|
||||||
|
|
||||||
~Camera();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Have camera face at a certain position.
|
|
||||||
*
|
|
||||||
* This will adjust the camera's rotation in order to put the
|
|
||||||
* specified coordinate at the center of the screen.
|
|
||||||
*
|
|
||||||
* @param[in] x Target x coordinate
|
|
||||||
* @param[in] y Target y coordinate
|
|
||||||
* @param[in] z Target z coordinate
|
|
||||||
*/
|
|
||||||
void LookAt(GLfloat x, GLfloat y, GLfloat z);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Have camera face at a certain position.
|
|
||||||
*
|
|
||||||
* This will adjust the camera's rotation in order to put the
|
|
||||||
* specified coordinate at the center of the screen.
|
|
||||||
*
|
|
||||||
* @param[in] target 3D vector with the target position
|
|
||||||
*/
|
|
||||||
void LookAt(const GLfloat* target);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Have camera face at a certain position.
|
|
||||||
*
|
|
||||||
* This will adjust the camera's rotation in order to put the
|
|
||||||
* specified coordinate at the center of the screen.
|
|
||||||
*
|
|
||||||
* @param[in] target 3D vector with the target position
|
|
||||||
*/
|
|
||||||
void LookAt(const glm::vec3& target);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Have camera face at a certain position.
|
|
||||||
*
|
|
||||||
* This will adjust the camera's rotation in order to put the
|
|
||||||
* specified coordinate at the center of the screen.
|
|
||||||
*
|
|
||||||
* @param[in] target An object to target
|
|
||||||
*/
|
|
||||||
void LookAt(const Transformable& target);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Gets the projection matrix of the camera.
|
|
||||||
*
|
|
||||||
* @returns a 4x4 projection matrix.
|
|
||||||
*/
|
|
||||||
const float* GetProjectionMatrix();
|
|
||||||
|
|
||||||
private:
|
|
||||||
float aspectRatio; ///< Aspect ration of the camera
|
|
||||||
float fov; ///< FOV of the camera
|
|
||||||
float zNear; ///< Near z clipping plane
|
|
||||||
float zFar; ///< Far z clipping plane
|
|
||||||
|
|
||||||
float* projection;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -13,7 +13,6 @@
|
||||||
#include <shader.hpp>
|
#include <shader.hpp>
|
||||||
#include <texture.hpp>
|
#include <texture.hpp>
|
||||||
#include <object.hpp>
|
#include <object.hpp>
|
||||||
#include <camera.hpp>
|
|
||||||
|
|
||||||
namespace oglu
|
namespace oglu
|
||||||
{
|
{
|
||||||
|
|
|
@ -334,6 +334,7 @@ namespace oglu
|
||||||
// TODO: Separate translation, rotation and scaling matrices.
|
// TODO: Separate translation, rotation and scaling matrices.
|
||||||
// Combine them only when the user wants the transformation matrix
|
// Combine them only when the user wants the transformation matrix
|
||||||
glm::mat4 transformation;
|
glm::mat4 transformation;
|
||||||
|
bool recalculateMatrix;
|
||||||
|
|
||||||
glm::vec3 scale;
|
glm::vec3 scale;
|
||||||
glm::quat orientation;
|
glm::quat orientation;
|
||||||
|
|
|
@ -1,83 +0,0 @@
|
||||||
#include "camera.hpp"
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
#include <glm/gtx/matrix_decompose.hpp>
|
|
||||||
#include <glm/gtx/quaternion.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 })
|
|
||||||
{
|
|
||||||
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(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)
|
|
||||||
{
|
|
||||||
LookAt(glm::vec3(x, y, z));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Camera::LookAt(const GLfloat* target)
|
|
||||||
{
|
|
||||||
LookAt(glm::make_vec3(target));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Camera::LookAt(const glm::vec3& target)
|
|
||||||
{
|
|
||||||
transformation = glm::lookAt(translation, target, glm::vec3(0.0f, 1.0f, 0.0f));
|
|
||||||
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Camera::LookAt(const Transformable& target)
|
|
||||||
{
|
|
||||||
LookAt(target.GetPosition());
|
|
||||||
}
|
|
||||||
|
|
||||||
const float* Camera::GetProjectionMatrix()
|
|
||||||
{
|
|
||||||
return projection;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,13 +10,13 @@
|
||||||
namespace oglu
|
namespace oglu
|
||||||
{
|
{
|
||||||
oglu::Transformable::Transformable() :
|
oglu::Transformable::Transformable() :
|
||||||
transformation(glm::mat4(1.0f))
|
transformation(glm::mat4(1.0f)), recalculateMatrix(false)
|
||||||
{
|
{
|
||||||
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
|
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
|
||||||
}
|
}
|
||||||
|
|
||||||
Transformable::Transformable(const Transformable& other) :
|
Transformable::Transformable(const Transformable& other) :
|
||||||
transformation(other.transformation)
|
transformation(other.transformation), recalculateMatrix(false)
|
||||||
{
|
{
|
||||||
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
|
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
|
||||||
}
|
}
|
||||||
|
@ -37,9 +37,8 @@ namespace oglu
|
||||||
|
|
||||||
void Transformable::SetPosition(const glm::vec3& position)
|
void Transformable::SetPosition(const glm::vec3& position)
|
||||||
{
|
{
|
||||||
glm::decompose(transformation, scale, orientation, this->translation, skew, perspective);
|
translation = position;
|
||||||
this->translation = translation - this->translation;
|
recalculateMatrix = true;
|
||||||
transformation = glm::translate(transformation, this->translation);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transformable::SetRotation(float rotX, float rotY, float rotZ)
|
void Transformable::SetRotation(float rotX, float rotY, float rotZ)
|
||||||
|
@ -54,9 +53,8 @@ namespace oglu
|
||||||
|
|
||||||
void Transformable::SetRotation(const glm::vec3& rotation)
|
void Transformable::SetRotation(const glm::vec3& rotation)
|
||||||
{
|
{
|
||||||
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
|
orientation = glm::quat(glm::radians(rotation));
|
||||||
orientation = glm::quat(glm::radians(rotation)) * (-orientation);
|
recalculateMatrix = true;
|
||||||
transformation = glm::rotate(transformation, glm::angle(orientation), glm::axis(orientation));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transformable::SetRotation(float angle, float xAxis, float yAxis, float zAxis)
|
void Transformable::SetRotation(float angle, float xAxis, float yAxis, float zAxis)
|
||||||
|
@ -71,9 +69,8 @@ namespace oglu
|
||||||
|
|
||||||
void Transformable::SetRotation(float angle, const glm::vec3& axis)
|
void Transformable::SetRotation(float angle, const glm::vec3& axis)
|
||||||
{
|
{
|
||||||
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
|
orientation = glm::angleAxis(glm::radians(angle), axis);
|
||||||
orientation = glm::angleAxis(glm::radians(angle), axis) * (-orientation);
|
recalculateMatrix = true;
|
||||||
transformation = glm::rotate(transformation, glm::angle(orientation), glm::axis(orientation));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transformable::SetScale(float scaleX, float scaleY, float scaleZ)
|
void Transformable::SetScale(float scaleX, float scaleY, float scaleZ)
|
||||||
|
@ -88,12 +85,8 @@ namespace oglu
|
||||||
|
|
||||||
void Transformable::SetScale(const glm::vec3& scale)
|
void Transformable::SetScale(const glm::vec3& scale)
|
||||||
{
|
{
|
||||||
glm::decompose(transformation, this->scale, orientation, translation, skew, perspective);
|
this->scale = scale;
|
||||||
this->scale = scale / this->scale;
|
recalculateMatrix = true;
|
||||||
if (this->scale.x == INFINITY) this->scale.x = scale.x;
|
|
||||||
if (this->scale.y == INFINITY) this->scale.y = scale.y;
|
|
||||||
if (this->scale.z == INFINITY) this->scale.z = scale.z;
|
|
||||||
transformation = glm::scale(transformation, this->scale);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transformable::Move(float x, float y, float z)
|
void Transformable::Move(float x, float y, float z)
|
||||||
|
@ -108,8 +101,8 @@ namespace oglu
|
||||||
|
|
||||||
void Transformable::Move(const glm::vec3& translation)
|
void Transformable::Move(const glm::vec3& translation)
|
||||||
{
|
{
|
||||||
transformation = glm::translate(transformation, translation);
|
this->translation += translation;
|
||||||
glm::decompose(transformation, scale, orientation, this->translation, skew, perspective);
|
recalculateMatrix = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transformable::Rotate(float rotX, float rotY, float rotZ)
|
void Transformable::Rotate(float rotX, float rotY, float rotZ)
|
||||||
|
@ -124,9 +117,8 @@ namespace oglu
|
||||||
|
|
||||||
void Transformable::Rotate(const glm::vec3& rotation)
|
void Transformable::Rotate(const glm::vec3& rotation)
|
||||||
{
|
{
|
||||||
glm::quat rot = glm::quat(glm::radians(rotation));
|
orientation *= glm::quat(glm::radians(rotation));
|
||||||
transformation = glm::rotate(transformation, glm::angle(rot), glm::axis(rot));
|
recalculateMatrix = true;
|
||||||
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transformable::Rotate(float angle, float xAxis, float yAxis, float zAxis)
|
void Transformable::Rotate(float angle, float xAxis, float yAxis, float zAxis)
|
||||||
|
@ -141,8 +133,8 @@ namespace oglu
|
||||||
|
|
||||||
void Transformable::Rotate(float angle, const glm::vec3& axis)
|
void Transformable::Rotate(float angle, const glm::vec3& axis)
|
||||||
{
|
{
|
||||||
transformation = glm::rotate(transformation, glm::radians(angle), axis);
|
orientation *= glm::angleAxis(glm::radians(angle), axis);
|
||||||
glm::decompose(transformation, scale, orientation, translation, skew, perspective);
|
recalculateMatrix = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transformable::Scale(float scaleX, float scaleY, float scaleZ)
|
void Transformable::Scale(float scaleX, float scaleY, float scaleZ)
|
||||||
|
@ -157,12 +149,21 @@ namespace oglu
|
||||||
|
|
||||||
void Transformable::Scale(const glm::vec3& scale)
|
void Transformable::Scale(const glm::vec3& scale)
|
||||||
{
|
{
|
||||||
transformation = glm::scale(transformation, scale);
|
this->scale += scale;
|
||||||
glm::decompose(transformation, this->scale, orientation, translation, skew, perspective);
|
recalculateMatrix = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const glm::mat4& Transformable::GetMatrix()
|
const glm::mat4& Transformable::GetMatrix()
|
||||||
{
|
{
|
||||||
|
if (recalculateMatrix)
|
||||||
|
{
|
||||||
|
transformation = glm::mat4(1.0f);
|
||||||
|
transformation = glm::translate(transformation, translation);
|
||||||
|
transformation *= glm::toMat4(orientation);
|
||||||
|
transformation = glm::scale(transformation, scale);
|
||||||
|
|
||||||
|
recalculateMatrix = false;
|
||||||
|
}
|
||||||
return transformation;
|
return transformation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue