コード例 #1
0
ファイル: Dragger.cpp プロジェクト: joevandyk/osg
void osgManipulator::setMaterialColor(const osg::Vec4& color, osg::Node& node)
{
    osg::Material* mat = dynamic_cast<osg::Material*>(node.getOrCreateStateSet()->getAttribute(osg::StateAttribute::MATERIAL));
    if (! mat)
    {
        mat = new osg::Material;
        node.getOrCreateStateSet()->setAttribute(mat);
    }
    mat->setDiffuse(osg::Material::FRONT_AND_BACK, color);
}
コード例 #2
0
void TextureResizeNonPowerOfTwoHintVisitor::apply(osg::Node & node)
{
    osg::StateSet * stateset = node.getOrCreateStateSet();

    if(stateset)
    {
	setHint(stateset);
    }
    traverse(node);
}
コード例 #3
0
    void ShaderVisitor::createProgram(const ShaderRequirements &reqs, osg::Node& node)
    {
        osg::StateSet* writableStateSet = NULL;
        if (mAllowedToModifyStateSets)
            writableStateSet = node.getOrCreateStateSet();
        else
            writableStateSet = getWritableStateSet(node);

        ShaderManager::DefineMap defineMap;
        for (unsigned int i=0; i<sizeof(defaultTextures)/sizeof(defaultTextures[0]); ++i)
        {
            defineMap[defaultTextures[i]] = "0";
            defineMap[std::string(defaultTextures[i]) + std::string("UV")] = "0";
        }
        for (std::map<int, std::string>::const_iterator texIt = reqs.mTextures.begin(); texIt != reqs.mTextures.end(); ++texIt)
        {
            defineMap[texIt->second] = "1";
            defineMap[texIt->second + std::string("UV")] = boost::lexical_cast<std::string>(texIt->first);
        }

        if (!reqs.mColorMaterial)
            defineMap["colorMode"] = "0";
        else
        {
            switch (reqs.mVertexColorMode)
            {
            default:
            case GL_AMBIENT_AND_DIFFUSE:
                defineMap["colorMode"] = "2";
                break;
            case GL_EMISSION:
                defineMap["colorMode"] = "1";
                break;
            }
        }

        defineMap["forcePPL"] = mForcePerPixelLighting ? "1" : "0";
        defineMap["clamp"] = mClampLighting ? "1" : "0";

        osg::ref_ptr<osg::Shader> vertexShader (mShaderManager.getShader(mDefaultVsTemplate, defineMap, osg::Shader::VERTEX));
        osg::ref_ptr<osg::Shader> fragmentShader (mShaderManager.getShader(mDefaultFsTemplate, defineMap, osg::Shader::FRAGMENT));

        if (vertexShader && fragmentShader)
        {
            writableStateSet->setAttributeAndModes(mShaderManager.getProgram(vertexShader, fragmentShader), osg::StateAttribute::ON);

            for (std::map<int, std::string>::const_iterator texIt = reqs.mTextures.begin(); texIt != reqs.mTextures.end(); ++texIt)
            {
                writableStateSet->addUniform(new osg::Uniform(texIt->second.c_str(), texIt->first), osg::StateAttribute::ON);
            }
        }
    }
コード例 #4
0
void AnimationMaterialCallback::update(osg::Node& node)
{
    osg::StateSet* stateset = node.getOrCreateStateSet();
    osg::Material* material =
        dynamic_cast<osg::Material*>(stateset->getAttribute(osg::StateAttribute::MATERIAL));

    if (!material)
    {
        material = new osg::Material;
        stateset->setAttribute(material,osg::StateAttribute::OVERRIDE);
    }

    _animationMaterial->getMaterial(getAnimationTime(),*material);
}
コード例 #5
0
    virtual void entityAdded(igplugincore::PluginContext&, unsigned int, osg::Node& node, const std::string& fileName)
    {
        if (!osgDB::fileExists(fileName+".xml")) return;

        osgDB::XmlNode* root = osgDB::readXmlFile(fileName+".xml");
        if (!root) return;
        if (!root->children.size()) return;
        if (root->children.at(0)->name != "OpenIg-Model-Definition")  return;

        osgDB::XmlNode::Children::iterator itr = root->children.at(0)->children.begin();
        for ( ; itr != root->children.at(0)->children.end(); ++itr )
        {
            if ((**itr).name == "Material")
            {
                osgDB::XmlNode* materialNode = *itr;
                osgDB::XmlNode::Children::iterator citr = materialNode->children.begin();

                osg::ref_ptr<osg::Material> material;

                for ( ; citr != materialNode->children.end(); ++citr )
                {
                    if ((**citr).name == "Name")
                    {
                        if ((**citr).contents == "Day")
                            _dayMaterial = material = new osg::Material;
                        else
                        if ((**citr).contents == "Night")
                            _nightMaterial = material = new osg::Material;
                    }
                    if ((**citr).name == "Ambient")
                    {
                        if (material.valid())
                        {
                            igcore::StringUtils::Tokens tokens = igcore::StringUtils::instance()->tokenize((**citr).contents);
                            if (tokens.size() == 4)
                            {
                                float r = atof(tokens.at(0).c_str());
                                float g = atof(tokens.at(1).c_str());
                                float b = atof(tokens.at(2).c_str());
                                float a = atof(tokens.at(3).c_str());

                                material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(r,g,b,a));
                            }
                        }
                    }
                    if ((**citr).name == "Diffuse")
                    {
                        if (material.valid())
                        {
                            igcore::StringUtils::Tokens tokens = igcore::StringUtils::instance()->tokenize((**citr).contents);
                            if (tokens.size() == 4)
                            {
                                float r = atof(tokens.at(0).c_str());
                                float g = atof(tokens.at(1).c_str());
                                float b = atof(tokens.at(2).c_str());
                                float a = atof(tokens.at(3).c_str());

                                material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(r,g,b,a));
                            }
                        }
                    }
                    if ((**citr).name == "Specular")
                    {
                        if (material.valid())
                        {
                            igcore::StringUtils::Tokens tokens = igcore::StringUtils::instance()->tokenize((**citr).contents);
                            if (tokens.size() == 4)
                            {
                                float r = atof(tokens.at(0).c_str());
                                float g = atof(tokens.at(1).c_str());
                                float b = atof(tokens.at(2).c_str());
                                float a = atof(tokens.at(3).c_str());

                                material->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4(r,g,b,a));
                            }
                        }
                    }
                    if ((**citr).name == "Shininess")
                    {
                        if (material.valid())
                        {
                            float shininess = atof((**citr).contents.c_str());

                            material->setShininess(osg::Material::FRONT_AND_BACK,shininess);
                        }
                    }
                }
            }
        }

        if (_dayMaterial.valid() && _nightMaterial.valid())
        {
            _runtimeMaterial = new osg::Material(*_dayMaterial);

            node.getOrCreateStateSet()->setAttributeAndModes(_runtimeMaterial,osg::StateAttribute::ON|osg::StateAttribute::PROTECTED|osg::StateAttribute::OVERRIDE);
        }

    }