Sign in to follow this  
ill

OpenGL How to copy the raw data used by OpenGL loaded by DevIL (Non pwr of 2 texture paddin)

Recommended Posts

OK, so i'm using DevIL to load textures into my OpenGL + SDL app. I'm using an image class to wrap all the image loading and drawing. It's going to be for a 2D engine so these will basically be sprites. Since it's for a 2D game where most of the sprite dimensions won't be powers of 2 I need to load them in padded with powers of 2 dimensions. I basically have everything working here right now except it doesn't appear to be copying the texture properly with my for loop. Basically each pixel is 4 bytes since the image is loaded in with RGBA format so for each pixel I copy over 4 bytes to the new array. The old array of image data is the nonpadded image. The new array is the padded image data. I don't really care what pixels there are outside of the boundary since those don't get drawn when I draw a textured quad with the sprite image since I also calculate the proper texture coordinates to clip the image to only show the valid pixel data. So from the way I see it, my for loop should be properly copying all of the image bytes but what I get instead is garbage when I display the padded image. I'm obviously doing something wrong and must be misunderstanding something about the way image data is stored in memory. Alternatively, I can use ILUT from DevIL to automatically load in the textures for me and pad them with power of 2 but I'm not sure if it'll allow me to extract data like image width and so on. Also I couldn't seem to get ILUT to properly load textures for me in the first place so I settled for the raw method. If someone has a good example of working ILUT texture loading directly into OpenGL that would be great. I'm also open to completely different ideas if mine totally sucks. Here is the entire loading function for the image.
/*
 * Loads image from file into an OpenGL texture
 *      fileNameArg:    file name of image to load
 *      xOriginArg:     x origin of image relative to top left corner
 *      yOriginArg:     y origin of image relative to top left corner
 */
Image::Image(const char* fileNameArg, GLfloat xOriginArg, GLfloat yOriginArg)
{
    //TODO: check if hardware supports nonpower of 2 texture dimensions to not do padding and save memory
    //TODO: check max texture width and height
    /*
     * int Max2DTextureWidth, Max2DTextureHeight;
     * glGetIntegerv(GL_MAX_TEXTURE_SIZE, &Max2DTextureWidth);
     * Max2DTextureWidth=Max2DTextureHeight;
     *
     */
    //TODO: place texture border pixels
    ILuint ilTexture;
    int paddedWidth;
    int paddedHeight;
    //GLvoid * imageData = NULL;
    ILubyte* imageDataOriginal = NULL;
    ILubyte* imageData = NULL;
    GLint imageBPP;

    ilGenImages(1, &ilTexture);
    ilBindImage(ilTexture);

    if(ilLoadImage(fileNameArg))
    {
        if(ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE))
        {
            width = ilGetInteger(IL_IMAGE_WIDTH);
            height = ilGetInteger(IL_IMAGE_HEIGHT);
            imageBPP = ilGetInteger(IL_IMAGE_BPP);

            //pad image if not power of 2
            paddedWidth = nextPow2(width);
            paddedHeight = nextPow2(height);

            //if resizing is necessary for padding
            if(paddedWidth != width || paddedHeight != height)
            {
                imageDataOriginal = ilGetData();

                ///////////////////////////////////////////////////
                //THIS IS WHERE I'M MANUALLY COPYING IMAGE DATA
                ///////////////////////////////////////////////////
                if((imageData = new (nothrow) ILubyte[width * imageBPP * height]) != NULL)
                {
                    for(int imageY; imageY <= height; imageY ++)
                    {
                        for(int imageX; imageX <= width * imageBPP; imageX ++)
                        {
                            imageData[imageX + imageY * paddedWidth * imageBPP] =
                                imageDataOriginal[imageX + imageY * width * imageBPP];
                        }
                    }
                }
                else
                {
                    exit(EXIT_FAILURE);
                }
            }
            else
            {
                imageData = ilGetData();
            }

            glGenTextures(1, &glTexture);
            glBindTexture(GL_TEXTURE_2D, glTexture);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

            glTexImage2D(GL_TEXTURE_2D, 0, imageBPP, paddedWidth, paddedHeight, 0,
                ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, imageData);
        }
        else
        {
            exit(EXIT_FAILURE);
        }
    }
    else
    {
        exit(EXIT_FAILURE);
    }

    ilDeleteImages(1, &ilTexture);

    if(paddedWidth != width || paddedHeight != height)
    {
        delete[] imageData;
    }

    if(glGetError() != GL_NO_ERROR)
    {
        exit(EXIT_FAILURE);
    }

    xOrigin = xOriginArg;
    yOrigin = yOriginArg;

    //set texture coordinates for clipping padded texture when texturing quad
    texClipCoordW = (float) width / paddedWidth;
    texClipCoordH = 1 - ((float) height / paddedHeight);
}

Share this post


Link to post
Share on other sites
You haven't initialized your loop variables. But assuming you want to initialize them to zero, and that texClipCoordW/H are the texture coordinates where the texture ends, the H-coordinate shall be texClipCoordH = (float)height / paddedHeight.

But, any reason you're not using the texture as it is, without padding to a power of two?

Share this post


Link to post
Share on other sites
HAHAHA thanks. Always helps to have someone else look at your code. I'm sure I would've never found this stupid mistake on my own.

Almost works now, except the entire image data in the sprite is offset down by 3 pixels.

I actually can't figure this out since it might be a way that GLtexImage2D is reading the pixel data.

This is the current for loop after a few fixes. cout outputs to stdout.txt so I can see that all the pixel data should be correct.

for(int imageY = 0; imageY < height; imageY ++)
{
for(int imageX = 0; imageX < width * imageBPP; imageX ++)
{
cout << (int) imageDataOriginal[imageX + imageY * width * imageBPP]
<< " " << (imageX + imageY * width * imageBPP) << endl;

imageData[imageX + imageY * paddedWidth * imageBPP] =
imageDataOriginal[imageX + imageY * width * imageBPP];
}
}


When it's loaded into OpenGL for a 24x29 image, it seems to be offset down by 3 pixels. 32-29 = 3. IDK if that's a coincidence or not. Anyway, the program always crashes for any other dimension so the fact that I have this 24x29 image just randomly not crashing on me while I'm testing is a miracle so I can actually see kinda what's happening. For example 32x24 crashes for some reason.

Basically, is there some kind of strange order that glTexImage2D reads the image data in, like in backwards y order or something? Maybe the raw data is actually upside down. I don't see how else this could be happening.






For now I fixed it by doing this:

if((imageData = new (nothrow) ILubyte[width * imageBPP * height]) != NULL)
{
for(int imageY = 0; imageY < height; imageY ++)
{
for(int imageX = 0; imageX < width * imageBPP; imageX ++)
{
//Here I'm offsetting the y coordinate of resulting image by paddedHeight-Height and it works great for all sizes
imageData[imageX + (imageY + (paddedHeight - height)) * paddedWidth * imageBPP] =
imageDataOriginal[imageX + imageY * width * imageBPP];
}
}
}

Is the image always loaded in upside down by OpenGL or is there some special case where this happens?

Also I figured out why I get a crash. After loading this in I free the data that was used.

//clean up image data
ilDeleteImages(1, &ilTexture);

if(paddedWidth != width || paddedHeight != height)
{
cout << "delA" << endl;

delete[] imageData;

cout << "delB" << endl;
}


I only get this crash when the image isn't 24x29 or isn't already with power of 2 dimensions (since that block shouldn't execute unless the image was padded). So should I not be freeing imageData or is there something seriously wrong or is my syntax completely off? I have a lot of experience with C and Java, but I'm basically just starting with C++ so I get a little confused sometimes. Like in C I would use malloc and free instead of new and delete. I could do that by including <cstdlib> or <stdlib.h> or something like that and use the C functions but what's the point of that? I might still get the error anyway and I would think it's better to use built in functions of the language.

[Edited by - ill on August 13, 2009 12:24:59 PM]

Share this post


Link to post
Share on other sites
You might want to look into the function when copying big chunks of data that you don't want to cut into peaces.

ILuint ilCopyPixels( ILuint XOff, ILuint YOff, ILuint ZOff, ILuint Width, ILuint Height, ILuint Depth, ILenum Format, ILenum Type, ILvoid *Data);

or

ILboolean ilOverlayImage(ILuint Src, ILint XCoord, ILint YCoord, ILint
ZCoord);
ILboolean ilBlit(ILuint Src, ILint DestX, ILint DestY, ILint DestZ, ILuint SrcX,
ILuint SrcY, ILuint SrcZ, ILuint Width, ILuint Height, ILuint
Depth);

which is probably more suitable in your case.

You can probably reduce your code immense using those functions instead of your custom method.

Also i think (not 100% sure tho) your array indexes should more look like

(x+y*width)*bpp

instead of

(x+y*width*bpp)

You could also use

ILboolean iluScale(ILuint Width, ILuint Height, ILuint Depth);

http://openil.sourceforge.net/docs/DevIL%20Manual.pdf

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this