How can I draw an Ellipse using openGL ES
There are many good tutorials that help you to learn Open GL ES. These tutorials particularly give examples to draw Ellipse, Circle, Square etc..
Some are here,
OpenGL ES for iPhone tutorial - By Simon Maurice
Open GL 2.0 for iPhone tutorial - by Ray Wenderlich
Resources for iPhone OpenGL ES - Beginners / Intermediate
Best Wishes
Draw a bunch of line segments around the circumference.
Also note that your system may not support anti-aliased lines (the iPhone doesn't). There's a nice article on drawing antialiased lines here. You can do something similar to draw the line segments (though you don't need the end-caps, so it's a bit simpler).
Another solution to anti-aliasing that requires more polygons is here.
Related
I'm not looking for a library or even open source code. I want to learn how to do this on my own.
Where do I start to find an online tutorial, a book chapter, or other educational material for generating a polygonal model of a 3D sphere suitable for feeding to Open GL ES on an iPhone, and then mapping the polygons to some sort of 2D map data so I can texture map the sphere? Is there some sort of software tool (blender? maya?) with a tutorial on how to do generate this data? Where is the best place to start?
How about these articles?
Procedural Spheres in OpenGL ES
OpenGL ES From the Ground Up, Part 6: Textures and Texture Mapping
I've heard good stuff about "iPhone 3D Programming". Jeff LaMarche also recommends it here.
Hope this helps!
While not OpenGL ES, I once tried porting across the examples from this chapter in the Red Book where they show how to create an icosahedron and subdivide it to produce smooth spheres. I only got as far as using a simple icosahedron to crudely represent a sphere in the code for my Molecules application. Perhaps you could extend that.
Apple has a Mac sample application, GLSLShowpiece, that textures a sphere in a couple of places, but they use gluSphere() to generate the sphere vertices, which is unavailable in OpenGL ES.
To be honest, I'm in the process of replacing the sphere rendering code in Molecules with a 2-D billboarding approach that uses shaders to generate the sphere coloring. This should allow for far smoother spheres without having to resort to massive amounts of geometry. See this paper for the kind of results you can produce this way.
I need help setting up multi-pass rendering with OpenGL ES 2.0 on the iPhone. I haven't been able to find an example which implements both rendering to a texture and multi-pass shading.
I'm looking for some instructions and sample code which implement:
First stage: Render to a texture
Second stage: Input that texture and render to screen
I have referenced Apple's OpenGL ES Programming Guide, OpenGL Shading Language (Orange Book), and O'Reilly's iPhone 3D Programming Book.
The Orange Book discusses deferred shading and provides two shader programs for first-pass and second-pass rendering, but doesn't provide example code to setup that application or show how to communicate data between both shaders.
Questions:
How to render to texture?
Using glDrawElements
How to input that texture to the next pass?
How to implement two shading programs?
How to alternate first- and second-pass shading programs?
Need to attach, detach, and call 'use' for each pass?
How to implement multi-pass shading?
I wrote a short example of doing just this (multiple render-to-texture passes on the iPhone using OpenGL ES 2.0) a few weeks ago: http://www.mat.ucsb.edu/a.forbes/blog/?p=245
**
Edit, this post is a bit old, and it has moved here:
http://blog.angusforbes.com/openglglsl-render-to-texture/
**
Ok, first of all: I'm no expert on OpenGL ES 2.0. I was kind of in the same situation where I wanted to do a multipass render setup, in one of my first OpenGL ES applications.
I also used the Orange Book. Check chapter 12. Framebuffer Objects > Examples. The first example demonstrates how to use a framebuffer to render to a texture, and then draws that texture to screen.
Basically using that example I created an application that renders some geometry to a texture using an effect shader, then renders that texture to screen, layered with some other content all using a different shader.
I'm not sure if this is the best approach, but it works for my purposes. My setup:
I create two framebuffers, the default and an offscreen one. Same for the renderbuffers
I create a texture which the app will render to
I bind the offscreen framebuffer, and attach the texture to it using glFramebufferTexture2D
My rendering:
bind the offscreen framebuffer.
use my first shader program
draw my geometry
bind the default framebuffer
use my second shader program
draw a fullscreen quad with the texture attached to it.
I have some development experience of CocoaTouch Layer, Core Service Layer iPhone Applications.
Now, I have to migrate for some pure 2d and 3d graphical iPhone Applications.
For that as per my knowledge, I need following frameworks to learn :
1) Core Graphics
2) OpenGL ES
3) Quartz Core
I want to know that what should be the approach to learn this framework quickly.
Or else which framework to learn and which is not, and using which source I should learn this frameworks.
Thanks in Advance
Core Graphics
For this, I'd first recommend reading the Quartz 2D Programming Guide. It's the best set of documentation you'll find out there on the subject. Next, I'd read through some of the questions here tagged core-graphics, quartz-2d, or quartz-graphics. There are some great source code examples in the answers to those questions.
OpenGL ES
There are many "getting started with OpenGL ES" questions here, with links to many resources. These include:
Books on OpenGL ES targeted towards the iPhone
Learning OpenGL ES 1.x
What do I have to learn to get done with a 3D racing game for the iPhone? What Tools do I need?
Want to display a 3D model on the iPhone: how to get started?
I personally recommend the book "Mobile 3D Graphics: with OpenGL ES and M3G", as well as the "OpenGL ES Programming Guide for iPhone OS". My application Molecules is open source and it uses OpenGL ES. Finally, Jeff LaMarche has a great tutorial series on OpenGL ES.
Core Animation
By Quartz Core, you probably mean Core Animation. Again, I'd start with Apple's Core Animation Programming Guide, then take a look at the code examples you can find here under the various core-animation tagged questions. Beyond that, Bill Dudney's Core Animation for Mac OS X and the iPhone book is a very good introduction, and Marcus Zarra and Matt Long's Core Animation book looks to go into more detail on the subject.
I'm not sure there is a quick way really. The docs are pretty vast. And you need a pretty solid knowledge of fundamental graphics principles. I had trouble getting my head round the concepts and I have used a lot of graphics toolkits.
Couple of points that might be useful:
when trying to draw a rotated image, you need to translate the coordinate transformation back to the QUARTZ origin (bottom left) not some custom origin you set up eg top-left
SO you're best off (unless you know you and no one else maintaining your code will ever want to daw rotated images) accepting the possibly new to you ideas of using bottom left coordinates (where appropriate) rather than trying to override them by changing the context CTMs
don't assume that Core Graphics can do everything you want. You may need to manipulate pixels manually yourself for some tasks
design with the memory limitations and numbers and sizes of buffers to do a processing sequence firmly in mind
design with the texture size limitation of 1024 pixels in mind
make sure you use algorithms and approaches that are within the capabilities of the CPU and/or GPU; test with a mock-up first to sound out the performance issues
if doing 3D animations in OpenGL ES make sure that you have a way to reconcile and synchronize real time with the physics model of whatever you are animating
consider colour management, shading and highlighting, pay particular attention to the device's capabilities there
where possible use Core Animation instead of Core Graphics
On an iPhone:
If I am writing a game that has multiple levels, with multiple animations (image sequences), jpg and png (transparent), some full screen and some not, some looped and some played once only. What is the best way of doing it? Each level might have up to 10MB of images. Add on to this music, and video (cut scenes). All 2D graphics, no 3D models.
Is OpenGL required? Or can this be achieved with Quartz or Core Animation?
I do similar using UIViews and a bit of Core Graphics (Quartz 2D) and it works fine. I've found the custom drawing in Core Graphics pushes it a bit further, tho - UIViews work best when given images rather than having to draw themselves. Also watch out for lots of transparencies. You'll probably find that large or long (many frame) animations will be the killer, though. There are some techniques for minimising the impact of the animations which involves allowing it to purge images from memory if not being immediately displayed (I forget the setting). This may result in your animations not being as smooth as you they would otherwise be (not sure if Open GL ES would help here, though).
You should probably prototype using UIViews, and decide then if it's worth doing the extra work for OpenGL ES. Also, if you're not already familiar with OpenGL/ Open GL ES it's a steep learning curve.
I've used both Quartz and OpenGL to do graphics on the iPhone, and while OpenGL has a much higher learning curve, it gives much better performance than Quartz. Let's say you have a scene that involves drawing 6 large, semi-transparent images on top of each other. Quartz will do it, but you'll probably get 15fps at best. OpenGL takes advantage of the iPhone's PowerVR chip and the drawing is hardware accelerated - so you can load those images into OpenGL textures and render at 25-30fps no problem.
I would agree with Phil though - try doing it using Quartz and see if it meets your needs. OpenGL is extremely powerful but it's API lacks some of the convenience features of Quartz (such as saving/restoring graphics state).
One another note entirely, you might want to take a look at Unity's iPhone development tools (http://unity3d.com/#iphone). They leverage OpenGL but provide you with an IDE to create your game. It abstracts away all of the graphics-level code, so you can focus on the high-level gameplay. My brother uses it to write iPhone games, and it's extremely cool.
I recommend having a look at Cocos2D iPhone.
cocos2d for iPhone is a framework for building 2D games, demos, and other graphical/interactive applications. It is based on the cocos2d design: it uses the same API, but instead of using python it uses objective-c.
Most likely OpenGl.
One advantage of using OpenGL ES would be that the investment of time for learning the technology could be applied to other platforms/contexts and your game is potentially more port-friendly. These may not be important to you.
I would suggest using Quartz. OpenGL ES is really best for 3d stuff. However both work fairly well, so if you already know OpenGL ES, it's fine to use that.
You should consider using a lot of less resources in your game, Apple recommends not to use more than 10 mb in texture for openGL apps.
Try texture atlas, reuse graphics, tile based graphics...but avoid to use to much graphic assets.
I want to do the following:
Tap the screen and draw 3 cricles around the the tapped point.
Is it better to do this with Core Animation or OpenGL ES?
Where do I start?
My experience is this: the more complex my app became, the more I realized I should have had used OpenGL ES for what I was trying to do.
So, for your situation, If what you described is all there is, sure, Core Graphics does the trick. But, I'm guessing there's more to it than three circles.
With no experience with OpenGL at all, the learning curve for ES was about 20 days.
Thus, my advice is: OpenGL ES for pretty much every frame-to-frame graphics based app.
As mentioned, the Core Graphics framework is probably what you want. A good way to go about it would be to subclass UIView, then override the two methods drawRect: and touchesEnded:withEvent:.
When a touch event ends on the UIView, you can get the point of the last touch from the event passed to touchesEnded:withEvent:, and store it somehow in the instance of your subclassed UIView.
Then, in your implementation of drawRect:, you'll get the stored last touch point, and draw three circles around it using three calls to CGContextAddEllipseInRect, as discussed here: Quartz 2D Programming Guide: Paths (registration as Apple Developer required).
The advantage of learning OpenGL ES is that the time you put in to learn it will serve you well in the future on iPhone Apps and on other devices.
In OpenGL ES, there's no built-in way to draw a circle, so use sine and cosine to build your circles out of line segments.
Core Graphics is definitely simpler, and better for 2D. OpenGL ES is made for 3D, but can also be used for 2D. Both can be used, so if you already know one, use that. It shouldn't really matter that much.
I already knew OpenGL, so I tend to use OpenGL ES even for 2D, but if you haven't used either before, go with Core Graphics.
This could best be done with Quartz 2D (also known as Core Graphics)
See Apple's Quartz programming guide