2022-11-08 17:16:05 -05:00
|
|
|
|
/* +------------------------------------------------------+
|
|
|
|
|
____/ \____ /| - Open source game framework licensed to freely use, |
|
|
|
|
|
\ / / | copy, modify and sell without restriction |
|
|
|
|
|
+--\ ^__^ /--+ | |
|
|
|
|
|
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
|
|
|
|
|
| ~~~~~~~~~~~~ | +------------------------------------------------------+
|
|
|
|
|
| SPACE ~~~~~ | /
|
|
|
|
|
| ~~~~~~~ BOX |/
|
|
|
|
|
+-------------*/
|
2021-10-18 17:33:33 -04:00
|
|
|
|
|
2021-09-20 02:32:15 -04:00
|
|
|
|
#include "Texture.hpp"
|
2021-10-18 17:33:33 -04:00
|
|
|
|
using namespace sb;
|
2021-09-20 02:32:15 -04:00
|
|
|
|
|
2021-09-24 02:43:38 -04:00
|
|
|
|
/* Have to pass our deleter to abstract base class at instantiation */
|
|
|
|
|
Texture::Texture() : GLObject(texture_deleter) {}
|
|
|
|
|
|
2021-09-20 02:32:15 -04:00
|
|
|
|
/* If an image path is passed at creation, the path will be stored in the class for later loading */
|
2021-09-24 02:43:38 -04:00
|
|
|
|
Texture::Texture(fs::path path) : Texture()
|
2021-09-20 02:32:15 -04:00
|
|
|
|
{
|
|
|
|
|
associate(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Store an image path as a member variable for loading later. Each texture should have one image
|
|
|
|
|
* path (support for multiple mipmap levels may be added later) */
|
|
|
|
|
void Texture::associate(fs::path path)
|
|
|
|
|
{
|
|
|
|
|
this->path = path;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 02:09:49 -04:00
|
|
|
|
/* Forward the GL texture generate function to the base class */
|
2021-09-24 02:43:38 -04:00
|
|
|
|
void Texture::generate()
|
2021-09-20 02:32:15 -04:00
|
|
|
|
{
|
2021-09-28 02:09:49 -04:00
|
|
|
|
GLObject::generate(glGenTextures);
|
2021-09-24 02:43:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 23:30:27 -05:00
|
|
|
|
/* Generate a GL_TEXTURE_2D texture ID and allocate GL_RGBA8 storage for the given size */
|
2021-09-24 02:43:38 -04:00
|
|
|
|
void Texture::generate(glm::vec2 size)
|
|
|
|
|
{
|
|
|
|
|
generate();
|
2021-09-20 02:32:15 -04:00
|
|
|
|
bind();
|
2021-11-09 23:30:27 -05:00
|
|
|
|
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, size.x, size.y);
|
2021-09-20 02:32:15 -04:00
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
2021-10-02 19:21:07 -04:00
|
|
|
|
sb::Log::gl_errors();
|
2021-09-20 02:32:15 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Texture::load()
|
|
|
|
|
{
|
2022-09-19 22:14:31 -04:00
|
|
|
|
if (!path.empty())
|
|
|
|
|
{
|
|
|
|
|
load(path);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream message;
|
|
|
|
|
message << "Cannot load, no path has been specified yet for texture " << id();
|
|
|
|
|
sb::Log::log(message, sb::Log::WARN);
|
|
|
|
|
}
|
2021-09-20 02:32:15 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Texture::load(fs::path path)
|
|
|
|
|
{
|
2022-09-19 22:14:31 -04:00
|
|
|
|
std::ostringstream message;
|
|
|
|
|
sb::Log::Level message_level;
|
|
|
|
|
if (path.has_filename())
|
|
|
|
|
{
|
|
|
|
|
/* Load file path as a surface object to access pixel data and flip into OpenGL orientation. Attach a destructor so it will free
|
|
|
|
|
* itself when it goes out of scope at the end of this function. */
|
|
|
|
|
std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)> surface(IMG_Load(path.c_str()), SDL_FreeSurface);
|
|
|
|
|
std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)> flipped_surface(rotozoomSurfaceXY(surface.get(), 0, 1, -1, 0), SDL_FreeSurface);
|
|
|
|
|
load(flipped_surface.get());
|
|
|
|
|
message << "Loading image at " << path;
|
|
|
|
|
message_level = sb::Log::INFO;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message << "Cannot load, " << path << " is not a vaild path to a file";
|
|
|
|
|
message_level = sb::Log::WARN;
|
|
|
|
|
}
|
|
|
|
|
sb::Log::log(message, message_level);
|
2021-09-20 02:32:15 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Texture::load(SDL_RWops* rw)
|
|
|
|
|
{
|
2022-09-19 22:14:31 -04:00
|
|
|
|
/* Load RW object as path as a surface object to access pixel data and flip into OpenGL orientation. Attach a destructor so it will free
|
|
|
|
|
* itself when it goes out of scope at the end of this function. */
|
2021-09-20 02:32:15 -04:00
|
|
|
|
std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)> surface(IMG_Load_RW(rw, 0), SDL_FreeSurface);
|
|
|
|
|
std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)> flipped_surface(rotozoomSurfaceXY(surface.get(), 0, 1, -1, 0), SDL_FreeSurface);
|
|
|
|
|
load(flipped_surface.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Texture::load(SDL_Surface* surface)
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream message;
|
2022-09-19 22:14:31 -04:00
|
|
|
|
sb::Log::Level message_level;
|
|
|
|
|
if (surface->w > 0 && surface->h > 0)
|
|
|
|
|
{
|
|
|
|
|
message << "Loading image from SDL surface (" << surface->w << "×" << surface->h << ", " << SDL_GetPixelFormatName(surface->format->format) << ")";
|
|
|
|
|
load(surface->pixels, {surface->w, surface->h}, GL_RGBA, GL_UNSIGNED_BYTE);
|
2022-09-22 17:21:53 -04:00
|
|
|
|
message_level = sb::Log::DEBUG;
|
2022-09-19 22:14:31 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message << "Cannot load into texture, invalid image data without dimensions found";
|
|
|
|
|
message_level = sb::Log::WARN;
|
|
|
|
|
}
|
|
|
|
|
sb::Log::log(message, message_level);
|
2021-09-20 02:32:15 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Texture::load(void* pixels, glm::vec2 size, GLenum format, GLenum type)
|
|
|
|
|
{
|
2022-09-19 22:14:31 -04:00
|
|
|
|
std::ostringstream message;
|
|
|
|
|
sb::Log::Level message_level;
|
|
|
|
|
if (size.x > 0 && size.y > 0)
|
2021-09-20 02:32:15 -04:00
|
|
|
|
{
|
2022-09-19 22:14:31 -04:00
|
|
|
|
if (!generated())
|
|
|
|
|
{
|
|
|
|
|
generate(size);
|
|
|
|
|
}
|
|
|
|
|
bind();
|
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size.x, size.y, format, type, pixels);
|
|
|
|
|
message << "Loaded " << size.x << "×" << size.y << " image into texture ID " << id();
|
2022-09-22 17:21:53 -04:00
|
|
|
|
message_level = sb::Log::DEBUG;
|
2021-09-20 02:32:15 -04:00
|
|
|
|
}
|
2022-09-19 22:14:31 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message << "Cannot load pixels with zero or negative size in either dimension";
|
|
|
|
|
message_level = sb::Log::WARN;
|
|
|
|
|
}
|
|
|
|
|
sb::Log::log(message, message_level);
|
2021-11-09 23:30:27 -05:00
|
|
|
|
sb::Log::gl_errors("after loading texture");
|
2021-09-20 02:32:15 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 21:57:56 -04:00
|
|
|
|
void Texture::bind() const
|
|
|
|
|
{
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, this->id());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* glGetTexlevelparameteriv is not available in OpenGL ES 3.0 */
|
|
|
|
|
#if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !defined(ANDROID)
|
2021-09-20 02:32:15 -04:00
|
|
|
|
void Texture::load(void* pixels, GLenum format, GLenum type)
|
|
|
|
|
{
|
|
|
|
|
if (!generated())
|
|
|
|
|
{
|
|
|
|
|
throw std::invalid_argument("This texture has not been generated and has no size property, so a size must be provided");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
load(pixels, size(), format, type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glm::vec2 Texture::size() const
|
|
|
|
|
{
|
|
|
|
|
if (generated())
|
|
|
|
|
{
|
|
|
|
|
bind();
|
|
|
|
|
int width, height;
|
|
|
|
|
int miplevel = 0;
|
|
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, miplevel, GL_TEXTURE_WIDTH, &width);
|
|
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, miplevel, GL_TEXTURE_HEIGHT, &height);
|
|
|
|
|
return {width, height};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return {0, 0};
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-06 20:44:23 -04:00
|
|
|
|
#endif
|
2021-09-20 02:32:15 -04:00
|
|
|
|
|
|
|
|
|
/* Textures are considered equal if they have the same ID */
|
|
|
|
|
bool Texture::operator==(const Texture& texture) const
|
|
|
|
|
{
|
|
|
|
|
return id() == texture.id();
|
|
|
|
|
}
|
2021-09-24 02:43:38 -04:00
|
|
|
|
|
|
|
|
|
/* This function gets passed to the abstract base class for deleting the texture data when the ID
|
|
|
|
|
* pointer goes out of scope (when all instances of this texture and its copies are out of scope) */
|
2021-10-18 17:33:33 -04:00
|
|
|
|
void sb::texture_deleter(GLuint* id)
|
2021-09-24 02:43:38 -04:00
|
|
|
|
{
|
|
|
|
|
/* not sure why SDL_Log works here at program end but SDL_LogDebug and SDL_LogInfo don't */
|
|
|
|
|
SDL_Log("destroying texture ID %i", *id);
|
|
|
|
|
glDeleteTextures(1, id);
|
|
|
|
|
}
|