Здравствуйте! Собственно, требуется добавить текст в glConrol. Встроенных методов для этого я не нашел. Как я понял, их и нету =\. Решением является сделать bitmap с текстом (средствами System.Drawing.Graphics.DrawString или др.) и натянуть её как текстуру. Это описано тут http://www.opentk.com/doc/graphics/how-to-render-text-using-opengl. Ну и, в общем, ничего не получается.
| Код | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Windows.Forms; using OpenTK.Graphics.OpenGL; using System.Drawing.Text;
namespace OpenEx_3 { public partial class Form1 : Form { Bitmap text_bmp; int text_texture;
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { glControl1.Paint += new PaintEventHandler(glControl1_Paint); SetupViewPoint(); text_bmp = new Bitmap(glControl1.Width, glControl1.Height); // match window 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); }
void glControl1_Paint(object sender, PaintEventArgs e) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Graphics gfx = Graphics.FromImage(text_bmp); gfx.Clear(Color.Transparent); gfx.DrawString("SSS", new Font(new FontFamily(GenericFontFamilies.SansSerif), 20, GraphicsUnit.Pixel), Brushes.Black, 10, 10); // Draw as many strings as you need 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, PixelInternalFormat.Rgba, Width, Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); text_bmp.UnlockBits(data);
GL.Enable(EnableCap.Texture2D); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
GL.Begin(BeginMode.Quads); GL.TexCoord2(0f, 1f); GL.Vertex2(0f, 0f); GL.TexCoord2(1f, 1f); GL.Vertex2(1f, 0f); GL.TexCoord2(1f, 0f); GL.Vertex2(1f, 1f); GL.TexCoord2(0f, 0f); GL.Vertex2(0f, 1f); GL.End();
GL.Flush(); GL.Finish(); glControl1.SwapBuffers(); }
private void SetupViewPoint() { GL.Viewport(0, 0, glControl1.Width, glControl1.Height); GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(-4, 4, -4, 4, -4, 4); GL.MatrixMode(MatrixMode.Modelview); }
} }
|
Что делаю не так? |