3 Commits
raylib ... 3D

Author SHA1 Message Date
1ab2377f8e add basic texture 2026-01-04 13:07:56 +01:00
45c9e5180f basic 3D zombie model 2026-01-03 23:33:26 +01:00
19ebe7b64f add pause shortcut 2026-01-03 23:32:02 +01:00
14 changed files with 2031 additions and 12 deletions

1721
assets/zombie.fbx Normal file

File diff suppressed because it is too large Load Diff

BIN
assets/zombie.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 994 B

View File

@@ -21,6 +21,8 @@ class DebugWorldState : public DisplayState {
std::vector<std::unique_ptr<client::Client>> m_FakeClients;
std::shared_ptr<server::FakeSocket> m_ServerSocket;
unsigned int m_PlaySpeed = 1;
public:
DebugWorldState(Display& a_Display);
~DebugWorldState();

View File

@@ -1,10 +1,11 @@
#pragma once
#include <memory>
#include <td/misc/SlotGuard.h>
#include <td/render/Camera.h>
#include <td/render/loader/FbxLoader.h>
#include <td/render/loader/GLLoader.h>
#include <td/render/shader/CameraShaderProgram.h>
#include <td/misc/SlotGuard.h>
namespace td {
namespace render {
@@ -15,6 +16,7 @@ class BasicRenderer {
virtual ~BasicRenderer() {}
void Render(const GL::VertexArray& a_Vao);
void Render(const Model& a_Model);
};
template <typename TShader>

View File

@@ -0,0 +1,19 @@
#pragma once
#include <td/render/loader/GLLoader.h>
#include <memory>
#include <string>
#include <vector>
namespace td {
struct Model {
std::vector<std::unique_ptr<GL::VertexArray>> m_Vaos;
};
namespace ModelLoader {
Model LoadModel(const std::string& fileName);
}
}

View File

@@ -0,0 +1,31 @@
#pragma once
#include <sp/common/NonCopyable.h>
#include <string>
namespace td {
namespace GL {
class Texture : private sp::NonCopyable {
public:
Texture(const char* a_Data, int a_Width, int a_Height, int a_Comp);
Texture(Texture&& a_Other);
~Texture();
void Bind() const;
void Unbind() const;
private:
unsigned int m_ID;
};
} // namespace GL
namespace TextureLoader {
GL::Texture LoadTexture(const std::string& fileName);
}
} // namespace td

View File

@@ -1,5 +1,7 @@
#pragma once
#include <td/render/loader/TextureLoader.h>
#include <td/render/loader/FbxLoader.h>
#include <td/render/Renderer.h>
#include <td/render/shader/EntityShader.h>
#include <td/game/World.h>
@@ -10,7 +12,8 @@ namespace render {
class EntityRenderer : public Renderer<shader::EntityShader> {
private:
game::WorldPtr m_World;
std::unique_ptr<GL::VertexArray> m_EntityVao;
Model m_EntityModel;
std::unique_ptr<GL::Texture> m_EntityTexture;
public:
EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World);

View File

@@ -75,8 +75,8 @@ DebugWorldState::DebugWorldState(Display& a_Display) : DisplayState(a_Display) {
}
void DebugWorldState::Update(float a_Delta) {
m_Server->Update(a_Delta);
m_Client->Update(a_Delta);
m_Server->Update(a_Delta * m_PlaySpeed);
m_Client->Update(a_Delta * m_PlaySpeed);
if (m_ClientState)
m_Renderer.Render(m_ClientState->GetCurrentLerp());
}
@@ -102,6 +102,10 @@ void DebugWorldState::OnKeyDown(SDL_Keycode a_Key) {
m_Client->Update(SECONDS);
break;
case SDLK_P:
m_PlaySpeed = 1 - m_PlaySpeed;
break;
default:
break;
}

View File

@@ -8,10 +8,17 @@ namespace render {
void BasicRenderer::Render(const GL::VertexArray& a_Vao) {
a_Vao.Bind();
glDrawArrays(GL_TRIANGLES, 0, a_Vao.GetVertexCount());
// glDrawElements(GL_TRIANGLES, a_Vao.GetVertexCount(), GL_UNSIGNED_INT, nullptr);
a_Vao.Unbind();
}
void BasicRenderer::Render(const Model& a_Model) {
for (const auto& vao : a_Model.m_Vaos) {
vao->Bind();
glDrawElements(GL_TRIANGLES, vao->GetVertexCount(), GL_UNSIGNED_INT, nullptr);
vao->Unbind();
}
}
RenderPipeline::RenderPipeline() {
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);

View File

@@ -0,0 +1,128 @@
#include <td/render/loader/FbxLoader.h>
#include <assimp/Importer.hpp> // C++ importer interface
#include <assimp/postprocess.h> // Post processing flags
#include <assimp/scene.h> // Output data structure
#include <iostream>
#include <memory>
#include <sp/common/DataBuffer.h>
#include <td/render/loader/GLLoader.h>
#include <vector>
#define VERTEX_SIZE 3
#define UV_SIZE 2
#define VERTEX_POSITION_INDEX 0
#define VERTEX_UV_INDEX 1
#define VERTEX_NORMAL_INDEX 2
namespace td {
namespace ModelLoader {
static std::unique_ptr<GL::VertexArray> ProcessMesh(aiMesh* mesh, const aiScene* scene, const aiMatrix4x4& transform) {
std::vector<float> positions;
std::vector<float> textureCoords;
std::vector<float> normals;
std::vector<unsigned int> indicies;
aiFace* faces = mesh->mFaces;
std::size_t faceNumber = mesh->mNumFaces;
for (std::size_t j = 0; j < faceNumber; j++) {
std::size_t offset = indicies.size();
std::size_t numIndices = faces[j].mNumIndices;
indicies.resize(indicies.size() + numIndices);
std::memcpy(indicies.data() + offset, faces[j].mIndices, numIndices * sizeof(unsigned int));
}
std::size_t vertNumber = mesh->mNumVertices;
aiVector3D* vertecies = mesh->mVertices;
for (std::size_t j = 0; j < vertNumber; j++) {
aiVector3D vertex = transform * vertecies[j];
positions.push_back(vertex.x);
positions.push_back(vertex.y);
positions.push_back(vertex.z);
}
if (mesh->HasNormals()) {
aiVector3D* vertexNormals = mesh->mNormals;
for (std::size_t j = 0; j < vertNumber; j++) {
aiVector3D normal = transform * vertexNormals[j];
normals.push_back(normal.x);
normals.push_back(normal.y);
normals.push_back(normal.z);
}
}
aiVector3D** vertexTexture = mesh->mTextureCoords;
for (std::size_t j = 0; j < vertNumber; j++) {
textureCoords.push_back(vertexTexture[0][j].x);
textureCoords.push_back(vertexTexture[0][j].y);
}
GL::VertexBuffer positionVBO(positions, VERTEX_SIZE);
positionVBO.AddVertexAttribPointer(VERTEX_POSITION_INDEX, VERTEX_SIZE, 0);
GL::VertexBuffer textureVBO(textureCoords, UV_SIZE);
textureVBO.AddVertexAttribPointer(VERTEX_UV_INDEX, UV_SIZE, 0);
GL::VertexBuffer normalVBO(normals, VERTEX_SIZE);
normalVBO.AddVertexAttribPointer(VERTEX_NORMAL_INDEX, VERTEX_SIZE, 0);
auto Vao = std::make_unique<GL::VertexArray>(GL::ElementBuffer{indicies});
Vao->Bind();
Vao->BindVertexBuffer(std::move(positionVBO));
Vao->BindVertexBuffer(std::move(textureVBO));
Vao->BindVertexBuffer(std::move(normalVBO));
Vao->Unbind();
return Vao;
}
static void ProcessNode(
aiNode* node, const aiScene* scene, std::vector<std::unique_ptr<GL::VertexArray>>& meshes, const aiMatrix4x4& transform) {
// recursive
for (unsigned int i = 0; i < node->mNumChildren; i++) {
ProcessNode(node->mChildren[i], scene, meshes, transform * node->mTransformation);
}
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(ProcessMesh(mesh, scene, transform * node->mTransformation));
}
}
Model LoadModel(const std::string& fileName) {
// DataBuffer fileData = utils::AssetsManager::GetAsset(fileName);
sp::DataBuffer fileData;
fileData.ReadFile(fileName);
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(fileName, aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_SortByPType |
aiProcess_OptimizeGraph | aiProcess_OptimizeMeshes | aiProcess_GlobalScale | aiProcess_FlipUVs);
if (!scene) {
std::cerr << "[ModelLoader] Failed to load model !\n";
return {};
}
// utils::LOGD(utils::Format("[ModelLoader]\tModel nodes : %i", scene->mRootNode->mNumMeshes));
Model model;
ProcessNode(scene->mRootNode, scene, model.m_Vaos, {});
std::cout << "Loaded " << model.m_Vaos.size() << " vaos !\n";
return model;
}
} // namespace ModelLoader
} // namespace td

View File

@@ -0,0 +1,89 @@
/*
* TextureLoader.cpp
*
* Created on: 15 nov. 2020
* Author: simon
*/
// #include "render/loader/TextureLoader.h"
// #include "render/loader/stb_image.h"
// #include "render/GL.h"
// #include "misc/Log.h"
// #include "misc/Format.h"
#include <td/render/loader/TextureLoader.h>
#include <stdexcept>
#include <td/misc/Log.h>
#include <utility>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include <td/render/OpenGL.h>
namespace td {
namespace TextureLoader {
GL::Texture LoadTexture(const std::string& fileName) {
int width, height, comp;
unsigned char* image = stbi_load(fileName.c_str(), &width, &height, &comp, STBI_rgb_alpha);
if (image == nullptr) {
utils::LOGE("Erreur lors du chargement de la texture !");
throw std::runtime_error("Failed to load texture");
}
GL::Texture texture(reinterpret_cast<const char*>(image), width, height, comp);
stbi_image_free(image);
// utils::LOGD(utils::format("Texture %s chargée !", fileName.c_str()));
return texture;
}
} // namespace TextureLoader
namespace GL {
Texture::Texture(const char* textureData, int width, int height, int comp) {
glGenTextures(1, &m_ID);
Bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
assert(comp == 3 || comp == 4);
if (comp == 3)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
Unbind();
}
Texture::Texture(Texture&& a_Other) {
std::swap(m_ID, a_Other.m_ID);
}
Texture::~Texture() {
glDeleteTextures(1, &m_ID);
}
void Texture::Bind() const {
glBindTexture(GL_TEXTURE_2D, m_ID);
}
void Texture::Unbind() const {
glBindTexture(GL_TEXTURE_2D, 0);
}
} // namespace GL
} // namespace td

View File

@@ -1,3 +1,6 @@
#include "td/render/Renderer.h"
#include "td/render/loader/TextureLoader.h"
#include <memory>
#include <td/render/renderer/EntityRenderer.h>
#include <td/render/loader/WorldLoader.h>
@@ -6,9 +9,10 @@ namespace td {
namespace render {
EntityRenderer::EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera), m_World(a_World) {
m_EntityVao = std::make_unique<GL::VertexArray>(WorldLoader::LoadMobModel());
m_EntityModel = ModelLoader::LoadModel("assets/zombie.fbx");
m_EntityTexture = std::make_unique<GL::Texture>(TextureLoader::LoadTexture("assets/zombie.png"));
m_Shader->Start();
m_Shader->SetColorEffect({1, 0, 1});
m_Shader->SetColorEffect({1, 1, 1});
}
EntityRenderer::~EntityRenderer() {}
@@ -23,7 +27,8 @@ void EntityRenderer::Render(float a_Lerp) {
float z = Lerp<game::Mob>(*mob, a_Lerp, [](const game::Mob& a_Mob) { return static_cast<float>(a_Mob.m_Position.y); });
m_Shader->SetModelPos({x, .001, z});
Renderer::Render(*m_EntityVao);
m_EntityTexture->Bind();
Renderer::Render(m_EntityModel);
}
}

View File

@@ -53,12 +53,16 @@ static const char vertexSource[] = R"(
#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 texCoords;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform vec3 modelPosition;
out vec2 pass_textureCoords;
void main(void){
pass_textureCoords = texCoords;
gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0);
}
)";
@@ -66,19 +70,23 @@ 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);
vec4 color = vec4(ColorEffect, 1.0) * texture(textureSampler, pass_textureCoords);
if (color.a <= 0.1)
discard;
out_color = color;
}
)";
#endif

View File

@@ -3,7 +3,7 @@ add_rules("mode.debug", "mode.release")
add_repositories("persson-repo https://git.ale-pri.com/Persson-dev/xmake-repo.git")
add_requires("imgui 1.92.0", {configs = {sdl3 = true, opengl3 = true}})
add_requires("libsdl3 3.2.16", "splib 2.3.2", "zlib", "glew", "fpm", "enet6")
add_requires("libsdl3 3.2.16", "splib 2.3.2", "zlib", "glew", "fpm", "enet6", "assimp", "stb")
set_languages("c++20")
@@ -21,7 +21,7 @@ target("Tower-Defense2")
add_includedirs("include", {public = true})
set_kind("binary")
add_files("src/**.cpp")
add_packages("libsdl3", "imgui", "glew", "splib", "zlib", "fpm", "enet6", {public = true})
add_packages("libsdl3", "imgui", "glew", "splib", "zlib", "fpm", "assimp", "enet6", "stb", {public = true})
set_rundir(".")
add_defines("TD_GL_LOADER_GLEW")