Sign in to follow this  
XTAL256

OpenGL multiple references to GL texture

Recommended Posts

I have an Image and Animation class that encapsulates an OpenGL texture id. Most of the time i only need to use each image in one instance but sometimes i need to have multiple references. That's ok for images since i can just copy the pointer but Animations have variables such as the current frame which need to be different for each instance. Ideally, i would have multiple objects that use the same texture id but if that is not possible i would like to know how to copy a texture. I use SOIL for loading and creating textures so i can use that to make a texture from image data but i'm not sure how to get the data from a texture id. Here is what i have so far:
unsigned char* temp;
glBindTexture(GL_TEXTURE_2D, copy->texId);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_INT, temp);
this->texId = SOIL_create_OGL_texture(temp, width, height,
                SOIL_LOAD_RGBA, SOIL_CREATE_NEW_ID,
                SOIL_FLAG_TEXTURE_REPEATS);
SOIL_free_image_data(temp);  // Delete temporary data

But it doesn't work. Can someone tell me if this is the correct way to copy a texture?

Share this post


Link to post
Share on other sites
Hi,

I personnally centralize all textures within a texture resource manager.

Its characteristics are:
- load in bitmap files into textures and provide a handle. If the texture is already loaded within the manager, provide the existing handle.
- select the texture within the graphics engine.
- remove a texture once it is no more usable.
- in case the texture handle daoes not exist, provide an default texture.
- transform multiple selections of the same texture into an empty instruction.

When you load your animated object (sprite or else) connect the loader to the texture manager to:
- load in the texture manager the bitmap.
- store the texture handle within the object class.

Then, when displaying multiple objects using the same texture, just call the render method of your object. This method will then provide to the graphics engine its handle to the texture so that the underlying texture manager correctly setup the texture.

Ghostly yours,
Red.

Share this post


Link to post
Share on other sites
Thanks for the advice Red Ghost, it sounds like a good idea but i would have to write the code for a texture manager and i am working on other parts of the game right now. This is the first game i have attempted to make so i don't have any experience with this sort of stuff. I might consider your idea once i have a working release of my game and i want to improve on things but for now i can get by with copying the textures.

Share this post


Link to post
Share on other sites
What is, my code? If that's the case then it must be some other problem that's causing it to crash.

EDIT: i found that after calling glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_INT, temp);, temp is NULL. I then realised that glGetTexImage does not allocate memory for 'temp' so i did that before i called it and now it crashes when it calls SOIL_create_OGL_texture. I couldn't get an exact location, the debugger would only show me the disassembly.

[Edited by - XTAL256 on September 17, 2008 4:52:39 AM]

Share this post


Link to post
Share on other sites
Hi,

It isn't that difficult to create a basic texture manager.
Consider simple needs:
- load in textures and provide the OpenGL handle
- avoid duplicate texture load
- remove texture.

You already have everything to create your own texture manager:

typedef struct {
std::string _FileName; //without the path
GLuint _Handle; //OpenGL handle
} TEXTURE_INFO;

class TextureManager {
protected:
std::list<TEXTURE_INFO*> _Textures;
//use here your own loading method
bool LoadTexture(const char* pFileName);

public:
TextureManager(void); //basic constructor
~TextureManager(void); //basic destructor which destroys the texture list

//this method:
//- checks wether the texture already exists.
// If so, it returns the associated handle.
//- Loads in the texture, registers it in OpenGL,
// and adds to the texture list a new TEXTURE_INFO
// with the name and the OpenGL handle.
//the returned handle can be stored in your game objects for
//correct display without any more calls to this manager.
GLuint AddTexture(const char* pFileName);

//this method loops through the TEXTURE_INFO list.
//if the texture exists, unregister the texture from OpenGL,
//then update the list
bool RemoveTexture(GLuint pHandle);
}




This manager really is basic. From there you can evolve into:
- hiding the OpenGL handle and select the texture through the texture manager via your own defined handle (easy step).
- improving the search for the handle (easy to medium step).
- checking that a texture still exists when calling for it. (case the texture was removed then called later). (medium step).
- avoiding the selection of the same texture multiple times (easy step - needs hiding the OpenGL handle)
- being thread independant (very hard step).
- ...

Ghostly yours,
Red.

Share this post


Link to post
Share on other sites
I didn't say it would be hard, i just said that i am focused on other parts of the game right now. I will but a TODO in my code to remind me to make a texture manager later.
thanks

Share this post


Link to post
Share on other sites
Opps... I'm sorry. I missed the memory allocation problem. ^^"

I think your program might still has some kind of memory corruption.

The data type parameter in glGetTexImage() you specified is GL_UNSIGNED_INT instead of GL_UNSIGNED_BYTE. Are you intended to do that? Did you allocate enough space for the variable "temp" accordingly?

The function SOIL_create_OGL_texture() does not have any parameter to specify the data type. So, please make sure the data type of the memory block you present to it is going to match the data type it assuming.

Share this post


Link to post
Share on other sites
I used GL_UNSIGNED_INT because i thought that was the size of each pixel (1 byte red, 1 byte green, 1 byte blue and 1 byte alpha). I tried it with GL_UNSIGNED_BYTE and it worked once but crashed when i tried to copy a second time. But that may be another problem. If i still have problems with it i will post again.
EDIT: yeah, the reason it was still crashing is 'cos i deleted the image that i was copying. So, problem solved :)

[Edited by - XTAL256 on September 18, 2008 5:58:23 AM]

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