Added LoadTexture function
- Added stb_image for image loading
This commit is contained in:
@@ -9,10 +9,15 @@ set(SOURCES
|
||||
Source/Shader.cpp
|
||||
Source/Renderer.h
|
||||
Source/Renderer.cpp
|
||||
vendor/stb/stb_image.h
|
||||
vendor/stb/stb_image.cpp
|
||||
)
|
||||
|
||||
# Add the executable target
|
||||
add_executable(App ${SOURCES})
|
||||
|
||||
target_link_libraries(App glfw)
|
||||
target_link_libraries(App glad)
|
||||
target_link_libraries(App glm)
|
||||
|
||||
target_include_directories(App PRIVATE vendor/stb)
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "stb_image.h"
|
||||
|
||||
Texture CreateTexture(int width, int height)
|
||||
{
|
||||
Texture result;
|
||||
@@ -21,6 +23,44 @@ Texture CreateTexture(int width, int height)
|
||||
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 result;
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
struct Texture
|
||||
{
|
||||
GLuint Handle = 0;
|
||||
@@ -18,6 +20,7 @@ struct Framebuffer
|
||||
};
|
||||
|
||||
Texture CreateTexture(int width, int height);
|
||||
Texture LoadTexture(const std::filesystem::path& path);
|
||||
Framebuffer CreateFramebufferWithTexture(const Texture texture);
|
||||
bool AttachTextureToFramebuffer(Framebuffer& framebuffer, const Texture texture);
|
||||
void BlitFramebufferToSwapchain(const Framebuffer framebuffer);
|
||||
2
App/vendor/stb/stb_image.cpp
vendored
Normal file
2
App/vendor/stb/stb_image.cpp
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
7988
App/vendor/stb/stb_image.h
vendored
Normal file
7988
App/vendor/stb/stb_image.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user