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

@@ -23,17 +23,41 @@ struct Camera {
float m_Pitch = -PI / 2.0f + 0.0000001f; 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 { class Renderer {
public: public:
static constexpr float m_AnimationSpeed = 2.0f; static constexpr float m_AnimationSpeed = 2.0f;
static constexpr float m_MouseSensitivity = 200.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: private:
std::unique_ptr<shader::WorldShader> m_WorldShader; std::unique_ptr<shader::WorldShader> m_WorldShader;
std::unique_ptr<shader::EntityShader> m_EntityShader; std::unique_ptr<shader::EntityShader> m_EntityShader;
@@ -54,6 +78,7 @@ public:
void RenderVAO(const GL::VertexArray& vao); void RenderVAO(const GL::VertexArray& vao);
void RenderModel(const Model& model); void RenderModel(const Model& model);
void RenderModel(const TexturedModel& model);
void AddZoom(float zoom); void AddZoom(float zoom);
void SetCamAngularMovement(const Vec2f& mov); void SetCamAngularMovement(const Vec2f& mov);

View File

@@ -29,7 +29,8 @@ private:
Renderer* m_Renderer; Renderer* m_Renderer;
game::World* m_World; game::World* m_World;
std::unique_ptr<GL::VertexArray> m_WorldVao; 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_CamPos;
Vec2f m_CursorPos; Vec2f m_CursorPos;
Vec2f m_HoldCursorPos; Vec2f m_HoldCursorPos;

View File

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

View File

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

View File

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

View File

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