Пример #1
0
ExtMesh *Scene::CreateInlinedMesh(const string &shapeName, const string &propName, const Properties &props) {
	// The mesh definition is in-lined
	u_int pointsSize;
	Point *points;
	if (props.IsDefined(propName + ".vertices")) {
		Property prop = props.Get(propName + ".vertices");
		if ((prop.GetSize() == 0) || (prop.GetSize() % 3 != 0))
			throw runtime_error("Wrong shape vertex list length: " + shapeName);

		pointsSize = prop.GetSize() / 3;
		points = TriangleMesh::AllocVerticesBuffer(pointsSize);
		for (u_int i = 0; i < pointsSize; ++i) {
			const u_int index = i * 3;
			points[i] = Point(prop.Get<float>(index), prop.Get<float>(index + 1), prop.Get<float>(index + 2));
		}
	} else
		throw runtime_error("Missing shape vertex list: " + shapeName);

	u_int trisSize;
	Triangle *tris;
	if (props.IsDefined(propName + ".faces")) {
		Property prop = props.Get(propName + ".faces");
		if ((prop.GetSize() == 0) || (prop.GetSize() % 3 != 0))
			throw runtime_error("Wrong shape face list length: " + shapeName);

		trisSize = prop.GetSize() / 3;
		tris = TriangleMesh::AllocTrianglesBuffer(trisSize);
		for (u_int i = 0; i < trisSize; ++i) {
			const u_int index = i * 3;
			tris[i] = Triangle(prop.Get<u_int>(index), prop.Get<u_int>(index + 1), prop.Get<u_int>(index + 2));
		}
	} else {
		delete[] points;
		throw runtime_error("Missing shape face list: " + shapeName);
	}

	Normal *normals = NULL;
	if (props.IsDefined(propName + ".normals")) {
		Property prop = props.Get(propName + ".normals");
		if ((prop.GetSize() == 0) || (prop.GetSize() / 3 != pointsSize))
			throw runtime_error("Wrong shape normal list length: " + shapeName);

		normals = new Normal[pointsSize];
		for (u_int i = 0; i < pointsSize; ++i) {
			const u_int index = i * 3;
			normals[i] = Normal(prop.Get<float>(index), prop.Get<float>(index + 1), prop.Get<float>(index + 2));
		}
	}

	UV *uvs = NULL;
	if (props.IsDefined(propName + ".uvs")) {
		Property prop = props.Get(propName + ".uvs");
		if ((prop.GetSize() == 0) || (prop.GetSize() / 2 != pointsSize))
			throw runtime_error("Wrong shape uv list length: " + shapeName);

		uvs = new UV[pointsSize];
		for (u_int i = 0; i < pointsSize; ++i) {
			const u_int index = i * 2;
			uvs[i] = UV(prop.Get<float>(index), prop.Get<float>(index + 1));
		}
	}

	return new ExtTriangleMesh(pointsSize, trisSize, points, tris, normals, uvs);
}