views:

647

answers:

3

I have no idea about it, but if that was possible (like Flash, for example), then that would be really cool.

I have an view-based app where I need probably some OpenGL ES capabilities, but don't want to have fullscreen OpenGL ES. I just need it at certain small areas for drawing little charts and other stuff that may be hard to do with core graphics.

+2  A: 

Yes,

you should be able to implement this through Quartz Core using layers (see the CALayer class documentation). Indeed, you can have layers hierarchies. Basically you associate each UIView to a different layer, then the layers are rendered together providing a single, composite layer. Besides, you can also apply transforms and animations to layers.

You need to import the QuartzCore header and do something like

#import <QuartzCore/QuartzCore.h>
UIView *myView = [[UIView alloc] initWithFrame...
UIView *openGLView = [UIView alloc] initWithFrame...

CaLayer *myViewLayer = myView.layer;
[myViewLayer addSubLayer: openGLView.layer];

Then, when myView appears on the screen, all the sublayers are merged together and rendered on screen. What happens is that each view renders its layer, while myViewLayer is rendered merging together the two layers.

You can have as many layers as you like. You can create an arbitrary hierarchi by using the CALayer methods

– addSublayer: – removeFromSuperlayer
– insertSublayer:atIndex:
– insertSublayer:below:
– insertSublayer:above:
– replaceSublayer:with:

unforgiven
+1  A: 

Yes, it is possible, but I strongly advise against it.

The Apple Technical Note TN2230: Optimizing OpenGL ES for iPhone OS covers all do's and don'ts.

This Apple documentation covering best practices could help understanding some more do's and don'ts.

Kriem
Why do you advise against it? What's the reason?
Thanks
It hurts performance incredibly.
Kriem
A: 

This "technique" works very well and is quite common, as UIKit is designed just for this kind of thing, it also works the other way as well (OpenGL in the background or foreground or any combination). Just remember to turn off Opaque flag.

But, I am not sure about rendering GL to a CALayer that is actively being animated; it may just not update during its animation. =) Have fun!

humasect