private static ImageBuffer LoadImageFromDisk(PartThumbnailWidget thumbnailWidget, string stlHashCode, Point2D size) { ImageBuffer tempImage = new ImageBuffer(size.x, size.y, 32, new BlenderBGRA()); string imageFileName = GetFilenameForSize(stlHashCode, size); if (File.Exists(imageFileName)) { if (partExtension == ".png") { if (ImageIO.LoadImageData(imageFileName, tempImage)) { return(tempImage); } } else { if (ImageTgaIO.LoadImageData(imageFileName, tempImage)) { return(tempImage); } } } return(null); }
public void TextWidgetVisibleTest() { GuiWidget rectangleWidget = new GuiWidget(100, 50); TextEditWidget itemToAdd = new TextEditWidget("test Item", 10, 10); rectangleWidget.AddChild(itemToAdd); rectangleWidget.DoubleBuffer = true; rectangleWidget.BackBuffer.NewGraphics2D().Clear(RGBA_Bytes.White); rectangleWidget.OnDraw(rectangleWidget.BackBuffer.NewGraphics2D()); ImageBuffer textOnly = new ImageBuffer(75, 20, 32, new BlenderBGRA()); textOnly.NewGraphics2D().Clear(RGBA_Bytes.White); #if true TypeFacePrinter stringPrinter = new TypeFacePrinter("test Item", 12); IVertexSource offsetText = new VertexSourceApplyTransform(stringPrinter, Affine.NewTranslation(1, -stringPrinter.LocalBounds.Bottom)); textOnly.NewGraphics2D().Render(offsetText, RGBA_Bytes.Black); #else textOnly.NewGraphics2D().DrawString("test Item", 1, 1); #endif if (saveImagesForDebug) { ImageTgaIO.Save(rectangleWidget.BackBuffer, "-rectangleWidget.tga"); //ImageTgaIO.Save(itemToAdd.Children[0].BackBuffer, "-internalTextWidget.tga"); ImageTgaIO.Save(textOnly, "-textOnly.tga"); } Assert.IsTrue(rectangleWidget.BackBuffer.FindLeastSquaresMatch(textOnly, 1), "TextWidgets need to be drawing."); rectangleWidget.Close(); }
private void SaveImage(ImageBuffer image, string dest) { if (saveImagesForDebug) { ImageTgaIO.Save(image, dest); } }
private static ImageBuffer LoadImageFromDisk(PartThumbnailWidget thumbnailWidget, string stlHashCode) { ImageBuffer tempImage = new ImageBuffer(BigRenderSize.x, BigRenderSize.y); string imageFileName = GetImageFileName(stlHashCode); if (File.Exists(imageFileName)) { if (partExtension == ".png") { if (ImageIO.LoadImageData(imageFileName, tempImage)) { return(tempImage); } } else { if (ImageTgaIO.LoadImageData(imageFileName, tempImage)) { return(tempImage); } } } return(null); }
public static ImageBuffer LoadImageFromDisk(string stlHashCode) { try { ImageBuffer tempImage = new ImageBuffer(BigRenderSize.x, BigRenderSize.y); string imageFileName = GetImageFileName(stlHashCode); if (File.Exists(imageFileName)) { if (partExtension == ".png") { if (ImageIO.LoadImageData(imageFileName, tempImage)) { return(tempImage); } } else { if (ImageTgaIO.LoadImageData(tempImage, imageFileName)) { return(tempImage); } } } } catch { } return(null); }
public new static GameObject Load(String PathName) { // First we load up the Data In the Serialization file. String gameDataObjectXMLPath = System.IO.Path.Combine(PathName, "ImageSequence"); GameImageSequence sequenceLoaded = LoadSerializationFileForFolder(gameDataObjectXMLPath); // Now lets look for and load up any images that we find. String[] tgaFilesArray = Directory.GetFiles(PathName, "*.tga"); List <String> sortedTgaFiles = new List <string>(tgaFilesArray); // Make sure they are sorted. sortedTgaFiles.Sort(); sequenceLoaded.m_Images = new ImageBuffer[sortedTgaFiles.Count]; int imageIndex = 0; foreach (String tgaFile in sortedTgaFiles) { sequenceLoaded.m_Images[imageIndex] = new ImageBuffer(new BlenderPreMultBGRA()); Stream imageStream = File.Open(tgaFile, FileMode.Open); ImageTgaIO.LoadImageData(sequenceLoaded.m_Images[imageIndex], imageStream, 32); imageIndex++; } if (sequenceLoaded.m_CenterOriginDurringPreprocessing) { sequenceLoaded.CenterOriginOffset(); } if (sequenceLoaded.m_CropToVisibleDurringPreprocessing) { sequenceLoaded.CropToVisible(); } return(sequenceLoaded); }
private static void OutputImage(ImageBuffer imageToOutput, string fileName) { if (saveImagesForDebug) { ImageTgaIO.Save(imageToOutput, fileName); } }
private void OutputImage(ImageBuffer imageToOutput, string fileName) { if (SaveImagesForDebug) { var dirAndFileName = Path.Combine("C:/Temp", fileName); ImageTgaIO.Save(imageToOutput, dirAndFileName); } }
private void OutputImages(GuiWidget control, GuiWidget test) { if (saveImagesForDebug) { ImageTgaIO.Save(control.BackBuffer, "image-control.tga"); ImageTgaIO.Save(test.BackBuffer, "image-test.tga"); } }
private void OutputImages(GuiWidget control, GuiWidget test) { if (saveImagesForDebug) { string outputPath = TestContext.CurrentContext.WorkDirectory; ImageTgaIO.Save(control.BackBuffer, Path.Combine(outputPath, "image-control.tga")); ImageTgaIO.Save(test.BackBuffer, Path.Combine(outputPath, "image-test.tga")); } }
public void CompareToLionTGA() { LionShape lionShape = new LionShape(); ImageBuffer renderedImage = new ImageBuffer(512, 400, 24, new BlenderBGR()); byte alpha = (byte)(.1 * 255); for (int i = 0; i < lionShape.NumPaths; i++) { lionShape.Colors[i].Alpha0To255 = alpha; } Affine transform = Affine.NewIdentity(); transform *= Affine.NewTranslation(-lionShape.Center.x, -lionShape.Center.y); transform *= Affine.NewTranslation(renderedImage.Width / 2, renderedImage.Height / 2); // This code renders the lion: VertexSourceApplyTransform transformedPathStorage = new VertexSourceApplyTransform(lionShape.Path, transform); Graphics2D renderer = renderedImage.NewGraphics2D(); renderer.Clear(new RGBA_Floats(1.0, 1.0, 1.0, 1.0)); renderer.Render(transformedPathStorage, lionShape.Colors, lionShape.PathIndex, lionShape.NumPaths); ImageTgaIO.Save(renderedImage, "TestOutput.tga"); Stream imageStream = File.Open("LionRenderMaster.tga", FileMode.Open); ImageBuffer masterImage = new ImageBuffer(); ImageTgaIO.LoadImageData(masterImage, imageStream, 24); bool sameWidth = masterImage.Width == renderedImage.Width; bool sameHeight = masterImage.Height == renderedImage.Height; Assert.IsTrue(sameWidth && sameHeight); Assert.IsTrue(masterImage.BitDepth == renderedImage.BitDepth); int unused; byte[] masterBuffer = masterImage.GetBuffer(out unused); byte[] renderedBuffer = renderedImage.GetBuffer(out unused); Assert.IsTrue(masterBuffer.Length == renderedBuffer.Length); for (int i = 0; i < masterBuffer.Length; i++) { if (masterBuffer[i] != renderedBuffer[i]) { Assert.IsTrue(false); } } }
private static ImageBuffer LoadImageFromDisk(PartThumbnailWidget thumbnailWidget, string stlHashCode, Point2D size) { ImageBuffer tempImage = new ImageBuffer(size.x, size.y, 32, new BlenderBGRA()); string applicationUserDataPath = ApplicationDataStorage.Instance.ApplicationUserDataPath; string folderToSavePrintsTo = Path.Combine(applicationUserDataPath, "data", "temp", "thumbnails"); string tgaFileName = Path.Combine(folderToSavePrintsTo, "{0}_{1}x{2}.tga".FormatWith(stlHashCode, size.x, size.y)); if (File.Exists(tgaFileName)) { if (ImageTgaIO.LoadImageData(tgaFileName, tempImage)) { return(tempImage); } } return(null); }
private static ImageBuffer BuildImageFromSTL(Mesh loadedMesh, string stlHashCode, Point2D size) { if (loadedMesh != null) { ImageBuffer tempImage = new ImageBuffer(size.x, size.y, 32, new BlenderBGRA()); Graphics2D partGraphics2D = tempImage.NewGraphics2D(); partGraphics2D.Clear(new RGBA_Bytes()); List <MeshEdge> nonManifoldEdges = loadedMesh.GetNonManifoldEdges(); if (nonManifoldEdges.Count > 0) { if (File.Exists("RunUnitTests.txt")) { partGraphics2D.Circle(4, 4, 4, RGBA_Bytes.Red); } } nonManifoldEdges = null; AxisAlignedBoundingBox aabb = loadedMesh.GetAxisAlignedBoundingBox(); double maxSize = Math.Max(aabb.XSize, aabb.YSize); double scale = size.x / (maxSize * 1.2); RectangleDouble bounds2D = new RectangleDouble(aabb.minXYZ.x, aabb.minXYZ.y, aabb.maxXYZ.x, aabb.maxXYZ.y); PolygonMesh.Rendering.OrthographicZProjection.DrawTo(partGraphics2D, loadedMesh, new Vector2((size.x / scale - bounds2D.Width) / 2 - bounds2D.Left, (size.y / scale - bounds2D.Height) / 2 - bounds2D.Bottom), scale, RGBA_Bytes.White); // and save it to disk string applicationUserDataPath = ApplicationDataStorage.Instance.ApplicationUserDataPath; string folderToSavePrintsTo = Path.Combine(applicationUserDataPath, "data", "temp", "thumbnails"); string tgaFileName = Path.Combine(folderToSavePrintsTo, "{0}_{1}x{2}.tga".FormatWith(stlHashCode, size.x, size.y)); if (!Directory.Exists(folderToSavePrintsTo)) { Directory.CreateDirectory(folderToSavePrintsTo); } ImageTgaIO.SaveImageData(tgaFileName, tempImage); // and give it back return(tempImage); } return(null); }
private static IImageByte DrawBorderOnSurface(BorderDouble border, string name) { var widget = GetBorderedWidget(border, name, doubleBuffer: true); var graphics2D = widget.NewGraphics2D(); graphics2D.Clear(Color.White); widget.OnDraw(graphics2D); if (debugResult) { string filePath = $@"c:\temp\{name}.tga"; ImageTgaIO.Save(widget.BackBuffer, filePath); Process.Start(filePath); } return(graphics2D.DestImage); }
private void CheckTestAgainstControl(ImageBuffer testImage, string testTypeString) { Directory.SetCurrentDirectory(TestContext.CurrentContext.TestDirectory); // there is an assumption that we got to save valid images at least once. string controlFileTga = testTypeString + " Control.tga"; string imageFolder = "ControlImages"; string testFailPathAndFileName = Path.Combine(imageFolder, testTypeString + " Test Fail.tga"); ImageBuffer controlImage = new ImageBuffer(); if (!Directory.Exists(imageFolder)) { Directory.CreateDirectory(imageFolder); } string controlPathAndFileName = Path.Combine(imageFolder, controlFileTga); if (File.Exists(controlPathAndFileName)) { ImageTgaIO.LoadImageData(controlImage, controlPathAndFileName); bool testIsSameAsControl = controlImage.Equals(testImage); if (!testIsSameAsControl) { // this image will be in the current output folder inside of imageFolder ImageTgaIO.Save(testImage, testFailPathAndFileName); } else if (File.Exists(testFailPathAndFileName)) { // we don't want to have these confounding our results. File.Delete(testFailPathAndFileName); } Assert.IsTrue(testIsSameAsControl); // If you want to create new control images select SetNextStatement to inside the else condition to create them. } else { ImageTgaIO.Save(testImage, controlPathAndFileName); } }
public static IObject3D Load(Stream fileStream, CancellationToken cancellationToken, Action <double, string> reportProgress = null, IObject3D source = null) { IObject3D root = source ?? new Object3D(); Stopwatch time = Stopwatch.StartNew(); // LOAD THE MESH DATA Obj objFile = new Obj(); objFile.LoadObj(fileStream); IObject3D context = new Object3D(); root.Children.Add(context); var mesh = new Mesh(); context.SetMeshDirect(mesh); foreach (var vertex in objFile.VertexList) { mesh.CreateVertex(vertex.X, vertex.Y, vertex.Z, CreateOption.CreateNew, SortOption.WillSortLater); } foreach (var face in objFile.FaceList) { List <int> zeroBased = new List <int>(face.VertexIndexList.Length); foreach (var index in face.VertexIndexList) { zeroBased.Add(index - 1); } mesh.CreateFace(zeroBased.ToArray(), CreateOption.CreateNew); } // load and apply any texture if (objFile.Material != "") { // TODO: have consideration for this being in a shared zip file string pathToObj = Path.GetDirectoryName(((FileStream)fileStream).Name); using (var materialsStream = File.OpenRead(Path.Combine(pathToObj, objFile.Material))) { var mtl = new Mtl(); mtl.LoadMtl(materialsStream); foreach (var material in mtl.MaterialList) { if (!string.IsNullOrEmpty(material.DiffuseTextureFileName)) { var pathToTexture = Path.Combine(pathToObj, material.DiffuseTextureFileName); if (File.Exists(pathToTexture)) { ImageBuffer diffuseTexture = new ImageBuffer(); // TODO: have consideration for this being in a shared zip file using (var ImageStream = File.OpenRead(pathToTexture)) { if (Path.GetExtension(material.DiffuseTextureFileName).ToLower() == ".tga") { ImageTgaIO.LoadImageData(diffuseTexture, ImageStream, 32); } else { AggContext.ImageIO.LoadImageData(ImageStream, diffuseTexture); } } if (diffuseTexture.Width > 0 && diffuseTexture.Height > 0) { for (int faceIndex = 0; faceIndex < objFile.FaceList.Count; faceIndex++) { var faceData = objFile.FaceList[faceIndex]; var polyFace = mesh.Faces[faceIndex]; polyFace.SetTexture(0, diffuseTexture); int edgeIndex = 0; foreach (FaceEdge faceEdge in polyFace.FaceEdges()) { int textureIndex = faceData.TextureVertexIndexList[edgeIndex] - 1; faceEdge.SetUv(0, new Vector2(objFile.TextureList[textureIndex].X, objFile.TextureList[textureIndex].Y)); edgeIndex++; } } context.Color = Color.White; root.Color = Color.White; } } } } } } time.Stop(); Debug.WriteLine(string.Format("OBJ Load in {0:0.00}s", time.Elapsed.TotalSeconds)); time.Restart(); bool hasValidMesh = root.Children.Where(item => item.Mesh.Faces.Count > 0).Any(); Debug.WriteLine("hasValidMesh: " + time.ElapsedMilliseconds); reportProgress?.Invoke(1, ""); return((hasValidMesh) ? root : null); }
private static ImageBuffer BuildImageFromMeshGroups(List <MeshGroup> loadedMeshGroups, string stlHashCode, Point2D size) { if (loadedMeshGroups != null && loadedMeshGroups.Count > 0 && loadedMeshGroups[0].Meshes != null && loadedMeshGroups[0].Meshes[0] != null) { ImageBuffer tempImage = new ImageBuffer(size.x, size.y, 32, new BlenderBGRA()); Graphics2D partGraphics2D = tempImage.NewGraphics2D(); partGraphics2D.Clear(new RGBA_Bytes()); AxisAlignedBoundingBox aabb = loadedMeshGroups[0].GetAxisAlignedBoundingBox(); for (int meshGroupIndex = 1; meshGroupIndex < loadedMeshGroups.Count; meshGroupIndex++) { aabb = AxisAlignedBoundingBox.Union(aabb, loadedMeshGroups[meshGroupIndex].GetAxisAlignedBoundingBox()); } double maxSize = Math.Max(aabb.XSize, aabb.YSize); double scale = size.x / (maxSize * 1.2); RectangleDouble bounds2D = new RectangleDouble(aabb.minXYZ.x, aabb.minXYZ.y, aabb.maxXYZ.x, aabb.maxXYZ.y); foreach (MeshGroup meshGroup in loadedMeshGroups) { foreach (Mesh loadedMesh in meshGroup.Meshes) { PolygonMesh.Rendering.OrthographicZProjection.DrawTo(partGraphics2D, loadedMesh, new Vector2((size.x / scale - bounds2D.Width) / 2 - bounds2D.Left, (size.y / scale - bounds2D.Height) / 2 - bounds2D.Bottom), scale, RGBA_Bytes.White); } } if (File.Exists("RunUnitTests.txt")) { foreach (Mesh loadedMesh in loadedMeshGroups[0].Meshes) { List <MeshEdge> nonManifoldEdges = loadedMesh.GetNonManifoldEdges(); if (nonManifoldEdges.Count > 0) { partGraphics2D.Circle(size.x / 4, size.x / 4, size.x / 8, RGBA_Bytes.Red); } } } // and save it to disk string imageFileName = GetFilenameForSize(stlHashCode, size); string folderToSavePrintsTo = Path.GetDirectoryName(imageFileName); if (!Directory.Exists(folderToSavePrintsTo)) { Directory.CreateDirectory(folderToSavePrintsTo); } if (partExtension == ".png") { ImageIO.SaveImageData(imageFileName, tempImage); } else { ImageTgaIO.SaveImageData(imageFileName, tempImage); } // and give it back return(tempImage); } return(null); }
private void CreateThumbnail() { string stlHashCode = this.ItemWrapper.FileHashCode.ToString(); ImageBuffer bigRender = new ImageBuffer(); if (!File.Exists(this.ItemWrapper.FileLocation)) { return; } List <MeshGroup> loadedMeshGroups = MeshFileIo.Load(this.ItemWrapper.FileLocation); RenderType renderType = GetRenderType(this.ItemWrapper.FileLocation); switch (renderType) { case RenderType.RAY_TRACE: { ThumbnailTracer tracer = new ThumbnailTracer(loadedMeshGroups, BigRenderSize.x, BigRenderSize.y); tracer.DoTrace(); bigRender = tracer.destImage; } break; case RenderType.PERSPECTIVE: { ThumbnailTracer tracer = new ThumbnailTracer(loadedMeshGroups, BigRenderSize.x, BigRenderSize.y); this.thumbnailImage = new ImageBuffer(this.buildingThumbnailImage); this.thumbnailImage.NewGraphics2D().Clear(new RGBA_Bytes(255, 255, 255, 0)); bigRender = new ImageBuffer(BigRenderSize.x, BigRenderSize.y); foreach (MeshGroup meshGroup in loadedMeshGroups) { double minZ = double.MaxValue; double maxZ = double.MinValue; foreach (Mesh loadedMesh in meshGroup.Meshes) { tracer.GetMinMaxZ(loadedMesh, ref minZ, ref maxZ); } foreach (Mesh loadedMesh in meshGroup.Meshes) { tracer.DrawTo(bigRender.NewGraphics2D(), loadedMesh, RGBA_Bytes.White, minZ, maxZ); } } if (bigRender == null) { bigRender = new ImageBuffer(this.noThumbnailImage); } } break; case RenderType.NONE: case RenderType.ORTHOGROPHIC: this.thumbnailImage = new ImageBuffer(this.buildingThumbnailImage); this.thumbnailImage.NewGraphics2D().Clear(new RGBA_Bytes(255, 255, 255, 0)); bigRender = BuildImageFromMeshGroups(loadedMeshGroups, stlHashCode, BigRenderSize); if (bigRender == null) { bigRender = new ImageBuffer(this.noThumbnailImage); } break; } // and save it to disk string imageFileName = GetImageFileName(stlHashCode); if (partExtension == ".png") { ImageIO.SaveImageData(imageFileName, bigRender); } else { ImageTgaIO.SaveImageData(imageFileName, bigRender); } bigRender.SetRecieveBlender(new BlenderPreMultBGRA()); this.thumbnailImage = ImageBuffer.CreateScaledImage(bigRender, (int)Width, (int)Height); UiThread.RunOnIdle(this.EnsureImageUpdated); OnDoneRendering(); }
private void CreateThumbnail() { string stlHashCode = this.PrintItem.FileHashCode.ToString(); ImageBuffer bigRender = new ImageBuffer(); if (!File.Exists(this.PrintItem.FileLocation)) { return; } List <MeshGroup> loadedMeshGroups = MeshFileIo.Load(this.PrintItem.FileLocation); RenderType renderType = GetRenderType(this.PrintItem.FileLocation); switch (renderType) { case RenderType.RAY_TRACE: { ThumbnailTracer tracer = new ThumbnailTracer(loadedMeshGroups, BigRenderSize.x, BigRenderSize.y); tracer.DoTrace(); bigRender = tracer.destImage; } break; case RenderType.PERSPECTIVE: { ThumbnailTracer tracer = new ThumbnailTracer(loadedMeshGroups, BigRenderSize.x, BigRenderSize.y); this.thumbnailImage = new ImageBuffer(this.buildingThumbnailImage); this.thumbnailImage.NewGraphics2D().Clear(new RGBA_Bytes(255, 255, 255, 0)); bigRender = new ImageBuffer(BigRenderSize.x, BigRenderSize.y, 32, new BlenderBGRA()); foreach (MeshGroup meshGroup in loadedMeshGroups) { double minZ = double.MaxValue; double maxZ = double.MinValue; foreach (Mesh loadedMesh in meshGroup.Meshes) { tracer.GetMinMaxZ(loadedMesh, ref minZ, ref maxZ); } foreach (Mesh loadedMesh in meshGroup.Meshes) { tracer.DrawTo(bigRender.NewGraphics2D(), loadedMesh, RGBA_Bytes.White, minZ, maxZ); } } if (bigRender == null) { bigRender = new ImageBuffer(this.noThumbnailImage); } } break; case RenderType.NONE: case RenderType.ORTHOGROPHIC: this.thumbnailImage = new ImageBuffer(this.buildingThumbnailImage); this.thumbnailImage.NewGraphics2D().Clear(new RGBA_Bytes(255, 255, 255, 0)); bigRender = BuildImageFromMeshGroups(loadedMeshGroups, stlHashCode, BigRenderSize); if (bigRender == null) { bigRender = new ImageBuffer(this.noThumbnailImage); } break; } // and save it to disk string imageFileName = GetImageFileName(stlHashCode); if (partExtension == ".png") { ImageIO.SaveImageData(imageFileName, bigRender); } else { ImageTgaIO.SaveImageData(imageFileName, bigRender); } ImageBuffer unScaledImage = new ImageBuffer(bigRender.Width, bigRender.Height, 32, new BlenderBGRA()); unScaledImage.NewGraphics2D().Render(bigRender, 0, 0); // If the source image (the one we downloaded) is more than twice as big as our dest image. while (unScaledImage.Width > Width * 2) { // The image sampler we use is a 2x2 filter so we need to scale by a max of 1/2 if we want to get good results. // So we scale as many times as we need to to get the Image to be the right size. // If this were going to be a non-uniform scale we could do the x and y separatly to get better results. ImageBuffer halfImage = new ImageBuffer(unScaledImage.Width / 2, unScaledImage.Height / 2, 32, new BlenderBGRA()); halfImage.NewGraphics2D().Render(unScaledImage, 0, 0, 0, halfImage.Width / (double)unScaledImage.Width, halfImage.Height / (double)unScaledImage.Height); unScaledImage = halfImage; } this.thumbnailImage = new ImageBuffer((int)Width, (int)Height, 32, new BlenderBGRA()); this.thumbnailImage.NewGraphics2D().Clear(new RGBA_Bytes(255, 255, 255, 0)); this.thumbnailImage.NewGraphics2D().Render(unScaledImage, 0, 0, 0, (double)this.thumbnailImage.Width / unScaledImage.Width, (double)this.thumbnailImage.Height / unScaledImage.Height); UiThread.RunOnIdle(this.EnsureImageUpdated); OnDoneRendering(); }
public static IObject3D Load(Stream fileStream, Action <double, string> reportProgress = null, IObject3D source = null) { IObject3D root = source ?? new Object3D(); var time = Stopwatch.StartNew(); // LOAD THE MESH DATA var objFile = new Obj(); objFile.LoadObj(fileStream); IObject3D context = new Object3D(); root.Children.Add(context); var mesh = new Mesh(); context.SetMeshDirect(mesh); foreach (var vertex in objFile.VertexList) { mesh.Vertices.Add(new Vector3Float(vertex.X, vertex.Y, vertex.Z)); } foreach (var face in objFile.FaceList) { for (int i = 0; i < face.VertexIndexList.Length; i++) { if (face.VertexIndexList[i] >= objFile.TextureList.Count) { int a = 0; } } mesh.Faces.Add(face.VertexIndexList[0] - 1, face.VertexIndexList[1] - 1, face.VertexIndexList[2] - 1, mesh.Vertices); if (face.VertexIndexList.Length == 4) { // add the other side of the quad mesh.Faces.Add(face.VertexIndexList[0] - 1, face.VertexIndexList[2] - 1, face.VertexIndexList[3] - 1, mesh.Vertices); } } // load and apply any texture if (objFile.Material != "") { // TODO: have consideration for this being in a shared zip file string pathToObj = Path.GetDirectoryName(((FileStream)fileStream).Name); //Try-catch block for when objFile.Material is not found try { using (var materialsStream = File.OpenRead(Path.Combine(pathToObj, objFile.Material))) { var mtl = new Mtl(); mtl.LoadMtl(materialsStream); foreach (var material in mtl.MaterialList) { if (!string.IsNullOrEmpty(material.DiffuseTextureFileName)) { var pathToTexture = Path.Combine(pathToObj, material.DiffuseTextureFileName); if (File.Exists(pathToTexture)) { var diffuseTexture = new ImageBuffer(); // TODO: have consideration for this being in a shared zip file using (var imageStream = File.OpenRead(pathToTexture)) { if (Path.GetExtension(material.DiffuseTextureFileName).ToLower() == ".tga") { ImageTgaIO.LoadImageData(diffuseTexture, imageStream, 32); } else { ImageIO.LoadImageData(imageStream, diffuseTexture); } } if (diffuseTexture.Width > 0 && diffuseTexture.Height > 0) { int meshFace = 0; for (int objFace = 0; objFace < objFile.FaceList.Count; objFace++, meshFace++) { var face = mesh.Faces[meshFace]; var faceData = objFile.FaceList[objFace]; int textureIndex0 = faceData.TextureVertexIndexList[0] - 1; var uv0 = new Vector2Float(objFile.TextureList[textureIndex0].X, objFile.TextureList[textureIndex0].Y); int textureIndex1 = faceData.TextureVertexIndexList[1] - 1; var uv1 = new Vector2Float(objFile.TextureList[textureIndex1].X, objFile.TextureList[textureIndex1].Y); int textureIndex2 = faceData.TextureVertexIndexList[2] - 1; var uv2 = new Vector2Float(objFile.TextureList[textureIndex2].X, objFile.TextureList[textureIndex2].Y); mesh.FaceTextures.Add(meshFace, new FaceTextureData(diffuseTexture, uv0, uv1, uv2)); if (faceData.TextureVertexIndexList.Length == 4) { meshFace++; int textureIndex3 = faceData.TextureVertexIndexList[3] - 1; var uv3 = new Vector2Float(objFile.TextureList[textureIndex3].X, objFile.TextureList[textureIndex3].Y); mesh.FaceTextures.Add(meshFace, new FaceTextureData(diffuseTexture, uv0, uv2, uv3)); } } context.Color = Color.White; root.Color = Color.White; } } } } } } catch (FileNotFoundException) { // Just continue as if obj.Material == "" to show object } } time.Stop(); Debug.WriteLine(string.Format("OBJ Load in {0:0.00}s", time.Elapsed.TotalSeconds)); time.Restart(); bool hasValidMesh = root.Children.Where(item => item.Mesh.Faces.Count > 0).Any(); Debug.WriteLine("hasValidMesh: " + time.ElapsedMilliseconds); reportProgress?.Invoke(1, ""); return(hasValidMesh ? root : null); }
public static void Main(string[] args) { // first we will show how to use the simple drawing functions in graphics 2D { ImageBuffer simpleImage = new ImageBuffer(640, 480, 32, new BlenderBGRA()); Graphics2D simpleImageGraphics2D = simpleImage.NewGraphics2D(); // clear the image to white simpleImageGraphics2D.Clear(Color.White); // draw a circle simpleImageGraphics2D.Circle(50, 50, 30, Color.Blue); // draw a line simpleImageGraphics2D.Line(10, 100, 520, 50, new Color(20, 200, 200)); // draw a filled box simpleImageGraphics2D.FillRectangle(60, 260, 200, 280, Color.Yellow); // and an outline around it simpleImageGraphics2D.Rectangle(60, 260, 200, 280, Color.Magenta); // draw some text simpleImageGraphics2D.DrawString("A Simple Example", 300, 400, 20); // and save this image out ImageTgaIO.Save(simpleImage, "SimpleDrawAndSave.tga"); } // now we will we will show how to use the render function to draw more complex things { ImageBuffer lessSimpleImage = new ImageBuffer(640, 480, 32, new BlenderBGRA()); Graphics2D lessSimpleImageGraphics2D = lessSimpleImage.NewGraphics2D(); // clear the image to white lessSimpleImageGraphics2D.Clear(Color.White); // draw a circle Ellipse ellipseTest = new Ellipse(0, 0, 100, 50); for (double angleDegrees = 0; angleDegrees < 180; angleDegrees += 22.5) { VertexSourceApplyTransform rotatedTransform = new VertexSourceApplyTransform(ellipseTest, Affine.NewRotation(MathHelper.DegreesToRadians(angleDegrees))); VertexSourceApplyTransform rotatedAndTranslatedTransform = new VertexSourceApplyTransform(rotatedTransform, Affine.NewTranslation(lessSimpleImage.Width / 2, 150)); lessSimpleImageGraphics2D.Render(rotatedAndTranslatedTransform, Color.Yellow); Stroke ellipseOutline = new Stroke(rotatedAndTranslatedTransform, 3); lessSimpleImageGraphics2D.Render(ellipseOutline, Color.Blue); } // and a little polygon VertexStorage littlePoly = new VertexStorage(); littlePoly.MoveTo(50, 50); littlePoly.LineTo(150, 50); littlePoly.LineTo(200, 200); littlePoly.LineTo(50, 150); littlePoly.LineTo(50, 50); lessSimpleImageGraphics2D.Render(littlePoly, Color.Cyan); // draw some text TypeFacePrinter textPrinter = new TypeFacePrinter("Printing from a printer", 30, justification: Justification.Center); IVertexSource translatedText = new VertexSourceApplyTransform(textPrinter, Affine.NewTranslation(new Vector2(lessSimpleImage.Width / 2, lessSimpleImage.Height / 4 * 3))); lessSimpleImageGraphics2D.Render(translatedText, Color.Red); Stroke strokedText = new Stroke(translatedText); lessSimpleImageGraphics2D.Render(strokedText, Color.Black); IVertexSource rotatedText = new VertexSourceApplyTransform(textPrinter, Affine.NewRotation(MathHelper.DegreesToRadians(90))); IVertexSource rotatedTranslatedText = new VertexSourceApplyTransform(rotatedText, Affine.NewTranslation(new Vector2(40, lessSimpleImage.Height / 2))); lessSimpleImageGraphics2D.Render(rotatedTranslatedText, Color.Black); // and save this image out ImageTgaIO.Save(lessSimpleImage, "LessSimpleDrawAndSave.tga"); } }