#define GLFW_INCLUDE_NONE #include "XCEngine/RHI/OpenGL/OpenGLTexture.h" #include #include #include #include namespace XCEngine { namespace RHI { OpenGLTexture::OpenGLTexture() : m_texture(0) , m_width(0) , m_height(0) , m_channels(0) { } OpenGLTexture::~OpenGLTexture() { Shutdown(); } bool OpenGLTexture::Initialize2D(int width, int height, int channels, const void* data, bool generateMipmap) { glGenTextures(1, &m_texture); glBindTexture(GL_TEXTURE_2D, m_texture); GLenum format = (channels == 4) ? GL_RGBA : GL_RGB; glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); if (generateMipmap) { glGenerateMipmap(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); } else { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); m_width = width; m_height = height; m_channels = channels; glBindTexture(GL_TEXTURE_2D, 0); return true; } bool OpenGLTexture::LoadFromFile(const char* path, bool flipVertical) { stbi_set_flip_vertically_on_load(flipVertical ? 1 : 0); unsigned char* data = stbi_load(path, &m_width, &m_height, &m_channels, 0); if (!data) { std::cout << "Failed to load texture: " << path << std::endl; return false; } bool result = Initialize2D(m_width, m_height, m_channels, data, true); stbi_image_free(data); return result; } void OpenGLTexture::Shutdown() { if (m_texture) { glDeleteTextures(1, &m_texture); m_texture = 0; } } void OpenGLTexture::Bind(int slot) const { glActiveTexture(GL_TEXTURE0 + slot); glBindTexture(GL_TEXTURE_2D, m_texture); } void OpenGLTexture::Unbind() const { glBindTexture(GL_TEXTURE_2D, 0); } } // namespace RHI } // namespace XCEngine