示例#1
0
        private bool InitializeBuffers(SharpDX.Direct3D11.Device device)
        {
            try
            {
                // Set number of vertices in the vertex array.
                VertexCount = 3;

                // Create the vertex array and load it with data.
                DVertexType[] vertices = new[]
                {
                    // Bottom left.
                    new DVertexType()
                    {
                        position = new Vector3(-1, -1, 0),
                        texture  = new Vector2(0, 1)
                    },
                    // Top middle.
                    new DVertexType()
                    {
                        position = new Vector3(0, 1, 0),
                        texture  = new Vector2(.5f, 0)
                    },
                    // Bottom right.
                    new DVertexType()
                    {
                        position = new Vector3(1, -1, 0),
                        texture  = new Vector2(1, 1)
                    }
                };

                // Set the number of instances in the array.
                InstanceCount = 4;

                DInstanceType[] instances = new DInstanceType[]
                {
                    new DInstanceType()
                    {
                        position = new Vector3(-1.5f, -1.5f, 5.0f)
                    },
                    new DInstanceType()
                    {
                        position = new Vector3(-1.5f, 1.5f, 5.0f)
                    },
                    new DInstanceType()
                    {
                        position = new Vector3(1.5f, -1.5f, 5.0f)
                    },
                    new DInstanceType()
                    {
                        position = new Vector3(1.5f, 1.5f, 5.0f)
                    }
                };

                // Create the vertex buffer.
                VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices);

                // Create the Instance instead of an Index Buffer buffer.
                InstanceBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, instances);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
示例#2
0
        private bool InitializeBuffer(SharpDX.Direct3D11.Device device, int ChunkWidth_L, int ChunkWidth_R, int ChunkHeight_L, int ChunkHeight_R, int ChunkDepth_L, int ChunkDepth_R)
        {
            try
            {
                //Set number of vertices in the vertex array.
                VertexCount = 8;
                // Set number of vertices in the index array.
                IndexCount = 36;


                // Create the vertex array and load it with data.
                Vertices = new[]
                {
                    new SC_VR_Touch_Shader.DVertex()
                    {
                        position = new Vector3(-1 * _sizeX, -1 * _sizeY, 1 * _sizeZ),
                        color    = _color,
                    },
                    new SC_VR_Touch_Shader.DVertex()
                    {
                        position = new Vector3(-1 * _sizeX, 1 * _sizeY, 1 * _sizeZ),
                        color    = _color,
                    },
                    new SC_VR_Touch_Shader.DVertex()
                    {
                        position = new Vector3(1 * _sizeX, -1 * _sizeY, 1 * _sizeZ),
                        color    = _color,
                    },
                    new SC_VR_Touch_Shader.DVertex()
                    {
                        position = new Vector3(1 * _sizeX, 1 * _sizeY, 1 * _sizeZ),
                        color    = _color,
                    },


                    new SC_VR_Touch_Shader.DVertex()
                    {
                        position = new Vector3(-1 * _sizeX, -1 * _sizeY, -1 * _sizeZ),
                        color    = _color,
                    },
                    new SC_VR_Touch_Shader.DVertex()
                    {
                        position = new Vector3(-1 * _sizeX, 1 * _sizeY, -1 * _sizeZ),
                        color    = _color,
                    },
                    new SC_VR_Touch_Shader.DVertex()
                    {
                        position = new Vector3(1 * _sizeX, -1 * _sizeY, -1 * _sizeZ),
                        color    = _color,
                    },
                    new SC_VR_Touch_Shader.DVertex()
                    {
                        position = new Vector3(1 * _sizeX, 1 * _sizeY, -1 * _sizeZ),
                        color    = _color,
                    },
                };

                // Create Indicies to load into the IndexBuffer.
                indices = new int[]
                {
                    0,  // Bottom left.
                    1,  // Top middle.
                    2,  // Bottom right.
                    3,
                    2,
                    1,

                    1,
                    5,
                    3,
                    7,
                    3,
                    5,

                    2,
                    3,
                    6,
                    7,
                    6,
                    3,

                    6,
                    7,
                    4,
                    5,
                    4,
                    7,

                    4,
                    5,
                    0,
                    1,
                    0,
                    5,

                    4,
                    0,
                    6,
                    2,
                    6,
                    0
                };



                /*lowestX = _vertArray.OrderBy(xx => xx.X).FirstOrDefault();
                 * highestX = _vertArray.OrderBy(xx => xx.X).Last();
                 *
                 * lowestY = _vertArray.OrderBy(yy => yy.X).FirstOrDefault();
                 * highestY = _vertArray.OrderBy(yy => yy.X).Last();
                 *
                 * lowestZ = _vertArray.OrderBy(zz => zz.X).FirstOrDefault();
                 * highestZ = _vertArray.OrderBy(zz => zz.X).Last();
                 */


                //var lowestXTopY = _vertArray.OrderBy(xx => xx.X).ThenBy(xx=>xx.Y).FirstOrDefault();
                //var highestXTopY = _vertArray.OrderBy(xx => xx.X).ThenBy(xx => xx.Y).Last();

                /*
                 *
                 * lowestY = _vertArray.OrderBy(yy => yy.X).FirstOrDefault();
                 * highestY = _vertArray.OrderBy(yy => yy.X).Last();
                 *
                 * lowestZ = _vertArray.OrderBy(zz => zz.X).FirstOrDefault();
                 * highestZ = _vertArray.OrderBy(zz => zz.X).Last();
                 */

                //double minX = Math.Min(Math.Min(lowestX.X, highestX.X), Math.Min(lb.X, rb.X));


                _vertArray = new SharpDX.Vector3[Vertices.Length];

                for (int i = 0; i < Vertices.Length; i++)
                {
                    _vertArray[i] = Vertices[i].position;
                }
                var min = _vertArray.OrderBy(xx => xx.X).ThenBy(xx => xx.Y).ThenBy(xx => xx.Z).FirstOrDefault();
                var max = _vertArray.OrderBy(xx => xx.X).ThenBy(xx => xx.Y).ThenBy(xx => xx.Z).Last();

                float sumX = 0;
                float sumY = 0;
                float sumZ = 0;

                for (int i = 0; i < Vertices.Length; i++)
                {
                    sumX += Vertices[i].position.X;
                    sumY += Vertices[i].position.Y;
                    sumZ += Vertices[i].position.Z;
                }

                sumX /= Vertices.Length;
                sumY /= Vertices.Length;
                sumZ /= Vertices.Length;

                var center = new Vector3(sumX, sumY, sumZ);
                _boundingBox = new boundingBox(center, min, max);



                //_boundingBox.center = center;
                //_boundingBox.min = min;
                //_boundingBox.max = max;

                //_boundingBox = new SharpDX.BoundingBox(min,max);



                //double minX = Math.Min(Math.Min(lt.X, rt.X), Math.Min(lb.X, rb.X));
                //double minY = Math.Min(Math.Min(lt.Y, rt.Y), Math.Min(lb.Y, rb.Y));

                //double maxX = Math.Max(Math.Max(lt.X, rt.X), Math.Max(lb.X, rb.X));
                //double maxY = Math.Max(Math.Max(lt.Y, rt.Y), Math.Max(lb.Y, rb.Y));



                int width  = 4;
                int heigth = 4;
                int depth  = 4;

                InstanceCount = width * heigth * depth;
                DInstanceType[] instances = new DInstanceType[width * heigth * depth];
                _LASTPOSITIONS = new Matrix[width * heigth * depth];

                int counter = 0;


                for (int x = -ChunkWidth_L; x <= ChunkWidth_R; x++)
                {
                    for (int y = -ChunkHeight_L; y <= ChunkHeight_R; y++)
                    {
                        for (int z = -ChunkDepth_L; z <= ChunkDepth_R; z++)
                        {
                            float posX = (x);
                            float posY = (y);
                            float posZ = (z);

                            var xxi = x;
                            var yyi = y;
                            var zzi = z;

                            if (xxi < 0)
                            {
                                xxi *= -1;
                                xxi  = (ChunkWidth_R) + xxi;
                            }
                            if (yyi < 0)
                            {
                                yyi *= -1;
                                yyi  = (ChunkHeight_R) + yyi;
                            }
                            if (zzi < 0)
                            {
                                zzi *= -1;
                                zzi  = (ChunkDepth_R) + zzi;
                            }

                            int _index = xxi + (ChunkWidth_L + ChunkWidth_R + 1) * (yyi + (ChunkHeight_L + ChunkHeight_R + 1) * zzi);


                            instances[counter] = new DInstanceType()
                            {
                                position = new Vector3(x, y, z),
                            };
                            _LASTPOSITIONS[counter]     = Matrix.Identity;
                            _LASTPOSITIONS[counter].M41 = instances[counter].position.X;
                            _LASTPOSITIONS[counter].M42 = instances[counter].position.Y;
                            _LASTPOSITIONS[counter].M43 = instances[counter].position.Z;


                            counter++;
                        }
                    }
                }

                // Create the vertex buffer.
                VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, Vertices);

                // Create the index buffer.
                IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices);

                InstanceBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, instances);
                //InstanceBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, instances, Utilities.SizeOf<DInstanceType>() * instances.Length, ResourceUsage.Dynamic, CpuAccessFlags.Write, ResourceOptionFlags.None, 0);

                // Delete arrays now that they are in their respective vertex and index buffers.
                //Vertices = null;
                //indices = null;

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        private bool InitializeBuffers(Device device)
        {
            // Set the number of vertices in the vertex array.
            VertexCount = 6;

            // Create the vertex array.
            DVertexType[] vertices = new DVertexType[VertexCount];

            // Load the vertex array with data.
            vertices[0].position = new Vector3(0.0f, 0.0f, 0.0f);  // Bottom left.
            vertices[0].texture  = new Vector2(0.0f, 1.0f);
            vertices[1].position = new Vector3(0.0f, 1.0f, 0.0f);  // Top left.
            vertices[1].texture  = new Vector2(0.0f, 0.0f);
            vertices[2].position = new Vector3(1.0f, 0.0f, 0.0f);  // Bottom right.
            vertices[2].texture  = new Vector2(1.0f, 1.0f);
            vertices[3].position = new Vector3(1.0f, 0.0f, 0.0f);  // Bottom right.
            vertices[3].texture  = new Vector2(1.0f, 1.0f);
            vertices[4].position = new Vector3(0.0f, 1.0f, 0.0f);  // Top left.
            vertices[4].texture  = new Vector2(0.0f, 0.0f);
            vertices[5].position = new Vector3(1.0f, 1.0f, 0.0f);  // Top right.
            vertices[5].texture  = new Vector2(1.0f, 0.0f);

            // Set up the description of the vertex buffer.
            // Now finally create the vertex buffer.
            VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices);

            // Release the array now that the vertex buffer has been created and loaded.
            vertices = null;

            // Set the number of instances in the array.
            InstanceCount = FoliageCount;

            // Create the instance array.
            Instances = new DInstanceType[InstanceCount];

            // Setup an initial matrix.
            Matrix matrix = Matrix.Identity;

            // Load the instance array with data.
            for (int i = 0; i < InstanceCount; i++)
            {
                Instances[i].matrix = matrix;
                Instances[i].color  = new Vector3(FoliageArray[i].r, FoliageArray[i].g, FoliageArray[i].b);
            }

            // Set up the description of the Dynamic instance buffer.
            BufferDescription instanceBufferDesc = new BufferDescription()
            {
                Usage               = ResourceUsage.Dynamic,
                SizeInBytes         = Utilities.SizeOf <DInstanceType>() * InstanceCount,
                BindFlags           = BindFlags.VertexBuffer,
                CpuAccessFlags      = CpuAccessFlags.Write,
                OptionFlags         = ResourceOptionFlags.None,
                StructureByteStride = 0
            };

            // Create the instance buffer.
            InstanceBuffer = SharpDX.Direct3D11.Buffer.Create(device, Instances, instanceBufferDesc);  /// C+ Way

            return(true);
        }