Esempio n. 1
0
        // This gets called when the drawing surface is ready
        protected override void OnLoad(EventArgs e)
        {
            // this call is optional, and meant to raise delegates
            // in case any are registered
            base.OnLoad(e);

            // UpdateFrame and RenderFrame are called
            // by the render loop. This is takes effect
            // when we use 'Run ()', like below
            UpdateFrame += delegate(object sender, FrameEventArgs args) {
                // Rotate at a constant speed
                for (int i = 0; i < 3; i++)
                {
                    rot [i] += (float)(rateOfRotationPS [i] * args.Time);
                }
            };

            RenderFrame += delegate {
                RenderCube();
            };

            GL.Enable(All.CullFace);
            GL.ShadeModel(All.Smooth);

            GL.Hint(All.PerspectiveCorrectionHint, All.Nicest);

            //mesh = new ObjMesh(ctx, "sphere/sphere.obj");
            mesh = new ObjMesh(ctx, "cube.obj");

            // Run the render loop
            Run(30);
        }
Esempio n. 2
0
 public static bool Load(Context context, ObjMesh mesh, string fileName)
 {
     try
     {
         using (var input = context.Assets.Open(fileName))
             using (StreamReader streamReader = new StreamReader(input))
             {
                 Load(mesh, streamReader);
                 streamReader.Close();
                 return(true);
             }
     }
     catch { return(false); }
 }
Esempio n. 3
0
        static void Load(ObjMesh mesh, TextReader textReader)
        {
            vertices  = new List <Vector3>();
            normals   = new List <Vector3>();
            texCoords = new List <Vector2>();
            objVerticesIndexDictionary = new Dictionary <ObjMesh.ObjVertex, short>();
            objVertices  = new List <ObjMesh.ObjVertex>();
            objTriangles = new List <ObjMesh.ObjTriangle>();
            objQuads     = new List <ObjMesh.ObjQuad>();

            string line;

            while ((line = textReader.ReadLine()) != null)
            {
                line = line.Trim(splitCharacters);
                line = line.Replace("  ", " ");

                string[] parameters = line.Split(splitCharacters);

                switch (parameters[0])
                {
                case "p":     // Point
                    break;

                case "g":
                    break;

                case "v":     // Vertex
                    float x = float.Parse(parameters[1], CultureInfo.InvariantCulture.NumberFormat);
                    float y = float.Parse(parameters[2], CultureInfo.InvariantCulture.NumberFormat);
                    float z = float.Parse(parameters[3], CultureInfo.InvariantCulture.NumberFormat);
                    vertices.Add(new Vector3(x, y, z));
                    break;

                case "vt":     // TexCoord
                    float u = float.Parse(parameters[1], CultureInfo.InvariantCulture.NumberFormat);
                    float v = float.Parse(parameters[2], CultureInfo.InvariantCulture.NumberFormat);
                    texCoords.Add(new Vector2(u, v));
                    break;

                case "vn":     // Normal
                    float nx = float.Parse(parameters[1], CultureInfo.InvariantCulture.NumberFormat);
                    float ny = float.Parse(parameters[2], CultureInfo.InvariantCulture.NumberFormat);
                    float nz = float.Parse(parameters[3], CultureInfo.InvariantCulture.NumberFormat);
                    normals.Add(new Vector3(nx, ny, nz));
                    break;

                case "f":
                    switch (parameters.Length)
                    {
                    case 4:
                        ObjMesh.ObjTriangle objTriangle = new ObjMesh.ObjTriangle();
                        objTriangle.Index0 = ParseFaceParameter(parameters[1]);
                        objTriangle.Index1 = ParseFaceParameter(parameters[2]);
                        objTriangle.Index2 = ParseFaceParameter(parameters[3]);
                        objTriangles.Add(objTriangle);
                        break;

                    case 5:
                        ObjMesh.ObjQuad objQuad = new ObjMesh.ObjQuad();
                        objQuad.Index0 = ParseFaceParameter(parameters[1]);
                        objQuad.Index1 = ParseFaceParameter(parameters[2]);
                        objQuad.Index2 = ParseFaceParameter(parameters[3]);
                        objQuad.Index3 = ParseFaceParameter(parameters[4]);
                        objQuads.Add(objQuad);
                        break;
                    }
                    break;
                }
            }

            mesh.Vertices  = objVertices.ToArray();
            mesh.Triangles = objTriangles.ToArray();
            mesh.Quads     = objQuads.ToArray();

            objVerticesIndexDictionary = null;
            vertices     = null;
            normals      = null;
            texCoords    = null;
            objVertices  = null;
            objTriangles = null;
            objQuads     = null;
        }