render entities

This commit is contained in:
2025-07-17 23:00:16 +02:00
parent b21439718b
commit b788caafa6
8 changed files with 228 additions and 158 deletions

24
src/td/game/Mobs.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include <td/game/Mobs.h>
namespace td {
namespace game {
const MobStats* GetMobStats(MobType type, std::uint8_t level) {
static MobStats stats;
return &stats;
}
const TowerImmunities& GetMobTowerImmunities(MobType type, std::uint8_t level) {
static TowerImmunities ti;
return ti;
}
const EffectImmunities& GetMobEffectImmunities(MobType type, std::uint8_t level) {
static EffectImmunities ei;
return ei;
}
} // namespace game
} // namespace td

View File

@@ -159,9 +159,9 @@ GL::VertexArray LoadTileSelectModel() {
colorVBO.AddVertexAttribPointer(1, 1, 0);
std::vector<unsigned int> indexes(positions.size() / 3, 0);
for (size_t i = 0; i < indexes.size(); i++) {
indexes[i] = i + 1;
}
// for (size_t i = 0; i < indexes.size(); i++) {
// indexes[i] = i + 1;
// }
GL::ElementBuffer indexVBO(indexes);
GL::VertexArray tileSelectVao(std::move(indexVBO));
@@ -211,6 +211,36 @@ RenderData LoadTowerModel(game::TowerPtr tower) {
}
GL::VertexArray LoadMobModel() {
std::vector<float> positions = {
-0.5, 0, -0.5,
-0.5, 0, 0.5,
0.5, 0, -0.5,
0.5, 0, -0.5,
-0.5, 0, 0.5,
0.5, 0, 0.5
};
std::vector<unsigned int> indexes(positions.size() / 3, 0);
// for (size_t i = 0; i < indexes.size(); i++) {
// indexes[i] = i + 1;
// }
GL::ElementBuffer indexVBO(indexes);
GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE);
positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0);
GL::VertexArray mobVao(std::move(indexVBO)); // each pos = 1 color
mobVao.Bind();
mobVao.BindVertexBuffer(std::move(positionVBO));
mobVao.Unbind();
return mobVao;
}
} // namespace WorldLoader

View File

@@ -0,0 +1,26 @@
#include <td/render/renderer/EntityRenderer.h>
#include <td/render/loader/WorldLoader.h>
namespace td {
namespace render {
EntityRenderer::EntityRenderer(Camera& a_Camera, const game::World& a_World) : Renderer(a_Camera), m_World(a_World) {
m_EntityVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::LoadMobModel()));
m_Shader->Start();
m_Shader->SetColorEffect({1, 0, 1});
}
EntityRenderer::~EntityRenderer() {}
void EntityRenderer::Render() {
m_Shader->Start();
for (const auto& mob : m_World.GetMobList()) {
const auto mobCoords = mob->GetCenter();
m_Shader->SetModelPos({mobCoords.GetX(), 1, mobCoords.GetY()});
Renderer::Render(*m_EntityVao);
}
}
} // namespace render
} // namespace td

View File

@@ -53,16 +53,12 @@ static const char vertexSource[] = R"(
#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 textureCoords;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform vec3 modelPosition;
out vec2 pass_textureCoords;
void main(void){
pass_textureCoords = textureCoords;
gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0);
}
)";
@@ -70,16 +66,13 @@ void main(void){
static const char fragmentSource[] = R"(
#version 330
in vec2 pass_textureCoords;
out vec4 out_color;
uniform vec3 ColorEffect;
uniform sampler2D textureSampler;
void main(void){
vec4 color = vec4(ColorEffect, 1.0) * texture(textureSampler, pass_textureCoords);
vec4 color = vec4(ColorEffect, 1.0);
if (color.a <= 0.1)
discard;
@@ -90,31 +83,21 @@ void main(void){
)";
#endif
EntityShader::EntityShader() : ShaderProgram() {}
void EntityShader::LoadShader() {
EntityShader::EntityShader() : CameraShaderProgram() {
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
}
void EntityShader::GetAllUniformLocation() {
CameraShaderProgram::GetAllUniformLocation();
m_LocationColorEffect = static_cast<unsigned int>(GetUniformLocation("ColorEffect"));
m_LocationViewMatrix = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
m_LocationPosition = static_cast<unsigned int>(GetUniformLocation("modelPosition"));
m_LocationProjectionMatrix = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
}
void EntityShader::SetColorEffect(const Vec3f& color) {
LoadVector(m_LocationColorEffect, color);
}
void EntityShader::SetProjectionMatrix(const Mat4f& proj) const {
LoadMat4(m_LocationProjectionMatrix, proj);
}
void EntityShader::SetViewMatrix(const Mat4f& view) const {
LoadMat4(m_LocationViewMatrix, view);
}
void EntityShader::SetModelPos(const Vec3f& pos) const {
LoadVector(m_LocationPosition, pos);
}