When to use OpenGL at iOS - ios

I am planning to develop a game which is actually a 2D collect'em up style game. There will be not much real time graphic synthesis, mostly there will be sprites floating around.
I know what I ask does not have a "%100 right" answer, but just want to hear opinions, when do you recommend using OpenGL, when to go straight with moving NSObjects on the screen with animations?

IMO it is always better to use OpenGL for graphics: moving, resizing, other transformation. And I suggest not to start from scratch but use some existing solution like http://www.cocos2d-iphone.org/

OpenGL is a 3D API, so if you are writing a 3D app then it is your only rational solution. If you are writing a 2D app, you would be better off using CoreGraphics as 2D animations are well supported and it is really quite easy to work with. Much easier than OpenGL at any rate. For example, rendering text is easy in a 2D app using UIKit and CoreGraphics but it is not trivial in OpenGL.

Related

OpenGL ES 2D Drawing Performance Estimation

I've been confronted with the extremely bad drawing Performance of Quartz/Core Graphics.
I don't believe its bad in every scenario, but in my occasion, where i need to redraw something like 3000 short lines frequently, it performs super bad.
Since the Modal (of MVC) is fixed I can not change how it spits out the data (if I could, i would have followed the advice, to only draw the changes, so the lines dont have to be redrawn every frame).
So as a conclusion I am considering using opengl for that purpose and I would like to ask u (experienced) guys for an estimation of how well it could work using opengl, before starting to work into that topic, as it seems by far more difficult
than Quartz.
You almost certainly see a speed performance lift from OpenGL over Quartz, however remember that whereas Quartz uses point to point drawing, OpenGL is based on the use of vertices and vertices points (essentially co-ordinates). You may find you need to do some mid-weight parsing work on your existing data source to re-work it into this vertices point system.
Also keep in mind that drawing text on top of an OpenGL ES object is a tricky task - it can be done (ironically) by using Quartz to generate an image, and then using this image as a texture.
I'd definitely recommend using OpenGL Kit as it will make life a bit easier for you as a beginner to OpenGL. Ray Wenderlich has an excellent starting point tutorial here :
http://www.raywenderlich.com/5223/beginning-opengl-es-2-0-with-glkit-part-1

For an iOS painting app, does it have to use either Quartz 2D or OpenGL?

I'm trying to implement a painting app with brush texture and blending, similar to that of an oil painting. I'm finding that even though Quartz 2D has been relatively simple to pick up, I've found it hard to implement the ideas of stroke texture/blending. Naively I tried hacking the shadow, and it looks "okay," but the performance sucks. I have a feeling that if I try to use a bitmap image for drawing I will have similar performance issues, but I do not know that to be the case. Meanwhile, the only alternative I see around is OpenGL, which feels like overkill and also is pretty intimidating. I've looked at GLPaint and I can't claim to understand some parts of it. Further working against me is that my knowledge of C is extremely limited and it really feels like if I understood C better I might understand OpenGL ES for iOS better as well.
Basically, I am wondering: given my requirement of producing an "oil painting" type effect for iOS painting, am I relegated to either the poor performance of Quartz 2D or learning the gigantic and honestly scary OpenGL framework? Are there any frameworks that "wrap" OpenGL functionality for mere mortals like myself?
Or, is there some other way of achieving stroke texture and blending that I am not aware of in Quartz?
iOS 5's GLKit will wrap a lot of OpenGL functionality for you, but it's going to be difficult to find an "oil painting library" because it is quite specific. If you want to work with graphics, sooner or later you are going to have to deal with OpenGL I imagine. It seems intimidating, but if you read a good book on it you will start to understand it better.
However, make sure you exhaust your possibilties on Quartz first. Perhaps you can find the function that is taking the most time and ask another question about how to improve its efficiency.
To answer the main question though, "yes".
However it focuses mainly on game development, there is a popular framework called cocos2d that wraps around OpenGL and provides simple APIs for 2D gfx development. If you decide to go on with OpenGL ES, I found this tutorial excellent and very comprehensible, even if it is focusing on 3D.

OpenGL vs Cocos2d: What to choose?

I understand that cocos2d it's really simple API, and that I can use it to do simple and huge 2D or even sometimes 3D games/applications. As well I understand that OpenGL it's more complicated, it's lower level API etc.
Question: What is better for implementing 2D/3D games? Why do we need to learn OpenGL if we have simple frameworks like cocos2d? What you can do with OpenGL that you can't do with cocos2d?
Thanks in advance!
What is better for implementing 2D/3D games?
Hard to tell, but a higher level API is always there to make things easier for you. For example you are writing a 2D shootem up. You will likely use a game loop, you will want to use sprites and make those move on the screen. You may want animations like explosions taking place. You'll end up writing your own higher level API to do those things. Cocos2D has solved those problems for you already. Any other frameworld should have solved it.
Why do we need to learn OpenGL if we have simple frameworks like cocos2d?
In case you like to cusomize the standard behaviour of a framework, especially the drawing part, you should get into openGL. If there is something you like to have which doesn't come out of the box you may find yourself reimplementing a base framework class. For example, look at the shaders used in Cocos2D 2.0. If you like some special blending mode, like a tinting effect, you won't get it for free. There is a colour attribute for a CCSprite but this may not be the result you're expecting. So you'll have to write your own shader and plug it into the sprite you like to be displayed in a different way.
What you can do with OpenGL that you can't do with cocos2d?
This comparison doesn't really work out, since cocos2d facilitates opengGL for the drawing part to build up that higher level api and make your life easier as a game developer.
Cocos2d is a wrapper around the 2D features of OpenGL (as of this: http://www.cocos2d-iphone.org/about) . Under the hood it itself uses OpenGL ES to implement its features. This is good because it means that there will be minimal performance overhead so you can start using its simpler API without having to get immersed to the definitely bigger learning path of OpenGL.
It has however only strong 2D support and if you plan to write later 3d games you loose all benefits of Cocos2d: why would you rewrite a 3d rendering engine with a 2d framework that under the hood uses a very strong 3d engine? You loose performance for a lot of unnecessary work.
So the simpler answer is: for 2d Cocos2d, for 3d OpenGL.
If you want to start OpenGL ES, this is a very good tutorial for beginners: http://iphonedevelopment.blogspot.it/2009/05/opengl-es-from-ground-up-table-of.html

Is Quartz2D or OpenGL more appropriate in my situation

I'm planning on developing a 2D game.It's a traffic control game with many different entities -lanes with variety of complexities, pedestrians,bike riders,cars with different privileges and off course traffic lights, etc. Although it's going to be 2D I want it to be as smooth as possible. The objects will mostly not be as realistic - a pedestrian, for example, will more like a cartoon personage than a real man- but the flow of the game should be natural. I'm having a little difficulty in making a decision as to whether to use Quartz or OpenGL. I read lots of threads in SO but I still need some more guidance. Thank you a lot.
For the performance view, OpenGL will be the best. Cocos2d a link is a very good framework, you can put images on canvas with very good performance.
I haven't use GLKit (from iOS5), but you can put OpenGL view in the UIKit, that will be good if you still would like to draw using core graphics, you can layer the UIKit and OpenGL.
I personally recommends Kobold2d: http://www.kobold2d.com/display/KKSITE/Home because it comes with many sample projects, you can start changing from.

iOS: image drawing performance

I have several fundamental questions to clearify to myself, hope you'll help...
I'm developing a game application. It has game loop and on each iteration i draw about 200 sprites. What is the fastest way of drawing bitmap? Currently I use [UIImage drawAtPoint] method to draw a sprite (sprite sizes can be 16x16 ... 64x64), but something tells me, this is not the best approach. As I understand, UIImage is based on Quartz technology? Can cocos2d provide the better performance?
I also thought about using OpenGL ES to reproduce drawing mechanism via textured objects.
If you're making a game you'll probably want to avoid Quartz/UIKit/CoreAnimation. They're designed to be simple and easy to use, at the cost of performance.
Opengl is the fastest way to draw sprites to the screen on the iPhone. There are two versions of Opengl in iOS: OpenglES 1.1 and OpenglES 2.0. If you're looking to support the iphone 3G and earlier, you'll need to either use 1.1 exclusively, or be able to determine the device you're running on and switch versions on the fly. Opengl is very fast but at the cost of a fairly steep learning curve and a lot of what you'd see as boilerplate code.
Cocos2d is an iOS game engine built on top of Opengl. It's designed to make using opengl easier (among many other things). If you're making a game, I'd suggest you use cocos2d as they've done a lot of the hard work already. If you don't you'll be spending 99% of your time getting opengl to work, rather than focusing on the game.
Good luck! :)

Resources