I don't know enough about all of the things involved, but SFML only supports RGBA 8bit 2D textures. Adding an abstraction for the texture, which when used with SFML itself makes only sense as a 2D texture, doesn't make too much sense.
Do you have some example code that goes with the changes you had made?
It's not a sf::Texture abstraction for "texture" but a texture abstraction for Shaders.
THIS IS NEEDED to enable all the GLSL uniform variables parameter compatibles with SFML.
To do a rendering with GLSL (even if it's 2D) you often need external uniform samplersXD variable (X is 1, 2 or 3): for instance rendering an audio spectrum (sampler1D), using time and a noise 3D texture (for post processing effects based on vec3 or vec2 input).
uniform sampler1D spectrum; //Impossible to pass 1D texture with SFML
uniform sampler3D noise3D; //Impossible to pass 3D texture with SFML
Example of inheriting (SFML style) class:
class Sampler1D : public sf::GlTexture
{
public:
Sampler1D();
virtual ~Sampler1D();
bool create(unsigned int size, const float*);
void update(const float* );
void update(const float* , unsigned int size, unsigned int x=0);
virtual void bind() const;
inline unsigned int getSize() const {
return _size;
}
inline bool isSmooth() const {
return _smooth;
}
inline bool isRepeated() const {
return _isRepeated;
}
void setSmooth(bool);
void setRepeated(bool);
private:
unsigned int _realSize;
unsigned int _size;
bool _smooth;
bool _isRepeated;
};
Sampler1D::Sampler1D() :
_realSize(0),
_size(0),
_smooth(false),
_isRepeated(false)
{
}
Sampler1D::~Sampler1D()
{
}
bool Sampler1D::create(unsigned int size, const float* data)
{
unsigned int realSize = GlTexture::getValidSize(size);
unsigned int maxSize = GlTexture::getMaximumSize();
if (realSize > maxSize)
{
std::cerr << "Failed to create texture, its internal size is too high "
<< "maximum is " << maxSize << std::endl;
return false;
}
_realSize=realSize;
_size=size;
ensureGlContext();
if (!m_texture)
{
GLuint texture;
glCheck(glGenTextures(1, &texture));
m_texture = static_cast<unsigned int>(texture);
glCheck(glEnable(GL_TEXTURE_1D));
}
glCheck(glBindTexture(GL_TEXTURE_1D, m_texture));
glCheck(glTexImage1D( GL_TEXTURE_1D, 0, GL_LUMINANCE, _realSize, 0, GL_LUMINANCE, GL_FLOAT, (const GLvoid*) 0 ));
glCheck(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, _isRepeated ? GL_REPEAT : GL_CLAMP_TO_EDGE));
glCheck(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, _isRepeated ? GL_REPEAT : GL_CLAMP_TO_EDGE));
glCheck(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, _smooth ? GL_LINEAR : GL_NEAREST));
glCheck(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, _smooth ? GL_LINEAR : GL_NEAREST));
glCheck(glBindTexture(GL_TEXTURE_1D, 0));
update(data, size);
return true;
}
void Sampler1D::update(const float* data){
update(data, _size);
}
void Sampler1D::update(const float* data, unsigned int size, unsigned int x)
{
if (data && m_texture)
{
ensureGlContext();
// Copy pixels from the given array to the texture
glCheck(glBindTexture(GL_TEXTURE_1D, m_texture));
glCheck(glTexSubImage1D(GL_TEXTURE_1D, 0, x, size, GL_LUMINANCE, GL_FLOAT, (const GLvoid*) data));
glCheck(glBindTexture(GL_TEXTURE_1D, 0));
}
}
void Sampler1D::bind() const
{
ensureGlContext();
glCheck(glBindTexture(GL_TEXTURE_1D, m_texture));
if (m_texture && _size != _realSize)
{
GLfloat matrix[16] = {1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f};
matrix[0] = (float) _size /(float)_realSize;
// Load the matrix
glCheck(glMatrixMode(GL_TEXTURE));
glCheck(glLoadMatrixf(matrix));
// Go back to model-view mode (sf::RenderTarget relies on it)
glCheck(glMatrixMode(GL_MODELVIEW));
}
}
void Sampler1D::setSmooth(bool smooth)
{
if (smooth != _smooth)
{
_smooth = smooth;
if (m_texture)
{
ensureGlContext();
glCheck(glBindTexture(GL_TEXTURE_1D, m_texture));
glCheck(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, _smooth ? GL_LINEAR : GL_NEAREST));
glCheck(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, _smooth ? GL_LINEAR : GL_NEAREST));
}
}
}
void Sampler1D::setRepeated(bool repeated)
{
if (repeated != _isRepeated)
{
_isRepeated = repeated;
if (m_texture)
{
ensureGlContext();
glCheck(glBindTexture(GL_TEXTURE_1D, m_texture));
glCheck(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, _isRepeated ? GL_REPEAT : GL_CLAMP_TO_EDGE));
glCheck(glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, _isRepeated ? GL_REPEAT : GL_CLAMP_TO_EDGE));
}
}
}