Problem using quickfont in immediate mode application.
bzuidgeest opened this issue · comments
Hello,
I have been working on porting an old c application to c#. The application uses opengl to render some graphics in what I understand to be called immediate mode. (I'm clearly inexperienced with opengl). I'm trying to get a 1 to 1 port going before I make broad changes, but I do have to replace the font rendering code. I thought to use quickfont and running through the examples I got it to display text in the application. Unfortunately as soon as I draw text the immediate mode graphics that come afterward are no longer visible on the screen. I cannot figure out what I'm doing that causes this. Might be obvious to people accustomed to opengl, but that's not me :)
I made the test code below to show my problem. The first quad renders and the second does not unless I comment out the qfont code.
Hope someone can point met in the correct direction.
This is on .net Core 2.0 using a quickfont lib modified to work with that (using system.drawing compat library and removing the GDI stuff.
static void Main(string[] args)
{
// Workarround for current directory being the project folder not the debug folder.
Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
INativeWindow window = new NativeWindow(
640,
480,
"TestApp",
GameWindowFlags.Default,
GraphicsMode.Default,
DisplayDevice.Default);
window.Visible = true;
IGraphicsContext context = new GraphicsContext(GraphicsMode.Default, window.WindowInfo);
context.MakeCurrent(window.WindowInfo);
(context as IGraphicsContextInternal).LoadAll();
GL.ClearColor(Color4.Purple);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
// This quad shows
GL.Begin(PrimitiveType.Quads);
GL.Color3(Color.Red);
GL.Vertex2(0, 0);
GL.Color3(Color.Blue);
GL.Vertex2(0.9f, 0);
GL.Color3(Color.Orange);
GL.Vertex2(1, -0.9f);
GL.Color3(Color.Green);
GL.Vertex2(0, -1);
GL.End();
Matrix4 projectionMatrix = Matrix4.CreateOrthographicOffCenter(0, 640, 0, 480, -1.0f, 1.0f);
// this text shows
QFont font = new QFont(@"test_condensed_regular.ttf", 72, new QuickFont.Configuration.QFontBuilderConfiguration(true));
QFontDrawing loadingText = new QFontDrawing();
loadingText.Print(font, "Testing..", new Vector3(100, 300, 0), QFontAlignment.Left, new QFontRenderOptions() { Colour = (Color)new Color4(0.8f, 0.1f, 0.1f, 1.0f) });
loadingText.RefreshBuffers();
loadingText.ProjectionMatrix = projectionMatrix;
loadingText.Draw();
// This quad does not show unless I comment out the QFont code
GL.Begin(PrimitiveType.Quads);
GL.Color3(Color.Red);
GL.Vertex2(0, 0);
GL.Color3(Color.Blue);
GL.Vertex2(0, 0.9f);
GL.Color3(Color.Orange);
GL.Vertex2(-0.9f, 1);
GL.Color3(Color.Green);
GL.Vertex2(-1, 0);
GL.End();
context.SwapBuffers();
Console.WriteLine("Hello World!");
Console.ReadKey();
}
Hi,
QuickFont only supports modern OpenGL rendering, and hasn't been tested with immediate mode applications.
Your problem occurs because QuickFont sets up it's own vertex and fragment shaders when you call the Draw() function, replacing the default immediate mode vertex and fragment shaders.
I think if you set up your own basic vertex and fragment shaders then the immediate mode draw calls might still work, but I have not tested it.
If at all possible I would suggest converting the drawing code to modern OpenGL as you port it to C#.
These two tutorials are a good introduction to modern OpenGL:
Thanks for answering. Seems I cannot escape learning modern open GL a little sooner than I hoped. The learnopengl.com site looks like an excellent resource.