public VisualisationWindow(math_classes.ZArrayDescriptor desc, Mesh.ColoringMethod method, ICamera cam, int fsaa_samples = 0, bool vsync = false)
            : base(100, 100, new GraphicsMode(32, 24, 0, fsaa_samples))
        {
            this.Width = DisplayDevice.Default.Width;
            this.Height = DisplayDevice.Default.Height - 70;
            this.Location = new System.Drawing.Point(0, 0);

            this.cam = cam;
            this.mesh = Mesh.FromZArray(desc, method);
            if (!vsync)
                this.VSync = VSyncMode.Off;
        }
        public VisualisationWindow(string obj_path, ICamera cam, int fsaa_samples = 0, bool vsync = false)
            : base(100, 100, new GraphicsMode(32, 24, 0, fsaa_samples))
        {
            this.Width = DisplayDevice.Default.Width;
            this.Height = DisplayDevice.Default.Height - 70;
            this.Location = new System.Drawing.Point(0, 0);

            this.cam = cam;
            this.mesh = Mesh.FromObject(obj_path)[0];
            if (!vsync)
                this.VSync = VSyncMode.Off;
        }
 public VisualisationWindow(math_classes.ZArrayDescriptor desc, Mesh.ColoringMethod method, ICamera cam, int width, int height, int fsaa_samples, bool vsync)
     : base(width, height, new GraphicsMode(32, 24, 0, fsaa_samples))
 {
     this.cam = cam;
     this.mesh = Mesh.FromZArray(desc, method);
     if (!vsync)
         this.VSync = VSyncMode.Off;
 }
 public SceneNode(Mesh mesh, int modelMatrixLoc)
 {
     this.mesh = mesh;
     modelLoc = modelMatrixLoc;
 }
        public static List<Mesh> FromObject(string path)
        {
            uint lineno = 1;
            StreamReader sr = new StreamReader(path);
            PrimitiveType ptype = PrimitiveType.Triangles;

            List<Mesh> meshes = new List<Mesh>();
            List<Vector3> vertices = new List<Vector3>();
            List<Vector3> normals = new List<Vector3>();
            List<uint> elements = new List<uint>();
            int offset = 0, primitiveCnt = 0;
            Mesh mesh = new Mesh(0, ptype);

            for (string current_line = sr.ReadLine();
                current_line != null;
                current_line = sr.ReadLine(), lineno++)
            {
                string[] split = current_line.Split(null);
                if (split.Length == 1 && split[0] == "")
                    continue;

                // get rid of empty strings (caused by repeated delimiters):
                // v\x20\x20\x200.1234 converts to: {"v", "", "", "0.1234"}
                int notempty = 0;
                for (int i = 0; i < split.Length; ++i)
                    if (split[i] != "")
                        ++notempty;

                string[] tok = split;
                if (notempty != split.Length)
                {
                    tok = new string[notempty];
                    for (int i = 0, j = 0; i < split.Length; ++i)
                        if (split[i] != "")
                            tok[j++] = split[i];
                }

                switch (tok[0])
                {
                    case "v":
                        if (tok.Length < 4)
                            throw new Exception("Parsing error at line " + lineno + ": expected at least 4 tokens");

                        vertices.Add(new Vector3(float.Parse(tok[1], System.Globalization.CultureInfo.InvariantCulture),
                            float.Parse(tok[2], System.Globalization.CultureInfo.InvariantCulture),
                            float.Parse(tok[3], System.Globalization.CultureInfo.InvariantCulture)));
                        normals.Add(new Vector3());
                        break;
                    case "f":
                        if (tok.Length < 4)
                            throw new Exception("Parsing error at line " + lineno + ": expected at least 4 tokens");

                        int i1 = ParseIndex(int.Parse(tok[1].Split('/')[0]), vertices.Count);
                        int i2 = ParseIndex(int.Parse(tok[2].Split('/')[0]), vertices.Count);
                        int i3 = ParseIndex(int.Parse(tok[3].Split('/')[0]), vertices.Count);
                        int i4 = tok.Length >= 5 ? int.Parse(tok[4].Split('/')[0]) : 0;

                        AddNormal(vertices, normals, i1, i2, i3);
                        elements.Add((uint)i1);
                        elements.Add((uint)i2);
                        elements.Add((uint)i3);
                        offset += 3;
                        primitiveCnt += 3;

                        if (i4 != 0)
                        {
                            i4 = ParseIndex(i4, vertices.Count);
                            elements.Add((uint)i1);
                            elements.Add((uint)i3);
                            elements.Add((uint)i4);
                            AddNormal(vertices, normals, i1, i3, i4);
                            offset += 3;
                            primitiveCnt += 3;
                        }
                        break;
                    case "o":
                    case "g":
                        if (mesh != null && primitiveCnt != 0) //wrong
                        {
                            mesh.primitiveCount = primitiveCnt;
                            primitiveCnt = 0;
                            meshes.Add(mesh);
                        }
                        mesh = new Mesh(offset, ptype);
                        break;
                }
            }

            if (meshes.Count == 0 && primitiveCnt == 0)
                return null;

            if (primitiveCnt != 0)
            {
                mesh.primitiveCount = primitiveCnt;
                meshes.Add(mesh);
            }

            // normals
            Vector4b[] colors = new Vector4b[normals.Count];
            for (int i = 0; i < normals.Count; ++i)
            {
                Vector3 n = normals[i].Normalized();
                calcFullcolor(ref n, ref colors[i]);
            }

            // arrange
            Vector3[] positions = new Vector3[vertices.Count];
            for (int i = 0; i < vertices.Count; ++i)
                positions[i] = vertices[i];

            uint[] elems = new uint[elements.Count];
            for (int i = 0; i < elements.Count; ++i)
                elems[i] = elements[i];

            int vao = arrangeData(positions, colors, elems);
            foreach (Mesh m in meshes)
                m.vao = vao;

            return meshes;
        }
 public static Mesh FromZArray(ZArrayDescriptor desc, ColoringMethod coloring)
 {
     Tuple<Vector3[], Vector4b[], uint[]> meshData = ParseZArray(desc, coloring);
     Mesh rv = new Mesh(meshData.Item1, meshData.Item2, meshData.Item3);
     rv.primitiveCount = meshData.Item3.Length;
     rv.primitiveType = PrimitiveType.TriangleStrip;
     return rv;
 }