Example #1
0
        public VertexP3N3T2[] CalculateVertices(float radius, float height, int segments, int rings)
        {
            int i = 0;
            // Load data into a vertex buffer or a display list afterwards.
            var data = new VertexP3N3T2[rings * segments];

            for (double y = 0; y < rings; y++)
            {
                double phi = (y / (rings - 1)) * Math.PI;
                for (double x = 0; x < segments; x++)
                {
                    double theta = (x / (segments - 1)) * 2 * Math.PI;

                    Vector3 v = new Vector3()
                    {
                        X = (float)(radius * Math.Sin(phi) * Math.Cos(theta)),
                        Y = (float)(height * Math.Cos(phi)),
                        Z = (float)(radius * Math.Sin(phi) * Math.Sin(theta)),
                    };
                    Vector3 n = Vector3.Normalize(v);
                    // Horizontal texture projection
                    Vector2 uv = new Vector2()
                    {
                        X = (float)(x / (segments - 1)),
                        Y = (float)(y / (rings - 1))
                    };
                    // Using data[i++] causes i to be incremented multiple times in Mono 2.2 (bug #479506).
                    data[i] = new VertexP3N3T2() { Position = v, Normal = n, TexCoord = uv };
                    i++;
                }
            }
            return data;
        }
Example #2
0
        VertexP3N3T2[] CalculateVertices(float radius, float height, byte segments, byte rings)
        {
            var data = new VertexP3N3T2[segments * rings];

            int i = 0;

            for (double y = 0; y < rings; y++)
            {
                double phi = (y / (rings - 1)) * Math.PI;
                for (double x = 0; x < segments; x++)
                {
                    double theta = (x / (segments - 1)) * 2 * Math.PI;

                    Vector3 v = new Vector3()
                    {
                        X = (float)(radius * Math.Sin(phi) * Math.Cos(theta)),
                        Y = (float)(height * Math.Cos(phi)),
                        Z = (float)(radius * Math.Sin(phi) * Math.Sin(theta)),
                    };
                    Vector3 n  = Vector3.Normalize(v);
                    Vector2 uv = new Vector2()
                    {
                        X = (float)(x / (segments - 1)),
                        Y = (float)(y / (rings - 1))
                    };
                    // Using data[i++] causes i to be incremented multiple times in Mono 2.2 (bug #479506).
                    data[i] = new VertexP3N3T2()
                    {
                        Position = v, Normal = n, TexCoord = uv
                    };
                    i++;

                    // Top - down sphere projection.
                    //Vector2 uv = new Vector2()
                    //{
                    //    X = (float)(Math.Atan2(n.X, n.Z) / Math.PI / 2 + 0.5),
                    //    Y = (float)(Math.Asin(n.Y) / Math.PI / 2 + 0.5)
                    //};
                }
            }

            return(data);
        }
Example #3
0
        VertexP3N3T2[] CalculateVertices(float radius, float height, byte segments, byte rings)
        {
            var data = new VertexP3N3T2[segments * rings];

            int i = 0;

            for (double y = 0; y < rings; y++)
            {
                double phi = (y / (rings - 1)) * Math.PI;
                for (double x = 0; x < segments; x++)
                {
                    double theta = (x / (segments - 1)) * 2 * Math.PI;

                    Vector3 v = new Vector3()
                    {
                        X = (float)(radius * Math.Sin(phi) * Math.Cos(theta)),
                        Y = (float)(height * Math.Cos(phi)),
                        Z = (float)(radius * Math.Sin(phi) * Math.Sin(theta)),
                    };
                    Vector3 n = Vector3.Normalize(v);
                    Vector2 uv = new Vector2()
                    {
                        X = (float)(x / (segments - 1)),
                        Y = (float)(y / (rings - 1))
                    };
                    // Using data[i++] causes i to be incremented multiple times in Mono 2.2 (bug #479506).
                    data[i] = new VertexP3N3T2() { Position = v, Normal = n, TexCoord = uv };
                    i++;

                    // Top - down sphere projection.
                    //Vector2 uv = new Vector2()
                    //{
                    //    X = (float)(Math.Atan2(n.X, n.Z) / Math.PI / 2 + 0.5),
                    //    Y = (float)(Math.Asin(n.Y) / Math.PI / 2 + 0.5)
                    //};
                }

            }

            return data;
        }
Example #4
0
        public Model Load(ObjFile obj_file)
        {
            var nv       = 0;
            var ni       = 0;
            var n        = obj_file.f.Count;
            var m        = obj_file.f[0].v.Length;
            var vertices = new VertexP3N3T2[n * m];
            var indices  = new List <uint>();

            for (var i = 0; i < n; i++)
            {
                var fi = obj_file.f[i];
                Debug.Assert(fi.v.Length == fi.vn.Length);
                Debug.Assert(fi.vn.Length == fi.vt.Length);
                for (var j = 0; j < fi.v.Length; j++)
                {
                    var vj = fi.v[j] - 1;
                    if (vj < 0)
                    {
                        vj += obj_file.v.Count + 1;
                    }
                    var vnj = fi.vn[j] - 1;
                    if (vnj < 0)
                    {
                        vnj += obj_file.vn.Count + 1;
                    }
                    var vtj = fi.vt[j] - 1;
                    if (vtj < 0)
                    {
                        vtj += obj_file.vt.Count + 1;
                    }
                    vertices[nv] = new VertexP3N3T2(obj_file.v[vj][0], obj_file.v[vj][1], obj_file.v[vj][2], obj_file.vn[vnj][0], obj_file.vn[vnj][1], obj_file.vn[vnj][2], obj_file.vt[vtj][0], 1 - obj_file.vt[vtj][1]);
                    nv++;
                }
                switch (fi.v.Length)
                {
                case 3:
                    indices.Add((uint)(ni + 0));
                    indices.Add((uint)(ni + 1));
                    indices.Add((uint)(ni + 2));
                    ni += fi.v.Length;
                    break;

                case 4:
                    indices.Add((uint)(ni + 0));
                    indices.Add((uint)(ni + 1));
                    indices.Add((uint)(ni + 2));
                    indices.Add((uint)(ni + 2));
                    indices.Add((uint)(ni + 3));
                    indices.Add((uint)(ni + 0));
                    ni += fi.v.Length;
                    break;

                default:
                    throw new NotImplementedException();
                }
            }

            var model = new Model();

            model.Mesh(new Mesh <VertexP3N3T2, uint>(vertices, indices.ToArray()));
            model.Mode = PrimitiveType.Triangles;
            return(model);
        }