This commit is contained in:
2025-07-16 00:32:40 +02:00
parent d1690192db
commit aaf76a3ff0
41 changed files with 2963 additions and 599 deletions

View File

@@ -0,0 +1,37 @@
#pragma once
#include <td/Maths.h>
namespace td {
namespace render {
class Camera {
private:
Mat4f m_ViewMatrix;
Mat4f m_ProjectionMatrix;
Mat4f m_InvViewMatrix;
Mat4f m_InvProjectionMatrix;
float m_CamDistance = 25.0f;
Vec3f m_CamPos{0, m_CamDistance, 0};
Vec2f m_CamLook{};
float m_Yaw = -PI / 2.0f;
float m_Pitch = -PI / 2.0f + 0.0000001f;
public:
const Mat4f& GetViewMatrix() const {
return m_ViewMatrix;
}
const Mat4f& GetProjectionMatrix() const {
return m_ProjectionMatrix;
}
void UpdatePerspective(float a_AspectRatio);
void SetCamPos(const Vec3f& a_NewPos);
};
} // namespace render
} // namespace td

View File

@@ -0,0 +1,8 @@
#pragma once
#ifdef TD_GL_LOADER_GLEW
#include <GL/glew.h>
#else
#include <glbinding/gl/gl.h>
using namespace gl;
#endif

View File

@@ -0,0 +1,47 @@
#pragma once
#include <td/render/Camera.h>
#include <td/render/loader/GLLoader.h>
#include <memory>
namespace td {
namespace render {
class Renderer {
protected:
Camera& m_Camera;
public:
Renderer(Camera& a_Camera) : m_Camera(a_Camera) {}
virtual ~Renderer() {}
virtual void Render() = 0;
void Render(const GL::VertexArray& a_Vao);
};
class RenderPipeline {
private:
std::vector<std::unique_ptr<Renderer>> m_Renderers;
public:
RenderPipeline();
~RenderPipeline() = default;
void AddRenderer(std::unique_ptr<Renderer>&& a_Renderer) {
m_Renderers.push_back(std::move(a_Renderer));
}
void Clear() {
m_Renderers.clear();
}
void Render() {
for (auto& renderer : m_Renderers) {
renderer->Render();
}
}
};
} // namespace render
} // namespace td

View File

@@ -0,0 +1,86 @@
#pragma once
#include <sp/common/NonCopyable.h>
#include <vector>
namespace td {
namespace GL {
struct VertexAttribPointer {
unsigned int m_Index;
unsigned int m_Size;
unsigned int m_Offset;
};
class ElementBuffer : private sp::NonCopyable {
private:
unsigned int m_ID = 0;
std::size_t m_TriangleCount;
public:
ElementBuffer(ElementBuffer&& other) {
std::swap(m_ID, other.m_ID);
m_TriangleCount = other.m_TriangleCount;
}
explicit ElementBuffer(const std::vector<unsigned int>& indicies);
~ElementBuffer();
void Bind() const;
void Unbind() const;
std::size_t GetTriangleCount() const {
return m_TriangleCount;
}
};
class VertexBuffer : private sp::NonCopyable {
private:
unsigned int m_ID = 0, m_DataStride;
std::vector<VertexAttribPointer> m_VertexAttribs;
public:
VertexBuffer(VertexBuffer&& other) {
std::swap(m_ID, other.m_ID);
m_VertexAttribs = std::move(other.m_VertexAttribs);
m_DataStride = other.m_DataStride;
}
VertexBuffer(const std::vector<float>& data, unsigned int stride);
~VertexBuffer();
void Bind() const;
void Unbind() const;
void AddVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset);
void BindVertexAttribs() const;
};
class VertexArray : private sp::NonCopyable {
private:
unsigned int m_ID = 0;
ElementBuffer m_ElementBuffer;
std::vector<VertexBuffer> m_VertexBuffers; // use to destroy vbos when become unused
public:
VertexArray(VertexArray&& other) : m_ElementBuffer(std::move(other.m_ElementBuffer)) {
std::swap(m_ID, other.m_ID);
m_VertexBuffers = std::move(other.m_VertexBuffers);
}
VertexArray(ElementBuffer&& indicies);
~VertexArray();
std::size_t GetVertexCount() const {
return m_ElementBuffer.GetTriangleCount();
}
void BindVertexBuffer(VertexBuffer&& vbo);
void Bind() const;
void Unbind() const;
private:
void BindElementArrayBuffer();
};
} // namespace GL
} // namespace td

View File

@@ -0,0 +1,25 @@
#pragma once
#include <td/game/World.h>
#include <td/render/loader/GLLoader.h>
namespace td {
namespace render {
namespace WorldLoader {
struct RenderData {
std::vector<float> positions;
std::vector<float> colors;
};
GL::VertexArray LoadMobModel();
GL::VertexArray LoadWorldModel(const td::game::World* world);
GL::VertexArray LoadTileSelectModel();
RenderData LoadTowerModel(game::TowerPtr tower);
} // namespace WorldLoader
} // namespace render
} // namespace td

View File

@@ -0,0 +1,25 @@
#pragma once
#include <td/game/World.h>
#include <td/render/Renderer.h>
#include <td/render/shader/WorldShader.h>
#include <td/render/loader/GLLoader.h>
namespace td {
namespace render {
class WorldRenderer : public Renderer {
private:
const game::World& m_World;
shader::WorldShader m_Shader;
std::unique_ptr<GL::VertexArray> m_WorldVao;
public:
WorldRenderer(Camera& a_Camera, const game::World& a_World);
virtual ~WorldRenderer();
virtual void Render() override;
};
} // namespace render
} // namespace td

View File

@@ -0,0 +1,30 @@
#pragma once
#include <td/render/shader/ShaderProgram.h>
namespace td {
namespace shader {
class EntityShader : public ShaderProgram {
private:
unsigned int m_LocationProjectionMatrix = 0;
unsigned int m_LocationViewMatrix = 0;
unsigned int m_LocationPosition = 0;
unsigned int m_LocationColorEffect = 0;
protected:
virtual void GetAllUniformLocation();
public:
EntityShader();
void LoadShader();
void SetColorEffect(const Vec3f& color);
void SetProjectionMatrix(const Mat4f& proj) const;
void SetViewMatrix(const Mat4f& view) const;
void SetModelPos(const Vec3f& pos) const;
};
} // namespace shader
} // namespace td

View File

@@ -0,0 +1,43 @@
#pragma once
#include <td/Maths.h>
#include <td/render/OpenGL.h>
#include <string>
namespace td {
namespace shader {
class ShaderProgram {
public:
ShaderProgram();
virtual ~ShaderProgram();
void Start() const;
void Stop() const;
void LoadProgramFile(const std::string& vertexFile, const std::string& fragmentFile);
void LoadProgram(const std::string& vertexSource, const std::string& fragmentSource);
protected:
virtual void GetAllUniformLocation() = 0;
int GetUniformLocation(const std::string& uniformName) const;
void LoadFloat(unsigned int location, float value) const;
void LoadInt(unsigned int location, int value) const;
void LoadVector(unsigned int location, const Vec2f& vector) const;
void LoadVector(unsigned int location, const Vec3f& vector) const;
void LoadBoolean(unsigned int location, bool value) const;
void LoadMat4(unsigned int location, const Mat4f& mat) const;
void CleanUp() const;
private:
unsigned int m_ProgramID;
unsigned int m_VertexShaderID;
unsigned int m_FragmentShaderID;
unsigned int LoadShaderFromFile(const std::string& file, GLenum type);
unsigned int LoadShader(const std::string& source, GLenum type);
};
} // namespace shader
} // namespace td

View File

@@ -0,0 +1,21 @@
#pragma once
#include <td/render/shader/ShaderProgram.h>
namespace td {
namespace shader {
class WorldShader : public ShaderProgram {
private:
unsigned int m_LocationProjection = 0, m_LocationView = 0;
protected:
void GetAllUniformLocation();
public:
WorldShader();
void SetProjectionMatrix(const Mat4f& proj) const;
void SetViewMatrix(const Mat4f& view) const;
};
} // namespace shader
} // namespace td