void initProgram() //инициализация { lastMousePos = new Vector2(Mouse.X, Mouse.Y); //objects.Add(new Cube()); //objects.Add(new Cube()); GL.GenBuffers(1, out ibo_elements); shaders.Add("default", new ShaderProgram("vs.glsl", "fs.glsl", true)); shaders.Add("textured", new ShaderProgram("vs_tex.glsl", "fs_tex.glsl", true)); activeShader = "textured"; textures.Add("opentksquare.png", loadImage("opentksquare.png")); textures.Add("opentksquare2.png", loadImage("opentksquare2.png")); TexturedCube tc = new TexturedCube(); tc.TextureID = textures["opentksquare.png"]; objects.Add(tc); TexturedCube tc2 = new TexturedCube(); tc2.TextureID = textures["opentksquare2.png"]; objects.Add(tc2); cam.Position += new Vector3(0f, 0f, 3f); Volume obj = ObjVolume.LoadFromFile("cow.obj"); obj.TextureID = textures["opentksquare.png"]; objects.Add(obj); Volume obj2 = ObjVolume.LoadFromFile("teapot.obj"); obj2.Position += new Vector3(0f, 2f, 0f); objects.Add(obj2); }
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); }
public static ObjVolume LoadFromString(string obj) { List <string> lines = new List <string>(obj.Split('\n')); 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> >(); foreach (string line in lines) { if (line.StartsWith("v ")) { string temp = line.Substring(2); temp = temp.Replace('.', ','); Vector3 vec = new Vector3(); if (temp.Count((char c) => c == ' ') == 2) { string[] vertparts = temp.Split(' '); 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); 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 (!success) { Console.WriteLine("Error parsing vertex: {0}", line); } } verts.Add(vec); } else if (line.StartsWith("f ")) { 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) { string[] faceparts = temp.Split((' ')); int i1, i2, i3; bool success = int.TryParse(faceparts[0], out i1); success |= int.TryParse(faceparts[1], out i2); success |= int.TryParse(faceparts[2], out i3); if (!success) { Console.WriteLine("Error parsing face: {0}", line); } else { face = new Tuple <int, int, int>(i1 - 1, i2 - 1, i3 - 1); faces.Add(face); } } } } ObjVolume vol = new ObjVolume(); vol.vertices = verts.ToArray(); vol.colors = colors.ToArray(); vol.texturecoords = texs.ToArray(); vol.faces = new List <Tuple <int, int, int> >(faces); return(vol); }