Cocoa/OpenGL Losing Textures - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Cocoa/OpenGL Losing Textures (/thread-3303.html)
Cocoa/OpenGL Losing Textures - gradyeightythree - May 3, 2007 04:52 PM
So in my continuing struggle to build a 2D tile based map editor with cocoa and open GL I have encountered another road block -- I am losing textures like crazy!
Before I get in to the code I wonder if there isn't some fundamental concept of loading textures that I am overlooking, so let me ask these questions first:
1) Is loading a texture in a function that returns a GLuint and then passing that GLuint to different classes a bad way to load/pass around textures?
2) When I say "losing textures" I mean that using a GLboolean and testing the texture with glIsTexture returns GL_FALSE -- could this mean something else?
If answers to those questions don't get me anywhere I'll post some code. The annoying thing is that it seems fairly random when the textures don't work. Oh and I load them using a URL to get a CGImageRef and convert that to a GLuint -- it works, sometimes....
Cocoa/OpenGL Losing Textures - tigakub - May 4, 2007 12:07 PM
I'm not sure if this is relevant, but I've found that I loose textures too. I use NSImage to load images, then extract them into my own C++ buffers (I do this because I reformat them to ARGB. I originally thought that after I use glTexImage2D to upload the texture, that I could delete the C++ buffers. I was wrong. I guess that OpenGL needs these buffers to stick around so that it can recover the image if GPU texture memory gets wiped.
So the upshot is this question: are you releasing the CGImageRef after creating the textures?
Cocoa/OpenGL Losing Textures - gradyeightythree - May 4, 2007 12:26 PM
Yes I am -- I wasn't aware that you needed it either. I'll try keeping that around and see what happens.
Cocoa/OpenGL Losing Textures - OneSadCookie - May 4, 2007 05:35 PM
tigakub Wrote:I originally thought that after I use glTexImage2D to upload the texture, that I could delete the C++ buffers. I was wrong. I guess that OpenGL needs these buffers to stick around so that it can recover the image if GPU texture memory gets wiped.
Freeing the memory passed to TexImage2D is perfectly legal (unless you're using the APPLE_client_storage extension). I do it all the time.
There's something else at work here. Hard to say what with no information, though
Cocoa/OpenGL Losing Textures - gradyeightythree - May 5, 2007 06:46 AM
Ok here's the situation. The use for this texture is to hold a tileset, so as of right now it is loaded from a subclass of the NSOpenGLView (which is set as the document of a scroll view ... that took me forever to figure out how to do).
Here is the load image/convert to GLuint method:
Every now and then this works, but more often then not I get a lost texture call when I implement the views "DrawRect" like so:
This happens with every texture i try to load.
I'm hoping this is one of those glaringly obvious easy fixes...
Cocoa/OpenGL Losing Textures - OneSadCookie - May 5, 2007 07:56 AM
Code looks OK at a glance... don't think I've seen the buggy bit yet
Are you loading the images when the same OpenGL context is current?
Why are you checking glIsTexture at all?
Are you calling glDeleteTextures somewhere accidentally?
Does the OpenGL profiler tell you anything?
Cocoa/OpenGL Losing Textures - gradyeightythree - May 5, 2007 12:35 PM
Quote:Are you loading the images when the same OpenGL context is current?
Ahh I think that might've been it. I have two separate open gl views embedded in different nsscrollview objects -- one for the map drawing and one for the tileset drawing. In the reshape code for both of the open gl views I have:
[ [self openGLContext ] makeCurrentContext ];
So I suppose when I adjust the map view window it screws up the texture loading which is supposed to take place in the to the tile set view context.
I put [[self openGLContext] makeCurrentContext]; at the beginning of my texture loading function in the tile set view and I haven't been able to get it to screw up as of yet. Does this seem right?
Cocoa/OpenGL Losing Textures - OneSadCookie - May 5, 2007 06:03 PM
NSOpenGLView makes sure that its context is current before calling -reshape and -drawRect:. Generally, you should only make OpenGL calls from those functions (and the functions they call). That avoids any possibility of getting the current context mixed up.
If you *really* need to make GL calls from elsewhere (and I don't see why you'd need to) then yes, you'll need to scatter -makeCurrentContext calls around the place. Be careful!
Cocoa/OpenGL Losing Textures - gradyeightythree - May 7, 2007 11:43 AM
So what is the best way to load a texture in my situation? Set up some sort of boolean variable that figures out whether a texture load has been requested inside the drawRect function? I don't understand why manually setting the current context is dangerous.
And thanks for all your help by the way, I really appreciate it.
Cocoa/OpenGL Losing Textures - ThemsAllTook - May 7, 2007 12:16 PM
gradyeightythree Wrote:So what is the best way to load a texture in my situation? Set up some sort of boolean variable that figures out whether a texture load has been requested inside the drawRect function?I've implemented something similar to this before. In one of my games, I was loading textures from a secondary thread, and since I was doing OpenGL stuff on the main thread, I couldn't upload them right away. So, each texture object carried a "deferred?" bit with it, and once the secondary thread completed, the main thread went through the loaded textures and uploaded each deferred texture's pixel data to OpenGL.