Example #1
0
        public ObjModelRenderer(ObjModel model, Drawer drawer)
        {
            this.drawer = drawer;
            light       = new Vector3(0, 0, -1).Normalize();

            var eye    = new Vector3(0f, 0f, 8f);
            var center = Vector3.Zero;
            var aspect = (float)drawer.Width / drawer.Height;

            projection = ProjectionMatrix(FovYFromFovX(90f, aspect), aspect, ZNear, ZFar);
            viewport   = ViewportMatrix(drawer.Width, drawer.Height, ViewDepth, drawer.Width, drawer.Height);
            view       = LookAtMatrix(eye, center, Vector3.Up);

            rot = Matrix.Identity(4, 4);

            SetModel(model);
        }
Example #2
0
 public void SetModel(ObjModel model)
 {
     this.model = model;
     vertices   = model.Vertices;
     faces      = model.Faces;
 }
Example #3
0
        public static ObjModel FromFile(string filename)
        {
            var timer = Stopwatch.StartNew();

            var mdl      = new ObjModel();
            var lines    = File.ReadLines(filename);
            var vertices = new List <Vector3>();

            var maxX = 0f;
            var minX = 0f;
            var maxY = 0f;
            var minY = 0f;
            var maxZ = 0f;
            var minZ = 0f;

            foreach (var line in lines)
            {
                if (line.StartsWith("#"))
                {
                    continue;
                }

                var args = line.Split(' ');
                switch (args[0])
                {
                default:
                    continue;

                case "v":
                    var x = float.Parse(args[1], CultureInfo.InvariantCulture);
                    var y = float.Parse(args[2], CultureInfo.InvariantCulture);
                    var z = float.Parse(args[3], CultureInfo.InvariantCulture);

                    maxX = Math.Max(x, maxX);
                    minX = Math.Min(x, minX);
                    maxY = Math.Max(y, maxY);
                    minY = Math.Min(y, minY);
                    maxZ = Math.Max(z, maxZ);
                    minZ = Math.Min(z, minZ);

                    vertices.Add(new Vector3(x, y, z));
                    break;

                case "f":
                    var i1Args = args[1].Split('/');
                    var i1     = int.Parse(i1Args[0], CultureInfo.InvariantCulture);

                    var i2Args = args[2].Split('/');
                    var i2     = int.Parse(i2Args[0], CultureInfo.InvariantCulture);

                    var i3Args = args[3].Split('/');
                    var i3     = int.Parse(i3Args[0], CultureInfo.InvariantCulture);

                    mdl.faces.Add(Tuple.Create(i1 - 1, i2 - 1, i3 - 1));
                    break;
                }
            }

            mdl.dimOrigin = new Vector3(minX, minY, minZ);
            mdl.dimSize   = new Vector3(maxX - minX, maxY - minY, maxZ - minZ);
            mdl.dimCenter = new Vector3(mdl.dimOrigin.X + (mdl.dimSize.X / 2f), mdl.dimOrigin.Y + (mdl.dimSize.Y / 2f), mdl.dimOrigin.Z + (mdl.dimSize.Z / 2f));

            foreach (var vert in vertices)
            {
                mdl.vertices.Add(new Vector3(
                                     vert.X,
                                     vert.Y,
                                     vert.Z));
            }

            timer.Stop();
            Console.WriteLine($"{Path.GetFileName(filename)} loaded in {timer.Elapsed.TotalMilliseconds} ms with {mdl.vertices.Count} vertices and {mdl.faces.Count} faces, dimensions {mdl.dimOrigin} : {mdl.dimSize} : {mdl.dimCenter}");

            return(mdl);
        }