예제 #1
0
    public static Mesh CreateSphere(float radius, int sliceCount, int stackCount)
    {	    

        Mesh mesh = new Mesh();

        List<VertexDescription.PosNormVertex> vertices = new List<VertexDescription.PosNormVertex>();
        List<int> indices = new List<int>();
        

	    //
	    // Compute the vertices stating at the top pole and moving down the stacks.
	    //

	    // Poles: note that there will be texture coordinate distortion as there is
	    // not a unique point on the texture map to assign to the pole when mapping
	    // a rectangular texture onto a sphere.
	    VertexDescription.PosNormVertex topVertex = Vertex(0.0f, +radius, 0.0f, 0.0f, +1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
	    VertexDescription.PosNormVertex bottomVertex = Vertex(0.0f, -radius, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);

        vertices.Add(topVertex);

	    float phiStep   = MathUtil.Pi/stackCount;
	    float thetaStep = MathUtil.TwoPi/sliceCount;

	    // Compute vertices for each stack ring (do not count the poles as rings).
	    for(int i = 1; i <= stackCount-1; ++i)
	    {
		    float phi = i*phiStep;

		    // Vertices of ring.
		    for(int j = 0; j <= sliceCount; ++j)
		    {
			    float theta = j*thetaStep;

			    VertexDescription.PosNormVertex v;

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

			    // Partial derivative of P with respect to theta
                v.Tangent.X = (float)(-radius * Math.Sin(phi) * Math.Sin(theta));
			    v.Tangent.Y = 0.0f;
                v.Tangent.Z = (float)(+radius * Math.Sin(phi) * Math.Cos(theta));

                v.Tangent.Normalize();

                v.Normal = Vector3.Normalize(v.Position);

			    v.TextCoord.X = theta / MathUtil.TwoPi;
			    v.TextCoord.Y = phi / MathUtil.Pi;

			    vertices.Add(v);
		    }
	    }

        vertices.Add(bottomVertex);

	    //
	    // Compute indices for top stack.  The top stack was written first to the vertex buffer
	    // and connects the top pole to the first ring.
	    //

	    for(int i = 1; i <= sliceCount; ++i)
	    {
            indices.Add(0);
		    indices.Add(i+1);
		    indices.Add(i);
	    }
	
	    //
	    // Compute indices for inner stacks (not connected to poles).
	    //

	    // Offset the indices to the index of the first vertex in the first ring.
	    // This is just skipping the top pole vertex.
	    int baseIndex = 1;
	    int ringVertexCount = sliceCount+1;
	    for(int i = 0; i < stackCount-2; ++i)
	    {
		    for(int j = 0; j < sliceCount; ++j)
		    {
                indices.Add(baseIndex + i * ringVertexCount + j);
                indices.Add(baseIndex + i * ringVertexCount + j + 1);
                indices.Add(baseIndex + (i + 1) * ringVertexCount + j);

                indices.Add(baseIndex + (i + 1) * ringVertexCount + j);
                indices.Add(baseIndex + i * ringVertexCount + j + 1);
                indices.Add(baseIndex + (i + 1) * ringVertexCount + j + 1);
		    }
	    }

	    //
	    // Compute indices for bottom stack.  The bottom stack was written last to the vertex buffer
	    // and connects the bottom pole to the bottom ring.
	    //

	    // South pole vertex was added last.
	    int southPoleIndex = vertices.Count-1;

	    // Offset the indices to the index of the first vertex in the last ring.
	    baseIndex = southPoleIndex - ringVertexCount;
	
	    for(int i = 0; i < sliceCount; ++i)
	    {
            indices.Add(southPoleIndex);
            indices.Add(baseIndex + i);
            indices.Add(baseIndex + i + 1);
	    }

        mesh.VertexCount = vertices.Count;
        mesh.IndexCount = indices.Count;

        mesh.VertexBuffer = SharpDX.Direct3D11.Buffer.Create(GraphicManager.Device, BindFlags.VertexBuffer, vertices.ToArray());
        mesh.IndexBuffer = SharpDX.Direct3D11.Buffer.Create(GraphicManager.Device, BindFlags.IndexBuffer, indices.ToArray());

        return mesh;
    }
예제 #2
0
        public static Mesh CreateCube(float width, float height, float depth)
        {

            Mesh mesh = new Mesh();

            float w2 = 0.5f*width;
	        float h2 = 0.5f*height;
	        float d2 = 0.5f*depth;

            mesh.VertexCount = 24;
            mesh.IndexCount = 36;

            VertexDescription.PosNormVertex[] v = new VertexDescription.PosNormVertex[24];

            // Fill in the front face vertex data.
            v[0] = Vertex(-w2, -h2, -d2, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
            v[1] = Vertex(-w2, +h2, -d2, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
            v[2] = Vertex(+w2, +h2, -d2, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
            v[3] = Vertex(+w2, -h2, -d2, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f);

            // Fill in the back face vertex data.
            v[4] = Vertex(-w2, -h2, +d2, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
            v[5] = Vertex(+w2, -h2, +d2, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
            v[6] = Vertex(+w2, +h2, +d2, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
            v[7] = Vertex(-w2, +h2, +d2, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f);

            // Fill in the top face vertex data.
            v[8] = Vertex(-w2, +h2, -d2, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
            v[9] = Vertex(-w2, +h2, +d2, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
            v[10] = Vertex(+w2, +h2, +d2, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
            v[11] = Vertex(+w2, +h2, -d2, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f);

            // Fill in the bottom face vertex data.
            v[12] = Vertex(-w2, -h2, -d2, 0.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
            v[13] = Vertex(+w2, -h2, -d2, 0.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
            v[14] = Vertex(+w2, -h2, +d2, 0.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
            v[15] = Vertex(-w2, -h2, +d2, 0.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f);

            // Fill in the left face vertex data.
            v[16] = Vertex(-w2, -h2, +d2, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
            v[17] = Vertex(-w2, +h2, +d2, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
            v[18] = Vertex(-w2, +h2, -d2, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
            v[19] = Vertex(-w2, -h2, -d2, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);

            // Fill in the right face vertex data.
            v[20] = Vertex(+w2, -h2, -d2, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f);
            v[21] = Vertex(+w2, +h2, -d2, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
            v[22] = Vertex(+w2, +h2, +d2, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f);
            v[23] = Vertex(+w2, -h2, +d2, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);

            int[] i = new int[36];

            // Fill in the front face index data
            i[0] = 0; i[1] = 1; i[2] = 2;
            i[3] = 0; i[4] = 2; i[5] = 3;

            // Fill in the back face index data
            i[6] = 4; i[7] = 5; i[8] = 6;
            i[9] = 4; i[10] = 6; i[11] = 7;

            // Fill in the top face index data
            i[12] = 8; i[13] = 9; i[14] = 10;
            i[15] = 8; i[16] = 10; i[17] = 11;

            // Fill in the bottom face index data
            i[18] = 12; i[19] = 13; i[20] = 14;
            i[21] = 12; i[22] = 14; i[23] = 15;

            // Fill in the left face index data
            i[24] = 16; i[25] = 17; i[26] = 18;
            i[27] = 16; i[28] = 18; i[29] = 19;

            // Fill in the right face index data
            i[30] = 20; i[31] = 21; i[32] = 22;
            i[33] = 20; i[34] = 22; i[35] = 23;


            mesh.VertexBuffer = SharpDX.Direct3D11.Buffer.Create(GraphicManager.Device, BindFlags.VertexBuffer, v);
            mesh.IndexBuffer = SharpDX.Direct3D11.Buffer.Create(GraphicManager.Device, BindFlags.IndexBuffer, i);

            return mesh;

        }