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?
views:
364answers:
2You 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.
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.