0
\$\begingroup\$

I have been trying to get textures loading properly onto my models but to no avail. I'm using picopng to load my images. enter image description here

Here is my code. [Texture code]

std::vector<unsigned char> buffer, image, aaa;

std::ifstream file(location.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
std::streamsize size = 0;
if (file.seekg(0, std::ios::end).good()) size = file.tellg();
if (file.seekg(0, std::ios::beg).good()) size -= file.tellg();
if (size > 0)
{
    buffer.resize((size_t)size);
    file.read((char*)(&buffer[0]), size);
}
else {
    buffer.clear();
}
unsigned long w, h;
int error = decodePNG(image, w, h, buffer.empty() ? 0 : &buffer[0], (unsigned long)buffer.size());

if (error != 0) std::cout << "error: " << error << std::endl;

glEnable(GL_TEXTURE_2D);
GLuint id;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, &image[0]);

glBindTexture(GL_TEXTURE_2D, 0);

glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
float mipMapingAffectivness = 0.4f;
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, mipMapingAffectivness);

Any advice?

[Update] I have tried flipping the image but to no avail, I have tried all 8 orientation variations and non seem to work.

\$\endgroup\$
1
  • \$\begingroup\$ Hod do we know how it's wrong? Can you add an UV map and how "right" texturing looks? \$\endgroup\$
    – Kromster
    Commented Nov 21, 2016 at 18:08

1 Answer 1

1
\$\begingroup\$

OpenGL textures are bottom to top, PNG are top to bottom.

You need to either flip the UVs vertically or flip the image vertically.

\$\endgroup\$
1
  • \$\begingroup\$ When I flip it, it dose not seem to make much of a diffrence, here is my code for (int y = h - 1; y >= 0; y--) { for (int x = 0; x < (w * 4); x++) { aaa.push_back(image[((w * 4)*y + x)]); } } \$\endgroup\$
    – 0xen
    Commented Nov 20, 2016 at 23:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .