public Triangle(Vector a, Vector b, Vector c, MyColor col) { vertices = new Vector[3]; vertices[0] = a; vertices[1] = b; vertices[2] = c; color = col; HasTexture = false; }
public Triangle(Vector a, Vector b, Vector c) { vertices = new Vector[3]; vertices[0] = a; vertices[1] = b; vertices[2] = c; color = new MyColor(0.5F, 0.5F, 0.5F); HasTexture = false; }
public Quad(Vector a, Vector b, Vector c, Vector d, MyColor col, string n) { vertices = new Vector[4]; vertices[0] = a; vertices[1] = b; vertices[2] = c; vertices[3] = d; color = col; name = n; }
public Scene(string inputFilename) { triangles = new List<Triangle>(); quads = new List<Quad>(); bullets = new List<Bullet>(); hud = new HUD(); StreamReader reader = new FileInfo(inputFilename).OpenText(); string line; Dictionary<string, Vector> loadedVertices = new Dictionary<string, Vector>(); Dictionary<string, int> loadedTextures = new Dictionary<string, int>(); Dictionary<string, float> values = new Dictionary<string,float>(); int texturesCount = 0; textures = new uint[texturesCount]; int lineCounter = 0; while (true) { line = reader.ReadLine(); if (line == null) break; line = System.Text.RegularExpressions.Regex.Replace(line, "#.*", "", System.Text.RegularExpressions.RegexOptions.Compiled); lineCounter++; if (line == "") continue; if (line.IndexOf("clear vertices") == 0) loadedVertices = new Dictionary<string, Vector>(); else if (line.IndexOf("start") == 0) { #region start string[] parts = line.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 6) { throw new Exception("line.split for start did not make the right length array. line " + lineCounter.ToString()); } cameraPosition.x = GetFloat(parts[1], values); cameraPosition.y = GetFloat(parts[2], values); cameraPosition.z = GetFloat(parts[3], values); lookAngleX = GetFloat(parts[4], values); lookAngleY = GetFloat(parts[5], values); #endregion } else if (line.Contains("vertex")) { #region vertex try { string[] parts = line.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 5) throw new Exception("line.Split for Vector did not make the right length array. line " + lineCounter.ToString()); string name = parts[1]; float x = GetFloat(parts[2], values); float y = GetFloat(parts[3], values); float z = GetFloat(parts[4], values); loadedVertices.Add(name, new Vector(x, y, z)); } catch (Exception e) { throw new Exception("error parsing input file. line " + lineCounter.ToString() + " . message: " + e.Message); } #endregion } else if (line.Contains("quad")) { #region quad string[] parts; parts = line.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); if (parts.Length > 10) { throw new Exception("line.split for quad was not the right length. line: " + lineCounter.ToString()); } if (parts[5] == "color") { float red = GetFloat(parts[6], values); float green = GetFloat(parts[7], values); float blue = GetFloat(parts[8], values); MyColor color = new MyColor(red, green, blue); string name = parts[9]; Vector v1 = loadedVertices[parts[1]]; Vector v2 = loadedVertices[parts[2]]; Vector v3 = loadedVertices[parts[3]]; Vector v4 = loadedVertices[parts[4]]; Quad quad = new Quad(v1, v2, v3, v4, color, name); quads.Add(quad); } else if (parts[5] == "tex") { Vector v1 = loadedVertices[parts[1]]; Vector v2 = loadedVertices[parts[2]]; Vector v3 = loadedVertices[parts[3]]; Vector v4 = loadedVertices[parts[4]]; string textureName = parts[6]; string name = parts[7]; Quad quad = new Quad(v1, v2, v3, v4, name, (uint)loadedTextures[textureName]); quads.Add(quad); } #endregion } else if (line.Contains("triangle")) { #region triangle string[] parts; try { parts = line.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 6) throw new Exception("line.split for triangle did not make the right length array. line " + lineCounter.ToString()); } catch (Exception e) { throw new Exception("error splitting triangle line in input file. line " + lineCounter.ToString() + " . message: " + e.Message); } try { if (parts[4] == "color") { float red = GetFloat(parts[5], values); float green = GetFloat(parts[6], values); float blue = GetFloat(parts[7], values); MyColor color = new MyColor(red, green, blue); Vector v1 = loadedVertices[parts[1]]; Vector v2 = loadedVertices[parts[2]]; Vector v3 = loadedVertices[parts[3]]; Triangle tri = new Triangle(v1, v2, v3, color); triangles.Add(tri); } else if (parts[4] == "texture") { Vector v1 = loadedVertices[parts[1]]; Vector v2 = loadedVertices[parts[2]]; Vector v3 = loadedVertices[parts[3]]; string textureName = parts[5]; Triangle tri = new Triangle(v1, v2, v3, (uint)loadedTextures[textureName]); triangles.Add(tri); } } catch (Exception e) { throw new Exception("error creating triangle in input file. line: " + lineCounter.ToString() + " . message: " + e.Message); } #endregion } else if (line.Contains("texture")) { #region texture string[] parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string file = parts[1]; string name = parts[2]; texturesCount++; loadedTextures[name] = texturesCount; LoadTexture(file, texturesCount); #endregion } else if (line.Contains("value")) { #region value string[] parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string name = parts[1]; float value = float.Parse(parts[2]); values[name] = value; #endregion } else if(line.Contains("clearValues")) { values = new Dictionary<string,float>(); } else { throw new Exception("line " + lineCounter.ToString() + " not understood"); } } reader.Close(); lastMouseX = Cursor.Position.X; lastMouseY = Cursor.Position.Y; }