generated from Persson-dev/OpenGLComputeShader
Added LoadTexture function
- Added stb_image for image loading
This commit is contained in:
@@ -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);
|
||||
Reference in New Issue
Block a user