2014-11-18 2 views
1

OpenTK를 사용하여 OpenGL 창 위에 텍스트를 그리려는 것입니다. 일부 자습서를 따라 갔지만 텍스트가 그려지는 곳의 텍스처를 사용할 수없는 경우에는 작동하지 않습니다. 그러면 흰색 창이 나타나고 테스트 용으로 그리는 QUAD가 사라집니다. 누군가 코드를 확인할 시간이 있다면 아래에 있습니다. 테스트 프로그램을 보내 검사를 빨리 할 수도 있습니다. 이것에 대한 도움을 미리 감사드립니다.OpenTK를 사용하여 텍스트 그리기

using System; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Windows.Forms; 

using OpenTK.Graphics.OpenGL; 

using System.Diagnostics; 


namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     Bitmap textBmp; 
     int textTexture = -1; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      if (!glControl1.Context.IsCurrent) 
      { 
       glControl1.MakeCurrent(); 
      } 

      GL.MatrixMode(MatrixMode.Projection); 
      GL.LoadIdentity(); 

      GL.Ortho(0, glControl1.Width, 0, glControl1.Height, -1000, 1000); 

      GL.Scale(1, 1, 1); 

      GL.Viewport(0, 0, glControl1.Width, glControl1.Height); 

      GL.ClearColor(Color.White); 

      // Better point and line drawing 
      GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest); 
      GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest); 

      GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha); 

      GL.Enable(EnableCap.PointSmooth); 
      GL.Enable(EnableCap.LineSmooth); 

      GL.Enable(EnableCap.Blend); 

      // Hide stuff behind in 3D 
      GL.Enable(EnableCap.DepthTest); 

      // Enable the texture 
      GL.Enable(EnableCap.Texture2D); 

      // Create Bitmap and OpenGL texture 
      textBmp = new Bitmap((int)glControl1.Width, (int)glControl1.Height); 

      textTexture = GL.GenTexture(); 
      GL.BindTexture(TextureTarget.Texture2D, textTexture); 

      GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); 
      GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); 

      GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, textBmp.Width, textBmp.Height, 0, 
          OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); 

      ErrorCode errorCode = GL.GetError(); 
      Debug.Assert(errorCode == ErrorCode.NoError, "OpenTK error!"); 
     } 

     private void glControl1_Paint(object sender, PaintEventArgs e) 
     { 
      ErrorCode errorCode; 

      GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit); 

      GL.PushMatrix(); 

      GL.Color3(Color.Black); 

      GL.Begin(PrimitiveType.Quads); 

      GL.Vertex3(10, 10, 10); 
      GL.Vertex3(40, 10, 10); 
      GL.Vertex3(40, 50, 10); 
      GL.Vertex3(10, 50, 10); 

      GL.End(); 

      if (textBmp != null) 
      { 
       using (Graphics gfx = Graphics.FromImage(textBmp)) 
       { 
        gfx.Clear(Color.Transparent); 
        gfx.DrawString("text", new Font("Arial", 10), Brushes.Black, new PointF(textBmp.Width/2, textBmp.Height)); 
       } 

       BitmapData data = textBmp.LockBits(new Rectangle(0, 0, textBmp.Width, textBmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

       GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, (int)glControl1.Width, (int)glControl1.Height, 0, 
        OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); 

       textBmp.UnlockBits(data); 

       errorCode = GL.GetError(); 
       Debug.Assert(errorCode == ErrorCode.NoError, "OpenTK error!"); 

       GL.Begin(PrimitiveType.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(); 
      } 

      errorCode = GL.GetError(); 
      Debug.Assert(errorCode == ErrorCode.NoError, "OpenTK error!"); 

      glControl1.SwapBuffers(); 
     } 
    } 
} 
+0

일반 3D GDI 텍스트 또는 텍스처 텍스트를 3D 표면에 배치 하시겠습니까? – ja72

+0

일반적인 2D 텍스트 만 축 근처에 배치하고 몇 가지 전설은 멋진 요소가 아닙니다. –

+0

'OpenTK'에 FPS 미터를 올려보세요. 텍스트 오버레이를 추가하는 방법을 보여줍니다. – ja72

답변

2

좋아, 마침내 그걸 만들 수있었습니다. 초기화에

난 그냥 한 :

gfx.DrawString(text, font, brush, new PointF(x, y)); 

을 그리고 렌더링 할 :

if (!control.Context.IsCurrent) 
{ 
    control.MakeCurrent(); 
} 

GL.Ortho(0, controlWidth, 0, controlHeight, -1000, 1000); 
GL.Scale(1, -1, 1); // I work with a top/left image and openGL is bottom/left 
GL.Viewport(0, 0, controlWidth, controlHeight); 
GL.ClearColor(Color.White); 
GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest); 
GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest); 
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha); 
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); 
GL.PolygonMode(MaterialFace.Front, PolygonMode.Line); 
GL.Enable(EnableCap.PointSmooth); 
GL.Enable(EnableCap.LineSmooth); 
GL.Enable(EnableCap.Blend); 
GL.Enable(EnableCap.DepthTest); 
GL.ShadeModel(ShadingModel.Smooth); 
GL.Enable(EnableCap.AutoNormal); 

bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 
gfx = Graphics.FromImage(bmp); 
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; 

texture = GL.GenTexture(); 
GL.BindTexture(TextureTarget.Texture2D, texture); 
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); 
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); 
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp.Width, bmp.Height, 0, 
OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); 

그런 다음 비트 맵에 텍스트를 작성하기 위해 속임수를 썼는지
if (!control.Context.IsCurrent) 
{ 
    control.MakeCurrent(); 
} 

GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); 

GL.MatrixMode(MatrixMode.Modelview); 

GL.Enable(EnableCap.Texture2D); 
GL.BindTexture(TextureTarget.Texture2D, Texture); 

GL.Begin(PrimitiveType.Quads); 

GL.TexCoord3(0.0f, 0.0f, 0f); GL.Vertex3(0f, 0f, 0f); 
GL.TexCoord3(1.0f, 0.0f, 0f); GL.Vertex3(realWidth, 0f, 0f); 
GL.TexCoord3(1.0f, 1.0f, 0f); GL.Vertex3(realWidth, realHeight, 0f); 
GL.TexCoord3(0.0f, 1.0f, 0f); GL.Vertex3(0f, realHeight, 0f); 

GL.End(); 

GL.Disable(EnableCap.Texture2D); 

control.SwapBuffers(); 

.
텍스쳐로 쿼드를 렌더링하기 직전에 GL.Enable (EnableCap.Texture2D)을, 그리고 이후에 GL.Disable (EnableCap.Texture2D)를 렌더링하기 전에 매우 중요합니다 (적어도 필자는 생각합니다) :

- GL.Enable (EnableCap.Texture2D)을 사용한 후 GL.BindTexture (TextureTarget.Texture2D, Texture).

희망이 있으면 도움이 될 것입니다. 내가 약간의 시간을 가질 수 있다면 C# 클래스를 만들고 여기에 게시 할 것입니다.

관련 문제