1 Commits

Author SHA1 Message Date
1ab2377f8e add basic texture 2026-01-04 13:07:56 +01:00
7 changed files with 139 additions and 5 deletions

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,6 @@
#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>
@@ -12,6 +13,7 @@ class EntityRenderer : public Renderer<shader::EntityShader> {
private:
game::WorldPtr m_World;
Model m_EntityModel;
std::unique_ptr<GL::Texture> m_EntityTexture;
public:
EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World);

View File

@@ -107,7 +107,7 @@ Model LoadModel(const std::string& fileName) {
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(fileName, aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_SortByPType |
aiProcess_OptimizeGraph | aiProcess_OptimizeMeshes | aiProcess_GlobalScale);
aiProcess_OptimizeGraph | aiProcess_OptimizeMeshes | aiProcess_GlobalScale | aiProcess_FlipUVs);
if (!scene) {
std::cerr << "[ModelLoader] Failed to load model !\n";

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,4 +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>
@@ -8,8 +10,9 @@ namespace render {
EntityRenderer::EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera), m_World(a_World) {
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() {}
@@ -24,6 +27,7 @@ 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});
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", "assimp")
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", "assimp", "enet6", {public = true})
add_packages("libsdl3", "imgui", "glew", "splib", "zlib", "fpm", "assimp", "enet6", "stb", {public = true})
set_rundir(".")
add_defines("TD_GL_LOADER_GLEW")