Example #1
0
        private void InitialiseThreeDeeView()
        {
            if (mSubject == null)
              {
            return;
              }

              int SCALE = 256;
              int GRIDSIZE = 9; // Should be an odd number so the centre is the middle of a tile.
              mRenderer = new ImmediateModeRenderer();
              mRenderer.Attach(mMainForm.CameraRenderingSurface);

              mScene = new Scene();
              mCamera = new Camera(80, 0.1, 1e10);
              mView = new GLTK.View(mScene, mCamera, mRenderer);

              mScene.Clear();
              if (mMainForm.CurrentLevel != null)
              {
            // Create a surface and fill it with a 10 x 10 grid of squares
            MMEdEntity lSurface = new MMEdEntity(mSubject);

            for (int x = 0; x < GRIDSIZE; x++)
            {
              for (int y = 0; y < GRIDSIZE; y++)
              {
            Mesh lSquare = new OwnedMesh(mSubject, PolygonMode.Quads);
            lSquare.AddFace(
              new Vertex(new GLTK.Point(x, y, 0), 0, 0),
              new Vertex(new GLTK.Point(x + 1, y, 0), 1, 0),
              new Vertex(new GLTK.Point(x + 1, y + 1, 0), 1, 1),
              new Vertex(new GLTK.Point(x, y + 1, 0), 0, 1));
            lSquare.RenderMode = RenderMode.Wireframe;
            lSurface.Meshes.Add(lSquare);
              }
            }

            // Add it to the scene at the origin
            lSurface.Scale(SCALE, SCALE, 1.0);
            short lOffset = (short)(-SCALE * GRIDSIZE / 2);
            GLTK.Point lNewPos = ThreeDeeViewer.Short3CoordToPoint(new Short3Coord(lOffset, lOffset, 0));
            lSurface.Position = new GLTK.Point(lNewPos.x, lNewPos.y, -lNewPos.z);
            mScene.AddRange(new MMEdEntity[] { lSurface });

            // Use a random object from the level for now.
            Level lLevel = mMainForm.CurrentLevel;
            TMDChunk lObject = lLevel.GetObjtById(1);
            mScene.AddRange(lObject.GetEntities(
              mMainForm.CurrentLevel,
              MMEd.Viewers.ThreeDee.eTextureMode.NormalTextures,
              eTexMetaDataEntries.Steering));

            string lExceptionWhen = "opening file";
            try
            {
              ThreeDeeViewer.SceneHolder sh;
              string lFilename = string.Format("{0}{1}..{1}camera-editor-scene.xml",
            Path.GetDirectoryName(
              new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath),
            Path.DirectorySeparatorChar);

              using (FileStream fs = File.OpenRead(lFilename))
              {
            lExceptionWhen = "deserialising the scene";
            XmlSerializer xs = new XmlSerializer(typeof(ThreeDeeViewer.SceneHolder));
            sh = (ThreeDeeViewer.SceneHolder)xs.Deserialize(fs);
              }
              lExceptionWhen = "fixing texture ids";
              Dictionary<int, int> lSavedTexIdsToLiveTexIds = new Dictionary<int, int>();
              foreach (ThreeDeeViewer.TextureHolder th in sh.Textures)
              {
            if (th.Bitmap != null)
            {
              lSavedTexIdsToLiveTexIds[th.ID] = AbstractRenderer.ImageToTextureId(th.Bitmap);
            }
            else
            {
              lSavedTexIdsToLiveTexIds[th.ID] = 0;
            }
              }
              foreach (Entity ent in sh.Entities)
              {
            foreach (Mesh m in ent.Meshes)
            {
              if (m.RenderMode == RenderMode.Textured)
              {
                m.Texture = lSavedTexIdsToLiveTexIds[m.Texture];
              }
            }
              }
              mScene.Objects.Clear();
              mScene.AddRange(sh.Entities);
            }
            catch (Exception err)
            {
              System.Diagnostics.Trace.WriteLine(err);
              MessageBox.Show(string.Format("Exception occurred while {0}: {1}", lExceptionWhen, err.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            UpdateCameraThreeDee();
              }
              else
              {
            System.Windows.Forms.MessageBox.Show("3D view is only available when editing a level");
            mMainForm.CameraRenderingSurface.Visible = false;
              }
        }