Dengine/src/Texture.c

78 lines
1.9 KiB
C
Raw Normal View History

#include <Dengine\Texture.h>
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)
{
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-17 14:40:40 +00:00
Texture genTexture(const GLuint width, const GLuint height,
const GLint bytesPerPixel, 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-17 14:40:40 +00:00
glCheckError();
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-17 14:40:40 +00:00
glGenTextures(1, &tex.id);
glCheckError();
glBindTexture(GL_TEXTURE_2D, tex.id);
glCheckError();
/* Load image into texture */
// 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();
// TODO(doyle): Not needed for sprites? glGenerateMipmap(GL_TEXTURE_2D);
/* 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();
/* Unbind and clean up */
glBindTexture(GL_TEXTURE_2D, 0);
2016-06-17 14:40:40 +00:00
glCheckError();
2016-06-17 14:40:40 +00:00
return tex;
}