2020-09-29 13:47:50 +02:00
|
|
|
#include "./texture.hpp"
|
|
|
|
|
|
|
|
#ifdef MM_OPENGL_3_GLES
|
|
|
|
#include <GLES3/gl3.h>
|
|
|
|
#else
|
|
|
|
#include <glad/glad.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MM::OpenGL {
|
|
|
|
|
|
|
|
uint32_t Texture::getHandle(void) const {
|
|
|
|
return _handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture::Texture(
|
|
|
|
uint32_t handle,
|
2021-06-14 17:44:59 +02:00
|
|
|
int32_t width_, int32_t height_,
|
2022-02-16 19:22:25 +01:00
|
|
|
int32_t internalFormat, int32_t format, int32_t type,
|
2022-02-17 16:27:49 +01:00
|
|
|
uint32_t samples_
|
2021-06-14 17:44:59 +02:00
|
|
|
) : _handle(handle), width(width_), height(height_),
|
2022-02-17 16:27:49 +01:00
|
|
|
samples(samples_),
|
|
|
|
_internalFormat(internalFormat), _format(format), _type(type) {
|
2022-02-16 19:22:25 +01:00
|
|
|
}
|
2020-09-29 13:47:50 +02:00
|
|
|
|
|
|
|
Texture::~Texture(void) {
|
|
|
|
glDeleteTextures(1, &_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::unbind(void) const {
|
2022-02-16 19:22:25 +01:00
|
|
|
// TODO: do i need ms variant?
|
2020-09-29 13:47:50 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::bind(uint32_t slot) const {
|
|
|
|
glActiveTexture(GL_TEXTURE0 + slot);
|
2022-02-17 16:27:49 +01:00
|
|
|
if (samples == 0) {
|
2022-02-16 19:22:25 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D, _handle);
|
|
|
|
} else {
|
2022-02-18 17:21:53 +01:00
|
|
|
#ifndef MM_OPENGL_3_GLES
|
2022-02-16 19:22:25 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _handle);
|
2022-02-18 17:21:53 +01:00
|
|
|
#else
|
|
|
|
assert(false && "GLES has no multisampling support");
|
|
|
|
#endif
|
2022-02-16 19:22:25 +01:00
|
|
|
}
|
2020-09-29 13:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::resize(int32_t new_width, int32_t new_height) {
|
|
|
|
// if (glTexImage2D == true)
|
|
|
|
|
2022-02-17 16:27:49 +01:00
|
|
|
if (samples == 0) {
|
2022-02-16 19:22:25 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D, _handle);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, _internalFormat, new_width, new_height, 0, _format, _type, NULL);
|
|
|
|
} else {
|
2022-02-18 17:21:53 +01:00
|
|
|
#ifndef MM_OPENGL_3_GLES
|
2022-02-16 19:22:25 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _handle);
|
2022-02-17 16:27:49 +01:00
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, _internalFormat, new_width, new_height, 0);
|
2022-02-18 17:21:53 +01:00
|
|
|
#else
|
|
|
|
assert(false && "GLES has no multisampling support");
|
|
|
|
#endif
|
2022-02-16 19:22:25 +01:00
|
|
|
}
|
2020-09-29 13:47:50 +02:00
|
|
|
|
|
|
|
// HACK: super dirty
|
|
|
|
*(const_cast<int32_t*>(&width)) = new_width;
|
|
|
|
*(const_cast<int32_t*>(&height)) = new_height;
|
|
|
|
}
|
|
|
|
|
2021-06-14 17:44:59 +02:00
|
|
|
Texture::handle_t Texture::createEmpty(int32_t internalFormat, int32_t width, int32_t height, int32_t format, int32_t type) {
|
2020-09-29 13:47:50 +02:00
|
|
|
uint32_t id;
|
|
|
|
glGenTextures(1, &id);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, id);
|
|
|
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, NULL);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
2021-06-14 17:44:59 +02:00
|
|
|
return handle_t(new Texture(id, width, height, internalFormat, format, type));
|
2020-09-29 13:47:50 +02:00
|
|
|
}
|
|
|
|
|
2022-02-16 19:22:25 +01:00
|
|
|
Texture::handle_t Texture::createEmptyMultiSampled(int32_t internalFormat, int32_t width, int32_t height, uint32_t samples) {
|
2022-02-18 17:21:53 +01:00
|
|
|
#ifndef MM_OPENGL_3_GLES
|
2022-02-16 19:22:25 +01:00
|
|
|
uint32_t id;
|
|
|
|
glGenTextures(1, &id);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, id);
|
|
|
|
|
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, internalFormat, width, height, GL_TRUE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
|
|
|
|
|
|
|
|
// HACK: format + type?
|
|
|
|
return handle_t(new Texture(id, width, height, internalFormat, 0, 0, samples));
|
2022-02-18 17:21:53 +01:00
|
|
|
#else
|
|
|
|
(void)internalFormat;
|
|
|
|
(void)width;
|
|
|
|
(void)height;
|
|
|
|
(void)samples;
|
|
|
|
assert(false && "GLES has no multisampling support");
|
2023-03-04 18:57:44 +01:00
|
|
|
return nullptr;
|
2022-02-18 17:21:53 +01:00
|
|
|
#endif
|
2022-02-16 19:22:25 +01:00
|
|
|
}
|
|
|
|
|
2020-09-29 13:47:50 +02:00
|
|
|
} // MM::OpenGL
|
|
|
|
|