Пример #1
0
        private void UpdateCameraThreeDee()
        {
            if (mCamera == null)
            {
                return;
            }

            // TODO: The elevation isn't rending quite right yet...
            short x = (short)(Math.Sin(-mSubject.Direction * Math.PI / 2048) * mSubject.Distance);
            short y = (short)(Math.Cos(-mSubject.Direction * Math.PI / 2048) * mSubject.Distance);

            mCamera.Position = ThreeDeeViewer.Short3CoordToPoint(new Short3Coord(x, y, mSubject.Elevation));
            mCamera.LookAt(new GLTK.Point(0, 0, 0), new GLTK.Vector(0, 0, 1));
            mMainForm.CameraRenderingSurface.Invalidate();
        }
Пример #2
0
        public void Serialize(string filePath, ThreeDeeViewer threeDeeViewer, MainForm mMainForm)
        {
            using (TextWriter stream = new StreamWriter(filePath))
              {
            stream.WriteLine("#VRML V2.0 utf8");
            foreach (Entity entity in mScene.Entities)
            {
              Matrix localTransf = entity.Transform;

              Vector translation;
              Vector rotationAxis;
              double rotationAmount;
              Vector scale;
              localTransf.Decompose(out translation, out rotationAxis, out rotationAmount, out scale);

              stream.WriteLine("Transform {");
              stream.WriteLine("  rotation {0} {1} {2} {3}", rotationAxis[0], rotationAxis[1], rotationAxis[2], rotationAmount);
              stream.WriteLine("  scale {0} {1} {2}", scale[0], scale[1], scale[2]);
              stream.WriteLine("  translation {0} {1} {2}", translation[0], translation[1], translation[2]);

              stream.WriteLine("  children [");

              foreach (Mesh mesh in entity.Meshes)
              {
            stream.WriteLine("    Shape {");
            stream.WriteLine("      geometry IndexedFaceSet {");
            stream.WriteLine("        coord Coordinate {");
            stream.WriteLine("          point [");
            foreach (Vertex v in mesh.Vertices)
            {
              stream.WriteLine("            {0} {1} {2},", v.Position.x, v.Position.y, v.Position.z);
            }
            stream.WriteLine("          ]");
            stream.WriteLine("        }");
            stream.WriteLine("        coordIndex [");
            writeCoordIndexAscending(stream, mesh);
            stream.WriteLine("        ]");

            if (mesh.RenderMode == RenderMode.Textured)
            {
              stream.WriteLine("        texCoord TextureCoordinate {");
              stream.WriteLine("          point [");
              foreach (Vertex v in mesh.Vertices)
              {
                stream.WriteLine("            {0} {1},", v.TexCoordX, 1 - v.TexCoordY);
              }
              stream.WriteLine("          ]");
              stream.WriteLine("        }");
              stream.WriteLine("        texCoordIndex [");
              writeCoordIndexAscending(stream, mesh);
              stream.WriteLine("        ]");
            }

            stream.WriteLine("        solid TRUE");
            stream.WriteLine("      }");
            stream.WriteLine("      appearance Appearance {");
            stream.WriteLine("        material Material {");
            stream.WriteLine("           diffuseColor 0 1 1");
            stream.WriteLine("        }");
            if (mesh.RenderMode == RenderMode.Textured)
            {
              stream.WriteLine("        texture ImageTexture {");
              stream.WriteLine("           url \"{0}\"", ImageUrl(mesh, filePath).Replace('\\', '/'));
              stream.WriteLine("        }");
            }
            stream.WriteLine("      }");
            stream.WriteLine("    }");
              }
              stream.WriteLine("  ]");
              stream.WriteLine("}");
            }
              }
        }
Пример #3
0
 public MMEdViewerView(ThreeDeeViewer xiViewer, Scene xiScene, Camera xiCamera, AbstractRenderer xiRenderer)
     : base(xiScene, xiCamera, xiRenderer)
 {
     mViewer = xiViewer;
 }
Пример #4
0
 public MMEdViewerView(ThreeDeeViewer xiViewer, Scene xiScene, Camera xiCamera, AbstractRenderer xiRenderer)
     : base(xiScene, xiCamera, xiRenderer)
 {
     mViewer = xiViewer;
 }
Пример #5
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;
            }
        }