libpng loading junk at bottom of my OpenGL textures...?

Member
Posts: 277
Joined: 2004.10
Post: #1
I am a bit rusty with OpenGL, but I know my code is fine, but the code I found on these forums by OneSadCookie is loading the texture weird.

First the png image I am trying to load and display is a 24 bit png, no alpha. I converted a gif checkerboard that was 512x512 off the internet.
(this is only a test, I fully understand licenses of code and images)

Here's a pic...

And for everyones information I'm using GLFW to setup my context, meaning it's cross-platform.

I don't know if it's the PNG header, or what that's at the bottom of that picture, but the texture is 512x512, a 128x128 is like half the image is just that garbled stuff.

the code I used..
Is here

I added a texName to Image class, and removed the whole static Image return junk in load_png_image in favor of references.


Here's *my* code:
*EDIT* THIS CODE IS INVALID, IT WILL NOT LOAD YOUR IMAGES PROPERLY
use the code from my very last post

Code:
#define tex_test 0

std::map<int, Image> textures;

int loadTextures(void)
{
    load_png_image("texture.png", 1, 0, 3, textures[tex_test]);
        if (textures[tex_test].data == NULL)
            return false;

    glGenTextures(1, &textures[0].texName);
    glBindTexture( GL_TEXTURE_2D, textures[0].texName);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, textures[tex_test].width, textures[tex_test].height, 0, GL_RGB, GL_UNSIGNED_BYTE, &textures[tex_test].data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    glEnable(GL_TEXTURE_2D);
    return true;
}

Thanks!
Nathan

Global warming is caused by hobos and mooses
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Well, the png code works fine for me, so there are three obvious places to look for problems:

1) your modifications to my png code
2) a corrupt PNG image
3) a miscompiled libpng

To check 1), you could take one of my programs that uses the code -- http://onesadcookie.com/~keith/Mechanical.tar.bz2 for example -- and see if it works when you replace one of my images with yours.

To check 2), you could load it up in Preview, GIMP, etc. and see if any of them complain about it and/or load it correctly. I'm pretty sure GIMP will be using libpng, so if it works for GIMP, there's definitely something odd going on.

3) would be trickier to check -- if it comes with automated tests, you can run those, otherwise you can stick your own dynamic libpng into the library load path for a program like GIMP which normally dynamically links to another libpng, and check that it still works.
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #3
Ok I tried pngtest.c which comes with libpng as a testing program.

The texture.png loaded but the output was different, meaning something is wrong. It also says that Libpng passes.

I can't really see two images failing, unless my converter is bad.

I'll go download GIMP and convert with that.

*edit*
I only changed the way your function passes back the data. Instead of a static return it uses a reference. The rest was kept the same.

Global warming is caused by hobos and mooses
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #4
Ok, Just something, but could it be that zlib and libpng don't have the same byte alignment?

After opening my png files in pngtest the pngout.png is smaller in size (like by 200 bytes)

*edit*
I created a new image in GIMP, and it seems that the image is being stretched to the left as well as junk showing up on the bottom.
Updated Image Link
Note, the picture is supposed to be a perfect 512 by 512, with the arrow in the center.

Global warming is caused by hobos and mooses
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #5
I did a clean compile of zlib and libpng. Same results as last post (the picture)

Although it did clean my project up some. I'm going to redo my libpng code by hand. Instead of copy-paste it.

I have piano guild this weekend so I'm turning in tonight.

Global warming is caused by hobos and mooses
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #6
Ok so I didn't goto sleep like I should have...
But I fixed it!

I changed your function back to the way it was, OneSadCookie, and then did this here...
Code:
Image * image;
int loadTextures(void)
{
    image = new Image;
    *image = load_png_image("texture.png", 1, 0, 3);
        if (image->data == NULL)
            return false;

    glGenTextures(1, &image->texName);
    glBindTexture( GL_TEXTURE_2D, image->texName);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, image->width, image->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    glEnable(GL_TEXTURE_2D);
    return true;
}

Now that's C mixed with C++, very bad programming style, but it worked. So I am gonna move everything to a nice class (loading, binding, etc. etc.)

I guess it had to do something with the reference. Actually I know very little about references or static returns.

Global warming is caused by hobos and mooses
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #7
Ok, so I found the problem, I don't know why it even showed some of the picture. But it was the last parameter in glTexImage2D (the data parameter)
I was passing &image.data which was handle, not a pointer.

So here's the code in it's final form.
Code:
struct Image {
    unsigned  width;
    unsigned  height;
    unsigned int texName;
    void     *data;
};

#define tex_test 0
#define tex_arrow 1

std::map<int, Image> images;
void loadTextures(void)
{
    loadTexture(tex_test,     "texture.png");
    loadTexture(tex_arrow,    "textureARROW.png");
}

void loadTexture(int index, const char * fileName)
{
    images[index] = load_png_image(fileName, 1, 0, 3);

    glGenTextures(1, &images[index].texName);
    glBindTexture( GL_TEXTURE_2D, images[index].texName);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, images[index].width, images[index].height, 0, GL_RGB, GL_UNSIGNED_BYTE, images[index].data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    glEnable(GL_TEXTURE_2D);
}

void bindTexture(int index)
{
    glBindTexture( GL_TEXTURE_2D, images[index].texName);
}

Global warming is caused by hobos and mooses
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  libPng And Memory Access Violations Jaden 12 11,770 Sep 4, 2013 04:50 AM
Last Post: miye
  [SOLVED]OpenGL edges of textures mk12 2 4,382 Sep 2, 2010 08:07 PM
Last Post: mk12
  OpenGL ES Loading images techy 4 3,858 Dec 24, 2009 02:01 PM
Last Post: techy
  OpenGL Image Textures mikey 52 24,409 Jun 30, 2009 10:42 AM
Last Post: AnotherJake
  Dealing with inverted textures in OpenGL johncmurphy 7 6,991 Jun 15, 2009 08:11 AM
Last Post: Skorche