예제 #1
0
        private void CalculatePositionAndNormal(Submodel rootModel, Submodel furthesModel, MetaSortInstruction sortInstruction)
        {
            Vector3 far = MakeVector3(furthesModel.Point);

            Vector3 center = (MakeVector3(rootModel.Point) + far) / 2.0f;

            Vector3 normal = Vector3.Normalize(far - center);

            // Set the center
            sortInstruction.Point  = MakeFixVector(center);
            sortInstruction.Normal = MakeFixVector(normal);
        }
예제 #2
0
        private MetaInstructionBase GetHierarchy(int modelIndex)
        {
            //Form the hierachy by the following process:
            //Pick the submitted model, and take one of its children
            //Generate a sortnorm with the child in front, and the model, with that child removed on the back
            //Recurse across the child in front, which will sort its children, and the original model on the back, with that child gone.
            Submodel submodel = currentModel.Submodels[modelIndex];
            BSPModel data     = submodels[modelIndex];

            //Check if no children. If not, we're done.
            if (data.ChildrenList.Count == 0)
            {
                data.CompileInterpreterData(vertexOffset);
                vertexOffset += data.NumVertices;
                MetaModelInstruction instruction = new MetaModelInstruction()
                {
                    Model     = submodel,
                    DataModel = data
                };

                return(instruction);
            }
            else
            {
                //Get the child and remove it from the front
                //int child = data.ChildrenList[0];
                //data.ChildrenList.RemoveAt(0);

                //Prefer closer objects first, to try to make sorting more reliable in complex situations.
                int childIndex = 0;
                Fix bestLength = 32700.0;
                for (int i = 0; i < data.ChildrenList.Count; i++)
                {
                    Fix dist = (currentModel.Submodels[data.ChildrenList[i]].Point - submodel.Point).Mag();
                    if (dist < bestLength)
                    {
                        childIndex = i;
                    }
                }

                int child = data.ChildrenList[childIndex];
                data.ChildrenList.RemoveAt(childIndex);

                //Generate a sortnorm instruction
                MetaSortInstruction instruction = new MetaSortInstruction();
                instruction.Normal = currentModel.Submodels[child].Normal;
                instruction.Point  = currentModel.Submodels[child].Point;

                //Front is the newly created child
                //Need a subcall entering it. A submodel should only ever be entered in the front once.
                MetaSubModelInstruction submodelInstruction = new MetaSubModelInstruction();
                submodelInstruction.SubModel    = currentModel.Submodels[child];
                submodelInstruction.Instruction = GetHierarchy(child);
                instruction.FrontInstruction    = submodelInstruction;

                //Back is the current set, but with the original child no longer considered
                instruction.BackInstruction = GetHierarchy(modelIndex);

                return(instruction);
            }

            /*MetaInstructionBase rootInstruction = new MetaModelInstruction
             * {
             *  Model = rootModel
             * };
             *
             * var meshesToProcess = rootModel.Children.ToList();
             *
             * while (meshesToProcess.Any())
             * {
             *  var closestMesh = this.GetClosestModel(meshesToProcess, rootModel);
             *
             *  meshesToProcess.Remove(closestMesh);
             *
             *  MetaSortInstruction sorting = new MetaSortInstruction();
             *
             *  CalculatePositionAndNormal(rootModel, closestMesh, sorting);
             *
             *  sorting.BackInstruction = new MetaSubModelInstruction
             *  {
             *      SubModel = closestMesh,
             *      Instruction = GetHierarchy(closestMesh)
             *
             *  };
             *
             *  sorting.FrontInstruction = rootInstruction;
             *
             *  rootInstruction = sorting;
             * }*/
            throw new Exception("PolymodelBuilder::GetHierarchy: generated null instruction");
        }