コード例 #1
0
ファイル: Program.cs プロジェクト: gaoyongcn/salmon-viewer
        /// <summary>
        /// Main method
        /// </summary>
        /// <param name="argv">
        /// A <see cref="System.String"/>
        /// </param>
        public static void Main(string[] argv)
        {
            // instantiate GLUT for our windowing provider
            Glut.glutInit();
            //Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_RGB | Glut.GLUT_ALPHA | Glut.GLUT_DEPTH);
            Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_RGB | Glut.GLUT_DEPTH);
            Glut.glutInitWindowSize( winW, winH );
            Glut.glutCreateWindow("Salmon Viewer");

            // initialize our OpenGL parameters
            Init();

            // if no arguments, show message
            if (argv.Length <= 0)
            {
                Console.WriteLine("No file was specified.");

                return;
            }

            file = null;
            switch (Path.GetExtension(argv[0]).ToLower())
            {
                case ".3ds":

                    try
                    {
                        // Load our 3DS model from the command line argument
                        file =  new ThreeDSFile( argv[0] );
                        model = file.Model;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("An Error occured: " + ex.Message);
                    }
                    break;

            //			case ".obj":
            //				new ObjFile(argv[0]);
            //				break;

                default:
                    Console.WriteLine("Not a supported file type.");
                    break;
            }

            modelCenter = new Vector((file.MaxX-file.MinX)/2+file.MinX,
                                     (file.MaxY-file.MinY)/2+file.MinY,
                                     (file.MaxZ-file.MinZ)/2+file.MinZ);

            // move eye so model is entirely visible at startup

            // center x/y at model's center x/y
            double width = file.MaxX-file.MinX;
            double height = file.MaxY-file.MinY;
            eye[0] = Convert.ToSingle(file.MinX+width/2);
            eye[1] = Convert.ToSingle(file.MinY+height/2);

            // use trigonometry to calculate the z value that exposes the model
            eye[2] = Convert.ToSingle(file.MaxZ + (width > height ? width : height / 2) /
                                      Math.Tan((Math.PI/180) * 90/2));

            // print viewer control keys to Console
            PrintInstructions();

            // instantiate GLUT event handlers
            Glut.glutDisplayFunc(new Glut.DisplayCallback(Display));
            Glut.glutIdleFunc(new Glut.IdleCallback (Idle) );
            Glut.glutKeyboardFunc(new Glut.KeyboardCallback(Keyboard));
            Glut.glutKeyboardUpFunc(new Glut.KeyboardUpCallback(KeyboardUp));
            Glut.glutReshapeFunc(new Glut.ReshapeCallback(Reshape));
            Glut.glutMotionFunc (new Glut.MotionCallback (Motion) );

            // start loop and wait for user input
            Glut.glutMainLoop();
        }
コード例 #2
0
ファイル: ObjFile.cs プロジェクト: gaoyongcn/salmon-viewer
        public ObjFile(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            if (!File.Exists(fileName))
            {
                throw new ArgumentException("3ds file could not be found", "fileName");
            }

            Model model = new Model();
            Entity entity = new Entity();
            model.Entities.Add(entity);

            List<Vector> vectors = new List<Vector>();
            List<Vector> normals = new List<Vector>();

            List<Quad> quads = new List<Quad>();
            List<Triangle> tris = new List<Triangle>();

            using (StreamReader sr = File.OpenText(fileName))
            {

                int curLineNo = 0;

                string line = null;
                bool done = false;
                while ((line = sr.ReadLine()) != null)
                {
                    curLineNo++;

                    if (done || line.Trim() == string.Empty ||  line.StartsWith("#"))
                    {
                        continue;
                    }

                    string[] parts = line.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);

                    switch (parts[0])
                    {
                    case "v":
                        // this is a vertex
                        vectors.Add(ParseVector(parts));
                        break;
                    case "vn":
                        normals.Add(ParseVector(parts));
                        break;
                    //case "g":
                //		done = true;
                //		break;
                    case "f":
                        // a face

                        if (parts.Length > 5)
                        {
                            throw new NotSupportedException( string.Format("Face found with more than four indices (line {0})", curLineNo));
                        }

                        if (parts.Length < 3)
                        {
                            throw new FormatException(string.Format("Face found with less three indices (line {0})", curLineNo));
                        }

                        //Console.WriteLine(line);

                        // apparently we cannot make the assumption that all faces are of the same number of vertices.

                        if (parts.Length == 4)
                        {
                            tris.Add(new Triangle(ParseFacePart(parts[1]), ParseFacePart(parts[2]), ParseFacePart(parts[3])));
                        }
                        else
                        {
                            quads.Add(new Quad(ParseFacePart(parts[1]), ParseFacePart(parts[2]), ParseFacePart(parts[3]), ParseFacePart(parts[4])));

                        }

                        break;
                    }

                }
                                    Console.WriteLine("v: {0} n: {1} q:{2}", vectors.Count,normals.Count, quads.Count);

            }
        }