void initProgram() { lastMousePos = new Vector2(Mouse.X, Mouse.Y); CursorVisible = false; cam.MouseSensitivity = 0.0025f; 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)); activeShader = "textured"; // Load textures from file textures.Add("opentksquare.png", loadImage("opentksquare.png")); textures.Add("opentksquare2.png", loadImage("opentksquare2.png")); // Create our objects TexturedCube tc = new TexturedCube(); tc.TextureID = textures["opentksquare.png"]; objects.Add(tc); TexturedCube tc2 = new TexturedCube(); tc2.Position += new Vector3(1f, 1f, 1f); tc2.TextureID = textures["opentksquare2.png"]; objects.Add(tc2); // OBJ models from files ObjVolume obj1 = ObjVolume.LoadFromFile("teapot.obj"); obj1.TextureID = textures["opentksquare.png"]; objects.Add(obj1); ObjVolume obj2 = ObjVolume.LoadFromFile("teapot.obj"); obj2.TextureID = textures["opentksquare.png"]; obj2.Position += new Vector3(0, 2f, 0); objects.Add(obj2); // Move camera away from origin cam.Position += new Vector3(0f, 0f, 3f); }
/// <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}", filename); } return obj; }
/// <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) { Console.WriteLine("File not found: {0}", filename); } catch (Exception) { Console.WriteLine("Error loading file: {0}", filename); } return(obj); }
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> colors = new List <Vector3>(); List <Vector2> texs = new List <Vector2>(); List <Tuple <int, int, int> > faces = new List <Tuple <int, int, int> >(); // 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.Count((char c) => c == ' ') == 2) // Check if there's enough elements for a vertex { String[] vertparts = temp.Split(' '); // Attempt to parse each part of the vertice bool success = float.TryParse(vertparts[0], out vec.X); success |= float.TryParse(vertparts[1], out vec.Y); success |= float.TryParse(vertparts[2], out vec.Z); // Dummy color/texture coordinates for now colors.Add(new Vector3((float)Math.Sin(vec.Z), (float)Math.Sin(vec.Z), (float)Math.Sin(vec.Z))); texs.Add(new Vector2((float)Math.Sin(vec.Z), (float)Math.Sin(vec.Z))); // If any of the parses failed, report the error if (!success) { Console.WriteLine("Error parsing vertex: {0}", line); } } verts.Add(vec); } else if (line.StartsWith("f ")) // Face definition { // Cut off beginning of line String temp = line.Substring(2); Tuple <int, int, int> face = new Tuple <int, int, int>(0, 0, 0); if (temp.Count((char c) => c == ' ') == 2) // Check if there's enough elements for a face { String[] faceparts = temp.Split(' '); int i1, i2, i3; // Attempt to parse each part of the face bool success = int.TryParse(faceparts[0], out i1); success |= int.TryParse(faceparts[1], out i2); success |= int.TryParse(faceparts[2], out i3); // If any of the parses failed, report the error if (!success) { Console.WriteLine("Error parsing face: {0}", line); } else { // Decrement to get zero-based vertex numbers face = new Tuple <int, int, int>(i1 - 1, i2 - 1, i3 - 1); faces.Add(face); } } } } // Create the ObjVolume ObjVolume vol = new ObjVolume(); vol.vertices = verts.ToArray(); vol.faces = new List <Tuple <int, int, int> >(faces); vol.colors = colors.ToArray(); vol.texturecoords = texs.ToArray(); return(vol); }
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> colors = new List<Vector3>(); List<Vector2> texs = new List<Vector2>(); List<Tuple<int, int, int>> faces = new List<Tuple<int, int, int>>(); // 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.Count((char c) => c == ' ') == 2) // Check if there's enough elements for a vertex { String[] vertparts = temp.Split(' '); // Attempt to parse each part of the vertice bool success = float.TryParse(vertparts[0], out vec.X); success |= float.TryParse(vertparts[1], out vec.Y); success |= float.TryParse(vertparts[2], out vec.Z); // Dummy color/texture coordinates for now colors.Add(new Vector3((float) Math.Sin(vec.Z), (float) Math.Sin(vec.Z), (float) Math.Sin(vec.Z))); texs.Add(new Vector2((float) Math.Sin(vec.Z), (float) Math.Sin(vec.Z))); // If any of the parses failed, report the error if (!success) { Console.WriteLine("Error parsing vertex: {0}", line); } } verts.Add(vec); } else if (line.StartsWith("f ")) // Face definition { // Cut off beginning of line String temp = line.Substring(2); Tuple<int, int, int> face = new Tuple<int, int, int>(0, 0, 0); if (temp.Count((char c) => c == ' ') == 2) // Check if there's enough elements for a face { String[] faceparts = temp.Split(' '); int i1, i2, i3; // Attempt to parse each part of the face bool success = int.TryParse(faceparts[0], out i1); success |= int.TryParse(faceparts[1], out i2); success |= int.TryParse(faceparts[2], out i3); // If any of the parses failed, report the error if (!success) { Console.WriteLine("Error parsing face: {0}", line); } else { // Decrement to get zero-based vertex numbers face = new Tuple<int, int, int>(i1 - 1, i2 - 1, i3 - 1); faces.Add(face); } } } } // Create the ObjVolume ObjVolume vol = new ObjVolume(); vol.vertices = verts.ToArray(); vol.faces = new List<Tuple<int, int, int>>(faces); vol.colors = colors.ToArray(); vol.texturecoords = texs.ToArray(); return vol; }