Show
Ignore:
Timestamp:
11/04/09 12:03:13 (4 years ago)
Author:
robert
Message:

Added --simple option and associated set up of a very simple geometry and shader scene graph

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgvertexattributes/osgvertexattributes.cpp

    r10655 r10714  
    284284}; 
    285285 
    286  
     286osg::Node* createSimpleTestModel() 
     287{ 
     288    osg::Group* group = new osg::Group; 
     289     
     290    osg::Geode* geode = new osg::Geode; 
     291    group->addChild(geode); 
     292     
     293    osg::Geometry* geometry = new osg::Geometry; 
     294    geode->addDrawable(geometry); 
     295 
     296    osg::Vec3Array* vertices = new osg::Vec3Array; 
     297    vertices->push_back(osg::Vec3(0.0,0.0,0.0)); 
     298    vertices->push_back(osg::Vec3(0.0,0.0,1.0)); 
     299    vertices->push_back(osg::Vec3(1.0,0.0,0.0)); 
     300    vertices->push_back(osg::Vec3(1.0,0.0,1.0)); 
     301    geometry->setVertexArray(vertices); 
     302 
     303    geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, 4)); 
     304     
     305    char vertexShaderSource[] =  
     306       "void main(void)\n" 
     307       "{\n" 
     308       "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" 
     309       "}\n"; 
     310 
     311    char fragmentShaderSource[] =  
     312        "void main(void)\n" 
     313        "{\n" 
     314        "    gl_FragColor = vec4(1.0,1.0,0.0,1.0); \n" 
     315        "}\n"; 
     316 
     317    osg::Program* program = new osg::Program; 
     318    program->addShader(new osg::Shader(osg::Shader::VERTEX, vertexShaderSource)); 
     319    program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource)); 
     320 
     321    geometry->getOrCreateStateSet()->setAttribute(program); 
     322     
     323    return group; 
     324} 
    287325 
    288326int main(int argc, char *argv[]) 
     
    297335    while (arguments.read("-o",outputFileName)) {} 
    298336 
    299     bool runShaderGen = true; 
    300     while (arguments.read("--shader-gen")) { runShaderGen = true; } 
    301     while (arguments.read("--no-shader-gen")) { runShaderGen = false; } 
     337    osg::ref_ptr<osg::Node> loadedModel; 
    302338 
    303339    bool runConvertToVertexAttributes = true; 
    304     while (arguments.read("--vertex-attrib")) { runConvertToVertexAttributes = true; } 
    305     while (arguments.read("--no-vertex-attrib")) { runConvertToVertexAttributes = false; } 
    306  
    307     osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); 
    308     if (!loadedModel.get()) 
     340    if (arguments.read("--simple") || arguments.read("--s")) 
    309341    { 
    310         osg::notify(osg::NOTICE)<<"No model loaded, please specify a model filename."<<std::endl; 
    311         return 1; 
     342        loadedModel = createSimpleTestModel(); 
    312343    } 
    313  
    314     if (runShaderGen) 
     344    else 
    315345    { 
    316         // convert fixed function pipeline to shaders 
    317         osgUtil::ShaderGenVisitor sgv; 
    318         loadedModel->accept(sgv); 
     346        bool runShaderGen = true; 
     347        while (arguments.read("--shader-gen")) { runShaderGen = true; } 
     348        while (arguments.read("--no-shader-gen")) { runShaderGen = false; } 
     349 
     350        while (arguments.read("--vertex-attrib")) { runConvertToVertexAttributes = true; } 
     351        while (arguments.read("--no-vertex-attrib")) { runConvertToVertexAttributes = false; } 
     352 
     353        loadedModel = osgDB::readNodeFiles(arguments); 
     354        if (!loadedModel.get()) 
     355        { 
     356            osg::notify(osg::NOTICE)<<"No model loaded, please specify a model filename."<<std::endl; 
     357            return 1; 
     358        } 
     359 
     360        if (runShaderGen) 
     361        { 
     362            // convert fixed function pipeline to shaders 
     363            osgUtil::ShaderGenVisitor sgv; 
     364            loadedModel->accept(sgv); 
     365        } 
     366 
     367        if (runConvertToVertexAttributes) 
     368        { 
     369            // find any conventional vertex, colour, normal and tex coords arrays and convert to vertex attributes 
     370            ConvertToVertexAttibArrays ctvaa; 
     371            loadedModel->accept(ctvaa); 
     372        } 
    319373    } 
    320  
    321     if (runConvertToVertexAttributes) 
    322     { 
    323         // find any conventional vertex, colour, normal and tex coords arrays and convert to vertex attributes 
    324         ConvertToVertexAttibArrays ctvaa; 
    325         loadedModel->accept(ctvaa); 
    326     } 
    327  
     374     
     375    if (!loadedModel) return 1; 
     376     
    328377    if (!outputFileName.empty()) 
    329378    {