From 63f53a2a76e8dc1ec5180202714db60e391b7c18 Mon Sep 17 00:00:00 2001 From: OusmBlueNinja <89956790+OusmBlueNinja@users.noreply.github.com> Date: Sun, 13 Apr 2025 12:45:30 -0500 Subject: [PATCH] Updated Normal Maps and texture Loading --- build.log | 15 +++--- imgui.ini | 26 +++++----- src/assets/scenes/test.cene | 16 +++++-- src/src/Components/SpriteComponent.cpp | 66 ++++++++++++++++---------- src/src/Components/SpriteComponent.h | 2 +- src/src/Engine.cpp | 1 - src/src/Renderer.cpp | 16 ++++++- src/src/Renderer.h | 1 + 8 files changed, 89 insertions(+), 54 deletions(-) diff --git a/build.log b/build.log index 08c0d60..4126992 100644 --- a/build.log +++ b/build.log @@ -1,15 +1,14 @@ -[COMPILE] g++ -std=c++20 -Wall -Isrc/include -Isrc/vendor -Isrc/vendor/imgui -IC:/msys64/mingw64/include -MMD -MP -c src\src\Engine.cpp -o src\build\src\Engine.oIn file included from C:/msys64/mingw64/include/yaml-cpp/parser.h:13, +[COMPILE] g++ -std=c++20 -Wall -Isrc/include -Isrc/vendor -Isrc/vendor/imgui -IC:/msys64/mingw64/include -MMD -MP -c src\src\Components\SpriteComponent.cpp -o src\build\src\Components\SpriteComponent.oIn file included from C:/msys64/mingw64/include/yaml-cpp/parser.h:13, from C:/msys64/mingw64/include/yaml-cpp/yaml.h:10, - from src\src\Entitys/Object.h:7, - from src\src\Engine.cpp:2: + from src\src\Components\Component.h:5, + from src\src\Components\SpriteComponent.h:3, + from src\src\Components\SpriteComponent.cpp:1: C:/msys64/mingw64/include/yaml-cpp/dll.h:22:65: note: '#pragma message: Defining YAML_CPP_API for DLL import' 22 | # pragma message( "Defining YAML_CPP_API for DLL import" ) | ^ -src\src\Engine.cpp: In function 'void DrawInspectorUI(std::shared_ptr)': -src\src\Engine.cpp:219:13: warning: unused variable 'type' [-Wunused-variable] - 219 | int type = light->GetType(); - | ^~~~ [LINK] g++ src\build\src\Engine.o src\build\src\main.o src\build\src\Renderer.o src\build\src\Components\CameraComponent.o src\build\src\Components\LightComponent.o src\build\src\Components\SpriteComponent.o src\build\src\Entitys\Object.o src\build\src\utils\FileDialog.o src\build\src\utils\Logging.o src\build\src\utils\Shader.o src\build\src\utils\utils.o src\build\vendor\imgui\imgui.o src\build\vendor\imgui\imgui_demo.o src\build\vendor\imgui\imgui_draw.o src\build\vendor\imgui\imgui_impl_glfw.o src\build\vendor\imgui\imgui_impl_opengl3.o src\build\vendor\imgui\imgui_tables.o src\build\vendor\imgui\imgui_widgets.o -o src\build\app.exe -LC:/msys64/mingw64/lib -lglfw3 -lglew32 -lopengl32 -lgdi32 -lyaml-cpp -lcomdlg32 -lssl -lcrypto -[TIME] Build duration: 6.67s +[TIME] Build duration: 3.58s +[RUN] Executed app.exe successfully. +[TIME] Total runtime: 114.21s diff --git a/imgui.ini b/imgui.ini index 2715f01..cfd5086 100644 --- a/imgui.ini +++ b/imgui.ini @@ -10,24 +10,24 @@ Collapsed=1 [Window][WindowOverViewport_11111111] Pos=0,19 -Size=1280,701 +Size=1920,1158 Collapsed=0 [Window][Inspector] -Pos=873,19 -Size=407,505 +Pos=1408,19 +Size=512,835 Collapsed=0 DockId=0x00000005,0 [Window][Scene Tree] Pos=0,19 -Size=263,701 +Size=263,1158 Collapsed=0 DockId=0x00000001,0 [Window][Viewport] Pos=265,19 -Size=606,412 +Size=1141,869 Collapsed=0 DockId=0x00000007,0 @@ -36,25 +36,25 @@ Size=1280,19 Collapsed=0 [Window][Performance Info] -Pos=873,526 -Size=407,194 +Pos=1408,856 +Size=512,321 Collapsed=0 DockId=0x00000006,0 [Window][Console] -Pos=265,433 -Size=606,287 +Pos=265,890 +Size=1141,287 Collapsed=0 DockId=0x00000008,0 [Docking][Data] -DockSpace ID=0x11111111 Window=0x1BBC0F80 Pos=0,19 Size=1280,701 Split=X - DockNode ID=0x00000003 Parent=0x11111111 SizeRef=1511,1158 Split=X +DockSpace ID=0x11111111 Window=0x1BBC0F80 Pos=0,19 Size=1920,1158 Split=X + DockNode ID=0x00000003 Parent=0x11111111 SizeRef=1406,1158 Split=X DockNode ID=0x00000001 Parent=0x00000003 SizeRef=263,701 HiddenTabBar=1 Selected=0x12EF0F59 - DockNode ID=0x00000002 Parent=0x00000003 SizeRef=1246,701 Split=Y Selected=0xC450F867 + DockNode ID=0x00000002 Parent=0x00000003 SizeRef=1141,701 Split=Y Selected=0xC450F867 DockNode ID=0x00000007 Parent=0x00000002 SizeRef=606,869 CentralNode=1 Selected=0xC450F867 DockNode ID=0x00000008 Parent=0x00000002 SizeRef=606,287 Selected=0xEA83D666 - DockNode ID=0x00000004 Parent=0x11111111 SizeRef=407,1158 Split=Y Selected=0x36DC96AB + DockNode ID=0x00000004 Parent=0x11111111 SizeRef=512,1158 Split=Y Selected=0x36DC96AB DockNode ID=0x00000005 Parent=0x00000004 SizeRef=407,835 HiddenTabBar=1 Selected=0x36DC96AB DockNode ID=0x00000006 Parent=0x00000004 SizeRef=407,321 HiddenTabBar=1 Selected=0x3FC1A724 diff --git a/src/assets/scenes/test.cene b/src/assets/scenes/test.cene index f42b900..89dc2c9 100644 --- a/src/assets/scenes/test.cene +++ b/src/assets/scenes/test.cene @@ -1,6 +1,6 @@ engine_version: 0.1.0 scene_name: test -scene_hash: beb59668150f92192118ae530ea3d931789cec7c937e2620b25516e0f88ac8a7 +scene_hash: c964933c59e665a145e853a714b47d1c39c80a3a91394f974e8e4dd4542e9e82 format_version: 1 objects: - name: Sprite Object @@ -38,7 +38,7 @@ objects: components: - type: SpriteComponent texture: C:\Users\spenc\OneDrive\Pictures\49555.jpg - normalMap: "" + normalMap: C:\Users\spenc\OneDrive\Pictures\images.jpg children: [] - name: Light position: [-5000, -5000] @@ -46,11 +46,19 @@ objects: components: - type: LightComponent color: + - 0.996078432 - 1 - 1 - - 1 - intensity: 10 + intensity: 0.800000012 radius: 100000000 falloff: 0.100000001 type: 0 + children: [] + - name: NewSprite + position: [-667, 194] + layer: -1 + components: + - type: SpriteComponent + texture: C:\Users\spenc\OneDrive\Pictures\ganges_river_pebbles_diff_1k.png + normalMap: C:\Users\spenc\OneDrive\Pictures\ganges_river_pebbles_nor_gl_1k.png children: [] \ No newline at end of file diff --git a/src/src/Components/SpriteComponent.cpp b/src/src/Components/SpriteComponent.cpp index f391e4f..16eace5 100644 --- a/src/src/Components/SpriteComponent.cpp +++ b/src/src/Components/SpriteComponent.cpp @@ -1,28 +1,30 @@ #include "SpriteComponent.h" -#define STB_IMAGE_IMPLEMENTATION +#define STB_IMAGE_IMPLEMENTATION #include #include #include #include "../utils/Logging.h" #include "../utils/utils.h" +SpriteComponent::SpriteComponent(Object *owner) : Component(owner) {} -SpriteComponent::SpriteComponent(Object* owner) : Component(owner) {} -unsigned int SpriteComponent::LoadTexture(const std::string& path) { + +unsigned int SpriteComponent::LoadTexture(const std::string &path, bool updateSize) +{ int w, h, channels; std::string filename = GetFilenameFromPath(path); stbi_set_flip_vertically_on_load(false); - unsigned char* data = stbi_load(path.c_str(), &w, &h, &channels, 4); - if (!data) { + unsigned char *data = stbi_load(path.c_str(), &w, &h, &channels, 4); + if (!data) + { Logger::LogError("Failed to load asset: '%s': %s", filename.c_str(), stbi_failure_reason()); - - texture_loaded = false; return 0; } - size = glm::vec2(w, h); + if (updateSize) + size = glm::vec2(w, h); unsigned int id; glGenTextures(1, &id); @@ -37,48 +39,60 @@ unsigned int SpriteComponent::LoadTexture(const std::string& path) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); stbi_image_free(data); - texture_loaded = true; Logger::LogDebug("Loaded Asset: %s, %d", filename.c_str(), id); - textureID = id; return id; } + + + bool SpriteComponent::HasTexture() { - return texture_loaded; + return texture_loaded; } - -void SpriteComponent::SetTexture(const std::string& path) { - if (path.empty()) return; +void SpriteComponent::SetTexture(const std::string &path) +{ + if (path.empty()) + return; texturePath = path; - LoadTexture(path); + textureID = LoadTexture(path, true); + if (textureID != 0) + { + texture_loaded = true; + } } - -void SpriteComponent::SetNormalMap(const std::string& path) { - if (path.empty()) return; +void SpriteComponent::SetNormalMap(const std::string &path) +{ + if (path.empty()) + return; normalMapPath = path; - LoadTexture(path); + normalMapID = LoadTexture(path, false); } -unsigned int SpriteComponent::GetTextureID() const { +unsigned int SpriteComponent::GetTextureID() const +{ return textureID; } -unsigned int SpriteComponent::GetNormalMapID() const { +unsigned int SpriteComponent::GetNormalMapID() const +{ return normalMapID; } -std::string SpriteComponent::GetTexturePath() const { +std::string SpriteComponent::GetTexturePath() const +{ return texturePath; } -std::string SpriteComponent::GetNormalMapPath() const { +std::string SpriteComponent::GetNormalMapPath() const +{ return normalMapPath; } -void SpriteComponent::Save(YAML::Emitter& out) const { +void SpriteComponent::Save(YAML::Emitter &out) const +{ out << YAML::BeginMap; out << YAML::Key << "type" << YAML::Value << "SpriteComponent"; out << YAML::Key << "texture" << YAML::Value << texturePath; @@ -86,11 +100,11 @@ void SpriteComponent::Save(YAML::Emitter& out) const { out << YAML::EndMap; } -void SpriteComponent::Load(const YAML::Node& node) { +void SpriteComponent::Load(const YAML::Node &node) +{ if (node["texture"] && !node["texture"].as().empty()) SetTexture(node["texture"].as()); if (node["normalMap"] && !node["normalMap"].as().empty()) SetNormalMap(node["normalMap"].as()); } - diff --git a/src/src/Components/SpriteComponent.h b/src/src/Components/SpriteComponent.h index fdfee8b..f974e05 100644 --- a/src/src/Components/SpriteComponent.h +++ b/src/src/Components/SpriteComponent.h @@ -38,5 +38,5 @@ private: unsigned int textureID = 0; unsigned int normalMapID = 0; bool texture_loaded = false; - unsigned int LoadTexture(const std::string& path); + unsigned int LoadTexture(const std::string& path, bool updateSize); }; diff --git a/src/src/Engine.cpp b/src/src/Engine.cpp index 680396f..82adcd0 100644 --- a/src/src/Engine.cpp +++ b/src/src/Engine.cpp @@ -190,7 +190,6 @@ void DrawInspectorUI(std::shared_ptr selected) selected->RemoveComponent(); } - // Camera UI... if (auto cam = selected->GetComponent()) { ImGui::SeparatorText("Camera Component"); diff --git a/src/src/Renderer.cpp b/src/src/Renderer.cpp index d6e66fb..3c62147 100644 --- a/src/src/Renderer.cpp +++ b/src/src/Renderer.cpp @@ -12,6 +12,8 @@ static Shader spriteShader; GLuint Renderer::fbo = 0; GLuint Renderer::textureColorBuffer = 0; +GLuint Renderer::defaultNormalMap = 0; + GLuint Renderer::rbo = 0; GLuint Renderer::quadVAO = 0; GLuint Renderer::quadVBO = 0; @@ -24,6 +26,7 @@ std::vector Renderer::s_Lights; + void Renderer::InitQuad() { float vertices[] = { // pos // uv @@ -73,6 +76,14 @@ void Renderer::Init() { spriteShader.LoadFromFile("src/assets/shaders/sprite.vert", "src/assets/shaders/sprite.frag"); + // Create a 1x1 flat normal map (RGB: 128,128,255) + unsigned char flatNormal[3] = { 128, 128, 255 }; + glGenTextures(1, &defaultNormalMap); + glBindTexture(GL_TEXTURE_2D, defaultNormalMap); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, flatNormal); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + s_DrawCalls = 0; } @@ -161,9 +172,11 @@ void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float z glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, sprite->GetTextureID()); + glActiveTexture(GL_TEXTURE1); if (sprite->GetNormalMapID()) { - glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, sprite->GetNormalMapID()); + } else { + glBindTexture(GL_TEXTURE_2D, defaultNormalMap); } glBindVertexArray(quadVAO); @@ -177,6 +190,7 @@ void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float z + int Renderer::GetDrawCallCount() { return s_DrawCalls; diff --git a/src/src/Renderer.h b/src/src/Renderer.h index 2cbc271..a589db3 100644 --- a/src/src/Renderer.h +++ b/src/src/Renderer.h @@ -30,6 +30,7 @@ public: private: static std::vector s_Lights; static GLuint fbo, textureColorBuffer, rbo; + static GLuint defaultNormalMap; static int width, height; static int s_DrawCalls; static GLuint shader, quadVAO, quadVBO;