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,
GLenum dataType,
GLenum texTarget) :
m_texSlot(0),
m_target(texTarget),
m_format(format),
m_internal_format(internal_format),
@ -16,8 +17,8 @@ Texture::Texture(GLenum format,
m_dataType(dataType),
m_hasMipMaps(false)
{
glGenTextures(1, &texId);
glBindTexture(m_target, texId);
glGenTextures(1, &m_texId);
glBindTexture(m_target, m_texId);
switch(m_target)
{
case GL_TEXTURE_2D :
@ -40,14 +41,15 @@ Texture::Texture(GLenum format,
}
Texture::Texture(Image* myImage, bool makeMipMaps) :
m_texSlot(0),
m_target(GL_TEXTURE_2D),
m_width(myImage->width),
m_height(myImage->height),
m_dataType(GL_UNSIGNED_BYTE),
m_hasMipMaps(makeMipMaps)
{
glGenTextures(1, &texId);
glBindTexture(m_target, texId);
glGenTextures(1, &m_texId);
glBindTexture(m_target, m_texId);
initPixels(myImage, GL_TEXTURE_2D);
setWrap(GL_REPEAT);
setFiltering(GL_LINEAR);
@ -56,14 +58,15 @@ Texture::Texture(Image* myImage, bool makeMipMaps) :
}
Texture::Texture(Image* myCubemapImages[6], bool makeMipMaps) :
m_texSlot(0),
m_target(GL_TEXTURE_CUBE_MAP),
m_width(myCubemapImages[0]->width),
m_height(myCubemapImages[0]->height),
m_dataType(GL_UNSIGNED_BYTE),
m_hasMipMaps(makeMipMaps)
{
glGenTextures(1, &texId);
glBindTexture(m_target, texId);
glGenTextures(1, &m_texId);
glBindTexture(m_target, m_texId);
for(int i=0; i<6; ++i)
initPixels(myCubemapImages[i], GL_TEXTURE_CUBE_MAP_POSITIVE_X + i);
setWrap(GL_CLAMP_TO_EDGE);
@ -73,6 +76,7 @@ Texture::Texture(Image* myCubemapImages[6], bool makeMipMaps) :
}
Texture::Texture(Texture* tex, bool halfDim) :
m_texSlot(0),
m_target(tex->m_target),
m_format(tex->m_format),
m_internal_format(tex->m_internal_format),
@ -81,8 +85,8 @@ Texture::Texture(Texture* tex, bool halfDim) :
m_dataType(tex->m_dataType),
m_hasMipMaps(false)
{
glGenTextures(1, &texId);
glBindTexture(m_target, texId);
glGenTextures(1, &m_texId);
glBindTexture(m_target, m_texId);
if(halfDim)
{
m_width /= 2;
@ -93,7 +97,7 @@ Texture::Texture(Texture* tex, bool halfDim) :
Texture::~Texture()
{
glDeleteTextures(1, &texId);
glDeleteTextures(1, &m_texId);
}
void Texture::initPixels(Image* myImage, GLenum target)
@ -138,7 +142,14 @@ void Texture::createMipMaps()
void Texture::bind(int slot)
{
glActiveTexture(GL_TEXTURE0+slot);
glBindTexture(m_target, texId);
if(slot >= 0)
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
{
private:
GLuint texId;
GLuint m_texId;
int m_texSlot;
GLenum m_target;
GLenum m_format;
GLenum m_internal_format;
@ -32,13 +33,15 @@ public:
// creates a standard texture from an image
Texture(Image* myImage, bool makeMipMaps = true);
// 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
Texture(Texture* tex, bool halfDim = false);
~Texture();
void bind(int slot);
GLuint getId() {return texId;}
void setSlot(int slot) { m_texSlot = slot; }
void bind(int slot = -1);
void unbind();
GLuint getId() {return m_texId;}
GLenum getTarget() {return m_target;}
void setWrap(GLint wrap);
void setFiltering(GLint filter);