From 91dc1864d3c73d68e279b238d501f410c6974fb5 Mon Sep 17 00:00:00 2001 From: OusmBlueNinja <89956790+OusmBlueNinja@users.noreply.github.com> Date: Thu, 17 Apr 2025 22:09:03 -0500 Subject: [PATCH] fixed normals --- __tmp_scene.yaml | 195 ------------------------- remake/build.log | 2 +- src/assets/scenes/lighting_test_2.cene | 8 +- src/assets/shaders/sprite.frag | 13 +- src/src/Engine.cpp | 12 +- src/src/Renderer.cpp | 67 +++++++++ src/src/Renderer.h | 42 +++++- src/src/utils/EngineConfig.cpp | 3 + src/src/utils/EngineConfig.h | 6 + 9 files changed, 134 insertions(+), 214 deletions(-) delete mode 100644 __tmp_scene.yaml diff --git a/__tmp_scene.yaml b/__tmp_scene.yaml deleted file mode 100644 index e557bc4..0000000 --- a/__tmp_scene.yaml +++ /dev/null @@ -1,195 +0,0 @@ -engine_version: 0.1.0 -scene_name: __tmp_scene -scene_hash: a0262a35199dae8ff5db4383e9e2d30c63c7a681d14f524a60210fde4c9dc71e -format_version: 1 -objects: - - name: Tiles - uid: f5e01f7892874a67b662633650b41dbd - id: 3 - position: [0, 0] - layer: 0 - visable: true - components: [] - children: - - name: Bark - uid: 7dc3bbf8affb4844ae3801f03857b904 - id: 4 - position: [0, 0] - layer: 0 - visable: true - components: - - type: SpriteComponent - texture: C:\Users\spenc\OneDrive\Pictures\textures\bark_willow_02_diff_1k.png - normalMap: C:\Users\spenc\OneDrive\Pictures\textures\bark_willow_02_nor_gl_1k.png - renderType: Lit - children: [] - - name: Planks - uid: 13d8988343354e3c8a1f51c03ed40cda - id: 5 - position: [1024, 0] - layer: 0 - visable: true - components: - - type: SpriteComponent - texture: C:\Users\spenc\OneDrive\Pictures\textures\wood_floor_worn_diff_1k.png - normalMap: C:\Users\spenc\OneDrive\Pictures\textures\wood_floor_worn_nor_gl_1k.png - renderType: Lit - children: [] - - name: Rocks - uid: cff28abe7e3b455ab9b756acc84cd2d7 - id: 6 - position: [0, 1024] - layer: 0 - visable: true - components: - - type: SpriteComponent - texture: C:\Users\spenc\OneDrive\Pictures\textures\ganges_river_pebbles_diff_1k.png - normalMap: C:\Users\spenc\OneDrive\Pictures\textures\ganges_river_pebbles_nor_gl_1k.png - renderType: Lit - children: [] - - name: Metal - uid: 98967eb30e5b429b992766d8062b7c17 - id: 7 - position: [1024, 1025] - layer: 0 - visable: true - components: - - type: SpriteComponent - texture: C:\Users\spenc\OneDrive\Pictures\textures\metal_plate_diff_1k.png - normalMap: C:\Users\spenc\OneDrive\Pictures\textures\metal_plate_nor_gl_1k.png - renderType: Lit - children: [] - - name: Logo - uid: c4ce6f16dfb347b0ae0ac67f5881b243 - id: 8 - position: [2048, 0] - layer: 0 - visable: true - components: - - type: SpriteComponent - texture: C:\Users\spenc\OneDrive\Pictures\blue_logo.png - normalMap: C:\Users\spenc\OneDrive\Pictures\textures\circuits_normal.jpg - renderType: Lit - children: [] - - name: Carbooon Fobar - uid: 5ea269572751401da6d86519d3513b7d - id: 9 - position: [2567, 1545] - layer: 1 - visable: true - components: - - type: SpriteComponent - texture: C:\Users\spenc\OneDrive\Pictures\textures\carbon-fiber-smooth-bl\carbon-fiber-smooth-bl\carbon-fiber_smooth_albedo.png - normalMap: C:\Users\spenc\OneDrive\Pictures\textures\carbon-fiber-smooth-bl\carbon-fiber-smooth-bl\carbon-fiber_smooth_normal-ogl.png - renderType: Lit - children: [] - - name: Mud - uid: a36b71937ba349bd8e6414f75be9ee16 - id: 10 - position: [0, 2561.80005] - layer: 0 - visable: true - components: - - type: SpriteComponent - texture: C:\Users\spenc\OneDrive\Pictures\textures\mud-bl\mud-bl\mud_albedo.png - normalMap: C:\Users\spenc\OneDrive\Pictures\textures\mud-bl\mud-bl\mud_normal-ogl.png - renderType: Lit - children: [] - - name: Lights - uid: 051b338a725a4076ad53ad8fa00c5f4e - id: 12 - position: [-556, 951] - layer: 0 - visable: true - components: [] - children: - - name: Red - uid: 6afde2dd47aa4557b6afb1a607c99dc8 - id: 13 - position: [512, 1024] - layer: 2 - visable: true - components: - - type: LightComponent - color: - - 1 - - 0 - - 0 - intensity: 2 - radius: 1000 - falloff: 0.100000001 - type: 0 - - type: SpriteComponent - texture: C:\Users\spenc\OneDrive\Documents\GitHub\Create-Engine\src\assets\icons\lightbulb-on-10.png - normalMap: "" - renderType: Unlit - children: [] - - name: Green - uid: 0f950d76d24b4dc18f54cab2c3aaaf9a - id: 14 - position: [1024, 512] - layer: 2 - visable: true - components: - - type: LightComponent - color: - - 0 - - 1 - - 0 - intensity: 2 - radius: 1000 - falloff: 1 - type: 0 - - type: SpriteComponent - texture: C:\Users\spenc\OneDrive\Documents\GitHub\Create-Engine\src\assets\icons\lightbulb-on-10.png - normalMap: "" - renderType: Unlit - children: [] - - name: Blue - uid: 09f722f51c7c4b0f98de3a0a16d127c4 - id: 15 - position: [250, 250] - layer: 2 - visable: true - components: - - type: LightComponent - color: - - 0 - - 0 - - 1 - intensity: 2 - radius: 1000 - falloff: 1 - type: 0 - - type: SpriteComponent - texture: C:\Users\spenc\OneDrive\Documents\GitHub\Create-Engine\src\assets\icons\lightbulb-on-10.png - normalMap: "" - renderType: Unlit - children: [] - - name: Ambient light - uid: d4fb425522d84a8cbbd7d1415bcd93df - id: 16 - position: [500, 500] - layer: 0 - visable: true - components: - - type: LightComponent - color: - - 1 - - 1 - - 1 - intensity: 0 - radius: 1000000 - falloff: 1 - type: 0 - children: [] - - name: ScriptHandler - uid: 895c655f3dda4aec9f2a354c1276c53e - id: 14 - position: [0, 0] - layer: 0 - visable: true - components: - - type: ScriptComponent - scriptPath: C:\Users\spenc\OneDrive\Documents\GitHub\Create-Engine\src\assets\lua\test.lua - children: [] \ No newline at end of file diff --git a/remake/build.log b/remake/build.log index a1973d2..f4bcebc 100644 --- a/remake/build.log +++ b/remake/build.log @@ -1,2 +1,2 @@ -[COMPILE] g++ -std=c++20 -Wall -Isrc/include -Isrc/include/lua -Isrc/vendor -Isrc/vendor/imgui -IC:/msys64/mingw64/include -IC:\msys64\mingw64\lib\libyaml-cpp.a -Isrc\vendor\imgui -MMD -MP -c src\src\Engine.cpp -o src\build\Engine.o [LINK] g++ src\build\Engine.o src\build\main.o src\build\Renderer.o src\build\Components\CameraComponent.o src\build\Components\LightComponent.o src\build\Components\ScriptComponent.o src\build\Components\SpriteComponent.o src\build\Components\TextComonent.o src\build\Components\TilemapComponent.o src\build\Entitys\Object.o src\build\utils\EngineConfig.o src\build\utils\ExceptionHandler.o src\build\utils\FileDialog.o src\build\utils\GameObjectsList.o src\build\utils\Logging.o src\build\utils\Shader.o src\build\utils\UID.o src\build\utils\utils.o src\build\imgui\imgui.o src\build\imgui\imgui_demo.o src\build\imgui\imgui_draw.o src\build\imgui\imgui_impl_glfw.o src\build\imgui\imgui_impl_opengl3.o src\build\imgui\imgui_tables.o src\build\imgui\imgui_widgets.o src\build\lapi.o src\build\lauxlib.o src\build\lbaselib.o src\build\lcode.o src\build\lcorolib.o src\build\lctype.o src\build\ldblib.o src\build\ldebug.o src\build\ldo.o src\build\ldump.o src\build\lfunc.o src\build\lgc.o src\build\linit.o src\build\liolib.o src\build\llex.o src\build\lmathlib.o src\build\lmem.o src\build\loadlib.o src\build\lobject.o src\build\lopcodes.o src\build\loslib.o src\build\lparser.o src\build\lstate.o src\build\lstring.o src\build\lstrlib.o src\build\ltable.o src\build\ltablib.o src\build\ltm.o src\build\lua.o src\build\luac.o src\build\lundump.o src\build\lutf8lib.o src\build\lvm.o src\build\lzio.o -o src\build\app.exe -LC:\msys64\mingw64\lib -lglfw3 -lglew32 -lopengl32 -lgdi32 -lyaml-cpp -lcomdlg32 -lssl -lcrypto +[RUN] Executed app.exe successfully. diff --git a/src/assets/scenes/lighting_test_2.cene b/src/assets/scenes/lighting_test_2.cene index 0335b40..e41fdf6 100644 --- a/src/assets/scenes/lighting_test_2.cene +++ b/src/assets/scenes/lighting_test_2.cene @@ -1,6 +1,6 @@ engine_version: 0.1.0 scene_name: lighting_test_2 -scene_hash: a0262a35199dae8ff5db4383e9e2d30c63c7a681d14f524a60210fde4c9dc71e +scene_hash: a0ae96d593e1990a6f3944184fd6595738629af0dcb2d4cdd83011a91aaa738c format_version: 1 objects: - name: Tiles @@ -50,7 +50,7 @@ objects: - name: Metal uid: 98967eb30e5b429b992766d8062b7c17 id: 7 - position: [1024, 1025] + position: [1024, 1024] layer: 0 visable: true components: @@ -74,7 +74,7 @@ objects: - name: Carbooon Fobar uid: 5ea269572751401da6d86519d3513b7d id: 9 - position: [2567, 1545] + position: [2559.30005, 1562] layer: 1 visable: true components: @@ -178,7 +178,7 @@ objects: - 1 - 1 - 1 - intensity: 0 + intensity: 0.0500000007 radius: 1000000 falloff: 1 type: 0 diff --git a/src/assets/shaders/sprite.frag b/src/assets/shaders/sprite.frag index 24e7145..75af612 100644 --- a/src/assets/shaders/sprite.frag +++ b/src/assets/shaders/sprite.frag @@ -9,9 +9,9 @@ uniform sampler2D uNormalMap; #define MAX_LIGHTS 512 -uniform int uLightCount; -uniform vec2 uLightPos[MAX_LIGHTS]; -uniform vec3 uLightColor[MAX_LIGHTS]; +uniform int uLightCount; +uniform vec2 uLightPos[MAX_LIGHTS]; +uniform vec3 uLightColor[MAX_LIGHTS]; uniform float uLightIntensity[MAX_LIGHTS]; uniform float uLightRadius[MAX_LIGHTS]; @@ -21,8 +21,11 @@ void main() if (texColor.a < 0.1) discard; - vec3 normal = texture(uNormalMap, vUV).rgb * 2.0 - 1.0; - normal = normalize(normal); + // unpack normal map and convert from [0,1] to [-1,1] + vec3 n = texture(uNormalMap, vUV).rgb * 2.0 - 1.0; + // invert the green channel for OpenGL + n.y = -n.y; + vec3 normal = normalize(n); vec3 finalLight = vec3(0.0); diff --git a/src/src/Engine.cpp b/src/src/Engine.cpp index 94d5fca..388c8d6 100644 --- a/src/src/Engine.cpp +++ b/src/src/Engine.cpp @@ -395,7 +395,7 @@ void Engine::Run() Logger::LogInfo("[LoadScene] Loading Scene."); if (!file.empty()) LoadScene(file); - selected = nullptr; + selected = nullptr; } ImGui::EndMenu(); } @@ -404,6 +404,8 @@ void Engine::Run() { // simple checkbox never closes the menu when you click it ImGui::Checkbox("Enable Lighting", &g_engineConfig.lighting_enabled); + ImGui::Checkbox("Enable Gizmos", &g_engineConfig.settings.draw_gizmos); + ImGui::EndMenu(); } @@ -542,6 +544,14 @@ void Engine::Run() light->GetColor(), light->GetIntensity(), light->GetRadius() * cameraZoom); + + Renderer::DrawGizmoCircle( + world, + light->GetRadius(), + 64, // segments + light->GetColor(), // circle color + cameraPos, + cameraZoom); } // Collect scripts diff --git a/src/src/Renderer.cpp b/src/src/Renderer.cpp index fed56cf..0f6925f 100644 --- a/src/src/Renderer.cpp +++ b/src/src/Renderer.cpp @@ -294,6 +294,73 @@ void Renderer::DrawEditorGrid(const glm::vec2 &cameraPos, float zoom) glEnd(); } +void Renderer::DrawGizmoLine( + const glm::vec2 &worldStart, + const glm::vec2 &worldEnd, + const glm::vec3 &color, + const glm::vec2 &cameraPos, + float zoom) +{ + // Transform to screen space + glm::vec2 a = (worldStart - cameraPos) * zoom + glm::vec2(width * 0.5f, height * 0.5f); + glm::vec2 b = (worldEnd - cameraPos) * zoom + glm::vec2(width * 0.5f, height * 0.5f); + + glUseProgram(0); + glLineWidth(2.0f); + glColor3f(color.r, color.g, color.b); + + glBegin(GL_LINES); + glVertex2f(a.x, a.y); + glVertex2f(b.x, b.y); + glEnd(); +} + +void Renderer::DrawGizmoRect( + const glm::vec2 &worldPos, + const glm::vec2 &size, + const glm::vec3 &color, + const glm::vec2 &cameraPos, + float zoom) +{ + glm::vec2 p0 = worldPos; + glm::vec2 p1 = worldPos + glm::vec2(size.x, 0.0f); + glm::vec2 p2 = worldPos + size; + glm::vec2 p3 = worldPos + glm::vec2(0.0f, size.y); + + // draw four edges + DrawGizmoLine(p0, p1, color, cameraPos, zoom); + DrawGizmoLine(p1, p2, color, cameraPos, zoom); + DrawGizmoLine(p2, p3, color, cameraPos, zoom); + DrawGizmoLine(p3, p0, color, cameraPos, zoom); +} + +void Renderer::DrawGizmoCircle( + const glm::vec2 &worldCenter, + float radius, + int segments, + const glm::vec3 &color, + const glm::vec2 &cameraPos, + float zoom) +{ + if (segments < 3) + segments = 3; + const float thetaStep = 2.0f * 3.14159265f / float(segments); + + glUseProgram(0); + glLineWidth(2.0f); + glColor3f(color.r, color.g, color.b); + + glBegin(GL_LINE_LOOP); + for (int i = 0; i < segments; ++i) + { + float theta = thetaStep * float(i); + glm::vec2 worldPoint = worldCenter + glm::vec2(std::cos(theta), std::sin(theta)) * radius; + glm::vec2 screenPoint = (worldPoint - cameraPos) * zoom + glm::vec2(width * 0.5f, height * 0.5f); + glVertex2f(screenPoint.x, screenPoint.y); + } + glEnd(); +} + GLuint Renderer::GetRenderTexture() { return textureColorBuffer; diff --git a/src/src/Renderer.h b/src/src/Renderer.h index e1b98bd..44dcfc3 100644 --- a/src/src/Renderer.h +++ b/src/src/Renderer.h @@ -7,10 +7,6 @@ #include "Components/SpriteComponent.h" #include "utils/EngineConfig.h" - - - - struct Light { glm::vec2 screenPos; glm::vec3 color; @@ -18,25 +14,55 @@ struct Light { float radius; }; -static int MAX_LIGHTS = g_engineConfig.gl_maxLight; - class Renderer { public: static void Init(); static void Resize(int w, int h); static void Begin(); static void End(); + static void DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float zoom, glm::vec2& CameraPos); - static void AddLight(const glm::vec2& screenPos, const glm::vec3& color, float intensity, float radius); static void DrawTilemap(TilemapComponent* tilemap, const glm::vec2& worldPos, float zoom, const glm::vec2& cameraPos); + + static void AddLight(const glm::vec2& screenPos, const glm::vec3& color, float intensity, float radius); static void ClearLights(); + static void DrawEditorGrid(const glm::vec2& cameraPos, float zoom); + + // —— New gizmo functions —— + // Draws a colored line between two world‑space points. + static void DrawGizmoLine( + const glm::vec2& worldStart, + const glm::vec2& worldEnd, + const glm::vec3& color, + const glm::vec2& cameraPos, + float zoom + ); + + // Draws a colored axis‑aligned rectangle in world space. + static void DrawGizmoRect( + const glm::vec2& worldPos, + const glm::vec2& size, + const glm::vec3& color, + const glm::vec2& cameraPos, + float zoom + ); + + // Draws a colored circle (approximated by segments) in world space. + static void DrawGizmoCircle( + const glm::vec2& worldCenter, + float radius, + int segments, + const glm::vec3& color, + const glm::vec2& cameraPos, + float zoom + ); + static GLuint GetRenderTexture(); static glm::ivec2 GetSize(); static int GetDrawCallCount(); static int GetLightsCount(); - private: static std::vector s_Lights; static GLuint fbo, textureColorBuffer, rbo; diff --git a/src/src/utils/EngineConfig.cpp b/src/src/utils/EngineConfig.cpp index 1735bed..0d8866d 100644 --- a/src/src/utils/EngineConfig.cpp +++ b/src/src/utils/EngineConfig.cpp @@ -7,5 +7,8 @@ EngineConfig g_engineConfig { .version = "0.1.0", .gl_version = "430", .gl_maxLight = 512, + .settings{ + .draw_gizmos = true, + }, }; diff --git a/src/src/utils/EngineConfig.h b/src/src/utils/EngineConfig.h index 139cb02..fd06044 100644 --- a/src/src/utils/EngineConfig.h +++ b/src/src/utils/EngineConfig.h @@ -1,11 +1,17 @@ #pragma once #include +struct UserSettings{ + bool draw_gizmos; +}; + struct EngineConfig { bool lighting_enabled; std::string version; std::string gl_version; int gl_maxLight; + + UserSettings settings; }; extern EngineConfig g_engineConfig;