203 lines
6.8 KiB
C++
203 lines
6.8 KiB
C++
#include "XCEngine/RHI/OpenGL/OpenGLTexture.h"
|
|
#include "XCEngine/RHI/OpenGL/OpenGLEnums.h"
|
|
#include <glad/glad.h>
|
|
#include <stb_image.h>
|
|
#include <iostream>
|
|
|
|
namespace XCEngine {
|
|
namespace RHI {
|
|
|
|
OpenGLTexture::OpenGLTexture()
|
|
: m_texture(0)
|
|
, m_type(OpenGLTextureType::Texture2D)
|
|
, m_width(0)
|
|
, m_height(0)
|
|
, m_depth(0)
|
|
, m_mipLevels(1)
|
|
, m_channels(0) {
|
|
}
|
|
|
|
OpenGLTexture::~OpenGLTexture() {
|
|
Shutdown();
|
|
}
|
|
|
|
bool OpenGLTexture::Initialize(OpenGLTextureType type, int width, int height, int depth, int mipLevels, OpenGLFormat format, const void* data) {
|
|
m_type = type;
|
|
m_width = width;
|
|
m_height = height;
|
|
m_depth = depth;
|
|
m_mipLevels = mipLevels;
|
|
|
|
unsigned int target = ToOpenGL(type);
|
|
GLint internalFormat;
|
|
GLenum glFormat, glType;
|
|
ToOpenGLFormat(format, internalFormat, glFormat, glType);
|
|
|
|
glGenTextures(1, &m_texture);
|
|
glBindTexture(target, m_texture);
|
|
|
|
if (type == OpenGLTextureType::TextureCube) {
|
|
for (int i = 0; i < 6; i++) {
|
|
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalFormat, width, height, 0, glFormat, glType, data);
|
|
}
|
|
} else if (type == OpenGLTextureType::Texture1D) {
|
|
glTexImage1D(GL_TEXTURE_1D, 0, internalFormat, width, 0, glFormat, glType, data);
|
|
} else if (type == OpenGLTextureType::Texture3D ||
|
|
type == OpenGLTextureType::Texture2DArray ||
|
|
type == OpenGLTextureType::TextureCubeArray) {
|
|
glTexImage3D(target, 0, internalFormat, width, height, depth, 0, glFormat, glType, data);
|
|
} else {
|
|
glTexImage2D(target, 0, internalFormat, width, height, 0, glFormat, glType, data);
|
|
}
|
|
|
|
if (mipLevels > 1) {
|
|
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
} else {
|
|
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
}
|
|
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
const GLint wrapMode = (type == OpenGLTextureType::TextureCube || type == OpenGLTextureType::TextureCubeArray)
|
|
? GL_CLAMP_TO_EDGE
|
|
: GL_REPEAT;
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrapMode);
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrapMode);
|
|
if (type == OpenGLTextureType::Texture3D ||
|
|
type == OpenGLTextureType::Texture2DArray ||
|
|
type == OpenGLTextureType::TextureCube ||
|
|
type == OpenGLTextureType::TextureCubeArray) {
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_R, wrapMode);
|
|
}
|
|
|
|
glBindTexture(target, 0);
|
|
return true;
|
|
}
|
|
|
|
bool OpenGLTexture::Initialize2D(int width, int height, int channels, const void* data, bool generateMipmap) {
|
|
m_channels = channels;
|
|
m_type = OpenGLTextureType::Texture2D;
|
|
m_width = width;
|
|
m_height = height;
|
|
m_depth = 1;
|
|
m_mipLevels = generateMipmap ? 0 : 1;
|
|
|
|
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);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
return true;
|
|
}
|
|
|
|
bool OpenGLTexture::InitializeCubeMap(int size, int mipLevels, OpenGLFormat format, const void* data) {
|
|
m_type = OpenGLTextureType::TextureCube;
|
|
m_width = size;
|
|
m_height = size;
|
|
m_depth = 1;
|
|
m_mipLevels = mipLevels;
|
|
|
|
GLint internalFormat;
|
|
GLenum glFormat, glType;
|
|
ToOpenGLFormat(format, internalFormat, glFormat, glType);
|
|
|
|
glGenTextures(1, &m_texture);
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture);
|
|
|
|
for (int i = 0; i < 6; i++) {
|
|
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalFormat, size, size, 0, glFormat, glType, data);
|
|
}
|
|
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, mipLevels > 1 ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
|
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, 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, STBI_rgb_alpha);
|
|
if (!data) {
|
|
std::cout << "Failed to load texture: " << path << std::endl;
|
|
return false;
|
|
}
|
|
|
|
m_channels = 4;
|
|
bool result = Initialize2D(m_width, m_height, m_channels, data, false);
|
|
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 {
|
|
unsigned int target = ToOpenGL(m_type);
|
|
glActiveTexture(GL_TEXTURE0 + slot);
|
|
glBindTexture(target, m_texture);
|
|
}
|
|
|
|
void OpenGLTexture::Unbind() const {
|
|
unsigned int target = ToOpenGL(m_type);
|
|
glBindTexture(target, 0);
|
|
}
|
|
|
|
void OpenGLTexture::BindImage(int slot, bool read, bool write) const {
|
|
unsigned int target = ToOpenGL(m_type);
|
|
GLenum access = (read && write) ? GL_READ_WRITE : (read ? GL_READ_ONLY : GL_WRITE_ONLY);
|
|
glBindImageTexture(slot, m_texture, 0, GL_FALSE, 0, access, GL_RGBA8);
|
|
}
|
|
|
|
void OpenGLTexture::GenerateMipmap() {
|
|
unsigned int target = ToOpenGL(m_type);
|
|
glBindTexture(target, m_texture);
|
|
glGenerateMipmap(target);
|
|
glBindTexture(target, 0);
|
|
}
|
|
|
|
void OpenGLTexture::SetFiltering(int minFilter, int magFilter) {
|
|
unsigned int target = ToOpenGL(m_type);
|
|
glBindTexture(target, m_texture);
|
|
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter);
|
|
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilter);
|
|
glBindTexture(target, 0);
|
|
}
|
|
|
|
void OpenGLTexture::SetWrapping(int wrapS, int wrapT, int wrapR) {
|
|
unsigned int target = ToOpenGL(m_type);
|
|
glBindTexture(target, m_texture);
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrapS);
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrapT);
|
|
if (wrapR >= 0 &&
|
|
(m_type == OpenGLTextureType::Texture3D ||
|
|
m_type == OpenGLTextureType::Texture2DArray ||
|
|
m_type == OpenGLTextureType::TextureCube ||
|
|
m_type == OpenGLTextureType::TextureCubeArray)) {
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_R, wrapR);
|
|
}
|
|
glBindTexture(target, 0);
|
|
}
|
|
|
|
} // namespace RHI
|
|
} // namespace XCEngine
|