views:

244

answers:

4

Is OpenGL ES really so much faster? Why? And is it really so horrible complicated to draw such simple things in OpenGL ES compared to drawing these in Quartz 2D?

For example, I have a UIView subclass with -drawRect: implemented, where I draw some lines, curves and circles. Just simple paths, with some shadow.

What would I do in OpenGL ES? Isn't there this nice EAGLView layer thing? One thing that comes to mind is how do touch events go into OpenGL ES? Maybe that's the complex thing about it? Are there tutorials on basic drawing operations in OpenGL ES?

+1  A: 

For simple things, Quartz 2D works fine, and it's really easy. Only move to OpenGL if you start having performance issues.

Kristopher Johnson
I had them, after 3 a few lines and curves. About 5 only. It's crazy. Performance sucks so much, more than any industrial vacuum cleaner can do.
dontWatchMyProfile
Drawing a few lines and curves should not cause problems, unless you are doing it a lot. Are you trying to do high-speed animation or something? If so, you should clarify the question.
Kristopher Johnson
+2  A: 

They are two completely different things:

  • Quartz2D provides a sort of canvas on which you draw things using an API that has lots of commodities to do basic things like you said: lines, polygons, blitting images, bezier paths, shadows and so on.. of course you can't go over these capabilities because it's quite a high level API that can do many simple things but nothing more

  • OpenGL ES instead is a very low level API that allows you a complete control over what's going to be displayed, you will build up even simple things from primitives (then it depends if we're talking about ES1.0 or ES2.0, they are quite different). It's basically crossplatform and it's intended to be used to instruct the GPU about what to show and how to show it.

Even simple things can be complicated with OpenGL ES mainly because it's very flexible, so you are going to need to know many concepts, put all of them together, and create your scene. For example you don't have a real "circle" primitive, what you will do is to draw a shape made of many small pieces onto a texture, then draw the texture onto a real quad of your display and then use it to move your circle around.

Then everything in OpenGL is made with transformations matrices, that are tools used to rotate, translate or scale objects inside your scene. They are a really elegant and slick tool but they need a stronger mathematical background compared to what you usually do with Quartz2D.

In addition if you plan to develop with OpenGL ES2.0 instead that ES1.0 things get even more complex: prior to 2.0 you had a backward compatibility that allowed you to use basic functions like glTranslate or glRotate that were used to translate or rotate things that were painted. With 2.0 they removed all of them leaving just the shader approach, so everything you will develop are shaders, that will be compiled and sent to the GPU for execution: this is quite naughty at the beginning, you'll have to understand clearly how the OGL pipeline works, what fragment shaders or pixel shaders are and so on.

My two cents: OGL is far cooler than Quartz2D but it's quite hard to learn because it requires knowledge of many topics, and since OGL ES is a standard not so old the documentation somehow lacks (so it would be easier to start with plain OGL and then take what needed for ES).

A performance note: since OGL ES just uses the GPU to do the dirty work, it's really FASTER compared to Quartz2D.

Jack
what if I place an OGL ES view above normal UIViews? Wouldn't the composing kill all my performance benefits?
dontWatchMyProfile
Comparing Quartz and OpenGL ES is comparing apples to oranges. One is a 2-D drawing API, and the other is for hardware-accelerated rendering, typically of 3-D content. A better comparison would be Core Animation vs OpenGL ES, which I point out in my answer.
Brad Larson
@mystify - Yes, compositing transparent or translucent OpenGL ES content will dramatically reduce rendering performance, and Apple recommends against it in almost all cases. There are a few rare exceptions where this is necessary, such as augmented reality applications, but that doesn't sound like your case. However, if your OpenGL ES content is in an opaque CAEAGLLayer, you won't see this performance degradation if it is placed on top of another view or layer.
Brad Larson
thanks for that info. I guess I'll have to go for OpenGL ES then.
dontWatchMyProfile
+4  A: 

Based on your earlier question, I assume the reason you want to switch to OpenGL is to accelerate the animation of your drawn elements. In that question, you were attempting to animate by redrawing your UIView's contents with Quartz every frame. That will be incredibly slow, because of the way that the iPhone's drawing system works. Every frame, your view's backing layer would need to be converted to a texture and re-uploaded to the GPU, a very slow set of operations.

As has been pointed out, going the way of pure OpenGL ES for this will take a tremendous amount of time and effort, unless you are willing to use a third-party framework like Cocos2D. Even then, I bet that you'll find drawing complex 2-D elements to be as hard or harder than with Quartz.

Instead, my recommendation is to not try animating your content by redrawing it each frame in Quartz, but by layering your drawing and using Core Animation to move these layers around. Even on the original iPhone OS devices, I've found that I can animate 50 translucent layers around using Core Animation at 60 frames per second. Core Animation, using Quartz for drawing, is far easier to work with than OpenGL ES, and can let you achieve close to the same level of performance if done properly. Trust me, I've used both.

Even if you need to animate a vector element as it changes shape, you can use something like CAShapeLayer to handle that animation for you.

See also my answer to this question. For resources on getting started with Core Animation, see my answer here.

Brad Larson
thanks. what a pitty you don't live in russia. would have spent you some drink now ;) can you recommend an tutorial that helps me get started with OpenGL ES on the iphone for the purpose of drawing just simple shapes?
dontWatchMyProfile
@mystify - Wisconsin is cold enough for me. Most of the OpenGL ES tutorials I know of are geared towards 3-D graphics, such as the ones listed in this question: http://stackoverflow.com/questions/72288/learning-opengl-es-1-x or this one: http://stackoverflow.com/questions/1148143/what-are-some-good-iphone-3d-2d-opengl-es-game-tutorials . All of the best 2-D stuff I know is for Quartz and Core Animation.
Brad Larson
+1  A: 

A typical performance seeking journey:

  • drawRect
  • Core Animation (CA)
  • OpenGL

IMO:

  • if anything can be done in drawRect and also can be done by CA, use CA
  • if anything can be done in OpenGL and also can be done by CA, use CA

just my 0.02

ohho