Updated Normal Maps and texture Loading

This commit is contained in:
OusmBlueNinja 2025-04-13 12:45:30 -05:00
parent 4ba7e8f888
commit 63f53a2a76
8 changed files with 89 additions and 54 deletions

View File

@ -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<Object>)':
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

View File

@ -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

View File

@ -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: []

View File

@ -1,28 +1,30 @@
#include "SpriteComponent.h"
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include <GL/glew.h>
#include <iostream>
#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<std::string>().empty())
SetTexture(node["texture"].as<std::string>());
if (node["normalMap"] && !node["normalMap"].as<std::string>().empty())
SetNormalMap(node["normalMap"].as<std::string>());
}

View File

@ -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);
};

View File

@ -190,7 +190,6 @@ void DrawInspectorUI(std::shared_ptr<Object> selected)
selected->RemoveComponent<SpriteComponent>();
}
// Camera UI...
if (auto cam = selected->GetComponent<CameraComponent>())
{
ImGui::SeparatorText("Camera Component");

View File

@ -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<Light> 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;

View File

@ -30,6 +30,7 @@ public:
private:
static std::vector<Light> s_Lights;
static GLuint fbo, textureColorBuffer, rbo;
static GLuint defaultNormalMap;
static int width, height;
static int s_DrawCalls;
static GLuint shader, quadVAO, quadVBO;