I got myself a second-hand Samsung Galaxy S at last, and started hacking on it!
The very first thing I wanted to try was porting the OpenGL wikibook C++ samples, that we wrote with OpenGL ES 2 in mind.
I started writing a minimal GLUT-compatible wrapper to run the samples as-is, using the Android NDK, and I'm making progress :) The Android NDK is getting nicer with Android 2.3, though it still feels less supported than Java apps (e.g. the resize events seem buggy and the keycodes header is incomplete...). Nonetheless, it's nice to be able to write C++ portable apps.
You can see how to use the wrapper, and how it works internally, at:
http://en.wikibooks.org/wiki/OpenGL_Programming/Installation/Android
One of the limitations is that GLES2 is not a true subset of OpenGL 2.1, in particular:
- the shader version is declared differently (#version 100 vs. #version 120)
- GLES2 shaders require float precision hints, but OpenGL 2.1 doesn't support them at all
I needed to pre-process these differences away, checking on GL_ES_VERSION_2_0 in the C++ source code to define a GLES2 macro in the shaders:
#ifdef GLES2 varying lowp vec4 f_color; # else varying vec4 f_color; #endif
Is there a better way?
Thanks for the input.
I saw you can do:
though I was looking for a way to do without the pre-processing.
Also AFAICS you need to define precision for uniforms in the vertex shader as well.
Nice trick! I gotta test it when I get back home tonight.
OpenGL ES 2.0 implementations are required to have a GL_ES macro predefined in the shaders. Thus you can just do the following to get a shader that will work with both desktop GL and ES 2.0 without manually defining anything:
Also, in case you need it, keep in mind that high precision (highp) support is optional in the fragment shader. You can use the the GL_FRAGMENT_PRECISION_HIGH definition to check for it:
-- Alexandros
Thanks for these tips
They work great!
Here's the shader loading code now :
Unlike what I wrote there's no need to define precision in the vertex shader, it's implicit according to the specs