public QuadRenderer(IRenderer renderer) { mesh = new Mesh(); var vertexFormat = new VertexFormat(); position = vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3)); texcoord = vertexFormat.Add(new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2)); color = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4)); // Some gfx cards fail to show last vertex right if texcoord is set to have 3 components vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.StaticDraw); indexBuffer = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.StaticDraw); mesh.VertexBufferRange = vertexBufferRange = vertexBuffer.CreateVertexBufferRange(); indexBufferRange = mesh.FindOrCreateIndexBufferRange( MeshMode.PolygonFill, indexBuffer, BeginMode.Triangles ); vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange); indexWriter = new IndexBufferWriter(indexBufferRange); }
public TextBuffer(FontStyle fontStyle) { this.fontStyle = fontStyle; mesh = new Mesh.Mesh(); var vertexFormat = new VertexFormat(); vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3)); vertexFormat.Add(new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2)); vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 3)); vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.DynamicDraw); indexBuffer = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.StaticDraw); mesh.VertexBufferRange = vertexBuffer.CreateVertexBufferRange(); var indexBufferRange = mesh.FindOrCreateIndexBufferRange( MeshMode.PolygonFill, indexBuffer, BeginMode.Triangles ); vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange); indexWriter = new IndexBufferWriter(indexBufferRange); }
public NinePatch(NinePatchStyle style) { this.style = style; //mesh = new Mesh.Mesh(BufferUsageHint.DynamicDraw); mesh = new RenderStack.Mesh.Mesh(); VertexFormat vertexFormat = new VertexFormat(); vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3)); vertexFormat.Add(new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2)); // \todo Allocate vertex buffers form from UI BufferPool and use double buffered Buffers // \todo Share one index buffer among all UI components that have the same index buffer //Buffer vertexBuffer = BufferPool.Instance.GetVertexBuffer(vertexFormat, BufferUsageHint.DynamicDraw); //Buffer indexBuffer = BufferPool.Instance.GetIndexBuffer(DrawElementsType.UnsignedShort, BufferUsageHint.StaticDraw); vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.DynamicDraw); indexBuffer = BufferFactory.Create(DrawElementsType.UnsignedShort, BufferUsageHint.StaticDraw); mesh.VertexBufferRange = vertexBuffer.CreateVertexBufferRange(); IBufferRange indexBufferRange = mesh.FindOrCreateIndexBufferRange( MeshMode.PolygonFill, indexBuffer, BeginMode.Triangles ); var writer = new IndexBufferWriter(indexBufferRange); vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange); // 12 13 14 15 // // 8 9 10 11 // // 4 5 6 7 // // 0 1 2 3 writer.BeginEdit(); writer.Quad(4, 5, 1, 0); writer.CurrentIndex += 6; writer.Quad(5, 6, 2, 1); writer.CurrentIndex += 6; writer.Quad(6, 7, 3, 2); writer.CurrentIndex += 6; writer.Quad(8, 9, 5, 4); writer.CurrentIndex += 6; writer.Quad(9, 10, 6, 5); writer.CurrentIndex += 6; writer.Quad(10, 11, 7, 6); writer.CurrentIndex += 6; writer.Quad(12, 13, 9, 8); writer.CurrentIndex += 6; writer.Quad(13, 14, 10, 9); writer.CurrentIndex += 6; writer.Quad(14, 15, 11, 10); writer.CurrentIndex += 6; writer.EndEdit(); // \bug //indexBuffer.UpdateAll(); }
private void UseMap(Map map) { this.map = map; position = map.Position; texcoord = map.Texcoord; color = map.Color; mesh = new Mesh(); mesh.VertexBufferRange = vertexBufferRange = map.VertexBuffer.CreateVertexBufferRange(); indexBufferRange = mesh.FindOrCreateIndexBufferRange( MeshMode.PolygonFill, map.IndexBuffer, BeginMode.Triangles ); vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange); indexWriter = new IndexBufferWriter(indexBufferRange); }
protected override void InitializeService() { if ( (RenderStack.Graphics.Configuration.canUseGeometryShaders) && (RenderStack.Graphics.Configuration.glslVersion >= 330) ) { material = new Material("", renderer.Programs["WideLine"], renderer.MaterialUB); } else { material = new Material("", renderer.Programs["ColorFill"], renderer.MaterialUB); } LineWidth = 1.0f; mesh = new Mesh(); var vertexFormat = new VertexFormat(); position = vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3)); edgeColor = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 1, 4)); vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.DynamicDraw); indexBuffer = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.DynamicDraw); mesh.VertexBufferRange = vertexBufferRange = vertexBuffer.CreateVertexBufferRange(); indexBufferRange = mesh.FindOrCreateIndexBufferRange( MeshMode.EdgeLines, indexBuffer, BeginMode.Lines ); vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange); indexWriter = new IndexBufferWriter(indexBufferRange); }
public void LowPrint( VertexBufferWriter vertexWriter, IndexBufferWriter indexWriter, float x, float y, float z, string text ) { if (string.IsNullOrEmpty(text) == true) { return; } y += common.Base; FontChar fontChar = null; for (int i = 0; i < text.Length; ++i) { char c = text[i]; fontChar = chars[c]; if (fontChar == null) { continue; } float a = fontChar.XAdvance; float w = fontChar.Width; float h = fontChar.Height; float ox = fontChar.XOffset; float oy = fontChar.YOffset; indexWriter.Quad( vertexWriter.CurrentIndex, vertexWriter.CurrentIndex + 1, vertexWriter.CurrentIndex + 2, vertexWriter.CurrentIndex + 3 ); indexWriter.CurrentIndex += 6; vertexWriter.Set(position, x + ox, y - oy, z); vertexWriter.Set(texCoord, fontChar.U, fontChar.V); vertexWriter.Set(color, 1.0f, 1.0f, 1.0f); ++vertexWriter.CurrentIndex; Bounds.Extend(x + ox, y - oy); vertexWriter.Set(position, x + w + ox, y - oy, z); vertexWriter.Set(texCoord, fontChar.U2, fontChar.V); vertexWriter.Set(color, 1.0f, 1.0f, 1.0f); ++vertexWriter.CurrentIndex; Bounds.Extend(x + w + ox, y - oy); vertexWriter.Set(position, x + w + ox, y - h - oy, z); vertexWriter.Set(texCoord, fontChar.U2, fontChar.V2); vertexWriter.Set(color, 1.0f, 1.0f, 1.0f); ++vertexWriter.CurrentIndex; Bounds.Extend(x + w + ox, y - h - oy); vertexWriter.Set(position, x + ox, y - h - oy, z); vertexWriter.Set(texCoord, fontChar.U, fontChar.V2); vertexWriter.Set(color, 1.0f, 1.0f, 1.0f); ++vertexWriter.CurrentIndex; Bounds.Extend(x + ox, y - h - oy); x += a; if ( (i + 1 < text.Length - 1) && (fontChar.Kernings != null) ) { char next = text[i + 1]; FontKerning compare = new FontKerning((short)next); int index = fontChar.Kernings.BinarySearch(compare); if (index >= 0) { short amount = fontChar.Kernings[index].Amount; x += (float)(amount); } } } }
public CurveTube(ICurve curve) { this.curve = curve; PointMesh = new Mesh(); LineMesh = new Mesh(); Line2Mesh = new Mesh(); TubeMesh = new Mesh(); var positionColor = new VertexFormat(); // pointMesh, lineMesh, line2Mesh var tubeVertexFormat = new VertexFormat(); positionColor.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3)); positionColor.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4)); tubeVertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3)); tubeVertexFormat.Add(new Attribute(VertexUsage.Tangent, VertexAttribPointerType.Float, 0, 3)); tubeVertexFormat.Add(new Attribute(VertexUsage.Normal, VertexAttribPointerType.Float, 0, 3)); tubeVertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4)); tubeVertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 1, 1)); tubeVertexFormat.Add(new Attribute(VertexUsage.Id, VertexAttribPointerType.UnsignedInt, 0, 1)); // Could perhaps use BufferPool var positionColorVertexBuffer = BufferFactory.Create(positionColor, BufferUsageHint.DynamicDraw); var tubeVertexBuffer = BufferFactory.Create(tubeVertexFormat, BufferUsageHint.DynamicDraw); var indexBuffer = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.DynamicDraw); PointMesh.VertexBufferRange = positionColorVertexBuffer.CreateVertexBufferRange(); LineMesh.VertexBufferRange = positionColorVertexBuffer.CreateVertexBufferRange(); Line2Mesh.VertexBufferRange = positionColorVertexBuffer.CreateVertexBufferRange(); TubeMesh.VertexBufferRange = tubeVertexBuffer.CreateVertexBufferRange(); var pointIndexBuffer = PointMesh.FindOrCreateIndexBufferRange( MeshMode.CornerPoints, indexBuffer, BeginMode.Points ); var lineIndexBuffer = LineMesh.FindOrCreateIndexBufferRange( MeshMode.EdgeLines, indexBuffer, BeginMode.Lines ); var line2IndexBuffer = Line2Mesh.FindOrCreateIndexBufferRange( MeshMode.EdgeLines, indexBuffer, BeginMode.Lines ); var tubeIndexBuffer = TubeMesh.FindOrCreateIndexBufferRange( MeshMode.PolygonFill, indexBuffer, BeginMode.Triangles ); tubeVertexWriter = new VertexBufferWriter(TubeMesh.VertexBufferRange); tubeIndexWriter = new IndexBufferWriter(tubeIndexBuffer); pointVertexWriter = new VertexBufferWriter(PointMesh.VertexBufferRange); pointIndexWriter = new IndexBufferWriter(pointIndexBuffer); lineVertexWriter = new VertexBufferWriter(LineMesh.VertexBufferRange); lineIndexWriter = new IndexBufferWriter(lineIndexBuffer); line2VertexWriter = new VertexBufferWriter(Line2Mesh.VertexBufferRange); line2IndexWriter = new IndexBufferWriter(line2IndexBuffer); UpdateIndexBuffers(); }
public void BuildMeshFromGeometry( BufferUsageHint bufferUsageHint, NormalStyle normalStyle, VertexFormat vertexFormat ) { if (Geometry.PolygonAttributes.Contains <Vector3>("polygon_normals") == false) { Geometry.ComputePolygonNormals(); } Geometry.ComputePointNormals("point_normals_smooth"); if (Geometry.PointAttributes.Contains <Vector3>("polygon_centroids") == false) { Geometry.ComputePolygonCentroids(); } var polygonIdsVector3 = Geometry.PolygonAttributes.FindOrCreate <Vector3>("polygon_ids_vec3"); var polygonIdsUInt32 = (RenderStack.Graphics.Configuration.useIntegerPolygonIDs) ? Geometry.PolygonAttributes.FindOrCreate <UInt32>("polygon_ids_uint") : null; Dictionary <Corner, Vector3> cornerNormals = null; Dictionary <Point, Vector3> pointNormals = null; Dictionary <Point, Vector3> pointNormalsSmooth = Geometry.PointAttributes.Find <Vector3>("point_normals_smooth"); bool normalsFound = false; if (Geometry.CornerAttributes.Contains <Vector3>("corner_normals")) { cornerNormals = Geometry.CornerAttributes.Find <Vector3>("corner_normals"); normalsFound = true; } if (Geometry.PointAttributes.Contains <Vector3>("point_normals")) { pointNormals = Geometry.PointAttributes.Find <Vector3>("point_normals"); normalsFound = true; } if (normalsFound == false) { //Geometry.ComputeCornerNormals(0.0f * (float)System.Math.PI); Geometry.SmoothNormalize("corner_normals", "polygon_normals", (0.0f * (float)System.Math.PI)); cornerNormals = Geometry.CornerAttributes.Find <Vector3>("corner_normals"); } Dictionary <Corner, Vector2> cornerTexcoords = null; Dictionary <Point, Vector2> pointTexcoords = null; if (Geometry.CornerAttributes.Contains <Vector2>("corner_texcoords")) { cornerTexcoords = Geometry.CornerAttributes.Find <Vector2>("corner_texcoords"); } if (Geometry.PointAttributes.Contains <Vector2>("point_texcoords")) { pointTexcoords = Geometry.PointAttributes.Find <Vector2>("point_texcoords"); } //Dictionary<Corner, Vector4> cornerColors = null; var cornerColors = default(Dictionary <Corner, Vector4>); Dictionary <Point, Vector4> pointColors = null; if (Geometry.CornerAttributes.Contains <Vector4>("corner_colors")) { cornerColors = Geometry.CornerAttributes.Find <Vector4>("corner_colors"); } if (Geometry.PointAttributes.Contains <Vector4>("point_colors")) { pointColors = Geometry.PointAttributes.Find <Vector4>("point_colors"); } var polygonNormals = Geometry.PolygonAttributes.Find <Vector3>("polygon_normals"); var polygonCentroids = Geometry.PolygonAttributes.Find <Vector3>("polygon_centroids"); var pointLocations = Geometry.PointAttributes.Find <Vector3>("point_locations"); var cornerIndices = Geometry.CornerAttributes.FindOrCreate <uint>("corner_indices"); var attributePosition = vertexFormat.FindAttribute(VertexUsage.Position, 0); var attributeNormal = vertexFormat.FindAttribute(VertexUsage.Normal, 0); /* content normals */ var attributeNormalFlat = vertexFormat.FindAttribute(VertexUsage.Normal, 1); /* flat normals */ var attributeNormalSmooth = vertexFormat.FindAttribute(VertexUsage.Normal, 2); /* smooth normals */ var attributeColor = vertexFormat.FindAttribute(VertexUsage.Color, 0); var attributeTexcoord = vertexFormat.FindAttribute(VertexUsage.TexCoord, 0); var attributeIdVec3 = vertexFormat.FindAttribute(VertexUsage.Id, 0); var attributeIdUInt = vertexFormat.FindAttribute(VertexUsage.Id, 0); // \note work in progress GetMesh = new Mesh(); IBuffer vertexBuffer = BufferPool.Instance.GetVertexBuffer(vertexFormat, bufferUsageHint); GetMesh.VertexBufferRange = vertexBuffer.CreateVertexBufferRange(); IBuffer indexBuffer = BufferPool.Instance.GetIndexBuffer(DrawElementsType.UnsignedInt, bufferUsageHint); #region prepare index buffers var polygonFillIndices = GetMesh.FindOrCreateIndexBufferRange( MeshMode.PolygonFill, indexBuffer, BeginMode.Triangles ); var edgeLineIndices = GetMesh.FindOrCreateIndexBufferRange( MeshMode.EdgeLines, indexBuffer, BeginMode.Lines ); /*BufferRange silhouetteLineIndices = GetMesh.FindOrCreateIndexBuffer( * MeshMode.EdgeLines, * indexBuffer, * BeginMode.Lines * );*/ var cornerPointIndices = GetMesh.FindOrCreateIndexBufferRange( MeshMode.CornerPoints, indexBuffer, BeginMode.Points ); var polygonCentroidIndices = GetMesh.FindOrCreateIndexBufferRange( MeshMode.PolygonCentroids, indexBuffer, BeginMode.Points ); #endregion prepare index buffers var vertexWriter = new VertexBufferWriter(GetMesh.VertexBufferRange); var polygonFillIndexWriter = new IndexBufferWriter(polygonFillIndices); var edgeLineIndexWriter = new IndexBufferWriter(edgeLineIndices); var cornerPointIndexWriter = new IndexBufferWriter(cornerPointIndices); var polygonCentroidIndexWriter = new IndexBufferWriter(polygonCentroidIndices); vertexWriter.BeginEdit(); polygonFillIndexWriter.BeginEdit(); edgeLineIndexWriter.BeginEdit(); cornerPointIndexWriter.BeginEdit(); polygonCentroidIndexWriter.BeginEdit(); UInt32 polygonIndex = 0; #region polygons cornerIndices.Clear(); foreach (Polygon polygon in Geometry.Polygons) { if (RenderStack.Graphics.Configuration.useIntegerPolygonIDs) { polygonIdsUInt32[polygon] = polygonIndex; } polygonIdsVector3[polygon] = Vector3.Vector3FromUint(polygonIndex); Vector3 polygonNormal = Vector3.UnitY; if (polygon.Corners.Count > 2 && polygonNormals != null && polygonNormals.ContainsKey(polygon)) { polygonNormal = polygonNormals[polygon]; } uint firstIndex = vertexWriter.CurrentIndex; uint previousIndex = firstIndex; #region corners foreach (Corner corner in polygon.Corners) { // Position vertexWriter.Position(pointLocations[corner.Point]); // Normal Vector3 normal = Vector3.UnitY; if ( (cornerNormals != null) && (polygon.Corners.Count > 2) && (cornerNormals.ContainsKey(corner) == true) ) { normal = cornerNormals[corner]; } else if (pointNormals != null && pointNormals.ContainsKey(corner.Point)) { normal = pointNormals[corner.Point]; } else if (pointNormalsSmooth != null && pointNormalsSmooth.ContainsKey(corner.Point)) { normal = pointNormalsSmooth[corner.Point]; } Vector3 pointNormal = Vector3.UnitY; if (pointNormals != null && pointNormals.ContainsKey(corner.Point)) { pointNormal = pointNormals[corner.Point]; } else if (pointNormalsSmooth != null && pointNormalsSmooth.ContainsKey(corner.Point)) { pointNormal = pointNormalsSmooth[corner.Point]; } switch (normalStyle) { case NormalStyle.CornerNormals: vertexWriter.Normal(normal); break; case NormalStyle.PointNormals: vertexWriter.Normal(pointNormal); break; case NormalStyle.PolygonNormals: vertexWriter.Normal(polygonNormal); break; } if (attributeNormalFlat != null) { vertexWriter.Set(attributeNormalFlat, polygonNormal); } if (attributeNormalSmooth != null) { vertexWriter.Set(attributeNormalSmooth, pointNormalsSmooth[corner.Point]); } // Texcoord if (attributeTexcoord != null) { if ( (cornerTexcoords != null) && (cornerTexcoords.ContainsKey(corner) == true) ) { vertexWriter.Set(attributeTexcoord, cornerTexcoords[corner]); } else if ( (pointTexcoords != null) && (pointTexcoords.ContainsKey(corner.Point) == true) ) { vertexWriter.Set(attributeTexcoord, pointTexcoords[corner.Point]); } } // Vertex Color if (attributeColor != null) { if ( (cornerColors != null) && (cornerColors.ContainsKey(corner) == true) ) { vertexWriter.Set(attributeColor, cornerColors[corner]); } else if ( (pointColors != null) && (pointColors.ContainsKey(corner.Point) == true) ) { vertexWriter.Set(attributeColor, pointColors[corner.Point]); } else { vertexWriter.Set(attributeColor, Vector4.One); } } // PolygonId if (RenderStack.Graphics.Configuration.useIntegerPolygonIDs && (attributeIdUInt != null)) { vertexWriter.Set(attributeIdUInt, polygonIndex); } if (attributeIdVec3 != null) { Vector3 v = Vector3.Vector3FromUint(polygonIndex); vertexWriter.Set(attributeIdVec3, v); } cornerPointIndexWriter.Point(vertexWriter.CurrentIndex); cornerPointIndexWriter.CurrentIndex++; cornerIndices[corner] = vertexWriter.CurrentIndex; if (previousIndex != firstIndex) { polygonFillIndexWriter.Triangle(firstIndex, vertexWriter.CurrentIndex, previousIndex); polygonFillIndexWriter.CurrentIndex += 3; } previousIndex = vertexWriter.CurrentIndex; ++vertexWriter.CurrentIndex; } #endregion corners ++polygonIndex; } #endregion polygons #region edges Geometry.BuildEdges(); foreach (Edge edge in Geometry.Edges.Keys) { if ( cornerIndices.ContainsKey(edge.A.Corners[0]) && cornerIndices.ContainsKey(edge.B.Corners[0]) ) { uint i0 = cornerIndices[edge.A.Corners[0]]; uint i1 = cornerIndices[edge.B.Corners[0]]; edgeLineIndexWriter.Line(i0, i1); edgeLineIndexWriter.CurrentIndex += 2; } } #endregion edges #region polygon centroids foreach (Polygon polygon in Geometry.Polygons) { Vector3 normal; if (polygon.Corners.Count > 2) { normal = polygonNormals[polygon]; } else { normal = new Vector3(0.0f, 1.0f, 0.0f); } vertexWriter.Position(polygonCentroids[polygon]); if (attributeNormal != null) { vertexWriter.Set(attributeNormal, normal); } if (attributeNormalFlat != null) { vertexWriter.Set(attributeNormalFlat, normal); } polygonCentroidIndexWriter.Point(vertexWriter.CurrentIndex); ++vertexWriter.CurrentIndex; ++polygonCentroidIndexWriter.CurrentIndex; } #endregion polygon centroids vertexWriter.EndEdit(); polygonFillIndexWriter.EndEdit(); edgeLineIndexWriter.EndEdit(); cornerPointIndexWriter.EndEdit(); polygonCentroidIndexWriter.EndEdit(); }