开发者

How much more complex is it to draw simple curves, lines and circles in OpenGL ES rather than in Quartz 2D?

开发者 https://www.devze.com 2022-12-28 02:42 出处:网络
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?

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 the开发者_JS百科re 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?


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.


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号