コード例 #1
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);
                vertices[i].Normal = GetHillsNormal(p.X, p.Z);
            }

            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;
        }
コード例 #2
0
        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);
            SubmeshGeometry quad     = AppendMeshData(GeometryGenerator.CreateQuad(0.0f, 0.0f, 1.0f, 1.0f, 0.0f), vertices, indices);

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

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

            _geometries[geo.Name] = geo;
        }
コード例 #3
0
        private void BuildBoxGeometry()
        {
            GeometryGenerator.MeshData box = GeometryGenerator.CreateBox(8.0f, 8.0f, 8.0f, 3);

            var boxSubmesh = new SubmeshGeometry
            {
                IndexCount         = box.Indices32.Count,
                StartIndexLocation = 0,
                BaseVertexLocation = 0
            };

            Vertex[] vertices = box.Vertices.Select(x => new Vertex
            {
                Pos    = x.Position,
                Normal = x.Normal,
                TexC   = x.TexC
            }).ToArray();

            short[] indices = box.GetIndices16().ToArray();

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

            geo.DrawArgs["box"] = boxSubmesh;

            _geometries[geo.Name] = geo;
        }
コード例 #4
0
        private void BuildRenderItems()
        {
            MeshGeometry    geo     = _geometries["carGeo"];
            SubmeshGeometry submesh = geo.DrawArgs["car"];

            var carRitem = new RenderItem
            {
                World              = Matrix.Translation(0.0f, 1.0f, 0.0f),
                ObjCBIndex         = 0,
                Mat                = _materials["gray0"],
                Geo                = geo,
                IndexCount         = submesh.IndexCount,
                StartIndexLocation = submesh.StartIndexLocation,
                BaseVertexLocation = submesh.BaseVertexLocation,
                Bounds             = submesh.Bounds
            };

            _ritemLayer[RenderLayer.Opaque].Add(carRitem);
            _allRitems.Add(carRitem);

            _pickedRitem = new RenderItem
            {
                ObjCBIndex = 1,
                Mat        = _materials["highlight0"],
                Geo        = geo,
                // Picked triangle is not visible until one is picked.
                Visible = false,
                // DrawCall parameters are filled out when a triangle is picked.
                IndexCount         = 0,
                StartIndexLocation = 0,
                BaseVertexLocation = 0
            };
            _ritemLayer[RenderLayer.Highlight].Add(_pickedRitem);
            _allRitems.Add(_pickedRitem);
        }
コード例 #5
0
        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.5f, 0.5f, 1.5f, 3), Color.DarkGreen, vertices, indices);
            SubmeshGeometry grid     = AppendMeshData(GeometryGenerator.CreateGrid(50.0f, 15.0f, 2, 40), Color.ForestGreen, vertices, indices);
            SubmeshGeometry sphere   = AppendMeshData(GeometryGenerator.CreateSphere(0.5f, 20, 20), Color.Crimson, vertices, indices);
            SubmeshGeometry cylinder = AppendMeshData(GeometryGenerator.CreateCylinder(0.5f, 0.3f, 3.0f, 20, 20), Color.SteelBlue, vertices, indices);
            SubmeshGeometry pyramid  = AppendMeshData(GeometryGenerator.Pyramid(1.5f, 0.5f, 1.5f, 3), Color.Purple, vertices, indices);
            SubmeshGeometry wedge    = AppendMeshData(GeometryGenerator.Wedge(1.5f, 0.5f, 1.5f, 3), Color.RosyBrown, vertices, indices);
            SubmeshGeometry diamond  = AppendMeshData(GeometryGenerator.Diamond(1.5f, 0.5f, 1.5f, 3), Color.MintCream, vertices, indices);
            SubmeshGeometry triPrism = AppendMeshData(GeometryGenerator.TriangularPrism(1.5f, 0.5f, 1.5f, 3), Color.DarkOrchid, vertices, indices);
            SubmeshGeometry hexPrism = AppendMeshData(GeometryGenerator.HexagonalPrism(1.5f, 0.5f, 1.5f, 3), Color.RoyalBlue, vertices, indices);
            SubmeshGeometry cone     = AppendMeshData(GeometryGenerator.CreateCone(0.5f, 0.3f, 3.0f, 20, 20), Color.SteelBlue, 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;
            geo.DrawArgs["pyramid"]  = pyramid;
            geo.DrawArgs["wedge"]    = wedge;
            geo.DrawArgs["diamond"]  = diamond;
            geo.DrawArgs["triPrism"] = triPrism;
            geo.DrawArgs["hexPrism"] = hexPrism;
            geo.DrawArgs["cone"]     = cone;
            _geometries[geo.Name]    = geo;
        }
コード例 #6
0
        private void BuildBoxGeometry()
        {
            Vertex[] vertices =
            {
                new Vertex {
                    Pos = new Vector3(-1.0f, -1.0f, -1.0f), Color = Color.White.ToVector4()
                },
                new Vertex {
                    Pos = new Vector3(-1.0f, +1.0f, -1.0f), Color = Color.Black.ToVector4()
                },
                new Vertex {
                    Pos = new Vector3(+1.0f, +1.0f, -1.0f), Color = Color.Red.ToVector4()
                },
                new Vertex {
                    Pos = new Vector3(+1.0f, -1.0f, -1.0f), Color = Color.Green.ToVector4()
                },
                new Vertex {
                    Pos = new Vector3(-1.0f, -1.0f, +1.0f), Color = Color.Blue.ToVector4()
                },
                new Vertex {
                    Pos = new Vector3(-1.0f, +1.0f, +1.0f), Color = Color.Yellow.ToVector4()
                },
                new Vertex {
                    Pos = new Vector3(+1.0f, +1.0f, +1.0f), Color = Color.Cyan.ToVector4()
                },
                new Vertex {
                    Pos = new Vector3(+1.0f, -1.0f, +1.0f), Color = Color.Magenta.ToVector4()
                }
            };

            short[] indices =
            {
                // front face
                0, 1, 2,
                0, 2, 3,

                // back face
                4, 6, 5,
                4, 7, 6,

                // left face
                4, 5, 1,
                4, 1, 0,

                // right face
                3, 2, 6,
                3, 6, 7,

                // top face
                1, 5, 6,
                1, 6, 2,

                // bottom face
                4, 0, 3,
                4, 3, 7
            };

            _boxGeo = MeshGeometry.New(Device, CommandList, vertices, indices);
        }
コード例 #7
0
        private void BuildRenderItems()
        {
            MeshGeometry    geo        = _geometries["skullGeo"];
            SubmeshGeometry submesh    = geo.DrawArgs["skull"];
            var             skullRitem = new RenderItem
            {
                ObjCBIndex         = 0,
                Mat                = _materials["tile0"],
                Geo                = _geometries["skullGeo"],
                IndexCount         = submesh.IndexCount,
                StartIndexLocation = submesh.StartIndexLocation,
                BaseVertexLocation = submesh.BaseVertexLocation,
                Bounds             = submesh.Bounds
            };
            // Instance count for the render item is set during update based on frustum culling.

            // Generate instance data.
            const int n = 5;

            skullRitem.Instances = new InstanceData[n * n * n];

            const float width  = 200.0f;
            const float height = 200.0f;
            const float depth  = 200.0f;

            const float x  = -0.5f * width;
            const float y  = -0.5f * height;
            const float z  = -0.5f * depth;
            const float dx = width / (n - 1);
            const float dy = height / (n - 1);
            const float dz = depth / (n - 1);

            for (int k = 0; k < n; k++)
            {
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        int index = k * n * n + i * n + j;
                        // Position instanced along a 3D grid.
                        skullRitem.Instances[index].World = new Matrix(
                            1.0f, 0.0f, 0.0f, 0.0f,
                            0.0f, 1.0f, 0.0f, 0.0f,
                            0.0f, 0.0f, 1.0f, 0.0f,
                            x + j * dx, y + i * dy, z + k * dz, 1.0f);

                        skullRitem.Instances[index].TexTransform  = Matrix.Scaling(2.0f, 2.0f, 1.0f);
                        skullRitem.Instances[index].MaterialIndex = index % _materials.Count;
                    }
                }
            }

            _allRitems.Add(skullRitem);

            // All the render items are opaque.
            _ritemLayers[RenderLayer.Opaque].AddRange(_allRitems);
        }
コード例 #8
0
        private void BuildWavesGeometry()
        {
            GeometryGenerator.MeshData grid = GeometryGenerator.CreateGrid(160.0f, 160.0f, _waves.RowCount, _waves.ColumnCount);

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

            for (int i = 0; i < grid.Vertices.Count; i++)
            {
                vertices[i].Pos    = grid.Vertices[i].Position;
                vertices[i].Normal = grid.Vertices[i].Normal;
                vertices[i].TexC   = grid.Vertices[i].TexC;
            }

            var indices = new int[3 * _waves.TriangleCount]; // 3 indices per face.

            Debug.Assert(_waves.VertexCount < int.MaxValue);

            // Iterate over each quad.
            int m = _waves.RowCount;
            int n = _waves.ColumnCount;
            int k = 0;

            for (int i = 0; i < m - 1; ++i)
            {
                for (int j = 0; j < n - 1; ++j)
                {
                    indices[k + 0] = i * n + j;
                    indices[k + 1] = i * n + j + 1;
                    indices[k + 2] = (i + 1) * n + j;

                    indices[k + 3] = (i + 1) * n + j;
                    indices[k + 4] = i * n + j + 1;
                    indices[k + 5] = (i + 1) * n + j + 1;

                    k += 6; // Next quad.
                }
            }

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

            geo.VertexByteStride     = Utilities.SizeOf <Vertex>();
            geo.VertexBufferByteSize = geo.VertexByteStride * _waves.VertexCount;

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

            geo.DrawArgs["grid"] = submesh;

            _geometries["waterGeo"] = geo;
        }
コード例 #9
0
        private void BuildQuadPatchGeometry()
        {
            Vector3[] vertices =
            {
                // Row 0
                new Vector3(-10.0f, -10.0f, +15.0f),
                new Vector3(-5.0f,    0.0f, +15.0f),
                new Vector3(+5.0f,    0.0f, +15.0f),
                new Vector3(+10.0f,   0.0f, +15.0f),

                // Row 1
                new Vector3(-15.0f,   0.0f,  +5.0f),
                new Vector3(-5.0f,    0.0f,  +5.0f),
                new Vector3(+5.0f,   20.0f,  +5.0f),
                new Vector3(+15.0f,   0.0f,  +5.0f),

                // Row 2
                new Vector3(-15.0f,   0.0f,  -5.0f),
                new Vector3(-5.0f,    0.0f,  -5.0f),
                new Vector3(+5.0f,    0.0f,  -5.0f),
                new Vector3(+15.0f,   0.0f,  -5.0f),

                // Row 3
                new Vector3(-10.0f,  10.0f, -15.0f),
                new Vector3(-5.0f,    0.0f, -15.0f),
                new Vector3(+5.0f,    0.0f, -15.0f),
                new Vector3(+25.0f,  10.0f, -15.0f)
            };

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

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

            geo.DrawArgs["quadpatch"] = new SubmeshGeometry
            {
                IndexCount         = indices.Length,
                StartIndexLocation = 0,
                BaseVertexLocation = 0
            };

            _geometries[geo.Name] = geo;
        }
コード例 #10
0
        private RenderItem AddRenderItem(RenderLayer layer, int objCBIndex, string geoName, string submeshName)
        {
            MeshGeometry    geo        = _geometries[geoName];
            SubmeshGeometry submesh    = geo.DrawArgs[submeshName];
            var             renderItem = new RenderItem
            {
                ObjCBIndex         = objCBIndex,
                Geo                = geo,
                IndexCount         = submesh.IndexCount,
                StartIndexLocation = submesh.StartIndexLocation,
                BaseVertexLocation = submesh.BaseVertexLocation
            };

            _ritemLayers[layer].Add(renderItem);
            _allRitems.Add(renderItem);
            return(renderItem);
        }
コード例 #11
0
        private void AddRenderItem(RenderLayer layer, int objCBIndex, string geoName, string submeshName, Matrix?world = null)
        {
            MeshGeometry    geo        = _geometries[geoName];
            SubmeshGeometry submesh    = geo.DrawArgs[submeshName];
            var             renderItem = new RenderItem
            {
                ObjCBIndex         = objCBIndex,
                Geo                = geo,
                IndexCount         = submesh.IndexCount,
                StartIndexLocation = submesh.StartIndexLocation,
                BaseVertexLocation = submesh.BaseVertexLocation,
                World              = world ?? Matrix.Identity
            };

            _ritemLayers[layer].Add(renderItem);
            _allRitems.Add(renderItem);
        }
コード例 #12
0
        private void BuildWavesGeometry()
        {
            var indices = new short[3 * _waves.TriangleCount]; // 3 indices per face.

            Debug.Assert(_waves.VertexCount < short.MaxValue);

            // Iterate over each quad.
            int m = _waves.RowCount;
            int n = _waves.ColumnCount;
            int k = 0;

            for (int i = 0; i < m - 1; ++i)
            {
                for (int j = 0; j < n - 1; ++j)
                {
                    indices[k + 0] = (short)(i * n + j);
                    indices[k + 1] = (short)(i * n + j + 1);
                    indices[k + 2] = (short)((i + 1) * n + j);

                    indices[k + 3] = (short)((i + 1) * n + j);
                    indices[k + 4] = (short)(i * n + j + 1);
                    indices[k + 5] = (short)((i + 1) * n + j + 1);

                    k += 6; // Next quad.
                }
            }

            // Vertices are set dynamically.
            var geo = MeshGeometry.New(Device, CommandList, indices, "waterGeo");

            geo.VertexByteStride     = Utilities.SizeOf <Vertex>();
            geo.VertexBufferByteSize = geo.VertexByteStride * _waves.VertexCount;

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

            geo.DrawArgs["grid"] = submesh;

            _geometries["waterGeo"] = geo;
        }
コード例 #13
0
        private void BuildRenderItems()
        {
            MeshGeometry    geo      = _geometries["boxGeo"];
            SubmeshGeometry submesh  = geo.DrawArgs["box"];
            var             boxRitem = new RenderItem
            {
                ObjCBIndex         = 0,
                Mat                = _materials["woodCrate"],
                Geo                = geo,
                PrimitiveType      = PrimitiveTopology.TriangleList,
                IndexCount         = submesh.IndexCount,
                StartIndexLocation = submesh.StartIndexLocation,
                BaseVertexLocation = submesh.BaseVertexLocation
            };

            _allRitems.Add(boxRitem);
            // All the render items are opaque.
            _ritemLayers[RenderLayer.Opaque].AddRange(_allRitems);
        }
コード例 #14
0
        private void BuildRenderItems()
        {
            MeshGeometry    geo            = _geometries["quadpatchGeo"];
            SubmeshGeometry submesh        = geo.DrawArgs["quadpatch"];
            var             quadPatchRitem = new RenderItem
            {
                World              = Matrix.Identity,
                TexTransform       = Matrix.Identity,
                ObjCBIndex         = 0,
                Mat                = _materials["whiteMat"],
                Geo                = geo,
                PrimitiveType      = PrimitiveTopology.PatchListWith16ControlPoints,
                IndexCount         = submesh.IndexCount,
                StartIndexLocation = submesh.StartIndexLocation,
                BaseVertexLocation = submesh.BaseVertexLocation
            };

            _ritemLayers[RenderLayer.Opaque].Add(quadPatchRitem);
            _allRitems.Add(quadPatchRitem);
        }
コード例 #15
0
        private RenderItem AddRenderItem(RenderLayer layer, int objCBIndex, string matName, string geoName, string submeshName,
                                         Matrix?world = null, Matrix?texTransform = null, PrimitiveTopology topology = PrimitiveTopology.TriangleList)
        {
            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,
                PrimitiveType      = topology
            };

            _ritemLayers[layer].Add(renderItem);
            _allRitems.Add(renderItem);
            return(renderItem);
        }
コード例 #16
0
        private void BuildQuadPatchGeometry()
        {
            Vector3[] vertices =
            {
                new Vector3(-10.0f, 0.0f, +10.0f),
                new Vector3(+10.0f, 0.0f, +10.0f),
                new Vector3(-10.0f, 0.0f, -10.0f),
                new Vector3(+10.0f, 0.0f, -10.0f)
            };

            short[] indices = { 0, 1, 2, 3 };

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

            geo.DrawArgs["quadpatch"] = new SubmeshGeometry
            {
                IndexCount         = indices.Length,
                StartIndexLocation = 0,
                BaseVertexLocation = 0
            };

            _geometries[geo.Name] = geo;
        }
コード例 #17
0
        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;
        }
コード例 #18
0
        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);
        }
コード例 #19
0
        private void Pick(Point sp)
        {
            Ray ray = _camera.GetPickingRay(sp, ClientWidth, ClientHeight);

            // Assume nothing is picked to start, so the picked render-item is invisible.
            _pickedRitem.Visible = false;

            // Check if we picked an opaque render item.  A real app might keep a separate "picking list"
            // of objects that can be selected.
            foreach (RenderItem ri in _ritemLayer[RenderLayer.Opaque])
            {
                MeshGeometry geo = ri.Geo;

                // Skip invisible render-items.
                if (ri.Visible == false)
                {
                    continue;
                }

                // Transform the picking ray into the object space of the mesh.

                Matrix invWorld = Matrix.Invert(ri.World);
                ray.Direction = Vector3.TransformNormal(ray.Direction, invWorld);
                ray.Position  = Vector3.TransformCoordinate(ray.Position, invWorld);

                // If we hit the bounding box of the Mesh, then we might have picked a Mesh triangle,
                // so do the ray/triangle tests.
                //
                // If we did not hit the bounding box, then it is impossible that we hit
                // the Mesh, so do not waste effort doing ray/triangle tests.
                float       tmin;
                BoundingBox bounds = ri.Bounds;
                if (ray.Intersects(ref bounds, out tmin))
                {
                    // NOTE: For the demo, we know what to cast the vertex/index data to. If we were mixing
                    // formats, some metadata would be needed to figure out what to cast it to.
                    var vertices = (Vertex[])geo.VertexBufferCPU;
                    var indices  = (int[])geo.IndexBufferCPU;
                    int triCount = ri.IndexCount / 3;

                    // Find the nearest ray/triangle intersection.
                    tmin = float.MaxValue;
                    for (int i = 0; i < triCount; ++i)
                    {
                        // Indices for this triangle.
                        int i0 = indices[i * 3 + 0];
                        int i1 = indices[i * 3 + 1];
                        int i2 = indices[i * 3 + 2];

                        // Vertices for this triangle.
                        Vector3 v0 = vertices[i0].Pos;
                        Vector3 v1 = vertices[i1].Pos;
                        Vector3 v2 = vertices[i2].Pos;

                        // We have to iterate over all the triangles in order to find the nearest intersection.
                        float t;
                        if (ray.Intersects(ref v0, ref v1, ref v2, out t))
                        {
                            if (t < tmin)
                            {
                                // This is the new nearest picked triangle.
                                tmin = t;
                                int pickedTriangle = i;

                                _pickedRitem.Visible            = true;
                                _pickedRitem.IndexCount         = 3;
                                _pickedRitem.BaseVertexLocation = 0;

                                // Picked render item needs same world matrix as object picked.
                                _pickedRitem.World          = ri.World;
                                _pickedRitem.NumFramesDirty = NumFrameResources;

                                // Offset to the picked triangle in the mesh index buffer.
                                _pickedRitem.StartIndexLocation = 3 * pickedTriangle;
                            }
                        }
                    }
                }
            }
        }
コード例 #20
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;
        }
コード例 #21
0
        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;
        }
コード例 #22
0
        private void BuildRoomGeometry()
        {
            // Create and specify geometry.  For this sample we draw a floor
            // and a wall with a mirror on it.  We put the floor, wall, and
            // mirror geometry in one vertex buffer.
            //
            //   |--------------|
            //   |              |
            //   |----|----|----|
            //   |Wall|Mirr|Wall|
            //   |    | or |    |
            //   /--------------/
            //  /   Floor      /
            // /--------------/

            Vertex[] vertices =
            {
                // Floor: Observe we tile texture coordinates.
                new Vertex(-3.5f, 0.0f, -10.0f, 0.0f, 1.0f,  0.0f, 0.0f, 4.0f), // 0
                new Vertex(-3.5f, 0.0f,   0.0f, 0.0f, 1.0f,  0.0f, 0.0f, 0.0f),
                new Vertex(7.5f,  0.0f,   0.0f, 0.0f, 1.0f,  0.0f, 4.0f, 0.0f),
                new Vertex(7.5f,  0.0f, -10.0f, 0.0f, 1.0f,  0.0f, 4.0f, 4.0f),

                // Wall: Observe we tile texture coordinates, and that we
                // leave a gap in the middle for the mirror.
                new Vertex(-3.5f, 0.0f,   0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 2.0f), // 4
                new Vertex(-3.5f, 4.0f,   0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f),
                new Vertex(-2.5f, 4.0f,   0.0f, 0.0f, 0.0f, -1.0f, 0.5f, 0.0f),
                new Vertex(-2.5f, 0.0f,   0.0f, 0.0f, 0.0f, -1.0f, 0.5f, 2.0f),

                new Vertex(2.5f,  0.0f,   0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 2.0f), // 8
                new Vertex(2.5f,  4.0f,   0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f),
                new Vertex(7.5f,  4.0f,   0.0f, 0.0f, 0.0f, -1.0f, 2.0f, 0.0f),
                new Vertex(7.5f,  0.0f,   0.0f, 0.0f, 0.0f, -1.0f, 2.0f, 2.0f),

                new Vertex(-3.5f, 4.0f,   0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f), // 12
                new Vertex(-3.5f, 6.0f,   0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f),
                new Vertex(7.5f,  6.0f,   0.0f, 0.0f, 0.0f, -1.0f, 6.0f, 0.0f),
                new Vertex(7.5f,  4.0f,   0.0f, 0.0f, 0.0f, -1.0f, 6.0f, 1.0f),

                // Mirror
                new Vertex(-2.5f, 0.0f,   0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f), // 16
                new Vertex(-2.5f, 4.0f,   0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f),
                new Vertex(2.5f,  4.0f,   0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f),
                new Vertex(2.5f,  0.0f,   0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f)
            };

            short[] indices =
            {
                // Floor
                0,   1,  2,
                0,   2,  3,

                // Walls
                4,   5,  6,
                4,   6,  7,

                8,   9, 10,
                8,  10, 11,

                12, 13, 14,
                12, 14, 15,

                // Mirror
                16, 17, 18,
                16, 18, 19
            };

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

            geo.DrawArgs["floor"] = new SubmeshGeometry
            {
                IndexCount         = 6,
                StartIndexLocation = 0,
                BaseVertexLocation = 0
            };
            geo.DrawArgs["wall"] = new SubmeshGeometry
            {
                IndexCount         = 18,
                StartIndexLocation = 6,
                BaseVertexLocation = 0
            };
            geo.DrawArgs["mirror"] = new SubmeshGeometry
            {
                IndexCount         = 6,
                StartIndexLocation = 24,
                BaseVertexLocation = 0
            };

            _geometries[geo.Name] = geo;
        }
コード例 #23
0
        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;
        }