opcon / QuickFont

A Modern OpenGL Font Rendering Library for OpenTK
https://github.com/opcon/QuickFont
MIT License
83 stars 25 forks source link

Using Quickfont in drawing environment #37

Closed Happe79 closed 5 years ago

Happe79 commented 6 years ago

Hello,

Thanks a lot for the Quickfont library. It helps me a lot.

I have some problems to embed Quickfont into an existing drawing context where lines an rectangles are drawn.

Every time I draw text like in the example all the other drawing primitives like lines and rectangles go away.

Here is my sample code (in the drawing routine):

QFont _myFont = null;

public void render()
{
         if (_myFont == null)
           {
                    QFontBuilderConfiguration fontConfig = new QFontBuilderConfiguration(false);

                    _myFont = new QFont(@"C:\Windows\Fonts\arial.ttf", 6, fontConfig);
                    _fontDrawing = new QFontDrawing();

                    // it does not matter if this part is inside the if section or not
                    _fontDrawing.DrawingPrimitives.Clear();
                    _fontDrawing.Print(_myFont, "This is my test text!", new Vector3(200, 200, 0), QFontAlignment.Left);
                    _fontDrawing.RefreshBuffers();
                }

 GL.Viewport(0, 0, (int)m_Viewport.GetDx(), (int)m_Viewport.GetDy());
  GL.Clear(ClearBufferMask.ColorBufferBit);
 GL.ClearColor(OpenTK.Graphics.Color4.CornflowerBlue);
 GL.Disable(EnableCap.Texture2D);

  // As a test just drawing a simple line over the screen             
  GL.MatrixMode(MatrixMode.Projection);
  GL.LoadIdentity();

  int dx1 = (int)m_Viewport.GetDx();
  if (dx1 < 1) dx1 = 1;
  int dy1 = (int)m_Viewport.GetDy();
  if (dy1 < 1) dy1 = 1;
  GL.Ortho(0, dx1, dy1, 0, 0.0f, 100.0f);

  GL.MatrixMode(MatrixMode.Modelview);
  GL.LoadIdentity();

  // this line is not visible :(
  GL.Color4(1.0, 0.0, 1.0, 1.0);
  GL.LineWidth(8.0f);
  GL.Begin(BeginMode.Lines);
  GL.Vertex3(100.0f, 100.0f, 0.0f);
  GL.Vertex3(1000.0f, 600.0f, 0.0f);
  GL.End();

  GL.Enable(EnableCap.Texture2D);
  _fontDrawing.VertexArrayObject.EnableAttributes();
  _fontDrawing.ProjectionMatrix = Matrix4.CreateOrthographicOffCenter(0, (int)m_Viewport.GetDx(), 0, 
  (int)m_Viewport.GetDy(), 0.0f, 100.0f);
   _fontDrawing.Draw();

  SwapBuffers();
}

I found out that it has something to do when the Shading is enabled. What is the best way to do this? Did I forget something.

Thanks alot for any help.

Best regards

Happe

opcon commented 6 years ago

Hi Happe,

It looks like you are using OpenGL immediate mode for your lines, whereas QuickFont uses "modern" OpenGL, i.e. shaders and vertex buffers. (see here for a brief explanation of the differences: https://stackoverflow.com/a/6734071) I have not tried combining the two, and I think that this is the source of the problem.

I would suggest removing your immediate mode OpenGL code and replacing it with the equivalent vertex buffers / shaders. Even though it might be more difficult in the beginning, it will be more efficient in the long run. Immediate mode has been deprecated in the latest OpenGL versions.

These two tutorials are a good introduction to modern OpenGL:

https://learnopengl.com/ https://open.gl/

Happe79 commented 6 years ago

Hello Opcon,

Thanks a lot for the anwser. Yes , at the moment the base is implemented in immediate mode. However in most cases I use display list. I will check if I can change it to modern OpenGL.

THX Happe