2022-11-08 17:16:05 -05:00
|
|
|
/*!<pre>
|
|
|
|
* /\ +------------------------------------------------------+
|
|
|
|
* ____/ \____ /| - Open source game framework licensed to freely use, |
|
|
|
|
* \ / / | copy, modify and sell without restriction |
|
|
|
|
* +--\ ^__^ /--+ | |
|
|
|
|
* | ~/ \~ | | - created for <https://foam.shampoo.ooo> |
|
|
|
|
* | ~~~~~~~~~~~~ | +------------------------------------------------------+
|
|
|
|
* | SPACE ~~~~~ | /
|
|
|
|
* | ~~~~~~~ BOX |/
|
|
|
|
* +--------------+ </pre>
|
|
|
|
*
|
|
|
|
* Texture
|
|
|
|
* =======
|
|
|
|
*
|
|
|
|
* The Texture class abstracts the file opening, data loading and binding steps of Open GL
|
|
|
|
* texture creation. Currently it only supports loading GL_TEXTURE_2D with GL_RGB8 pixels
|
|
|
|
* and automatic GL_NEAREST mipmapping. Support may be added for users to pass in their own
|
|
|
|
* pixel data, to customize mipmapping, and more. The class can also be used in a custom
|
|
|
|
* way by passing the Texture ID to GL functions instead of using the class's member
|
|
|
|
* functions.
|
|
|
|
*/
|
2021-09-20 02:32:15 -04:00
|
|
|
|
2021-10-18 17:33:33 -04:00
|
|
|
#ifndef SB_TEXTURE_H_
|
|
|
|
#define SB_TEXTURE_H_
|
2021-09-20 02:32:15 -04:00
|
|
|
|
2021-10-18 17:33:33 -04:00
|
|
|
#include <sstream>
|
2021-09-20 02:32:15 -04:00
|
|
|
#include <stdexcept>
|
2022-10-17 21:57:56 -04:00
|
|
|
|
2021-10-02 19:21:07 -04:00
|
|
|
#include "glm/vec2.hpp"
|
2021-09-20 02:32:15 -04:00
|
|
|
#include "SDL.h"
|
|
|
|
#include "SDL_image.h"
|
|
|
|
#include "sdl2-gfx/SDL2_rotozoom.h"
|
2022-10-17 21:57:56 -04:00
|
|
|
|
2021-09-20 02:32:15 -04:00
|
|
|
#include "filesystem.hpp"
|
2021-09-24 02:43:38 -04:00
|
|
|
#include "GLObject.hpp"
|
2021-10-02 19:21:07 -04:00
|
|
|
#include "Log.hpp"
|
2021-09-20 02:32:15 -04:00
|
|
|
|
2021-10-18 17:33:33 -04:00
|
|
|
namespace sb
|
2021-09-20 02:32:15 -04:00
|
|
|
{
|
2021-10-18 17:33:33 -04:00
|
|
|
|
|
|
|
class Texture : public GLObject
|
|
|
|
{
|
|
|
|
|
|
|
|
private:
|
2021-09-24 02:43:38 -04:00
|
|
|
|
2021-10-18 17:33:33 -04:00
|
|
|
fs::path path = "";
|
2021-09-20 02:32:15 -04:00
|
|
|
|
2021-10-18 17:33:33 -04:00
|
|
|
public:
|
2021-09-20 02:32:15 -04:00
|
|
|
|
2021-10-18 17:33:33 -04:00
|
|
|
Texture();
|
|
|
|
Texture(fs::path);
|
|
|
|
void associate(fs::path);
|
|
|
|
void generate();
|
|
|
|
void generate(glm::vec2);
|
2022-09-19 22:14:31 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* @overload load(fs::path path)
|
|
|
|
*
|
|
|
|
* Load a texture from the path that was set on this object at initialization.
|
|
|
|
*
|
|
|
|
* @see Texture(fs::path)
|
|
|
|
*/
|
2021-10-18 17:33:33 -04:00
|
|
|
void load();
|
2022-09-19 22:14:31 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* @overload load(SDL_Surface* surface)
|
|
|
|
*
|
|
|
|
* Load a texture from a path to an image file. This will create an SDL Surface from the image file at a given path and forward it
|
|
|
|
* to the load overload which accepts a surface pointer.
|
|
|
|
*
|
|
|
|
* @param path Filesystem path to an image file
|
|
|
|
*/
|
|
|
|
void load(fs::path path);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @overload load(SDL_Surface* surface)
|
|
|
|
*
|
|
|
|
* Load texture from an SDL RW stream
|
|
|
|
*/
|
|
|
|
void load(SDL_RWops* rw);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @overload load(void* pixels, glm::vec2 size, GLenum format, GLenum type)
|
|
|
|
*
|
|
|
|
* Load texture from an SDL surface
|
|
|
|
*/
|
|
|
|
void load(SDL_Surface* surface);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Load raw pixel data into texture using OpenGL's `glTexSubImage2D`. The format and type determine how the data will be loaded.
|
|
|
|
* The format is the pixel format, and the type is the type of the data.
|
|
|
|
*
|
|
|
|
* @param pixels Raw pointer to pixel data memory. The type of data pointed to should be given to the format argument.
|
|
|
|
* @param size Dimensions of the image
|
|
|
|
* @param format Format of each pixel
|
|
|
|
* @param type Type pointed to by the pixel data pointer
|
|
|
|
*/
|
2022-09-06 20:44:23 -04:00
|
|
|
void load(void* pixels, glm::vec2 size, GLenum format = GL_RGBA, GLenum type = GL_UNSIGNED_BYTE);
|
|
|
|
|
2022-10-17 21:57:56 -04:00
|
|
|
|
|
|
|
/* glGetTexlevelparameteriv is not available in OpenGL ES 3.0 */
|
|
|
|
#if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !defined(ANDROID)
|
2022-09-06 20:44:23 -04:00
|
|
|
/*!
|
|
|
|
* @overload load(void* pixels, glm::vec2 size, GLenum format, GLenum type)
|
|
|
|
*
|
|
|
|
* The texture must have been previously generated with a size to use this generic pixel data load function. The size is
|
|
|
|
* determined with `glGetTexLevelParameter`, which is only available to OpenGL ES 3.1+, so this overload is not available
|
2022-11-08 17:16:05 -05:00
|
|
|
* for Emscripten or Android builds.
|
2022-09-13 01:01:20 -04:00
|
|
|
*
|
|
|
|
* see OpenGL's `glTexSubImage2D`
|
|
|
|
*
|
|
|
|
* @param pixels pointer to pixel data
|
|
|
|
* @param format GL format of pixel data (for example, GL_RGBA)
|
|
|
|
* @param type data type of the pixel data (for example, GL_UNSIGNED_BYTE if each byte is one of the RGBA values)
|
2022-09-06 20:44:23 -04:00
|
|
|
*/
|
|
|
|
void load(void* pixels, GLenum format = GL_RGBA, GLenum type = GL_UNSIGNED_BYTE);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Return the size in pixels of mipmap level 0 (the only mipmap level supported by this class). If the texture hasn't been,
|
|
|
|
* generated, return {0, 0}. `glGetTexLevelParameter` is only available in OpenGL ES 3.1+, so this function is removed from
|
2022-11-08 17:16:05 -05:00
|
|
|
* Emscripten and Android builds.
|
2022-09-06 20:44:23 -04:00
|
|
|
*
|
|
|
|
* @return glm::vec2 A vector consisting of {TEXTURE_MIPMAP_WIDTH, TEXTURE_MIPMAP_HEIGHT}
|
|
|
|
*/
|
2021-10-18 17:33:33 -04:00
|
|
|
glm::vec2 size() const;
|
2022-09-06 20:44:23 -04:00
|
|
|
#endif
|
|
|
|
|
2022-09-13 01:01:20 -04:00
|
|
|
void bind() const override;
|
2021-10-18 17:33:33 -04:00
|
|
|
bool operator==(const Texture&) const;
|
2021-09-20 02:32:15 -04:00
|
|
|
|
2021-10-18 17:33:33 -04:00
|
|
|
};
|
2021-09-20 02:32:15 -04:00
|
|
|
|
2021-10-18 17:33:33 -04:00
|
|
|
void texture_deleter(GLuint*);
|
2021-09-20 02:32:15 -04:00
|
|
|
|
2021-10-18 17:33:33 -04:00
|
|
|
}
|
2021-09-24 02:43:38 -04:00
|
|
|
|
2021-09-20 02:32:15 -04:00
|
|
|
#endif
|