Esempio n. 1
0
        private RenderItem AddRenderItem(RenderLayer layer, int objCBIndex, string matName, string geoName, string submeshName,
                                         Matrix?world = null, Matrix?texTransform = null)
        {
            MeshGeometry    geo        = _geometries[geoName];
            SubmeshGeometry submesh    = geo.DrawArgs[submeshName];
            var             renderItem = new RenderItem
            {
                ObjCBIndex         = objCBIndex,
                Mat                = _materials[matName],
                Geo                = geo,
                IndexCount         = submesh.IndexCount,
                StartIndexLocation = submesh.StartIndexLocation,
                BaseVertexLocation = submesh.BaseVertexLocation,
                World              = world ?? Matrix.Identity,
                TexTransform       = texTransform ?? Matrix.Identity
            };

            _ritemLayers[layer].Add(renderItem);
            _allRitems.Add(renderItem);
            return(renderItem);
        }
        private void BuildTreeSpritesGeometry()
        {
            const int treeCount = 16;
            var       vertices  = new TreeSpriteVertex[treeCount];

            for (int i = 0; i < treeCount; i++)
            {
                float x = MathHelper.Randf(-45.0f, 45.0f);
                float z = MathHelper.Randf(-45.0f, 45.0f);
                float y = GetHillsHeight(x, z);

                // Move tree slightly above land height.
                y += 8.0f;

                vertices[i].Pos  = new Vector3(x, y, z);
                vertices[i].Size = new Vector2(20.0f, 20.0f);
            }

            short[] indices =
            {
                0, 1,  2,  3,  4,  5,  6, 7,
                8, 9, 10, 11, 12, 13, 14, 15
            };

            var submesh = new SubmeshGeometry
            {
                IndexCount         = indices.Length,
                StartIndexLocation = 0,
                BaseVertexLocation = 0
            };

            var geo = MeshGeometry.New(Device, CommandList, vertices, indices, "treeSpritesGeo");

            geo.DrawArgs["points"] = submesh;

            _geometries[geo.Name] = geo;
        }
        private void AddRenderItem(RenderLayer layer, int objCBIndex, string matName, string geoName, string submeshName,
                                   Matrix?world = null, Matrix?texTransform = null, Vector2?dmTexelSize = null, float?gridSpatialStep = null)
        {
            MeshGeometry    geo     = _geometries[geoName];
            SubmeshGeometry submesh = geo.DrawArgs[submeshName];

            var renderItem = new RenderItem
            {
                ObjCBIndex         = objCBIndex,
                Mat                = _materials[matName],
                Geo                = geo,
                IndexCount         = submesh.IndexCount,
                StartIndexLocation = submesh.StartIndexLocation,
                BaseVertexLocation = submesh.BaseVertexLocation
            };

            if (world.HasValue)
            {
                renderItem.World = world.Value;
            }
            if (texTransform.HasValue)
            {
                renderItem.TexTransform = texTransform.Value;
            }
            if (dmTexelSize.HasValue)
            {
                renderItem.DisplacementMapTexelSize = dmTexelSize.Value;
            }
            if (gridSpatialStep.HasValue)
            {
                renderItem.GridSpatialStep = gridSpatialStep.Value;
            }

            _ritemLayers[layer].Add(renderItem);
            _allRitems.Add(renderItem);
        }
        private void BuildLandGeometry()
        {
            GeometryGenerator.MeshData grid = GeometryGenerator.CreateGrid(160.0f, 160.0f, 50, 50);

            //
            // Extract the vertex elements we are interested and apply the height function to
            // each vertex. In addition, color the vertices based on their height so we have
            // sandy looking beaches, grassy low hills, and snow mountain peaks.
            //

            var vertices = new Vertex[grid.Vertices.Count];

            for (int i = 0; i < grid.Vertices.Count; i++)
            {
                Vector3 p = grid.Vertices[i].Position;
                vertices[i].Pos    = p;
                vertices[i].Pos.Y  = GetHillsHeight(p.X, p.Z);
                vertices[i].Normal = GetHillsNormal(p.X, p.Z);
                vertices[i].TexC   = grid.Vertices[i].TexC;
            }

            List <short> indices = grid.GetIndices16();

            var geo = MeshGeometry.New(Device, CommandList, vertices, indices.ToArray(), "landGeo");

            var submesh = new SubmeshGeometry
            {
                IndexCount         = indices.Count,
                StartIndexLocation = 0,
                BaseVertexLocation = 0
            };

            geo.DrawArgs["grid"] = submesh;

            _geometries["landGeo"] = geo;
        }
        private void BuildShapeGeometry()
        {
            //
            // We are concatenating all the geometry into one big vertex/index buffer. So
            // define the regions in the buffer each submesh covers.
            //

            var vertices = new List <Vertex>();
            var indices  = new List <short>();

            SubmeshGeometry box      = AppendMeshData(GeometryGenerator.CreateBox(1.0f, 1.0f, 1.0f, 3), vertices, indices);
            SubmeshGeometry grid     = AppendMeshData(GeometryGenerator.CreateGrid(20.0f, 30.0f, 60, 40), vertices, indices);
            SubmeshGeometry sphere   = AppendMeshData(GeometryGenerator.CreateSphere(0.5f, 20, 20), vertices, indices);
            SubmeshGeometry cylinder = AppendMeshData(GeometryGenerator.CreateCylinder(0.5f, 0.3f, 3.0f, 20, 20), vertices, indices);

            var geo = MeshGeometry.New(Device, CommandList, vertices, indices.ToArray(), "shapeGeo");

            geo.DrawArgs["box"]      = box;
            geo.DrawArgs["grid"]     = grid;
            geo.DrawArgs["sphere"]   = sphere;
            geo.DrawArgs["cylinder"] = cylinder;

            _geometries[geo.Name] = geo;
        }
        private void BuildSkullGeometry()
        {
            var vertices = new List <Vertex>();
            var indices = new List <int>();
            int vCount = 0, tCount = 0;

            using (var reader = new StreamReader("Models\\Skull.txt"))
            {
                var input = reader.ReadLine();
                if (input != null)
                {
                    vCount = Convert.ToInt32(input.Split(':')[1].Trim());
                }

                input = reader.ReadLine();
                if (input != null)
                {
                    tCount = Convert.ToInt32(input.Split(':')[1].Trim());
                }

                do
                {
                    input = reader.ReadLine();
                } while (input != null && !input.StartsWith("{", StringComparison.Ordinal));

                for (int i = 0; i < vCount; i++)
                {
                    input = reader.ReadLine();
                    if (input != null)
                    {
                        var vals = input.Split(' ');
                        vertices.Add(new Vertex
                        {
                            Pos = new Vector3(
                                Convert.ToSingle(vals[0].Trim(), CultureInfo.InvariantCulture),
                                Convert.ToSingle(vals[1].Trim(), CultureInfo.InvariantCulture),
                                Convert.ToSingle(vals[2].Trim(), CultureInfo.InvariantCulture)),
                            Normal = new Vector3(
                                Convert.ToSingle(vals[3].Trim(), CultureInfo.InvariantCulture),
                                Convert.ToSingle(vals[4].Trim(), CultureInfo.InvariantCulture),
                                Convert.ToSingle(vals[5].Trim(), CultureInfo.InvariantCulture))
                        });
                    }
                }

                do
                {
                    input = reader.ReadLine();
                } while (input != null && !input.StartsWith("{", StringComparison.Ordinal));

                for (var i = 0; i < tCount; i++)
                {
                    input = reader.ReadLine();
                    if (input == null)
                    {
                        break;
                    }
                    var m = input.Trim().Split(' ');
                    indices.Add(Convert.ToInt32(m[0].Trim()));
                    indices.Add(Convert.ToInt32(m[1].Trim()));
                    indices.Add(Convert.ToInt32(m[2].Trim()));
                }
            }

            var geo     = MeshGeometry.New(Device, CommandList, vertices.ToArray(), indices.ToArray(), "skullGeo");
            var submesh = new SubmeshGeometry
            {
                IndexCount         = indices.Count,
                StartIndexLocation = 0,
                BaseVertexLocation = 0
            };

            geo.DrawArgs["skull"] = submesh;

            _geometries[geo.Name] = geo;
        }
        private void BuildSkullGeometry()
        {
            var vertices = new List <Vertex>();
            var indices = new List <int>();
            int vCount = 0, tCount = 0;

            using (var reader = new StreamReader("Models\\Skull.txt"))
            {
                var input = reader.ReadLine();
                if (input != null)
                {
                    vCount = Convert.ToInt32(input.Split(':')[1].Trim());
                }

                input = reader.ReadLine();
                if (input != null)
                {
                    tCount = Convert.ToInt32(input.Split(':')[1].Trim());
                }

                do
                {
                    input = reader.ReadLine();
                } while (input != null && !input.StartsWith("{", StringComparison.Ordinal));

                for (int i = 0; i < vCount; i++)
                {
                    input = reader.ReadLine();
                    if (input != null)
                    {
                        string[] vals = input.Split(' ');

                        var pos = new Vector3(
                            Convert.ToSingle(vals[0].Trim(), CultureInfo.InvariantCulture),
                            Convert.ToSingle(vals[1].Trim(), CultureInfo.InvariantCulture),
                            Convert.ToSingle(vals[2].Trim(), CultureInfo.InvariantCulture));

                        var normal = new Vector3(
                            Convert.ToSingle(vals[3].Trim(), CultureInfo.InvariantCulture),
                            Convert.ToSingle(vals[4].Trim(), CultureInfo.InvariantCulture),
                            Convert.ToSingle(vals[5].Trim(), CultureInfo.InvariantCulture));

                        // Generate a tangent vector so normal mapping works.  We aren't applying
                        // a texture map to the skull, so we just need any tangent vector so that
                        // the math works out to give us the original interpolated vertex normal.
                        Vector3 tangent = Math.Abs(Vector3.Dot(normal, Vector3.Up)) < 1.0f - 0.001f
                            ? Vector3.Normalize(Vector3.Cross(normal, Vector3.Up))
                            : Vector3.Normalize(Vector3.Cross(normal, Vector3.ForwardLH));

                        vertices.Add(new Vertex
                        {
                            Pos      = pos,
                            Normal   = normal,
                            TangentU = tangent
                        });
                    }
                }

                do
                {
                    input = reader.ReadLine();
                } while (input != null && !input.StartsWith("{", StringComparison.Ordinal));

                for (var i = 0; i < tCount; i++)
                {
                    input = reader.ReadLine();
                    if (input == null)
                    {
                        break;
                    }
                    var m = input.Trim().Split(' ');
                    indices.Add(Convert.ToInt32(m[0].Trim()));
                    indices.Add(Convert.ToInt32(m[1].Trim()));
                    indices.Add(Convert.ToInt32(m[2].Trim()));
                }
            }

            var geo     = MeshGeometry.New(Device, CommandList, vertices.ToArray(), indices.ToArray(), "skullGeo");
            var submesh = new SubmeshGeometry
            {
                IndexCount         = indices.Count,
                StartIndexLocation = 0,
                BaseVertexLocation = 0
            };

            geo.DrawArgs["skull"] = submesh;

            _geometries[geo.Name] = geo;
        }
Esempio n. 8
0
        private void BuildLandGeometry()
        {
            GeometryGenerator.MeshData grid = GeometryGenerator.CreateGrid(160.0f, 160.0f, 50, 50);

            //
            // Extract the vertex elements we are interested and apply the height function to
            // each vertex. In addition, color the vertices based on their height so we have
            // sandy looking beaches, grassy low hills, and snow mountain peaks.
            //

            var vertices = new Vertex[grid.Vertices.Count];

            for (int i = 0; i < grid.Vertices.Count; i++)
            {
                Vector3 p = grid.Vertices[i].Position;
                vertices[i].Pos   = p;
                vertices[i].Pos.Y = GetHillsHeight(p.X, p.Z);

                // Color the vertex based on its height.
                if (vertices[i].Pos.Y < -10.0f)
                {
                    // Sandy beach color.
                    vertices[i].Color = new Vector4(1.0f, 0.96f, 0.62f, 1.0f);
                }
                else if (vertices[i].Pos.Y < 5.0f)
                {
                    // Light yellow-green.
                    vertices[i].Color = new Vector4(0.48f, 0.77f, 0.46f, 1.0f);
                }
                else if (vertices[i].Pos.Y < 12.0f)
                {
                    // Dark yellow-green.
                    vertices[i].Color = new Vector4(0.1f, 0.48f, 0.19f, 1.0f);
                }
                else if (vertices[i].Pos.Y < 20.0f)
                {
                    // Dark brown.
                    vertices[i].Color = new Vector4(0.45f, 0.39f, 0.34f, 1.0f);
                }
                else
                {
                    // White snow.
                    vertices[i].Color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
                }
            }

            List <short> indices = grid.GetIndices16();

            var geo = MeshGeometry.New(Device, CommandList, vertices, indices.ToArray(), "landGeo");

            var submesh = new SubmeshGeometry
            {
                IndexCount         = indices.Count,
                StartIndexLocation = 0,
                BaseVertexLocation = 0
            };

            geo.DrawArgs["grid"] = submesh;

            _geometries["landGeo"] = geo;
        }