Ejemplo n.º 1
0
        private static vec3 GenNormal(vertex a, vertex b, vertex c)
        {
            var dir1 = a.pos - c.pos;
            var dir2 = b.pos - c.pos;

            return(dir1.cross(dir2));
            //return OpenTK.Vector3.Cross(dir1.ToOpenTKVec(), dir2.ToOpenTKVec()).ToNumsVec();
        }
Ejemplo n.º 2
0
        public vertex VertexFurthestAwayFromPivot()
        {
            vertex res = this.vertices[0];

            for (int i = 1; i < this.vertices.Count; i++)
            {
                if (res.pos.sqlength < this.vertices[i].pos.sqlength)
                {
                    res = vertices[i];
                }
            }

            return(res);
        }
Ejemplo n.º 3
0
 public Triangle(vertex v1, vertex v2, vertex v3, uint i1, uint i2, uint i3)
 {
     this.v1 = v1; this.v2 = v2; this.v3 = v3;
     this.i1 = i1; this.i2 = i2; this.i3 = i3;
 }
Ejemplo n.º 4
0
        public static Mesh GenCube()
        {
            const float k     = 0.5f;
            const float n     = -k;
            var         verts = new vertex[] {
                // front side
                new vertex(new vec3(n, n, n), new vec2(), new vec3()),
                new vertex(new vec3(k, n, n), new vec2(), new vec3()),
                new vertex(new vec3(n, k, n), new vec2(), new vec3()),
                new vertex(new vec3(k, k, n), new vec2(), new vec3()),

                // back side
                new vertex(new vec3(n, n, k), new vec2(), new vec3()),
                new vertex(new vec3(k, n, k), new vec2(), new vec3()),
                new vertex(new vec3(n, k, k), new vec2(), new vec3()),
                new vertex(new vec3(k, k, k), new vec2(), new vec3()),

                // right side
                new vertex(new vec3(n, n, n), new vec2(), new vec3()),
                new vertex(new vec3(n, k, n), new vec2(), new vec3()),
                new vertex(new vec3(n, n, k), new vec2(), new vec3()),
                new vertex(new vec3(n, k, k), new vec2(), new vec3()),

                // left side
                new vertex(new vec3(k, n, n), new vec2(), new vec3()),
                new vertex(new vec3(k, n, k), new vec2(), new vec3()),
                new vertex(new vec3(k, k, n), new vec2(), new vec3()),
                new vertex(new vec3(k, k, k), new vec2(), new vec3()),

                // top side
                new vertex(new vec3(n, k, n), new vec2(), new vec3()),
                new vertex(new vec3(k, k, n), new vec2(), new vec3()),
                new vertex(new vec3(k, k, k), new vec2(), new vec3()),
                new vertex(new vec3(n, k, k), new vec2(), new vec3()),

                // bottom side
                new vertex(new vec3(n, n, n), new vec2(), new vec3()),
                new vertex(new vec3(k, n, n), new vec2(), new vec3()),
                new vertex(new vec3(k, n, k), new vec2(), new vec3()),
                new vertex(new vec3(n, n, k), new vec2(), new vec3()),
            };
            var ind = new uint[] {
                /*0,1,2,     2,1,3,    // front
                 * 4,6,5,     7,5,6,    // back
                 * 8,9,10,    11,10,9,  // right
                 * 12,13,14,  15,14,13, // left
                 * 16,17,18,  19,16,18, // top
                 * 20,23,22,  21,20,22  // bottom
                 */

                2, 1, 0, 3, 1, 2,       // front
                5, 6, 4, 6, 5, 7,       // back
                10, 9, 8, 9, 10, 11,    // right
                14, 13, 12, 13, 14, 15, // left
                18, 17, 16, 18, 16, 19, // top
                22, 23, 20, 22, 20, 21  // bottom
            };
            var m = new Mesh(verts, ind);

            m.GenNormals();
            return(m);
        }
Ejemplo n.º 5
0
 public vertex lerp(vertex other, float time)
 {
     return(new vertex(pos.lerp(other.pos, time), texcoord.lerp(other.texcoord, time), normal.lerp(other.normal, time)));
 }
Ejemplo n.º 6
0
        private void subdivide()
        {
            int icount = indices.Count;

            var ind = new uint[icount];

            indices.CopyTo(ind);
            indices.Clear();

            var vex = new vertex[vertices.Count];

            vertices.CopyTo(vex);
            vertices.Clear();

            void _vertex(vertex v)
            {
                var i = vertices.IndexOf(v);

                if (i == -1)
                {
                    vertices.Add(v);
                    i = vertices.Count - 1;
                }
                indices.Add((uint)i);
            }

            for (int i = 0; i < icount; i += 3)
            {
                uint i1 = ind[i],
                     i2 = ind[i + 1],
                     i3 = ind[i + 2];

                vertex v1 = vex[i1],
                       v2 = vex[i2],
                       v3 = vex[i3];

                /*     v3
                 *      o
                 *     / \
                 * vm2 o---o vm3
                 *   / \ / \
                 *  o---o---o
                 * v1    vm1    v2  */

                vertex vm1 = v1.lerp(v2, .5f),
                       vm2 = v1.lerp(v3, .5f),
                       vm3 = v2.lerp(v3, .5f);

                // triangle 1 (lower left)
                _vertex(v1); _vertex(vm1); _vertex(vm2);

                // triangle 2 (middle)
                _vertex(vm1); _vertex(vm3); _vertex(vm2);

                // triangle 3 (lower right)
                _vertex(vm1); _vertex(v2); _vertex(vm3);

                // triangle 4 (top)
                _vertex(vm2); _vertex(vm3); _vertex(v3);
            }
        }