Пример #1
0
GLLObjFile::GLLObjFile(CFURLRef location)
{
    std::string filename = GLLStringFromFileURL(location);
    
    int fdes = ::open(filename.c_str(), O_RDONLY);
    if (fdes < 0) {
        throw std::runtime_error("Could not open file");
    }
    
    struct stat statistics;
    if (fstat(fdes, &statistics) < 0) {
        close(fdes);
        throw std::runtime_error("Could not get file size");
    }
    
    const char *buffer = (const char *) mmap(nullptr, statistics.st_size, PROT_READ, MAP_PRIVATE, fdes, 0);
    close(fdes);
    const char *current = buffer;
    const char *end = &buffer[statistics.st_size];
    
    materialLibraryURLs = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
    std::string activeMaterial("");
    unsigned activeMaterialStart = 0;
    bool hasFirstMaterial = false;
    while(current != end)
    {
        while (current != end && (*current == ' ' || *current == '\n' || *current == '\r')) {
            current++;
        }
        if (current == end)
            break;
        
        switch (*current) {
            case 'f':
                parseFace(current, end);
                break;
            case 'v':
                current += 1;
                switch (*current) {
                    case 'n': // Normals
                        current += 1;
                        parseVector(current, end, normals, 3);
                        break;
                    case 't': // Tex coords
                        current += 1;
                        parseVector(current, end, texCoords, 2);
                        break;
                    case 'c': // Colors
                        current += 1;
                        parseVector(current, end, colors, 4);
                        break;
                    case ' ': // Vertex
                        parseVector(current, end, vertices, 3);
                        break;
                    default:
                        skipToEndOfLine(current, end);
                        break;
                }
                break;
            case 'm':
                if (followsString(current, end, "mtllib")) {
                    std::string mtllib = stringToEndOfLine(current, end);
                    
                    try
                    {
                        CFURLRef mtllibLocation = GLLCreateURLFromString(mtllib, location);
                        CFArrayAppendValue(materialLibraryURLs, mtllibLocation);
                        CFRelease(mtllibLocation);
                    }
                    catch (std::exception &e)
                    {
                        std::cerr << "Ignoring mtllib: " << e.what() << std::endl;
                    }
                } else {
                    skipToEndOfLine(current, end);
                }
                break;
            case 'u':
                if (followsString(current, end, "usemtl")) {
                    if (hasFirstMaterial)
                    {
                        // End previous material run
                        materialRanges.push_back(MaterialRange(activeMaterialStart, (unsigned) originalIndices.size(), activeMaterial));
                    }
                    else
                        hasFirstMaterial = true;
                    
                    current += 1;
                    activeMaterial = stringToEndOfLine(current, end);
                    activeMaterialStart = (unsigned) originalIndices.size();
                } else {
                    skipToEndOfLine(current, end);
                }
                break;
            case '#': // Comment
            default:
                skipToEndOfLine(current, end);
                break;
        }
    }
    munmap((void *) buffer, statistics.st_size);
    
    // Wrap up final material group
    materialRanges.push_back(MaterialRange(activeMaterialStart, (unsigned) originalIndices.size(), activeMaterial));
    
    fillIndices();
}
Пример #2
0
void TriggerET::InitializeEntity(
	Entity& entity,
	const EntityModuleParameters* spawnParams) const
{
	auto gameEM = entity.GetModule<GameEM>();
	auto physicsEM = entity.GetModule<PhysicsEM>();
	auto sceneEM = entity.GetModule<SceneEM>();

	const auto params = static_cast<const TriggerEMParams*>(spawnParams);
	auto& timeController = params->m_timeController;
	auto& comboController = params->m_comboController;
	auto& player = params->m_player;
	auto triggerType = params->m_triggerType;

	auto trigger = std::unique_ptr<Trigger>(
		new Trigger(triggerType, player, comboController, timeController));

	auto collisionBox = std::unique_ptr<CollisionBox>(new CollisionBox());
	switch (triggerType)
	{
	case TriggerTypeBoost30:
	case TriggerTypeBoost45:
	case TriggerTypeBoost60:
		collisionBox->SetPosition(Vec3f(0.0f, 100.0f, -1.175f));
		collisionBox->SetDimensions(Vec3f(0.5f, 100.0f, 0.625f));
		break;

	case TriggerTypeSlow:
	case TriggerTypeAngle:
	case TriggerTypeStop:
	case TriggerTypeBlock:
		collisionBox->SetPosition(Vec3f(0.0f, 100.0f, -0.55f));
		collisionBox->SetDimensions(Vec3f(0.5f, 100.0f, 1.25f));
		break;
	}
	physicsEM->AddCollisionShape(std::move(collisionBox));
	physicsEM->SetCollisionListener(trigger.get());
	physicsEM->SetImmovable(true);

	gameEM->SetGameObject(std::move(trigger));

	std::string activeTextureFilePath;
	std::string inactiveTextureFilePath;

	switch(triggerType)
	{
	case TriggerTypeBoost30:
		activeTextureFilePath = "Triggers/Boost30Active.png";
		inactiveTextureFilePath = "Triggers/Boost30Inactive.png";
		break;

	case TriggerTypeBoost45:
		activeTextureFilePath = "Triggers/Boost45Active.png";
		inactiveTextureFilePath = "Triggers/Boost45Inactive.png";
		break;

	case TriggerTypeBoost60:
		activeTextureFilePath = "Triggers/Boost60Active.png";
		inactiveTextureFilePath = "Triggers/Boost60Inactive.png";
		break;

	case TriggerTypeSlow:
		activeTextureFilePath = "Triggers/SlowActive.png";
		inactiveTextureFilePath = "Triggers/SlowInactive.png";
		break;

	case TriggerTypeAngle:
		activeTextureFilePath = "Triggers/Angle.png";
		inactiveTextureFilePath = activeTextureFilePath;
		break;

	case TriggerTypeStop:
		activeTextureFilePath = "Triggers/StopActive.png";
		inactiveTextureFilePath = "Triggers/StopInactive.png";
		break;

	case TriggerTypeBlock:
		activeTextureFilePath = "Triggers/BlockActive.png";
		inactiveTextureFilePath = "Triggers/BlockInactive.png";
		break;
	}

	auto activeModel = Model("nanaka/models/billboard.nmdl");
	Material activeMaterial(activeModel.GetMaterial());
	activeMaterial.SetTexture(Texture(activeTextureFilePath));
	activeModel.SetMaterial(activeMaterial);

	activeModel.SetPosition(Vec3f(0.0f, 0.0f, 0.0f));
	activeModel.SetRotation(Quat::NoRot());
	activeModel.SetScale(Vec3f(2.0f, 1.0f, 4.0f));

	sceneEM->SetAttachment<Model>(s_gfxIdActive, activeModel);

	auto inactiveModel = Model("nanaka/models/billboard.nmdl");
	Material inactiveMaterial(inactiveModel.GetMaterial());
	inactiveMaterial.SetTexture(Texture(inactiveTextureFilePath));
	inactiveModel.SetMaterial(inactiveMaterial);

	inactiveModel.SetPosition(Vec3f(0.0f, 0.0f, 0.0f));
	inactiveModel.SetRotation(Quat::NoRot());
	inactiveModel.SetScale(Vec3f(2.0f, 1.0f, 4.0f));

	sceneEM->SetAttachment<Model>(s_gfxIdInactive, inactiveModel);
}