Ejemplo n.º 1
0
CollisionSceneEntity::CollisionSceneEntity(SceneEntity *entity, int type, bool compoundChildren) {
	sceneEntity = entity;
	shape = NULL;
	
	this->type = type;
	enabled = true;	
	lastPosition = entity->getPosition();	
	
	
	btMatrix3x3 basisA;
	basisA.setIdentity();
	
	collisionObject = new btCollisionObject();
	collisionObject->getWorldTransform().setBasis(basisA);	
	
	if(compoundChildren) {
		 btCompoundShape* compoundShape = new btCompoundShape();
		 
		 for(int i=0; i < entity->getNumChildren(); i++) {
			SceneEntity *child = (SceneEntity*)entity->getChildAtIndex(i);
			btCollisionShape *childShape = createCollisionShape(child, child->collisionShapeType);
			btTransform transform;
			
			child->rebuildTransformMatrix();

			btScalar mat[16];		
			Matrix4 ent_mat = child->getTransformMatrix();
	
			for(int i=0; i < 16; i++) {
				mat[i] = ent_mat.ml[i];
			}			
			
			transform.setFromOpenGLMatrix(mat);
			compoundShape->addChildShape(transform, childShape);			
		 }	
		 
		 shape = compoundShape;
	} else {
		shape = createCollisionShape(entity, type);	
	}
	
	if(shape) {
		collisionObject->setCollisionShape(shape);
	}	
	
//	if(type == SHAPE_MESH) {		
//		concaveShape = dynamic_cast<btConcaveShape*>(shape);
//	} else {
		convexShape	= dynamic_cast<btConvexShape*>(shape);		
//	}		
}
Ejemplo n.º 2
0
void Scene::loadScene(const String& fileName) {
	OSFILE *inFile = OSBasics::open(fileName.c_str(), "rb");
	if(!inFile) {
		Logger::log("Error opening scene file\n");
		return;
	}
	
	Number r,g,b,a;
	
	OSBasics::read(&r, sizeof(Number), 1, inFile);
	OSBasics::read(&g, sizeof(Number), 1, inFile);
	OSBasics::read(&b, sizeof(Number), 1, inFile);
	clearColor.setColor(r,g,b,1.0f);
	
	OSBasics::read(&r, sizeof(Number), 1, inFile);
	OSBasics::read(&g, sizeof(Number), 1, inFile);
	OSBasics::read(&b, sizeof(Number), 1, inFile);
	ambientColor.setColor(r,g,b,1.0f);
	
	
	unsigned int numObjects, objectType,namelen;
	char buffer[1024];
	char flag;
	Number t[3],rq[4];
	SceneEntity *newEntity;
	
	unsigned int lightmapIndex = 0;
	
	OSBasics::read(&flag, 1, 1, inFile);
	hasLightmaps = false;
	if(flag == 1)
		hasLightmaps = true;
	
	OSBasics::read(&numObjects, sizeof(unsigned int), 1, inFile);
	Logger::log("Loading scene (%d objects)\n", numObjects);
	for(int i=0; i < numObjects; i++) {
		
		OSBasics::read(t, sizeof(Number), 3, inFile);
		OSBasics::read(rq, sizeof(Number), 4, inFile);
		newEntity = NULL;
		
		OSBasics::read(&objectType, sizeof(unsigned int), 1, inFile);
		
		SceneMesh *newSceneMesh;
		Mesh *newMesh;
		Material *newMaterial;
		
		switch(objectType) {
			case ENTITY_MESH:			
				
				if(hasLightmaps) {
					OSBasics::read(&lightmapIndex, sizeof(unsigned int), 1, inFile);			
				} else {
					lightmapIndex = 0;
				}
				
				
				OSBasics::read(&namelen, sizeof(unsigned int), 1, inFile);
				memset(buffer, 0, 1024);
				OSBasics::read(buffer, 1, namelen, inFile);
				
				Logger::log("adding mesh (texture: %s)\n", buffer);
				
				OSBasics::read(&r, sizeof(Number), 1, inFile);
				OSBasics::read(&g, sizeof(Number), 1, inFile);
				OSBasics::read(&b, sizeof(Number), 1, inFile);
				OSBasics::read(&a, sizeof(Number), 1, inFile);
				
				newMaterial = (Material*) CoreServices::getInstance()->getResourceManager()->getResource(Resource::RESOURCE_MATERIAL, buffer);
				newMesh = new Mesh(Mesh::TRI_MESH);
				newMesh->loadFromFile(inFile);
				newSceneMesh = new SceneMesh(newMesh);
				
				newSceneMesh->setColor(r,g,b,a);
				newSceneMesh->lightmapIndex = lightmapIndex;
				addEntity(newSceneMesh);
				newEntity = (SceneEntity*)newSceneMesh;
				staticGeometry.push_back(newSceneMesh);
				if(newMaterial != NULL)
					newSceneMesh->setMaterial(newMaterial);
				
				break;
			case ENTITY_ENTITY: {
				Logger::log("loading entity\n");
				String entityType = readString(inFile);					
				SceneEntity *newCustomEntity = new SceneEntity();
				newCustomEntity->custEntityType = entityType;					
				newEntity = newCustomEntity;
				customEntities.push_back(newCustomEntity);					
			} break;
			case ENTITY_COLLMESH: {
			/*
				unsigned int collType,numVertices,numFaces;
				Number co[3];
				OSBasics::read(&collType, sizeof(unsigned int), 1, inFile);
				OSBasics::read(&numVertices, sizeof(unsigned int), 1, inFile);
				
				Mesh *mesh = new Mesh(Mesh::TRI_MESH);
				
				for(int i=0; i < numVertices; i++) {
					OSBasics::read(co, sizeof(Number), 3, inFile);
					Vertex *newVert = new Vertex(co[0], co[1], co[2]);
					mesh->addVertex(newVert);
				}
				
				OSBasics::read(&numFaces, sizeof(unsigned int), 1, inFile);
				
				unsigned int fo[3];
				for(int i=0; i < numFaces; i++) {
					OSBasics::read(fo, sizeof(unsigned int), 3, inFile);						
					Polygon *newPoly = new Polygon();
					newPoly->addVertex(mesh->getVertex(fo[0]));
					newPoly->addVertex(mesh->getVertex(fo[1]));
					newPoly->addVertex(mesh->getVertex(fo[2]));
					mesh->addPolygon(newPoly);
				}
				
				SceneMesh *newMesh = new SceneMesh(mesh);
				newEntity = newMesh;
				loadCollisionChild(newEntity);
				collisionGeometry.push_back(newMesh);
				newMesh->visible = false;
				
				//					ScenePrimitive *test = new ScenePrimitive(ScenePrimitive::TYPE_BOX, newMesh->bBox.x,newMesh->bBox.y,newMesh->bBox.z);				
				//					newMesh->addEntity(test);
				*/
			}
			
				break;
			case ENTITY_CAMERA:
				newEntity = (SceneEntity*)this->getDefaultCamera();
				break;
			case ENTITY_LIGHT:
				/*
				Number col[3],e,d;
				unsigned int lType;
				OSBasics::read(&lType, sizeof(unsigned int), 1, inFile);				
				OSBasics::read(&e, sizeof(Number), 1, inFile);
				OSBasics::read(&d, sizeof(Number), 1, inFile);
				OSBasics::read(col, sizeof(Number), 3, inFile);
				
				SceneLight *newLight = new SceneLight(lType, e, d, this);
				newLight->lightColor.setColor(col[0],col[1],col[2],1.0f);
				addLight(newLight);
				newEntity = (SceneEntity*)newLight;
				*/
				break;
		}
		
		
		if(newEntity != NULL) {		
			unsigned int numProps;
			OSBasics::read(&numProps, sizeof(unsigned int), 1, inFile);
			for(int i=0; i < numProps; i++) {
				String propName,propValue;
				propName = readString(inFile);
				propValue = readString(inFile);
				EntityProp prop;
				prop.propName = propName;
				prop.propValue = propValue;
				newEntity->entityProps.push_back(prop);
			}						
			
			if(objectType == ENTITY_MESH) {
				if(newEntity->getEntityProp("vertexnormals") == "false")
					((SceneMesh*)newEntity)->getMesh()->useVertexNormals(false);
				
				if(newEntity->getEntityProp("collision") != "") {
					if(newEntity->getEntityProp("collision")  == "mesh") {
						loadCollisionChild(newEntity, false, 3);
					}
				}
			}
			
			newEntity->setPosition(t[0], t[1], t[2]);
			newEntity->setRotationQuat(rq[0], rq[1], rq[2], rq[3]);
			newEntity->rebuildTransformMatrix();						
		}		
	}
	
	if(!hasLightmaps) {
		OSBasics::close(inFile);
		return;
	}
	/*
	 unsigned int lmsize,numLightmaps,imageWidth, imageHeight;
	 OSBasics::read(&numLightmaps, sizeof(unsigned int), 1, inFile);
	 
	 packer = new LightmapPacker(this);
	 char *imageData;
	 
	 for(int i=0 ; i < numLightmaps; i++) {
	 OSBasics::read(&imageWidth, sizeof(unsigned int), 1, inFile);
	 OSBasics::read(&imageHeight, sizeof(unsigned int), 1, inFile);
	 OSBasics::read(&lmsize, sizeof(unsigned int), 1, inFile);
	 imageData = (char*)malloc(lmsize);
	 OSBasics::read(imageData, 1, lmsize, inFile);
	 Image *newImage = new Image(imageData, imageWidth, imageHeight);
	 packer->images.push_back(newImage);	
	 free(imageData);
	 }
	 
	 packer->bindTextures();
	 */
	OSBasics::close(inFile);
}