Beispiel #1
0
Scene Scene::deserialized(io::ReaderRef reader, AssetLoader<StaticMesh>& mesh_loader, const AssetPtr<Material>& default_material) {
	y_profile();

	struct Header {
		u32 magic;
		AssetType type;
		u32 version;

		u32 statics;
		u32 lights;

		bool is_valid() const {
			return magic == fs::magic_number &&
				   type == AssetType::Scene &&
				   version == 2;
		}
	};

	auto header = reader->read_one<Header>();
	if(!header.is_valid()) {
		y_throw("Invalid header.");
	}

	Scene scene;
	scene.static_meshes().set_min_capacity(header.statics);
	scene.lights().set_min_capacity(header.lights);

	{
		for(u32 i = 0; i != header.statics; ++i) {
			auto id = reader->read_one<AssetId>();
			auto transform = reader->read_one<math::Transform<>>();

			if(id == AssetId::invalid_id()) {
				log_msg("Skipping asset with invalid id.", Log::Warning);
				continue;
			}

			try {
				auto mesh = mesh_loader.load(id);
				auto inst = std::make_unique<StaticMeshInstance>(mesh, default_material);
				inst->transform() = transform;
				scene.static_meshes().emplace_back(std::move(inst));
			} catch(std::exception& e) {
				log_msg(fmt("Unable to load asset: %, Skipping.", e.what()), Log::Warning);
				continue;
			}
		}
	}

	{
		for(u32 i = 0; i != header.lights; ++i) {
			// load as point, then read on top. Maybe change this ?
			auto light = std::make_unique<Light>(Light::Point);
			reader->read_one(*light);
			scene.lights().emplace_back(std::move(light));
		}
	}

	return scene;
}
Beispiel #2
0
void addBunny( std::shared_ptr<Container> container )
{
    AssetLoader loader;
    std::string modelPath = "models";

    // bunnies
    std::string bunnyPath = modelPath + "/stanford/bunny/reconstruction";
    auto mesh = loader.load( bunnyPath + "/bun_zipper_res2.ply" );
    //auto mesh = loader.load( bunnyPath + "/bun_zipper_res4.ply" );

    if( !mesh ) {
        fprintf( stderr, "Error loading mesh\n" );
        return;
    }

    //mesh->material = std::make_shared<DiffuseMaterial>( 0.0f, 0.66, 0.42f ); // emerald green
    mesh->material = std::make_shared<MirrorMaterial>();

    printf("Building octree\n");
    auto mesh_octree = new TMOctreeAccelerator( *std::dynamic_pointer_cast<TriangleMesh>(mesh) );
    mesh_octree->build();
    mesh->accelerator = mesh_octree;
    mesh->transform = std::make_shared<Transform>();
    *mesh->transform = makeTranslation( Vector4( 0.0, 0.2, -0.5 ) );
    container->add( mesh );
}
Beispiel #3
0
//////////////////////////////////////////
//     Standard Material Test Models
//////////////////////////////////////////
std::shared_ptr<TriangleMesh> loadMaterialTestModel( AssetLoader & loader )
{
    std::string modelBasePath = "models";
    //auto mesh = loader.load( modelBasePath + "/dacunni/material_test1.obj" );
    //auto mesh = loader.load( modelBasePath + "/dacunni/material_test1.stl" );
    auto mesh = loader.load( modelBasePath + "/tf3dm.com/soccerball/untitled.ply" );
    return mesh;
}
Beispiel #4
0
//////////////////////////////////////////
//     Standard Material Test Models
//////////////////////////////////////////
std::shared_ptr<TriangleMesh> loadMaterialTestModel( AssetLoader & loader )
{
    std::string modelBasePath = "models";
    //auto mesh = loader.load( modelBasePath + "/dacunni/material_test1.obj" );
    //auto mesh = loader.load( modelBasePath + "/dacunni/material_test1.stl" );
    auto mesh = loader.load( modelBasePath + "/tf3dm.com/soccerball/untitled.ply" );
    //auto mesh = loader.load( modelBasePath + "/uvsphere.ply" );
    //auto mesh = loader.loadMultiPartMerged( modelBasePath + "/test_objects/mori/testObj.obj" );
#if 0
    auto mesh = loader.loadMultiPartMerged( modelBasePath + "/test_objects/mitsuba/mitsuba.obj" );
#endif
#if 0
    mesh->makeCanonical();
#endif
#if 1
    mesh->accelerator = new TMOctreeAccelerator( *mesh );
    mesh->accelerator->build();
#endif
    return mesh;
}