[osg-users] OSG and OpenGL ES 2.0

Christian Ruzicka c.ruzicka at gmx.de
Wed Apr 6 08:53:30 PDT 2011


Hi,

it's just a really simple scene but it should help you figuring out, if your OpenGLES 2.0 support works (tested on iPhone):


Code:
// create geometry
osg::Group* scene = new osg::Group;
osg::Geode* geode = new osg::Geode;
osg::Geometry* geo = new osg::Geometry;
scene->addChild(geode);
geode->addDrawable(geo);

// set vertices
osg::Vec3Array* vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(0.0, 0.0, 0.0));
vertices->push_back(osg::Vec3(0.0, 0.0, 1.0));
vertices->push_back(osg::Vec3(1.0, 0.0, 0.0));
vertices->push_back(osg::Vec3(1.0, 0.0, 1.0));
geo->setVertexArray(vertices);

// set colors
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));
colors->push_back(osg::Vec4(0.0, 1.0, 0.0, 1.0));
colors->push_back(osg::Vec4(0.0, 0.0, 1.0, 1.0));
colors->push_back(osg::Vec4(1.0, 0.0, 1.0, 1.0));
geo->setVertexAttribArray(7, colors);
geo->setVertexAttribBinding(7, osg::Geometry::BIND_PER_VERTEX);

// set primitive set
geo->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, 4));
geo->setUseVertexBufferObjects(true);

// declare shaders
char vertSource[] =
"attribute vec4 osg_Vertex;\n"
"attribute vec4 a_col;"
"uniform mat4 osg_ModelViewProjectionMatrix;\n"
"varying vec4 v_col;"

"void main(void)\n"
"{\n"
"gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;\n"
"v_col = a_col;\n"
"}\n";

char fragSource[] =
"precision mediump float;\n"
"varying vec4 v_col;"

"void main(void)\n"
"{\n"
"gl_FragColor = v_col;\n"
"}\n";

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

// set scene in viewer...




HTH,
Christian

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








More information about the osg-users mailing list