![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
Senior Member
Join Date: Sep 2005
Location: Brighton, UK
Posts: 584
|
Here is an OpenGL/GDI Font, inspired by D3DFont. Its pretty straight forward to use.
Just create the font object specifying the name of the typeface and the rest of the attributes and then call initialise. When you're rendering you scene call render with your string and the coordinates you require. As usual, I'm aiming for simplicity, and tried to keep dependencies down to a minimum. I only really use this as a debugging tool so its not the most efficient possible. However its not bad, with very few tweaks you can use this in a more efficient way. I havent bothered to do any checking for large font sizes. It will probably crash. You could export the texture file (look at where the texture is uploaded at gluBuild2DMipmaps) and use that on non-windows platforms. Of course, you'd have to use public domain typeface. Let me know what you think! Here is the code which you can hopefully just drag and drop into your Windows/OpenGL project. Font.h Code:
Font.cpp Code:
Last edited by Reedbeta : 02-13-2007 at 10:40 AM. |
|
|
|
|
|
#2 |
|
New Member
Join Date: Dec 2005
Posts: 27
|
How about a simple demo using it?
|
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Sep 2005
Location: Brighton, UK
Posts: 584
|
Its pretty simple, but just for you, I've adapted a glut example
Code:
|
|
|
|
|
|
#4 |
|
New Member
Join Date: Dec 2005
Posts: 27
|
Do you know what's going on?
![]() I just changed the background color of font and display to see outline of characters better. Didn't change anything else. |
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Sep 2005
Location: Brighton, UK
Posts: 584
|
It should look like this:
![]() If I change the background colour, like this: ![]() |
|
|
|
|
|
#6 |
|
New Member
Join Date: Dec 2005
Posts: 27
|
Ah, I think I see the problem. The posted code is bad.
You posted: else if (*ptr == 'n') and it should be: else if (*ptr == '\n') Code:
Code tags work ok for me. Is there a reason why the code was posted bad? Any other errors I miss? |
|
|
|
|
|
#7 |
|
Senior Member
Join Date: Sep 2005
Location: Brighton, UK
Posts: 584
|
Very odd... I've not made any changes to the code since posting it.
Perhaps it was the way I cut and paste it.... maybe. There are two instances of 'n' that need changing to '\n' (one in getTextSize, the other in render) |
|
|
|
|
|
#8 |
|
DevMaster Staff
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
|
Hmm...I made the changes. I think this might have to do with the string not being properly MySQL escaped when submitted through the COTD form. I always have to add backslashes on quotes and things when I use it.
___________________________________________
Currently working at Sucker Punch reedbeta.com - OpenGL demos and other projects Luabridge - a lightweight, dependency-free C++/Lua binding library. CD Lite - an unobtrusive, minimal CD player application for Windows. |
|
|
|
|
|
#9 |
|
New Member
Join Date: Dec 2005
Posts: 27
|
What I would find most useful in a OpenGL Font renderer, would be a function that
would mimic the parameters of TextOut(). Basically, an OpenGL version of TextOut(). Both functions should render text in the exactly the same location on screen, at exactly the same scale and size. |
|
|
|
|
|
#10 |
|
DevMaster Staff
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
|
Lost, what does TextOut do that this sample does not? Both of them just take a string and a location at which to display it. The only other thing I can see that TextOut does is responds to left/center/right alignment settings, but that could easily be added to this class.
EDIT: another interesting extention I can think of is to let the render function take printf-style varargs...this would be pretty easy to glom on with vsnprintf ![]()
___________________________________________
Currently working at Sucker Punch reedbeta.com - OpenGL demos and other projects Luabridge - a lightweight, dependency-free C++/Lua binding library. CD Lite - an unobtrusive, minimal CD player application for Windows. Last edited by Reedbeta : 02-13-2007 at 04:30 PM. |
|
|
|
|
|
#11 |
|
New Member
Join Date: Dec 2005
Posts: 27
|
TexOut() takes logical coordinates, while the above code takes pixel coordinates,
so they don't render text at the same location on the screen. The exact location, size and scale is important for me, because I might want to print it out. In other words, you could use OpenGL's TextOut() as a preview, and then use GDI's TextOut() for printing. If I could, I would use TextOut() directly on the backbuffer, but you can't. http://support.microsoft.com/kb/131024 Last edited by Lost : 02-13-2007 at 06:47 PM. |
|
|
|
|
|
#12 | |
|
New Member
Join Date: Dec 2005
Posts: 27
|
I rendered GDI's TextOut() on the top, and font.cpp on the bottom, same font style and font size,
spacing seems to be off by some pixels: ![]() I am serious about trying to create an exact copy of GDI TextOut() down to the last pixel. ![]() I'm not sure if this has to do with OpenGL's pixel rasterization? Microsoft has some recommendations. The rasterization rules are specific: Quote:
Last edited by Lost : 02-13-2007 at 09:02 PM. |
|
|
|
|
|
|
#13 |
|
DevMaster Staff
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
|
Well, I admit I'm "lost" as to why you would care so much about a few single-pixel errors - I don't see why you can't just use TextOut itself. But, if you want, feel free to take dave's code and modify it to fix the offsets.
___________________________________________
Currently working at Sucker Punch reedbeta.com - OpenGL demos and other projects Luabridge - a lightweight, dependency-free C++/Lua binding library. CD Lite - an unobtrusive, minimal CD player application for Windows. |
|
|
|
|
|
#14 |
|
Senior Member
Join Date: Sep 2005
Location: Brighton, UK
Posts: 584
|
If you want an exact copy of what you get in text out, you should probably render the entire string to texture then display that instead of the technique I'm using. But in the case you've shown, it looks like the space is just 1 pixel to large. Perhaps spacing_ should be (fontSize_ / 3) - 1
The font probably renders the same as D3DFont. Last edited by dave_ : 02-14-2007 at 01:45 AM. |
|
|
|
|
|
#15 | |||
|
New Member
Join Date: Dec 2005
Posts: 27
|
Quote:
Quote:
Quote:
so spacing seems to be complicated. And rendering out an entire line is not practical for me, as length will always be an issue. Per character is best. |
|||
|
|
|
|
|
#16 | |
|
DevMaster Staff
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
|
Quote:
Right, but why would you want to use TextOut on an OpenGL backbuffer anyway? You mentioned using it to construct some screen preview for printing, but if that's the case all your printing has to be done using GDI anyway, so your screen preview can be done in GDI too, and you could just use TextOut itself. ![]() And I don't get your point about multi-pixel errors when the text is enlarged. Proportional to the size of the text the errors will still be very, very small. Really, why bother worrying about such small offsets? It's not like TextOut is some paragon of typographical perfection anyway.
___________________________________________
Currently working at Sucker Punch reedbeta.com - OpenGL demos and other projects Luabridge - a lightweight, dependency-free C++/Lua binding library. CD Lite - an unobtrusive, minimal CD player application for Windows. |
|
|
|
|
|
|
#17 |
|
New Member
Join Date: Dec 2005
Posts: 27
|
I want to use elements of OpenGL and TextOut() together (interactively), and the
only way to do that is to render TextOut() to the backbuffer. But this is prohibited. The exact position, size and scale of the font is important. The OpenGL version of TextOut() must match its GDI equivalent exactly. |
|
|
|
|
|
#18 | |
|
DevMaster Staff
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
|
Quote:
You still haven't answered my question: why is it so important? Honestly, I can't think of a good reason.
___________________________________________
Currently working at Sucker Punch reedbeta.com - OpenGL demos and other projects Luabridge - a lightweight, dependency-free C++/Lua binding library. CD Lite - an unobtrusive, minimal CD player application for Windows. |
|
|
|
|
|
|
#19 | |
|
Senior Member
Join Date: Sep 2005
Location: Brighton, UK
Posts: 584
|
Quote:
|
|
|
|
|
|
|
#20 | ||
|
New Member
Join Date: Dec 2005
Posts: 27
|
Quote:
![]() WYSIWYG. It doesn't make much sense to print something out that doesn't look exactly what you created. I hope you understand that. Quote:
I could probably create a pbuffer the same size as the backbuffer, and copy it over at the end of every render. It just depends on how much video memory is available, as all buffers must be created in video memory. I might have a screen display of 1024x768x32 or larger, and might also want to create some extra pbuffers for other work in addition to the 3 buffers already being used for flipping (font/back/pbuffer copy). I'm guessing most 3D cards nowadays come with 128MB or more? Last edited by Lost : 02-15-2007 at 12:34 AM. |
||
|
|
|
|
|
#21 | |
|
DevMaster Staff
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
|
Quote:
True, but my original points remain. Printers don't support OpenGL. Therefore, if you're printing something, either (a) you are using GDI to construct the printed image, in which case you could just use GDI to construct the screen preview as well and use TextOut in both cases, or (b) you're printing a bitmap rendered by OpenGL, in which case the bitmap would be a pixel-for-pixel copy (or maybe a high-res rerendering) of the bitmap on the screen including the text by whatever method it was drawn. So, your request still doesn't make any sense to me. ![]()
___________________________________________
Currently working at Sucker Punch reedbeta.com - OpenGL demos and other projects Luabridge - a lightweight, dependency-free C++/Lua binding library. CD Lite - an unobtrusive, minimal CD player application for Windows. |
|
|
|
|
|
|
#22 | |
|
Senior Member
Join Date: Sep 2005
Location: Brighton, UK
Posts: 584
|
Quote:
I really dont understand what you're trying to do but I mean the other way round. I mean use GDI to draw your document/TextOut onto a bitmap then use that bitmap as a texture, like I'm doing with the fonts. Last edited by dave_ : 02-15-2007 at 03:31 AM. |
|
|
|
|
|
|
#23 | ||
|
New Member
Join Date: Dec 2005
Posts: 27
|
Quote:
(c) Bitmap rendered by OpenGL and TextOut() If I can get TextOut() to the backbuffer somehow, then my problems will be solved. Quote:
The problem is, the end result must be displayed interactively, so creating a buffer in software, and then copying to video memory would be extremely slow. All buffers should really be created in video memory. And also, there's an issue with layering the elements of the scene. Text is generally the last element to be rendered, so by that alone, it wouldn't work out very well. |
||
|
|
|
|
|
#24 |
|
Senior Member
Join Date: Sep 2005
Location: Hamburg / Germany
Posts: 597
|
If you need the same kerning as Win32 TextOut I might have what you need.
This litte test-code generates the same output. You can use it to extract the kerning tables from ttf-fonts. Note that most fonts have different kerning tables for different font sizes, so you can't just extract one table and use it for scaled fonts (it will look like shit). Code:
|
|
|
|
|
|
#25 |
|
New Member
Join Date: Dec 2005
Posts: 27
|
Oops, got it compiled. Thanks.
Missing: Code:
GGO_GRAY8_BITMAP returns 65 levels of gray (0 to 64). Shouldn't this line be: Code:
Last edited by Lost : 02-15-2007 at 08:23 PM. |
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|