/// <summary> /// IDWriteTextLayout::Draw calls this function to instruct the client to render a run of glyphs. /// </summary> /// <param name="clientDrawingContext">The application-defined drawing context passed to <see cref="M:SharpDX.DirectWrite.TextLayout.Draw_(System.IntPtr,System.IntPtr,System.Single,System.Single)" />.</param> /// <param name="baselineOriginX">The pixel location (X-coordinate) at the baseline origin of the glyph run.</param> /// <param name="baselineOriginY">The pixel location (Y-coordinate) at the baseline origin of the glyph run.</param> /// <param name="measuringMode">The measuring method for glyphs in the run, used with the other properties to determine the rendering mode.</param> /// <param name="glyphRun">Pointer to the glyph run instance to render.</param> /// <param name="glyphRunDescription">A pointer to the optional glyph run description instance which contains properties of the characters associated with this run.</param> /// <param name="clientDrawingEffect">Application-defined drawing effects for the glyphs to render. Usually this argument represents effects such as the foreground brush filling the interior of text.</param> /// <returns> /// If the method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. /// </returns> /// <unmanaged>HRESULT DrawGlyphRun([None] void* clientDrawingContext,[None] FLOAT baselineOriginX,[None] FLOAT baselineOriginY,[None] DWRITE_MEASURING_MODE measuringMode,[In] const DWRITE_GLYPH_RUN* glyphRun,[In] const DWRITE_GLYPH_RUN_DESCRIPTION* glyphRunDescription,[None] IUnknown* clientDrawingEffect)</unmanaged> /// <remarks> /// The <see cref="M:SharpDX.DirectWrite.TextLayout.Draw_(System.IntPtr,System.IntPtr,System.Single,System.Single)" /> function calls this callback function with all the information about glyphs to render. The application implements this callback by mostly delegating the call to the underlying platform's graphics API such as {{Direct2D}} to draw glyphs on the drawing context. An application that uses GDI can implement this callback in terms of the <see cref="M:SharpDX.DirectWrite.BitmapRenderTarget.DrawGlyphRun(System.Single,System.Single,SharpDX.Direct2D1.MeasuringMode,SharpDX.DirectWrite.GlyphRun,SharpDX.DirectWrite.RenderingParams,SharpDX.Color4)" /> method. /// </remarks> public override SDX.Result DrawGlyphRun( object clientDrawingContext, float baselineOriginX, float baselineOriginY, MeasuringMode measuringMode, GlyphRun glyphRun, GlyphRunDescription glyphRunDescription, SDX.ComObject clientDrawingEffect) { if ((glyphRun.Indices == null) || (glyphRun.Indices.Length == 0)) { return(SDX.Result.Ok);; } SharpDX.DirectWrite.Factory dWriteFactory = GraphicsCore.Current.FactoryDWrite; SharpDX.Direct2D1.Factory d2DFactory = GraphicsCore.Current.FactoryD2D; // Extrude geometry data out of given glyph run SimplePolygon2DGeometrySink geometryExtruder = new SimplePolygon2DGeometrySink(new Vector2(baselineOriginX, baselineOriginY)); using (PathGeometry pathGeometry = new PathGeometry(d2DFactory)) { // Write all geometry data into a standard PathGeometry object using (GeometrySink geoSink = pathGeometry.Open()) { glyphRun.FontFace.GetGlyphRunOutline( glyphRun.FontSize, glyphRun.Indices, glyphRun.Advances, glyphRun.Offsets, glyphRun.IsSideways, glyphRun.BidiLevel % 2 == 1, geoSink); geoSink.Close(); } // Simplify written geometry and write it into own structure pathGeometry.Simplify(GeometrySimplificationOption.Lines, m_geometryOptions.SimplificationFlatternTolerance, geometryExtruder); } // Structure for caching the result VertexStructure tempStructure = new VertexStructure(); VertexStructureSurface tempSurface = tempStructure.CreateSurface(); // Create the text surface if (m_geometryOptions.MakeSurface) { // Separate polygons by clock direction // Order polygons as needed for further hole finding algorithm IEnumerable <Polygon2D> fillingPolygons = geometryExtruder.GeneratedPolygons .Where(actPolygon => actPolygon.EdgeOrder == EdgeOrder.CounterClockwise) .OrderBy(actPolygon => actPolygon.BoundingBox.Size.X * actPolygon.BoundingBox.Size.Y); List <Polygon2D> holePolygons = geometryExtruder.GeneratedPolygons .Where(actPolygon => actPolygon.EdgeOrder == EdgeOrder.Clockwise) .OrderByDescending(actPolygon => actPolygon.BoundingBox.Size.X * actPolygon.BoundingBox.Size.Y) .ToList(); // Build geometry for all polygons int loopPolygon = 0; foreach (Polygon2D actFillingPolygon in fillingPolygons) { // Find all corresponding holes BoundingBox2D actFillingPolygonBounds = actFillingPolygon.BoundingBox; IEnumerable <Polygon2D> correspondingHoles = holePolygons .Where(actHolePolygon => actHolePolygon.BoundingBox.IsContainedBy(actFillingPolygonBounds)) .ToList(); // Two steps here: // - Merge current filling polygon and all its holes. // - Remove found holes from current hole list Polygon2D polygonForRendering = actFillingPolygon; Polygon2D polygonForTriangulation = actFillingPolygon.Clone(); List <Vector2> cutPoints = new List <Vector2>(); foreach (Polygon2D actHole in correspondingHoles) { holePolygons.Remove(actHole); polygonForRendering = polygonForRendering.MergeWithHole(actHole, Polygon2DMergeOptions.Default, cutPoints); polygonForTriangulation = polygonForTriangulation.MergeWithHole(actHole, new Polygon2DMergeOptions() { MakeMergepointSpaceForTriangulation = true }); } loopPolygon++; int actBaseIndex = (int)tempStructure.CountVertices; EdgeOrder edgeOrder = polygonForRendering.EdgeOrder; float edgeSize = edgeOrder == EdgeOrder.CounterClockwise ? 0.1f : 0.4f; // Append all vertices to temporary VertexStructure for (int loop = 0; loop < polygonForRendering.Vertices.Count; loop++) { // Calculate 3d location and texture coordinate Vector3 actVertexLocation = new Vector3( polygonForRendering.Vertices[loop].X, 0f, polygonForRendering.Vertices[loop].Y); Vector2 actTexCoord = new Vector2( (polygonForRendering.Vertices[loop].X - polygonForRendering.BoundingBox.Location.X) / polygonForRendering.BoundingBox.Size.X, (polygonForRendering.Vertices[loop].Y - polygonForRendering.BoundingBox.Location.Y) / polygonForRendering.BoundingBox.Size.Y); if (float.IsInfinity(actTexCoord.X) || float.IsNaN(actTexCoord.X)) { actTexCoord.X = 0f; } if (float.IsInfinity(actTexCoord.Y) || float.IsNaN(actTexCoord.Y)) { actTexCoord.Y = 0f; } // Append the vertex to the result tempStructure.AddVertex( new Vertex( actVertexLocation, m_geometryOptions.SurfaceVertexColor, actTexCoord, new Vector3(0f, 1f, 0f))); } // Generate cubes on each vertex if requested if (m_geometryOptions.GenerateCubesOnVertices) { for (int loop = 0; loop < polygonForRendering.Vertices.Count; loop++) { Color4 colorToUse = Color4.GreenColor; float pointRenderSize = 0.1f; if (cutPoints.Contains(polygonForRendering.Vertices[loop])) { colorToUse = Color4.RedColor; pointRenderSize = 0.15f; } Vector3 actVertexLocation = new Vector3( polygonForRendering.Vertices[loop].X, 0f, polygonForRendering.Vertices[loop].Y); tempSurface.BuildCube24V(actVertexLocation, pointRenderSize, colorToUse); } } // Triangulate the polygon IEnumerable <int> triangleIndices = polygonForTriangulation.TriangulateUsingCuttingEars(); if (triangleIndices == null) { continue; } if (triangleIndices == null) { throw new SeeingSharpGraphicsException("Unable to triangulate given PathGeometry object!"); } // Append all triangles to the temporary structure using (IEnumerator <int> indexEnumerator = triangleIndices.GetEnumerator()) { while (indexEnumerator.MoveNext()) { int index1 = indexEnumerator.Current; int index2 = 0; int index3 = 0; if (indexEnumerator.MoveNext()) { index2 = indexEnumerator.Current; } else { break; } if (indexEnumerator.MoveNext()) { index3 = indexEnumerator.Current; } else { break; } tempSurface.AddTriangle( (int)(actBaseIndex + index3), (int)(actBaseIndex + index2), (int)(actBaseIndex + index1)); } } } } // Make volumetric outlines int triangleCountWithoutSide = tempSurface.CountTriangles; if (m_geometryOptions.MakeVolumetricText) { float volumetricTextDepth = m_geometryOptions.VolumetricTextDepth; if (m_geometryOptions.VerticesScaleFactor > 0f) { volumetricTextDepth = volumetricTextDepth / m_geometryOptions.VerticesScaleFactor; } // Add all side surfaces foreach (Polygon2D actPolygon in geometryExtruder.GeneratedPolygons) { foreach (Line2D actLine in actPolygon.Lines) { tempSurface.BuildRect4V( new Vector3(actLine.StartPosition.X, -volumetricTextDepth, actLine.StartPosition.Y), new Vector3(actLine.EndPosition.X, -volumetricTextDepth, actLine.EndPosition.Y), new Vector3(actLine.EndPosition.X, 0f, actLine.EndPosition.Y), new Vector3(actLine.StartPosition.X, 0f, actLine.StartPosition.Y), m_geometryOptions.VolumetricSideSurfaceVertexColor); } } } // Do also make back surface? if (m_geometryOptions.MakeBackSurface) { for (int loop = 0; loop < triangleCountWithoutSide; loop++) { Triangle triangle = tempSurface.Triangles[loop]; Vertex vertex0 = tempStructure.Vertices[triangle.Index1]; Vertex vertex1 = tempStructure.Vertices[triangle.Index2]; Vertex vertex2 = tempStructure.Vertices[triangle.Index3]; Vector3 changeVector = new Vector3(0f, -m_geometryOptions.VolumetricTextDepth, 0f); tempSurface.AddTriangle( vertex2.Copy(vertex2.Position - changeVector, Vector3.Negate(vertex2.Normal)), vertex1.Copy(vertex1.Position - changeVector, Vector3.Negate(vertex1.Normal)), vertex0.Copy(vertex0.Position - changeVector, Vector3.Negate(vertex0.Normal))); } } // TODO: Make this configurable tempStructure.ToggleCoordinateSystem(); // Scale the text using given scale factor if (m_geometryOptions.VerticesScaleFactor > 0f) { Matrix4x4 scaleMatrix = Matrix4x4.CreateScale( m_geometryOptions.VerticesScaleFactor, m_geometryOptions.VerticesScaleFactor, m_geometryOptions.VerticesScaleFactor); Matrix4Stack transformMatrix = new Matrix4Stack(scaleMatrix); transformMatrix.TransformLocal(m_geometryOptions.VertexTransform); tempStructure.UpdateVerticesUsingRelocationFunc((actVector) => Vector3.Transform(actVector, transformMatrix.Top)); } // Calculate all normals before adding to target structure if (m_geometryOptions.CalculateNormals) { tempStructure.CalculateNormalsFlat(); } // Merge temporary structure to target structure m_targetSurface.AddStructure(tempStructure); return(SDX.Result.Ok); }
/// <summary> /// Builds the structures. /// </summary> public override VertexStructure BuildStructure(StructureBuildOptions buildOptions) { VertexStructure result = new VertexStructure(); // Calculate parameters Vector3 firstCoordinate = new Vector3( -((TilesX * TileWidth) / 2f), 0f, -((TilesZ * TileWidth) / 2f)); float tileWidthX = this.TileWidth; float tileWidthZ = this.TileWidth; float fieldWidth = tileWidthX * TilesX; float fieldDepth = tileWidthZ * TilesZ; float fieldWidthHalf = fieldWidth / 2f; float fieldDepthHalf = fieldDepth / 2f; int tileMiddleX = (TilesX % 2 == 0) && (this.HighlightXZLines) ? this.TilesX / 2 : 1; int tileMiddleZ = (TilesZ % 2 == 0) && (this.HighlightXZLines) ? this.TilesZ / 2 : 1; // Define lower ground structure if (this.GenerateGround) { VertexStructureSurface lowerGround = result.CreateSurface(); lowerGround.EnableTextureTileMode(new Vector2(TileWidth, TileWidth)); lowerGround.BuildRect4V( new Vector3(-fieldWidthHalf, -0.01f, -fieldDepthHalf), new Vector3(fieldWidthHalf, -0.01f, -fieldDepthHalf), new Vector3(fieldWidthHalf, -0.01f, fieldDepthHalf), new Vector3(-fieldWidthHalf, -0.01f, fieldDepthHalf), new Vector3(0f, 1f, 0f), this.GroundColor); lowerGround.Material = this.GroundMaterial; } // Define line structures VertexStructureSurface genStructureDefaultLine = result.CreateSurface(); VertexStructureSurface genStructureGroupLine = result.CreateSurface(); for (int actTileX = 0; actTileX < TilesX + 1; actTileX++) { Vector3 localStart = firstCoordinate + new Vector3(actTileX * tileWidthX, 0f, 0f); Vector3 localEnd = localStart + new Vector3(0f, 0f, tileWidthZ * TilesZ); Color4 actLineColor = this.LineColor; float devider = actTileX % this.GroupTileCount == 0 ? this.LineSmallDevider : this.LineBigDevider; if (this.HighlightXZLines && (actTileX == tileMiddleX)) { actLineColor = this.ZLineHighlightColor; devider = this.LineSmallDevider; } VertexStructureSurface targetStruture = actTileX % this.GroupTileCount == 0 ? genStructureGroupLine : genStructureDefaultLine; targetStruture.BuildRect4V( localStart - new Vector3(tileWidthX / devider, 0f, 0f), localStart + new Vector3(tileWidthX / devider, 0f, 0f), localEnd + new Vector3(tileWidthX / devider, 0f, 0f), localEnd - new Vector3(tileWidthX / devider, 0f, 0f), actLineColor); if (this.BuildBackFaces) { targetStruture.BuildRect4V( localEnd - new Vector3(tileWidthX / devider, 0f, 0f), localEnd + new Vector3(tileWidthX / devider, 0f, 0f), localStart + new Vector3(tileWidthX / devider, 0f, 0f), localStart - new Vector3(tileWidthX / devider, 0f, 0f), actLineColor); } } for (int actTileZ = 0; actTileZ < TilesZ + 1; actTileZ++) { Vector3 localStart = firstCoordinate + new Vector3(0f, 0f, actTileZ * tileWidthZ); Vector3 localEnd = localStart + new Vector3(tileWidthX * TilesX, 0f, 0f); Color4 actLineColor = this.LineColor; float devider = actTileZ % this.GroupTileCount == 0 ? this.LineSmallDevider : this.LineBigDevider; if (this.HighlightXZLines && (actTileZ == tileMiddleZ)) { actLineColor = this.XLineHighlightColor; devider = this.LineSmallDevider; } VertexStructureSurface targetStruture = actTileZ % this.GroupTileCount == 0 ? genStructureGroupLine : genStructureDefaultLine; targetStruture.BuildRect4V( localStart + new Vector3(0f, 0f, tileWidthZ / devider), localStart - new Vector3(0f, 0f, tileWidthZ / devider), localEnd - new Vector3(0f, 0f, tileWidthZ / devider), localEnd + new Vector3(0f, 0f, tileWidthZ / devider), actLineColor); if (this.BuildBackFaces) { targetStruture.BuildRect4V( localEnd + new Vector3(0f, 0f, tileWidthZ / devider), localEnd - new Vector3(0f, 0f, tileWidthZ / devider), localStart - new Vector3(0f, 0f, tileWidthZ / devider), localStart + new Vector3(0f, 0f, tileWidthZ / devider), actLineColor); } } genStructureDefaultLine.Material = this.LineMaterial; genStructureGroupLine.Material = this.LineMaterial; if (genStructureDefaultLine.CountTriangles == 0) { result.RemoveSurface(genStructureDefaultLine); } if (genStructureGroupLine.CountTriangles == 0) { result.RemoveSurface(genStructureGroupLine); } // Return all generated structures return(result); }
/// <summary> /// Builds the structure. /// </summary> public override VertexStructure BuildStructure(StructureBuildOptions buildOptions) { VertexStructure result = new VertexStructure(); // Hold dictionary containg materials and corresponding structures Dictionary <NamedOrGenericKey, VertexStructureSurface> materialRelated = new Dictionary <NamedOrGenericKey, VertexStructureSurface>(); // Build bottom structure VertexStructureSurface bottomSurface = result.CreateSurface(); bottomSurface.Material = m_bottomMaterial; materialRelated[m_bottomMaterial] = bottomSurface; // Calculate half vector of total ground size. Vector2 totalHalfSize = new Vector2(m_totalSizeWithoutBorder.X / 2f, m_totalSizeWithoutBorder.Y / 2f); Vector2 tileHalfSize = new Vector2(m_tileSize.X / 2f, m_tileSize.Y / 2f); // Build all tiles foreach (FloorTile actTile in m_groundTiles) { // Get the material of the tile NamedOrGenericKey actMaterial = actTile.Material; if (actMaterial.IsEmpty) { actMaterial = m_groundMaterial; } // Get surface object VertexStructureSurface actSurface = null; if (materialRelated.ContainsKey(actMaterial)) { actSurface = materialRelated[actMaterial]; } else { actSurface = result.CreateSurface(); actSurface.Material = actMaterial; materialRelated[actMaterial] = actSurface; } // Get position of the tile Vector3 tilePosition = new Vector3( (actTile.XPos * m_tileSize.X) - totalHalfSize.X, 0f, (actTile.YPos * m_tileSize.Y) - totalHalfSize.Y); // Add tile information to current VertexStructures actSurface.BuildCubeTop4V( new Vector3(tilePosition.X, -m_height, tilePosition.Z), new Vector3(m_tileSize.X, m_height, m_tileSize.Y), Color4.White); bottomSurface.BuildCubeBottom4V( new Vector3(tilePosition.X, -m_height, tilePosition.Z), new Vector3(m_tileSize.X, m_height, m_tileSize.Y), Color4.White); } // Build all borders VertexStructureSurface borderSurface = null; if (materialRelated.ContainsKey(m_borderMaterial)) { borderSurface = materialRelated[m_borderMaterial]; } else { borderSurface = result.CreateSurface(); borderSurface.Material = m_borderMaterial; materialRelated[m_borderMaterial] = borderSurface; } foreach (BorderInformation actBorder in m_borders) { if (m_borderSize <= 0f) { Vector3 tilePosition = new Vector3( (actBorder.TileXPos * m_tileSize.X) - totalHalfSize.X, 0f, (actBorder.TileYPos * m_tileSize.Y) - totalHalfSize.Y); //Build simple borders switch (actBorder.Location) { case BorderLocation.Left: borderSurface.BuildRect4V( new Vector3(tilePosition.X, -m_height, tilePosition.Z), new Vector3(tilePosition.X, 0f, tilePosition.Z), new Vector3(tilePosition.X, 0f, tilePosition.Z + m_tileSize.Y), new Vector3(tilePosition.X, -m_height, tilePosition.Z + m_tileSize.Y)); break; case BorderLocation.Top: borderSurface.BuildRect4V( new Vector3(tilePosition.X, -m_height, tilePosition.Z + m_tileSize.Y), new Vector3(tilePosition.X, 0f, tilePosition.Z + m_tileSize.Y), new Vector3(tilePosition.X + m_tileSize.X, 0f, tilePosition.Z + m_tileSize.Y), new Vector3(tilePosition.X + m_tileSize.X, -m_height, tilePosition.Z + m_tileSize.Y)); break; case BorderLocation.Right: borderSurface.BuildRect4V( new Vector3(tilePosition.X + m_tileSize.X, -m_height, tilePosition.Z + m_tileSize.Y), new Vector3(tilePosition.X + m_tileSize.X, 0f, tilePosition.Z + m_tileSize.Y), new Vector3(tilePosition.X + m_tileSize.X, 0f, tilePosition.Z), new Vector3(tilePosition.X + m_tileSize.X, -m_height, tilePosition.Z)); break; case BorderLocation.Bottom: borderSurface.BuildRect4V( new Vector3(tilePosition.X + m_tileSize.X, -m_height, tilePosition.Z), new Vector3(tilePosition.X + m_tileSize.X, 0f, tilePosition.Z), new Vector3(tilePosition.X, 0f, tilePosition.Z), new Vector3(tilePosition.X, -m_height, tilePosition.Z)); break; } } else { //Build complex borders } } //Return all generated VertexStructures return(result); }