How to render simple 2d text in openGL with delphi? - delphi

I am trying to find a way to display a simple text on an openGL form in Delphi.
I tried this with Labels, but they were not showing up. I'm guessing that the openGL window was rendered on top of it, so I tried to call Label1.BringToFront; but this didn't work.
I also heard of a library called freetype, but I couldn't find info, how to implement it in Delphi.
What is the simplest way to render out text on an openGL form in Delphi?

The simplest way is to use opengl's wglfontbitmaps
const numberchar =128
allocate:
wglMakeCurrent(FDC, FRC);
FFontBase := glGenLists(numberchar);
SelectObject(FDC, FFont.Handle);
wglUseFontBitmaps(FDC,0,128, FFontBase);
draw(atext:ansistring);
glPushAttrib(GL_LIST_BIT);
glListBase(FFontbase);
glCallLists(Length(aText), GL_UNSIGNED_BYTE, Pansichar(aText)); list
glPopAttrib;
free:
glDeleteLists(FFontBase, numberchar);
The draw code is only partial. Best to look for tutorials for similar code.
Note this will only give you basic unscaled font. For fancy text effects, indeed, freetype is the most common route.
A good place to start with Delphi and opengl is http://www.delphigl.com/ Mostly German, but some English boards too.
An alternative (provided that you have D2010+) is to look in the direction of DirectX. D2010+ comes with a directx context component.

Related

Lua graphics display on an existing window

I want to display graphics on top of an existing window, is that possible using pure Lua? because i cannot find the stuff i need, i only found how to display graphics in an specific OpenGL window.
I've looked to some code posted on github
https://github.com/ittner/lua-gd/tree/master
https://github.com/stetre/moongl
https://github.com/vrld/G4L
that's all i've found
There is no way to do it in pure Lua, as it doesn't provide any platform-specific drawing/GUI methods. It's possible to use some library (like those you've listed), but I'm not familiar with them. You can probably use wxlua, which is a wrapper around wxwidgets and provides SetTransparent method on its wxFrame object and ways to draw on a surface with transparent background or transparent brush (you can set the degree of transparency). You still need to find the position of the window you are interested in (to cover it with your window) and this is going to be system-dependent. For example, on Windows you can do this by using winapi window functions.

iOS - Displaying text with OpenGL ES 2.0

I'm surprisingly struggling a lot to display text with OpenGL ES 2.0. There are a ton of posts on stackoverflow, debating the subject, showing a few lines of code, or showing links from 2010 but working with OpenGL ES 1.x (not compatible).
But they are quite vague, and to my knowledge there is not complete code or convenient way to display text with version 2.
Do you know if there is a modern way to display text ? Like just adding a pod and writing something like this ?
[font drawText:#"This is a text" size:#12];
Thanks a lot in advance, any help would be very much liked.
EDIT 1 : I have to use OpenGL 2.0, and I can't use something else, for internal reasons.
EDIT 2 : I've found two libraries that do it :
FTGLES : It crashes at runtime when I try to use it
https://github.com/chandl34/public/tree/master/personal/c%2B%2B/Font
This one is simple but written for ES1, so I need to port the code to ES2.
EDIT 3 : ES1 and ES2 are different : ES2 works with shaders.
Since you are targeting ES2 and shaders are supported it might be a bit hard to create a tool that does this for you. But if you find such a tool it might be conflicted with your flow in many ways such as binding its own shaders and buffers, using settings on blending, depth buffer... Then if it would do that you are having trouble as you may not have a good control over how and where the text is drawn (on a 3D rotating box for instance).
But from your question (it's snippet) it seems more like all you want to do is add some 2D overlay with text which would look exactly like using an UILabel. If this is the case then I suggest you to actually use UILabel to draw these texts. You can add all of these views easily on your view that shows openGL content.
In the other case where you still need to draw text on a 3D object and want to do it very easily I suggest you to still use UILabel but create its screenshot and push it to a new (or atlas) texture. Then you can draw it as any other object. UILabel will then handle all fonts, alignments, colors, multiline and text wrapping, font size adjustments... So if you already have a system to draw a texture in the scene you should not be too far from creating yourself a tool that draws some text on the screen since you use a texture to transfer the data.
Nothing has changed since OpenGL ES1. Usually text is displayed in planar projection and created quads that are textured using font texture. There are many tutorials on this topic.
One example how to do it.
However, this is quite a lot of work when you started from scratch.
There might be better way, but depending on what you plan to do might not be suitable. You may mix UIKit with OpenGL view, having text drawn with UIKit as overlay. (UILabel, UIButton..etc.)

Making a "piece of paper with text on it" in OpenGL (Specifically on iOS 5)

I've never done OpenGL, but I'm looking for some pointers on this particular question on an AR app I'm practicing with.
I'd like to make an app with a "flat rectangle" along with text written on the surface of the rectangle. Visually, I'm imagining something along the lines of a piece of paper with text written on it. Each time the app starts, the text would be something different (the text is pulled from a plist file).
The user would be able to view the paper from all sides, much as if there was a piece of paper hanging in front of him.
Is this trivial to do in OpenGL? How could I get started?
Sorry for the really open-ended question, but I wanted to get a feel for how this kind of thing is done.
Looking at the OpenGL template source code in the Xcode sample projects, I see that there is a big array of vertices. I presume that to create a "flat" rectangle, I'd essentally just have to remove or make the z-axis zero. And then the dynamic text that will attach to the surface of the flat rectangle...I dont have any idea how to do that......
This question is hard to answer unambiguously. In general, this is trivial, but then again it is not.
Drawing a "flat rectangle with something on it" is a couple of API calls, as simple as it can get. Drawing text in OpenGL in an efficient way, and high quality, and without big preprocessing is an entirely different story.
What I would do is render text using whatever the "normal system-supported" way is under iOS (just like you would draw in any window, I wouldn't know this specific detail), but draw into a bitmap rather than on the screen. This should be supported, pretty much every OS has supported this for at least 10-15 years. Then turn this bitmap into a texture, bind it, and draw your trivial flat quad with OpenGL (set up a vertex buffer with 4 vertices, each vertex a texture coordinate, and draw two triangles - as easy as it gets).
The huge advantage of that is that you get to use the installed system fonts (or any fonts available), you don't need to generate a bitmap font and don't need to think about really ugly things such as hinting and proper spacing, and it's much easier to mix different text styles, etc. OpenGL has built-in support for text too, of course, but it is not terribly efficient or nice either. If the text does not change every millisecond, it's really best to render it using the standard renderer that the operating system provides (yes, that probably won't be hardware accelerated, but so what... since the user must read the text, it likely won't change every millisecond).
Now it gets more complicated if your "piece of paper" should bend and twist too, or do a page peel effect rather than being just a flat rectangle. In that case you need to tesselate it, which can be harder than it sounds, too. Not all tesselations look optimal for all bends/twists, or they do but do not have the optimal (read as minimum) number of vertices.
There is an article on "page peel" and such tesselation in one of the GPU Gems or GPU Pro books, let me search...
There: Andreas Bizzotto: "A Shader-Based eBook Reader - Page peeling effect", GPU Pro2 pp. 278-299
Maybe you can get hold of a copy or are lucky enough to find it on Google Books or something.

Options for pure-graphics display in Delphi 2009

My whole experience with direct use of graphics consists of drawing shapes using Turbo Pascal’s gdi unit on a 386 machine ages ago, finding it unbearably slow and never giving it a second thought. In other words, I have next to no idea where to begin.
For a simple Internet radio player application, I would like to design a graphic display somewhat similar to Winamp (but a bit larger and easier on the eyes, since the illegibility of such displays is one reason I’m trying to do my own).
Simple graphic components like those imitating LED displays are nowhere near sufficient, of course. I don’t expect to be drawing clickable UI controls, and certainly not skins – just the readout, with text, digits and a few symbols. I understand Delphi 2010 supports Direct2D, but I only have D2009.
What are my options? Are there any 3rd party components that would help?
On edit two small points. I need the drawing to be flicker-free (i.e,, unlike what I experienced all those years ago in Turbo Pascal :-). Is TCanvas going to be fast enough for that? Also, I would probably want to use alphablending, which I don't think I can get with the basic TextOut, LineTo etc. graphics API. (I just don't know what's possible). What about GDI+?
A library for fast 2D graphics for Delphi is available as open source on Sourceforge:
Graphics32 (home page: http://www.graphics32.org/)
Graphics32 is a graphics library for
Delphi and Kylix/CLX. Optimized for
32-bit pixel formats, it provides fast
operations with pixels and graphic
primitives. In most cases Graphics32
considerably outperforms the standard
TBitmap/TCanvas methods.
Features Some of Graphics32 features include:
Fast per-pixel access up to 100 times faster compared to standard TBitmap;
High-performance Bitmap alpha blending (including per-pixel alpha blending);
Pixel, line and polygon antialiasing with sub-pixel accuracy (combined with alpha blending);
Arbitrary polygon transformations and custom fillings;
Bitmap resampling with high quality reconstruction filters (e.g. Lanczos, Cubic, Mitchell);
A unique state-of-the-art rasterization system;
Affine transformations of bitmaps: rotations, scaling, etc with sub-pixel accuracy;
Arbitrary projective transformations of bitmaps;
Arbitrary remapping transformations of bitmaps (e.g. for Warping, Morphing);
Flexible supersampling implementation for maximum sampling quality;
Flicker-free image displaying components with optimized double buffering via advanced MicroTiles based repaint optimizer;
Multiple customizible easy-to-use overlay layers;
Locking of bitmaps for safe multithreading;
A property editor for RGB and alpha channel loading;
Design-time loading of image formats supported by standard TPicture;
Works on Borland Delphi, C++ Builder and Kylix (The last version that supported Kylix was 1.8.3).
As of version 1.5.1b Graphics32 is licensed under the terms of the Mozilla Public License.
Take a look at TCanvas. It's built into most visual controls and contains easy methods for the sort of simple drawing you're looking at, without the need for mucking around with DirectX or OpenGL. If you want a really simple surface you can draw on, place a blank TImage TPaintBox on the form and use its Canvas as your drawing surface.
Mostly I agree with Mason and the comments. TPaintBox is a very simple component really. You implement the OnPaint event and are in complete control. It is fast enough on a fast-enough machine.
Put it on a TScrollBox and you don't even need to worry about where the user has scrolled to :)
Make sure you check out the DoubleBufferred property of the parent control(panel/ScrollBox).
If it is a complex drawing/graphic also check out InvalidateRect - this controls which area of a canvas is redrawn.
Of course like all graphics/drawing it gets pretty complicated pretty quickly :(
Just saw your edit. If you need alpha blending and flicker-free drawing, TCanvas is probably not going to be good enough for you. You should look at components that wrap OpenGL or Direct3D and provide you a high-power graphics canvas.
One such package is Asphyre, though I'm not sure if they have a version that works on Delphi 2009+ yet. I've also got a custom-built library that wraps SDL 1.3 and gives you a drawing frame component you can place on a Delphi form. It does work on Delphi 2009 and later, but I haven't released it as its own package yet. You could find it if you dig around in the SVN archive of my game engine, but if you can wait a day or two I could try to get it made into an easily-usable BPL component package and available for download.

How to draw a text (write on the screen) using standard OpenGL functions?

I know I can do it with GLUT. But, I have a big project and I had problem adding GLUT (I am using Delphi) to it.
So, what other options do I have?
You can't do it with standard OpenGL functions, unless you basically have textures with text in them, or a list of characters on a texture that you draw from. But don't reinvent the wheel - I would recommend FTGL for rendering text in an OpenGL view. It has several different rendering methods and takes care of things like kerning for you, supports unicode, and has good text metrics features too.
Use TextSuite. http://textsuite.opengl24.de/
Use FreeType to yield a bitmap given your text.
Edit: It is a mature cross platform library that brings a complete text display capabilities based on fonts including normal Windows TrueType fonts. Here is its definition from Wikipedia:
FreeType is a software library written
in C that implements a font
rasterization engine. It is used to
rasterize characters into bitmaps and
provides support for other
font-related operations.
If you do decide to implement your own font system be sure to use a texture atlas, I've seen too many OpenGL demos that use one texture per glyph, which leads to atrocious performance (due to the overheads incurred in the OpenGL driver and texture caches).

Resources