Jump to content
  • Advertisement
Sign in to follow this  
Decrius

OpenGL Obtaining part of an image and bind that as texture

This topic is 3856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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;
}
}

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!