All pastes #708979 Raw Edit

Something

public text v1 · immutable
#708979 ·published 2007-09-24 08:00 UTC
rendered paste body
        public static ModelObject Load(GraphicsDevice Device)
        {
            StreamReader reader = new StreamReader("belltower.obj");

            List<VertexPositionTexture> vptVerticies = new List<VertexPositionTexture>();
            List<Vector3> verticies = new List<Vector3>();
            List<Vector2> uvcoords = new List<Vector2>();

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();

                line = line.Trim();

                if (line.Length < 1)
                    continue;

                switch (line[0])
                {
                    case 'v':
                        if (line[1] == 't')
                        {
                            string data = line.Substring(3);
                            string[] values = data.Split(' ');

                            Vector2 coord = new Vector2(float.Parse(values[0]), float.Parse(values[1]));

                            uvcoords.Add(coord);
                        }
                        else
                        {
                            string data = line.Substring(2);
                            string[] values = data.Split(' ');

                            Vector3 vert = new Vector3(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]));

                            verticies.Add(vert);
                        }
                        break;

                    case 'f':
                        string fData = line.Substring(2);
                        string[] fValues = fData.Split(' ');

                        for (int i = 0; i < fValues.Length; i++)
                        {
                            string[] index = fValues[i].Split('/');

                            int vIndex = int.Parse(index[0]);
                            int uvIndex = int.Parse(index[1]);

                            vptVerticies.Add(new VertexPositionTexture(verticies[vIndex - 1], uvcoords[uvcoords.Count + uvIndex]));
                        }
                        break;
                }
            }

            reader.Close();

            ModelObject modelObject = new ModelObject();
            ModelSurface surface = new ModelSurface();
            ModelSurfacePart part = new ModelSurfacePart();

            surface.VertexBuffer = new VertexBuffer(Device, VertexPositionTexture.SizeInBytes * vptVerticies.Count, ResourceUsage.None, ResourceManagementMode.Automatic);
            surface.VertexBuffer.SetData<VertexPositionTexture>(vptVerticies.ToArray());

            short[] indices = new short[vptVerticies.Count];

            for (int x = 0; x < vptVerticies.Count; x++) 
            {
                indices[x] = (short)x; 
            }

            surface.IndexBuffer = new IndexBuffer(Device, typeof(short), indices.Length, ResourceUsage.None, ResourceManagementMode.Automatic);
            surface.IndexBuffer.SetData<short>(indices);

            part.BaseVertex = 0;
            part.NumVertices = indices.Length;
            part.PrimitiveCount = indices.Length / 3;
            part.StartIndex = 0;
            part.StreamOffset = 0;
            part.VertexDeclaration = new VertexDeclaration(Device, VertexPositionTexture.VertexElements);
            part.VertexStride = VertexPositionTexture.SizeInBytes;

            surface.SurfaceParts.Add(part);
            modelObject.Surfaces.Add(surface);

            Console.WriteLine("Indicies: " + indices.Length);
            Console.WriteLine("Vertitices: " + vptVerticies.Count);
            Console.WriteLine("UV Coords: " + uvcoords.Count);

            return modelObject;
        }