public void UpdateLine2Mesh() { if (adaptivePoints.Count == 0) { UpdateTubeMesh(); } var vertexFormat = Line2Mesh.VertexBufferRange.VertexFormat; var position = vertexFormat.FindAttribute(VertexUsage.Position, 0); var color = vertexFormat.FindAttribute(VertexUsage.Color, 0); line2VertexWriter.BeginEdit(); line2IndexWriter.BeginEdit(); foreach (float t in adaptivePoints) { Vector3 pos = curve.PositionAt(t); float r, g, b; RenderStack.Math.Conversions.HSVtoRGB(360.0f * t, 1.0f, 1.0f, out r, out g, out b); line2IndexWriter.Line(line2VertexWriter.CurrentIndex, line2VertexWriter.CurrentIndex + 1); line2IndexWriter.CurrentIndex += 2; line2VertexWriter.Set(color, r, g, b, 1.0f); line2VertexWriter.Set(position, pos.X, pos.Y, pos.Z); ++line2VertexWriter.CurrentIndex; line2VertexWriter.Set(color, r, g, b, 1.0f); line2VertexWriter.Set(position, pos.X, 0.0f, pos.Z); ++line2VertexWriter.CurrentIndex; } line2VertexWriter.EndEdit(); line2IndexWriter.EndEdit(); }
public void UpdatePointMesh() { if (adaptivePoints.Count == 0) { UpdateTubeMesh(); } var vertexFormat = PointMesh.VertexBufferRange.VertexFormat; var position = vertexFormat.FindAttribute(VertexUsage.Position, 0); var color = vertexFormat.FindAttribute(VertexUsage.Color, 0); pointVertexWriter.BeginEdit(); pointIndexWriter.BeginEdit(); foreach (float t in adaptivePoints) { Vector3 pos = curve.PositionAt(t); pointVertexWriter.Set(position, pos.X, pos.Y, pos.Z); pointVertexWriter.Set(color, 1.0f, 1.0f, 1.0f, 1.0f); pointIndexWriter.Point(pointVertexWriter.CurrentIndex); ++pointVertexWriter.CurrentIndex; ++pointIndexWriter.CurrentIndex; } pointVertexWriter.EndEdit(); pointIndexWriter.EndEdit(); }
public void UpdateRender() { vertexWriter.BeginEdit(); indexWriter.BeginEdit(); int visibleCount = 0; for (byte y = 0; y < 128; ++y) { for (long x = 0; x < 16; ++x) { long wx = this.worldX + x; for (long z = 0; z < 16; ++z) { long wz = this.worldZ + z; if (map.IsVisible(wx, y, wz)) { Cube(wx, y, wz, map[wx, y, wz]); ++visibleCount; } } } } System.Diagnostics.Debug.WriteLine("Chunk had " + visibleCount + " visible cubes"); vertexWriter.EndEdit(); indexWriter.EndEdit(); }
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(); }
public void UpdateIndexBuffers() { lineIndexWriter.BeginEdit(); UInt32 index = 0; for (int i = 0; i < curve.Count - 1; ++i) { lineIndexWriter.Line(index, index + 1); lineIndexWriter.CurrentIndex += 2; index += 1; } for (int i = 0; i < curve.Count; ++i) { lineIndexWriter.Line(index + 1, index + 2); lineIndexWriter.CurrentIndex += 2; index += 2; } lineIndexWriter.EndEdit(); }
public void EndPrint() { vertexWriter.EndEdit(); indexWriter.EndEdit(); }
public virtual void UpdateTubeMesh() { var vertexFormat = TubeMesh.VertexBufferRange.VertexFormat; tubePosition = vertexFormat.FindAttribute(VertexUsage.Position, 0); tubeNormal = vertexFormat.FindAttribute(VertexUsage.Normal, 0); tubeTangent = vertexFormat.FindAttribute(VertexUsage.Tangent, 0); tubeColor = vertexFormat.FindAttribute(VertexUsage.Color, 0); tubeT = vertexFormat.FindAttribute(VertexUsage.Color, 1); tubeId = vertexFormat.FindAttribute(VertexUsage.Id, 0); tubeVertexWriter.BeginEdit(); tubeIndexWriter.BeginEdit(); // \todo hack fixme ((GenericCurve)(curve)).UpdateNURBS(); // Compute initial N Vector3 pos = curve.PositionAt(0.0f); Vector3 posNext = curve.PositionAt(1.0f / 512.0f); Vector3 d1 = posNext - pos; Vector3 T = Vector3.Normalize(d1); Vector3 N = d1.MinAxis; Vector3 B = Vector3.Normalize(Vector3.Cross(T, N)); LastN = Vector3.Normalize(Vector3.Cross(B, T)); UpdateTubeMeshWithAdaptiveSubdivision(); for (int stack = 1; stack < TubeStackCount - 2; ++stack) { int nextStack = stack + 1; for (int slice = 0; slice < tubeSliceCount; ++slice) { int nextSlice = (slice + 1) % tubeSliceCount; tubeIndexWriter.Quad( (uint)(stack * tubeSliceCount + nextSlice), (uint)(stack * tubeSliceCount + slice), (uint)(nextStack * tubeSliceCount + slice), (uint)(nextStack * tubeSliceCount + nextSlice) ); tubeIndexWriter.CurrentIndex += 6; } } for (int slice = 0; slice < tubeSliceCount; ++slice) { int nextSlice1 = (slice + 1) % tubeSliceCount; tubeIndexWriter.Triangle( (uint)(0 * tubeSliceCount), (uint)(0 * tubeSliceCount + slice), (uint)(0 * tubeSliceCount + nextSlice1) ); tubeIndexWriter.CurrentIndex += 3; } for (int slice = 0; slice < tubeSliceCount; ++slice) { int nextSlice1 = (slice + 1) % tubeSliceCount; tubeIndexWriter.Triangle( (uint)((TubeStackCount - 1) * tubeSliceCount + nextSlice1), (uint)((TubeStackCount - 1) * tubeSliceCount + slice), (uint)((TubeStackCount - 1) * tubeSliceCount) ); tubeIndexWriter.CurrentIndex += 3; } tubeVertexWriter.EndEdit(); tubeIndexWriter.EndEdit(); }
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(); }