/***************************************************************
* Function: createText()
***************************************************************/
Geode *CAVEGroupReferenceAxis::createText3D(osgText::Text3D **text)
{
    Geode *textGeode = new Geode;
    *text = new osgText::Text3D;
    textGeode->addDrawable(*text);

    (*text)->setFont(CAVEGeode::getDataDir() + "Fonts/TN.ttf");
    (*text)->setCharacterSize(gCharSize, 0.7);
    (*text)->setCharacterDepth(gCharDepth);
    (*text)->setPosition(Vec3(0, 0, 0));
    (*text)->setAlignment(osgText::Text3D::CENTER_BOTTOM);
    (*text)->setDrawMode(osgText::Text3D::TEXT);
    (*text)->setAxisAlignment(osgText::Text3D::XZ_PLANE);
    (*text)->setRenderMode(osgText::Text3D::PER_GLYPH);
    (*text)->setText("");

    Material *material = new Material;
    material->setDiffuse(Material::FRONT_AND_BACK, Vec4(0, 1, 0, 1));
    material->setAmbient(Material::FRONT_AND_BACK, Vec4(0, 1, 0, 1));
    material->setAlpha(Material::FRONT_AND_BACK, 1.0f);

    StateSet *stateset = textGeode->getOrCreateStateSet();
    stateset->setAttributeAndModes(material, StateAttribute::OVERRIDE | StateAttribute::ON);
    stateset->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON );
    stateset->setRenderingHint(StateSet::TRANSPARENT_BIN);

    return textGeode;
}
Ejemplo n.º 2
0
/***************************************************************
* Function: applyColorTexture()
***************************************************************/
void CAVEGeode::applyColorTexture(const Vec3 &diffuse, const Vec3 &specular, const float &alpha, const string &texFilename)
{
    StateSet *stateset = getOrCreateStateSet();

    /* update material parameters */
    Material *material = dynamic_cast <Material*> (stateset->getAttribute(StateAttribute::MATERIAL)); 
    if (!material) 
        material = new Material;
    material->setDiffuse(Material::FRONT_AND_BACK, Vec4(diffuse, 1.f));
    material->setSpecular(Material::FRONT_AND_BACK, Vec4(specular, 1.f));
    material->setAlpha(Material::FRONT_AND_BACK, alpha);
    stateset->setAttributeAndModes(material, StateAttribute::ON);

    /* update texture image */
    Texture2D *texture = dynamic_cast <Texture2D*> (stateset->getAttribute(StateAttribute::TEXTURE));
    if (!texture) 
        texture = new Texture2D;
    Image* texImage = osgDB::readImageFile(texFilename);
    texture->setImage(texImage); 
    texture->setWrap(Texture::WRAP_S,Texture::REPEAT);
    texture->setWrap(Texture::WRAP_T,Texture::REPEAT);
    texture->setDataVariance(Object::DYNAMIC);
    stateset->setTextureAttributeAndModes(0, texture, StateAttribute::ON);
    stateset->setMode(GL_BLEND, StateAttribute::OVERRIDE | osg::StateAttribute::ON );
    stateset->setRenderingHint(StateSet::TRANSPARENT_BIN);

    /* update of color & texture properties */
    mDiffuse = diffuse;
    mSpecular = specular;
    mAlpha = alpha;
    mTexFilename = texFilename;
}
Ejemplo n.º 3
0
// Constructor: CAVEGeodeSnapWireframe
CAVEGeodeSnapWireframe::CAVEGeodeSnapWireframe()
{
    // unit grid size 'mSnappingUnitDist' will inherit the default value 'gSnappingUnitDist' unless modified
    mSnappingUnitDist = gSnappingUnitDist;

    mInitPosition = Vec3(0, 0, 0);
    mScaleVect = Vec3(1, 1, 1);
    mDiagonalVect = Vec3(1, 1, 1);

    mBaseGeometry = new Geometry();
    mSnapwireGeometry = new Geometry();
    addDrawable(mBaseGeometry);
    addDrawable(mSnapwireGeometry);

    Material* material = new Material;
    material->setAmbient(Material::FRONT_AND_BACK, Vec4(0.0, 1.0, 0.0, 1.0));
    material->setDiffuse(Material::FRONT_AND_BACK, Vec4(1.0, 1.0, 0.0, 1.0));
    material->setSpecular(Material::FRONT_AND_BACK, osg::Vec4( 1.f, 1.f, 1.f, 1.0f));
    material->setAlpha(Material::FRONT_AND_BACK, 0.f);

    StateSet* stateset = new StateSet();
    stateset->setAttributeAndModes(material, StateAttribute::OVERRIDE | StateAttribute::ON);
    stateset->setMode(GL_BLEND, StateAttribute::OVERRIDE | osg::StateAttribute::ON );
    stateset->setMode(GL_LIGHTING, StateAttribute::OVERRIDE | StateAttribute::ON );
    stateset->setRenderingHint(StateSet::TRANSPARENT_BIN);
    setStateSet(stateset);
}
Ejemplo n.º 4
0
//Constructor
CAVEGeodeShape::CAVEGeodeShape(const Type &typ, const Vec3 &initVect, const Vec3 &sVect):
		mCenterVect(Vec3(0, 0, 0)), mNumVertices(0), mNumNormals(0), mNumTexcoords(0),
		mDOCollectorIndex(-1)
{
    mVertexArray = new Vec3Array;
    mNormalArray = new Vec3Array;
    mUDirArray = new Vec3Array;
    mVDirArray = new Vec3Array;
    mTexcoordArray = new Vec2Array;
    mVertexMaskingVector.clear();

    switch (typ)
    {
	case BOX: initGeometryBox(initVect, sVect); break;
	case CYLINDER: initGeometryCylinder(initVect, sVect); break;
	default: break;
    }

    /* texture coordinates is associated with size of the geometry */
    Image* img = osgDB::readImageFile(CAVEGeode::getDataDir() + "Textures/White.JPG");
    Texture2D* texture = new Texture2D(img);
    texture->setWrap(Texture::WRAP_S, Texture::MIRROR);
    texture->setWrap(Texture::WRAP_T, Texture::MIRROR);

    Material *material = new Material;
    material->setSpecular(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1));
    material->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1));
    material->setAlpha(Material::FRONT_AND_BACK, 1.0f);

    StateSet *stateset = getOrCreateStateSet();
    stateset->setTextureAttributeAndModes(0, texture, StateAttribute::ON);
    stateset->setAttributeAndModes(material, StateAttribute::OVERRIDE | StateAttribute::ON);
    stateset->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON );
    stateset->setRenderingHint(StateSet::TRANSPARENT_BIN);
}
Ejemplo n.º 5
0
int main()
{
    using namespace osg;
//    // read Neuron Obj file from desk
//    ref_ptr<Node> model = osgDB::readNodeFile( "Neuron.obj" );
//    model->setName("Neuron");
    ref_ptr<MatrixTransform> mt = new MatrixTransform;
//    mt->addChild( model.get() );
    myModel model1 ("Neuron.obj","Neuron");
    mt = model1.getMatrix();


    osgUtil::SmoothingVisitor sv;
    model->accept( sv );

    // BlendFunc
    ref_ptr<BlendFunc> blendFunc = new BlendFunc;
    blendFunc->setFunction( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
    StateSet* stateset = model->getOrCreateStateSet();
    stateset->setAttributeAndModes( blendFunc );

    // Material
    ref_ptr<StateSet> nodess =  model->getOrCreateStateSet();
    ref_ptr<Material> nodeMaterial = new Material;
    nodeMaterial->setDiffuse( Material::FRONT   , Vec4(0.5f,0.5f,0.5f,1.0f));
    nodeMaterial->setAmbient( Material::FRONT   , Vec4(2.0f,0.0f,0.0f,1.0f) );
    nodeMaterial->setTransparency(Material::FRONT , 0.25f);
    //nodeMaterial->setAlpha( Material::FRONT , 0.5f );
    nodess->setAttribute( nodeMaterial.get() );

    // create camera
    ref_ptr<Camera> camera = new Camera();
    camera->setClearMask( GL_DEPTH_BUFFER_BIT );
    camera->setRenderOrder( Camera::POST_RENDER );
    camera->setReferenceFrame( Camera::ABSOLUTE_RF );
    camera->setViewMatrixAsLookAt( Vec3d(-1.0f,0.0f,0.0f) , Vec3d(0.0,0.0,0.0) , Vec3d(0.0f,1.0f,0.0f) );

    // create root node
    ref_ptr<Group> root = new Group;
    // add child to it
    root->addChild( mt.get() );
    root->addChild( camera.get() );

    ref_ptr<PickHandler> picker = new PickHandler;
    root->addChild( picker->getOrCreateSelectionBox() );

    ref_ptr<ModelController> ctrler = new ModelController( mt.get() );

    // create viewer to display data
    osgViewer::Viewer viewer;
    viewer.addEventHandler( picker.get() );
    viewer.addEventHandler( ctrler.get() );
    viewer.setSceneData( root.get() );
    return viewer.run();
}
Ejemplo n.º 6
0
/***************************************************************
* Function: applyAlpha()
***************************************************************/
void CAVEGeode::applyAlpha(const float &alpha)
{
    StateSet *stateset = getOrCreateStateSet();

    /* update material parameters */
    Material *material = dynamic_cast <Material*> (stateset->getAttribute(StateAttribute::MATERIAL)); 
    if (!material) material = new Material;
    material->setAlpha(Material::FRONT_AND_BACK, alpha);
    stateset->setAttributeAndModes(material, StateAttribute::ON);

    /* update alpha value */
    mAlpha = alpha;
}
Ejemplo n.º 7
0
Archivo: Wire.cpp Proyecto: nixz/covise
void Wire::coverStuff(void)
{
    //start of design functions
    geom = new osg::Geometry();
    geode = new Geode();
    geode->setNodeMask(geode->getNodeMask() & ~(Isect::Walk | Isect::Intersection | Isect::Collision | Isect::Touch | Isect::Pick));
    geom->setUseDisplayList(true);
    geom->setUseVertexBufferObjects(false);
    geode->addDrawable(geom.get());
    vert = new Vec3Array;
    primitives = new DrawArrayLengths(PrimitiveSet::POLYGON);
    indices = new UShortArray();
    normals = new Vec3Array;
    //cindices = new UShortArray()   //colors = new Vec4Array();
    StateSet *geoState = geom->getOrCreateStateSet();

    this->createGeom();

    geom->setVertexArray(vert.get());
    geom->setVertexIndices(indices.get());
    geom->setNormalIndices(indices.get());
    geom->setNormalArray(normals.get());
    //geom->setColorIndices(cindices.get());
    //geom->setColorArray(colors.get());
    geom->addPrimitiveSet(primitives.get());

    geoState = geode->getOrCreateStateSet();
    if (globalmtl.get() == NULL)
    {
        globalmtl = new Material;
        globalmtl->ref();
        globalmtl->setColorMode(Material::OFF);
        globalmtl->setAmbient(Material::FRONT_AND_BACK, Vec4(0.2f, 0.2f, 0.2f, 1.0));
        globalmtl->setDiffuse(Material::FRONT_AND_BACK, Vec4(0.9f, 0.9f, 0.9f, 1.0));
        globalmtl->setSpecular(Material::FRONT_AND_BACK, Vec4(0.9f, 0.9f, 0.9f, 1.0));
        globalmtl->setEmission(Material::FRONT_AND_BACK, Vec4(0.0f, 0.0f, 0.0f, 1.0));
        globalmtl->setShininess(Material::FRONT_AND_BACK, 10.0f);
    }

    geoState->setRenderingHint(StateSet::OPAQUE_BIN);
    geoState->setMode(GL_BLEND, StateAttribute::OFF);
    geoState->setAttributeAndModes(globalmtl.get(), StateAttribute::ON);
    //char name[1000];
    //sprintf(name,"Wire %d", this->getID());
    coVRShader *SolidClipping = coVRShaderList::instance()->get("SolidClipping");
    if (SolidClipping)
        SolidClipping->apply(geode, geom);
    geode->setName(this->getName());
    this->coverGroup->addChild(geode.get());
    //end of design functions
}
Ejemplo n.º 8
0
// Create and return a StateSet appropriate for performing an occlusion
//   query test (disable lighting, texture mapping, etc). Probably some
//   room for improvement here. Could disable shaders, for example.
StateSet* initOQState()
{
    StateSet* state = new StateSet;
    // TBD Possible bug, need to allow user to set render bin number.
    state->setRenderBinDetails( 9, "RenderBin" );

    state->setMode( GL_LIGHTING, StateAttribute::OFF | StateAttribute::PROTECTED);
    state->setTextureMode( 0, GL_TEXTURE_2D, StateAttribute::OFF | StateAttribute::PROTECTED);
    state->setMode( GL_CULL_FACE, StateAttribute::ON | StateAttribute::PROTECTED);

    ColorMask* cm = new ColorMask( false, false, false, false );
    state->setAttributeAndModes( cm, StateAttribute::ON | StateAttribute::PROTECTED);

    Depth* d = new Depth( Depth::LEQUAL, 0.f, 1.f, false );
    state->setAttributeAndModes( d, StateAttribute::ON | StateAttribute::PROTECTED);

    PolygonMode* pm = new PolygonMode( PolygonMode::FRONT_AND_BACK, PolygonMode::FILL );
    state->setAttributeAndModes( pm, StateAttribute::ON | StateAttribute::PROTECTED);

    PolygonOffset* po = new PolygonOffset( -1., -1. );
    state->setAttributeAndModes( po, StateAttribute::ON | StateAttribute::PROTECTED);

    return state;
}
//Constructor: CAVEGeodeSnapSolidshape
CAVEGeodeSnapSolidshape::CAVEGeodeSnapSolidshape(): mSnappingUnitDist(0.0f)
{
    mInitPosition = Vec3(0, 0, 0);
    mScaleVect = Vec3(1, 1, 1);

    Material* material = new Material;
    material->setDiffuse(Material::FRONT_AND_BACK, Vec4(1.0f, 0.5f, 0.0f, 1.0f));
    material->setSpecular(Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 0.5f, 0.0f, 1.0f));
    material->setAlpha(Material::FRONT, 0.8f);

    StateSet* stateset = new StateSet();
    stateset->setAttributeAndModes(material, StateAttribute::OVERRIDE | StateAttribute::ON);
    stateset->setMode(GL_BLEND, StateAttribute::OVERRIDE | osg::StateAttribute::ON );
    stateset->setMode(GL_LIGHTING, StateAttribute::OVERRIDE | StateAttribute::ON );
    stateset->setRenderingHint(StateSet::TRANSPARENT_BIN);
    setStateSet(stateset);
}
/***************************************************************
* Function: setVSParamountPreviewHighlight()
***************************************************************/
void VirtualScenicHandler::setVSParamountPreviewHighlight(bool flag, Geode *paintGeode)
{
    StateSet *stateset = paintGeode->getOrCreateStateSet();
    Material *material = dynamic_cast<Material*> (stateset->getAttribute(StateAttribute::MATERIAL)); 
    if (!material) 
        material = new Material;
    if (flag)
    {
        material->setAlpha(Material::FRONT_AND_BACK, 0.5f);
        material->setDiffuse(Material::FRONT_AND_BACK, Vec4(0.3, 1.0, 0.3, 1.0));
        material->setSpecular(Material::FRONT_AND_BACK, Vec4(0.3, 1.0, 0.3, 1.0));
    }
    else
    {
        material->setAlpha(Material::FRONT_AND_BACK, 1.0f);
        material->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1));
        material->setSpecular(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1));
    }
    stateset->setAttributeAndModes(material, StateAttribute::OVERRIDE | StateAttribute::ON);
}
Ejemplo n.º 11
0
/***************************************************************
* Function: ANIMCreateSinglePageGeodeAnimation()
***************************************************************/
void ANIMCreateSinglePageGeodeAnimation(const string& texfilename, Geode **flipUpGeode, Geode **flipDownGeode,
					AnimationPathCallback **flipUpCallback, AnimationPathCallback **flipDownCallback)
{
    /* coordinates of page object */
    Vec3 topleft = Vec3(-0.19, 0, 0);
    Vec3 bottomleft = Vec3(-0.19, 0, -0.28);
    Vec3 bottomright = Vec3( 0.19, 0, -0.28);
    Vec3 topright = Vec3( 0.19, 0, 0);
    Vec3 start = Vec3(0, -0.004, 0);
    Vec3 end = Vec3(0, 0.008, 0);
    float pageH = 0.28, pageW = 0.38;

    /* create page pain geometry */
    *flipUpGeode = new Geode;
    *flipDownGeode = new Geode;

    Geometry *pageGeometry = new Geometry();
    Vec3Array* vertices = new Vec3Array;
    Vec2Array* texcoords = new Vec2Array(4);
    Vec3Array* normals = new Vec3Array;

    vertices->push_back(topleft);	(*texcoords)[0].set(0, 1);
    vertices->push_back(bottomleft);	(*texcoords)[1].set(0, 0);
    vertices->push_back(bottomright);	(*texcoords)[2].set(1, 0);
    vertices->push_back(topright);	(*texcoords)[3].set(1, 1);
    
    for (int i = 0; i < 4; i++) 
    {
        normals->push_back(Vec3(0, -1, 0));
    }

    DrawElementsUInt* rectangle = new DrawElementsUInt(PrimitiveSet::POLYGON, 0);
    rectangle->push_back(0);	rectangle->push_back(1);
    rectangle->push_back(2);	rectangle->push_back(3);

    pageGeometry->addPrimitiveSet(rectangle);
    pageGeometry->setVertexArray(vertices);
    pageGeometry->setTexCoordArray(0, texcoords);
    pageGeometry->setNormalArray(normals);
    pageGeometry->setNormalBinding(Geometry::BIND_PER_VERTEX);

    (*flipUpGeode)->addDrawable(pageGeometry);
    (*flipDownGeode)->addDrawable(pageGeometry);

    /* apply image textures to page geodes */
    Image* imgFloorplan = osgDB::readImageFile(texfilename);
    int imgW = imgFloorplan->s();
    int imgH = imgFloorplan->t();
    Texture2D* texFloorplan = new Texture2D(imgFloorplan); 
    texFloorplan->setWrap(Texture::WRAP_S, Texture::CLAMP);
    texFloorplan->setWrap(Texture::WRAP_T, Texture::CLAMP);

    float imgRatio = (float) imgW / imgH;
    float pageRatio = pageW / pageH;
    if (imgRatio <= pageRatio)
    {
        (*texcoords)[0].set((1.0 - pageRatio / imgRatio) * 0.5, 1);
        (*texcoords)[1].set((1.0 - pageRatio / imgRatio) * 0.5, 0);
        (*texcoords)[2].set((1.0 + pageRatio / imgRatio) * 0.5, 0);
        (*texcoords)[3].set((1.0 + pageRatio / imgRatio) * 0.5, 1);
    }
    else
    {
        (*texcoords)[0].set(0, (1.0 + imgRatio / pageRatio) * 0.5);
        (*texcoords)[1].set(0, (1.0 - imgRatio / pageRatio) * 0.5);
        (*texcoords)[2].set(1, (1.0 - imgRatio / pageRatio) * 0.5);
        (*texcoords)[3].set(1, (1.0 + imgRatio / pageRatio) * 0.5);
    }

    Material *transmaterial = new Material;
    transmaterial->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1));
    transmaterial->setAlpha(Material::FRONT_AND_BACK, 0.8f);

    Material *solidmaterial = new Material;
    solidmaterial->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1));
    solidmaterial->setAlpha(Material::FRONT_AND_BACK, 1.0f);

    StateSet *flipUpStateSet = (*flipUpGeode)->getOrCreateStateSet();
    flipUpStateSet->setTextureAttributeAndModes(0, texFloorplan, StateAttribute::ON);
    flipUpStateSet->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON );
    flipUpStateSet->setRenderingHint(StateSet::TRANSPARENT_BIN);
    flipUpStateSet->setAttributeAndModes(transmaterial, StateAttribute::OVERRIDE | StateAttribute::ON);

    StateSet *flipDownStateSet = (*flipDownGeode)->getOrCreateStateSet();
    flipDownStateSet->setTextureAttributeAndModes(0, texFloorplan, StateAttribute::ON);
    flipDownStateSet->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON );
    flipDownStateSet->setRenderingHint(StateSet::TRANSPARENT_BIN);
    flipDownStateSet->setAttributeAndModes(solidmaterial, StateAttribute::OVERRIDE | StateAttribute::ON);

    /* create page flipping animation call backs */
    AnimationPath* animationPathFlipUp = new AnimationPath;
    AnimationPath* animationPathFlipDown = new AnimationPath;
    animationPathFlipUp->setLoopMode(AnimationPath::NO_LOOPING);
    animationPathFlipDown->setLoopMode(AnimationPath::NO_LOOPING);

    Vec3 flipUpOffset, flipDownOffset;
    Quat flipUpQuat, flipDownQuat;
    Vec3 offsetstep = (end - start) / ANIM_SKETCH_BOOK_PAGE_FLIP_SAMPS;
    float anglestep = M_PI * 2 / ANIM_SKETCH_BOOK_PAGE_FLIP_SAMPS;
    float timestep = 1.0f / ANIM_SKETCH_BOOK_PAGE_FLIP_SAMPS;
    for (int i = 0; i < ANIM_SKETCH_BOOK_PAGE_FLIP_SAMPS + 1; i++)
    {
        float val = i * timestep;
        flipUpOffset = start + offsetstep * i;
        flipDownOffset = end - offsetstep * i;
        flipUpQuat = Quat(i * anglestep, Vec3(-1, 0, 0));
        flipDownQuat = Quat(i * anglestep, Vec3(1, 0, 0));
        animationPathFlipUp->insert(val, AnimationPath::ControlPoint(flipUpOffset, flipUpQuat, Vec3(1, 1, 1)));
        animationPathFlipDown->insert(val, AnimationPath::ControlPoint(flipDownOffset, flipDownQuat, Vec3(1, 1, 1)));
    }

    *flipUpCallback = new AnimationPathCallback(animationPathFlipUp, 0.0, 1.0f / ANIM_SKETCH_BOOK_PAGE_FLIP_TIME);
    *flipDownCallback = new AnimationPathCallback(animationPathFlipDown, 0.0, 1.0f / ANIM_SKETCH_BOOK_PAGE_FLIP_TIME);
}
Ejemplo n.º 12
0
void JTOpenPlugin::setMaterial(osg::Node *osgNode, JtkHierarchy *CurrNode)
{
    JtkMaterial *partMaterial = NULL;
    ((JtkPart *)CurrNode)->getMaterial(partMaterial);
    if (partMaterial)
    {
        float *ambient = NULL,
              *diffuse = NULL,
              *specular = NULL,
              *emission = NULL,
              shininess = -999.0;
        StateSet *stateset = NULL;
        osg::Geode *osgGeode = dynamic_cast<osg::Geode *>(osgNode);
        if (osgGeode)
        {
            osg::Drawable *drawable = osgGeode->getDrawable(0);
            if (drawable)
            {
                stateset = drawable->getOrCreateStateSet();
            }
            else
                stateset = osgGeode->getOrCreateStateSet();
        }
        else
            stateset = osgNode->getOrCreateStateSet();

        osg::Material *mtl = new osg::Material();

        partMaterial->getAmbientColor(ambient);
        if (ambient)
        {
            mtl->setAmbient(Material::FRONT_AND_BACK, Vec4(ambient[0], ambient[1], ambient[2], ambient[3]));
            JtkEntityFactory::deleteMemory(ambient);
        }

        partMaterial->getDiffuseColor(diffuse);
        if (diffuse)
        {
            mtl->setDiffuse(Material::FRONT_AND_BACK, Vec4(diffuse[0], diffuse[1], diffuse[2], diffuse[3]));
            JtkEntityFactory::deleteMemory(diffuse);
        }

        partMaterial->getSpecularColor(specular);
        if (specular)
        {
            mtl->setSpecular(Material::FRONT_AND_BACK, Vec4(specular[0], specular[1], specular[2], specular[3]));
            JtkEntityFactory::deleteMemory(specular);
        }

        partMaterial->getEmissionColor(emission);
        if (emission)
        {
            mtl->setEmission(Material::FRONT_AND_BACK, Vec4(emission[0], emission[1], emission[2], emission[3]));
            JtkEntityFactory::deleteMemory(emission);
        }

        partMaterial->getShininess(shininess);
        if (shininess != -999.0)
        {
            mtl->setShininess(Material::FRONT_AND_BACK, shininess);
        }
        stateset->setAttributeAndModes(mtl, StateAttribute::ON);
    }
}
Ejemplo n.º 13
0
Geode *ClipPlanePlugin::loadPlane()
{

    // *5---*6---*7
    // |    |    |
    // *3--------*4
    // |    |    |
    // *0---*1---*2

    float w = cover->getSceneSize() * 0.1; // width of plane

    Vec3Array *lineCoords = new Vec3Array(12);
    (*lineCoords)[0].set(-w, -0.01, -w);
    (*lineCoords)[1].set(w, -0.01, -w);
    (*lineCoords)[2].set(-w, -0.01, 0.0f);
    (*lineCoords)[3].set(w, -0.01, 0.0f);
    (*lineCoords)[4].set(-w, -0.01, w);
    (*lineCoords)[5].set(w, -0.01, w);
    (*lineCoords)[6].set(-w, -0.01, -w);
    (*lineCoords)[7].set(-w, -0.01, w);
    (*lineCoords)[8].set(0.0f, -0.01, -w);
    (*lineCoords)[9].set(0.0f, -0.01, w);
    (*lineCoords)[10].set(w, -0.01, -w);
    (*lineCoords)[11].set(w, -0.01, w);

    DrawArrayLengths *primitives = new DrawArrayLengths(PrimitiveSet::LINE_STRIP);
    for (int i = 0; i < 6; i++)
    {
        primitives->push_back(2);
    }

    Vec3Array *lineColors = new Vec3Array(12);
    for (int i = 0; i < 12; i++)
    {
        (*lineColors)[i].set(Vec3(1.0f, 1.0f, 1.0f));
    }

    Geometry *geoset = new Geometry();
    geoset->setVertexArray(lineCoords);
    geoset->addPrimitiveSet(primitives);
    geoset->setColorArray(lineColors);

    Material *mtl = new Material;
    mtl->setColorMode(Material::AMBIENT_AND_DIFFUSE);
    mtl->setAmbient(Material::FRONT_AND_BACK, Vec4(0.2f, 0.2f, 0.2f, 1.0f));
    mtl->setDiffuse(Material::FRONT_AND_BACK, Vec4(0.9f, 0.9f, 0.9f, 1.0f));
    mtl->setSpecular(Material::FRONT_AND_BACK, Vec4(0.9f, 0.9f, 0.9f, 1.0f));
    mtl->setEmission(Material::FRONT_AND_BACK, Vec4(0.0f, 0.0f, 0.0f, 1.0f));
    mtl->setShininess(Material::FRONT_AND_BACK, 16.0f);

    Geode *geode = new Geode;
    geode->setName("ClipPlane");
    geode->addDrawable(geoset);

    StateSet *geostate = geode->getOrCreateStateSet();
    geostate->setAttributeAndModes(mtl, StateAttribute::ON);
    geostate->setMode(GL_LIGHTING, StateAttribute::OFF);
    LineWidth *lineWidth = new LineWidth(3.0);
    geostate->setAttributeAndModes(lineWidth, StateAttribute::ON);
    geode->setStateSet(geostate);

    return geode;
}
Ejemplo n.º 14
0
void CircleShape::generate()
{
	/********************************************************************
	*																    *
	*					Vertices									    *
	*																    *
	********************************************************************
	*/
	int numVertices = 0;

	vertices = new Vec3Array;
	for (int i=0; i <= 360; i = i + degree )
	{	
		double angle = (i*2*PI)/360;		
		double x,z;
		x = cos(angle)*radius;
		z = sin(angle)*radius;


		vertices->push_back(center);
		vertices->push_back(Vec3d(x,0,z)+center);	
		numVertices+=2;		
	}

	vertices->push_back(center);
	setVertexArray(vertices);

	/********************************************************************
	*								    *
	*				colors				    *
	*								    *
	********************************************************************
	*/

	colors = new Vec4Array(numVertices);
	for (int g = 0; g < numVertices; g+=2)
	{
		(*colors)[g].set(color.x(), color.y(), color.z(), color.w());
		(*colors)[g+1].set(gradient.x(), gradient.y(), gradient.z(), gradient.w());	
	}

	setColorArray(colors);	
	setColorBinding(Geometry::BIND_PER_VERTEX);



	addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLE_STRIP, 0,2+(360*2/degree)));


	/********************************************************************
	*								    *
	*			stateset and material			    *
	*	  							    *
	********************************************************************
	*/

	// stateset and material
	StateSet* state = getOrCreateStateSet();
	state->setMode(GL_BLEND,StateAttribute::ON|StateAttribute::OVERRIDE);
	Material* mat = new Material(); 
	mat->setAlpha(Material::FRONT_AND_BACK, 0.1);
	mat->setColorMode(Material::AMBIENT_AND_DIFFUSE);
	state->setAttributeAndModes(mat,StateAttribute::ON | StateAttribute::OVERRIDE);


	/********************************************************************
	*								    *
	*			blending				    *
	*								    *
	********************************************************************
	*/


	// blending

	BlendFunc* bf = new BlendFunc(BlendFunc::SRC_ALPHA, BlendFunc::ONE_MINUS_SRC_ALPHA );
	state->setAttributeAndModes(bf);

	state->setRenderingHint(StateSet::TRANSPARENT_BIN);
	state->setMode(GL_LIGHTING, StateAttribute::ON);

	setStateSet(state);
}
/***************************************************************
* Function: createFloorplanGeometry()
***************************************************************/
void VirtualScenicHandler::createFloorplanGeometry(const int numPages, 
    CAVEAnimationModeler::ANIMPageEntry **pageEntryArray)
{
    if (numPages <= 0)
    {
        return;
    }

    for (int i = 0; i < numPages; i++)
    {
        // create floorplan geometry
        float length = pageEntryArray[i]->mLength;
        float width = pageEntryArray[i]->mWidth;
        float altitude = pageEntryArray[i]->mAlti;

        Geode *floorplanGeode = new Geode;
        Geometry *floorplanGeometry = new Geometry;

        Vec3Array* vertices = new Vec3Array;
        Vec3Array* normals = new Vec3Array;
        Vec2Array* texcoords = new Vec2Array(4);

        vertices->push_back(Vec3(-length / 2,  width / 2, altitude));	(*texcoords)[0].set(0, 1);
        vertices->push_back(Vec3(-length / 2, -width / 2, altitude));	(*texcoords)[1].set(0, 0);
        vertices->push_back(Vec3( length / 2, -width / 2, altitude));	(*texcoords)[2].set(1, 0);
        vertices->push_back(Vec3( length / 2,  width / 2, altitude));	(*texcoords)[3].set(1, 1);

        for (int k = 0; k < 4; k++) 
        { 
            normals->push_back(Vec3(0, 0, 1));
        }

        DrawElementsUInt* rectangle = new DrawElementsUInt(PrimitiveSet::POLYGON, 0);
        rectangle->push_back(0);	rectangle->push_back(1);
        rectangle->push_back(2);	rectangle->push_back(3);

        floorplanGeometry->addPrimitiveSet(rectangle);
        floorplanGeometry->setVertexArray(vertices);
        floorplanGeometry->setNormalArray(normals);
        floorplanGeometry->setTexCoordArray(0, texcoords);
        floorplanGeometry->setNormalBinding(Geometry::BIND_PER_VERTEX);

        floorplanGeode->addDrawable(floorplanGeometry);
        mFloorplanSwitch->addChild(floorplanGeode);

        /* load floorplan images */
        Material *transmaterial = new Material;
        transmaterial->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1));
        transmaterial->setAlpha(Material::FRONT_AND_BACK, 1.0f);

        Image* imgFloorplan = osgDB::readImageFile(pageEntryArray[i]->mTexFilename);

        Texture2D* texFloorplan = new Texture2D(imgFloorplan); 

        StateSet *floorplanStateSet = floorplanGeode->getOrCreateStateSet();
        floorplanStateSet->setTextureAttributeAndModes(0, texFloorplan, StateAttribute::ON);
        floorplanStateSet->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON );
        floorplanStateSet->setRenderingHint(StateSet::TRANSPARENT_BIN);
        floorplanStateSet->setAttributeAndModes(transmaterial, StateAttribute::OVERRIDE | StateAttribute::ON);
    }
}
Ejemplo n.º 16
0
Archivo: sky.cpp Proyecto: BodyViz/osg
Node *makeSky( void )
{
    int i, j;
    float lev[] = { -5, -1.0, 1.0, 15.0, 30.0, 60.0, 90.0  };
    float cc[][4] =
    {
        { 0.0, 0.0, 0.15 },
        { 0.0, 0.0, 0.15 },
        { 0.4, 0.4, 0.7 },
        { 0.2, 0.2, 0.6 },
        { 0.1, 0.1, 0.6 },
        { 0.1, 0.1, 0.6 },
        { 0.1, 0.1, 0.6 },
    };
    float x, y, z;
    float alpha, theta;
    float radius = 20.0f;
    int nlev = sizeof( lev )/sizeof(float);

    Geometry *geom = new Geometry;

    Vec3Array& coords = *(new Vec3Array(19*nlev));
    Vec4Array& colors = *(new Vec4Array(19*nlev));
    Vec2Array& tcoords = *(new Vec2Array(19*nlev));
    
    
    int ci = 0;

    for( i = 0; i < nlev; i++ )
    {
        for( j = 0; j <= 18; j++ )
        {
            alpha = osg::DegreesToRadians(lev[i]);
            theta = osg::DegreesToRadians((float)(j*20));

            x = radius * cosf( alpha ) * cosf( theta );
            y = radius * cosf( alpha ) * -sinf( theta );
            z = radius * sinf( alpha );

            coords[ci][0] = x;
            coords[ci][1] = y;
            coords[ci][2] = z;

            colors[ci][0] = cc[i][0];
            colors[ci][1] = cc[i][1];
            colors[ci][2] = cc[i][2];
            colors[ci][3] = 1.0;

            tcoords[ci][0] = (float)j/18.0;
            tcoords[ci][1] = (float)i/(float)(nlev-1);

            ci++;
        }


    }

    for( i = 0; i < nlev-1; i++ )
    {
        DrawElementsUShort* drawElements = new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP);
        drawElements->reserve(38);

        for( j = 0; j <= 18; j++ )
        {
            drawElements->push_back((i+1)*19+j);
            drawElements->push_back((i+0)*19+j);
        }

        geom->addPrimitiveSet(drawElements);
    }
    
    geom->setVertexArray( &coords );
    geom->setTexCoordArray( 0, &tcoords );

    geom->setColorArray( &colors );
    geom->setColorBinding( Geometry::BIND_PER_VERTEX );


    Texture2D *tex = new Texture2D;
    tex->setImage(osgDB::readImageFile("Images/white.rgb"));

    StateSet *dstate = new StateSet;

    dstate->setTextureAttributeAndModes(0, tex, StateAttribute::OFF );
    dstate->setTextureAttribute(0, new TexEnv );
    dstate->setMode( GL_LIGHTING, StateAttribute::OFF );
    dstate->setMode( GL_CULL_FACE, StateAttribute::ON );
    

    // clear the depth to the far plane.
    osg::Depth* depth = new osg::Depth;
    depth->setFunction(osg::Depth::ALWAYS);
    depth->setRange(1.0,1.0);   
    dstate->setAttributeAndModes(depth,StateAttribute::ON );

    dstate->setRenderBinDetails(-2,"RenderBin");

    geom->setStateSet( dstate );

    Geode *geode = new Geode;
    geode->addDrawable( geom );

    geode->setName( "Sky" );

    return geode;
}
Ejemplo n.º 17
0
//----------------------------------------------------------------------
void MathematicPlugin::drawBoundingBox(BoundingBox *box)
{
    if (cover->debugLevel(3))
        fprintf(stderr, "MathematicPlugin::drawBoundingBox\n");

    boxGeode_ = new Geode();

    Vec3 bpoints[8];
    bpoints[0].set(box->xMin(), box->yMin(), box->zMin());
    bpoints[1].set(box->xMax(), box->yMin(), box->zMin());
    bpoints[2].set(box->xMax(), box->yMax(), box->zMin());
    bpoints[3].set(box->xMin(), box->yMax(), box->zMin());
    bpoints[4].set(box->xMin(), box->yMin(), box->zMax());
    bpoints[5].set(box->xMax(), box->yMin(), box->zMax());
    bpoints[6].set(box->xMax(), box->yMax(), box->zMax());
    bpoints[7].set(box->xMin(), box->yMax(), box->zMax());

    Geometry *lineGeometry[12];
    Vec3Array *vArray[12];
    DrawArrays *drawable[12];

    for (int i = 0; i < 12; i++)
    {
        lineGeometry[i] = new Geometry();
        vArray[i] = new Vec3Array();
        //fprintf(stderr,"MathematicPlugin::drawBoundingBox bpoints[0] %f %f %f\n", bpoints[0].x(), bpoints[0].y(), bpoints[0].z());
        //fprintf(stderr,"MathematicPlugin::drawBoundingBox bpoints[1] %f %f %f\n", bpoints[1].x(), bpoints[1].y(), bpoints[1].z());
        lineGeometry[i]->setVertexArray(vArray[i]);
        drawable[i] = new DrawArrays(PrimitiveSet::LINES, 0, 2);
        lineGeometry[i]->addPrimitiveSet(drawable[i]);
        LineWidth *linewidth = new LineWidth();
        linewidth->setWidth(1.0);
        boxGeode_->addDrawable(lineGeometry[i]);
    }

    // lines
    vArray[0]->push_back(bpoints[0]);
    vArray[0]->push_back(bpoints[1]);
    vArray[1]->push_back(bpoints[1]);
    vArray[1]->push_back(bpoints[2]);
    vArray[2]->push_back(bpoints[2]);
    vArray[2]->push_back(bpoints[3]);
    vArray[3]->push_back(bpoints[3]);
    vArray[3]->push_back(bpoints[0]);
    vArray[4]->push_back(bpoints[4]);
    vArray[4]->push_back(bpoints[5]);
    vArray[5]->push_back(bpoints[5]);
    vArray[5]->push_back(bpoints[6]);
    vArray[6]->push_back(bpoints[6]);
    vArray[6]->push_back(bpoints[7]);
    vArray[7]->push_back(bpoints[7]);
    vArray[7]->push_back(bpoints[4]);
    vArray[8]->push_back(bpoints[0]);
    vArray[8]->push_back(bpoints[4]);
    vArray[9]->push_back(bpoints[3]);
    vArray[9]->push_back(bpoints[7]);
    vArray[10]->push_back(bpoints[2]);
    vArray[10]->push_back(bpoints[6]);
    vArray[11]->push_back(bpoints[1]);
    vArray[11]->push_back(bpoints[5]);

    Material *material = new Material();
    material->setDiffuse(Material::FRONT_AND_BACK, Vec4(1.0, 1.0, 1.0, 1.0));
    material->setAmbient(Material::FRONT_AND_BACK, Vec4(1.0, 1.0, 1.0, 1.0));
    material->setSpecular(Material::FRONT_AND_BACK, Vec4(1.0, 1.0, 1.0, 1.0));
    StateSet *stateSet = VRSceneGraph::instance()->loadDefaultGeostate();
    stateSet->setMode(GL_BLEND, StateAttribute::ON);
    stateSet->setAttributeAndModes(material);
    stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    boxGeode_->setStateSet(stateSet);

    boxGeode_->setNodeMask(boxGeode_->getNodeMask() & (~Isect::Intersection) & (~Isect::Pick));

    cover->getObjectsRoot()->addChild(boxGeode_);
}
Ejemplo n.º 18
0
/***************************************************************
* Function: ANIMLoadGeometryCreator()
*
* xformScaleFwd: Root transform node for inflating geometries
* xformScaleBwd: Root transform node for shrinking geometries
* sphereExteriorSwitch: Switch control for single exterior sphere
*
***************************************************************/
void ANIMLoadGeometryCreator(PositionAttitudeTransform** xformScaleFwd, PositionAttitudeTransform** xformScaleBwd,
			     Switch **sphereExteriorSwitch, Geode **sphereExteriorGeode,
			     int &numTypes, ANIMShapeSwitchEntry ***shapeSwitchEntryArray)
{
    *xformScaleFwd = new PositionAttitudeTransform;
    *xformScaleBwd = new PositionAttitudeTransform;

    MatrixTransform *geomCreatorTrans = new MatrixTransform;

    MatrixTransform *sphereExteriorTrans = new MatrixTransform;
    *sphereExteriorSwitch = new Switch;
    Switch *createBoxSwitch = new Switch;
    Switch *createCylinderSwitch = new Switch;

    (*xformScaleFwd)->addChild(geomCreatorTrans);
    (*xformScaleBwd)->addChild(geomCreatorTrans);

    geomCreatorTrans->addChild(*sphereExteriorSwitch);
    geomCreatorTrans->addChild(createBoxSwitch);
    geomCreatorTrans->addChild(createCylinderSwitch);
    
    osg::Vec3 pos(-1, 0, 0);

    // create drawables, geodes and attach them to animation switches
    *sphereExteriorGeode = new Geode();
    Sphere *sphere = new Sphere(osg::Vec3(), ANIM_VIRTUAL_SPHERE_RADIUS);
    ShapeDrawable *sphereDrawable = new ShapeDrawable(sphere);
    (*sphereExteriorGeode)->addDrawable(sphereDrawable);


    Box *box = new Box(osg::Vec3(0.1, 0, 0), ANIM_VIRTUAL_SPHERE_RADIUS / 1.9);
    (*sphereExteriorGeode)->addDrawable(new ShapeDrawable(box));

    float r = ANIM_VIRTUAL_SPHERE_RADIUS / 3.0;
    Cylinder *cylinder = new Cylinder(osg::Vec3(-0.05, 0, -0.05), r, r * 2);
    (*sphereExteriorGeode)->addDrawable(new ShapeDrawable(cylinder));
    
    Cone *cone = new osg::Cone(osg::Vec3(0, -0.1, 0.05), r, r * 2);
    (*sphereExteriorGeode)->addDrawable(new ShapeDrawable(cone));


    Material *transmaterial = new Material;
    transmaterial->setDiffuse(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1));
    transmaterial->setAlpha(Material::FRONT_AND_BACK, 0.6f);

    Image* envMap = osgDB::readImageFile(ANIMDataDir() + "Textures/ShapeContainer.JPG");
    Texture2D* envTex = new Texture2D(envMap);    
    
    StateSet *sphereStateSet = (sphereDrawable)->getOrCreateStateSet();
    sphereStateSet->setMode(GL_BLEND, StateAttribute::OVERRIDE | StateAttribute::ON );
    sphereStateSet->setRenderingHint(StateSet::TRANSPARENT_BIN);
    sphereStateSet->setAttributeAndModes(transmaterial, StateAttribute::OVERRIDE | StateAttribute::ON);
    sphereStateSet->setTextureAttributeAndModes(0, envTex, StateAttribute::ON);
    sphereStateSet->setMode(GL_CULL_FACE, StateAttribute::ON);

    sphereExteriorTrans->addChild(*sphereExteriorGeode);
    (*sphereExteriorSwitch)->addChild(sphereExteriorTrans);
    (*sphereExteriorSwitch)->setAllChildrenOn();

    // write into shape switch entry array record
    numTypes = 2;
    *shapeSwitchEntryArray = new ANIMShapeSwitchEntry*[numTypes];
    (*shapeSwitchEntryArray)[0] = new ANIMShapeSwitchEntry;
    (*shapeSwitchEntryArray)[1] = new ANIMShapeSwitchEntry;
    (*shapeSwitchEntryArray)[0]->mSwitch = createBoxSwitch;
    (*shapeSwitchEntryArray)[1]->mSwitch = createCylinderSwitch;

    ANIMCreateSingleShapeSwitchAnimation(&((*shapeSwitchEntryArray)[0]), CAVEGeodeShape::BOX);
    ANIMCreateSingleShapeSwitchAnimation(&((*shapeSwitchEntryArray)[1]), CAVEGeodeShape::CYLINDER);

    /* set up the forward / backward scale animation paths for geometry creator */
    AnimationPath* animationPathScaleFwd = new AnimationPath;
    AnimationPath* animationPathScaleBwd = new AnimationPath;
    animationPathScaleFwd->setLoopMode(AnimationPath::NO_LOOPING);
    animationPathScaleBwd->setLoopMode(AnimationPath::NO_LOOPING);
   
    Vec3 scaleFwd, scaleBwd;
    float step = 1.f / ANIM_VIRTUAL_SPHERE_NUM_SAMPS;
    for (int i = 0; i < ANIM_VIRTUAL_SPHERE_NUM_SAMPS + 1; i++)
    {
        float val = i * step;
        scaleFwd = Vec3(val, val, val);
        scaleBwd = Vec3(1.f-val, 1.f-val, 1.f-val);
        animationPathScaleFwd->insert(val, AnimationPath::ControlPoint(pos, Quat(), scaleFwd));
        animationPathScaleBwd->insert(val, AnimationPath::ControlPoint(pos, Quat(), scaleBwd));
    }

    AnimationPathCallback *animCallbackFwd = new AnimationPathCallback(animationPathScaleFwd, 
						0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME);
    AnimationPathCallback *animCallbackBwd = new AnimationPathCallback(animationPathScaleBwd, 
						0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME); 
    (*xformScaleFwd)->setUpdateCallback(animCallbackFwd);
    (*xformScaleBwd)->setUpdateCallback(animCallbackBwd);
}
Ejemplo n.º 19
0
void TriangleShape::generate()	
{
	/****************************************************************
	 *								*
	 *			Vertices				*
	 *								*
	 ****************************************************************
	 */
	


	// check if all 3 points are present, otherwise use default method to calculate vertices
	if(!genVer)
	{		
		vertices = new Vec3Array(3);
		(*vertices)[0].set(p1);
		(*vertices)[1].set(p2);
		(*vertices)[2].set(p3);		
	}
	else if (genVer)
	{
		vertices = new Vec3Array();
		// top point
		Vec3d v1(center.x(), center.y(), center.z()+length/2);
		// bottom left point
		Vec3d v2((center.x()-cos(120)*length/2), center.y(), (center.z()-sin(-60)*length/2));
		// bottom right point
		Vec3d v3((center.x()-cos(60)*length/2), center.y(), (center.z()-sin(-60)*length/2));
				
		vertices->push_back(center);
		vertices->push_back(v1);
		vertices->push_back(center);
		vertices->push_back(v2);
		vertices->push_back(center);
		vertices->push_back(v3);	
		vertices->push_back(center);
		vertices->push_back(v1);
			
		
	}

	setVertexArray(vertices);

	/****************************************************************
	 *								*
	 *			normals					*
	 *								*
	 ****************************************************************
	 */


	Vec3Array* normals = new Vec3Array(1);
	(*normals)[0].set(1.0f, 1.0f, 0.0f);
	setNormalArray(normals);
	setNormalBinding(Geometry::BIND_OVERALL);

	/****************************************************************
	 *								*
	 *			colors					*
	 *								*
	 ****************************************************************
	 */
	
	if (!genVer)
	{
		colors = new Vec4Array(3);
		(*colors)[0].set(color1.x(), color1.y(), color1.z(), color1.w());
		(*colors)[1].set(color2.x(), color2.y(), color2.z(), color2.w());
		(*colors)[2].set(color3.x(), color3.y(), color3.z(), color3.w());
		addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLES, 0, 3));

	}
	else
	{
		colors = new Vec4Array();
		colors->push_back(color1);
		colors->push_back(color2);
		colors->push_back(color1);
		colors->push_back(color2);
		colors->push_back(color1);
		colors->push_back(color2);
		colors->push_back(color1);
		colors->push_back(color2);
		
		
		
		addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLE_STRIP, 0, 8));	
	}
	
	
	
	setColorArray(colors);
	setColorBinding(Geometry::BIND_PER_VERTEX);	
	

	
	/****************************************************************
	 *								*
	 *		stateset and material				*
	 *								*
	 ****************************************************************
	 */

	
	StateSet* state = getOrCreateStateSet();
	state->setMode(GL_BLEND,StateAttribute::ON|StateAttribute::OVERRIDE);
	Material* mat = new Material(); 
	mat->setAlpha(Material::FRONT_AND_BACK, 0.1);
	mat->setColorMode(Material::AMBIENT_AND_DIFFUSE);
	state->setAttributeAndModes(mat,StateAttribute::ON | StateAttribute::OVERRIDE);


	/****************************************************************
	 *								*
	 *			blending				*
	 *								*
	 ****************************************************************
	 */

	BlendFunc* bf = new BlendFunc(BlendFunc::SRC_ALPHA, BlendFunc::ONE_MINUS_SRC_ALPHA );
	state->setAttributeAndModes(bf);

	state->setRenderingHint(StateSet::TRANSPARENT_BIN);
	state->setMode(GL_LIGHTING, StateAttribute::ON);

	setStateSet(state);


	
}
Ejemplo n.º 20
0
void BoxShape::generate()	
{
	/****************************************************************
	 *								*
	 *			Vertices				*
	 *								*
	 ****************************************************************
	 */
	vertices = new Vec3Array();

  //assuming box points coming this way:        5-----------6
  //                                           /|          /|
  //                                          / |         / |
  //                                         /  |        /  |
  //                                        0---4-------3---7
  //                                        |  /        |  /
  //                                        | /         | / 
  //                                        |/          |/   
  //                                        1-----------2 




	// check if all 4 points are present, otherwise use default method to calculate vertices
	if(!genVer)
	{		
		vertices->push_back(p1);
		vertices->push_back(p2);
		vertices->push_back(p3);		
		vertices->push_back(p4);
		vertices->push_back(p5);
		vertices->push_back(p6);
		vertices->push_back(p7);		
		vertices->push_back(p8);
		
		//setVertexArray(vertices);
			
		
	}
	else if (genVer)
	{		                                                    // -y = coming out of screen?
		vertices->push_back(Vec3( center.x()-(width/2) , center.y()-(depth/2) , center.z()+(height/2) ));
		vertices->push_back(Vec3( center.x()-(width/2) , center.y()-(depth/2) , center.z()-(height/2) ));
		vertices->push_back(Vec3( center.x()+(width/2) , center.y()-(depth/2) , center.z()-(height/2) ));		
		vertices->push_back(Vec3( center.x()+(width/2) , center.y()-(depth/2) , center.z()+(height/2) ));
		vertices->push_back(Vec3( center.x()-(width/2) , center.y()+(depth/2) , center.z()-(height/2) ));
		vertices->push_back(Vec3( center.x()-(width/2) , center.y()+(depth/2) , center.z()+(height/2) ));
		vertices->push_back(Vec3( center.x()+(width/2) , center.y()+(depth/2) , center.z()+(height/2) ));		
		vertices->push_back(Vec3( center.x()+(width/2) , center.y()+(depth/2) , center.z()-(height/2) ));
		
		//setVertexArray(vertices);
	
	}

	  setVertexArray(vertices);
  	
  	//front face
		DrawElementsUInt* frontface = new DrawElementsUInt(PrimitiveSet::QUADS, 0);
		frontface->push_back(0);
		frontface->push_back(1);
		frontface->push_back(2);
		frontface->push_back(3);
		addPrimitiveSet(frontface);
		//back face
		DrawElementsUInt* backface = new DrawElementsUInt(PrimitiveSet::QUADS, 0);
		backface->push_back(4);
		backface->push_back(5);
		backface->push_back(6);
		backface->push_back(7);
		addPrimitiveSet(backface);
		//top face
		DrawElementsUInt* topface = new DrawElementsUInt(PrimitiveSet::QUADS, 0);
		topface->push_back(5);
		topface->push_back(0);
		topface->push_back(3);
		topface->push_back(6);
		addPrimitiveSet(topface);
		//bottom face
		DrawElementsUInt* bottomface = new DrawElementsUInt(PrimitiveSet::QUADS, 0);
		bottomface->push_back(1);
		bottomface->push_back(4);
		bottomface->push_back(7);
		bottomface->push_back(2);
		addPrimitiveSet(bottomface);
		//left face
		DrawElementsUInt* leftface = new DrawElementsUInt(PrimitiveSet::QUADS, 0);
		leftface->push_back(0);
		leftface->push_back(5);
		leftface->push_back(4);
		leftface->push_back(1);
		addPrimitiveSet(leftface);
		//right face
		DrawElementsUInt* rightface = new DrawElementsUInt(PrimitiveSet::QUADS, 0);
		rightface->push_back(3);
		rightface->push_back(2);
		rightface->push_back(7);
		rightface->push_back(6);
		addPrimitiveSet(rightface);



	/****************************************************************
	 *								*
	 *			normals	  				*
	 *								*
	 ****************************************************************
	 */


	Vec3Array* normals = new Vec3Array(1);
	(*normals)[0].set(1.0f, 1.0f, 1.0f);
	setNormalArray(normals);
	setNormalBinding(Geometry::BIND_OVERALL);

	/****************************************************************
	 *								*
	 *			colors					*
	 *								*
	 ****************************************************************
	 */
	/*if (!genVer)
	{
		colors = new Vec4Array(8);
		(*colors)[0].set(color1.x(), color1.y(), color1.z(), color1.w());
		(*colors)[1].set(color1.x(), color1.y(), color1.z(), color1.w());
		(*colors)[2].set(color2.x(), color2.y(), color2.z(), color2.w());		
		(*colors)[3].set(color2.x(), color2.y(), color2.z(), color2.w());	
		(*colors)[4].set(color1.x(), color1.y(), color1.z(), color1.w());
		(*colors)[5].set(color1.x(), color1.y(), color1.z(), color1.w());
		(*colors)[6].set(color2.x(), color2.y(), color2.z(), color2.w());		
		(*colors)[7].set(color2.x(), color2.y(), color2.z(), color2.w());	
		
	}
	else
	{*/
		colors = new Vec4Array(8);
		(*colors)[0].set(color1.x(), color1.y(), color1.z(), color1.w());
		(*colors)[1].set(color2.x(), color2.y(), color2.z(), color2.w());		
		(*colors)[2].set(color1.x(), color1.y(), color1.z(), color1.w());
		(*colors)[3].set(color2.x(), color2.y(), color2.z(), color2.w());
		(*colors)[4].set(color1.x(), color1.y(), color1.z(), color1.w());
		(*colors)[5].set(color2.x(), color2.y(), color2.z(), color2.w());
		(*colors)[6].set(color1.x(), color1.y(), color1.z(), color1.w());
		(*colors)[7].set(color2.x(), color2.y(), color2.z(), color2.w());
		//(*colors)[8].set(color1.x(), color1.y(), color1.z(), color1.w());	
		//(*colors)[9].set(color2.x(), color2.y(), color2.z(), color2.w());
	
	//}
	setColorArray(colors);
	setColorBinding(Geometry::BIND_PER_VERTEX);	

	


	
	/****************************************************************
	 *								*
	 *			stateset and material			*
	 *								*
	 ****************************************************************
	 */

	
	StateSet* state = getOrCreateStateSet();
	state->setMode(GL_BLEND,StateAttribute::ON|StateAttribute::OVERRIDE);
	Material* mat = new Material(); 
	mat->setAlpha(Material::FRONT_AND_BACK, 0.1);
	mat->setColorMode(Material::AMBIENT_AND_DIFFUSE);
	state->setAttributeAndModes(mat,StateAttribute::ON | StateAttribute::OVERRIDE);


	/****************************************************************
	 *								*
	 *			blending				*
	 *								*
	 ****************************************************************
	 */

	BlendFunc* bf = new BlendFunc(BlendFunc::SRC_ALPHA, BlendFunc::ONE_MINUS_SRC_ALPHA );
	state->setAttributeAndModes(bf);

	state->setRenderingHint(StateSet::TRANSPARENT_BIN);
	state->setMode(GL_LIGHTING, StateAttribute::ON);

	setStateSet(state);


	

}
Ejemplo n.º 21
0
int main(int argc, char** argv)
{
    osg::ArgumentParser arguments(&argc,argv);
    osgViewer::Viewer viewer(arguments);

    if (arguments.argc() <= 1) {
        cerr << "Need a scene.\n";
        return 1;
    }

    osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFiles(arguments);
    if (!loadedModel) {
        cerr << "couldn't load " << argv[1] << "\n";
        return 1;
    }
    osgUtil::Optimizer optimizer;
    optimizer.optimize(loadedModel.get());
    const BoundingSphere bound = loadedModel->getBound();
    const float displacement = 2.25 * bound.radius();
    Group* scene = new Group;
    StateSet* rootSS = scene->getOrCreateStateSet();

    Shader* vertexShader = new Shader(Shader::VERTEX);
    vertexShader->setShaderSource(vertexShaderSource);
    Shader* fragmentShader = new Shader(Shader::FRAGMENT);
    fragmentShader->setShaderSource(fragmentShaderSource);
    Program* prog = new Program;
    prog->addShader(vertexShader);
    prog->addShader(fragmentShader);
    prog->addBindUniformBlock("colors0", 0);
    rootSS->setAttributeAndModes(prog, StateAttribute::ON);
    // Place 3 instances of the loaded model with different uniform
    // blocks for each.
    //
    // The blocksize is known because of the std140 format.
    const unsigned blockSize = 20 * sizeof(GLfloat);
    ref_ptr<FloatArray> colorArray
        = new FloatArray(&colors1[0],
                         &colors1[sizeof(colors1) / sizeof(GLfloat)]);
    ref_ptr<UniformBufferObject> ubo = new UniformBufferObject;
    colorArray->setBufferObject(ubo.get());
    Group* group1 = new Group;
    StateSet* ss1 = group1->getOrCreateStateSet();
    group1->addChild(loadedModel.get());
    scene->addChild(group1);
    ref_ptr<UniformBufferBinding> ubb1
        = new UniformBufferBinding(0, ubo.get(), 0, blockSize);
    ss1->setAttributeAndModes(ubb1.get(), StateAttribute::ON);

    ref_ptr<FloatArray> colorArray2
        = new FloatArray(&colors2[0],
                         &colors2[sizeof(colors2) / sizeof(GLfloat)]);
    ref_ptr<UniformBufferObject> ubo2 = new UniformBufferObject;
    colorArray2->setBufferObject(ubo2.get());
    MatrixTransform* group2 = new MatrixTransform;
    Matrix mat2 = Matrix::translate(-displacement, 0.0, 0.0);
    group2->setMatrix(mat2);
    StateSet* ss2 = group2->getOrCreateStateSet();
    group2->addChild(loadedModel.get());
    scene->addChild(group2);
    ref_ptr<UniformBufferBinding> ubb2
        = new UniformBufferBinding(0, ubo2.get(), 0, blockSize);
    ss2->setAttributeAndModes(ubb2.get(), StateAttribute::ON);

    ref_ptr<FloatArray> colorArray3
        = new FloatArray(&colors2[0],
                         &colors2[sizeof(colors2) / sizeof(GLfloat)]);
    ref_ptr<UniformBufferObject> ubo3 = new UniformBufferObject;
    colorArray3->setBufferObject(ubo3.get());
    MatrixTransform* group3 = new MatrixTransform;
    Matrix mat3 = Matrix::translate(displacement, 0.0, 0.0);
    group3->setMatrix(mat3);
    StateSet* ss3 = group3->getOrCreateStateSet();
    group3->addChild(loadedModel.get());
    scene->addChild(group3);
    ref_ptr<UniformBufferBinding> ubb3
        = new UniformBufferBinding(0, ubo3.get(), 0, blockSize);
    ubb3->setUpdateCallback(new UniformBufferCallback);
    ubb3->setDataVariance(Object::DYNAMIC);
    ss3->setAttributeAndModes(ubb3.get(), StateAttribute::ON);

    viewer.setSceneData(scene);
    viewer.realize();
    return viewer.run();
}
Ejemplo n.º 22
0
Archivo: trees.cpp Proyecto: Sylla/osg
Node *makeTrees( void )
{
    Group *group = new Group;
    int i;

    getDatabaseCenterRadius( dbcenter, &dbradius );
    struct _tree  *t;

    Texture2D *tex = new Texture2D;
    tex->setImage(osgDB::readImageFile("Images/tree0.rgba"));

    StateSet *dstate = new StateSet;
    
    dstate->setTextureAttributeAndModes(0, tex, StateAttribute::ON );
    dstate->setTextureAttribute(0, new TexEnv );

    dstate->setAttributeAndModes( new BlendFunc, StateAttribute::ON );

    AlphaFunc* alphaFunc = new AlphaFunc;
    alphaFunc->setFunction(AlphaFunc::GEQUAL,0.05f);
    dstate->setAttributeAndModes( alphaFunc, StateAttribute::ON );

    dstate->setMode( GL_LIGHTING, StateAttribute::OFF );
    
    dstate->setRenderingHint( StateSet::TRANSPARENT_BIN );

    int tt[] = { 15, 30, 45, 58, 72, 75, 93, 96, 105, -1 };
    int *ttp = tt;

    i = 0;
    while( i < 105 )
    {
        ttx = trees[i].x;
        tty = trees[i].y;
        qsort( &trees[i], 105 - i, sizeof( _tree ), ct );

        i += *ttp;
        ttp++;
    }

    t = trees;
    i = 0;
    ttp = tt;
    while( *ttp != -1 )
    {
        Billboard *bb = new Billboard;
        //int starti = i;

        for( ; i < (*ttp); i++ )
        {
            t->x -= 0.3f;
            float  h = Hat(t->x, t->y, t->z );
            Vec3 pos( t->x, t->y, t->z-h );
            Geometry *geom = makeTree( t, dstate );
            bb->addDrawable( geom, pos );
            t++;
        }
        group->addChild( bb );
        ttp++;
    }

    return group;
}
Ejemplo n.º 23
0
int main(int argc, char **argv)
{
    // use an ArgumentParser object to manage the program arguments.
    ArgumentParser arguments(&argc,argv);
    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()
                         + " demonstrates using a floating point depth buffer.\nThe user can invert the depth buffer range and choose among available multi-sample configurations.");
    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
    arguments.getApplicationUsage()->addCommandLineOption("--far <number>", "Set far plane value");
    // if user request help write it out to cout.
    if (arguments.read("-h") || arguments.read("--help"))
    {
        arguments.getApplicationUsage()->write(std::cout);
        return 1;
    }
    float zFar = 500.0f;
    while (arguments.read("--far", zFar))
        ;
    // construct the viewer.
    osgViewer::Viewer viewer;
    ref_ptr<AppState> appState = new AppState(&viewer);
    viewer.addEventHandler(new osgViewer::StatsHandler);
    viewer.addEventHandler(new osgViewer::WindowSizeHandler);
    viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
    // The aspect ratio is set to the correct ratio for the window in
    // setupGC().
    viewer.getCamera()
        ->setProjectionMatrixAsPerspective(40.0, 1.0, appState->zNear, zFar);
    GraphicsContext* gc = setupGC(viewer, arguments);
    if (!gc)
        return 1;
    gc->setResizedCallback(new ResizedCallback(appState.get()));
    const GraphicsContext::Traits* traits = gc->getTraits();
    width = traits->width;
    height = traits->height;
    if (arguments.argc()<=1)
    {
        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
        return 1;
    }
    ref_ptr<Node> loadedModel = osgDB::readNodeFiles(arguments);
    if (!loadedModel) {
        cerr << "couldn't load " << argv[1] << "\n";
        return 1;
    }
    osgUtil::Optimizer optimizer;
    optimizer.optimize(loadedModel.get());
    // creates texture to be rendered
    Switch* sw = makeTexturesAndGeometry(width, height, appState->sw.get());
    ref_ptr<Camera> rttCamera = makeRttCamera(gc, width, height);
    rttCamera->setRenderOrder(Camera::PRE_RENDER);
    viewer.addSlave(rttCamera.get());
    appState->camera = rttCamera;
    // geometry and slave camera to display the result
    Group* displayRoot = new Group;
    displayRoot->addChild(sw);
    displayRoot->addChild(appState->textProjection.get());
    StateSet* displaySS = displayRoot->getOrCreateStateSet();
    displaySS->setMode(GL_LIGHTING, StateAttribute::OFF);
    displaySS->setMode(GL_DEPTH_TEST, StateAttribute::OFF);
    Camera* texCamera = new Camera;
    texCamera->setGraphicsContext(gc);
    texCamera->setClearMask(GL_COLOR_BUFFER_BIT);
    texCamera->setClearColor(Vec4(0.0, 0.0, 0.0, 0.0));
    texCamera->setReferenceFrame(Camera::ABSOLUTE_RF);
    texCamera->setViewport(0, 0, width, height);
    texCamera->setDrawBuffer(GL_BACK);
    texCamera->setReadBuffer(GL_BACK);
    texCamera->addChild(displayRoot);
    texCamera->setAllowEventFocus(false);
    texCamera->setCullingMode(CullSettings::NO_CULLING);
    texCamera->setProjectionResizePolicy(Camera::FIXED);
    viewer.addSlave(texCamera, Matrixd(), Matrixd(), false);
    viewer.addEventHandler(new ConfigHandler(appState.get()));

    // add model to the viewer.
    Group* sceneRoot = new Group;
    StateSet* sceneSS = sceneRoot->getOrCreateStateSet();
    Depth* depth = new Depth(Depth::GEQUAL, 1.0, 0.0);
    sceneSS->setAttributeAndModes(depth,(StateAttribute::ON
                                         | StateAttribute::OVERRIDE));
#if 0
    // Hack to work around Blender osg export bug
    sceneSS->setAttributeAndModes(new CullFace(CullFace::BACK));
#endif
    sceneRoot->addChild(loadedModel.get());
    appState->setStateFromConfig(validConfigs[0]);
    appState->updateNear();
    viewer.addEventHandler(new DepthHandler(appState.get(), depth));
    // add the help handler
    viewer.addEventHandler(new osgViewer
                           ::HelpHandler(arguments.getApplicationUsage()));

    viewer.setSceneData(sceneRoot);

    return viewer.run();
}