예제 #1
0
파일: OctTree.c 프로젝트: AylwynTaras/NGen
///
//adds a game object to a node of the oct tree and logs all nodes in which it is contained.
//
//Parameters:
//	tree: A pointer to the oct tree the object is being added to
//	node: A pointer to the node the object is being added to
//	obj: A pointer to the game object being added to the tree
void OctTree_Node_AddAndLog(OctTree* tree, struct OctTree_Node* node, GObject* obj)
{
	//If this node has children, determine which children the object collides with
	if(node->children != NULL)
	{
		for(int i = 0; i < 8; i++)
		{
			unsigned char collisionStatus = OctTree_Node_DoesObjectCollide(node->children + i, obj);
			//If the object is fully contained in this node
			if(collisionStatus == 2)
			{
				//Add the nobject to this node & stop looping
				OctTree_Node_AddAndLog(tree, node->children + i, obj);
				break;
			}
			//Else if the object is partially contained in this node
			else  if(collisionStatus == 1)
			{
				//Add the object to this node & keep looping
				OctTree_Node_AddAndLog(tree, node->children + i, obj);
			}
		}
	}
	//If this node has no children
	else
	{
		//Can we hold another object? or are we too deep to subdivide?
		if(node->data->size <= tree->maxOccupancy || node->depth >= tree->maxDepth)
		{
			//Make sure we aren't already holding a pointer to the object...
			if(DynamicArray_ContainsWithin(node->data, &obj, node->data->size) == 0)
			{
				//Add the object!
				DynamicArray_Append(node->data, &obj);

				//Find the entry for this object in the treemap
				DynamicArray* log = NULL;

				//Is this object already contained in the map?
				if(HashMap_Contains(tree->map, &obj, sizeof(GObject*)) == 1)
				{
					log = (DynamicArray*)HashMap_LookUp(tree->map, &obj, sizeof(GObject*))->data;
				}
				else
				{
					//Add this object to the tree map
					log = DynamicArray_Allocate();
					DynamicArray_Initialize(log, sizeof(struct OctTree_NodeStatus));

					HashMap_Add(tree->map, &obj, log, sizeof(GObject*));
				}

				//Add this node to the log of the object
				//Get the containment status!
				unsigned char status = OctTree_Node_DoesObjectCollide(node, obj);
				//create a Nodestatus struct
				struct OctTree_NodeStatus entry;
				entry.node = node;
				entry.collisionStatus = status;

				//Log the entry!
				DynamicArray_Append(log, &entry);
			}
			//Else, it is already contained
			else
			{
				//Check if the status of containment in this node has changed
				//Get the object's log
				DynamicArray* log = (DynamicArray*)HashMap_LookUp(tree->map, &obj, sizeof(GObject*))->data;
				//Find the entry of this node in the log
				struct OctTree_NodeStatus* entry = NULL;
				for(unsigned int i = 0; i < log->size; i++)
				{
					struct OctTree_NodeStatus* mightBeTheEntry = (struct OctTree_NodeStatus*)DynamicArray_Index(log, i);
					//If we find it
					if(mightBeTheEntry->node == node)
					{
						entry = mightBeTheEntry;
						break;		
					}
				}

				//If this object is logged as being in this node
				if(entry != NULL)
				{

					//Compare the status of the entry to the current status
					unsigned char status = OctTree_Node_DoesObjectCollide(node, obj);
					if(entry->collisionStatus !=  status)
					{
						//Update the status
						entry->collisionStatus = status;
					}
				}
				//This object is not logged as being in this node
				else
				{
					//Create an entry
					//Get the containment status!
					unsigned char status = OctTree_Node_DoesObjectCollide(node, obj);
					//create a Nodestatus struct
					struct OctTree_NodeStatus entry;
					entry.node = node;
					entry.collisionStatus = status;

					//Log the entry!
					DynamicArray_Append(log, &entry);
				}
			}
		}
		//Else, we are out of room and can subdivide!
		else
		{
			OctTree_Node_SubdivideAndLog(tree, node);
			//Finally, recall this function to add the object to one of the children
			OctTree_Node_AddAndLog(tree, node, obj);
		}
	}
}
예제 #2
0
///
//Loads all of the engines assets into the internal asset buffer
void AssetManager_LoadAssets(void)
{
	//Load meshes
	HashMap_Add(assetBuffer->meshMap, "Cube", Loader_LoadOBJFile("./Assets/Models/cube.obj"), strlen("Cube"));
	//HashMap_Add(assetBuffer->meshMap, "Cube", Generator_GenerateCubeMesh(2.0f));
	HashMap_Add(assetBuffer->meshMap, "Sphere", Loader_LoadOBJFile("./Assets/Models/sphere.obj"), strlen("Sphere"));
	//HashMap_Add(assetBuffer->meshMap, "Sphere", Generator_GenerateSphereMesh(2.0f, 25));
	HashMap_Add(assetBuffer->meshMap, "Cylinder", Loader_LoadOBJFile("./Assets/Models/cylinder.obj"), strlen("Cylinder"));
	//HashMap_Add(assetBuffer->meshMap, "Cylinder", Generator_GenerateCylinderMesh(1.0f, 2.0f, 25));
	HashMap_Add(assetBuffer->meshMap, "Cone", Loader_LoadOBJFile("./Assets/Models/cone.obj"), strlen("Cone"));
	//HashMap_Add(assetBuffer->meshMap, "Cone", Generator_GenerateConeMesh(1.0f, 2.0f, 25));
	HashMap_Add(assetBuffer->meshMap, "Pipe", Loader_LoadOBJFile("./Assets/Models/pipe.obj"), strlen("Pipe"));
	//HashMap_Add(assetBuffer->meshMap, "Pipe", Generator_GenerateTubeMesh(1.0f, 0.5f, 2.0f, 25));
	HashMap_Add(assetBuffer->meshMap, "Torus", Loader_LoadOBJFile("./Assets/Models/torus.obj"), strlen("Torus"));
	//HashMap_Add(assetBuffer->meshMap, "Torus", Generator_GenerateTorusMesh(2.0f, 1.0f, 10));
	HashMap_Add(assetBuffer->meshMap, "CubeWire", Loader_LoadOBJFile("./Assets/Models/cubewire.obj"), strlen("CubeWire"));


	HashMap_Add(assetBuffer->meshMap, "Suzanne", Loader_LoadOBJFile("./Assets/Models/suzanne.obj"), strlen("Suzanne"));
	HashMap_Add(assetBuffer->meshMap, "Tetrahedron", Loader_LoadOBJFile("./Assets/Models/tetrahedron.obj"),strlen("Tetrahedron"));
	HashMap_Add(assetBuffer->meshMap, "Trash Can", Loader_LoadOBJFile("./Assets/Models/trashcan.obj"), strlen("Trash Can"));
	HashMap_Add(assetBuffer->meshMap, "Bottle", Loader_LoadOBJFile("./Assets/Models/bottle.obj"), strlen("Bottle"));
	HashMap_Add(assetBuffer->meshMap, "Target", Loader_LoadOBJFile("./Assets/Models/target.obj"), strlen("Target"));
	HashMap_Add(assetBuffer->meshMap, "Arrow", Loader_LoadOBJFile("./Assets/Models/arrow.obj"), strlen("Arrow"));

	
	

	
	


	//Load textures
	struct Image* i = Loader_Load24BitBMPFile("./Assets/Textures/test.bmp");
	Texture* t = Texture_Allocate();
	Texture_Initialize(t, i);
	HashMap_Add(assetBuffer->textureMap, "Test", t, strlen("Test"));
	
	i = Loader_Load24BitBMPFile("./Assets/Textures/earth.bmp");
	t = Texture_Allocate();
	Texture_Initialize(t, i);
	HashMap_Add(assetBuffer->textureMap, "Earth", t, strlen("Earth"));

	i = Loader_Load24BitBMPFile("./Assets/Textures/white.bmp");
	t = Texture_Allocate();
	Texture_Initialize(t, i);
	HashMap_Add(assetBuffer->textureMap, "White", t, strlen("White"));

	i = Loader_Load24BitBMPFile("./Assets/Textures/trash.bmp");
	t = Texture_Allocate();
	Texture_Initialize(t, i);
	HashMap_Add(assetBuffer->textureMap, "Trash Can", t, strlen("Trash Can"));

	i = Loader_Load24BitBMPFile("./Assets/Textures/arrow.bmp");
	t = Texture_Allocate();
	Texture_Initialize(t, i);
	HashMap_Add(assetBuffer->textureMap, "Arrow", t, strlen("Arrow"));

	i = Loader_Load24BitBMPFile("./Assets/Textures/bottle.bmp");
	t = Texture_Allocate();
	Texture_Initialize(t, i);
	HashMap_Add(assetBuffer->textureMap, "Bottle", t, strlen("Bottle"));

	i = Loader_Load24BitBMPFile("./Assets/Textures/target.bmp");
	t = Texture_Allocate();
	Texture_Initialize(t, i);
	HashMap_Add(assetBuffer->textureMap, "Target", t, strlen("Target"));

	i = Loader_Load24BitBMPFile("./Assets/Textures/concrete.bmp");
	t = Texture_Allocate();
	Texture_Initialize(t, i);
	HashMap_Add(assetBuffer->textureMap, "Floor", t, strlen("Floor"));

	i = Loader_Load24BitBMPFile("./Assets/Textures/wall2.bmp");
	t = Texture_Allocate();
	Texture_Initialize(t, i);
	HashMap_Add(assetBuffer->textureMap, "Wall", t, strlen("Wall"));

	i = Loader_Load24BitBMPFile("./Assets/Textures/wood.bmp");
	t = Texture_Allocate();
	Texture_Initialize(t, i);
	HashMap_Add(assetBuffer->textureMap, "Table", t, strlen("Table"));
}