Example #1
0
		private void BuildModelAsNode(ref SourceModel sourceModel)
		{
			var streamMesh2 = new SeparateStreamsMesh();
			var meshBuilder2 = new MeshBuilder(streamMesh2, this);
			var beginModelFace = sourceModel.firstface;
			var endModelFace = beginModelFace + sourceModel.numfaces;
			for (int index = beginModelFace; index < endModelFace; ++index)
			{
				//if (!usedFaces[index])
				{
					int meshIndex;
					SeparateStreamsSubmesh subMesh2 = meshBuilder2.EnsureSubMesh(
						new BspSubmeshKey(0, new BspMaterialKey(0, 0)), out meshIndex);
					this.BuildFace(ref this.faces[index], new BspSubmeshStreams(subMesh2,meshStreams,StreamConverterFactory));
					//Trace.WriteLine(string.Format("Face {0} is not references from leaves", index));
				}
			}
			this.Scene.Geometries.Add(streamMesh2);
		}
Example #2
0
		protected override void BuildScene()
		{
			this.CollectLeafsInCluster();
			//BuildVisibilityList();

			var node = new Node();
			this.Scene.Nodes.Add(node);

			if (this.buildBsp)
			{
				this.Scene.Geometries.Add(streamMesh);
				var meshBuilder = new MeshBuilder(streamMesh, this);
				node.Mesh = streamMesh;

				var vsdNodes = new BspVsdTreeNode[this.nodes.Length];
				for (int index = 0; index < this.nodes.Length; index++)
				{
					var sourceNode = this.nodes[index];
					vsdNodes[index] = new BspVsdTreeNode
						{
							Min = new Float3(sourceNode.box.boxMinX, sourceNode.box.boxMinY, sourceNode.box.boxMinZ),
							Max = new Float3(sourceNode.box.boxMaxX, sourceNode.box.boxMaxY, sourceNode.box.boxMaxZ),
							PositiveNodeIndex = sourceNode.front,
							NegativeNodeIndex = sourceNode.back,
							N = this.planes[sourceNode.planenum].normal,
							D = this.planes[sourceNode.planenum].dist,
						};
					if (sourceNode.face_num > 0)
					{
						//TODO: put this faces into geometry
						sourceNode.face_num = sourceNode.face_num;
					}
				}

				List<int> visibleClustersLookup = new List<int>();
				List<int> visibleMeshesLookup = new List<int>();
				var vsdLeaves = new BspVsdTreeLeaf[this.leaves.Length];
				var vsdClusters = new BspVsdTreeCluster[this.clusters.Length];
				//int nodesMeshId;
				//var nodesMesh = meshBuilder.EnsureSubMesh(new BspSubmeshKey(-1, new BspMaterialKey(0, 0)), out nodesMeshId);
				//for (int index = 0; index < this.nodes.Length; index++)
				//{
				//    for (int j = nodes[index].face_id; j < nodes[index].face_id + nodes[index].face_num;++j )
				//        this.BuildFace(ref this.faces[j], nodesMesh, streamMesh);
				//}
				for (int index = 0; index < this.leaves.Length; index++)
				{
					var sourceLeaf = this.leaves[index];
					vsdLeaves[index] = new BspVsdTreeLeaf
						{
							Min = new Float3(sourceLeaf.box.boxMinX, sourceLeaf.box.boxMinY, sourceLeaf.box.boxMinZ),
							Max = new Float3(sourceLeaf.box.boxMaxX, sourceLeaf.box.boxMaxY, sourceLeaf.box.boxMaxZ),
							Cluster = sourceLeaf.cluster,
						};
				}
				for (int index = 0; index < this.clusters.Length; index++)
				{
					Dictionary<int, bool> uniqueSubmeshes = new Dictionary<int, bool>();
					var sourceCluster = this.clusters[index];
					vsdClusters[index] = new BspVsdTreeCluster
						{
							VisibleClustersCount = sourceCluster.visiblity.Count,
							VisibleClustersOffset = visibleClustersLookup.Count,
							VisibleMeshesOffset = visibleMeshesLookup.Count,
						};

					visibleClustersLookup.AddRange(sourceCluster.visiblity);

					Dictionary<int, bool> uniqueFaces = new Dictionary<int, bool>();

					foreach (var leafIndex in sourceCluster.leaves)
					{
						var sourceLeaf = this.leaves[leafIndex];
						var faceBegin = sourceLeaf.firstleafface;
						var faceEnd = faceBegin + sourceLeaf.numleaffaces;
						for (int f = faceBegin; f < faceEnd; ++f)
						{
							uniqueFaces[this.leafFaces[f]] = true;
						}
					}
					foreach (var uniqueFace in uniqueFaces)
					{
						int texIndex = 0; // this.texInfo[this.faces[faceIndex].texinfo].texdata;
						int lightmapIndex = 0; //this.faces[faceIndex].lightmap;

						int meshIndex;
						SeparateStreamsSubmesh subMesh =
							meshBuilder.EnsureSubMesh(new BspSubmeshKey(index, new BspMaterialKey(texIndex, lightmapIndex)), out meshIndex);
						if (!uniqueSubmeshes.ContainsKey(meshIndex))
						{
							visibleMeshesLookup.Add(meshIndex);
							uniqueSubmeshes[meshIndex] = true;
						}
						BspSubmeshStreams submeshStreams= new BspSubmeshStreams(subMesh, meshStreams,StreamConverterFactory);
						this.BuildFace(ref this.faces[uniqueFace.Key], submeshStreams);
					}
					vsdClusters[index].VisibleMeshesCount = visibleMeshesLookup.Count - vsdClusters[index].VisibleMeshesOffset;
				}

				//for (int index = 0; index < usedFaces.Length; index++)
				//{
				//    if (!usedFaces[index])
				//    {
				//        for (int i = 0; i < this.models.Length; i++)
				//        {
				//            if (this.models[i].firstface <= index && this.models[i].firstface+this.models[i].numfaces > index)
				//            {
				//                Trace.WriteLine(string.Format("Lost face {0} belongs to model {1}", index, i));
				//                break;
				//            }
				//        }
				//        for (int i = 0; i < this.nodes.Length; i++)
				//        {
				//            if (this.nodes[i].face_id <= index && this.nodes[i].face_id + this.nodes[i].face_num > index)
				//            {
				//                Trace.WriteLine(string.Format("Lost face {0} belongs to node {1}", index, i));
				//                break;
				//            }
				//        }
				//        for (int i = 0; i < this.leaves.Length; i++)
				//        {
				//            var begin = this.leaves[i].firstleafface;
				//            var end = begin+this.leaves[i].numleaffaces;
				//            while (begin<end)
				//            {
				//                if (leafFaces[begin] == index)
				//                {
				//                    Trace.WriteLine(string.Format("Lost face {0} belongs to leaf {1}", index, i));
				//                    break;
				//                }
				//                ++begin;
				//            }
				//        }
				//    }
				//}

				this.Scene.VsdProvider = new BspVsdProvider
					{
						VisibleClustersLookupTable = visibleClustersLookup.ToArray(),
						VisibleMeshesLookupTable = visibleMeshesLookup.ToArray(),
						Clusters = vsdClusters,
						Leaves = vsdLeaves,
						Models = (from model in this.models select new BspVsdTreeModel { RootNode = model.headnode }).ToArray(),
						Level = streamMesh,
						Nodes = vsdNodes
					};

				this.BuildAdditionalNodes();
			}
			else
			{
				this.BuildAdditionalNodes();
				node.Mesh = this.Scene.Geometries[0];
			}

			this.BuildEntityNodes(this.entitiesInfo);
		}