PDA

View Full Version : Write text on texture



shade
09-06-2010, 08:23 AM
Hi to everybody, This is my first post on this forum!
I'm fighting with Opengl, In a 2d application I need to write rotate text (90¬?).
I read that the best way is to write normally on a texture then rotate the texture, but I don't know how to write a text on a transparent texture?
Any Idea?

regards
Lorenzo

User137
09-06-2010, 10:01 AM
It doesn't matter if the text is on texture or rendered in other ways, key thing is that with OpenGL you have full control on where and which way your rendered things show up.

1) Can you render text in normal way with what you have?

2) You can use commands glTranslate() glRotatef() to place and rotate your text.
If you want to rotate 90 so that rotation center point is in the middle of your text, then it goes about:
glPushMatrix; // May want to recall previous matrix afterwards
glTranslate(textX, textY, 0);
glRotatef(90, 0, 0, 1);
glTranslate(-textWidth(text)/2, -fontHeight/2, 0);
DrawText(text);
glPopMatrix;

shade
10-06-2010, 01:43 PM
Thank's User137, I will try it soon as possible!
I'm working on OPENGL for the first time and I'm a little bit confused!

shade
22-07-2010, 03:17 PM
I tested it but seems doesn't work, the 2d font text doesn't rotate!
I'm working on D7 using old borland Opengl header, maybe this is the cause?!
I tried to using DLGOPNEGL header but this cause a lot of errors on init functions that work fine in OPENGL header (I need to work in 2d way on a region(panel) of my application)!
Can someone know a way to use dlgopengl for working in this way (2d on a region), I'm usually use this method and with opengl header work fine:




fillchar(pfd,SizeOf(pfd),0);
with pfd do begin
nSize := SizeOf(pfd);
nVersion := 1; {The current version of the desccriptor is 1}
dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
iPixelType := PFD_TYPE_RGBA;
cColorBits := 24; {support 24-bit color}
cDepthBits := 32; {depth of z-axis}
iLayerType := PFD_MAIN_PLANE;
end;
FglDC := getDC(handle);//the main object is tcustompanel
FormatIndex := ChoosePixelFormat(FglDC,@pfd);
SetPixelFormat(FglDC,FormatIndex,@pfd);
GLContext := wglCreateContext(FglDC);
wglMakeCurrent(FglDC,GLContext);
//glEnable(GL_LINE_SMOOTH); //Antialaising ON
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);


glViewPort(0,0,ClientWidth,ClientHeight);
glMatrixMode(GL_PROJECTION); // Change Matrix Mode to Projection
glLoadIdentity();
gluOrtho2D(0.0,ClientWidth,ClientHeight,0.0);
glMatrixMode(GL_MODELVIEW); // Return to the modelview matrix

User137
22-07-2010, 05:42 PM
Can you show the code which renders text to OpenGL?

shade
23-07-2010, 03:58 PM
I call the glPrint_Rot procedure that use the glPrint (that work fine for default text 0 deg).



procedure glPrint(x,y:integer;text: string;Abase_Font:GLuint);
begin
if text = '' then exit;
glrasterpos2i(x, y);
glPushAttrib(GL_LIST_BIT);
glListBase(Abase_Font-(CHR_Code_Start));//se no i caratteri sono swiftati di uno???
glCallLists(length(text), GL_UNSIGNED_BYTE, Pchar(text));
glPopAttrib;

end;

procedure glPrint_Rot(x,y:integer;text: string;Abase_Font:GLuint;Rot:double);
begin
glPushMatrix;
glTranslated(x, y, 0);
glRotatef(Rot, 0, 0, 1);
glPrint(x,y,text,Abase_Font);
glPopMatrix;
end;

User137
23-07-2010, 09:01 PM
I think you need to take out

glrasterpos2i(x, y);
I don't specifically understand what it does (i suspect it nullifies the rotation), however you already move caret to right position with glTranslate so it is unnecessary to do it second time.

shade
26-07-2010, 09:12 AM
Sorry User137, I tried this solution but seems doesn't work!
If you want I made an easier program to test the problem directly! If you want you can download it and verify by your self if there is something wrong, or if it is possible to find a solution.
thanks for your support!

http://www.abulafia.it/temp/OGL_simple.zip

User137
26-07-2010, 01:05 PM
I have looked in the code and come to conclusion that wglUseFontBitmaps() is the issue. It creates a bitmap, and these graphics don't seem to follow world matrix at all. Nehe example uses wglUseFontOutlines() instead to make vector displaylist. Tutorial was here:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=14

It worked for what i tried:
procedure TForm2.BuildFont(name: Pansichar; size: integer);
var
Font: HFONT;
gmf: array[0..255] of GLYPHMETRICSFLOAT;
...
wglUseFontOutlines(FglDC, 32, 96, base, 0,
0.2, WGL_FONT_POLYGONS, @gmf);

procedure TForm2.glPrint_Rot(x,y:integer; text: string; Rot:double);
begin
if text = '' then exit;
glPushMatrix;
glTranslatef(x, y, 0);
glRotatef(Rot, 0, 0, 1);
glPushAttrib(GL_LIST_BIT);
glListBase(base-32);
glScale(20,20,20); // The font size
glCallLists(length(text), GL_UNSIGNED_BYTE, Pchar(text));
glPopAttrib;
glPopMatrix;
end;

shade
26-07-2010, 01:32 PM
Ok, I'll try this!
Initially I assumed that bitmap fonts could not be rotated, which is why I wrote this post.
Somewhere I read that the only way was to rotate the texture on which they were used!?
The thing that I didn't know was that the outline-fonts could be used as 2d, mistakenly I thought that they worked only in 3d project!
Thanks, I'll know you the results!

shade
28-07-2010, 12:39 PM
Ok, I made some tests, yes with Outline font it is possible to rotate text, the problem is that with this kind of font I can't work in 2d space, or I'm not able! :no:
Seems that when I use the glOrtho function to create the 2d transformation, the text becomes very very small!
If you look in this example, when you press the Ortho button you see that the text (or what I think it is) very very small in the bottom left of the window!
I need to work using pixel coordinate and not spacial one!

http://www.abulafia.it/temp/OGL_SimpleNF.zip

Thanks
Lorenzo

User137
28-07-2010, 02:16 PM
You didn't read all of my last post. I used glScale(20,20,20) to make it normal size. The font is 1 pixel wide without scaling, if you scale by 20 it should be about same as size 20 font. Notice that glTranslate call after scaling will be multiplied aswell. (Use glPushmatrix and glPopmatrix wisely).

Actually glScale(20,20,1) would maybe work better, would keep the text flat so ortho mode's Z-buffer won't be reached too fast.

shade
28-07-2010, 02:51 PM
Sorry, I had not seen the reference to glScale function and I had focused on the example provided!
Thanks now it is clear and it works fine! :-[