fixed texture being wrongly loaded.

This commit is contained in:
Andreas Ortmann 2016-10-05 19:12:15 +02:00
parent 63b5954631
commit fc7213fe03

View file

@ -77,18 +77,29 @@ namespace endofthejedi {
png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
NULL, NULL, NULL); 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 //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_width = (int) pow(2, ceilf(log2f(twidth)));
int pot_height = (int) pow(2, ceilf(log2f(m_size.y))); 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("#########################################");*/ m_size = glm::vec2();
/*printf("# PNG: width=%d, height=%d\n", width, height);*/
/*printf("# powers of two: w=%d, h=%d\n", pot_width, pot_height);*/ puts("#########################################");
/*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. // Update the png info struct.
png_read_update_info(png_ptr, info_ptr); png_read_update_info(png_ptr, info_ptr);
@ -96,7 +107,18 @@ namespace endofthejedi {
// Row size in bytes. // Row size in bytes.
int rowbytes = png_get_rowbytes(png_ptr, info_ptr); 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);*/ /*printf("rowbytes: %d, pot_rowbytes=%d\n", rowbytes, pot_rowbytes);*/
// Allocate the image_data as a big block, to be given to opengl // 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); glBindTexture(GL_TEXTURE_2D, m_texture_id);
glTexImage2D(GL_TEXTURE_2D, glTexImage2D(GL_TEXTURE_2D,
0, 0,
GL_RGBA, GL_RGB,
pot_width, pot_width,
pot_height, pot_height,
0, 0,
GL_RGBA, GL_RGB,
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
(GLvoid*) image_data); (GLvoid*) image_data);