From fc7213fe03757421c007b5394666d1edc75817eb Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Wed, 5 Oct 2016 19:12:15 +0200 Subject: [PATCH] fixed texture being wrongly loaded. --- game/renderer_polygon_3d/image_texture.cpp | 44 ++++++++++++++++------ 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/game/renderer_polygon_3d/image_texture.cpp b/game/renderer_polygon_3d/image_texture.cpp index 064d11a..bacaee5 100644 --- a/game/renderer_polygon_3d/image_texture.cpp +++ b/game/renderer_polygon_3d/image_texture.cpp @@ -77,18 +77,29 @@ namespace endofthejedi { png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, NULL, NULL, NULL); + + if (color_type == PNG_COLOR_TYPE_RGB) { + puts("PNG_COLOR_TYPE_RGB"); + } else if (color_type == PNG_COLOR_TYPE_RGBA) { + puts("PNG_COLOR_TYPE_RGBA"); + } + //update width and height based on png info - m_size = glm::vec2(twidth, theight); - int pot_width = (int) pow(2, ceilf(log2f(m_size.x))); - int pot_height = (int) pow(2, ceilf(log2f(m_size.y))); + int pot_width = (int) pow(2, ceilf(log2f(twidth))); + int pot_height = (int) pow(2, ceilf(log2f(theight))); - m_uvScale = m_size / glm::vec2(pot_width, pot_height); + m_uvScale = glm::vec2( + twidth / (float) pot_width, + theight / (float) pot_height); - /*puts("#########################################");*/ - /*printf("# PNG: width=%d, height=%d\n", width, height);*/ - /*printf("# powers of two: w=%d, h=%d\n", pot_width, pot_height);*/ - /*puts("#########################################");*/ + m_size = glm::vec2(); + + puts("#########################################"); + printf("# PNG: width=%d, height=%d\n", twidth, theight); + printf("# powers of two: w=%d, h=%d\n", pot_width, pot_height); + printf("# uv scale two: x=%f, y=%f\n", m_uvScale.x, m_uvScale.y); + puts("#########################################"); // Update the png info struct. png_read_update_info(png_ptr, info_ptr); @@ -96,7 +107,18 @@ namespace endofthejedi { // Row size in bytes. int rowbytes = png_get_rowbytes(png_ptr, info_ptr); - int pot_rowbytes = 4 * (int) pow(2, ceilf(log2f(rowbytes/4))); + int valuesPerColor; + switch(color_type) { + case PNG_COLOR_TYPE_RGB: valuesPerColor = 3; break; + case PNG_COLOR_TYPE_RGBA: valuesPerColor = 4; break; + default: + printf("error: could not parse png color that is not RGB or RGBA\n"); + png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); + fclose(fp); + return false; + } + + int pot_rowbytes = valuesPerColor * (int) pow(2, ceilf(log2f(rowbytes/valuesPerColor))); /*printf("rowbytes: %d, pot_rowbytes=%d\n", rowbytes, pot_rowbytes);*/ // Allocate the image_data as a big block, to be given to opengl @@ -131,11 +153,11 @@ namespace endofthejedi { glBindTexture(GL_TEXTURE_2D, m_texture_id); glTexImage2D(GL_TEXTURE_2D, 0, - GL_RGBA, + GL_RGB, pot_width, pot_height, 0, - GL_RGBA, + GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*) image_data);