Dengine/src/Texture.cpp

76 lines
1.9 KiB
C++
Raw Normal View History

#include <Dengine\Texture.h>
namespace Dengine
{
Texture::Texture()
2016-06-08 07:29:16 +00:00
: id(0)
, width(0)
, height(0)
2016-06-16 14:14:58 +00:00
, internalFormat(GL_RGBA)
2016-06-08 07:29:16 +00:00
, imageFormat(GL_RGB)
, wrapS(GL_REPEAT)
, wrapT(GL_REPEAT)
, filterMinification(GL_LINEAR)
, filterMagnification(GL_LINEAR)
{
2016-06-08 07:29:16 +00:00
glGenTextures(1, &this->id);
}
enum BytesPerPixel
{
Greyscale = 1,
GreyscaleAlpha = 2,
RGB = 3,
RGBA = 4,
};
INTERNAL GLint getGLFormat(BytesPerPixel bytesPerPixel, b32 srgb)
{
switch (bytesPerPixel)
{
2016-06-16 14:14:58 +00:00
case Greyscale:
return GL_LUMINANCE;
case GreyscaleAlpha:
return GL_LUMINANCE_ALPHA;
case RGB:
return (srgb ? GL_SRGB : GL_RGB);
case RGBA:
return (srgb ? GL_SRGB_ALPHA : GL_RGBA);
default:
// TODO(doyle): Invalid
// std::cout << "getGLFormat() invalid bytesPerPixel: "
// << bytesPerPixel << std::endl;
return GL_LUMINANCE;
}
}
2016-06-16 14:14:58 +00:00
void Texture::generate(const GLuint width, const GLuint height, const GLint bytesPerPixel,
2016-06-08 07:29:16 +00:00
const u8 *const image)
{
2016-06-16 14:14:58 +00:00
// TODO(doyle): Let us set the parameters gl params as well
2016-06-08 07:29:16 +00:00
this->width = width;
this->height = height;
2016-06-08 07:29:16 +00:00
glBindTexture(GL_TEXTURE_2D, this->id);
/* Load image into texture */
// TODO(doyle) Figure out the gl format
2016-06-16 14:14:58 +00:00
this->imageFormat = getGLFormat(static_cast<enum BytesPerPixel>(bytesPerPixel), FALSE);
glTexImage2D(GL_TEXTURE_2D, 0, this->internalFormat, this->width, this->height, 0,
this->imageFormat, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
/* Set parameter of currently bound texture */
2016-06-08 07:29:16 +00:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->wrapT);
2016-06-16 14:14:58 +00:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, this->filterMinification);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, this->filterMagnification);
/* Unbind and clean up */
glBindTexture(GL_TEXTURE_2D, 0);
}
2016-06-08 07:29:16 +00:00
void Texture::bind() const { glBindTexture(GL_TEXTURE_2D, this->id); }
}