improved texture class

This commit is contained in:
Anselme 2016-06-10 10:47:01 +02:00
parent b5f0395cca
commit 610cf8af44
2 changed files with 29 additions and 15 deletions

View File

@ -8,6 +8,7 @@ Texture::Texture(GLenum format,
int height, int height,
GLenum dataType, GLenum dataType,
GLenum texTarget) : GLenum texTarget) :
m_texSlot(0),
m_target(texTarget), m_target(texTarget),
m_format(format), m_format(format),
m_internal_format(internal_format), m_internal_format(internal_format),
@ -16,8 +17,8 @@ Texture::Texture(GLenum format,
m_dataType(dataType), m_dataType(dataType),
m_hasMipMaps(false) m_hasMipMaps(false)
{ {
glGenTextures(1, &texId); glGenTextures(1, &m_texId);
glBindTexture(m_target, texId); glBindTexture(m_target, m_texId);
switch(m_target) switch(m_target)
{ {
case GL_TEXTURE_2D : case GL_TEXTURE_2D :
@ -40,14 +41,15 @@ Texture::Texture(GLenum format,
} }
Texture::Texture(Image* myImage, bool makeMipMaps) : Texture::Texture(Image* myImage, bool makeMipMaps) :
m_texSlot(0),
m_target(GL_TEXTURE_2D), m_target(GL_TEXTURE_2D),
m_width(myImage->width), m_width(myImage->width),
m_height(myImage->height), m_height(myImage->height),
m_dataType(GL_UNSIGNED_BYTE), m_dataType(GL_UNSIGNED_BYTE),
m_hasMipMaps(makeMipMaps) m_hasMipMaps(makeMipMaps)
{ {
glGenTextures(1, &texId); glGenTextures(1, &m_texId);
glBindTexture(m_target, texId); glBindTexture(m_target, m_texId);
initPixels(myImage, GL_TEXTURE_2D); initPixels(myImage, GL_TEXTURE_2D);
setWrap(GL_REPEAT); setWrap(GL_REPEAT);
setFiltering(GL_LINEAR); setFiltering(GL_LINEAR);
@ -56,14 +58,15 @@ Texture::Texture(Image* myImage, bool makeMipMaps) :
} }
Texture::Texture(Image* myCubemapImages[6], bool makeMipMaps) : Texture::Texture(Image* myCubemapImages[6], bool makeMipMaps) :
m_texSlot(0),
m_target(GL_TEXTURE_CUBE_MAP), m_target(GL_TEXTURE_CUBE_MAP),
m_width(myCubemapImages[0]->width), m_width(myCubemapImages[0]->width),
m_height(myCubemapImages[0]->height), m_height(myCubemapImages[0]->height),
m_dataType(GL_UNSIGNED_BYTE), m_dataType(GL_UNSIGNED_BYTE),
m_hasMipMaps(makeMipMaps) m_hasMipMaps(makeMipMaps)
{ {
glGenTextures(1, &texId); glGenTextures(1, &m_texId);
glBindTexture(m_target, texId); glBindTexture(m_target, m_texId);
for(int i=0; i<6; ++i) for(int i=0; i<6; ++i)
initPixels(myCubemapImages[i], GL_TEXTURE_CUBE_MAP_POSITIVE_X + i); initPixels(myCubemapImages[i], GL_TEXTURE_CUBE_MAP_POSITIVE_X + i);
setWrap(GL_CLAMP_TO_EDGE); setWrap(GL_CLAMP_TO_EDGE);
@ -73,6 +76,7 @@ Texture::Texture(Image* myCubemapImages[6], bool makeMipMaps) :
} }
Texture::Texture(Texture* tex, bool halfDim) : Texture::Texture(Texture* tex, bool halfDim) :
m_texSlot(0),
m_target(tex->m_target), m_target(tex->m_target),
m_format(tex->m_format), m_format(tex->m_format),
m_internal_format(tex->m_internal_format), m_internal_format(tex->m_internal_format),
@ -81,8 +85,8 @@ Texture::Texture(Texture* tex, bool halfDim) :
m_dataType(tex->m_dataType), m_dataType(tex->m_dataType),
m_hasMipMaps(false) m_hasMipMaps(false)
{ {
glGenTextures(1, &texId); glGenTextures(1, &m_texId);
glBindTexture(m_target, texId); glBindTexture(m_target, m_texId);
if(halfDim) if(halfDim)
{ {
m_width /= 2; m_width /= 2;
@ -93,7 +97,7 @@ Texture::Texture(Texture* tex, bool halfDim) :
Texture::~Texture() Texture::~Texture()
{ {
glDeleteTextures(1, &texId); glDeleteTextures(1, &m_texId);
} }
void Texture::initPixels(Image* myImage, GLenum target) void Texture::initPixels(Image* myImage, GLenum target)
@ -138,7 +142,14 @@ void Texture::createMipMaps()
void Texture::bind(int slot) void Texture::bind(int slot)
{ {
glActiveTexture(GL_TEXTURE0+slot); if(slot >= 0)
glBindTexture(m_target, texId); m_texSlot = slot;
glActiveTexture(GL_TEXTURE0+m_texSlot);
glBindTexture(m_target, m_texId);
} }
void Texture::unbind()
{
glActiveTexture(GL_TEXTURE0+m_texSlot);
glBindTexture(m_target, 0);
}

View File

@ -9,7 +9,8 @@ class Image;
class Texture class Texture
{ {
private: private:
GLuint texId; GLuint m_texId;
int m_texSlot;
GLenum m_target; GLenum m_target;
GLenum m_format; GLenum m_format;
GLenum m_internal_format; GLenum m_internal_format;
@ -32,13 +33,15 @@ public:
// creates a standard texture from an image // creates a standard texture from an image
Texture(Image* myImage, bool makeMipMaps = true); Texture(Image* myImage, bool makeMipMaps = true);
// creates a cubeMap from 6 images // creates a cubeMap from 6 images
Texture(Image* myCubemapImages[6], bool makeMipMaps = true); Texture(Image* myCubemapImages[6], bool makeMipMaps = true);
// creates a texture from another // creates a texture from another
Texture(Texture* tex, bool halfDim = false); Texture(Texture* tex, bool halfDim = false);
~Texture(); ~Texture();
void bind(int slot); void setSlot(int slot) { m_texSlot = slot; }
GLuint getId() {return texId;} void bind(int slot = -1);
void unbind();
GLuint getId() {return m_texId;}
GLenum getTarget() {return m_target;} GLenum getTarget() {return m_target;}
void setWrap(GLint wrap); void setWrap(GLint wrap);
void setFiltering(GLint filter); void setFiltering(GLint filter);