std::shared_ptr<RuntimeGameObject> RuntimeGameObject::Create(_In_ const std::shared_ptr<RuntimeGameWorld>& world, _In_ const std::map<std::wstring, std::wstring>& properties)
    {
        CHECK_NOT_NULL(g_gameObjectFactory);

        // Get details from the gameplay module
        GameObjectCreateParameters parameters = g_gameObjectFactory(world, properties);

        // Create base object with those parameters
        std::shared_ptr<RuntimeGameObject> object(GDKNEW RuntimeGameObject(world, parameters));

        // stash away these initial properties for later
        object->_initialProperties = properties;

        // do we have rigid body info? (requires a collision primitive)
        if (parameters.collisionPrimitive)
        {
            RigidBodyCreateParameters rigidBodyParams(parameters.position, 100.0f, parameters.collisionPrimitive.get());
            rigidBodyParams.type = PhysicsBodyTypeToRigidBodyType(parameters.physicsType);
            rigidBodyParams.gravityScale = (parameters.floating) ? 0.0f : 1.0f;
            object->_body = world->GetPhysicsWorld()->CreateBody(object, rigidBodyParams);
        }

        // if we're in the editor, let's create the picking mesh & sphere
        if (world->IsEditing())
        {
            std::vector<Triangle> triangles;
            Matrix identity = Matrix::Identity();

            auto& visuals = object->GetVisualInfos();
            if (visuals.size())
            {
                for (auto i = 0; i < visuals.size(); ++i)
                {
                    Collision::TriangleListFromGeometry(Content::LoadGeometryContent(visuals[i].geometry), identity, 0, triangles);
                }

                object->_triangleMesh = CollisionPrimitive::Create(TriangleMesh(SpacePartitionType::AabbTree, triangles));

                // loose sphere around the AABB. Not a best fit, but fast to compute and "good enough" as a pre-test
                Vector3 aabbMin, aabbMax;
                GetAabbForPrimitive(object->_triangleMesh.get(), &aabbMin, &aabbMax);
                object->_sphere = CollisionPrimitive::Create(Sphere((aabbMin + aabbMax) * 0.5f, (aabbMax - aabbMin).Length() * 0.5f));
            }
        }

        // Bind the object and controller, if one was provided
        if (parameters.controller)
        {
            parameters.controller->OnCreate(object);
        }

        return object;
    }
Beispiel #2
0
    GameObjectCreateParameters Door::Create(_In_ const std::shared_ptr<IGameWorld>& gameWorld, _In_ const std::shared_ptr<QuakeProperties>& quakeProps)
    {
        UNREFERENCED_PARAMETER(gameWorld);

        GameObjectCreateParameters params;
        params.className = L"func_door";
        params.targetName = std::wstring(L"*") + std::to_wstring(quakeProps->GetModel());
        params.visuals = gameWorld->GetModel(quakeProps->GetModel());
        params.physicsType = PhysicsBodyType::Kinematic;

        params.position = quakeProps->GetOrigin();
        params.rotation = 0.0f;
        auto angle = quakeProps->GetAngle();

        Vector3 openPosition;
        if (fabsf(angle - Math::ToRadians(-1.0f)) < Math::Epsilon)
        {
            openPosition = Vector3(0, 100, 0);
        }
        else if (fabsf(angle - Math::ToRadians(-2.0f)) < Math::Epsilon)
        {
            openPosition = Vector3(0, -100, 0);
        }
        else
        {
            Vector3 dir = Vector3(0, 0, 1);
            dir = Matrix::TransformNormal(dir, Matrix::CreateRotationY(angle));
            openPosition = dir * 100;
        }

        auto door = std::shared_ptr<Door>(GDKNEW Door(openPosition));
        door->_angleToStore = angle;
        params.controller = door;

        std::vector<Triangle> triangles;
        for (uint32_t i = 0; i < params.visuals.size(); ++i)
        {
            Collision::TriangleListFromGeometry(Content::LoadGeometryContent(params.visuals[i].geometry), Matrix::Identity(), 0, triangles);
        }
        params.collisionPrimitive = CollisionPrimitive::Create(TriangleMesh(SpacePartitionType::AabbTree, triangles));

        // Add a trigger to open us
        std::map<std::wstring, std::wstring> triggerProps;
        triggerProps[L"classname"] = L"trigger_relay"; // switched from trigger_once to trigger_relay until more triggers are defined.
        triggerProps[L"target"] = params.targetName;
        triggerProps[L"model"] = std::wstring(L"*") + std::to_wstring(quakeProps->GetModel());
        triggerProps[L"dontsave"] = L"true";
        gameWorld->CreateObject(triggerProps);

        return params;
    }
StridingMeshInterface^ StridingMeshInterface::GetManaged(btStridingMeshInterface* stridingMesh)
{
	if (stridingMesh == nullptr)
		return nullptr;

	btTriangleMesh* triangleMesh = static_cast<btTriangleMesh*>(stridingMesh);
	if (triangleMesh != 0)
		return gcnew TriangleMesh(triangleMesh);

#ifndef DISABLE_UNCOMMON
	btTriangleIndexVertexMaterialArray* triangleIndexVertexMaterialArray = static_cast<btTriangleIndexVertexMaterialArray*>(stridingMesh);
	if (triangleIndexVertexMaterialArray != 0)
		return gcnew TriangleIndexVertexMaterialArray(triangleIndexVertexMaterialArray);
#endif

	btTriangleIndexVertexArray* triangleIndexVertexArray = static_cast<btTriangleIndexVertexArray*>(stridingMesh);
	if (triangleIndexVertexArray != 0)
		return gcnew TriangleIndexVertexArray(triangleIndexVertexArray);

	throw gcnew NotImplementedException();
}
        std::shared_ptr<CollisionPrimitive> TriangleMeshFromGeometry(_In_ const std::shared_ptr<GeometryContent>& content, _In_ uint32_t frame)
        {
            uint32_t numIndices = content->GetNumIndicesPerFrame();

            if (numIndices == 0)
                return nullptr;

            uint32_t stride = content->GetVertexStride();
            const uint32_t* indices = content->GetIndices() + numIndices * frame;
            const byte_t* positions = content->GetAttributeData(GeometryContent::AttributeType::Float3, GeometryContent::AttributeName::Position, 0);

            Vector3 a, b, c;
            std::vector<Triangle> triangles;

            for (uint32_t i = 0; i < numIndices; i+=3)
            {
                a = *reinterpret_cast<const Vector3*>(positions + stride * indices[i]);
                b = *reinterpret_cast<const Vector3*>(positions + stride * indices[i+1]);
                c = *reinterpret_cast<const Vector3*>(positions + stride * indices[i+2]);
                triangles.push_back(Triangle(a, b, c));
            }

            return CollisionPrimitive::Create(TriangleMesh(SpacePartitionType::AabbTree, triangles));
        }
Beispiel #5
0
//Creates a TriangleMesh instance from provided xml data
TriangleMesh Visitor::loadMeshFromXml(pugi::xml_node& resource, Node& container)
{
	bool static_mesh = false;
	Shader mesh_shader;
	Texture mesh_texture;
	pugi::xml_node scenario_texture = resource.child("Texture");

	if (resource.attribute("StaticObject"))
	{
		static_mesh = true;
	}

	if (resource.child("Texture") || resource.child("Texture3D") || resource.child("FrameBufferTexture"))
		for (pugi::xml_node_iterator textures = resource.begin(); textures != resource.end(); ++textures)
		{
			//mesh_texture = loadTextureFromXml(*textures);
			mesh_texture = loadTextureFromXml(resource.child("Texture"));
			//if (mesh_texture != nullptr)
			break;
		}
	else
		mesh_texture = scene_objects.getTextureByKey(resource.attribute("Texture").as_string());

	std::string fs_path = resource.attribute("FSPath").as_string();
	std::string vs_path = resource.attribute("VSPath").as_string();
	mesh_shader = scene_objects.getShaderByKey(vs_path.append(fs_path));

	std::string obj_path = resource.attribute("ObjFile").as_string();
	//mesh_buffers = scene_objects.getBufferByKey(obj_path);
	pugi::xml_attribute instance = resource.attribute("InstanceCount");

	vec3 position;
	vec3 velocity;

	//Now gather all information for uniforms to the shaders
	for (pugi::xml_node_iterator uniforms = resource.begin(); uniforms != resource.end(); ++uniforms)
	{
		if (std::strcmp(uniforms->name(), "UniformBlock") == 0)
		{
			UniformBlock to_add = synthesizeBlock(*uniforms);
			mesh_shader.addUniformBlock(to_add);
		}
		if (std::strcmp(uniforms->name(), "Uniform") == 0)
		{
			ShaderParameter uniform = parseUniform(*uniforms);
			if (strcmp(uniform.name.c_str(), "velocity") == 0)
			{
				velocity.x = uniform.client_data[0];
				velocity.y = uniform.client_data[1];
				velocity.z = uniform.client_data[2];
			}
			else
				mesh_shader.addUniform(uniform);

			if (strcmp(uniform.name.c_str(), "translation") == 0)
			{
				position.x = uniform.client_data[0];
				position.y = uniform.client_data[1];
				position.z = uniform.client_data[2];
			}
		}
	}


	//Now loop over all nodes to determine which blocks the shader should bind to 
	for (pugi::xml_node_iterator uniforms = resource.begin(); uniforms != resource.end(); ++uniforms)
	{
		if (std::strcmp(uniforms->name(), "ShaderUniforms") == 0)
		{
			std::string block_name = uniforms->attribute("Name").as_string();
			int block_index = uniforms->attribute("BindingPoint").as_int();
		}
	}

	//Lava.png
	//dry_clay.jpg
	//Redbrick.jpg
	//rock04.jpg
	//Rosewood.jpg

	//Checkered Marble.png
	//Ocean Texture.png
	//Stonewall.jpg
	//Ambient Green.jpg
	//Red Pattern.jpg
	//Green Wall.jpg
	//Red Fuzzy Pattern.jpg
	//Brick Circle Pattern.jpg
	ObjFileReader* mesh_reader = scene_objects.getFileReader(obj_path);
	int num_buffers = mesh_reader->getNumSubMeshes();
	Texture second = scene_objects.getTextureByKey("Lava.png");
	Texture third = scene_objects.getTextureByKey("Redbrick.jpg");
	Texture fourth = scene_objects.getTextureByKey("Rosewood.jpg");
	Texture fifth = scene_objects.getTextureByKey("Checkered Marble.png");

	Texture sixth = scene_objects.getTextureByKey("Lava.png");
	Texture seventh = scene_objects.getTextureByKey("Ocean Texture.png");
	Texture eighth= scene_objects.getTextureByKey("Stonewall.jpg");
	Texture ninth = scene_objects.getTextureByKey("Ambient Green.jpg");
	Texture tenth = scene_objects.getTextureByKey("Red Pattern.jpg");
	Texture eleventh = scene_objects.getTextureByKey("Brick Circle Pattern.jpg");
	Texture twelfth = scene_objects.getTextureByKey("Green Wall.jpg");
	Texture thirteenth = scene_objects.getTextureByKey("Brick Circle Pattern.jpg");


	for (int i = 0; i < num_buffers; i++)
	{
		BufferManager mesh_buffers;
		TriangleMesh to_return;

		mesh_buffers = mesh_reader->getPrimitive(i);

		//if (instance)
		//	to_return = TriangleMesh(instance.as_int());
		//else
		if (i % 13 == 0)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, mesh_texture, static_mesh);
		else if (i % 13 == 1)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, second, static_mesh);
		else if (i % 13 == 2)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, third, static_mesh);
		else if (i % 13 == 3)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, fourth, static_mesh);
		else if (i % 13 == 4)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, fifth, static_mesh);
		else if (i % 13 == 5)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, sixth, static_mesh);
		else if (i % 13 == 6)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, seventh, static_mesh);
		else if (i % 13 == 7)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, eighth, static_mesh);
		else if (i % 13 == 8)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, ninth, static_mesh);
		else if (i % 13 == 9)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, tenth, static_mesh);
		else if (i % 13 == 10)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, eleventh, static_mesh);
		else if (i % 13 == 11)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, twelfth, static_mesh);
		else if (i % 13 == 12)
			to_return = TriangleMesh(mesh_shader, mesh_buffers, thirteenth, static_mesh);


		//to_return.setPosition(position);
		to_return.setVelocity(velocity);

		container.meshes.push_back(to_return);
	}
	TriangleMesh dummy;
	return dummy;
}
void XmlVisitor::visitCompositeGrid(CompositeGrid* tiles)
{
	if (tiles->is_leaf)
	{
		std::string current = file_handler.getNextPath();
		pugi::xml_parse_result res = doc.load_file(current.c_str()); 
		if (!res)
			std::cout << "Errors in XML file\n" << current.c_str() << "\n";
		for (pugi::xml_node_iterator current = doc.root().begin(); current != doc.root().end(); ++current)
		{
			for (pugi::xml_node_iterator resource = current->begin(); resource != current->end(); ++resource)
			{
				if (std::strcmp(resource->name(), "TriangleMesh") == 0)
				{

					std::string fs_path = resource->attribute("FSPath").as_string();
					std::string vs_path = resource->attribute("VSPath").as_string();
					std::string obj_path = resource->attribute("ObjFile").as_string();
					std::string tex_path = resource->attribute("Texture").as_string();
					pugi::xml_attribute test = resource->attribute("UniformBlock");
					std::string uniform_block = resource->attribute("UniformBlock").as_string();

					ObjectState scenario_state = scene_objects.synthesizeObject(vs_path.append(fs_path), obj_path, tex_path);
					UniformBlock to_add = UniformBlock(uniform_block, 0);
					float* model_view = new float[16];
					float* projection = new float[16];
					int k = 0;
					float l = 4.0 * (float) tiles->left / 512.0 - 4.0;
					float r = 4.0 * (float) tiles->right / 512.0 - 4.0;
					float b = 4.0 * (float) tiles->bottom / 512.0 - 4.0;
					float t = 4.0 * (float) tiles->top / 512.0 - 4.0;

					model_view[0] = 0.01;
					model_view[1] = 0;
					model_view[2] = 0;
					model_view[3] = 0;

					model_view[4] = 0;
					model_view[5] = 0.01;
					model_view[6] = 0;
					model_view[7] = 0;

					model_view[8] = 0;
					model_view[9] = 0;
					model_view[10] = 0.01;
					model_view[11] = 0;

					model_view[12] = (l + r) / 2;
					model_view[13] = (b + t) / 2;
					model_view[14] = -3.0;
					model_view[15] = 1;

					projection[0] = 1;
					projection[1] = 0;
					projection[2] = 0;
					projection[3] = 0;

					projection[4] = 0;
					projection[5] = 1;
					projection[6] = 0;
					projection[7] = 0;

					projection[8] = 0;
					projection[9] = 0;
					projection[10] = -11.0 / 9.0;
					projection[11] = -1;

					projection[12] = 0;
					projection[13] = 0;
					projection[14] = -20.0 / 9.0;
					projection[15] = 0;

					ShaderParameter param = ShaderParameter("ModelView", 16 * 4, 16, model_view, true);
					ShaderParameter param2 = ShaderParameter("Projection", 16 * 4, 16, projection, true);
					to_add.addParameter(param);
					to_add.addParameter(param2);
					scenario_state.addUniformBlock(to_add);
					tiles->meshes.push_back( TriangleMesh(scenario_state) );
				}
			}
		}
	}

	if (!tiles->is_leaf)
		for (CompositeGrid* child: tiles->children)
			child->acceptVisitor(this);
}