2016-06-04 12:42:22 +00:00
|
|
|
#include <Dengine\Texture.h>
|
|
|
|
|
2016-06-05 07:54:41 +00:00
|
|
|
enum BytesPerPixel
|
|
|
|
{
|
|
|
|
Greyscale = 1,
|
|
|
|
GreyscaleAlpha = 2,
|
|
|
|
RGB = 3,
|
|
|
|
RGBA = 4,
|
|
|
|
};
|
|
|
|
|
2016-06-17 14:40:40 +00:00
|
|
|
INTERNAL GLint getGLFormat(i32 bytesPerPixel, b32 srgb)
|
2016-06-05 07:54:41 +00:00
|
|
|
{
|
|
|
|
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-05 07:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:40:40 +00:00
|
|
|
Texture genTexture(const GLuint width, const GLuint height,
|
|
|
|
const GLint bytesPerPixel, const u8 *const image)
|
2016-06-04 12:42:22 +00:00
|
|
|
{
|
2016-06-16 14:14:58 +00:00
|
|
|
// TODO(doyle): Let us set the parameters gl params as well
|
2016-06-17 14:40:40 +00:00
|
|
|
glCheckError();
|
2016-06-18 09:12:09 +00:00
|
|
|
Texture tex = {0};
|
2016-06-17 14:40:40 +00:00
|
|
|
tex.width = width;
|
|
|
|
tex.height = height;
|
|
|
|
tex.internalFormat = GL_RGBA;
|
|
|
|
tex.wrapS = GL_REPEAT;
|
|
|
|
tex.wrapT = GL_REPEAT;
|
|
|
|
tex.filterMinification = GL_LINEAR;
|
|
|
|
tex.filterMagnification = GL_LINEAR;
|
2016-06-04 12:42:22 +00:00
|
|
|
|
2016-06-17 14:40:40 +00:00
|
|
|
glGenTextures(1, &tex.id);
|
|
|
|
glCheckError();
|
|
|
|
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, tex.id);
|
|
|
|
glCheckError();
|
2016-06-04 12:42:22 +00:00
|
|
|
|
|
|
|
/* Load image into texture */
|
2016-06-05 07:54:41 +00:00
|
|
|
// TODO(doyle) Figure out the gl format
|
2016-06-17 14:40:40 +00:00
|
|
|
tex.imageFormat = getGLFormat(bytesPerPixel, FALSE);
|
|
|
|
glCheckError();
|
2016-06-16 14:14:58 +00:00
|
|
|
|
2016-06-17 14:40:40 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, tex.internalFormat, tex.width, tex.height, 0,
|
|
|
|
tex.imageFormat, GL_UNSIGNED_BYTE, image);
|
|
|
|
glCheckError();
|
2016-06-18 14:34:20 +00:00
|
|
|
|
|
|
|
// TODO(doyle): Not needed for sprites? glGenerateMipmap(GL_TEXTURE_2D);
|
2016-06-04 12:42:22 +00:00
|
|
|
|
|
|
|
/* Set parameter of currently bound texture */
|
2016-06-17 14:40:40 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, tex.wrapS);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tex.wrapT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
|
|
tex.filterMinification);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
|
|
|
tex.filterMagnification);
|
|
|
|
glCheckError();
|
2016-06-04 12:42:22 +00:00
|
|
|
|
|
|
|
/* Unbind and clean up */
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2016-06-17 14:40:40 +00:00
|
|
|
glCheckError();
|
2016-06-04 12:42:22 +00:00
|
|
|
|
2016-06-17 14:40:40 +00:00
|
|
|
return tex;
|
2016-06-04 12:42:22 +00:00
|
|
|
}
|