From 211ea2f26351957f82365aa2840f0acdf4ad5a24 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 4 Nov 2009 11:03:13 +0000 Subject: [PATCH] Added --simple option and associated set up of a very simple geometry and shader scene graph --- .../osgvertexattributes.cpp | 93 ++++++++++++++----- 1 file changed, 71 insertions(+), 22 deletions(-) diff --git a/examples/osgvertexattributes/osgvertexattributes.cpp b/examples/osgvertexattributes/osgvertexattributes.cpp index 1cea2014e..9f748799b 100644 --- a/examples/osgvertexattributes/osgvertexattributes.cpp +++ b/examples/osgvertexattributes/osgvertexattributes.cpp @@ -283,7 +283,45 @@ class ConvertToVertexAttibArrays : public osg::NodeVisitor AttributeAlias _texCoordAlias[8]; }; +osg::Node* createSimpleTestModel() +{ + osg::Group* group = new osg::Group; + + osg::Geode* geode = new osg::Geode; + group->addChild(geode); + + osg::Geometry* geometry = new osg::Geometry; + geode->addDrawable(geometry); + 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)); + geometry->setVertexArray(vertices); + + geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, 4)); + + char vertexShaderSource[] = + "void main(void)\n" + "{\n" + " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" + "}\n"; + + char fragmentShaderSource[] = + "void main(void)\n" + "{\n" + " gl_FragColor = vec4(1.0,1.0,0.0,1.0); \n" + "}\n"; + + osg::Program* program = new osg::Program; + program->addShader(new osg::Shader(osg::Shader::VERTEX, vertexShaderSource)); + program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource)); + + geometry->getOrCreateStateSet()->setAttribute(program); + + return group; +} int main(int argc, char *argv[]) { @@ -296,35 +334,46 @@ int main(int argc, char *argv[]) std::string outputFileName; while (arguments.read("-o",outputFileName)) {} - bool runShaderGen = true; - while (arguments.read("--shader-gen")) { runShaderGen = true; } - while (arguments.read("--no-shader-gen")) { runShaderGen = false; } + osg::ref_ptr loadedModel; bool runConvertToVertexAttributes = true; - while (arguments.read("--vertex-attrib")) { runConvertToVertexAttributes = true; } - while (arguments.read("--no-vertex-attrib")) { runConvertToVertexAttributes = false; } - - osg::ref_ptr loadedModel = osgDB::readNodeFiles(arguments); - if (!loadedModel.get()) + if (arguments.read("--simple") || arguments.read("--s")) { - osg::notify(osg::NOTICE)<<"No model loaded, please specify a model filename."<accept(sgv); - } + bool runShaderGen = true; + while (arguments.read("--shader-gen")) { runShaderGen = true; } + while (arguments.read("--no-shader-gen")) { runShaderGen = false; } - if (runConvertToVertexAttributes) - { - // find any conventional vertex, colour, normal and tex coords arrays and convert to vertex attributes - ConvertToVertexAttibArrays ctvaa; - loadedModel->accept(ctvaa); - } + while (arguments.read("--vertex-attrib")) { runConvertToVertexAttributes = true; } + while (arguments.read("--no-vertex-attrib")) { runConvertToVertexAttributes = false; } + loadedModel = osgDB::readNodeFiles(arguments); + if (!loadedModel.get()) + { + osg::notify(osg::NOTICE)<<"No model loaded, please specify a model filename."<accept(sgv); + } + + if (runConvertToVertexAttributes) + { + // find any conventional vertex, colour, normal and tex coords arrays and convert to vertex attributes + ConvertToVertexAttibArrays ctvaa; + loadedModel->accept(ctvaa); + } + } + + if (!loadedModel) return 1; + if (!outputFileName.empty()) { osgDB::writeNodeFile(*loadedModel, outputFileName);