Obtaining part of an image and bind that as texture

Started by
6 comments, last by haegarr 16 years ago
I have lots of texture's and I thought it'd be a good idea to store them by category in one bigger texture. That is, images are placed in the bigger image with known x, y, w, h values, then I load the big image into memory and pick one of the smaller images inside. That smaller image should be loaded into OpenGL. I use Corona for the image loading/converting, which returns me a void* to the pixel buffer. Code:
#include <corona.h>

class Textures
{
    public:
        void load(int, char *[]);

        GLuint *ids;

    private:
        Texture *textures;
        int size;
};

void Textures::load(int isize, char *filenames[])
{
    if (isize > 0)
    {
        if (size)
        {
            free();
        }

        size = isize;
        textures = new (nothrow) Texture[size];
        if (!textures)
        {
            fatal << "could not allocate memory" << POS;
            quit();
        }

        ids = new (nothrow) GLuint[size];
        if (!ids)
        {
            fatal << "could not allocate memory" << POS;
            quit();
        }

        corona::Image *image = NULL;
        for (int i = 0; i < size; i++)
        {
            image = corona::OpenImage(filenames, corona::PF_R8G8B8A8);
            if (image)
            {
                glGenTextures(1, &textures.texture);
                textures.w = image->getWidth();
                textures.h = image->getHeight();

                ids = textures.texture;
                glBindTexture(GL_TEXTURE_2D, textures.texture);

                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->getWidth(), image->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image->getPixels());
            }
            else
            { warning << "could not allocate memory or load image [" << filenames << "]" << POS; }
        }
        delete image;
    }
    else
    { warning << "textures already loaded [" << isize << "]" << POS; }
}
Can I somehow cut a part of the pixel buffer and put that into OpenGL? Or do we have a function in OpenGL for this? Thank you, Decrius
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
You can send only a subset (a rectangular region) of the data in memory, to OpenGL with glTexSubImage2D (http://www.opengl.org/sdk/docs/man/xhtml/glTexSubImage2D.xml). Just make sure you set the glPixelStore parameters correctly (GL_UNPACK_SKIP_ROWS, GL_UNPACK_SKIP_PIXELS, GL_UNPACK_ROW_LENGTH).
Doesn't really seems what I'm looking for...

I wrote this function, though it doesn't really works...

#include <corona.h>class Textures{    public:        void load(int, char *[]);        void getPixels(unsigned char *, int, int, int, int, int, int);        GLuint *ids;    private:        Texture *textures;        int size;};void Textures::load(int isize, char *filenames[]){    if (isize > 0)    {        if (size)        {            free();        }        size = isize;        textures = new (nothrow) Texture[size];        if (!textures)        {            fatal << "could not allocate memory" << POS;            quit();        }        ids = new (nothrow) GLuint[size];        if (!ids)        {            fatal << "could not allocate memory" << POS;            quit();        }        corona::Image *image = NULL;        for (int i = 0; i < size; i++)        {            image = corona::OpenImage(filenames, corona::PF_R8G8B8A8);            if (image)            {                glGenTextures(1, &textures.texture);                textures.w = image->getWidth();                textures.h = image->getHeight();                ids = textures.texture;                glBindTexture(GL_TEXTURE_2D, textures.texture);                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);                if (image->getWidth() == 1000)                {                    getPixels((unsigned char *) image->getPixels(), 200, 20, image->getWidth() - 200, image->getHeight() - 20, image->getWidth(), image->getHeight());                }                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->getWidth(), image->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, (void *) image->getPixels());            }            else            { warning << "could not allocate memory or load image [" << filenames << "]" << POS; }        }        delete image;    }    else    { warning << "textures already loaded [" << isize << "]" << POS; }}void Textures::getPixels(unsigned char *pixels, int x, int y, int w, int h, int w_max, int h_max){    if (!((x <= 0 || (x + w) >= w_max) && (y <= 0 || (y + h) >= h_max) && w >= w_max && h >= h_max))    {        unsigned char *new_pixels = new unsigned char [w * h * 4];        for (int ih = 0; ih < h; ih++)        {            for (int iw = 0; iw < w; iw++)            {                new_pixels[iw * ih * 4] = pixels[(x + iw) * (y + ih) * 4];            }        }        *pixels = *new_pixels;        delete [] new_pixels;    }}
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
If I understand you correctly, you want to take many smaller images put them into a large texture atlas, and then grab some image out of that atlas to render with?

Well if you know how large of texture sizes your card can deal with, you create as large as you want up to the limit size, and then call glTexSubImage2D() and upload that smaller texture into the larger one and then later use the texture coordinates to reference that image. Not sure if that is what you are looking for....
Why don't you just use a paint software and make separate files?
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by MARS_999
If I understand you correctly, you want to take many smaller images put them into a large texture atlas, and then grab some image out of that atlas to render with?

Well if you know how large of texture sizes your card can deal with, you create as large as you want up to the limit size, and then call glTexSubImage2D() and upload that smaller texture into the larger one and then later use the texture coordinates to reference that image. Not sure if that is what you are looking for....


Yes, I could've done that too, but I thought selecting a part of the texture before uploading would be much faster.

Quote:Original post by V-man
Why don't you just use a paint software and make separate files?


It's already seperated, but I want them to be sticking together in categories...for example:

All normal buttons, selected buttons, pressed buttons and disabled buttons in one image, because I can have around 30 buttons.

So, OR uploading one file and handling it, OR uploading 120 small images directly into memory.

First method requires only one request from the harddisk, and is thus faster I think.

[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
If anyone is interested in the code:

unsigned char *getPixels(unsigned char *, unsigned char *, int, int, int, int, int, int);int main(){    // load texture into memory, in this case it is in 'image->getPixels()' from Corona                    int width = 200;                    int height = 800;                    unsigned char *pixels = new unsigned char [width * height * 4];                    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void *)                        getPixels((unsigned char *) image->getPixels(), pixels, 0, 0, width, height, image->getWidth(), image->getHeight()));                    delete [] pixels;}unsigned char *getPixels(unsigned char *pixels, unsigned char *new_pixels, int x, int y, int w, int h, int w_max, int h_max){    if (x >= 0 && y >= 0 && w > 0 && h > 0 && ((w - x) < w_max || (h - y) < h_max))    {        for (int ih = 0; ih < h; ih++)        {            for (int iw = 0; iw < w; iw++)            {                int npos = (iw * 4) + (ih * w * 4);                int opos = ((x + iw + ((w_max - w) * ih)) * 4) + ((y + ih) * w * 4);                new_pixels[npos] = pixels[opos];                new_pixels[npos + 1] = pixels[opos + 1];                new_pixels[npos + 2] = pixels[opos + 2];                new_pixels[npos + 3] = pixels[opos + 3];            }        }        return new_pixels;    }    return pixels;}
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Why should ndhb's solution not work for you? From your code snippets it seems me that the pixel format of your files is useable for OpenGL. Then, glPixelStore can be used to specify the rectangular texture region out of a bigger source image. Furthur, using glTexSubImage can be more efficient than using glTexImage. IMHO his suggestion is definitely worth a look, since it unburdens you from making copies of image portions.

This topic is closed to new replies.

Advertisement