コード例 #1
0
        // loads a single cgf in the viewer
        public void LoadCgf(CgfLoader cgf)
        {
            var vertices          = new List <VertexPositionColor>();
            var collisionVertices = new List <VertexPositionColor>();
            var lineVertices      = new List <VertexPositionColor>();

            var xform = Matrix.Identity;

            DrawCgfInWorld(cgf, ref xform, Vector3.Zero, CgfDrawStyle.Brush, vertices, collisionVertices, lineVertices);

            m_renderData.vertexBuffer = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, vertices);

            m_renderData.collisionVertexBuffer = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, collisionVertices);

            if (lineVertices.Count > 0)
            {
                m_renderData.lineVertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), lineVertices.Count, BufferUsage.WriteOnly);
                m_renderData.lineVertexBuffer.SetData <VertexPositionColor>(lineVertices.ToArray());
            }
        }
コード例 #2
0
        // load terrain vertices for rendering
        private List <VertexBuffer> LoadH32Terrain(GraphicsDevice GraphicsDevice, H32Loader h32, float waterLevel)
        {
            if (h32.isEmpty)
            {
                return(null);
            }

            bool createBothTriangles = true;
            int  triCount            = (createBothTriangles ? 2 : 1);

            var terrainVertices = new VertexPositionColorNormal[h32.width * h32.width * 3 * triCount];
            int dst             = 0;

            for (int y = 0; y < h32.width - 1; y++)
            {
                for (int x = 0; x < h32.width - 1; x++)
                {
                    var p1 = h32.VertexLookup(x, y);
                    var p2 = h32.VertexLookup(x, y + 1);
                    var p3 = h32.VertexLookup(x + 1, y);
                    var p4 = h32.VertexLookup(x + 1, y + 1);

                    Color color;
                    if (h32.IsCutout(x, y))
                    {
                        color = Color.Cyan;
                    }
                    else if (p1.Z < waterLevel)
                    {
                        color = Color.Blue;
                    }
                    else if (p1.Z <= waterLevel + 2)
                    {
                        color = Color.SandyBrown;
                    }
                    else if (Math.Abs(p1.Z - p2.Z) >= 2 || Math.Abs(p1.Z - p3.Z) >= 2)
                    {
                        color = Color.White;//Red;
                    }
                    else
                    {
                        color = Color.Green;
                    }

                    // Tri 1/2
                    var normal1 = MathUtil.CalculateNormal(p1, p2, p3);
                    terrainVertices[dst++] = new VertexPositionColorNormal(p1, color, normal1);
                    terrainVertices[dst++] = new VertexPositionColorNormal(p3, color, normal1);
                    terrainVertices[dst++] = new VertexPositionColorNormal(p2, color, normal1);

                    // Tri 2/2
                    if (createBothTriangles)
                    {
                        var normal2 = MathUtil.CalculateNormal(p2, p4, p3);
                        terrainVertices[dst++] = new VertexPositionColorNormal(p2, color, normal2);
                        terrainVertices[dst++] = new VertexPositionColorNormal(p3, color, normal2);
                        terrainVertices[dst++] = new VertexPositionColorNormal(p4, color, normal2);
                    }
                }
            }

            return(VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, terrainVertices));
        }
コード例 #3
0
        private void LoadContentHelper(DirManager levelDir, DirManager meshesDir)
        {
            var TIMER = DateTime.Now;

            //=========

            var vertices               = new List <VertexPositionColor>();
            var vegVertices            = new List <VertexPositionColor>();
            var collisionVertices      = new List <VertexPositionColor>();
            var doorVertices1          = new List <VertexPositionColor>();
            var doorVertices2          = new List <VertexPositionColor>();
            var doorCollisionVertices1 = new List <VertexPositionColor>();
            var doorCollisionVertices2 = new List <VertexPositionColor>();
            var lineVertices           = new List <VertexPositionColor>();

            //==========
            //Window.Title = "Loading brushes...";
            LoadBrushes(meshesDir, levelDir, vertices, collisionVertices, lineVertices);

            //============
            //Window.Title = "Loading vegetation...";
            LoadVegetation(meshesDir, levelDir, vegVertices, collisionVertices, lineVertices);

            //============
            // doors have 2 states - start and end. doors can start open or closed, so the end state is the opposite.
            LoadDoors(meshesDir, levelDir, 0, doorVertices1, doorCollisionVertices1, lineVertices);
            LoadDoors(meshesDir, levelDir, 999999, doorVertices2, doorCollisionVertices2, lineVertices);

            // ===========
            // terrain
            H32Loader h32;

            using (var landMapStream = levelDir.OpenFile(@"terrain\land_map.h32"))
                h32 = new H32Loader(landMapStream);

            Debug.WriteLine("Data load TIME: " + (DateTime.Now - TIMER));



            // ============== NAVMESH TEST =====================
            var floorLineVertices =
                new List <VertexPositionColor>(); // disable navmesh

            //LoadNavMeshTestData(h32);



            //============
            m_renderData.vertexBuffer = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, vertices);

            m_renderData.vegetationVertexBuffer = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, vegVertices);

            m_renderData.collisionVertexBuffer = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, collisionVertices);

            m_renderData.doorVertexBuffer1          = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, doorVertices1);
            m_renderData.doorVertexBuffer2          = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, doorVertices2);
            m_renderData.doorCollisionVertexBuffer1 = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, doorCollisionVertices1);
            m_renderData.doorCollisionVertexBuffer2 = VertexBufferUtil.CreateLargeVertexBuffer(GraphicsDevice, doorCollisionVertices2);

            if (lineVertices.Count > 0)
            {
                m_renderData.lineVertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), lineVertices.Count, BufferUsage.WriteOnly);
                m_renderData.lineVertexBuffer.SetData <VertexPositionColor>(lineVertices.ToArray());
            }

            if (floorLineVertices.Count > 0)
            {
                m_renderData.floorLineVertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), floorLineVertices.Count, BufferUsage.WriteOnly);
                m_renderData.floorLineVertexBuffer.SetData <VertexPositionColor>(floorLineVertices.ToArray());
            }

            vertices               = null;
            vegVertices            = null;
            collisionVertices      = null;
            doorVertices1          = null;
            doorVertices2          = null;
            doorCollisionVertices1 = null;
            doorCollisionVertices2 = null;
            lineVertices           = null;
            GC.Collect();

            //============

            //Window.Title = "Loading terrain...";
            using (var levelDataXml = levelDir.OpenFile("leveldata.xml"))
            {
                m_renderData.terrainVertexBuffer =
                    LoadH32Terrain(GraphicsDevice, h32, new LevelDataXmlLoader(levelDataXml).WaterLevel);
            }
            GC.Collect();
        }