Esempio n. 1
0
	MainApplication::ComponentCollection MainApplication::addDrawDebug(MeshCollider * collider, int level)
	{
		BVHNode * root = collider->m_bvh->root();

		Material * wireframe = new Material(Shader::find("shader"));
		wireframe->setAmbientColor(glm::vec4(1.0f));
		wireframe->setWireframe(true);

		std::vector<BVHNode *> nodes;
		collectBvhs(nodes, root, level, level);

		int leaves = 0;
		ComponentCollection components;

		for (unsigned i = 0; i < nodes.size(); i++) {
			BoundingSphere * bs = nodes[i]->m_bv;
			if (nodes[i]->m_isLeaf)
				leaves++;

			MeshObject * sphere = new MeshObject(MeshFactory::Sphere(glm::vec4(1.0f), 10), wireframe);

			sphere->transform().translate(bs->c);
			sphere->transform().translate(collider->transform().position());
			sphere->transform().scale(glm::vec3(1.0f) * bs->r);

			components.push_back(sphere);
		}

		Trace::info("Nodes at level %d: %d (%d leaves)\n", level, nodes.size(), leaves);

		return components;
	}
Esempio n. 2
0
	void MainApplication::initScene()
	{
		m_components.clear();
		m_debugComponents.clear();
		g_world = SoftBodyWorld();
		g_body = 0;

		// Create wood material
		Material * woodMaterial = new Material(Shader::find("shader"));
		woodMaterial->setTexture(new Texture("resources/wood.bmp"));

		// Load bowl model
		MeshObject * bowl = new MeshObject(MeshFactory::FromFile("resources/bowl.ply"), woodMaterial);
		bowl->transform().translate(glm::vec3(0.0f, 2.0f, 0.0f));
		bowl->transform().update();

		// Load low-poly model of bowl for collision detection
		Mesh * lowpoly = MeshFactory::FromFile("resources/bowl-low.ply");

		MeshCollider * bowlCollider = new MeshCollider(bowl);
		bowlCollider->m_mesh = lowpoly;
		bowlCollider->m_bvh = BVH::constructFromMesh(lowpoly);
		CollisionDetector::instance()->addCollider(bowlCollider);

		m_components.push_back(bowl);

		// Create cloth material
		Material * clothMaterial = new Material(Shader::find("shader"));
		clothMaterial->setTexture(new Texture("resources/cloth.bmp"));
		clothMaterial->setDiffuseColor(glm::vec4(0.8f, 0.8f, 0.8f, 1.0f));
		clothMaterial->setAmbientColor(glm::vec4(0.3f, 0.3f, 0.3f, 1.0f));
		clothMaterial->setSpecularColor(glm::vec4(0.5f, 0.5f, 0.5f, 1.0f));

		// Create cloth mesh with attached soft body information
		MeshObject * cloth = SoftBody::createCloth(clothMaterial, &g_world, &g_body);
		cloth->transform().translate(glm::vec3(-2.5f, 5.0f, -2.5f));
		cloth->transform().update();

		m_components.push_back(cloth);

		// Create material for apple
		Material * appleMaterial = new Material(Shader::find("shader"));
		appleMaterial->setDiffuseColor(glm::vec4(0.9f));
		appleMaterial->setTexture(new Texture("resources/apple.bmp"));

		// Load apple model from file
		MeshObject * apple = new MeshObject(MeshFactory::FromFile("resources/apple.ply"), appleMaterial);
		apple->transform().translate(glm::vec3(0.0f, 3.0f, 10.0f));
		apple->transform().scale(glm::vec3(0.5f));

		// Create a mathematical sphere collider for the apple
		SphereCollider * sphereCollider = new SphereCollider(apple);
		CollisionDetector::instance()->addCollider(sphereCollider);

		m_components.push_back(apple);

		// Create animator that animates the apple
		KeyframeAnimator<glm::vec3> * anim = new KeyframeAnimator<glm::vec3>(apple, new LinearInterpolator<glm::vec3>, apple->transform().position());
		anim->addKeyframe(0.0f, glm::vec3(0.0f, 3.0f, 10.0f));
		anim->addKeyframe(20000.0f, glm::vec3(0.0f, 3.0f, -10.0f));
		anim->addKeyframe(40000.0f, glm::vec3(0.0f, 3.0f, 10.0f));
		m_ballAnimator = anim;

		// Create some debug spheres for the BVH
		int level = 0;
		while (true) {
			ComponentCollection components = addDrawDebug(bowlCollider, level);
			if (components.empty())
				break;

			m_debugComponents[level] = components;
			level++;
		}
	}
Esempio n. 3
0
	void MainApplication::initScene()
	{
		m_components.clear();

		Material * grassMaterial = new Material(Shader::find("shader"));
		grassMaterial->setTexture(new Texture("resources/ground.bmp"));
		grassMaterial->setDiffuseColor(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
		grassMaterial->setAmbientColor(glm::vec4(0.5f, 0.5f, 0.5f, 1.0f));

		Mesh * terrain = MeshFactory::Terrain();
		MeshObject * grass = new MeshObject(terrain, grassMaterial);
		grass->transform().scale(glm::vec3(10.0f));
		grass->transform().translate(glm::vec3(-5.0f, 0.0f, -5.0f));

		m_components.push_back(grass);

		{ // palm tree
			PlantDefinition * def = new PlantDefinition();
			def->iterations = 2;
			def->angle = 22.5f;
			def->diameter = 0.10f;
			def->length = 0.20f;
			def->thinning = 1.9f;
			def->axiom = "F";
			def->addTerminals("+-!?&/[]Q");
			def->addProduction('F', "F[-F]F[+F]F[&F]F[/F]F[QQQQQ]F");
			def->barkTexture = "resources/bark_palm.bmp";
			def->leafTexture = "resources/palm_leaf.bmp";
			def->size = 0.5f;

			LSystem * system = new LSystem(def);

			glm::vec3 position = terrain->vertexAt(random.randXX(0, terrain->vertices().size()-1)).position;
			position = glm::vec3(grass->transform().world() * glm::vec4(position, 1.0f));
			system->transform().translate(position);
			m_components.push_back(system);
		}

		{ // tropical tree
			PlantDefinition * def = new PlantDefinition();
			def->addTerminals("+-!?&/[]<Q`");
			def->iterations = 7;
			def->angle = 22.5f;
			def->diameter = 0.15f;
			def->length = 0.15f;
			def->thinning = 1.5f;
			def->axiom = "A";
			def->addProduction('A', "[+FLA]?????[+FLA]???????`[+FLA]");
			def->addProduction('F', "S?????F");
			def->addProduction('S', "F");
			def->addProduction('L', "[Q--Q][Q&&Q]");
			def->barkTexture = "resources/bark.bmp";
			def->leafTexture = "resources/leaf.bmp";

			LSystem * system = new LSystem(def);

			glm::vec3 position = terrain->vertexAt(random.randXX(0, terrain->vertices().size()-1)).position;
			position = glm::vec3(grass->transform().world() * glm::vec4(position, 1.0f));
			system->transform().translate(position);
			m_components.push_back(system);
		}

		{ // small tree
			PlantDefinition * def = new PlantDefinition();
			def->addTerminals("+-!?&/[]<FQ");
			def->iterations = 13;
			def->angle = 15.0f;
			def->diameter = 0.02f;
			def->length = 0.15f;
			def->thinning = 1.3f;
			def->axiom = "FA";
			def->addProduction('A', "/FB???B?????BQ");
			def->addProduction('B', "[//F??????A]");
			def->barkTexture = "resources/bark2.bmp";
			def->leafTexture = "resources/papaya_leaf.bmp";

			LSystem * system = new LSystem(def, 2000.0f);
		
			glm::vec3 position = terrain->vertexAt(random.randXX(0, terrain->vertices().size()-1)).position;
			position = glm::vec3(grass->transform().world() * glm::vec4(position, 1.0f));
			system->transform().translate(position);

			m_components.push_back(system);
		}

		{ // "maple" tree
			PlantDefinition * def = new PlantDefinition();
			def->addTerminals("+-!?&/[]<FQ");
			def->iterations = 6;
			def->angle = 19.0f;
			def->diameter = 0.075f;
			def->length = 0.20f;
			def->thinning = 2.0f;
			def->axiom = "F&A";
			def->addProduction('A', "F[-F/AQ]?[+FAQ]&[!FAQ]");
			def->barkTexture = "resources/bark_maple.bmp";
			def->leafTexture = "resources/maple_leaf.bmp";

			LSystem * system = new LSystem(def, 6000.0f);
		
			glm::vec3 position = terrain->vertexAt(random.randXX(0, terrain->vertices().size()-1)).position;
			position = glm::vec3(grass->transform().world() * glm::vec4(position, 1.0f));
			system->transform().translate(position);

			m_components.push_back(system);
		}

		{ // deathly tree
			PlantDefinition * def = new PlantDefinition();
			def->addTerminals("+-!?&/[]Q");
			def->iterations = 3;
			def->angle = 22.5f;
			def->diameter = 0.02f;
			def->length = 0.20f;
			def->thinning = 1.1f;
			def->axiom = "F";
			def->addProduction('F', "FF-[-F&F!F]QQ![&F??FQ-FQ]");
			def->barkTexture = "resources/bark_black.bmp";
			def->leafTexture = "resources/white_leaf.bmp";

			LSystem * system = new LSystem(def, 3500.0f);
		
			glm::vec3 position = terrain->vertexAt(random.randXX(0, terrain->vertices().size()-1)).position;
			position = glm::vec3(grass->transform().world() * glm::vec4(position, 1.0f));
			system->transform().translate(position);

			m_components.push_back(system);
		}

		//Trace::info("Generated: %s\n", plant->generate().c_str());
	}
Esempio n. 4
0
	void MainApplication::setupSimulation()
	{
		m_started = false;

		/// Create floor

		Material * floorMaterial = new Material(Shader::find("shader"));
		floorMaterial->setTexture(new Texture("resources/floor.bmp"));
		floorMaterial->setAmbientColor(glm::vec4(0.3f));
		floorMaterial->setShininess(120.0f);
		floorMaterial->setDiffuseColor(glm::vec4(0.6f));
		floorMaterial->setSpecularColor(glm::vec4(0.2f));

		MeshObject * floor = new MeshObject(MeshFactory::Plane(glm::vec4(1.0f), 5), floorMaterial);
		floor->transform().scale() = glm::vec3(5.0f, 1.0f, 5.0f);

		PlaneCollider * floorCollider = new PlaneCollider(floor, 0);
		floorCollider->m_normal = glm::vec3(0.0f, 1.0f, 0.0f);
		floorCollider->m_d = 0.0f;
		floorCollider->name = "floor";
		CollisionDetector::instance()->addCollider(floorCollider);

		m_components.push_back(floor);

		// Create pool table

		Material * tableMaterial = new Material(Shader::find("shader"));
		tableMaterial->setDiffuseColor(glm::vec4(0.8f));
		tableMaterial->setAmbientColor(glm::vec4(0.2f));
		tableMaterial->setSpecularColor(glm::vec4(0.3f));
		tableMaterial->setTexture(new Texture("resources/table.bmp"));

		MeshObject * table = new MeshObject(MeshFactory::FromFile("resources/pooltable.ply"), tableMaterial);
		//table->transform().scale() = glm::vec3(0.01f);
		m_components.push_back(table);

		// Create some walls

		PlaneCollider * tabletop = new PlaneCollider(0);
		tabletop->m_normal = glm::vec3(0.0f, 1.0f, 0.0f);
		tabletop->m_d = 1.2f;
		tabletop->name = "tabletop";
		CollisionDetector::instance()->addCollider(tabletop);

		PlaneCollider * wall1 = new PlaneCollider(0);
		wall1->m_normal = glm::vec3(-1.0f, 0.0f, 0.0f);
		wall1->m_d = -0.939f;
		wall1->name = "wall1";
		CollisionDetector::instance()->addCollider(wall1);

		PlaneCollider * wall2 = new PlaneCollider(0);
		wall2->m_normal = glm::vec3(1.0f, 0.0f, 0.0f);
		wall2->m_d = -0.920f;
		wall2->name = "wall2";
		CollisionDetector::instance()->addCollider(wall2);

		PlaneCollider * wall3 = new PlaneCollider(0);
		wall3->m_normal = glm::vec3(0.0f, 0.0f, -1.0f);
		wall3->m_d = -1.812f;
		wall3->name = "wall3";
		CollisionDetector::instance()->addCollider(wall3);

		PlaneCollider * wall4 = new PlaneCollider(0);
		wall4->m_normal = glm::vec3(0.0f, 0.0f, 1.0f);
		wall4->m_d = -1.839f;
		wall4->name = "wall4";
		CollisionDetector::instance()->addCollider(wall4);

		/// Create cube

		int count = 15;
		float radius = 0.025f;
		float omega = float(2*M_PI) / count;

		for (int i = 0; i < count; i++)
		{
			Material * cubeMaterial = new Material(Shader::find("shader"));
			std::stringstream ss;
			ss << "resources/ball-" << i << ".bmp";
			cubeMaterial->setTexture(new Texture(ss.str().c_str()));
			cubeMaterial->setDiffuseColor(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));

			MeshObject * cube = new MeshObject(MeshFactory::Sphere(), cubeMaterial);
			//cube->transform().translate(glm::vec3(i * 0.05f, 0.2f + 2 * float(i / 20.0f), i * 0.025f));
			cube->transform().translate(glm::vec3(radius * std::cos(i*omega), 1.25f + i * 0.25f, radius * std::sin(i*omega)));
			cube->transform().scale() = glm::vec3(0.05715f);
			cube->m_rigidbody = new RigidBody(cube);

			SphereCollider * cubeCollider = new SphereCollider(cube, cube->m_rigidbody);
			cubeCollider->m_radius = 0.05715f;
			ss.clear();
			ss << "sphere" << i;
			cubeCollider->name = ss.str();
			CollisionDetector::instance()->addCollider(cubeCollider);

			m_components.push_back(cube);

			m_physics->addObject(cube->m_rigidbody);
		}
	}