Esempio n. 1
0
Geometry* SceneLoader::LoadGeometry(QDomElement element)
{
  Geometry* geom = GeometryPool::Get()->GetGeometry(_geometryName, _geometryIdx);
  if (geom)
    {
      return geom;
    }

  if (element.nodeName() != QString ("geometry"))
    {
      printf ("ceXMLDataLoader::LoadGeometry: Illegal data format: '%s' != 'geometry'\n", element.nodeName().toStdString().c_str()); fflush (stdout);
      return 0;
    }

  geom = new Geometry ();

  QDomElement staticElement = element.firstChildElement("staticmesh");
  if (!staticElement.isNull())
    {
      ceMesh* mesh = LoadMesh (staticElement);
      if (!mesh)
        {
          geom->Release();
          return 0;
        }
      iStaticMesh* staticMesh = _engine->GetDevice()->CreateStaticMesh();
      staticMesh->SetMesh(mesh);
      geom->SetMesh(staticMesh);
      geom->SetMetaMesh(MT_Static,
      staticElement.firstChildElement("mesh").attribute("filename"));
    }

  MaterialManager* matMgr = Session::Get()->GetMaterialManager();

  QDomElement materialElement = element.firstChildElement("material");
  Material* mat = 0;
  if (!materialElement.isNull())
    {
      mat = matMgr->GetMaterial(materialElement.text());
    }

  if (!mat)
    {
      mat = matMgr->GetDefaultMaterial();
    }
  geom->SetMetaMaterial(mat);

  GeometryPool::Get()->Set(_geometryName, _geometryIdx, geom);
  return geom;

}
Esempio n. 2
0
Geometry* ObjectMan::LoadObject(string id,
	string name,
	bool collidable,
	string parent_id)
{
	objLoader* loader = new objLoader();
	string filename = MODELSPATH + name + GEOEXT;
	
	cout << "\t" << filename << "...";
	
	char* file = (char*)(filename).c_str();
	loader->load(file);

	obj_vector** verts = loader->vertexList;
	int num_v = loader->vertexCount;
	vector<Vec4> vertices;

	obj_vector** norms = loader->normalList;
	int num_n = loader->normalCount;
	vector<Vec4> normals;

	obj_vector** uvs = loader->textureList;
	int num_u = loader->textureCount;
	vector<Vec4> uv_points;

	obj_face** face_list = loader->faceList;
	int num_f = loader->faceCount;
	vector<Face> faces;

	int* index_of_faces = new int[num_f * 3];

	btScalar* index_of_verts = new btScalar[num_v * 3];

	for (int i = 0; i < num_v; i++)
	{
		obj_vector* cur = verts[i];
		Vec4 point((float)cur->e[0], (float)cur->e[1], (float)cur->e[2]);
		vertices.push_back(point);
		index_of_verts[i * 3] = btScalar((float)cur->e[0]);
		index_of_verts[i * 3 + 1] = btScalar((float)cur->e[1]);
		index_of_verts[i * 3 + 2] = btScalar((float)cur->e[2]);
	}

	for (int j = 0; j < num_n; j++)
	{
		obj_vector* cur = norms[j];
		Vec4 norm((float)cur->e[0], (float)cur->e[1], (float)cur->e[2]);
		normals.push_back(norm);
	}

	for (int k = 0; k < num_u; k++)
	{
		obj_vector* cur = uvs[k];
		Vec4 uv_point((float)cur->e[0], (float)cur->e[1], 0);
		uv_points.push_back(uv_point);
		//if (id == "car") {
		//	printf("UV: %f\t%f\n", uv_point[0], uv_point[1]);
		//}
	}

	for (int m = 0; m < num_f; m++)
	{
		obj_face* cur = face_list[m];
		Face new_face;

		for (int n = 0; n < cur->vertex_count; n++)
		{
			index_of_faces[(m * 3) + n] = cur->vertex_index[n];
			new_face.vertices.push_back(vertices[cur->vertex_index[n]]);
			new_face.normals.push_back(normals[cur->normal_index[n]]);
			new_face.uvs.push_back(uv_points[cur->texture_index[n]]);
		}

		faces.push_back(new_face);
	}

	btTriangleIndexVertexArray* mesh = new btTriangleIndexVertexArray(num_f,
		index_of_faces,
		3 * sizeof(int),
		num_v,
		index_of_verts,
		3 * sizeof(btScalar));

	Geometry* p = NULL;
	map<string, Geometry*>::const_iterator parentFound = objectMap.find(parent_id);
	if (parentFound != objectMap.end()) {
		p = parentFound->second;
	}

	Geometry* geo = new Geometry(id, vertices, normals, uv_points, faces, p);
	geo->SetMesh(mesh);
	objectMap.insert(make_pair(id, geo));
	objects.push_back(geo);

	if (collidable)
	{
		collidables.push_back(geo);
	}

	// Is there a reason the loader isn't deleted? I tried uncommenting this, and nothing appeared to die a violent death.
	//  -Tyler
	//delete loader;

	cout << "DONE." << endl;

	return geo;
}