Hello,

In this post I want to report my progress on using OSG with OpenGL ES 2.0 on 
the IPhone.

I am using OSG for my master thesis at the HPI. I needed a cross platform 
rendering system which supports OpenGL ES 2.0 (to have shader support on iOS). 
OSG was the only mature rendering system offering OpenGL ES 2.0 support and 
having some progress on running it on the IPhone.

Building and setting up OSG on Mac and Windows was pretty easy. Luckily, there 
is an OSG fork on GitHub which has an IPhone branch (stmh/osg/tree/iphone). 
Using that branch, I managed to get an OSG sample running on the IPhone 
Simulator. 

I am using Version 4 of the iOS SDK, so I adjusted the Xcode project 
(IPhone_Project/OSGIPhone.xcodeproj) to use version 4 as the base SDK and 
changed the target to version 4 for the simulator.

The only code change I had to make it get it running was:

Code:
diff --git a/src/osgDB/FileUtils.cpp b/src/osgDB/FileUtils.cpp
index 573886a..073beb8 100644
--- a/src/osgDB/FileUtils.cpp
+++ b/src/osgDB/FileUtils.cpp
@@ -51,7 +51,7 @@ typedef char TCHAR;
//>OSG_IPHONE
//IPhone includes
#include "TargetConditionals.h"
- #if (TARGET_OS_IPHONE) && !(TARGET_IPHONE_SIMULATOR) //only when on device
+ #if (TARGET_OS_IPHONE)
#define stat64 stat
#endif
//<OSG_IPHONE


The sample was running, but only using OpenGL ES 1.x...

Ok. So I started changing OSG's build config (IPhone_Project/config/osg/Config):

Code:
#ifndef OSG_CONFIG
#define OSG_CONFIG 1

//static link needed on IPhone
#define OSG_LIBRARY_STATIC

#define OSG_USE_FLOAT_MATRIX 
/* #undef OSG_USE_FLOAT_PLANE */
#define OSG_USE_FLOAT_BOUNDINGSPHERE
#define OSG_USE_FLOAT_BOUNDINGBOX
#define OSG_USE_REF_PTR_IMPLICIT_OUTPUT_CONVERSION
/* #undef OSG_USE_UTF8_FILENAME */
#define OSG_DISABLE_MSVC_WARNINGS

/* #undef OSG_GLU_AVAILABLE */
/* #undef OSG_GL1_AVAILABLE */
/* #undef OSG_GL2_AVAILABLE */
/* #undef OSG_GL3_AVAILABLE */
/* #undef OSG_GLES1_AVAILABLE */
#define OSG_GLES2_AVAILABLE 


/* #undef OSG_GL_DISPLAYLISTS_AVAILABLE */
/* #undef OSG_GL_MATRICES_AVAILABLE */
/* #undef OSG_GL_VERTEX_FUNCS_AVAILABLE */
/* #undef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE */
/* #undef OSG_GL_FIXED_FUNCTION_AVAILABLE */

#endif



Next location for adjustments was the GraphicsWindowIPhone 
(src/osgViewer/GraphicsWindowIPhone.mm). GL functions which were suffixed with 
OES for OpenGL ES 1 do not have this suffix in OpenGL ES 2. So I changed the 
following:

Code:
diff --git a/src/osgViewer/GraphicsWindowIPhone.mm 
b/src/osgViewer/GraphicsWindowIPhone.mm
index 9dab961..36cf916 100755
--- a/src/osgViewer/GraphicsWindowIPhone.mm
+++ b/src/osgViewer/GraphicsWindowIPhone.mm
@@ -11,6 +11,28 @@
#import <OpenGLES/ES1/glext.h>
#else
#import <OpenGLES/ES2/glext.h>
+// in GLES2, the OES suffix if dropped from function names
+#define glGenFramebuffersOES glGenFramebuffers
+#define glGenRenderbuffersOES glGenRenderbuffers
+#define glBindFramebufferOES glBindFramebuffer
+#define glBindRenderbufferOES glBindRenderbuffer
+#define glFramebufferRenderbufferOES glFramebufferRenderbuffer
+#define glGetRenderbufferParameterivOES glGetRenderbufferParameteriv
+#define glRenderbufferStorageOES glRenderbufferStorage
+#define glDeleteRenderbuffersOES glDeleteRenderbuffers
+#define glDeleteFramebuffersOES glDeleteFramebuffers
+#define glCheckFramebufferStatusOES glCheckFramebufferStatus
+
+#define GL_FRAMEBUFFER_OES GL_FRAMEBUFFER
+#define GL_RENDERBUFFER_OES GL_RENDERBUFFER
+#define GL_RENDERBUFFER_WIDTH_OES GL_RENDERBUFFER_WIDTH 
+#define GL_RENDERBUFFER_HEIGHT_OES GL_RENDERBUFFER_HEIGHT
+#define GL_COLOR_ATTACHMENT0_OES GL_COLOR_ATTACHMENT0
+#define GL_DEPTH_ATTACHMENT_OES GL_DEPTH_ATTACHMENT
+#define GL_DEPTH_COMPONENT16_OES GL_DEPTH_COMPONENT16
+#define GL_STENCIL_INDEX8_OES GL_STENCIL_INDEX8
+#define GL_FRAMEBUFFER_COMPLETE_OES GL_FRAMEBUFFER_COMPLETE
+#define GL_STENCIL_ATTACHMENT_OES GL_STENCIL_ATTACHMENT
#endif

#include "IPhoneUtils.h"



Ok. Now OSG builds completely with the OpenGL ES 2 config. But running the 
sample application provided with the IPhone branch 
(IPhone_Project/iphoneExamples/simple/) fails while compiling the shaders. This 
happens due to quite hard restrictions in GLSL ES. This issue seems similar to 
thread #6120 (ShaderGen and OpenGL ES2). I started hacking around in 
src/osgUtil/ShaderGen.cpp, but I think the cleaner solution is to provide 
custom shaders. 

So finally, the OSG application code I am currently running on the IPhone 
(OpenGL ES 2), Windows (OpenGL 2) and Mac OS X (OpenGL 2) looks like this (only 
the essential parts shown):

Code:

#define SHADER_COMPAT \
"#ifndef GL_ES\n" \
"#if (__VERSION__ <= 110)\n" \
"#define lowp\n" \
"#define mediump\n" \
"#define highp\n" \
"#endif\n" \
"#endif\n"

static const char* vertSource = {
SHADER_COMPAT
"// colors a fragment based on its position\n"
"varying mediump vec4 color;\n"
"void main(void) {\n"
"color = gl_Vertex;\n"
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
"}\n"
};

static const char* fragSource = {
SHADER_COMPAT
"varying mediump vec4 color;\n"
"void main(void) {\n"
"gl_FragColor = clamp( color, 0.0, 1.0 );\n"
"}\n"
};

void Application::init() {

osg::setNotifyLevel(osg::INFO);

m_root = new osg::MatrixTransform();

osg::Geode* geode = new osg::Geode();
osg::ShapeDrawable* drawable = new osg::ShapeDrawable(new osg::Box(osg::Vec3(1, 
1, 1), 1));
geode->addDrawable(drawable);

osg::Program* program = new osg::Program;
program->setName("shader");
program->addShader(new osg::Shader(osg::Shader::VERTEX, vertSource));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragSource));
geode->getOrCreateStateSet()->setAttributeAndModes(program, 
osg::StateAttribute::ON);

m_root->addChild(geode);

m_viewer = new osgViewer::Viewer();

#if !defined(TARGET_OS_IPHONE)
m_viewer->setUpViewInWindow(100, 100, 800, 600, 0);
#endif

m_viewer->setSceneData(m_root.get());
m_viewer->setCameraManipulator(new osgGA::TrackballManipulator);
}




My next step will be to make a textured model load. Here again, the generated 
shaders fail to compile.

Well, I now, this is all no rocket science, but I thought it might be 
interesting for someone though.
Any comments, suggestions and hints are very welcome!

rti

PS: Sorry, the code is not indented, but somehow the forum screws up TABs and 
two or more spaces in a row.
PPS: Sorry, that there are no links in that post, but the forum did not allow 
it since this is my first post.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=31049#31049





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to