opcon / QuickFont

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

Inlcuded Examples do not work, some form of Documentation required #35

Closed ghost closed 5 years ago

ghost commented 7 years ago

The provided Examples in the Readme do not work. In addition to containing multiple spelling mistakes (such as "DrawingPimitiveses" instead of "DrawingPrimitives"), it contains multiple variables not mentioned or explained anywhere else. I tried replacing these variables with values that seem resonable to me however it does not work.

The included "Example" Folder is also not very useable in my opinion. In addition to the massive amount of different strings and other variables whose purpose is rather unclear to someone not familiar with this code, that hideous switch absolutely needs to go.

I think some sort of Documentation or Starter Guide is necessary.

opcon commented 7 years ago

Hi @Sh0rti, thanks for your feedback. I will fix the Readme so that the listed code should work if it's copy-and-pasted.

I will also have a go at cleaning up the Example project and see if I can make the use of QuickFont more clear.

Unfortunately I don't have time to produce some form of Documentation or Starter Guide at the moment, but it's something I will consider in the future. In the meantime, I have attempted to ensure that each method and variable is documented with in-code comments, which takes the place of a separate documentation for now.

ghost commented 7 years ago

Thank you!

In the meantime, I decided to give this another go. I'm quite sure I did everything right, but unfortunately now I get a NullReferenceException inside the QuickFont.dll when I call the draw() Method of my QFontDrawing.

I create the font and the drawing like this

_myFont = new QFont("fonts/raleway.ttf", 72, new QFontBuilderConfiguration(true));
_drawing = new QFontDrawing();

then in my OnLoad I do this

_drawing.DrawingPrimitives.Clear();
_drawing.Print(_myFont, "text1", new Vector3(0,0,0), QFontAlignment.Left);

and in my Draw Loop I do

_drawing.ProjectionMatrix = Matrix4.Identity;
_drawing.Draw();

right before calling SwapBuffers(). I think the problem might be the ProjectionMatrix, but I don't know what else to do there since the readme example simply has "proj" there which is never explained.

Can you help me out? 😢

ghost commented 7 years ago

(Sorry if I'm spamming you here, I just want to get this to work 😶 ) I now use this as my projection matrix, like in the example:

_projectionMatrix = Matrix4.CreateOrthographicOffCenter(ClientRectangle.X, ClientRectangle.Width, ClientRectangle.Y, ClientRectangle.Height, -1.0f, 1.0f);

And of course, set it like this

_drawing.ProjectionMatrix = _projectionMatrix;

I still get the NullReferenceException though.

opcon commented 7 years ago

From the code you've posted it looks like you are not calling

_drawing.RefreshBuffers();

The Print(...) functions store up the strings to print, and then the RefreshBuffers() function creates the vertex buffers and fills them with the required data. I think that could be the cause of the NullReferenceException.

Try adding a call to RefeshBuffers() after you call Print() and see if it fixes it. If not then I am happy to take a look at your project if possible and see where the problem is :)

Ps. you are right about the Projection Matrix, sorry that's not mentioned in the readme. It is the screen projection matrix used by the QuickFont shaders, so the way you've set it up there is correct.

ghost commented 7 years ago

Thanks, I completely forgot about that! :X

Unfortunately this gives me the same problem I had when I tried it before opening this issue. As soon as I have RefreshBuffers somewhere in my drawing loop, the result it that the OpenTK window is just completely blank with only the ClearColor. No text and my textures don't render either. When I remove RefreshBuffers() (and Draw() so I don't get an Exception) the textures start working again.

For testing, I have removed everything not related to QuickFont from my drawing loop. It now looks like this:

protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.ClearColor(Color4.CornflowerBlue);

            _drawing.DrawingPrimitives.Clear();
            _drawing.ProjectionMatrix = _projectionMatrix;
            _drawing.Print(_myFont, "text1", new Vector3(0, 0, 0), QFontAlignment.Left);
            _drawing.RefreshBuffers();
            _drawing.Draw();

            this.SwapBuffers();
        }

I have also added an OnResize method like in the example:

protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
            _projectionMatrix = Matrix4.CreateOrthographicOffCenter(ClientRectangle.X, ClientRectangle.Width, ClientRectangle.Y, ClientRectangle.Height, -1.0f, 1.0f);
        }

I am using QuickFont.Desktop v4.4.6159.26433 and OpenTK 3.0.0 prerelase (I have tried it with OpenTK 2.0.0 stable, but it didn't change anything), both downloaded from NuGet.

ghost commented 7 years ago

Soo...

To ensure that nothing that I did is conflicting with QuickFont, I have created a new project. It does nothing except trying to render text, yet it has the same issues as the other one. The only file in this project looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using QuickFont;
using QuickFont.Configuration;

namespace quickfonttest
{
    static class Program
    {
        public static void Main(string[] args)
        {
            new Window().Run(60,60);
        }
    }

    class Window : GameWindow
    {
        private QFont _myFont;
        private QFontDrawing _drawing;
        private Matrix4 _projectionMatrix;

        public Window() : base(1280,720)
        {

        }

        protected override void OnLoad(EventArgs e)
        {
            GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
            _projectionMatrix = Matrix4.CreateOrthographicOffCenter(ClientRectangle.X, ClientRectangle.Width, ClientRectangle.Y, ClientRectangle.Height, -1.0f, 1.0f);
            _myFont = new QFont("raleway.ttf", 32, new QFontBuilderConfiguration(true));
            _drawing = new QFontDrawing();
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.ClearColor(Color4.CornflowerBlue);

            _drawing.DrawingPrimitives.Clear();
            _drawing.ProjectionMatrix = _projectionMatrix;
            _drawing.Print(_myFont, "text1", new Vector3(0, 0, 0), QFontAlignment.Left);
            _drawing.RefreshBuffers();
            _drawing.Draw();

            this.SwapBuffers();
        }
    }
}

As in the other project, the only thing drawn is the ClearColor. Nothing else, no exceptions, no errors, nothing. The font file exists and is valid.

What am I doing wrong? 😕

opcon commented 7 years ago

Thanks for the sample project, I had a look and realised it's because you are drawing the font at (0,0). With the orthographic off-center projection this is the lower left corner of the window. QuickFont currently draws text down and to the right of the specified point, which means that the text is being drawn offscreen.

Sorry I didn't pick it up earlier, I've done the same thing myself a bunch of times haha

To fix, simply change the coordinates you're drawing at to (0, 100, 0) and you should see the text :)

ghost commented 7 years ago

Ah, that makes sense. I see the text now!

After checking the position of my Camera, it seems like QuickFont is messing with it. I'll need to check that 😄

Thanks for helping me out!

opcon commented 7 years ago

No worries, I'm glad it worked! Let me know if you have any other questsions