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

Is OpenGL ES really much faster? What for? And is it not surprising that it is so difficult to do such simple things in OpenGL ES compared to drawing them in Quartz 2D?

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

What should I do in OpenGL ES? Isn't there such a nice thing EAGLView? One thing that comes to mind is how do touch events come into OpenGL ES? Maybe this is a tricky thing? Are there tutorials on basic drawing operations in OpenGL ES?

+2


a source to share


4 answers


Based on the previous question , I am assuming that the reason you want to switch to OpenGL is to speed up the animation of your drawn elements. In this question, you tried to animate by redrawing the content of your UIView with quartz on every frame. It will be incredibly slow due to the way the iPhone's drawing system works. Every frame, your view support layer has to be converted to a texture and reloaded to the GPU, very slow.

As already pointed out, the pure OpenGL ES path to do this will take a huge amount of time and effort unless you want to use a third party framework like Cocos2D . Even then, I'm sure you will find that the complexity of complex 2D elements is tricky or harder than with quartz.

Instead, my recommendation is not to try to animate your content by redrawing it every frame in Quartz, but by splitting up the picture and using Core Animation to move those layers around. Even on original iPhone OS devices, I found I could animate 50 semi-transparent layers using Core Animation at 60 frames per second. Core Animation using Quartz for drawing is much easier to work with than OpenGL ES, and can allow you to achieve similar performance levels if done correctly. Trust me, I've used both.



Even if you need to animate the vector element on reshaping, 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 .

+9


a source


These are two completely different things:

  • Quartz2D provides a canvas view on which you draw things using an API that has a lot of goods to do the basic things as you said: lines, polygons, blinking images, bezier paths, shadows, etc. Of course, you cannot use these capabilities, because it is a fairly high-level API that can do a lot of simple things, but nothing more.

  • OpenGL ES is a very low level API that allows you to have complete control over what is displayed, you will create even simple things from primitives (then it depends if we're talking about ES1.0 or ES2.0, they are completely different). is mostly cross-platform and it is intended to be used to tell the GPU what to show and how to show it.

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

Everything in OpenGL is then done with transformation matrices, which are tools used to rotate, translate, or scale objects within your scene. They are a really sleek and elegant tool, but they need a stronger mathematical background than what you usually do with Quartz2D.



Also, if you plan on developing with OpenGL ES2.0 instead of making ES1.0 things even more complex: before 2.0 you had backward compatibility that allowed you to use basic functionality like glTranslate

or glRotate

that was used to translate or turning things that have been drawn. With 2.0 they removed all of them, leaving only the shader approach, so all you develop are shaders that will be compiled and sent to the GPU for execution: this is completely naughty at the beginning, you will need to be clear about how OGL works what fragment shaders or pixel shaders etc.

My two cents: OGL is much cooler than Quartz2D, but it is quite difficult to learn it because it requires knowledge of many topics, and since OGL ES is standard, not so old, the documentation is somehow lacking (so it would be easier to start with plain OGL and then take what ES needs).

Performance note: Since OGL ES just uses the GPU to do the dirty work, it is indeed BASED compared to Quartz2D.

+2


a source


For simple things, Quartz 2D works great and it's really easy. Switch to OpenGL if you are having performance issues.

+1


a source


Typical search journey:

  • DrawRect
  • Core Animation (CA)
  • Opengl

MMO:

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

only mine 0.02

+1


a source







All Articles