/// <summary> /// Initialize the device objects /// </summary> /// <param name="dev">The grpahics device used to initialize</param> public void InitializeDeviceObjects(Device dev) { if (dev != null) { // Set up our events dev.DeviceReset += new System.EventHandler(this.RestoreDeviceObjects); } // Keep a local copy of the device device = dev; textureState0 = device.TextureState[0]; textureState1 = device.TextureState[1]; samplerState0 = device.SamplerState[0]; renderState = device.RenderState; // Create a bitmap on which to measure the alphabet Bitmap bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g.TextContrast = 0; // Establish the font and texture size textureScale = 1.0f; // Draw fonts into texture without scaling // Calculate the dimensions for the smallest power-of-two texture which // can hold all the printable characters textureWidth = textureHeight = 128; for (;;) { try { // Measure the alphabet PaintAlphabet(g, true); } catch (System.InvalidOperationException) { // Scale up the texture size and try again textureWidth *= 2; textureHeight *= 2; continue; } break; } // If requested texture is too big, use a smaller texture and smaller font, // and scale up when rendering. Direct3D.Caps d3dCaps = device.DeviceCaps; // If the needed texture is too large for the video card... if (textureWidth > d3dCaps.MaxTextureWidth) { // Scale the font size down to fit on the largest possible texture textureScale = (float)d3dCaps.MaxTextureWidth / (float)textureWidth; textureWidth = textureHeight = d3dCaps.MaxTextureWidth; for(;;) { // Create a new, smaller font ourFontHeight = (int) Math.Floor(ourFontHeight * textureScale); systemFont = new System.Drawing.Font(systemFont.Name, ourFontHeight, systemFont.Style); try { // Measure the alphabet PaintAlphabet(g, true); } catch (System.InvalidOperationException) { // If that still doesn't fit, scale down again and continue textureScale *= 0.9F; continue; } break; } } // Release the bitmap used for measuring and create one for drawing bmp.Dispose(); bmp = new Bitmap(textureWidth, textureHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); g = Graphics.FromImage(bmp); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g.TextContrast = 0; // Draw the alphabet PaintAlphabet(g, false); // Create a new texture for the font from the bitmap we just created fontTexture = Texture.FromBitmap(device, bmp, 0, Pool.Managed); RestoreDeviceObjects(null, null); }
/// <summary> /// Initialize the device objects /// </summary> /// <param name="dev">The grpahics device used to initialize</param> public void InitializeDeviceObjects(Device dev) { if (dev != null) { // Set up our events dev.DeviceReset += new System.EventHandler(this.RestoreDeviceObjects); } // Keep a local copy of the device device = dev; textureState0 = device.TextureState[0]; textureState1 = device.TextureState[1]; samplerState0 = device.SamplerState[0]; renderState = device.RenderState; // Establish the font and texture size textureScale = 1.0f; // Draw fonts into texture without scaling // Large fonts need larger textures if (ourFontHeight > 60) { textureWidth = textureHeight = 2048; } else if (ourFontHeight > 30) { textureWidth = textureHeight = 1024; } else if (ourFontHeight > 15) { textureWidth = textureHeight = 512; } else { textureWidth = textureHeight = 256; } // If requested texture is too big, use a smaller texture and smaller font, // and scale up when rendering. Direct3D.Caps d3dCaps = device.DeviceCaps; if (textureWidth > d3dCaps.MaxTextureWidth) { textureScale = (float)d3dCaps.MaxTextureWidth / (float)textureWidth; textureWidth = textureHeight = d3dCaps.MaxTextureWidth; } Bitmap bmp = new Bitmap(textureWidth, textureHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g.TextContrast = 0; string str; float x = 0; float y = 0; Point p = new Point(0, 0); Size size = new Size(0, 0); // Calculate the spacing between characters based on line height size = g.MeasureString(" ", systemFont).ToSize(); x = spacingPerChar = (int)Math.Ceiling(size.Height * 0.3); for (char c = (char)32; c < (char)127; c++) { str = c.ToString(); // We need to do some things here to get the right sizes. The default implemententation of MeasureString // will return a resolution independant size. For our height, this is what we want. However, for our width, we // want a resolution dependant size. Size resSize = g.MeasureString(str, systemFont).ToSize(); size.Height = resSize.Height + 1; // Now the Resolution independent width if (c != ' ') // We need the special case here because a space has a 0 width in GenericTypoGraphic stringformats { resSize = g.MeasureString(str, systemFont, p, StringFormat.GenericTypographic).ToSize(); size.Width = resSize.Width; } else { size.Width = resSize.Width; } if ((x + size.Width + spacingPerChar) > textureWidth) { x = spacingPerChar; y += size.Height; } if (c != ' ') // We need the special case here because a space has a 0 width in GenericTypoGraphic stringformats { g.DrawString(str, systemFont, Brushes.White, new Point((int)x, (int)y), StringFormat.GenericTypographic); } else { g.DrawString(str, systemFont, Brushes.White, new Point((int)x, (int)y)); } textureCoords[c - 32, 0] = ((float)(x + 0 - spacingPerChar)) / textureWidth; textureCoords[c - 32, 1] = ((float)(y + 0 + 0)) / textureHeight; textureCoords[c - 32, 2] = ((float)(x + size.Width + spacingPerChar)) / textureWidth; textureCoords[c - 32, 3] = ((float)(y + size.Height + 0)) / textureHeight; x += size.Width + (2 * spacingPerChar); } // Create a new texture for the font from the bitmap we just created fontTexture = Texture.FromBitmap(device, bmp, 0, Pool.Managed); RestoreDeviceObjects(null, null); }
/// <summary> /// Initialize the device objects /// </summary> /// <param name="dev">The grpahics device used to initialize</param> public void InitializeDeviceObjects(Device dev) { if (dev != null) { // Set up our events dev.DeviceReset += new System.EventHandler(this.RestoreDeviceObjects); } // Keep a local copy of the device device = dev; textureState0 = device.TextureState[0]; textureState1 = device.TextureState[1]; samplerState0 = device.SamplerState[0]; renderState = device.RenderState; // Create a bitmap on which to measure the alphabet Bitmap bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g.TextContrast = 0; // Establish the font and texture size textureScale = 1.0f; // Draw fonts into texture without scaling // Calculate the dimensions for the smallest power-of-two texture which // can hold all the printable characters textureWidth = textureHeight = 128; for (;;) { try { // Measure the alphabet PaintAlphabet(g, true); } catch (System.InvalidOperationException) { // Scale up the texture size and try again textureWidth *= 2; textureHeight *= 2; continue; } break; } // If requested texture is too big, use a smaller texture and smaller font, // and scale up when rendering. Direct3D.Caps d3dCaps = device.DeviceCaps; // If the needed texture is too large for the video card... if (textureWidth > d3dCaps.MaxTextureWidth) { // Scale the font size down to fit on the largest possible texture textureScale = (float)d3dCaps.MaxTextureWidth / (float)textureWidth; textureWidth = textureHeight = d3dCaps.MaxTextureWidth; for (;;) { // Create a new, smaller font ourFontHeight = (int)Math.Floor(ourFontHeight * textureScale); systemFont = new System.Drawing.Font(systemFont.Name, ourFontHeight, systemFont.Style); try { // Measure the alphabet PaintAlphabet(g, true); } catch (System.InvalidOperationException) { // If that still doesn't fit, scale down again and continue textureScale *= 0.9F; continue; } break; } } // Release the bitmap used for measuring and create one for drawing bmp.Dispose(); bmp = new Bitmap(textureWidth, textureHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); g = Graphics.FromImage(bmp); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g.TextContrast = 0; // Draw the alphabet PaintAlphabet(g, false); // Create a new texture for the font from the bitmap we just created fontTexture = Texture.FromBitmap(device, bmp, 0, Pool.Managed); RestoreDeviceObjects(null, null); }
public override void Draw() { base.Draw(); System.Diagnostics.Debug.WriteLine("ILDXGraphPlot2D_Draw"); bool antAlias = m_dxPanel.Device.RenderState.AntiAliasedLineEnable; m_dxPanel.Device.RenderState.AntiAliasedLineEnable = true; #region draw line // draw linestrip - simple case - fast vertex rendering if (m_properties.Style == LineStyle.Solid && m_properties.Width == 1) { m_dxPanel.Device.SetStreamSource(0, m_vertexBuffer, 0); m_dxPanel.Device.VertexFormat = m_vertexBuffer.Description.VertexFormat; m_dxPanel.Device.DrawPrimitives(m_primitiveType, 0, m_primCount); } else if (m_properties.Style == LineStyle.None) { } else { // generate line pattern if (m_properties.Style == LineStyle.UserPattern) { m_line.Pattern = m_properties.Pattern; } else { m_line.Pattern = (int)m_properties.Style; } m_line.PatternScale = m_properties.PatternScale; m_line.Width = m_properties.Width; // extract points CustomVertex.PositionColored[] vertices = (CustomVertex.PositionColored[])m_vertexBuffer.Lock(0, 0); Vector3[] pointCoords = new Vector3[m_Vertcount]; for (int i = 0; i < m_Vertcount; i++) { pointCoords[i] = new Vector3(vertices[i].X, vertices[i].Y, vertices[i].Z); } m_vertexBuffer.Unlock(); vertices = null; // extract trafo matrix(/-ces) Matrix mat = Matrix.Identity; mat *= m_dxPanel.Device.Transform.World; mat *= m_dxPanel.Device.Transform.View; mat *= m_dxPanel.Device.Transform.Projection; m_line.Antialias = false; // draw all lines //m_line.Antialias = true; m_line.Begin(); for (int i = 0; i < m_primCount; i++) { m_line.DrawTransform(pointCoords, mat, m_properties.Color); } m_line.End(); } #endregion #region draw points if (m_marker.Style == MarkerStyle.Square) { float pSize = m_dxPanel.Device.RenderState.PointSize; m_dxPanel.Device.RenderState.PointSize = m_marker.Size; m_dxPanel.Device.SetStreamSource(0, m_vertexBuffer, 0); m_dxPanel.Device.VertexFormat = m_vertexBuffer.Description.VertexFormat; m_dxPanel.Device.DrawPrimitives(PrimitiveType.PointList, 0, m_primCount + 1); m_dxPanel.Device.RenderState.PointSize = pSize; } else if (m_marker.Style == MarkerStyle.None) { } else { // draw all other marker styles as bitmap float pSize = m_dxPanel.Device.RenderState.PointSize; TextureStateManager oldStage = m_dxPanel.Device.TextureState[0]; m_dxPanel.Device.RenderState.PointSize = m_marker.Size; m_dxPanel.Device.SetTexture(0, m_pointShape); m_dxPanel.Device.RenderState.PointSpriteEnable = true; m_dxPanel.Device.RenderState.SourceBlend = Blend.InvSourceColor; m_dxPanel.Device.RenderState.DestinationBlend = Blend.BothSourceAlpha; m_dxPanel.Device.RenderState.AlphaBlendEnable = true; m_dxPanel.Device.SetStreamSource(0, m_vertexBuffer, 0); m_dxPanel.Device.VertexFormat = m_vertexBuffer.Description.VertexFormat; m_dxPanel.Device.DrawPrimitives(PrimitiveType.PointList, 0, m_primCount + 1); m_dxPanel.Device.RenderState.PointSize = pSize; m_dxPanel.Device.RenderState.AlphaBlendEnable = false; } #endregion m_dxPanel.Device.RenderState.AntiAliasedLineEnable = antAlias; }