Example #1
0
ParticleSystemModel::Specification::Specification(const Any& a) {
    *this = Specification();

    if (a.nameBeginsWith("ParticleSystemModel::Emitter")) {        
        emitterArray.append(a);
    } else { 
        a.verifyNameBeginsWith("ParticleSystemModel::Specification");
        Any array = a.get("emitterArray", Any(Any::ARRAY));
        array.getArray(emitterArray);
        hasPhysics = a.get("hasPhysics", true);
    }

}
Example #2
0
Any Scene::load(const std::string& scene) {
    std::string filename;
    clear();
    m_modelTable.clear();
    m_name = scene;

    bool isFilename = endsWith(toLower(scene), ".scn.any") || endsWith(toLower(scene), ".Scene.Any");

    if (isFilename) {
        filename = scene;
    } else {
        const std::string* f = filenameTable().getPointer(scene);
        if (f == NULL) {
            throw "No scene with name '" + scene + "' found in (" + 
                stringJoin(filenameTable().getKeys(), ", ") + ")";
        }

        filename = *f;
    }

    Any any;
    any.load(filename);

    {
        const std::string& n = any.get("name", filename);

        // Ensure that this name appears in the filename table if it does not already,
        // so that it can be loaded by name in the future.
        if (! filenameTable().containsKey(n)) {
            filenameTable().set(n, filename);
        }
    }

    m_sourceAny = any;

    // Load the lighting environment (do this before loading entities, since some of them may
    // be lights that will enter this array)
    bool hasEnvironmentMap = false;
    if (any.containsKey("localLightingEnvironment")) {
         m_localLightingEnvironment = any["localLightingEnvironment"];
         hasEnvironmentMap = any["localLightingEnvironment"].containsKey("environmentMap");
    }
        

    // Load the models
    if (any.containsKey("models")) {
        Any models = any["models"];
        if (models.size() > 0) {
            for (Any::AnyTable::Iterator it = models.table().begin(); it.isValid(); ++it) {
                const std::string name = it->key;
                Any v = it->value;
                createModel(v, name);
            }
        }
    }

    // Instance the models
    if (any.containsKey("entities")) { 
        Any entities = any["entities"];
        if (entities.size() > 0) {
            for (Table<std::string, Any>::Iterator it = entities.table().begin(); it.isValid(); ++it) {
                const std::string& name = it->key;
                const std::string& entityType = it->value.name();

                createEntity(entityType, name, it->value);
            }
        }
    }
    
    shared_ptr<Texture> skyboxTexture = Texture::whiteCube();

    Any skyAny;

    // Use the environment map as a skybox if there isn't one already, and vice versa
    Array<shared_ptr<Skybox> > skyboxes;
    getTypedEntityArray<Skybox>(skyboxes);
    if ( skyboxes.size() == 0 ) {
        if (any.containsKey("skybox")) {
            createEntity("Skybox", "skybox", any["skybox"]);
            m_skybox = typedEntity<Skybox>("skybox");
        } else if (hasEnvironmentMap) {
            m_skybox = Skybox::create("skybox", this, Array<ScaledTexture>(m_localLightingEnvironment.environmentMapArray[0]), Array<SimTime>(0.0), 0, SplineExtrapolationMode::CLAMP, false, false);
            insert(m_skybox);
        } else {
            m_skybox = Skybox::create("skybox", this, Array<ScaledTexture>(ScaledTexture(Texture::whiteCube(), 1.0f)), Array<SimTime>(0.0), 0, SplineExtrapolationMode::CLAMP, false, false);
            insert(m_skybox);
        }
    }

    if (any.containsKey("environmentMap")) {
        throw std::string("environmentMap field has been replaced with localLightingEnvironment");
    }


    // Default to using the skybox as an environment map if none is specified.
    if (! hasEnvironmentMap) {
        m_localLightingEnvironment.environmentMapArray.append(ScaledTexture(m_skybox->keyframeArray()[0].texture, m_skybox->keyframeArray()[0].constant));
    }


    //////////////////////////////////////////////////////

    if (m_cameraArray.size() == 0) {
        // Create a default camera, back it up from the origin
        m_cameraArray.append(Camera::create("camera"));
        m_cameraArray.last()->setFrame(CFrame::fromXYZYPRDegrees(0,1,-5,0,-5));
    }

    setTime(any.get("time", 0.0));
    m_lastVisibleChangeTime = m_lastLightChangeTime = m_lastStructuralChangeTime = System::time();

    m_defaultCameraName = (std::string)any.get("defaultCamera", "");


    // Set the initial positions, repeating a few times to allow
    // objects defined relative to others to reach a fixed point
    for (int i = 0; i < 3; ++i) {
        for (int e = 0; e < m_entityArray.size(); ++e) {
            m_entityArray[e]->onSimulation(m_time, nan());
        }
    }

    // Pose objects so that they have bounds.
    {
        Array< shared_ptr<Surface> > ignore;
        onPose(ignore);
    }

    return any;
}
Example #3
0
/**
   \param flags first byte of websocket frame, see websocket RFC, http://tools.ietf.org/html/rfc6455#section-5.2
   \param data, data_len payload data. Mask, if any, is already applied. */
static int websocket_data_handler(struct mg_connection* conn, int flags, char* data, size_t data_len) {
    // Lower 4 bits are the opcode
    const int opcode = flags & 0xF;

    switch (opcode) {
    case 0x0: // Continuation
        debugPrintf("Connection 0x%x: Received continuation, ignoring\n", (unsigned int)(uintptr_t)conn);
        return 1;

    case 0x1: // Text
        break;

    case 0x2: // Binary
        debugPrintf("Connection 0x%x: Received binary data, ignoring\n", (unsigned int)(uintptr_t)conn);
        return 1;

    case 0x8: // Close connection
        debugPrintf("Connection 0x%x: Received close connection\n", (unsigned int)(uintptr_t)conn);
        return 0;

    case 0x9: // Ping
        debugPrintf("Connection 0x%x: Received ping\n", (unsigned int)(uintptr_t)conn);
        // TODO: Send pong
        return 1;

    case 0xA: // Pong
        // Don't have to do anything
        debugPrintf("Connection 0x%x: Received pong\n", (unsigned int)(uintptr_t)conn);
        return 1;

    default: // Reserved
        debugPrintf("Connection 0x%x: Received reserved opcode 0x%x, ignoring\n", (unsigned int)(uintptr_t)conn, opcode);
        return 1;
   }
                                      
    // debugPrintf("rcv: '%.*s'\n", (int) data_len, data);

    if ((data_len == 6) && (memcmp(data, "\"ping\"", 6) == 0)) {
        // This is our application protocol ping message; ignore it
        return 1;
    }

    if ((data_len < 2) || (data[0] != '{')) {
        // Some corrupt message
        debugPrintf("Message makes no sense\n");
        return 1;
    }


    try {
        TextInput t(TextInput::FROM_STRING, data, data_len);
        const Any msg(t);

        const int UNKNOWN = 0;
        const int SEND_IMAGE = 1000;
        const int type = msg.get("type", UNKNOWN);

        switch (type) {
        case UNKNOWN:
            debugPrintf("Cannot identify message type\n");
            break;

        case SEND_IMAGE:
            clientWantsImage = 1;
            break;

        case GEventType::KEY_DOWN:
        case GEventType::KEY_UP:
            {
                GEvent event;
                memset(&event, 0, sizeof(event));
                event.type = type;
                Any key = msg.get("key", Any());
                Any keysym = key.get("keysym", Any());
                event.key.keysym.sym = GKey::Value((int)keysym.get("sym", 0));
                event.key.state = (type == GEventType::KEY_DOWN) ? GButtonState::PRESSED : GButtonState::RELEASED;
                remoteEventQueue.pushBack(event);
            }
            break;

        default:
            debugPrintf("Unrecognized type\n");
            break;
        };

    } catch (...) {
        debugPrintf("Message makes no sense\n");
    }

    // Returning zero means stoping websocket conversation.
    return 1;
}
Example #4
0
Scene::Ref Scene::create(const std::string& scene, GCamera& camera) 
{
	if (scene == "") 
	{
		return NULL;
	}

	Scene::Ref s = new Scene();

	const std::string* f = filenameTable().getPointer(scene);
	if (f == NULL) 
	{
		throw "No scene with name '" + scene + "' found in (" + 
			stringJoin(filenameTable().getKeys(), ", ") + ")";
	}
	const std::string& filename = *f;

	Any any;
	any.load(filename);

	// Load the lighting
	s->m_lighting = Lighting::create(any.get("lighting", Lighting::Specification()));

	// Load the models
	Any models = any["models"];
	typedef ReferenceCountedPointer<ReferenceCountedObject> ModelRef;
	Table< std::string, ModelRef > modelTable;
	for (Any::AnyTable::Iterator it = models.table().begin(); it.hasMore(); ++it) {
		ModelRef m;
		Any v = it->value;
		if (v.nameBeginsWith("ArticulatedModel")) {
			m = ArticulatedModel::create(v);
		} else if (v.nameBeginsWith("MD2Model")) {
			m = MD2Model::create(v);
		} else if (v.nameBeginsWith("MD3Model")) {
			m = MD3Model::create(v);
		} else {
			debugAssertM(false, "Unrecognized model type: " + v.name());
		}

		modelTable.set(it->key, m);        
	}

	// Instance the models
	Any entities = any["entities"];
	for (Table<std::string, Any>::Iterator it = entities.table().begin(); it.hasMore(); ++it) {
		const std::string& name = it->key;
		const Any& modelArgs = it->value;

		modelArgs.verifyType(Any::ARRAY);
		const ModelRef* model = modelTable.getPointer(modelArgs.name());
		modelArgs.verify((model != NULL), 
			"Can't instantiate undefined model named " + modelArgs.name() + ".");

		PhysicsFrameSpline frameSpline;
		ArticulatedModel::PoseSpline poseSpline;
		if (modelArgs.size() >= 1) {
			frameSpline = modelArgs[0];
			if (modelArgs.size() >= 2) {
				// Poses 
				poseSpline = modelArgs[1];
			}
		}

		ArticulatedModel::Ref artModel = model->downcast<ArticulatedModel>();
		MD2Model::Ref         md2Model = model->downcast<MD2Model>();
		MD3Model::Ref         md3Model = model->downcast<MD3Model>();

		if (artModel.notNull()) {
			s->m_entityArray.append(Entity::create(name, frameSpline, artModel, poseSpline));
		} else if (md2Model.notNull()) {
			s->m_entityArray.append(Entity::create(name, frameSpline, md2Model));
		} else if (md3Model.notNull()) {
			s->m_entityArray.append(Entity::create(name, frameSpline, md3Model));
		}
	}

	// Load the camera
	camera = any["camera"];

	if (any.containsKey("skybox")) {
		Any sky = any["skyBox"];
		s->m_skyBoxConstant = sky.get("constant", 1.0f);
		if (sky.containsKey("texture")) {
			s->m_skyBoxTexture = Texture::create(sky["texture"]);
		}
	} else {
		s->m_skyBoxTexture = s->m_lighting->environmentMapTexture;
		s->m_skyBoxConstant = s->m_lighting->environmentMapConstant;
	}

	// Default to using the skybox as an environment map if none is specified.
	if (s->m_skyBoxTexture.notNull() && s->m_lighting->environmentMapTexture.isNull()) {
		s->m_lighting->environmentMapTexture  = s->m_skyBoxTexture;
		s->m_lighting->environmentMapConstant = s->m_skyBoxConstant;
	}

	return s;
}