views:

364

answers:

2

When I program with OpenGL ES on iPhone OS, I have to use a lot of OpenGL functions. This looks very procedural to me. I wonder if I could make a class called "cube", which will draw an cube if called. Or must all the application code be in one huge scope?

+4  A: 

You can structure your code in any way you like. Yes, OpenGL commands are procedural imperative style, but you can built your own abstractions in e.g. object-oriented style on top of them. So you can have a cube object, that has certain properties, and then draw method that uses OpenGL commands to draw a cube with those properties.

For example our 2D library built on top of OpenGL has layers as objects and those layers form a hierarchy of layers where each layer is just responsible for drawing itself and applying transformations so that they apply to it's child layers. Thus, for example, we can construct easily complex shapes from simple shape layers (circles, rectangles, convex polygons, images ...) and by rotating a parent layer child layers rotate correctly as one complex shape.

tequilatango
I see... that's cool. Your lib is self-made? I think I'll have to create something similar to the whole UIKit / core animation system to do nice stuff. But I wonder if that makes sense, since a lot of abstractation is bad for performance, isn't it?
Thanks
Too much of an abstraction can be bad for performance, but it's unlikely that you overdo it that way. And you can always optimize later. We did our own library because we wanted to have identical library, programming style (quite functional) and idioms to what we have in Flash already. But I recommend that you start with e.g. Cocos2D for iPhone if you need 2D library or e.g. Oolong Engine or SIO2 engine if you need 3D.
tequilatango
+1  A: 

You can make classes in objective c, but there's also an interesting article with mixing the objective c with c++ to handle your classes. This is what I'm using for my OpenGLES application.

http://mitchallen.com/iphone/archives/64

ing0
I stuck with straight C for my OpenGL ES handling. I'm curious about adding C++ to Objective-C. Would you say it makes handling memory easier, harder, or doesn't affect it at all?
Nosredna
if that would be outside of cocoa: definitely harder. at least i believe that.
Thanks
It all depends on what you know already as far as easier / harder goes. Personally I've a lot of c++ experience so for me it was a million times easier as I find objective c's way of handling classes extremely horrible. It's not necessarily harder just because it's outside of cocoa, again it depends on your experience.
ing0