Ejemplo n.º 1
0
JSONDrawArrayLengths::JSONDrawArrayLengths(osg::DrawArrayLengths& array)
{
    getMaps()["First"] = new JSONValue<int>(array.getFirst());
    getMaps()["Mode"] = getDrawMode(array.getMode());

    JSONArray* jsonArray = new JSONArray;
    for (unsigned int i = 0; i < array.size(); i++) {
        jsonArray->getArray().push_back(new JSONValue<int>(array[i]));
    }
    getMaps()["ArrayLengths"] = jsonArray;
}
Ejemplo n.º 2
0
JSONObject* WriteVisitor::createJSONStateSet(osg::StateSet* stateset)
{

    if (_maps.find(stateset) != _maps.end()) {
        return _maps[stateset]->getShadowObject();
    }

    osg::ref_ptr<JSONObject> jsonStateSet = new JSONStateSet;
    _maps[stateset] = jsonStateSet;
    jsonStateSet->addUniqueID();

    translateObject(jsonStateSet.get(), stateset);

    if (stateset->getRenderingHint() == osg::StateSet::TRANSPARENT_BIN) {
        jsonStateSet->getMaps()["RenderingHint"] = new JSONValue<std::string>("TRANSPARENT_BIN");
    }

    bool blendEnabled = false;
    if (stateset->getMode(GL_BLEND) == osg::StateAttribute::ON) {
        blendEnabled = true;
    }

    osg::ref_ptr<JSONArray> textureAttributeList = new JSONArray;
    int lastTextureIndex = -1;
    for (int i = 0; i < 32; ++i) {
        osg::Texture* texture = dynamic_cast<osg::Texture*>(stateset->getTextureAttribute(i,osg::StateAttribute::TEXTURE));

        JSONArray* textureUnit = new JSONArray;
        JSONObject* jsonTexture = createJSONTexture(texture);
        textureAttributeList->getArray().push_back(textureUnit);

        if (jsonTexture) {
            JSONObject* textureObject = new JSONObject;
            textureObject->getMaps()["osg.Texture"] = jsonTexture;
            textureUnit->getArray().push_back(textureObject);
            lastTextureIndex = i;
        }
    }
    if (lastTextureIndex > -1) {
        textureAttributeList->getArray().resize(lastTextureIndex+1);
        jsonStateSet->getMaps()["TextureAttributeList"] = textureAttributeList;
    }


    osg::ref_ptr<JSONArray> attributeList = new JSONArray;

    osg::Material* material = dynamic_cast<osg::Material*>(stateset->getAttribute(osg::StateAttribute::MATERIAL));
    if (material) {
        JSONObject* obj = new JSONObject;
        obj->getMaps()["osg.Material"] = createJSONMaterial(material);
        attributeList->getArray().push_back(obj);
    }

    osg::BlendFunc* blendFunc = dynamic_cast<osg::BlendFunc*>(stateset->getAttribute(osg::StateAttribute::BLENDFUNC));
    if (blendFunc) {
        JSONObject* obj = new JSONObject;
        obj->getMaps()["osg.BlendFunc"] = createJSONBlendFunc(blendFunc);
        attributeList->getArray().push_back(obj);
    } else if (blendEnabled == true) {
        JSONObject* obj = new JSONObject;
        osg::ref_ptr<osg::BlendFunc> defaultBlend = new osg::BlendFunc();
        obj->getMaps()["osg.BlendFunc"] = createJSONBlendFunc(defaultBlend.get());
        attributeList->getArray().push_back(obj);
    }

    osg::ref_ptr<osg::CullFace> cullFace = dynamic_cast<osg::CullFace*>(stateset->getAttribute(osg::StateAttribute::CULLFACE));
    osg::StateAttribute::GLModeValue cullMode = stateset->getMode(GL_CULL_FACE);
    if (cullFace || cullMode != osg::StateAttribute::INHERIT) {
        JSONObject* obj = new JSONObject;
        JSONObject* cf = 0;
        if (cullMode == osg::StateAttribute::OFF) {
            osg::ref_ptr<osg::CullFace> defaultCull = new osg::CullFace();
            cf = createJSONCullFace(defaultCull.get());
            cf->getMaps()["Mode"] = new JSONValue<std::string>("DISABLE");
            obj->getMaps()["osg.CullFace"] = cf;
            attributeList->getArray().push_back(obj);
        } else {
            if (!cullFace) {
                cullFace = new osg::CullFace();
            }
            cf = createJSONCullFace(cullFace);
        }
        obj->getMaps()["osg.CullFace"] = cf;
        attributeList->getArray().push_back(obj);
    }

    osg::BlendColor* blendColor = dynamic_cast<osg::BlendColor*>(stateset->getAttribute(osg::StateAttribute::BLENDCOLOR));
    if (blendColor) {
        JSONObject* obj = new JSONObject;
        obj->getMaps()["osg.BlendColor"] = createJSONBlendColor(blendColor);
        attributeList->getArray().push_back(obj);
    }
    

    if (!attributeList->getArray().empty()) {
        jsonStateSet->getMaps()["AttributeList"] = attributeList;
    }


    osg::StateSet::ModeList modeList = stateset->getModeList();
    for (unsigned int i = 0; i < modeList.size(); ++i) {
        // add modes
    }

    if (jsonStateSet->getMaps().empty())
        return 0;
    return jsonStateSet.release();
}
Ejemplo n.º 3
0
void translateObject(JSONObject* json, osg::Object* osg)
{
    if (!osg->getName().empty()) {
        json->getMaps()["Name"] = new JSONValue<std::string>(osg->getName());
    }

    osgSim::ShapeAttributeList* osgSim_userdata = dynamic_cast<osgSim::ShapeAttributeList* >(osg->getUserData());
    if (osgSim_userdata) {
        JSONObject* jsonUDC = new JSONObject();
        jsonUDC->addUniqueID();

        JSONArray* jsonUDCArray = new JSONArray();
        jsonUDC->getMaps()["Values"] = jsonUDCArray;
        for (unsigned int i = 0; i < osgSim_userdata->size(); i++) {
            const osgSim::ShapeAttribute& attr = (*osgSim_userdata)[i];
            JSONObject* jsonEntry = new JSONObject();
            jsonEntry->getMaps()["Name"] = new JSONValue<std::string>(attr.getName());
            osg::ref_ptr<JSONValue<std::string> > value;
            switch(attr.getType()) {
            case osgSim::ShapeAttribute::INTEGER:
            {
                std::stringstream ss;
                ss << attr.getInt();
                value = new JSONValue<std::string>(ss.str());
            }
            break;
            case osgSim::ShapeAttribute::DOUBLE:
            {
                std::stringstream ss;
                ss << attr.getDouble();
                value = new JSONValue<std::string>(ss.str());
            }
            break;
            case osgSim::ShapeAttribute::STRING:
            {
                std::stringstream ss;
                ss << attr.getString();
                value = new JSONValue<std::string>(ss.str());
            }
            break;
            }
            jsonEntry->getMaps()["Value"] = value;
            jsonUDCArray->getArray().push_back(jsonEntry);
        }
        json->getMaps()["UserDataContainer"] = jsonUDC;

    } else if (osg->getUserDataContainer()) {
        JSONObject* jsonUDC = new JSONObject();
        jsonUDC->addUniqueID();

        if (!osg->getUserDataContainer()->getName().empty()) {
            jsonUDC->getMaps()["Name"] = new JSONValue<std::string>(osg->getUserDataContainer()->getName());
        }
        JSONArray* jsonUDCArray = new JSONArray();
        jsonUDC->getMaps()["Values"] = jsonUDCArray;
        for (unsigned int i = 0; i < osg->getUserDataContainer()->getNumUserObjects(); i++) {
            osg::Object* o = osg->getUserDataContainer()->getUserObject(i);
            typedef osg::TemplateValueObject<std::string> ValueObject;
            {
                ValueObject* uv = dynamic_cast<ValueObject* >(o);
                if (uv) {
                    JSONObject* jsonEntry = new JSONObject();
                    jsonEntry->getMaps()["Name"] = new JSONValue<std::string>(uv->getName());
                    jsonEntry->getMaps()["Value"] = new JSONValue<std::string>(uv->getValue());
                    jsonUDCArray->getArray().push_back(jsonEntry);
                }
            }

        }
        json->getMaps()["UserDataContainer"] = jsonUDC;
    }
}