create special classes for render objects and 3d objects
This commit is contained in:
parent
80e5f4344c
commit
f7941908ef
|
@ -1,6 +1,6 @@
|
|||
add_executable(visualizer
|
||||
"main.cpp" "Application.cpp"
|
||||
"backend/VertexArrayObject.cpp" "backend/Shader.cpp" "Cuboid.cpp")
|
||||
"backend/VertexArrayObject.cpp" "backend/Shader.cpp" "Cuboid.cpp" "backend/Drawable.cpp" "backend/Transformable.cpp")
|
||||
|
||||
target_sources(visualizer PUBLIC
|
||||
${CMAKE_SOURCE_DIR}/vendor/glad/src/glad.c
|
||||
|
|
|
@ -75,21 +75,15 @@ Cuboid::Cuboid()
|
|||
ShaderManager::GetInstance().Register(CUBOID_ID, shader);
|
||||
}
|
||||
|
||||
model = glm::mat4(1.0);
|
||||
type = PrimitiveType::Lines;
|
||||
|
||||
view = glm::lookAt(glm::vec3(2.0f, 2.0f, -3.5f), glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
|
||||
perspective = glm::perspective(glm::radians(100.0f), 16.0f / 9.0f, 0.01f, 100.0f);
|
||||
}
|
||||
|
||||
void Cuboid::Render()
|
||||
void Cuboid::InitializeShader()
|
||||
{
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
|
||||
shader->Use();
|
||||
shader->SetUniform("model", model);
|
||||
shader->SetUniform("model", transformation);
|
||||
shader->SetUniform("view", view);
|
||||
shader->SetUniform("perspective", perspective);
|
||||
|
||||
vao->Render(GL_LINES);
|
||||
}
|
||||
|
|
|
@ -1,24 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include "backend/VertexArrayObject.hpp"
|
||||
#include "backend/Shader.hpp"
|
||||
#include "backend/Drawable.hpp"
|
||||
#include "backend/Transformable.hpp"
|
||||
|
||||
/**
|
||||
* A cuboid that sits at a position and expands into all
|
||||
* three spatial directions
|
||||
*/
|
||||
class Cuboid
|
||||
class Cuboid :
|
||||
public Drawable, public Transformable
|
||||
{
|
||||
public:
|
||||
Cuboid();
|
||||
|
||||
void Render();
|
||||
void InitializeShader() override;
|
||||
|
||||
private:
|
||||
VertexArrayObject vao;
|
||||
Shader shader;
|
||||
|
||||
// TODO: Remove view and projection matrices from cube class
|
||||
glm::mat4 model, view, perspective;
|
||||
glm::mat4 view, perspective;
|
||||
};
|
13
src/backend/Drawable.cpp
Normal file
13
src/backend/Drawable.cpp
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "Drawable.hpp"
|
||||
|
||||
void Drawable::Render()
|
||||
{
|
||||
shader->Use();
|
||||
InitializeShader();
|
||||
vao->Render(static_cast<unsigned int>(type));
|
||||
}
|
||||
|
||||
void Drawable::SetPrimitiveType(PrimitiveType type)
|
||||
{
|
||||
this->type = type;
|
||||
}
|
36
src/backend/Drawable.hpp
Normal file
36
src/backend/Drawable.hpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include "VertexArrayObject.hpp"
|
||||
#include "Shader.hpp"
|
||||
|
||||
enum class PrimitiveType
|
||||
{
|
||||
Lines = GL_LINES,
|
||||
LineStrip = GL_LINE_STRIP,
|
||||
LineLoop = GL_LINE_LOOP,
|
||||
|
||||
Triangles = GL_TRIANGLES,
|
||||
TriangleStrip = GL_TRIANGLE_STRIP,
|
||||
TriangleFan = GL_TRIANGLE_FAN
|
||||
};
|
||||
|
||||
class Drawable
|
||||
{
|
||||
public:
|
||||
Drawable(const Drawable& other) = delete;
|
||||
void operator=(const Drawable& other) = delete;
|
||||
|
||||
virtual void InitializeShader() = 0;
|
||||
void Render();
|
||||
void SetPrimitiveType(PrimitiveType type);
|
||||
|
||||
protected:
|
||||
Drawable() {}
|
||||
|
||||
protected:
|
||||
VertexArrayObject vao;
|
||||
Shader shader;
|
||||
|
||||
PrimitiveType type = PrimitiveType::Triangles;
|
||||
};
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
class AbstractShader
|
||||
{
|
||||
|
@ -21,6 +22,8 @@ public:
|
|||
|
||||
private:
|
||||
unsigned int id;
|
||||
|
||||
bool recording = false;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<AbstractShader> Shader;
|
||||
|
|
72
src/backend/Transformable.cpp
Normal file
72
src/backend/Transformable.cpp
Normal file
|
@ -0,0 +1,72 @@
|
|||
#include "Transformable.hpp"
|
||||
|
||||
Transformable::Transformable() :
|
||||
position(0.0f), scale(1.0f), orientation(0.0, 0.0, 0.0, 0.0)
|
||||
{
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
const glm::vec3& Transformable::GetPosition()
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
void Transformable::SetPosition(const glm::vec3& pos)
|
||||
{
|
||||
position = pos;
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
void Transformable::Move(const glm::vec3& direction)
|
||||
{
|
||||
position += direction;
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
const glm::vec3 Transformable::GetRotation()
|
||||
{
|
||||
return glm::eulerAngles(orientation);
|
||||
}
|
||||
|
||||
void Transformable::SetRotation(const glm::vec3& axis, float angle)
|
||||
{
|
||||
orientation = glm::quat(glm::radians(angle), axis);
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
void Transformable::SetRotation(const glm::vec3& eulerAngles)
|
||||
{
|
||||
orientation = glm::quat(eulerAngles);
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
void Transformable::Rotate(const glm::vec3& axis, float angle)
|
||||
{
|
||||
orientation = glm::rotate(orientation, glm::radians(angle), axis);
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
const glm::vec3& Transformable::SetScale()
|
||||
{
|
||||
return scale;
|
||||
}
|
||||
|
||||
void Transformable::SetScale(const glm::vec3& scale)
|
||||
{
|
||||
this->scale = scale;
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
void Transformable::Scale(const glm::vec3& factor)
|
||||
{
|
||||
this->scale *= scale; // I pray this is component-wise multiplication
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
|
||||
void Transformable::CalculateTransformationMatrix()
|
||||
{
|
||||
transformation = glm::mat4(1.0f);
|
||||
glm::scale(transformation, scale);
|
||||
transformation *= glm::toMat4(orientation);
|
||||
glm::translate(transformation, position);
|
||||
}
|
32
src/backend/Transformable.hpp
Normal file
32
src/backend/Transformable.hpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
|
||||
class Transformable
|
||||
{
|
||||
public:
|
||||
Transformable();
|
||||
|
||||
const glm::vec3& GetPosition();
|
||||
void SetPosition(const glm::vec3& pos);
|
||||
void Move(const glm::vec3& direction);
|
||||
|
||||
const glm::vec3 GetRotation();
|
||||
void SetRotation(const glm::vec3& axis, float angle);
|
||||
void SetRotation(const glm::vec3& eulerAngles);
|
||||
void Rotate(const glm::vec3& axis, float angle);
|
||||
|
||||
const glm::vec3& SetScale();
|
||||
void SetScale(const glm::vec3& scale);
|
||||
void Scale(const glm::vec3& factor);
|
||||
|
||||
private:
|
||||
void CalculateTransformationMatrix();
|
||||
|
||||
protected:
|
||||
glm::mat4 transformation;
|
||||
|
||||
glm::vec3 position, scale;
|
||||
glm::quat orientation;
|
||||
};
|
Loading…
Reference in a new issue