Lazarus

Programming => Graphics and Multimedia => OpenGL => Topic started by: marcov on November 28, 2015, 06:01:19 pm

Title: An opengl canvas with signed distance fonts
Post by: marcov on November 28, 2015, 06:01:19 pm
In the past I've dropped hints that I was working on something OpenGL, well, here it is :-)

It is a simple texture with a layered overlay of primitive types (text, rectangles, lines, circles) with pan and zoom, optimized for speed in camera/vision applications. One overlay overlays the picture, one functions as HUD.

The font support is based on signed distance fonts. The test project is not multiplatform (so windows only, win64 should be ok), though many units are relatively platform independent.

http://www.stack.nl/~marcov/standaloneglv3.zip
(exe and source)

Requires Delphi XE3+ or FPC 3.1.1+/Lazarus trunk.   FPC3.0 might work, but requires the only recently committed new records-with-methods versions of TPoint(f) and TRect(f), so won't work out of the box
Title: Re: An opengl canvas
Post by: Ocye on November 30, 2015, 01:29:32 pm
Many thanks for sharing your code. But I wonder that yours as well as many other OpenGL toolkits is based on dglOpenGL instead of TOpenGLContext which is part of the FCL. It adds a lot of unnecessary dependency and incompatibility. For instance, if I want to adopt one your font classes I cannot just add it. I'd rather have to read and understand the code in order to replicate it for my project.
And having a project that is "not multiplatform" contradicts the idea of Freepascal, IMHO.

Please don't read the text as an insulting. To me it's a pity that a lot of work is done for nothing.
Title: Re: An opengl canvas
Post by: marcov on November 30, 2015, 02:33:50 pm
Many thanks for sharing your code. But I wonder that yours as well as many other OpenGL toolkits is based on dglOpenGL instead of TOpenGLContext which is part of the FCL.

Afaik topenglcontrol is a separate component (in components/) and not part of the LCL, let alone the FCL)

But that doesn't matter, the reason is very visible in the original post, it can be compiled with Delphi, so unnecessary Lazarus components are avoided (besides, the codebase is older than the topenglcontrol component)

Quote
adds a lot of unnecessary dependency and incompatibility. For instance, if I want to adopt one your font classes I cannot just add it. I'd rather have to read and understand the code in order to replicate it for my project.

Well, if you think TOpenGLControl is great, port it to Delphi and have it added to dglopengl? If you want to make it universal, make sure it is available everywhere.

Quote
And having a project that is "not multiplatform" contradicts the idea of Freepascal, IMHO.

IMHO that is nonsense. The FPC packages and even RTL have many units that are only for one OS (-type). Yes, some common functionality is abstracted, but not all code (and even less, user's) should be multiplatform.

We are not Java with a state-in-a-state abstraction.

(though strictly speaking it quite portable, since many font solutions are based on ported X packages  and this one is not)

Quote
Please don't read the text as an insulting. To me it's a pity that a lot of work is done for nothing.

IMHO it is a redundant since the original post made the delphi and windows usage clear.  You can port it to openglcontrol and other OSes if you like/need that. 

I might test multi-OS myself one day for fun, but it is not a priority.  I've no interest in openglcontrol, unless it is ported, I think the pascal opengl community is too small to fragment
it unnecessarily over Delphi and FPC.

Title: Re: An opengl canvas
Post by: Ocye on November 30, 2015, 03:32:53 pm
Okay, if you start from Delphi it makes sense. I didn't touch it after 2.0. Good point to understand why people do things differently.
Title: Re: An opengl canvas
Post by: aradeonas on November 30, 2015, 03:50:39 pm
Interesting code marcov.
I just talking about OpenGL GUI in MailList and I dont know your system can help it ot not.
Can you explain more or maybe check MailList?
Title: Re: An opengl canvas
Post by: marcov on November 30, 2015, 04:22:01 pm
Interesting code marcov.
I just talking about OpenGL GUI in MailList and I dont know your system can help it ot not.
Can you explain more or maybe check MailList?

This is not an opengl gui or even going in that direction.  It is reasonably modern opengl use though, and if you would like an opengl GUI, you could maybe
reuse code, specially the font support. But currently it doesn't even feature wrapping (because it is an "infinite" canvas)

Moreover this demo requires quite modern GPU hardware (geometry shaders), only to accelerate uploading, and many older Socs don't do geometry shaders (or not efficiently)

So probably only as an help to learn a bit more advanced (and that depends on viewpoint) OpenGL



Title: Re: An opengl canvas
Post by: aradeonas on November 30, 2015, 04:23:44 pm
Thank you.
Title: Re: An opengl canvas with signed distance fonts
Post by: marcov on March 17, 2016, 10:45:25 am
I haven't done much on this the last two months, but in january I found that you can improve the look of the fonts significantly by playing with the discard cutoff in the newtextfrag shader source.

Locally I changed it from .1 to .4 and that made the fonts more streamlined andbetter looking.
Title: Re: An opengl canvas with signed distance fonts
Post by: PeterX on June 07, 2024, 01:48:23 pm
The font support is based on signed distance fonts. The test project is not multiplatform (so windows only, win64 should be ok), though many units are relatively platform independent.

http://www.stack.nl/~marcov/standaloneglv3.zip
(exe and source)
Hi marcov, this link is invalid now.
Can I get it somewhere else ?

Thanks, Peter
Title: Re: An opengl canvas with signed distance fonts
Post by: marcov on June 07, 2024, 03:29:44 pm
Here it downloads, but I have to explicitely allow it, since it contains an EXE
Title: Re: An opengl canvas with signed distance fonts
Post by: PeterX on June 07, 2024, 07:27:14 pm
Here it downloads, but I have to explicitely allow it, since it contains an EXE
You're right, on the job the rules prevented the download.  Thanks !
Title: Re: An opengl canvas with signed distance fonts
Post by: PeterX on June 07, 2024, 09:55:26 pm
Okay .. show stopper now is

Code: Pascal  [Select][+][-]
  1.   TBW8ImageOnSelect = function (sender:TObject;x,y:integer;Button: TMouseButton; Shift: TShiftState):integer of Object;
  2.   Tonloadoverlay = procedure (meta:TOverlaydata) of object;
  3.   TBW8ImageOnSelectCollection = TList<TBW8ImageOnSelect>;
  4.  

=> TList<TBW8ImageOnSelect>

Projekt kompilieren, Modus: Release, Ziel: D:\Lazarus Projects\standaloneglv3\lib\i386-win32\standalonegltest.exe: Exit code 1, Fehler: 4, Warnungen: 12, Hinweise: 33
...
glblitter.pas(542,7) Note: Local variable "elementformat" not used
glblitfrm.pas(32,56) Error: Identifier not found "TList$1"
glblitfrm.pas(32,57) Error: Illegal expression
glblitfrm.pas(86,22) Error: Illegal expression
glblitfrm.pas(166,29) Error: Type "TBW8ImageOnSelectCollection" is not completely defined


I probably should not try to compile it with Lazarus (3.2) ?
Title: Re: An opengl canvas with signed distance fonts
Post by: PeterX on February 27, 2025, 11:35:18 am
Hi Marcov,,

what's up with this code piece ?

I never saw '<' or '>' in Lazarus sourececode.
Does this only work in Delphi ?

Code: Pascal  [Select][+][-]
  1. TBW8ImageOnSelectCollection = TList[b]<TBW8ImageOnSelect>[/b];

Okay, problem seems to be that there's no '\x.bmp'

Code: Pascal  [Select][+][-]
  1.   bw8image.LoadImage( GetExeDir +'\x.bmp', nil);
  2.  
Title: Re: An opengl canvas with signed distance fonts
Post by: TRon on February 27, 2025, 11:43:32 am
I never saw '<' or '>' in Lazarus sourececode.
Those are used when defining generics (https://wiki.freepascal.org/Generics).

Quote
Does this only work in Delphi ?
No, generics are also part of Free Pascal. Note that mode delphi and mode objfpc work slightly different wrt to generics.

See also reference manual Chapter Generics (https://www.freepascal.org/docs-html/ref/refch8.html).
Title: Re: An opengl canvas with signed distance fonts
Post by: marcov on February 27, 2025, 11:45:35 am
Nearly all my code is $mode delphi. But usually it is specified in the source.
Title: Re: An opengl canvas with signed distance fonts
Post by: PeterX on February 27, 2025, 11:55:43 am
Nearly all my code is $mode delphi. But usually it is specified in the source.

Is the following link still valid ?
Did You do any changes to the code since 07/2024 ?

http://www.stack.nl/~marcov/standaloneglv3.zip
(exe and source)
Title: Re: An opengl canvas with signed distance fonts
Post by: marcov on February 27, 2025, 01:56:28 pm
The download works. There has been new development since then, but I don't have something I can package separately.
Title: Re: An opengl canvas with signed distance fonts
Post by: PeterX on February 27, 2025, 02:53:01 pm
The download works. There has been new development since then, but I don't have something I can package separately.
Thanks !  I'm on the job and download ist blocked here.
So I'll do the download this evening.
Title: Re: An opengl canvas with signed distance fonts
Post by: TRon on February 27, 2025, 03:00:06 pm
fwiw:
I had to remove some windows from unit clauses, then
Code: [Select]
Compile Project, Mode: Release, Target: /media/projects/fpc/standaloneglv3/standalonegltest: Exit code 1, Errors: 4
generics.defaults.pas(2786,31) Error: Generics without specialization cannot be used as a type for a variable
generics.defaults.pas(2784,45) Error: function header doesn't match any method of this class "class Default(TExtendedHashFactoryClass):<erroneous type>;"
generics.defaults.pas(2779,45) Error: Found declaration: class Default:TExtendedEqualityComparer$1.IExtendedEqualityComparer$1<GENERICS.DEFAULTS.TExtendedEqualityComparer$1.T>; Static;
generics.defaults.pas(810,20) Error: Found declaration: class Default(TExtendedHashFactoryClass):TExtendedEqualityComparer$1.IExtendedEqualityComparer$1<GENERICS.DEFAULTS.TExtendedEqualityComparer$1.T>; Static;

Does the project (still) requires trunk or should 3.2.2 be able to handle that (or perhaps downgrade from 3.2.2 to ...) ?

PS: No importance for me as I just gave it a spin in order to see if and what kind of hurdles might be encountered.
Title: Re: An opengl canvas with signed distance fonts
Post by: PeterX on February 27, 2025, 04:26:16 pm
Code: [Select]
generics.defaults.pas

This and other such files are already in folder

C:\lazarus\fpc\3.2.2\source\packages\rtl-generics\src
Title: Re: An opengl canvas with signed distance fonts
Post by: TRon on February 27, 2025, 07:44:23 pm
This and other such files are already in folder
Ah yes, good hint. Thank you PeterX.

That helped with getting through compiling all the generics related code.

25+ windows related snippets commented to finally end up with FPC in a nearly endless loop in an attempt to locate the dglopengl unit going back'n'forth between FPC packages directory and Lazarus components directory (not once it tried to locate the file in the current folder). Unless I overlooked some setting that forces this behaviour,  I have to give up (had to do that anyways because there is too many windows API calls to simply fix that on short notice).
Title: Re: An opengl canvas with signed distance fonts
Post by: marcov on February 27, 2025, 08:41:12 pm
Afaik the creation of an opengl context is somewhat OS specific, and also some of the (non signed distance) WGL font routines are.
Title: Re: An opengl canvas with signed distance fonts
Post by: PeterX on February 27, 2025, 08:46:25 pm
I just found something about Signed Distance Field Fonts
https://www.redblobgames.com/x/2403-distance-field-fonts/

Unfortunately this one for Linux and Mac only:
https://github.com/ShoYamanishi/SDFont

Title: Re: An opengl canvas with signed distance fonts
Post by: PeterX on February 28, 2025, 10:10:15 am
This and other such files are already in folder
Ah yes, good hint. Thank you PeterX.

That helped with getting through compiling all the generics related code.
Example code that doesn't run with the actual Lazarus release version ( currently 3.8 )
is always difficult and mostly unusable for the average User.
Title: Re: An opengl canvas with signed distance fonts
Post by: marcov on February 28, 2025, 10:59:45 am
I think I originally generated my fontmaps with bmfont (http://bitsquid.blogspot.com/2010/04/distance-field-based-rendering-of.html). At some point I also used Hiero, a Java thingy.

Title: Re: An opengl canvas with signed distance fonts
Post by: JernejL on February 28, 2025, 12:37:24 pm
Awesome! good to see more people doing GPU stuff in lazarus / fpc.
 
Next example: Animated & skinned meshes using assimp? :)
 
Title: Re: An opengl canvas with signed distance fonts
Post by: marcov on February 28, 2025, 12:49:06 pm
I only do 2D things, it is for work.  This program was a kind of unit test that later became a benchmark of the second iteration of that functionality.

The first generation was based on a demo made by Almindor in 2007-2008 timeframe.

Since then I have added things like false color textures and I'm currently trying to fix signed fonts when the texture is shown 90 degrees rotated. The texture itself and the other overlay bits are ok, but signed distance fonts are garbled.
Title: Re: An opengl canvas with signed distance fonts
Post by: PeterX on February 28, 2025, 03:02:01 pm
I think I originally generated my fontmaps with bmfont (http://bitsquid.blogspot.com/2010/04/distance-field-based-rendering-of.html). At some point I also used Hiero, a Java thingy.
Thanks for this link, I'll try that.

Maybe I can get these ä ö ü and other chars like '°' onto the OpenGL ..
Title: Re: An opengl canvas with signed distance fonts
Post by: marcov on February 28, 2025, 03:38:00 pm
ChrisR (last name Rorden iirc), did some opengl on the Mac, including multi channel signed distance fonts.

Searching on the forum for signed distance and look at the first few pages of results might yield some interesting threads.

From my notes is that while I started with bmfont, I moved to sdfont later I think, because I couldn't get Hiero to work half of the time.


E.g. https://forum.lazarus.freepascal.org/index.php/topic,37470.msg255979.html#msg255979
Title: Re: An opengl canvas with signed distance fonts
Post by: PeterX on March 01, 2025, 01:06:04 pm
I think I originally generated my fontmaps with bmfont (http://bitsquid.blogspot.com/2010/04/distance-field-based-rendering-of.html). At some point I also used Hiero, a Java thingy.
Thanks for this link, I'll try that.
I must say this page still exists
but download is impossible.

"Grab a binary version here:     distance_field.exe.7z"


There's a working link on the WayBack Machine
Quote
https://web.archive.org/web/20140821105038/http://www.bitsquid.se/files/distance_field.exe.7z
Title: Re: An opengl canvas with signed distance fonts
Post by: ChrisR on May 01, 2025, 02:41:25 am
My cross platform Lazarus solution is here (project 5)
  https://github.com/neurolabusc/OpenGLCoreTutorials

You can see compiled open source applications that use these here
  https://github.com/neurolabusc/surf-ice/releases
and here
  https://github.com/rordenlab/MRIcroGL/releases

The previously listed redblobgames articles is a great introduction, and both that work and my own rely on this core font generator
  https://github.com/Chlumsky/msdf-atlas-gen
TinyPortal © 2005-2018