Show
Ignore:
Timestamp:
09/19/14 12:35:23 (4 weeks ago)
Author:
robert
Message:

Moved widgets from VolumeEditorWidget? to TransferFunctionWidget?, and widget utilities into WidgetUtils?.

Files:
1 modified

Legend:

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

    r12886 r13427  
    3232#include <osg/Switch> 
    3333#include <osg/Texture2D> 
     34#include <osg/TextureBuffer> 
     35#include <osg/Image> 
    3436#include <osg/TexEnv> 
    3537#include <osg/VertexProgram> 
     
    142144 
    143145    osg::Node* createGeometryShaderGraph(Cell* cell, osg::StateSet* stateset); 
     146     
     147    osg::Node* createTextureBufferGraph(Cell* cell, osg::Geometry* templateGeometry); 
     148     
    144149    void CollectTreePositions(Cell* cell, std::vector< osg::Vec3 >& positions); 
    145150 
    146151    osg::Node* createHUDWithText(const std::string& text); 
    147152 
    148     osg::Node* createScene(unsigned int numTreesToCreates); 
     153    osg::Node* createScene(unsigned int numTreesToCreates, unsigned int maxNumTreesPerCell); 
    149154 
    150155    void advanceToNextTechnique(int delta=1) 
     
    10061011} 
    10071012 
     1013osg::Node* ForestTechniqueManager::createTextureBufferGraph(Cell* cell, osg::Geometry* templateGeometry) 
     1014{ 
     1015    bool needGroup = !(cell->_cells.empty()); 
     1016    bool needTrees = !(cell->_trees.empty()); 
     1017 
     1018    osg::Geode* geode = 0; 
     1019    osg::Group* group = 0; 
     1020 
     1021    if (needTrees) 
     1022    { 
     1023        osg::Geometry* geometry = (osg::Geometry*)templateGeometry->clone( osg::CopyOp::DEEP_COPY_PRIMITIVES ); 
     1024        osg::DrawArrays* primSet = dynamic_cast<osg::DrawArrays*>( geometry->getPrimitiveSet(0) ); 
     1025        primSet->setNumInstances( cell->_trees.size() ); 
     1026        geode = new osg::Geode; 
     1027        geode->addDrawable(geometry); 
     1028         
     1029        osg::ref_ptr<osg::Image> treeParamsImage = new osg::Image; 
     1030        treeParamsImage->allocateImage( 3*cell->_trees.size(), 1, 1, GL_RGBA, GL_FLOAT ); 
     1031         
     1032        unsigned int i=0; 
     1033        for(TreeList::iterator itr=cell->_trees.begin(); 
     1034            itr!=cell->_trees.end(); 
     1035            ++itr,++i) 
     1036        { 
     1037            osg::Vec4f* ptr = (osg::Vec4f*)treeParamsImage->data(3*i); 
     1038            Tree& tree = **itr; 
     1039            ptr[0] = osg::Vec4f(tree._position.x(),tree._position.y(),tree._position.z(),1.0); 
     1040            ptr[1] = osg::Vec4f((float)tree._color.r()/255.0f,(float)tree._color.g()/255.0f, (float)tree._color.b()/255.0f, 1.0); 
     1041            ptr[2] = osg::Vec4f(tree._width, tree._height, 1.0, 1.0); 
     1042        } 
     1043        osg::ref_ptr<osg::TextureBuffer> tbo = new osg::TextureBuffer; 
     1044        tbo->setImage( treeParamsImage.get() ); 
     1045        tbo->setInternalFormat(GL_RGBA32F); 
     1046        geometry->getOrCreateStateSet()->setTextureAttribute(1, tbo.get()); 
     1047        geometry->setInitialBound( cell->_bb ); 
     1048    } 
     1049 
     1050    if (needGroup) 
     1051    { 
     1052        group = new osg::Group; 
     1053        for(Cell::CellList::iterator itr=cell->_cells.begin(); 
     1054            itr!=cell->_cells.end(); 
     1055            ++itr) 
     1056        { 
     1057            group->addChild(createTextureBufferGraph(itr->get(),templateGeometry)); 
     1058        } 
     1059 
     1060        if (geode) group->addChild(geode); 
     1061 
     1062    } 
     1063    if (group) return group; 
     1064    else return geode; 
     1065} 
    10081066 
    10091067 
     
    11041162} 
    11051163 
    1106 osg::Node* ForestTechniqueManager::createScene(unsigned int numTreesToCreates) 
     1164osg::Node* ForestTechniqueManager::createScene(unsigned int numTreesToCreates, unsigned int maxNumTreesPerCell) 
    11071165{ 
    11081166    osg::Vec3 origin(0.0f,0.0f,0.0f); 
     
    11211179    osg::ref_ptr<Cell> cell = new Cell; 
    11221180    cell->addTrees(trees); 
    1123     cell->divide(); 
     1181    cell->divide(maxNumTreesPerCell); 
    11241182    std::cout<<"done."<<std::endl; 
    11251183 
     
    11541212        osg::Group* group = new osg::Group; 
    11551213        group->addChild(createBillboardGraph(cell.get(),dstate)); 
    1156         group->addChild(createHUDWithText("Using osg::Billboard's to create a forest\n\nPress left cursor key to select osg::Vertex/Geometry/FragmentProgram shader based forest\nPress right cursor key to select double quad based forest")); 
     1214        group->addChild(createHUDWithText("Using osg::Billboard's to create a forest\n\nPress left cursor key to select geometry instancing with Texture Buffer Object\nPress right cursor key to select double quad based forest")); 
    11571215        _techniqueSwitch->addChild(group); 
    11581216        std::cout<<"done."<<std::endl; 
     
    13071365        osg::Group* group = new osg::Group; 
    13081366        group->addChild(createGeometryShaderGraph(cell.get(), stateset)); 
    1309         group->addChild(createHUDWithText("Using osg::Vertex/Geometry/FragmentProgram to create a forest\n\nPress left cursor key to select OpenGL Shader based forest\nPress right cursor key to select osg::Billboard based forest")); 
     1367        group->addChild(createHUDWithText("Using osg::Vertex/Geometry/FragmentProgram to create a forest\n\nPress left cursor key to select OpenGL Shader based forest\nPress right cursor key to select geometry instancing with Texture Buffer Object")); 
    13101368 
    13111369        _techniqueSwitch->addChild(group); 
    13121370        std::cout<<"done."<<std::endl; 
    13131371    } 
     1372     
     1373    { 
     1374        std::cout<<"Creating forest using geometry instancing and texture buffer objects ..."; 
     1375 
     1376        osg::StateSet* stateset = new osg::StateSet(*dstate, osg::CopyOp::DEEP_COPY_ALL); 
     1377        { 
     1378            osg::Program* program = new osg::Program; 
     1379            stateset->setAttribute(program); 
     1380 
     1381            char vertexShaderSource[] = 
     1382                "#version 420 compatibility\n" 
     1383                "uniform samplerBuffer dataBuffer;\n" 
     1384                "layout(location = 0) in vec3 VertexPosition;\n" 
     1385                "layout(location = 8) in vec3 VertexTexCoord;\n" 
     1386                "out vec2 TexCoord;\n" 
     1387                "out vec4 Color;\n" 
     1388                "void main()\n" 
     1389                "{\n" 
     1390                "   int instanceAddress = gl_InstanceID * 3;\n" 
     1391                "   vec3 position = texelFetch(dataBuffer, instanceAddress).xyz;\n" 
     1392                "   Color         = texelFetch(dataBuffer, instanceAddress + 1);\n" 
     1393                "   vec2 size     = texelFetch(dataBuffer, instanceAddress + 2).xy;\n" 
     1394                "   mat4 mvpMatrix = gl_ModelViewProjectionMatrix *\n"  
     1395                "        mat4( size.x, 0.0, 0.0, 0.0,\n" 
     1396                "              0.0, size.x, 0.0, 0.0,\n" 
     1397                "              0.0, 0.0, size.y, 0.0,\n" 
     1398                "              position.x, position.y, position.z, 1.0);\n" 
     1399                "   gl_Position = mvpMatrix * vec4(VertexPosition,1.0) ;\n" 
     1400                "   TexCoord = VertexTexCoord.xy;\n" 
     1401                "}\n"; 
     1402 
     1403            char fragmentShaderSource[] = 
     1404                "#version 420 core\n" 
     1405                "uniform sampler2D baseTexture; \n" 
     1406                "in vec2 TexCoord;\n" 
     1407                "in vec4 Color;\n" 
     1408                "layout(location = 0, index = 0) out vec4 FragData0;\n" 
     1409                "void main(void) \n" 
     1410                "{\n" 
     1411                "    FragData0 = Color*texture(baseTexture, TexCoord);\n" 
     1412                "}\n"; 
     1413 
     1414            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertexShaderSource); 
     1415            program->addShader(vertex_shader); 
     1416 
     1417            osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource); 
     1418            program->addShader(fragment_shader); 
     1419 
     1420            osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); 
     1421            stateset->addUniform(baseTextureSampler); 
     1422             
     1423            osg::Uniform* dataBufferSampler = new osg::Uniform("dataBuffer",1); 
     1424            stateset->addUniform(dataBufferSampler); 
     1425        } 
     1426         
     1427        osg::ref_ptr<osg::Geometry> templateGeometry = createOrthogonalQuadsNoColor(osg::Vec3(0.0f,0.0f,0.0f),1.0f,1.0f); 
     1428        templateGeometry->setUseVertexBufferObjects(true); 
     1429        templateGeometry->setUseDisplayList(false); 
     1430        osg::Node* textureBufferGraph = createTextureBufferGraph(cell.get(), templateGeometry.get()); 
     1431        textureBufferGraph->setStateSet( stateset ); 
     1432        osg::Group* group = new osg::Group; 
     1433        group->addChild(textureBufferGraph); 
     1434        group->addChild(createHUDWithText("Using geometry instancing to create a forest\n\nPress left cursor key to select osg::Vertex/Geometry/FragmentProgram based forest\nPress right cursor key to select osg::Billboard based forest")); 
     1435 
     1436        _techniqueSwitch->addChild(group); 
     1437         
     1438        std::cout<<"done."<<std::endl; 
     1439    } 
     1440     
    13141441 
    13151442    _currentTechnique = 0; 
     
    13341461    osgViewer::Viewer viewer(arguments); 
    13351462 
    1336     float numTreesToCreates = 10000; 
    1337     arguments.read("--trees",numTreesToCreates); 
     1463    unsigned int numTreesToCreate = 10000; 
     1464    arguments.read("--trees",numTreesToCreate); 
     1465     
     1466    unsigned int maxNumTreesPerCell = sqrtf(static_cast<float>(numTreesToCreate)); 
     1467     
     1468    arguments.read("--trees-per-cell",maxNumTreesPerCell); 
    13381469 
    13391470    osg::ref_ptr<ForestTechniqueManager> ttm = new ForestTechniqueManager; 
    1340  
     1471     
    13411472    // add the stats handler 
    13421473    viewer.addEventHandler(new osgViewer::StatsHandler); 
     
    13461477 
    13471478    // add model to viewer. 
    1348     viewer.setSceneData( ttm->createScene((unsigned int)numTreesToCreates) ); 
     1479    viewer.setSceneData( ttm->createScene(numTreesToCreate, maxNumTreesPerCell) ); 
    13491480 
    13501481