Dengine/src/Texture.c

79 lines
2.1 KiB
C
Raw Normal View History

2016-06-28 06:00:03 +00:00
#include "Dengine\Texture.h"
enum BytesPerPixel
{
bytesPerPixel_Greyscale = 1,
bytesPerPixel_GreyscaleAlpha = 2,
bytesPerPixel_RGB = 3,
bytesPerPixel_RGBA = 4,
};
2016-06-17 14:40:40 +00:00
INTERNAL GLint getGLFormat(i32 bytesPerPixel, b32 srgb)
{
switch (bytesPerPixel)
{
case bytesPerPixel_Greyscale:
2016-06-16 14:14:58 +00:00
return GL_LUMINANCE;
case bytesPerPixel_GreyscaleAlpha:
2016-06-16 14:14:58 +00:00
return GL_LUMINANCE_ALPHA;
case bytesPerPixel_RGB:
2016-06-16 14:14:58 +00:00
return (srgb ? GL_SRGB : GL_RGB);
case bytesPerPixel_RGBA:
2016-06-16 14:14:58 +00:00
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);
ASSERT(tex.imageFormat == GL_RGBA);
2016-06-17 14:40:40 +00:00
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;
}