Esempio n. 1
0
        static public void GetMidPoint(Faces faces, out Vector3D ptMidPoint)
        {
            Vector3D ptMin, ptMax;

            FaceUtils.GetExtents(faces, out ptMin, out ptMax);
            ptMidPoint = (ptMin + ptMax) * 0.5f;
        }
Esempio n. 2
0
        public void LoadDataSet(string fileName, Color clr)
        {
            //Debug.Assert( this.Faces.Count == 0 );
            //Debug2.Push( "Parse Object Definition File" );

            TokenReader tr = new TokenReader(fileName, ' ');

            int vertexCount   = int.Parse(tr.GetToken());
            int triangleCount = int.Parse(tr.GetToken());

            _vertices  = new Vector3D[vertexCount];
            _normals   = new Vector3D[vertexCount];
            _triangles = new int[triangleCount * 3];

            _trianglePlanes = new Plane3D[triangleCount];

            for (int v = 0; v < vertexCount; v++)
            {
                tr.GetToken();
                _vertices[v] = new Vector3D(
                    float.Parse(tr.GetToken()),
                    float.Parse(tr.GetToken()),
                    float.Parse(tr.GetToken()));
                _normals[v] = new Vector3D(0, 0, 0);
            }

            // translate vertices
            Vector3D ptOldMin, ptOldMax;

            FaceUtils.GetExtents(_vertices, out ptOldMin, out ptOldMax);
            Vector3D ptOldMidPoint = (ptOldMin + ptOldMax) * 0.5f;
            float    fOldScale     = (ptOldMidPoint - ptOldMin).GetMagnitude();

            for (int i = 0; i < vertexCount; i++)
            {
                _vertices[i] = (_vertices[i] - ptOldMidPoint) / fOldScale * this.Scale + this.MidPoint;
            }

            for (int t = 0; t < triangleCount; t++)
            {
                tr.GetToken();

                // triangle vertices
                _triangles[t * 3 + 0] = int.Parse(tr.GetToken());
                _triangles[t * 3 + 1] = int.Parse(tr.GetToken());
                _triangles[t * 3 + 2] = int.Parse(tr.GetToken());

                // skip extra triangle info
                tr.GetToken();
                tr.GetToken();
                tr.GetToken();

                // calculate triangle normal
                Vector3D vertexA = _vertices[_triangles[t * 3 + 0]];
                Vector3D vertexB = _vertices[_triangles[t * 3 + 1]];
                Vector3D vertexC = _vertices[_triangles[t * 3 + 2]];
                _trianglePlanes[t] = Plane3D.FromCoplanarPoints(vertexA, vertexB, vertexC);
                //Vector3D.Cross( vertexC - vertexB, vertexA - vertexB ).GetUnit();
            }

            tr.Close();

            //Debug2.Pop();

            /*for( int v = 0; v < vertexCount; v ++ ) {
             *      if( vertexNormals[ v ].GetMagnitude() > 0.1 ) {
             *              vertexNormals[ v ].Normalize();
             *      }
             *      else {
             *              vertexNormals[ v ] = new Vector3D( 1, 0, 0 );
             *      }
             * }  */

            //Debug2.Push( "Calculate Face Normals" );

            float fLimit = 0f;

            for (int t = 0; t < triangleCount; t++)
            {
                for (int v = 0; v < 3; v++)
                {
                    int      vIndex    = _triangles[t * 3 + v];
                    Vector3D normalSum = _trianglePlanes[t].Normal;
                    Vector3D normal    = _trianglePlanes[t].Normal;
                    for (int t2 = 0; t2 < triangleCount; t2++)
                    {
                        if (t2 == t)
                        {
                            continue;
                        }
                        bool bMatch = false;
                        for (int v2 = 0; v2 < 3; v2++)
                        {
                            if (vIndex == _triangles[t2 * 3 + v2])
                            {
                                bMatch = true;
                                break;
                            }
                        }
                        if (bMatch == true)
                        {
                            Vector3D normal2 = _trianglePlanes[t2].Normal;
                            if (Vector3D.Dot(normal, normal2) > fLimit)
                            {
                                normalSum += normal2;
                            }
                        }
                    }
                    if (normalSum.GetMagnitude() > 0.01)
                    {
                        _normals[vIndex] = normalSum.GetUnit();
                    }
                    else
                    {
                        _normals[vIndex] = (Vector3D)normal.Clone();
                    }
                }
            }
            //Debug2.Pop();

            /*
             *      int a = faceIndices[ f, 0 ];
             *      int b = faceIndices[ f, 1 ];
             *      int c = faceIndices[ f, 2 ];
             *
             *      Face face = this.Faces[f];
             *      face.VertexNormals = new Vector3DCollection();
             *      face.VertexNormals.Add( vertexNormals[ a ].Clone() );
             *      face.VertexNormals.Add( vertexNormals[ b ].Clone() );
             *      face.VertexNormals.Add( vertexNormals[ c ].Clone() );
             * }*/
        }
Esempio n. 3
0
        //-----------------------------------------------------------------------------

        static public World  FromWorldcraftMap(WorldcraftMap wm)
        {
            // create new world
            World world = new World();

            world.Textures = wm.Textures;
            world.SkyBox   = new SkyBox("redSky");
            world.FileName = Path.Combine(ExoEngine.sWorldPath, Path.ChangeExtension(Path.GetFileName(wm.FileName), ExoEngine.sWorldExtension));
            world.Dirty    = true;


            // find the starting location
            WorldcraftObject woStartPosition = wm.Objects.FindByClassName("StartPosition");

            if (woStartPosition == null)
            {
                return(null);
            }

            Vector3D ptStartPosition;

            FaceUtils.GetMidPoint(woStartPosition.Faces, out ptStartPosition);
            world.StartPosition    = ptStartPosition;
            world.StartOrientation = woStartPosition.GetPropertyInteger("orientation", 0);

            // find the main light
            WorldcraftObject woLight = wm.Objects.FindByClassName("Light");

            if (woLight == null)
            {
                return(null);
            }

            Vector3D ptLight;

            FaceUtils.GetMidPoint(woLight.Faces, out ptLight);
            world.Light = ptLight;

            // find "Default" group... it is the static world
            WorldcraftObject woDefault = wm.Objects.FindByClassName("Default");

            if (woDefault == null)
            {
                return(null);
            }

            // setup BSP tree
            Faces faces = new Faces();

            // handle the other objects
            foreach (WorldcraftObject wo in wm.Objects)
            {
                if (wo.ClassName == "Water")
                {
                    Vector3D ptMin, ptMax;
                    FaceUtils.GetExtents(wo.Faces, out ptMin, out ptMax);
                    Water water = new Water(ptMin, ptMax, wo.GetPropertyFloat("waveHeight", 100));
                    water.Color = wo.GetPropertyColor("color", Color.Azure);
                    world.Entities.Add(water);
                }
                else if (wo.ClassName == "Duck")
                {
                    Vector3D ptMin, ptMax;
                    FaceUtils.GetExtents(wo.Faces, out ptMin, out ptMax);
                    Duck duck = new Duck(ptMin, ptMax);
                    duck.LoadDataSet(Path.Combine(ExoEngine.sSpritePath, "duck.odf"), wo.GetPropertyColor("color", Color.Yellow));
                    world.Entities.Add(duck);

                    foreach (Face face in wo.Faces)
                    {
                        face.Visible = false;
                    }
                    faces.AddRange(wo.Faces);
                }
            }

            world.Entities.SortByPriority();

            faces.AddRange(woDefault.Faces);
            FaceUtils.OptimizeFaces(faces);
            world.BSPTreeRoot = BSPTreeNode.FromFaces(faces);

            return(world);
        }