66 lines
2.3 KiB
C++
66 lines
2.3 KiB
C++
|
#pragma once
|
||
|
|
||
|
#include "glclasses.hpp"
|
||
|
|
||
|
#include <glm/vec2.hpp>
|
||
|
|
||
|
// based on sources of knoc/chaqu
|
||
|
|
||
|
namespace endofthejedi {
|
||
|
|
||
|
/**
|
||
|
* @brief This sprite struct stores all information needed to render an
|
||
|
* arbitrary PNG texture to a quad.
|
||
|
*
|
||
|
* If the loaded texture dimensions have no powers of a bit more space is
|
||
|
* allocated and extra padding is added.
|
||
|
*/
|
||
|
class ImageTexture {
|
||
|
public:
|
||
|
ImageTexture(const std::string &filename) : m_path(filename), m_valid(false)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
~ImageTexture()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief Try to load a PNG image from file into an opengl texture.
|
||
|
*
|
||
|
* @param path Path of PNG file
|
||
|
* @param texture The used texture id will be saved there.
|
||
|
* @param width Width of the loaded texture in pixels.
|
||
|
* @param height Height of the loaded texture in pixels.
|
||
|
* @param uv_scale_x Horizontal portion of the screen which is padded to get
|
||
|
* power-of-two texture.
|
||
|
* @param uv_scale_y Vertical portion of the screen which is padded to get
|
||
|
* power-of-two texture.
|
||
|
*
|
||
|
* Note: The current active texture unit should be bound (GL_TEXTURE0 etc...)
|
||
|
* before calling this
|
||
|
*
|
||
|
* @return true if loading suceeded, false on errors.
|
||
|
*/
|
||
|
bool loadPng();
|
||
|
|
||
|
bool valid() const { return m_valid; }
|
||
|
|
||
|
glm::vec2 size() const { return glm::vec2(m_width, m_height); }
|
||
|
glm::vec2 uvScale() const { return glm::vec2(m_uv_scale_x, m_uv_scale_y); }
|
||
|
|
||
|
GLuint textureId() const { return m_texture_id; }
|
||
|
|
||
|
public:
|
||
|
std::string m_path; // path for the loaded texture is saved here
|
||
|
int m_width; // used width of the pixels of the texture
|
||
|
int m_height; // used height of the pixels of the texture
|
||
|
float m_uv_scale_x; // factor to scale uv-coordinates to get rid of non-power-of-two padding
|
||
|
float m_uv_scale_y; // factor to scale uv-coordinates to get rid of non-power-of-two padding
|
||
|
|
||
|
bool m_valid;
|
||
|
|
||
|
GLuint m_texture_id; // texture binding id
|
||
|
};
|
||
|
}
|