Пример #1
0
        private void btn_loadTerrain_Click(object sender, EventArgs e)
        {
            fil_openTerrain.FileName = m_dirTerrain;

            /* Open the load dialog */
            DialogResult loadResult = fil_openTerrain.ShowDialog();
            if (loadResult == DialogResult.Abort || loadResult == DialogResult.Cancel)
                return;

            System.IO.Stream stream = fil_openTerrain.OpenFile();

            /* Create the terrain (just height map, no mesh will be created yet) */
            m_terrain = m_data.LoadTerrainFile(stream);

            if (m_terrain != null)
            {
                prp_terrainProperties.SelectedObject = m_terrain;
                rdr_terrainView.ChangeViewMode(RenderPane.ViewMode.eTerrainViewer);
                rdr_terrainView.AddTerrainToScene(m_terrain);
            }
        }
Пример #2
0
        public LevelTerrain LoadTerrainFile(System.IO.Stream stream)
        {
            System.IO.BinaryReader br = new System.IO.BinaryReader(stream);

            byte[] fileBuffer = new byte[stream.Length];
            br.Read(fileBuffer, 0, (int)stream.Length);

            LevelTerrain terrain = new LevelTerrain();
            Scale scales = new Scale();
            scales.Scales = new Vector3(10.0f, 1.0f, 10.0f);
            terrain.Scaling = scales;

            // assume square image
            int dim = (int)Math.Sqrt(stream.Length);
            terrain.HeightMapDimensions = new Point(dim, dim);
            terrain.HeightMap = fileBuffer;

            String name = "TERRAIN" + GetNextTerrainIdx();
            terrain.Name = name;
            return terrain;
        }
Пример #3
0
        public void LoadLevelFile(System.IO.Stream stream)
        {
            System.IO.BinaryReader br = new System.IO.BinaryReader(stream);

            /* Get Current App Version For Comparison */
            String currentVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

            /* Check Loading File's Version To Current */
            String fileVersion = br.ReadString();
            if (fileVersion != currentVersion)
            {
                /* ERROR: File Version Out of Date */
                throw new IncorrectFileVersionException();
            }

            /* Load the Level Attributes */
            levelName = br.ReadString();

            /* Load the Object Type Palette */
            List<FileOps.LevelObjectTypeDefinition> types = FileOps.ReadObjectPalette(br);

            /* Level Terrain */
            int numTerrainChunks = br.ReadInt32();
            for (int i = 0; i < numTerrainChunks; i++)
            {
                LevelTerrain terrain = new LevelTerrain();
                terrain.Name = br.ReadString();
                terrain.Position = FileOps.ReadVec3(br);

                Scale scale = new Scale();
                scale.scales = FileOps.ReadVec3(br);
                terrain.Scaling = scale;

                Point pt = new Point();
                pt.X = br.ReadInt32();
                pt.Y = br.ReadInt32();
                terrain.HeightMapDimensions = pt;

                int numSamples = terrain.HeightMapDimensions.X * terrain.HeightMapDimensions.Y;
                byte[] heightMap = new byte[numSamples];
                br.Read(heightMap, 0, numSamples);
                terrain.HeightMap = heightMap;

                terrainObjects.Add(terrain.Name, terrain);
            }

            /* Load Level Object Instances */
            int levelCnt = br.ReadInt32();
            System.Collections.Generic.List<LevelObject> lvlObjs = new List<LevelObject>();

            for (int i = 0; i < levelCnt; i++)
            {
                LevelObject lo = new LevelObject();

                /* Save Object Instance Attributes */
                lo.Index = br.ReadInt32();
                lo.Name = br.ReadString();
                lo.TypeId = new Guid(br.ReadString());

                /* Object Instance Transforms */
                lo.Rotation = FileOps.ReadVec3(br);
                lo.Scale = FileOps.ReadVec3(br);
                lo.Position = FileOps.ReadVec3(br);

                lvlObjs.Add(lo);

                m_nextUnusedObID = Math.Max(lo.Index, m_nextUnusedObID);
                m_nextUnusedObID++;
            }

            br.Close();

            /* Load the Level Object Types Definitions From
             * Files For Each Type in Level's Object Palette */
            foreach (FileOps.LevelObjectTypeDefinition t in types)
            {
                /* Try to Open the Type's File */
                String typeFilename = levelFileName;
                int idx = typeFilename.LastIndexOf('\\')+1;
                typeFilename = typeFilename.Substring(0, idx);
                typeFilename += t.FileName;

                System.IO.FileStream typeFile = null;

                try
                {
                    typeFile = System.IO.File.Open(typeFilename, System.IO.FileMode.Open);

                    /* Fill Out a New Object Type from The File Definition */
                    LevelObjectType lot = LoadTypeFile(typeFile);

                    /* Verify the Type Matches the Guid From the Level Type Palette */
                    if (lot.TypeID == t.TypeId)
                    {
                        this.objectPalette.Add(lot.Name, lot);
                    }
                    else
                    {
                        System.Diagnostics.Debug.Assert(true);
                    }

                    /* Increment the Type ID counter */
                    m_nextUnusedTypeID = Math.Max(lot.Index, m_nextUnusedTypeID);
                    m_nextUnusedTypeID++;
                }
                finally
                {
                    typeFile.Close();
                }
            }

            /* Add the object to the level linked to their type */
            foreach (LevelObject lo in lvlObjs)
            {
                AddObjectToLvl(lo, false);
            }
        }
Пример #4
0
        public void AddTerrainToLvl(LevelTerrain terr, bool remove)
        {
            if (!remove)
            {

                bool found = true;
                while (found == true)
                {
                    found = false;
                    foreach (KeyValuePair<String, LevelTerrain> kv in terrainObjects)
                    {
                        if (kv.Key == terr.Name)
                        {
                            /* find a new name that doesn't conflict */
                            int id = Convert.ToInt32(terr.Name.Substring(terr.Name.Length - 1));
                            terr.Name = terr.Name.Substring(0, terr.Name.Length - 1) + id.ToString();
                            found = true;
                            break;
                        }

                    }
                }

                terrainObjects.Add(terr.Name, terr);
                if (ObjectTerrainAdded != null)
                {
                    LevelEventArgs e = new LevelEventArgs();
                    e.terr = terr;
                    ObjectTerrainAdded(this, e);
                }
            }
            else
            {
                terrainObjects.Remove(terr.Name);
                if (ObjectTerrainRemoved != null)
                {
                    LevelEventArgs e = new LevelEventArgs();
                    e.terr = terr;
                    ObjectTerrainRemoved(this, e);
                }
            }
        }
Пример #5
0
        public void AddTerrainToScene(LevelTerrain terrain)
        {
            System.Diagnostics.Debug.Assert(m_mode == ViewMode.eTerrainViewer || m_mode == ViewMode.eLevelViewer);

            DrawableLevelTerrain drawTerrain = new DrawableLevelTerrain(terrain);
            drawTerrain.Init(Services);
            m_scene.Add(terrain.Name, (ISceneObject)drawTerrain);
        }