Exemple #1
0
        private void GenerateGeometry()
        {
            float spacing = 1000.0f;

            // Create vertices
            numVertices = numRows * numCols;
            numIndices  = 3 * ((numRows - 1) * (numCols - 1) * 2);

            VertexPosTex[] vertices  = new VertexPosTex[numVertices];
            float          halfWidth = (float)(numRows - 1) * spacing * 0.5f;
            float          halfDepth = (float)(numCols - 1) * spacing * 0.5f;
            float          du        = 1.0f / (numRows - 1);
            float          dv        = 1.0f / (numCols - 1);

            for (int i = 0; i < numRows; i++)
            {
                float z = (halfDepth - i * spacing) * -1.0f;
                for (int j = 0; j < numCols; j++)
                {
                    float x = -halfWidth + j * spacing;
                    float y = height;

                    vertices[i * numCols + j].pos   = new Vector3(x, y, z);
                    vertices[i * numCols + j].tex.X = du * j * textureScale;
                    vertices[i * numCols + j].tex.Y = dv * i * textureScale;
                }
            }

            // Create indices
            int[] indices = new int[numIndices];

            int k = 0;

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

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

                    k += 6;
                }
            }

            // Create Vertex Buffer
            vertexBuffer = new VertexBuffer(device, VertexPosTex.VertexLayout,
                                            numVertices, BufferUsage.WriteOnly);
            vertexBuffer.SetData(vertices);

            // Create Index Buffer
            indexBuffer = new IndexBuffer(device, IndexElementSize.ThirtyTwoBits,
                                          numIndices, BufferUsage.WriteOnly);
            indexBuffer.SetData(indices);
        }
Exemple #2
0
        private void GenerateGeometry()
        {
            // Create vertices
            VertexPosTex[] vertices = new VertexPosTex[4];
            vertices[0] = new VertexPosTex(new Vector3(-size.X, -size.Y, 0.0f), new Vector2(0.0f, 1.0f));
            vertices[1] = new VertexPosTex(new Vector3(-size.X, +size.Y, 0.0f), new Vector2(0.0f, 0.0f));
            vertices[2] = new VertexPosTex(new Vector3(+size.X, -size.Y, 0.0f), new Vector2(1.0f, 1.0f));
            vertices[3] = new VertexPosTex(new Vector3(+size.X, +size.Y, 0.0f), new Vector2(1.0f, 0.0f));

            vertexBuffer = new VertexBuffer(device, VertexPosTex.VertexLayout,
                                            vertices.Length, BufferUsage.WriteOnly);
            vertexBuffer.SetData(vertices);

            // Create AABB
            aabb = new BoundingBox(new Vector3(position.X - size.X, position.Y - size.Y, position.Z - size.X),
                                   new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.X));
        }
Exemple #3
0
        private void GenerateLight()
        {
            // Create light vertices
            VertexPosTex[] vertices = new VertexPosTex[]
            {
                new VertexPosTex(new Vector3(-1.0f, -1.0f, 0.0f), new Vector2(0.0f, 1.0f)),
                new VertexPosTex(new Vector3(-1.0f, 1.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new VertexPosTex(new Vector3(1.0f, -1.0f, 0.0f), new Vector2(1.0f, 1.0f)),
                new VertexPosTex(new Vector3(1.0f, 1.0f, 0.0f), new Vector2(1.0f, 0.0f))
            };

            // Create vertex buffer
            vertexBuffer = new VertexBuffer(device, VertexPosTex.VertexLayout,
                                            vertices.Length, BufferUsage.WriteOnly);
            vertexBuffer.SetData(vertices);

            BuildAABB();
        }
        private void GenerateVertices()
        {
            // Create full screen quad vertices in screen space
            VertexPosTex[] vertices = new VertexPosTex[]
            {
                new VertexPosTex(new Vector3(-1.0f, -1.0f, 0.0f), new Vector2(0.0f, 1.0f)),
                new VertexPosTex(new Vector3(-1.0f, 1.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new VertexPosTex(new Vector3(1.0f, -1.0f, 0.0f), new Vector2(1.0f, 1.0f)),

                new VertexPosTex(new Vector3(1.0f, -1.0f, 0.0f), new Vector2(1.0f, 1.0f)),
                new VertexPosTex(new Vector3(-1.0f, 1.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new VertexPosTex(new Vector3(1.0f, 1.0f, 0.0f), new Vector2(1.0f, 0.0f))
            };

            // Create vertex buffer
            vertexBuffer = new VertexBuffer(device, VertexPosTex.VertexLayout,
                                            vertices.Length, BufferUsage.WriteOnly);
            vertexBuffer.SetData(vertices);
        }
Exemple #5
0
        private void GenerateGeometry()
        {
            int numStacks = 30;
            int numSlices = 30;

            List <VertexPosTex> vertexList = new List <VertexPosTex>();
            List <int>          indexList  = new List <int>();

            float phiStep = (float)(MathHelper.Pi / numStacks);

            // do not count the poles as rings
            int numRings = numStacks - 1;

            // Compute vertices for each stack ring.
            for (int i = 1; i <= numRings; ++i)
            {
                float phi = i * phiStep;

                // vertices of ring
                float thetaStep = 2.0f * (float)(MathHelper.Pi / numSlices);
                for (int j = 0; j <= numSlices; ++j)
                {
                    float theta = j * thetaStep;

                    VertexPosTex v = new VertexPosTex();

                    // spherical to cartesian
                    v.pos.X = radius * (float)Math.Sin(phi) * (float)Math.Cos(theta);
                    v.pos.Y = radius * (float)Math.Cos(phi);
                    v.pos.Z = radius * (float)Math.Sin(phi) * (float)Math.Sin(theta);

                    v.tex.X = theta / (2.0f * MathHelper.Pi);
                    v.tex.Y = phi / MathHelper.Pi;

                    vertexList.Add(v);
                }
            }

            // poles: note that there will be texture coordinate distortion
            vertexList.Add(new VertexPosTex(new Vector3(0.0f, -radius, 0.0f),
                                            new Vector2(0.0f, 1.0f)));
            vertexList.Add(new VertexPosTex(new Vector3(0.0f, radius, 0.0f),
                                            new Vector2(0.0f, 0.0f)));

            int northPoleIndex = vertexList.ToArray().Length - 1;
            int southPoleIndex = vertexList.ToArray().Length - 2;

            int numRingVertices = numSlices + 1;

            // Compute indices for inner stacks (not connected to poles).
            for (int i = 0; i < numStacks - 2; ++i)
            {
                for (int j = 0; j < numSlices; ++j)
                {
                    indexList.Add(i * numRingVertices + j);
                    indexList.Add(i * numRingVertices + j + 1);
                    indexList.Add((i + 1) * numRingVertices + j);

                    indexList.Add((i + 1) * numRingVertices + j);
                    indexList.Add(i * numRingVertices + j + 1);
                    indexList.Add((i + 1) * numRingVertices + j + 1);
                }
            }

            // Compute indices for top stack.  The top stack was written
            // first to the vertex buffer.
            for (int i = 0; i < numSlices; ++i)
            {
                indexList.Add(northPoleIndex);
                indexList.Add(i + 1);
                indexList.Add(i);
            }

            // Compute indices for bottom stack.  The bottom stack was written
            // last to the vertex buffer, so we need to offset to the index
            // of first vertex in the last ring.
            int baseIndex = (numRings - 1) * numRingVertices;

            for (int i = 0; i < numSlices; ++i)
            {
                indexList.Add(southPoleIndex);
                indexList.Add(baseIndex + i);
                indexList.Add(baseIndex + i + 1);
            }

            VertexPosTex[] vertices = vertexList.ToArray();
            int[]          indices  = indexList.ToArray();

            numVertices = vertices.Length;
            numIndices  = indices.Length;

            // Create vertex buffer
            vertexBuffer = new VertexBuffer(device, VertexPosTex.VertexLayout,
                                            vertices.Length, BufferUsage.WriteOnly);
            vertexBuffer.SetData(vertices);

            // Create index buffer
            indexBuffer = new IndexBuffer(device, IndexElementSize.ThirtyTwoBits,
                                          indices.Length, BufferUsage.WriteOnly);
            indexBuffer.SetData(indices);
        }