public static void parsePLYFile(String fileName, CornerTableMesh container) { int lineNumber = 0; StreamReader reader = null; string temp = null; if (!fileName.EndsWith(".ply")) { MainMethod.reportError("PolyGonFileParser.parsePLYFile : Filename: \"" + fileName + "\" does not appear to be a polygon file"); } try { reader = new StreamReader(fileName); } catch (FileNotFoundException fnfe) { MainMethod.reportError("PolygonFilePasrer.parsePLYFile : Error could not find file. Exception message:" + fnfe.Message); return; } PLYFileHeader header = new PLYFileHeader(); lineNumber = parseHeader(reader, header); if (lineNumber < 0) { return; } List<Colour4f> colourList = new List<Colour4f>(); OpenTK.Vector3 tempPosition = new OpenTK.Vector3(0.0f, 0.0f, 0.0f); OpenTK.Vector3 tempNormal = new OpenTK.Vector3(0.0f, 0.0f, 0.0f); Colour4f tempColour = new Colour4f(0.0f, 0.0f, 0.0f, 0.0f); int vertexIndex = 0; int faceIndex = 0; int triangleIndex = 0; while (reader.Peek() > -1) { temp = getNewLine(reader, ref lineNumber); //First Parse Vertices if (vertexIndex < header.vertexCount) { if (String.IsNullOrWhiteSpace(temp)) { continue; } vertexIndex++; tempPosition.X = 0.0f; tempPosition.Y = 0.0f; tempPosition.Z = 0.0f; tempColour.red = Config.convertSettingToFloat("colours", "corner_default_red"); tempColour.green = Config.convertSettingToFloat("colours", "corner_default_green"); tempColour.blue = Config.convertSettingToFloat("colours", "corner_default_blue"); tempColour.alpha = Config.convertSettingToFloat("colours", "corner_default_alpha"); string[] pieces = temp.Split(' '); if (header.xIndex >= 0) { tempPosition.X = float.Parse(pieces[header.xIndex]); } if (header.yIndex >= 0) { tempPosition.Y = float.Parse(pieces[header.yIndex]); } if (header.zIndex >= 0) { tempPosition.Z = float.Parse(pieces[header.zIndex]); } if(header.normalXIndex >= 0) { tempNormal.X = float.Parse(pieces[header.normalXIndex]); } if(header.normalYIndex >= 0) { tempNormal.Y = float.Parse(pieces[header.normalYIndex]); } if(header.normalZIndex >= 0) { tempNormal.Z = float.Parse(pieces[header.normalZIndex]); } if (header.redIndex >= 0) { tempColour.red = float.Parse(pieces[header.redIndex]); } if (header.greenIndex >= 0) { tempColour.green = float.Parse(pieces[header.greenIndex]); } if (header.blueIndex >= 0) { tempColour.blue = float.Parse(pieces[header.blueIndex]); } if (header.alphaIndex >= 0) { tempColour.alpha = float.Parse(pieces[header.alphaIndex]); } if (header.normalXIndex != -1 && header.normalYIndex != -1 && header.normalZIndex != -1) { container.vertices.Add(new Vertex3f(tempPosition, tempNormal)); } else { container.vertices.Add(new Vertex3f(tempPosition)); } colourList.Add(new Colour4f(tempColour)); if (vertexIndex == header.vertexCount) { header.vertexIndexConversion = weldVertices(ref container.vertices); } } else if (faceIndex < header.faceCount) { if (String.IsNullOrWhiteSpace(temp)) { continue; } String[] pieces = temp.Split(' '); if (pieces.Length < 4) { MainMethod.reportError("Not enough information on line: " + lineNumber + " to make a face."); continue; } faceIndex++; if (pieces[0] == "3") { Corner tempCornerOne = new Corner(header.vertexIndexConversion[int.Parse(pieces[1])], container.vertices[header.vertexIndexConversion[int.Parse(pieces[1])]], colourList[int.Parse(pieces[1])], triangleIndex); Corner tempCornerTwo = new Corner(header.vertexIndexConversion[int.Parse(pieces[2])], container.vertices[header.vertexIndexConversion[int.Parse(pieces[2])]], colourList[int.Parse(pieces[2])], triangleIndex); Corner tempCornerThree = new Corner(header.vertexIndexConversion[int.Parse(pieces[3])], container.vertices[header.vertexIndexConversion[int.Parse(pieces[3])]], colourList[int.Parse(pieces[3])], triangleIndex); container.triangles.Add(tempCornerOne); container.triangles.Add(tempCornerTwo); container.triangles.Add(tempCornerThree); triangleIndex++; } else if (pieces[0] == "4") { Corner tempCornerOne = new Corner(header.vertexIndexConversion[int.Parse(pieces[1])], container.vertices[header.vertexIndexConversion[int.Parse(pieces[1])]], colourList[int.Parse(pieces[1])], triangleIndex); Corner tempCornerTwo = new Corner(header.vertexIndexConversion[int.Parse(pieces[2])], container.vertices[header.vertexIndexConversion[int.Parse(pieces[2])]], colourList[int.Parse(pieces[2])], triangleIndex); Corner tempCornerThree = new Corner(header.vertexIndexConversion[int.Parse(pieces[3])], container.vertices[header.vertexIndexConversion[int.Parse(pieces[3])]], colourList[int.Parse(pieces[3])], triangleIndex); triangleIndex++; Corner tempCornerFour = new Corner(header.vertexIndexConversion[int.Parse(pieces[1])], container.vertices[header.vertexIndexConversion[int.Parse(pieces[1])]], colourList[int.Parse(pieces[1])], triangleIndex); Corner tempCornerFive = new Corner(header.vertexIndexConversion[int.Parse(pieces[3])], container.vertices[header.vertexIndexConversion[int.Parse(pieces[3])]], colourList[int.Parse(pieces[3])], triangleIndex); Corner tempCornerSix = new Corner(header.vertexIndexConversion[int.Parse(pieces[4])], container.vertices[header.vertexIndexConversion[int.Parse(pieces[4])]], colourList[int.Parse(pieces[4])], triangleIndex); //Corner tempCornerFour = new Corner(int.Parse(pieces[1]), container.vertices[int.Parse(pieces[1])], colourList[int.Parse(pieces[1])], triangleIndex); //Corner tempCornerFive = new Corner(int.Parse(pieces[3]), container.vertices[int.Parse(pieces[3])], colourList[int.Parse(pieces[3])], triangleIndex); //Corner tempCornerSix = new Corner(int.Parse(pieces[4]), container.vertices[int.Parse(pieces[4])], colourList[int.Parse(pieces[4])], triangleIndex); triangleIndex++; container.triangles.Add(tempCornerOne); container.triangles.Add(tempCornerTwo); container.triangles.Add(tempCornerThree); container.triangles.Add(tempCornerFour); container.triangles.Add(tempCornerFive); container.triangles.Add(tempCornerSix); } else { MainMethod.reportError("Error program does not support meshes that are not quads or triangles"); } } else { //We dont' care about lines } } reader.Close(); }
//int texture2; ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// public CornerTableMesh(String fileName) { this.vertices = new List<Vertex3f>(); this.triangles = new List<Corner>(); if (fileName == null) { MainMethod.reportError("File name passed to Corner Table is null"); } else { PolygonFileParser.parsePLYFile(fileName, this); buildCornerInformation(); } selectedCorner = triangles[0]; /// <summary> /// ///////////////////////////////////////////////////////////////// /// </summary> /// <summary> /// Setup OpenGL and load resources here. /// </summary> /// <param name="e">Not used.</param> // texture/bitmap GL.ClearColor(Color.MidnightBlue); GL.Enable(EnableCap.Texture2D); GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); GL.GenTextures(1, out texture); GL.BindTexture(TextureTarget.Texture2D, texture); BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); bitmap.UnlockBits(data); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); /* // textrue2/bitmap2 GL.GenTextures(1, out texture2); GL.BindTexture(TextureTarget.Texture2D, texture2); BitmapData data2 = bitmap2.LockBits(new System.Drawing.Rectangle(0, 0, bitmap2.Width, bitmap2.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data2.Width, data2.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data2.Scan0); bitmap2.UnlockBits(data2); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); */ /// <summary> /// ///////////////////////////////////////////////////////////////// /// </summary> }