OpenGL for Android and iPhone - iphone

In discussion with some colleagues we were wondering whether OpenGL work developed for Android or iPhone are effectively interchangeable given that both support the spec.
Or is the reality of sharing OpenGL between the two platforms more a case of quirks, tweaks and not as easy as one might have hoped.

An OpenGL implementation normally consists of two parts:
1. Platform specific part. This has function usually related to creating and displaying surfaces.
2. The OpenGL API. This part is the same on all platforms for the specific implementation of OpenGL, in the case of Android, OpenGLES 1.0.
What this means is that the bulk of your OpenGL code should be easy to port.
In C, you might have glLoadIdentity();
In Java on Android, something like gl.glLoadIdentity();
So for the bulk of your code you can cut and paste, and then search and replace prefixes like 'gl.'
Now for the fun part: you really need to be careful what version you are coding against. OpenGL for the desktop has APIs which don't exist in OpenGLES. There are also some OpenGL data types specific to each platform. In addition, you have 1.0 (e.g. Android) 1.1 (e.g. iPhone) 2.0 (e.g. iPhone GS) to deal with. The differences in API often have to do with additional hardware capability, so it's not like you can write some easy wrapper code to emulate 2.0 features in 1.0/1.1.

OpenGL ES on Android is done according to Khronos Java GLES spec JSR239 , and wraps GL calls in something like glinst.glBindBuffer(FloatBuffer.wrap(data) ... )
OpenGL ES on iPhone is done using stock GL.h files and the same call will just look like glBindBuffer(data...)
The code will not be interchangeable and will cause many quirks, even before you get into the whole mess of differences between 1.0 1.1 and 2.0 APis.

Both platforms use OpenGL ES, but Wikipedia claims that Android uses 1.0 while the iPhone uses 1.1 (original and 3g) an 2.0 for the 3gs link. It's likely that at least some programs will use api functions not included in 1.0, so there won't be full compatibility between the 2 (well 3).

Related

Cross-platform development with GLKit?

I've just installed the current Xcode 4 release and realized the new "GLKit" API thats also used in the OpenGL Template for iOS. So, I want to develop an cross-plattform game for iOS, Mac & Windows. GLKit seems like a step backwards in terms of portability, but the GLKit Features (specially like asynchronous texture loading) are great benefits...
How can I develop a cross-plattform game with GLKit? I would need to write any grapics code twice, right? Or is there something like an GLKit implementation for windows?
Any idea, workaround, code-design advice (how to divide the GLKit Code from other components?) is welcome - thanks!
I can't think of anything that GLKit does which cannot be replaced by non-GLKit code. It can make some OpenGL ES 2.0 tasks simpler by abstracting them away from you, but it doesn't do anything unique. The closest it comes to having something you can't replicate elsewhere (from what I've tinkered with) is the accelerated matrix and vector math functions it provides, but even those could be replaced with a little Accelerate or NEON code.
While I'd like to use GLKit within my applications, I'm still supporting iOS 4.2 with them, which doesn't have GLKit available. There's nothing that I'm currently prevented from doing because I can't use GLKit, I just have to write a little more code to handle things like view updates, texture uploads, matrix math, etc. There's plenty of documentation and examples out there for these operations, so you don't need to worry about being on your own when it comes to implementing them.
You're going to have much larger problems than GLKit when trying to make your code be cross-platform with the Mac, and expecially so with Windows. Desktop OpenGL and OpenGL ES have some differences in their implementations, and you'll need to be aware of the places to substitute one function or built-in variable for another. CAOpenGLLayer and NSOpenGLView on the Mac are also different from the CAEAGLLayer on iOS, and AppKit overall is a different animal than UIKit.
Windows will be even more of a challenge to support, because you won't be able to use any of the Cocoa frameworks, and most likely no Objective-C as well. This will take a lot more of your time than any OpenGL / OpenGL ES differences.

Transitioning from OpenGL ES 1.1 to OpenGL ES 2.0

It's been a while since iPhone 3GS came out, and now there might be enough market share of OpenGL ES 2.0 supporting devices to warrant developing in it.
But the situation is a lot of developers probably already have huge code bases in OpenGL ES 1.1
How does one transitionf rom ES 1.1 to ES 2.0? I suppose the matrices need to be taken care of, as well as stuff like GL_FOG, GL_CULL maybe?
Is it possible to write "substitutes" for these functions, e.g your own glTranslatef, glPushmatrix etc? Will this mean performance hit?
What other considerations are there for transitioning to ES 2.0? What advantages and disadvantages (besides the obvious device support issue) comes with using either of these?
Looking at the amount of es 2.0 tags compared to standard es tags in stackoverflow, it looks like it's not the time for 2.0 yet though.
Don't just go by activity in the tags on Stack Overflow when trying to determine whether or not to use OpenGL ES 2.0. For one thing, not every 2.0 or shader-related question is tagged as such. Also, a lot of information was present about OpenGL ES 1.1 at or soon after the launch of the iPhone SDK, so people are much more familiar with that API. There clearly is a lot of interest in OpenGL ES 2.0, as evidenced by the fact that my one class session on the subject is by far the most popular of all of my course videos.
For the most part, the way you handle your geometry will be the same between 1.1 and 2.0, as well as things like your framebuffers, but everything else shifts from being determined by built-in functions to your own shaders. You will have to write some code to replicate simple functions like using the model view matrix or texturing, but those tend to only require a few lines in a shader. For example, using the model view matrix to adjust your vertices is as simple as placing a line like this in your vertex shader:
vec4 transformedPosition = modelViewProjMatrix * position;
Personally, I replaced the glRotate(), etc. functions a long while ago using the Core Animation helper functions to manipulate what effectively is a model view matrix. This made it trivial to move that code across to OpenGL ES 2.0.
Jeff LaMarche also has an extremely useful helper class for wrapping most of your shader program setup code in his article here.
For a great guide on making the transition to OpenGL ES 1.1, see the "Migration from OpenGL ES 1.0 to OpenGL ES 2.0" article which is a chapter in the book GPU Pro and can be found within the documentation that accompanies the free PowerVR SDK.
I've explained what OpenGL ES 2.0 can be good for in my previous answers here and here, but perhaps it would be useful to demonstrate a before-and-after in regards to what the new API can give you.
OpenGL ES 1.1:
OpenGL ES 2.0:
Hopefully, you can see the payoff of replacing some of the built-in functions with shaders.
If you have existing projects, I wouldn't recommend moving to 2.0, considering the effort required is very likely more than it'd be worth. That said, for any new projects, there's no reason to even bother to consider 1.1 anymore in my opinion. Most of the devices that have been sold are 3GS' or 4s, both of which are more than capable of handling 2.0.
OpenGL ES 2.0 has a few common things with OpenGL ES 1.1 (in rendering i mean). For each rendering you'll need a shader. For example, FOG also is created with shader. But you will have more power.
+1 to #jer his answer

CoreGraphics Alternative?

I'm looking for a 2D rendering library as an alternative to CoreGraphics on iPhone. Everything in my app is pretty dynamic, which makes splitting things up into layers and animations rather hard.
I'm quite familiar with OpenGL, and that is how rendering is implemented right now. Everything would be so much easier to expand and development would go so much faster if I didn't have to worry about the low-level stuff in OpenGL, though (And my code would look so much neater :D).
I prefer C++ over Objective-C, so if you know any C++ libraries for rendering, that would be great. I can work with C too. Path-based rendering, like in CoreGraphics or the JavaScript Canvas API, would be beneficial. Would Cairo work on iPhone?
I've actually been working on my own 2D renderer, which I'll probably release even if I don't end up using in my app, because I enjoy working on it. Does the iPhone support the stencil buffer? I can do polygon triangulation, or use GLU's tesselation library, but the stencil buffer would safe a load of work in the long run.
Edit: Also, I've implemented rendering in this app with CoreGraphics before, and it didn't get as good of a frame rate as I'd like. I did some research, and people have suggested not using CoreGraphics for things that are constantly redrawing the screen. Some said CoreGraphics doesn't use the GPU, others said it was some caching mechanism. I've avoided it ever since.
MonkVG is an OpenVG 1.1 like vector graphics API implementation currently using an OpenGL ES backend that should be compatible with any HW that supports OpenGL ES 2.0 which includes most iOS and Android devices.
This is an open source BSD licensed project that is in active development. At the time of this writing it is in a very early pre-release state (very minimal features implemented). Contributors and sponsors welcome.
It can be found at GitHub http://github.com/micahpearlman/MonkVG
Also, there is a SVG and SWF (flash) renderers built on top of MonkVG:
MonkSVG
MonkSWF

iPhone cost vs. benefit - OpenGL ES 1.x vs 2.0

I'm not sure if this question has been asked already, my stackoverflow-fu has failed me.
So I'm building an OpenGL-ES-based iPhone game and pretty much all of the examples I've found out in the wild are on OpenGL ES 1.x. Which is fine because at least I'm (re)learning a lot about OpenGL in general.
Now that newer devices support OpenGL-ES 2.0, I'm wondering if anyone has ported their OpenGL-ES 1.x app to 2.0 and if so were there any performance or efficiency gains? For instance, I can setup my lighting (in 1.x) with glLightf(blahblah) and I'm done with lighting...but apparently that function doesn't exist in 2.0 so I'm forced to write it myself? So, how can somebody with no experience "programming the pipeline" accomplish this? Is there a default lighting implementation in 2.0?
I'm probably speaking out of ignorance as I haven't really found any solid iPhone-specific OpenGL-ES 2.0 information.
Any help in this space will be greatly appreciated.
From what I've read, and from my limited time working with it, going to OpenGL ES 2.0 from 1.1 isn't so much a matter of performance as it is about capabilities. If you watch the Mastering OpenGL ES for iPhone videos (part of the iPhone Getting Started Videos available through the iPhone Developer Program site), Apple even states that if you can do what you need to under OpenGL ES 1.1, you don't need to step up to 2.0.
OpenGL ES 2.0's fully programmable pipeline can make simple actions much harder than doing the same thing in 1.1, because you need to write code for parts of the pipeline that were handled for you before. However, 2.0 makes practical many stunning effects that you just couldn't do in 1.1. For example, I recommend watching the WWDC 2010 session video 417 - OpenGL ES Shading and Advanced Rendering and the Graphics and Media State of the Union to see what's possible using OpenGL ES 2.0.
To date, few applications have used OpenGL ES 2.0, given the limited subset of iPhone devices that had compatible GPUs and the lack of documentation and examples. I think we'll see this start to change as the pre-iPhone 3G S devices are phased out. In particular, the iPad has had OpenGL ES 2.0 from launch, so if you are designing an application for it you can rely on these capabilities to be there. More code examples and documentation are sure to appear in the near future.

Engine for use with PC and Iphone?

Are there any engines that allow me to develop for pc and iphone at the same time? My preferred language would be c#, but that probably won't happen, so I probably will learn c++ or java.
I want a 2d engine, by the way.
No experience with it but...
http://www.torquepowered.com/products/torque-2D/
C# and Java aren't allowed on the iPhone, so a C or C++ engine would be your best bet. I'm guessing you're doing a game--if so, you'll probably want to use OpenGL. I don't know any specifically, but here's a full list.
Working with pure Core Animation layers can yield cross-platform 2-D drawing and animation between iPhone, iPad, and Mac. As an example of this, the Core Plot framework runs on Mac and iPhone from the same codebase. Core Animation lets you do some pretty complex animations and layout in 2-D.
It is against the rules in OS 4 to write an iPhone app in something other than Apple's dev tools. However, IANAL but I'd expect that it is in theory ok to take the app you made there and then try to run it on an emulation layer etc.. on the /other/ platform(s). Not sure about direct solutions, but check out the GNU Objective-C runtime / GNUStep, they might be a helpful starting point.