public void Dispose() { if (vertexBuffer != null) { vertexBuffer.Dispose(); GC.SuppressFinalize(vertexBuffer); vertexBuffer = null; } if (glyphCache != null) { glyphCache.CleanUp(); glyphCache = null; } }
public void PrepareBatch() { if (glyphCache == null) { glyphCache = GlyphCache.GetCache(Height); } // Add All Glyphs foreach (Text3d t3d in Items) { foreach (char c in t3d.Text) { glyphCache.AddGlyph(c); } } // Calculate Metrics TextObject.Text = ""; TextObject.FontSize = (float)Height*.50f; System.Drawing.Font font = TextObject.Font; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; Bitmap bmp = new Bitmap(20, 20); Graphics g = Graphics.FromImage(bmp); // Create Index Buffers List<PositionColoredTextured> verts = new List<PositionColoredTextured>(); foreach (Text3d t3d in Items) { float fntAdjust = font.Size / 128f; String text = t3d.Text; SizeF size = g.MeasureString(text, font); float factor = .6666f; t3d.width = size.Width * (float)t3d.scale * factor; t3d.height = size.Height * (float)t3d.scale * factor; float left = 0; int charsLeft = text.Length; int index = 0; // SetMeasurableCharacterRanges has a limit of 32 items per call; while (charsLeft > 0) { int charsNow = Math.Min(32, charsLeft); charsLeft -= charsNow; CharacterRange[] ranges = new CharacterRange[charsNow]; for (int i = 0; i < charsNow; i++) { ranges[i] = new CharacterRange(i + index, 1); } sf.SetMeasurableCharacterRanges(ranges); Region[] reg = g.MeasureCharacterRanges(text, font, new RectangleF(new PointF(0, 0), size), sf); for (int i = 0; i < (charsNow); i++) { GlyphItem item = glyphCache.GetGlyphItem(text[i+index]); RectangleF rectf = reg[i].GetBounds(g); RectangleF position = new RectangleF(rectf.Left * (float)t3d.scale * factor, rectf.Top * (float)t3d.scale * factor, rectf.Width * (float)t3d.scale * factor, rectf.Height * (float)t3d.scale * factor); position = new RectangleF(left * (float)t3d.scale * factor, 0 * (float)t3d.scale * factor, item.Extents.Width * fntAdjust * (float)t3d.scale * factor, item.Extents.Height * fntAdjust * (float)t3d.scale * factor); left += item.Extents.Width * fntAdjust; t3d.AddGlyphPoints(verts, item.Size, position, item.UVRect); } index += charsNow; } } g.Dispose(); GC.SuppressFinalize(g); bmp.Dispose(); font.Dispose(); vertCount = verts.Count; vertexBuffer = new PositionColorTexturedVertexBuffer11(vertCount, RenderContext11.PrepDevice); PositionColoredTextured[] vertBuf = (PositionColoredTextured[])vertexBuffer.Lock(0, 0); // Lock the buffer (which will return our structs) for (int i = 0; i < vertCount; i++) { vertBuf[i] = verts[i]; } vertexBuffer.Unlock(); glyphVersion = glyphCache.Version; }
public void CreateWarpVertexBuffer() { ReadWarpMeshFile(); int warpSubX = meshX - 1; int warpSubY = meshY - 1; CleanUpWarpBuffers(); warpIndexBuffer = new IndexBuffer11(typeof(short), (warpSubX * warpSubY * 6), RenderContext11.PrepDevice); warpVertexBuffer = new PositionColorTexturedVertexBuffer11(((warpSubX + 1) * (warpSubY + 1)), RenderContext11.PrepDevice); warpVertexCount = ((warpSubX + 1) * (warpSubY + 1)); int index = 0; PositionColorTexturedVertexBuffer11 vb = warpVertexBuffer; // Create a vertex buffer PositionColoredTextured[] verts = (PositionColoredTextured[])vb.Lock(0, 0); // Lock the buffer (which will return our structs) int x1, y1; double textureStepX = 1.0f / warpSubX; double textureStepY = 1.0f / warpSubY; for (y1 = 0; y1 <= warpSubY; y1++) { for (x1 = 0; x1 <= warpSubX; x1++) { index = y1 * (warpSubX + 1) + x1; verts[index].Position = mesh[x1, y1].Position; verts[index].Tu = mesh[x1, y1].Tu; verts[index].Tv = mesh[x1, y1].Tv; verts[index].Color = mesh[x1, y1].Color; } } vb.Unlock(); warpTriangleCount = (warpSubX) * (warpSubY) * 2; short[] indexArray = (short[])warpIndexBuffer.Lock(); index = 0; for (y1 = 0; y1 < warpSubY; y1++) { for (x1 = 0; x1 < warpSubX; x1++) { // First triangle in quad indexArray[index] = (short)(y1 * (warpSubX + 1) + x1); indexArray[index + 1] = (short)((y1 + 1) * (warpSubX + 1) + x1); indexArray[index + 2] = (short)(y1 * (warpSubX + 1) + (x1 + 1)); // Second triangle in quad indexArray[index + 3] = (short)(y1 * (warpSubX + 1) + (x1 + 1)); indexArray[index + 4] = (short)((y1 + 1) * (warpSubX + 1) + x1); indexArray[index + 5] = (short)((y1 + 1) * (warpSubX + 1) + (x1 + 1)); index += 6; } } this.warpIndexBuffer.Unlock(); }
void CreateDomeFaceVertexBuffer(int face) { int domeSubX = 50; int domeSubY = 50; CleanupDomeVertexBuffer(face); double fea = Math.Min(250, Properties.Settings.Default.FisheyeAngle) / 180; double fa = Math.Min(250, Properties.Settings.Default.FisheyeAngle); domeIndexBuffer[face] = new IndexBuffer11(typeof(short), (domeSubX * domeSubY * 6), RenderContext11.PrepDevice); domeVertexBuffer[face] = new PositionColorTexturedVertexBuffer11(((domeSubX + 1) * (domeSubY + 1)), RenderContext11.PrepDevice); domeVertexCount = domeSubX * domeSubY * 6; int index = 0; PositionColorTexturedVertexBuffer11 vb = domeVertexBuffer[face]; PositionColoredTextured[] verts = (PositionColoredTextured[])vb.Lock(0, 0); int x1, y1; Vector3d topLeft = new Vector3d(); Vector3d topRight = new Vector3d(); Vector3d bottomLeft = new Vector3d(); Vector3d bottomRight = new Vector3d(); RenderTypes faceType = (RenderTypes)face; switch (faceType) { case RenderTypes.DomeFront: topLeft = new Vector3d(-1, 1, 1); topRight = new Vector3d(1, 1, 1); bottomLeft = new Vector3d(-1, -1, 1); bottomRight = new Vector3d(1, -1, 1); break; case RenderTypes.DomeRight: topLeft = new Vector3d(1, 1, 1); topRight = new Vector3d(1, 1, -1); bottomLeft = new Vector3d(1, -1, 1); bottomRight = new Vector3d(1, -1, -1); break; case RenderTypes.DomeUp: topLeft = new Vector3d(-1, 1, -1); topRight = new Vector3d(1, 1, -1); bottomLeft = new Vector3d(-1, 1, 1); bottomRight = new Vector3d(1, 1, 1); break; case RenderTypes.DomeLeft: topLeft = new Vector3d(-1, 1, -1); topRight = new Vector3d(-1, 1, 1); bottomLeft = new Vector3d(-1, -1, -1); bottomRight = new Vector3d(-1, -1, 1); break; case RenderTypes.DomeBack: topLeft = new Vector3d(1, 1, -1); topRight = new Vector3d(-1, 1, -1); bottomLeft = new Vector3d(1, -1, -1); bottomRight = new Vector3d(-1, -1, -1); break; } double textureStepX = 1.0f / domeSubX; double textureStepY = 1.0f / domeSubY; for (y1 = 0; y1 <= domeSubY; y1++) { double tv; if (y1 != domeSubY) { tv = textureStepY * y1; } else { tv = 1; } for (x1 = 0; x1 <= domeSubX; x1++) { double tu; if (x1 != domeSubX) { tu = textureStepX * x1; } else { tu = 1; } Vector3d top = Vector3d.Lerp(topLeft, topRight, tu); Vector3d bottom = Vector3d.Lerp(bottomLeft, bottomRight, tu); Vector3d net = Vector3d.Lerp(top, bottom, tv); net.Normalize(); Coordinates netNet = Coordinates.CartesianToSpherical2(net.Vector3); double dist = (180 - (netNet.Lat + 90)) / (180 * fea); dist = Math.Min(.5, dist); double x = Math.Sin((netNet.Lng + 90) / 180 * Math.PI) * dist; double y = Math.Cos((netNet.Lng + 90) / 180 * Math.PI) * dist; index = y1 * (domeSubX + 1) + x1; verts[index].Position = new SharpDX.Vector4((float)x, (float)y, .9f, 1); verts[index].Tu = (float)tu; verts[index].Tv = (float)tv; verts[index].Color = Color.White; } } vb.Unlock(); domeTriangleCount = (domeSubX) * (domeSubY) * 2; short[] indexArray = (short[])domeIndexBuffer[face].Lock(); index = 0; for (y1 = 0; y1 < domeSubY; y1++) { for (x1 = 0; x1 < domeSubX; x1++) { //index = (y1 * domeSubX * 6) + 6 * x1; // First triangle in quad indexArray[index] = (short)(y1 * (domeSubX + 1) + x1); indexArray[index + 1] = (short)((y1 + 1) * (domeSubX + 1) + x1); indexArray[index + 2] = (short)(y1 * (domeSubX + 1) + (x1 + 1)); // Second triangle in quad indexArray[index + 3] = (short)(y1 * (domeSubX + 1) + (x1 + 1)); indexArray[index + 4] = (short)((y1 + 1) * (domeSubX + 1) + x1); indexArray[index + 5] = (short)((y1 + 1) * (domeSubX + 1) + (x1 + 1)); index += 6; } } this.domeIndexBuffer[face].Unlock(); }
private void MakeDistortionGrid() { Bitmap bmpBlend = new Bitmap(config.BlendFile); FastBitmap fastBlend = new FastBitmap(bmpBlend); Bitmap bmpDistort = new Bitmap(config.DistortionGrid); FastBitmap fastDistort = new FastBitmap(bmpDistort); fastBlend.LockBitmapRgb(); fastDistort.LockBitmapRgb(); int subX = bmpBlend.Width - 1; int subY = subX; if (distortIndexBuffer != null) { distortIndexBuffer.Dispose(); GC.SuppressFinalize(distortIndexBuffer); } if (distortVertexBuffer != null) { distortVertexBuffer.Dispose(); GC.SuppressFinalize(distortVertexBuffer); } distortIndexBuffer = new IndexBuffer11(typeof(int), (subX * subY * 6), RenderContext11.PrepDevice); distortVertexBuffer = new PositionColorTexturedVertexBuffer11(((subX + 1) * (subY + 1)), RenderContext11.PrepDevice); distortVertexCount = (subX + 1) * (subY + 1); int index = 0; // Create a vertex buffer PositionColoredTextured[] verts = (PositionColoredTextured[])distortVertexBuffer.Lock(0, 0); // Lock the buffer (which will return our structs) int x1, y1; unsafe { double maxU = 0; double maxV = 0; double textureStepX = 1.0f / subX; double textureStepY = 1.0f / subY; for (y1 = 0; y1 <= subY; y1++) { double tv; for (x1 = 0; x1 <= subX; x1++) { double tu; index = y1 * (subX + 1) + x1; PixelDataRgb* pdata = fastDistort.GetRgbPixel(x1, y1); tu = (float)(pdata->blue + ((uint)pdata->red % 16) * 256) / 4095f; tv = (float)(pdata->green + ((uint)pdata->red / 16) * 256) / 4095f; //tu = (tu - .5f) * 1.7777778 + .5f; if (tu > maxU) { maxU = tu; } if (tv > maxV) { maxV = tv; } verts[index].Position = new SharpDX.Vector4(((float)x1 / subX) - .5f, (1f - ((float)y1 / subY)) - .5f, .9f, 1f); verts[index].Tu = (float)tu; verts[index].Tv = (float)tv; PixelDataRgb* pPixel = fastBlend.GetRgbPixel(x1, y1); verts[index].Color = Color.FromArgb(255, pPixel->red, pPixel->green, pPixel->blue); } } distortVertexBuffer.Unlock(); distortTriangleCount = (subX) * (subY) * 2; uint[] indexArray = (uint[])distortIndexBuffer.Lock(); index = 0; for (y1 = 0; y1 < subY; y1++) { for (x1 = 0; x1 < subX; x1++) { // First triangle in quad indexArray[index] = (uint)(y1 * (subX + 1) + x1); indexArray[index + 1] = (uint)((y1 + 1) * (subX + 1) + x1); indexArray[index + 2] = (uint)(y1 * (subX + 1) + (x1 + 1)); // Second triangle in quad indexArray[index + 3] = (uint)(y1 * (subX + 1) + (x1 + 1)); indexArray[index + 4] = (uint)((y1 + 1) * (subX + 1) + x1); indexArray[index + 5] = (uint)((y1 + 1) * (subX + 1) + (x1 + 1)); index += 6; } } this.distortIndexBuffer.Unlock(); } fastDistort.UnlockBitmap(); fastBlend.UnlockBitmap(); fastDistort.Dispose(); GC.SuppressFinalize(fastDistort); fastBlend.Dispose(); GC.SuppressFinalize(fastBlend); }
public void PrepareBatch() { if (glyphCache == null) { glyphCache = GlyphCache.GetCache(Height); } // Add All Glyphs foreach (Text3d t3d in Items) { foreach (char c in t3d.Text) { glyphCache.AddGlyph(c); } } // Calculate Metrics TextObject.Text = ""; TextObject.FontSize = (float)Height * .50f; System.Drawing.Font font = TextObject.Font; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; Bitmap bmp = new Bitmap(20, 20); Graphics g = Graphics.FromImage(bmp); // Create Index Buffers List <PositionColoredTextured> verts = new List <PositionColoredTextured>(); foreach (Text3d t3d in Items) { float fntAdjust = font.Size / 128f; String text = t3d.Text; SizeF size = g.MeasureString(text, font); float factor = .6666f; t3d.width = size.Width * (float)t3d.scale * factor; t3d.height = size.Height * (float)t3d.scale * factor; float left = 0; int charsLeft = text.Length; int index = 0; // SetMeasurableCharacterRanges has a limit of 32 items per call; while (charsLeft > 0) { int charsNow = Math.Min(32, charsLeft); charsLeft -= charsNow; CharacterRange[] ranges = new CharacterRange[charsNow]; for (int i = 0; i < charsNow; i++) { ranges[i] = new CharacterRange(i + index, 1); } sf.SetMeasurableCharacterRanges(ranges); Region[] reg = g.MeasureCharacterRanges(text, font, new RectangleF(new PointF(0, 0), size), sf); for (int i = 0; i < (charsNow); i++) { GlyphItem item = glyphCache.GetGlyphItem(text[i + index]); RectangleF rectf = reg[i].GetBounds(g); RectangleF position = new RectangleF(rectf.Left * (float)t3d.scale * factor, rectf.Top * (float)t3d.scale * factor, rectf.Width * (float)t3d.scale * factor, rectf.Height * (float)t3d.scale * factor); position = new RectangleF(left * (float)t3d.scale * factor, 0 * (float)t3d.scale * factor, item.Extents.Width * fntAdjust * (float)t3d.scale * factor, item.Extents.Height * fntAdjust * (float)t3d.scale * factor); left += item.Extents.Width * fntAdjust; t3d.AddGlyphPoints(verts, item.Size, position, item.UVRect); } index += charsNow; } } g.Dispose(); GC.SuppressFinalize(g); bmp.Dispose(); font.Dispose(); vertCount = verts.Count; vertexBuffer = new PositionColorTexturedVertexBuffer11(vertCount, RenderContext11.PrepDevice); PositionColoredTextured[] vertBuf = (PositionColoredTextured[])vertexBuffer.Lock(0, 0); // Lock the buffer (which will return our structs) for (int i = 0; i < vertCount; i++) { vertBuf[i] = verts[i]; } vertexBuffer.Unlock(); glyphVersion = glyphCache.Version; }