make use of TexturedModel

This commit is contained in:
2023-06-28 22:19:18 +02:00
parent 385dcf11d0
commit b985cc7ade
6 changed files with 91 additions and 33 deletions

View File

@@ -16,24 +16,48 @@ struct Camera {
Mat4f InvProjectionMatrix;
float CamDistance = 25.0f;
Vec3f CamPos {0, CamDistance, 0};
Vec2f CamLook {};
Vec3f CamPos{ 0, CamDistance, 0 };
Vec2f CamLook{};
float m_Yaw = -PI / 2.0f;
float m_Pitch = -PI / 2.0f + 0.0000001f;
};
struct Model {
std::unique_ptr<GL::VertexArray> vao;
Vec3f positon;
Vec3f color = { 1, 1, 1 };
};
class TexturedModel {
private:
std::unique_ptr<GL::VertexArray> m_Vao;
std::unique_ptr<GL::Texture> m_Texture;
Vec3f m_Positon;
Vec3f m_Color = { 1, 1, 1 };
public:
REMOVE_COPY(TexturedModel);
TexturedModel(GL::VertexArray&& vao, GL::Texture&& texture);
TexturedModel(TexturedModel&& other);
~TexturedModel() {}
const GL::VertexArray& GetVao() const { return *m_Vao; }
const GL::Texture& GetTexture() const { return *m_Texture; }
Vec3f GetPosition() const { return m_Positon; }
Vec3f GetColor() const { return m_Color; }
void SetPosition(Vec3f newPos) { m_Positon = newPos; }
void SetColor(Vec3f newColor) { m_Color = newColor; }
};
class Renderer {
public:
static constexpr float m_AnimationSpeed = 2.0f;
static constexpr float m_MouseSensitivity = 200.0f;
struct Model {
std::unique_ptr<GL::VertexArray> vao;
std::unique_ptr<GL::Texture> texture;
Vec3f positon;
Vec3f color = { 1, 1, 1 };
};
private:
std::unique_ptr<shader::WorldShader> m_WorldShader;
std::unique_ptr<shader::EntityShader> m_EntityShader;
@@ -42,7 +66,7 @@ private:
Vec3f m_BackgroundColor;
Camera m_Camera {};
Camera m_Camera{};
public:
Renderer();
~Renderer();
@@ -54,6 +78,7 @@ public:
void RenderVAO(const GL::VertexArray& vao);
void RenderModel(const Model& model);
void RenderModel(const TexturedModel& model);
void AddZoom(float zoom);
void SetCamAngularMovement(const Vec2f& mov);

View File

@@ -29,7 +29,8 @@ private:
Renderer* m_Renderer;
game::World* m_World;
std::unique_ptr<GL::VertexArray> m_WorldVao;
std::unique_ptr<Renderer::Model> m_MobModel, m_SelectTileModel;
std::unique_ptr<Model> m_SelectTileModel;
std::unique_ptr<TexturedModel> m_MobModel;
Vec2f m_CamPos;
Vec2f m_CursorPos;
Vec2f m_HoldCursorPos;

View File

@@ -1,11 +1,16 @@
#pragma once
#include "GLLoader.h"
#include "render/Renderer.h"
namespace td {
namespace render {
namespace MobLoader {
GL::VertexArray LoadMobModel();
TexturedModel LoadMobModel();
} // namespace loader
} // namespace MobLoader
} // namespace render
} // namespace td

View File

@@ -69,11 +69,19 @@ void Renderer::RenderModel(const Model& model) {
m_EntityShader->Start();
m_EntityShader->SetModelPos(model.positon);
m_EntityShader->SetColorEffect(model.color);
if (model.texture != nullptr)
model.texture->Bind();
model.vao->Bind();
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(model.vao->GetVertexCount()));
model.vao->Unbind();
}
void Renderer::RenderModel(const TexturedModel& model) {
m_EntityShader->Start();
m_EntityShader->SetModelPos(model.GetPosition());
m_EntityShader->SetColorEffect(model.GetColor());
model.GetTexture().Bind();
model.GetVao().Bind();
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(model.GetVao().GetVertexCount()));
model.GetVao().Unbind();
GL::Texture::Unbind();
}

View File

@@ -29,12 +29,10 @@ void WorldRenderer::LoadModels() {
utils::LOGD("World Created !");
m_WorldVao = std::make_unique<GL::VertexArray>(WorldLoader::LoadWorldModel(m_World));
m_SelectTileModel = std::make_unique<Renderer::Model>();
m_SelectTileModel = std::make_unique<Model>();
m_SelectTileModel->vao = std::make_unique<GL::VertexArray>(WorldLoader::LoadTileSelectModel());
m_MobModel = std::make_unique<Renderer::Model>();
m_MobModel->texture = std::make_unique<GL::Texture>(TextureLoader::LoadTexture("Assets/zombie.png"));
m_MobModel->vao = std::make_unique<GL::VertexArray>(MobLoader::LoadMobModel());
m_MobModel = std::make_unique<TexturedModel>(MobLoader::LoadMobModel());
utils::LOGD(utils::format("Vertex Count : %u", m_WorldVao->GetVertexCount()));
}
@@ -99,8 +97,8 @@ void WorldRenderer::RenderWorld() const {
void WorldRenderer::RenderMobs() const {
for (game::MobPtr mob : m_World->GetMobList()) {
m_MobModel->positon = { mob->GetCenterX(), 0, mob->GetCenterY() };
m_MobModel->color = mob->HasTakenDamage() ? Vec3f{ 1, 0.5, 0.5 } : Vec3f{ 1, 1, 1 };
m_MobModel->SetPosition({ mob->GetCenterX(), 0, mob->GetCenterY() });
m_MobModel->SetColor(mob->HasTakenDamage() ? Vec3f{ 1, 0.5, 0.5 } : Vec3f{ 1, 1, 1 });
m_Renderer->RenderModel(*m_MobModel);
}
}

View File

@@ -1,5 +1,6 @@
#include "Defines.h"
#include "render/loader/MobLoader.h"
#include "render/loader/TextureLoader.h"
#include <fstream>
#include <map>
@@ -7,6 +8,20 @@
#include <algorithm>
namespace td {
namespace render {
TexturedModel::TexturedModel(GL::VertexArray&& vao, GL::Texture&& texture) {
m_Vao = std::make_unique<GL::VertexArray>(std::move(vao));
m_Texture = std::make_unique<GL::Texture>(std::move(texture));
}
TexturedModel::TexturedModel(TexturedModel&& other) {
m_Vao = std::move(other.m_Vao);
m_Texture = std::move(other.m_Texture);
m_Positon = other.m_Positon;
m_Color = other.m_Color;
}
namespace MobLoader {
const static int POSITION_VERTEX_SIZE = 3;
@@ -17,9 +32,9 @@ typedef Vec2f TextureUV;
typedef Vec3f Normal;
struct Index {
int vertexIndex;
int textureIndex;
int normalIndex;
std::size_t vertexIndex;
std::size_t textureIndex;
std::size_t normalIndex;
};
enum DataType : std::uint8_t {
@@ -43,8 +58,8 @@ static DataType GetDataType(const std::string& type) {
return it != Types.end() ? it->second : dt_None;
}
GL::VertexArray LoadMobModel() {
std::ifstream fileStream{ "Assets/zombie.obj" };
static GL::VertexArray LoadMobVao(const std::string& objFile) {
std::ifstream fileStream{ objFile };
std::string line;
std::vector<Vertex> tempVertecies;
@@ -104,20 +119,20 @@ GL::VertexArray LoadMobModel() {
Index tempIndicies[4];
for (size_t i = 0; i < 4; i++){
for (std::size_t i = 0; i < 4; i++){
ss >> tempIndicies[i].vertexIndex;
ss >> tempIndicies[i].textureIndex;
ss >> tempIndicies[i].normalIndex;
}
static const std::vector<int> vertexOrder = {0, 1, 2, 0, 2, 3};
static const std::vector<std::size_t> vertexOrder = {0, 1, 2, 0, 2, 3};
for(int i = 0; i < vertexOrder.size(); i++) {
for(std::size_t i = 0; i < vertexOrder.size(); i++) {
Index& index = tempIndicies[vertexOrder[i]];
int vertexIndex = index.vertexIndex - 1;
int textureIndex = index.textureIndex - 1;
int normalIndex = index.normalIndex - 1;
std::size_t vertexIndex = index.vertexIndex - 1;
std::size_t textureIndex = index.textureIndex - 1;
std::size_t normalIndex = index.normalIndex - 1;
Vertex vertex = tempVertecies[vertexIndex];
TextureUV texture = tempTextureUvs[textureIndex];
@@ -138,7 +153,7 @@ GL::VertexArray LoadMobModel() {
break;
}
case dt_None:
default:
break;
}
}
@@ -153,8 +168,14 @@ GL::VertexArray LoadMobModel() {
mobVao.BindVertexBuffer(positionVBO);
mobVao.BindVertexBuffer(textureVBO);
mobVao.Unbind();
return mobVao;
}
} // namespace loader
TexturedModel LoadMobModel() {
return {LoadMobVao("Assets/zombie.obj"), TextureLoader::LoadTexture("Assets/zombie.png")};
}
} // namespace MobLoader
} // namespace render
} // namespace td