Skip to content

Instantly share code, notes, and snippets.

@Raffaele3D
Created April 26, 2017 09:22
Show Gist options
  • Save Raffaele3D/69447118291d2b730ca9475531558aff to your computer and use it in GitHub Desktop.
Save Raffaele3D/69447118291d2b730ca9475531558aff to your computer and use it in GitHub Desktop.
Draw text lines by OpenTk in the OpenGL Window with C# and Windows Forms
public void statistics()
{
if (statisticsOnOff)
{
//Generate the plane containing the statistics
GL.Begin(PrimitiveType.Quads);
GL.TexCoord2(0f, 1f); GL.Vertex2(0f, 0f);
GL.TexCoord2(1f, 1f); GL.Vertex2(glControl1.Width, 0f);
GL.TexCoord2(1f, 0f); GL.Vertex2(glControl1.Width, glControl1.Height);
GL.TexCoord2(0f, 0f); GL.Vertex2(0f, glControl1.Height);
GL.End();
if (drawStatistics)
{
// Upload the Bitmap to OpenGL.
// Do this only when text changes.
BitmapData data = text_bmp.LockBits(new Rectangle(0, 0, text_bmp.Width, text_bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
text_bmp.UnlockBits(data);
//Text rendering in th OpenGL Controller
// Render text using System.Drawing.
// Do this only when text changes.
if (displayStatistics)
{
fpsToGeometry = "fps " + idleCounter.ToString();
}
else
{
fpsToGeometry = "";
statisticsOnOff = false;
glControl1_Resized = true;
}
using (Graphics gfx = Graphics.FromImage(text_bmp))
{
gfx.Clear(Color.Transparent);
gfx.DrawString(fpsToGeometry, drawFont, drawBrush, drawPoint); // Draw as many strings as you need
}
drawStatistics = false;
}
if (setUpViewPort)
{
//Enabling the use of the textures in the OpenGL controller
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
}
if (!statisticsTextureCreated)
{
//Text rendering in the OpenGL Controller
// Create Bitmap and OpenGL texture
text_bmp = new Bitmap(glControl1.Width, glControl1.Height); // match glControl1 size
text_texture = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, text_texture);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, text_bmp.Width, text_bmp.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); // just allocate me
statisticsTextureCreated = true;
};
if (glControl1_Resized)
{
// Ensure Bitmap and texture match window size
text_bmp.Dispose();
text_bmp = new Bitmap(glControl1.Width, glControl1.Height);
GL.BindTexture(TextureTarget.Texture2D, text_texture);
GL.TexImage2D(TextureTarget.Texture2D, 0, 0, 0, text_bmp.Width, text_bmp.Height,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
glControl1_Resized = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment