62 lines
1.6 KiB
C++
62 lines
1.6 KiB
C++
#include "texture.h"
|
|
#include "glassert.h"
|
|
#include <QImage>
|
|
|
|
Texture::Texture(const QString filename) : type(GL_TEXTURE_2D)
|
|
{
|
|
glAssert(glGenTextures(1, &texId));
|
|
glAssert(glBindTexture(type, texId));
|
|
|
|
createTexture(filename, GL_TEXTURE_2D);
|
|
setWrap(GL_REPEAT);
|
|
setFiltering(GL_NEAREST);
|
|
}
|
|
|
|
Texture::Texture(const QString filename[6]) : type(GL_TEXTURE_CUBE_MAP)
|
|
{
|
|
glAssert(glActiveTexture(GL_TEXTURE0));
|
|
glAssert(glGenTextures(1, &texId));
|
|
glAssert(glBindTexture(type, texId));
|
|
|
|
for(int i=0; i<6; ++i)
|
|
{
|
|
createTexture(filename[i], GL_TEXTURE_CUBE_MAP_POSITIVE_X + i);
|
|
setWrap(GL_CLAMP_TO_EDGE);
|
|
setFiltering(GL_LINEAR);
|
|
}
|
|
}
|
|
|
|
Texture::~Texture()
|
|
{
|
|
glAssert(glDeleteTextures(1, &texId));
|
|
}
|
|
|
|
void Texture::createTexture(QString filename, GLenum textureSlot)
|
|
{
|
|
QImage img(filename);
|
|
int bpp = (img.depth() == 32) ? GL_RGBA : GL_RGB;
|
|
int format = (img.depth() == 32) ? GL_BGRA : GL_BGR;
|
|
glAssert(glTexImage2D(textureSlot, 0, bpp, img.width(), img.height(), 0, format, GL_UNSIGNED_BYTE, img.bits()));
|
|
}
|
|
|
|
void Texture::setWrap(GLint wrap)
|
|
{
|
|
glAssert(glTexParameteri(type, GL_TEXTURE_WRAP_S, wrap));
|
|
glAssert(glTexParameteri(type, GL_TEXTURE_WRAP_T, wrap));
|
|
glAssert(glTexParameteri(type, GL_TEXTURE_WRAP_R, wrap));
|
|
}
|
|
|
|
void Texture::setFiltering(GLint filter)
|
|
{
|
|
glAssert(glTexParameteri(type, GL_TEXTURE_MIN_FILTER, filter));
|
|
glAssert(glTexParameteri(type, GL_TEXTURE_MAG_FILTER, filter));
|
|
}
|
|
|
|
void Texture::bind(int slot)
|
|
{
|
|
GLenum texSlot = GL_TEXTURE0+slot;
|
|
glAssert(glActiveTexture(texSlot));
|
|
glAssert(glBindTexture(type, texId));
|
|
}
|
|
|