/// <summary> /// Loads a model from a file. /// </summary> /// <param name="filename">File to load model from</param> /// <returns>ObjVolume of loaded model</returns> public static ObjVolume LoadFromFile(string filename) { ObjVolume obj = new ObjVolume(); try { using (StreamReader reader = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read))) { obj = LoadFromString(reader.ReadToEnd()); } } catch (FileNotFoundException e) { Console.WriteLine("File not found: {0}", filename); } catch (Exception e) { Console.WriteLine("Error loading file: {0}.\n{1}", filename, e); } return(obj); }
void initProgram() { lastMousePos = new Vector2(Mouse.X, Mouse.Y); GL.GenBuffers(1, out ibo_elements); // Load shaders from file shaders.Add("default", new ShaderProgram("vs.glsl", "fs.glsl", true)); shaders.Add("textured", new ShaderProgram("vs_tex.glsl", "fs_tex.glsl", true)); shaders.Add("normal", new ShaderProgram("vs_norm.glsl", "fs_norm.glsl", true)); shaders.Add("lit", new ShaderProgram("vs_lit.glsl", "fs_lit.glsl", true)); activeShader = "lit"; loadMaterials("opentk.mtl"); loadMaterials("police.mtl"); // Create our objects TexturedCube tc = new TexturedCube(); tc.TextureID = textures[materials["opentk1"].DiffuseMap]; tc.CalculateNormals(); tc.Material = materials["opentk1"]; objects.Add(tc); TexturedCube tc2 = new TexturedCube(); tc2.Position += new Vector3(1f, 1f, 1f); tc2.TextureID = textures[materials["opentk2"].DiffuseMap]; tc2.CalculateNormals(); tc2.Material = materials["opentk2"]; objects.Add(tc2); // Move camera away from origin textures.Add("earth.png", loadImage("earth.png")); ObjVolume earth = ObjVolume.LoadFromFile("earth.obj"); earth.TextureID = textures["earth.png"]; earth.Position += new Vector3(1f, 1f, -2f); earth.Material = new Material(new Vector3(0.15f), new Vector3(1), new Vector3(0.2f), 5); objects.Add(earth); textures.Add("police.jpg", loadImage("police.jpg")); ObjVolume car = ObjVolume.LoadFromFile("police.obj"); car.TextureID = textures["police.jpg"]; car.Position += new Vector3(1f, 1f, -4f); car.Material = materials["opentk1"]; car.Name = "Police"; objects.Add(car); textures.Add("tank.png", loadImage("tank.png")); ObjVolume tank = ObjVolume.LoadFromFile("tank.obj"); tank.TextureID = textures["tank.png"]; tank.Position += new Vector3(1f, 1f, -5f); tank.Material = materials["opentk1"]; tank.Name = "Tank"; objects.Add(tank); }
public static ObjVolume LoadFromString(string obj) { // Seperate lines from the file List <String> lines = new List <string>(obj.Split('\n')); // Lists to hold model data List <Vector3> verts = new List <Vector3>(); List <Vector3> normals = new List <Vector3>(); List <Vector2> texs = new List <Vector2>(); List <Tuple <TempVertex, TempVertex, TempVertex> > faces = new List <Tuple <TempVertex, TempVertex, TempVertex> >(); // Base values verts.Add(new Vector3()); texs.Add(new Vector2()); normals.Add(new Vector3()); int currentindice = 0; // Read file line by line foreach (String line in lines) { if (line.StartsWith("v ")) // Vertex definition { // Cut off beginning of line String temp = line.Substring(2); Vector3 vec = new Vector3(); if (temp.Trim().Count((char c) => c == ' ') == 2) // Check if there's enough elements for a vertex { String[] vertparts = temp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // Attempt to parse each part of the vertice bool success = float.TryParse(vertparts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out vec.X); success |= float.TryParse(vertparts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out vec.Y); success |= float.TryParse(vertparts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out vec.Z); // If any of the parses failed, report the error if (!success) { Console.WriteLine("Error parsing vertex: {0}", line); } } else { Console.WriteLine("Error parsing vertex: {0}", line); } verts.Add(vec); } else if (line.StartsWith("vt ")) // Texture coordinate { // Cut off beginning of line String temp = line.Substring(2); Vector2 vec = new Vector2(); if (temp.Trim().Count((char c) => c == ' ') > 0) // Check if there's enough elements for a vertex { String[] texcoordparts = temp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // Attempt to parse each part of the vertice bool success = float.TryParse(texcoordparts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out vec.X); success |= float.TryParse(texcoordparts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out vec.Y); // If any of the parses failed, report the error if (!success) { Console.WriteLine("Error parsing texture coordinate: {0}", line); } } else { Console.WriteLine("Error parsing texture coordinate: {0}", line); } texs.Add(vec); } else if (line.StartsWith("vn ")) // Normal vector { // Cut off beginning of line String temp = line.Substring(2); Vector3 vec = new Vector3(); if (temp.Trim().Count((char c) => c == ' ') == 2) // Check if there's enough elements for a normal { String[] vertparts = temp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // Attempt to parse each part of the vertice bool success = float.TryParse(vertparts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out vec.X); success |= float.TryParse(vertparts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out vec.Y); success |= float.TryParse(vertparts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out vec.Z); // If any of the parses failed, report the error if (!success) { Console.WriteLine("Error parsing normal: {0}", line); } } else { Console.WriteLine("Error parsing normal: {0}", line); } normals.Add(vec); } else if (line.StartsWith("f ")) // Face definition { // Cut off beginning of line String temp = line.Substring(2); Tuple <TempVertex, TempVertex, TempVertex> face = new Tuple <TempVertex, TempVertex, TempVertex>(new TempVertex(), new TempVertex(), new TempVertex()); if (temp.Trim().Count((char c) => c == ' ') == 2) // Check if there's enough elements for a face { String[] faceparts = temp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); int v1, v2, v3; int t1, t2, t3; int n1, n2, n3; // Attempt to parse each part of the face bool success = int.TryParse(faceparts[0].Split('/')[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out v1); success |= int.TryParse(faceparts[1].Split('/')[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out v2); success |= int.TryParse(faceparts[2].Split('/')[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out v3); if (faceparts[0].Count((char c) => c == '/') >= 2) { success |= int.TryParse(faceparts[0].Split('/')[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out t1); success |= int.TryParse(faceparts[1].Split('/')[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out t2); success |= int.TryParse(faceparts[2].Split('/')[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out t3); success |= int.TryParse(faceparts[0].Split('/')[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out n1); success |= int.TryParse(faceparts[1].Split('/')[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out n2); success |= int.TryParse(faceparts[2].Split('/')[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out n3); } else { if (texs.Count > v1 && texs.Count > v2 && texs.Count > v3) { t1 = v1; t2 = v2; t3 = v3; } else { t1 = 0; t2 = 0; t3 = 0; } if (normals.Count > v1 && normals.Count > v2 && normals.Count > v3) { n1 = v1; n2 = v2; n3 = v3; } else { n1 = 0; n2 = 0; n3 = 0; } } // If any of the parses failed, report the error if (!success) { Console.WriteLine("Error parsing face: {0}", line); } else { TempVertex tv1 = new TempVertex(v1, n1, t1); TempVertex tv2 = new TempVertex(v2, n2, t2); TempVertex tv3 = new TempVertex(v3, n3, t3); face = new Tuple <TempVertex, TempVertex, TempVertex>(tv1, tv2, tv3); faces.Add(face); } } else { Console.WriteLine("Error parsing face: {0}", line); } } } // Create the ObjVolume ObjVolume vol = new ObjVolume(); foreach (var face in faces) { FaceVertex v1 = new FaceVertex(verts[face.Item1.Vertex], normals[face.Item1.Normal], texs[face.Item1.Texcoord]); FaceVertex v2 = new FaceVertex(verts[face.Item2.Vertex], normals[face.Item2.Normal], texs[face.Item2.Texcoord]); FaceVertex v3 = new FaceVertex(verts[face.Item3.Vertex], normals[face.Item3.Normal], texs[face.Item3.Texcoord]); vol.faces.Add(new Tuple <FaceVertex, FaceVertex, FaceVertex>(v1, v2, v3)); } return(vol); }