Added LoadTexture function

- Added stb_image for image loading
This commit is contained in:
TheCherno
2025-04-21 16:18:38 +10:00
parent 9e6f7d5e8d
commit 87873485f3
5 changed files with 8039 additions and 1 deletions

View File

@@ -9,10 +9,15 @@ set(SOURCES
Source/Shader.cpp Source/Shader.cpp
Source/Renderer.h Source/Renderer.h
Source/Renderer.cpp Source/Renderer.cpp
vendor/stb/stb_image.h
vendor/stb/stb_image.cpp
) )
# Add the executable target # Add the executable target
add_executable(App ${SOURCES}) add_executable(App ${SOURCES})
target_link_libraries(App glfw) target_link_libraries(App glfw)
target_link_libraries(App glad) target_link_libraries(App glad)
target_link_libraries(App glm) target_link_libraries(App glm)
target_include_directories(App PRIVATE vendor/stb)

View File

@@ -2,6 +2,8 @@
#include <iostream> #include <iostream>
#include "stb_image.h"
Texture CreateTexture(int width, int height) Texture CreateTexture(int width, int height)
{ {
Texture result; Texture result;
@@ -21,6 +23,44 @@ Texture CreateTexture(int width, int height)
return result; return result;
} }
Texture LoadTexture(const std::filesystem::path& path)
{
int width, height, channels;
std::string filepath = path.string();
unsigned char* data = stbi_load(filepath.c_str(), &width, &height, &channels, 0);
if (!data)
{
std::cerr << "Failed to load texture: " << filepath << "\n";
return {};
}
GLenum format = channels == 4 ? GL_RGBA :
channels == 3 ? GL_RGB :
channels == 1 ? GL_RED : 0;
Texture result;
result.Width = width;
result.Height = height;
glCreateTextures(GL_TEXTURE_2D, 1, &result.Handle);
glTextureStorage2D(result.Handle, 1, (format == GL_RGBA ? GL_RGBA8 : GL_RGB8), width, height);
glTextureSubImage2D(result.Handle, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, data);
glTextureParameteri(result.Handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(result.Handle, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureParameteri(result.Handle, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(result.Handle, GL_TEXTURE_WRAP_T, GL_REPEAT);
glGenerateTextureMipmap(result.Handle);
stbi_image_free(data);
return result;
}
Framebuffer CreateFramebufferWithTexture(const Texture texture) Framebuffer CreateFramebufferWithTexture(const Texture texture)
{ {
Framebuffer result; Framebuffer result;

View File

@@ -4,6 +4,8 @@
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <filesystem>
struct Texture struct Texture
{ {
GLuint Handle = 0; GLuint Handle = 0;
@@ -18,6 +20,7 @@ struct Framebuffer
}; };
Texture CreateTexture(int width, int height); Texture CreateTexture(int width, int height);
Texture LoadTexture(const std::filesystem::path& path);
Framebuffer CreateFramebufferWithTexture(const Texture texture); Framebuffer CreateFramebufferWithTexture(const Texture texture);
bool AttachTextureToFramebuffer(Framebuffer& framebuffer, const Texture texture); bool AttachTextureToFramebuffer(Framebuffer& framebuffer, const Texture texture);
void BlitFramebufferToSwapchain(const Framebuffer framebuffer); void BlitFramebufferToSwapchain(const Framebuffer framebuffer);

2
App/vendor/stb/stb_image.cpp vendored Normal file
View File

@@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

7988
App/vendor/stb/stb_image.h vendored Normal file

File diff suppressed because it is too large Load Diff