Example #1
0
        public RTSUnitModel(RTSRenderer renderer, Stream sModel, Texture2D tAnim)
        {
            // Create With The Animation Texture
            AnimationTexture = tAnim;

            // Parse The Model File
            VertexPositionNormalTexture[] pVerts;
            VertexPositionTexture[]       verts;
            int[] inds;
            if (!ObjParser.TryParse(sModel, out pVerts, out inds, MODEL_READ_FLAGS))
            {
                throw new ArgumentException("Bad Model File Format");
            }

            // Reformat Vertices
            verts = new VertexPositionTexture[pVerts.Length];
            for (int i = 0; i < verts.Length; i++)
            {
                verts[i].Position          = new Vector3((float)((i + 0.1) / AnimationTexture.Width), 0, 0);
                verts[i].TextureCoordinate = pVerts[i].TextureCoordinate;
            }

            // Create Model Geometry
            RTSModelHelper.CreateBuffers(renderer, verts, VertexPositionTexture.VertexDeclaration, inds, out vbModel, out ibModel, BufferUsage.WriteOnly);
        }
Example #2
0
 private void LoadBulletModel(RTSRenderer renderer, Stream s, ParsingFlags pf = ParsingFlags.ConversionOpenGL)
 {
     VertexPositionNormalTexture[] v;
     int[] inds;
     ObjParser.TryParse(s, out v, out inds, pf);
     VertexPositionTexture[] verts = new VertexPositionTexture[v.Length];
     for (int i = 0; i < verts.Length; i++)
     {
         verts[i].Position          = v[i].Position;
         verts[i].TextureCoordinate = v[i].TextureCoordinate;
     }
     plBullets.VBuffer = renderer.CreateVertexBuffer(VertexPositionTexture.VertexDeclaration, verts.Length, BufferUsage.WriteOnly);
     plBullets.VBuffer.SetData(verts);
     plBullets.IBuffer = renderer.CreateIndexBuffer(IndexElementSize.ThirtyTwoBits, inds.Length, BufferUsage.WriteOnly);
     plBullets.IBuffer.SetData(inds);
 }
Example #3
0
        static void SuperBake(FileInfo[] files, string pathOut, RichTextBox log)
        {
            VertexPositionNormalTexture[] verts;
            int[] inds;

            // Find The Width Of The Bitmap First
            int bmpWidth = 0;

            using (FileStream fs = File.OpenRead(files[0].FullName)) {
                ObjParser.TryParse(fs, out verts, out inds, ParsingFlags.ConversionOpenGL);
                bmpWidth = verts.Length;
            }
            Bitmap bmp = new Bitmap(bmpWidth, files.Length * 3);

            log.AppendText("Creating New Image " + bmp.Width + " x " + bmp.Height + "\n");

            // Bake
            int   py = 0;
            float percent = 0f, dp = 100f / files.Length;

            log.AppendText("Percent Complete: " + percent + "%\n");
            foreach (var fi in files)
            {
                using (FileStream fs = File.OpenRead(fi.FullName)) {
                    ObjParser.TryParse(fs, out verts, out inds, ParsingFlags.ConversionOpenGL);
                }
                for (int px = 0; px < bmp.Width || px < verts.Length; px++)
                {
                    bmp.SetPixel(px, py, Convert(verts[px].Position.X));
                    bmp.SetPixel(px, py + 1, Convert(verts[px].Position.Y));
                    bmp.SetPixel(px, py + 2, Convert(verts[px].Position.Z));
                }
                py      += 3;
                percent += dp;
                log.AppendText("Percent Complete: " + percent + "%\n");
            }

            // Save The Image
            bmp.Save(pathOut, System.Drawing.Imaging.ImageFormat.Png);
            log.AppendText("File Saved To - \n" + pathOut);
            bmp.Dispose();
        }
Example #4
0
        public RTSBuildingModel(RTSRenderer renderer, Stream sModel)
        {
            // Parse The Model File
            VertexPositionNormalTexture[] pVerts;
            VertexPositionTexture[]       verts;
            int[] inds;
            if (!ObjParser.TryParse(sModel, out pVerts, out inds, MODEL_READ_FLAGS))
            {
                throw new ArgumentException("Bad Model File Format");
            }
            verts = new VertexPositionTexture[pVerts.Length];
            for (int i = 0; i < verts.Length; i++)
            {
                verts[i].Position          = pVerts[i].Position;
                verts[i].TextureCoordinate = pVerts[i].TextureCoordinate;
            }

            // Create Model Geometry
            RTSModelHelper.CreateBuffers(renderer, verts, VertexPositionTexture.VertexDeclaration, inds, out vbModel, out ibModel, BufferUsage.WriteOnly);
        }
Example #5
0
        static void TestBake(FileInfo[] files, string pathBMP, RichTextBox log)
        {
            Bitmap bmp = Bitmap.FromFile(pathBMP) as Bitmap;

            VertexPositionNormalTexture[] verts;
            int[] inds;
            int   py = 0;
            float percent = 0f, dp = 100f / files.Length;
            float totalDistSq = 0f, maxDistSq = 0f;

            log.AppendText("Percent Complete: " + percent + "%\n");
            foreach (var fi in files)
            {
                using (FileStream fs = File.OpenRead(fi.FullName)) {
                    ObjParser.TryParse(fs, out verts, out inds, ParsingFlags.ConversionOpenGL);
                }
                for (int px = 0; px < bmp.Width || px < verts.Length; px++)
                {
                    Vector3 point = new Vector3(
                        Convert(bmp.GetPixel(px, py)),
                        Convert(bmp.GetPixel(px, py + 1)),
                        Convert(bmp.GetPixel(px, py + 2))
                        );
                    float d = (point - verts[px].Position).LengthSquared();
                    totalDistSq += d;
                    if (d > maxDistSq)
                    {
                        maxDistSq = d;
                    }
                }
                py      += 3;
                percent += dp;
                log.AppendText("Percent Complete: " + percent + "%\n");
            }
            log.AppendText("Total Error (Sum Squares): " + totalDistSq + "\nMax Error: " + maxDistSq + "\n");

            bmp.Dispose();
        }
Example #6
0
        private void Rebuild(string fIn, string fOut)
        {
            // Check Args
            FileInfo fiIn = new FileInfo(fIn);

            if (!fiIn.Exists)
            {
                Console.WriteLine("File Does Not Exist");
                return;
            }
            FileInfo fiOut = new FileInfo(fOut);

            if (!fiOut.Directory.Exists)
            {
                Console.WriteLine("Output Directory Does Not Exist");
                return;
            }

            // Read Model
            Stream s = File.OpenRead(fiIn.FullName);

            VertexPositionNormalTexture[] verts;
            int[] inds;
            if (!ObjParser.TryParse(s, out verts, out inds, ParsingFlags.ConversionOpenGL))
            {
                s.Dispose();
                Console.WriteLine("Could Not Read Model");
                return;
            }
            s.Dispose();

            // Compute The AABB Of The Terrain
            BoundingBox aabb = ComputeAABB(verts);
            Vector3     mid  = aabb.Max + aabb.Min;
            Vector3     dif  = aabb.Max - aabb.Min;
            Vector3     top  = new Vector3(mid.X, aabb.Max.Y, mid.Z);

            mid          *= 0.5f;
            fx.FogStart   = 1f;
            fx.FogEnd     = aabb.Max.Y - aabb.Min.Y + 1f;
            fx.World      = Matrix.Identity;
            fx.View       = Matrix.CreateLookAt(top + Vector3.UnitY, mid, -Vector3.UnitZ);
            fx.Projection = Matrix.CreateOrthographic(dif.X, dif.Z, 0, dif.Y + 2f);

            // Append A Plane At The Bottom
            int vc = verts.Length, ic = inds.Length;

            Array.Resize(ref verts, verts.Length + 4);
            Array.Resize(ref inds, inds.Length + 6);
            inds[ic++]  = vc + 0;
            inds[ic++]  = vc + 1;
            inds[ic++]  = vc + 2;
            inds[ic++]  = vc + 2;
            inds[ic++]  = vc + 1;
            inds[ic++]  = vc + 3;
            verts[vc++] = new VertexPositionNormalTexture(
                new Vector3(aabb.Min.X, aabb.Min.Y, aabb.Min.Z),
                Vector3.UnitY, Vector2.Zero
                );
            verts[vc++] = new VertexPositionNormalTexture(
                new Vector3(aabb.Max.X, aabb.Min.Y, aabb.Min.Z),
                Vector3.UnitY, Vector2.UnitX
                );
            verts[vc++] = new VertexPositionNormalTexture(
                new Vector3(aabb.Min.X, aabb.Min.Y, aabb.Max.Z),
                Vector3.UnitY, Vector2.UnitY
                );
            verts[vc++] = new VertexPositionNormalTexture(
                new Vector3(aabb.Max.X, aabb.Min.Y, aabb.Max.Z),
                Vector3.UnitY, Vector2.One
                );

            // Create Model
            VertexBuffer vb = new VertexBuffer(G, VertexPositionNormalTexture.VertexDeclaration, verts.Length, BufferUsage.WriteOnly);

            vb.SetData(verts);
            IndexBuffer ib = new IndexBuffer(G, IndexElementSize.ThirtyTwoBits, inds.Length, BufferUsage.WriteOnly);

            ib.SetData(inds);

            // Render The Height
            if (rtHeight != null)
            {
                rtHeight.Dispose();
            }
            rtHeight = new RenderTarget2D(G, 4096, 4096, false, SurfaceFormat.Color, DepthFormat.Depth24);
            G.SetRenderTarget(rtHeight);
            G.SetVertexBuffer(vb);
            G.Indices = ib;
            fx.CurrentTechnique.Passes[0].Apply();
            G.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vb.VertexCount, 0, ib.IndexCount / 3);

            // Dispose Of Buffers
            G.SetRenderTarget(null);
            G.Clear(Color.Black);
            G.SetVertexBuffer(null);
            G.Indices = null;
            vb.Dispose();
            ib.Dispose();

            // Save The Image
            using (Stream os = File.Create(fiOut.FullName)) {
                rtHeight.SaveAsPng(os, rtHeight.Width, rtHeight.Height);
            }

            ShouldRebuild = false;
        }