/* * FOLLOWING CODE HAS BEEN DIRECTLY ADDED FROM * TOPIC4, WHICH WAS FOUND ON LEARNIT! * Only very minor changes has been made to adjust * for a change in programming language. */ private void DrawObjs() { Vertex v1, v2, v3; Vertex n1, n2, n3; var TexCoord = new double[2]; // Enable Texturing and setup. GL.Enable(EnableCap.Texture2D); var skyGraphics = skybox.GraphicsObj; List <Triangle> skyboxTriangles = graphController.GetShape(skyGraphics.ShapeIndex); //DrawSkyBox(); // Currently a background (not working) GL.Enable(EnableCap.Lighting); for (int j = 0; j < drawObjList.Count; j++) { var curSimObj = drawObjList[j]; Graphics.GraphicsObject curGraphObj = curSimObj.GraphicsObj; List <Triangle> shape = graphController.GetShape(curGraphObj.ShapeIndex); if (j < 7) { GL.BindTexture(TextureTarget.Texture2D, textureIds[j]); } else { GL.BindTexture(TextureTarget.Texture2D, textureIds[7]); } for (int i = 0; i < shape.Count; i++) { v1 = curGraphObj.vertices[shape[i].vi1]; v2 = curGraphObj.vertices[shape[i].vi2]; v3 = curGraphObj.vertices[shape[i].vi3]; n1 = curGraphObj.normals[shape[i].ni1]; n2 = curGraphObj.normals[shape[i].ni2]; n3 = curGraphObj.normals[shape[i].ni3]; GL.Begin(PrimitiveType.Triangles); GL.TexCoord2(v1.u, v1.v); GL.Normal3(n1.x, n1.y, n1.z); GL.Vertex3(v1.x, v1.y, v1.z); GL.TexCoord2(v2.u, v2.v); GL.Normal3(n2.x, n2.y, n2.z); GL.Vertex3(v2.x, v2.y, v2.z); GL.TexCoord2(v3.u, v3.v); GL.Normal3(n3.x, n3.y, n3.z); GL.Vertex3(v3.x, v3.y, v3.z); GL.End(); } } }
private void TransformObjs(List <Matrix4> transMatrix) { for (int simIndex = 0; simIndex < drawObjList.Count; simIndex++) { Graphics.GraphicsObject gObj = drawObjList[simIndex].GraphicsObj; Matrix4 curTransMat = transMatrix[simIndex]; TransformObj(gObj, curTransMat); } }
public void ReadObjFile(String filename) { List <Vertex> newVertices = new List <Vertex>(); List <Vertex> newNormals = new List <Vertex>(); List <Triangle> newTriangles = new List <Triangle>(); using (System.IO.StreamReader streamReader = new System.IO.StreamReader(filename)) { String curLine = ""; String[] curLineData; // Neccessary for double string conversion. // This is due to commas being used instead of punctuation in some cultures. System.Globalization.NumberFormatInfo numFormat = System.Globalization.CultureInfo.GetCultureInfo("en-US").NumberFormat; while (!streamReader.EndOfStream) { curLine = streamReader.ReadLine(); curLineData = curLine.Split(' '); switch (curLineData[0]) { case "v": { Vertex newVertex = new Vertex(); newVertex.x = Double.Parse(curLineData[1], numFormat); newVertex.y = Double.Parse(curLineData[2], numFormat); newVertex.z = Double.Parse(curLineData[3], numFormat); double[] texCoords = GetTextureCoord(newVertex.x, newVertex.y, newVertex.z); newVertex.u = texCoords[0]; newVertex.v = texCoords[1]; newVertices.Add(newVertex); break; } case "vn": { Vertex newNormal = new Vertex(); newNormal.x = Double.Parse(curLineData[1], numFormat); newNormal.y = Double.Parse(curLineData[2], numFormat); newNormal.z = Double.Parse(curLineData[3], numFormat); newNormals.Add(newNormal); break; } case "f": { Triangle newTriangle = new Triangle(); StringBuilder strSanitizer = new StringBuilder(); /* * Creates a stringbuilder to edit input data for easier manipulation. * The string sanitizer creates a new string where all values are seperated * by '/', and then resplits that string into a new array. */ strSanitizer.Append(curLineData[1] + '/' + curLineData[2] + '/' + curLineData[3]); strSanitizer.Replace("//", "/"); String[] vertice_normal_array = strSanitizer.ToString().Split('/'); // Conversion from .obj files 1-indexing to ordinary programming happens here. newTriangle.vi1 = Int32.Parse(vertice_normal_array[0]) - 1; newTriangle.ni1 = Int32.Parse(vertice_normal_array[1]) - 1; newTriangle.vi2 = Int32.Parse(vertice_normal_array[2]) - 1; newTriangle.ni2 = Int32.Parse(vertice_normal_array[3]) - 1; newTriangle.vi3 = Int32.Parse(vertice_normal_array[4]) - 1; newTriangle.ni3 = Int32.Parse(vertice_normal_array[5]) - 1; strSanitizer.Clear(); newTriangles.Add(newTriangle); break; } default: throw new ArgumentException("Input file contains line not starting with v, vn, or f. Please check input."); } } } GraphicsObject newGraphObj = new GraphicsObject(newVertices, newNormals, shapeIndex); this.gfxObjs.Add(newGraphObj); this.shapes.Add(newTriangles); shapeIndex++; }