Files
Dawn/src/dawnopengl/display/Texture.hpp
2024-06-24 08:45:12 -05:00

135 lines
3.6 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnopengl.hpp"
#include "display/Color.hpp"
namespace Dawn {
class TextureRenderTarget;
typedef GLuint textureslot_t;
enum class TextureFormat {
R = GL_RED,
#ifdef GL_RG
RG = GL_RG,
#endif
RGB = GL_RGB,
RGBA = GL_RGBA
};
enum class TextureWrapMode {
REPEAT = GL_REPEAT,
MIRRORED_REPEAT = GL_MIRRORED_REPEAT,
CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
#ifdef GL_CLAMP_TO_BORDER
CLAMP_TO_BORDER = GL_CLAMP_TO_BORDER
#endif
};
enum class TextureFilterMode {
NEAREST = 0,
LINEAR = 1
};
enum class TextureDataFormat {
UNSIGNED_BYTE = sizeof(uint8_t),
FLOAT = sizeof(float_t)
};
class Texture {
private:
int32_t width = -1;
int32_t height = -1;
GLuint id = -1;
enum TextureFormat format;
enum TextureDataFormat dataFormat;
void updateTextureProperties();
void bufferRaw(const void *data);
public:
enum TextureWrapMode wrapModeX = TextureWrapMode::REPEAT;
enum TextureWrapMode wrapModeY = TextureWrapMode::REPEAT;
enum TextureFilterMode filterModeMin = TextureFilterMode::NEAREST;
enum TextureFilterMode filterModeMag = TextureFilterMode::NEAREST;
enum TextureFilterMode mipMapFilterModeMin = TextureFilterMode::NEAREST;
enum TextureFilterMode mipMapFilterModeMag = TextureFilterMode::NEAREST;
/**
* Returns the width of the texture.
*
* @return Width of the texture.
*/
int32_t getWidth();
/**
* Returns the height of the texture.
*
* @return Height of the texture.
*/
int32_t getHeight();
/**
* Initializes a texture.
*
* @param width Width of the texture (in pixels).
* @param height Height of the texture (in pixels).
* @param format Data format of the texture to use.
* @param dataFormat Data format of the texture to use.
*/
void setSize(
const int32_t width,
const int32_t height,
const enum TextureFormat format,
const enum TextureDataFormat dataForat
);
/**
* Returns true only when the texture has been loaded, sized and put on
* the gpu for rendering.
*
* @return True if ready, otherwise false.
*/
bool_t isReady();
/**
* Buffer pixel data onto the GPU. Pixel buffering is rather costly so
* avoid doing this too often.
*
* @param pixels Array of pixels you're trying to buffer.
*/
void buffer(const struct ColorU8 pixels[]);
/**
* Buffer pixel data onto the GPU. Pixel buffering is rather costly so
* avoid doing this too often.
*
* @param pixels Array of pixels you're trying to buffer.
*/
void buffer(const struct Color pixels[]);
/**
* Buffer pixel data onto the GPU. Pixel buffering is rather costly so
* avoid doing this too often.
*
* @param pixels Array of pixels you're trying to buffer.
*/
void buffer(const uint8_t pixels[]);
/**
* Binds the texture to the given slot (for use by the shaders).
*
* @param slot Slot to bind to.
*/
void bind(const uint8_t slot);
/*
* Destructs and disposes the texture off the GPU.
*/
~Texture();
};
}