Last active
September 25, 2024 18:10
-
-
Save RafaelOliveira/ce746088ced9d3ebf4c309a6acea4922 to your computer and use it in GitHub Desktop.
Load a png texture in OpenGL with libpng
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MYAPI void LoadPngTexture(const char* fileName, int* width, int* height) | |
{ | |
png_image image; | |
memset(&image, 0, sizeof(image)); | |
image.version = PNG_IMAGE_VERSION; | |
if (!png_image_begin_read_from_file(&image, fileName)) { | |
return; | |
} | |
png_bytep buffer; | |
image.format = PNG_FORMAT_BGRA; | |
buffer = new png_byte[PNG_IMAGE_SIZE(image)]; | |
if (!png_image_finish_read(&image, NULL, buffer, 0, NULL)) { | |
png_image_free(&image); | |
return; | |
} | |
*width = image.width; | |
*height = image.height; | |
glGenTextures(1, &texDrawData.activeTextureId); | |
glBindTexture(GL_TEXTURE_2D, texDrawData.activeTextureId); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, *width, *height, | |
0, GL_BGRA, GL_UNSIGNED_BYTE, buffer); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
// unbind the texture | |
glBindTexture(GL_TEXTURE_2D, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment