public override void LoadResources() { config = new Config("Game.ini"); TextureManager.AddTexture("monsterTexture", @"Content/textures/monsterText1.png"); TextureManager.AddTexture("skybox", @"Content/textures/skybox.png"); TextureManager.AddTexture("font0", @"Content/textures/font0.png"); TextureManager.AddTexture("font1", @"Content/textures/font1.png"); TextureManager.AddTexture("font2", @"Content/textures/font2.png"); TextureManager.AddTexture("crosshair0", @"Content/textures/crosshair0.png"); TextureManager.AddTexture("white", @"Content/textures/white.png"); TextureManager.AddTexture("huescale", @"Content/textures/huescale.png"); TextureManager.AddTexture("spedb", @"Content/textures/speedometer_base.png"); TextureManager.AddTexture("spedp", @"Content/textures/speedometer_pointer.png"); TextureManager.AddTexture("map0a", @"Content/textures/map0/darkBrick.png", TextureMinFilter.Linear, TextureMagFilter.Linear); TextureManager.AddTexture("map0b", @"Content/textures/map0/rockWall.png", TextureMinFilter.Linear, TextureMagFilter.Linear); TextureManager.AddTexture("map0c", @"Content/textures/map0/crate.png", TextureMinFilter.Linear, TextureMagFilter.Linear); TextureManager.AddTexture("map0d", @"Content/textures/map0/metal.png", TextureMinFilter.Linear, TextureMagFilter.Linear); TextureManager.AddTexture("map0e", @"Content/textures/floor0.png", TextureMinFilter.Linear, TextureMagFilter.Linear); TextureManager.AddTexture("playerTexture", @"Content/textures/player.png"); TextureManager.AddTexture("testimage", @"Content/textures/testimage.png"); for(int i = 0; i < config.GetInt("numSlides"); i++) { TextureManager.AddTexture("slide" + i.ToString("D2"), @"Content/textures/slides/slide" + i.ToString("D2") + ".png", TextureMinFilter.Nearest, TextureMagFilter.Linear); } testHueScale.mesh = ObjConverter.ConvertObjToMesh(File.ReadAllText(@"Content/models/monsterUVd.obj"), new Vector3(101, -19, 205)); monster.mesh = ObjConverter.ConvertObjToMesh(File.ReadAllText(@"Content/models/monsterUVd.obj"), new Vector3(101, -19, 205)); collisionVisuals.mesh = ObjConverter.ConvertObjToMesh(File.ReadAllText(@"Content/models/map1/collision.obj")); skybox.mesh = ObjConverter.ConvertObjToMesh(File.ReadAllText(@"Content/models/skybox3.obj")); map1.mesh = ObjConverter.ConvertObjToVboMesh(File.ReadAllText(@"Content/models/map1/map1.obj")); //TODO: offset map2a.mesh = ObjConverter.ConvertObjToVboMesh(File.ReadAllText(@"Content/models/map2/map2a.obj")); map2b.mesh = ObjConverter.ConvertObjToVboMesh(File.ReadAllText(@"Content/models/map2/map2c.obj")); map2c.mesh = ObjConverter.ConvertObjToVboMesh(File.ReadAllText(@"Content/models/map2/map2b.obj")); map2d.mesh = ObjConverter.ConvertObjToVboMesh(File.ReadAllText(@"Content/models/map2/map2d.obj")); map2e.mesh = ObjConverter.ConvertObjToVboMesh(File.ReadAllText(@"Content/models/map2/map2e.obj")); map2f.mesh = ObjConverter.ConvertObjToVboMesh(File.ReadAllText(@"Content/models/map2/map2f.obj")); Lamp.lampHeadMesh = ObjConverter.ConvertObjToVboMesh(File.ReadAllText(@"Content/models/lamp/lamphead.obj")); Lamp.lampPostMesh = ObjConverter.ConvertObjToVboMesh(File.ReadAllText(@"Content/models/lamp/lamppost.obj")); slideMesh = ObjConverter.ConvertObjToMesh(File.ReadAllText(@"Content/models/slide.obj")); mapCollision.AddRange(ObjConverter.ConvertObjToAABBarray(File.ReadAllText(@"Content/models/map1/collision.obj"))); //playerAABB = ObjConverter.ConvertObjToAABBarray(File.ReadAllText(@"Content/models/player.obj"))[0]; BasicClock.clockMesh = ObjConverter.ConvertObjToVboMesh(File.ReadAllText(@"Content/models/player.obj")); BasicClock.clockMesh.material.textureName = "huescale"; }
/// <summary> /// Converts the given .obj file to a Mesh object that supports VBO rendering /// </summary> /// <param name="inputFile">.obj file (not path)</param> /// <param name="offset">Mesh offset</param> /// <returns>Generated Mesh</returns> public static Mesh ConvertObjToVboMesh(string inputFile, Vector3 offset) { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; Mesh output = new Mesh(); List<Vertex> vOuput = new List<Vertex>(); string[] inputElements = inputFile.Split(new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries); int vCount = 1; int vnCount = 1; int vtCount = 1; int totalVertices = 0; int totalNormals = 0; int totalTextCoordinates = 0; foreach(string s in inputElements) { string[] decomposed = s.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); if(decomposed[0] == "v" | decomposed[0] == "V") { totalVertices++; } else if(decomposed[0] == "vt") { totalTextCoordinates++; } else if(decomposed[0] == "vn") { totalNormals++; } } //Vertex[] vertices = new Vertex[totalVertices + 1]; Vector3[] vertices = new Vector3[totalVertices + 1]; Vector3[] normals = new Vector3[totalNormals + 1]; Vector2[] textCoords = new Vector2[totalTextCoordinates + 1]; List<Face> faces = new List<Face>(); foreach(string s in inputElements) { string[] decomposed = s.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); switch(decomposed[0]) { #region Comment case "#": break; #endregion #region Geometric vertex case "v": case "V": Vector3 pos = new Vector3((float)Convert.ToDouble(decomposed[1].Replace(",", ".")), (float)Convert.ToDouble(decomposed[2].Replace(",", ".")), (float)Convert.ToDouble(decomposed[3].Replace(",", "."))); vertices[vCount] = pos - offset; vCount++; break; #endregion #region Texture vertex case "vt": Vector2 tex = new Vector2((float)Convert.ToDouble(decomposed[1].Replace(",", ".")), 1f - (float)Convert.ToDouble(decomposed[2].Replace(",", "."))); textCoords[vtCount] = tex; vtCount++; break; #endregion #region Vertex normal case "vn": Vector3 nrm = new Vector3((float)Convert.ToDouble(decomposed[1].Replace(",", ".")), (float)Convert.ToDouble(decomposed[2].Replace(",", ".")), (float)Convert.ToDouble(decomposed[3].Replace(",", "."))); normals[vnCount] = nrm; vnCount++; break; #endregion #region Face case "f": case "F": int length = decomposed.Length - 1; int[] vertexIndices = new int[length]; int[] normalIndices = new int[length]; int[] textureIndices = new int[length]; for(int i = 0; i < decomposed.Length - 1; i++) { string[] vertexString = decomposed[i + 1].Split(new string[] { "/" }, StringSplitOptions.None); vertexIndices[i] = Convert.ToInt32(vertexString[0]); textureIndices[i] = Convert.ToInt32(vertexString[1]); normalIndices[i] = Convert.ToInt32(vertexString[2]); } faces.Add(new Face(vertexIndices, textureIndices, normalIndices)); break; #endregion #region Mtl library case "mtllib": //TODO: MTLLIB break; #endregion #region Mtl library reference case "usemtl": //TODO: USEMTL break; #endregion } } foreach(Face f in faces) { Vertex[] vertexArr = new Vertex[f.vIndices.Length]; if(f.vIndices.Length == 3) { for(int i = 0; i < 3; i++) { vertexArr[i] = new Vertex(vertices[f.vIndices[i]], normals[f.vnIndices[i]], textCoords[f.vtIndices[i]]); } } else { Debugger.Break(); } vOuput.AddRange(vertexArr); output.polygonList.Add(new Polygon(vertexArr)); } output.vertexArray = vOuput.ToArray(); Debug.WriteLine("Obj conversion complete: " + faces.Count + " faces were converted."); return output; }