PDA

View Full Version : glOrtho: How can I MOVE my text



Structural
01-09-2003, 11:24 PM
I used Nehe's method of drawing text: a display list with all characters, then calling the characters in list with a char array.
Problem is that all my text I print is in the bottom of the screen and printed right behind the previous text.

My code that handles the drawing:




glPushMatrix();
glOrtho(0,bound_x,bound_y,0,-1,1);
glTranslatef(300,400,0);
glPushAttrib(GL_LIST_BIT);
glListBase(base - 32);

glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
glPopAttrib();
glPopMatrix();


As you see I have tried to use translatef to move the text about, but it doesn't work. Obviously I'm missing something simple.

Thanks!

Morglum
01-10-2003, 12:20 AM
Show us what's inside the display list.

Structural
01-10-2003, 12:23 AM
Name of the list is "base", here I assemble the characters.




HFONT font;
HFONT oldfont;

base = glGenLists(96);


font = CreateFont( -size,
0,
0,
0,
FW_BOLD,
FALSE,
FALSE,
FALSE,
ANSI_CHARSET,
OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY,
FF_DONTCARE|DEFAULT_PITCH,
name);

oldfont = (HFONT)SelectObject(hDC, font);
wglUseFontBitmaps(hDC, 32, 96, base);
SelectObject(hDC, oldfont);
DeleteObject(font);

T
01-10-2003, 12:41 AM
I *think* you have to use glRasterPos* commands to control the output position rather than glTranslatef.

Structural
01-10-2003, 01:32 AM
Yes, I've used glRasterPos and it seems to work. A few things though...

1: x axis is flipped. Is the ortho effected by the rotation? I have rotated my camera 180 degrees, so it seems to be logical.

2: I thought to be smart and reloaded the identity matrix to see where the x flip came from... yeh, right, no text appears AT ALL...
When i take out the rasterpos my text moves off the screen: it starts in the bottom of the screen and then quickly moves to the right. I mean, WTF?




glColor3f(rc,gc,bc);

glPushMatrix();
glMatrixMode(GL_PROJECTION);

glPushMatrix();
glLoadIdentity(); // <- bad bad matrix!
glOrtho(0,800,0,600,-1,1);
glMatrixMode(GL_MODELVIEW);
//glRasterPos2f(x,y); // <- If left in, no text visible, if not, text moves off screen
glPushAttrib(GL_LIST_BIT);
glListBase(base - 32);

glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
glPopAttrib();
glPopMatrix();



[This message has been edited by Structural (edited 01-10-2003).]

T
01-10-2003, 02:00 AM
RasterPos will be affected by the current modelview matrix.

The code fragment above has a mismatch with the number of PushMatrix and PopMatrix commands - does this occur in the program, or have you missed a pop off the cut/paste. Also remember that PopMatrix will pop the current matrix - you push the projection, but pop the modelview.

[This message has been edited by T (edited 01-10-2003).]

Structural
01-10-2003, 02:20 AM
oi... hadden't seen that, that was indeed a bug. But anyway, I took the MatrixMode out but the text keep running off the screen.
When I put in a glMatrixMode(GL_MODELVIEW) line in the end to match the pushes, followed by another pop there isn't any change also...

I copy-pasted NEHE's DISPLAY code from his lesson17 (texture mapped fonts) that also used the otho view, but the same things happens, while his demo works ok.

Any ideas of what the hell is going on?

T
01-10-2003, 02:44 AM
If using RasterPos results in nothing visible, then that suggests that the position you are giving it is not visible. Beware that if you place the rasterpos off the left of the screen, nothing will get drawn even if you think some of the text should be visible (if rasterpos is not visible nothing gets drawn at all).

Without using RasterPos you say the text moves. I *think* the rasterpos will get updated when you have drawn the text to be at the end of the text, thus repeated drawing may make the text move to the right. Just a thought.

Structural
01-10-2003, 03:35 AM
I have tried many reasonable values that should lie in my raster, but nothing... Even values between 0 and 1...

When I leave out the loadIdentity it works fine. I do see text without the loadidentity, but as soon as I put in the loadidentity it's gone.....

Could it have something to do with the last two parameters of glOrtho: the far and near clipping plane? I have tried this with different values too (originally -1 and 1) (no result ofcourse).

T
01-10-2003, 03:47 AM
What is it you are trying to do? Place text just using 2D pixel coordinates or place text within a 3D scene.

I'm guessing that you are trying to place text by specifying its 2D pixel coords. In this case your projection matrix should be set with ortho (like you are doing) and your modelview matrix should be identity.

If you are using 2D commands then z will be assumed zero and it should be within the range in your ortho command.

Why do you rotate the camera 180degrees?

Structural
01-10-2003, 03:58 AM
Originally posted by T:
What is it you are trying to do? Place text just using 2D pixel coordinates or place text within a 3D scene.

Why do you rotate the camera 180degrees?

To the first point: I simply want to place some text with 2D coords. Nothing fancy really (exept that it's the first time I try this).

To the second point: I'm making my first game to learn things like reflections: pong3D (no comments please), and I chose for that setup in the first place. I chose that the camera should look down to the positive Z.

01-10-2003, 08:30 AM
Here's how I do it to get the text overlayed on the scene.

/2D OVERLAY STUFF
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,1,0,1); //coordinates are 0,0 to 1,1 in 2D
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0f,1.0f,1.0f); //color for text
glRasterPos2f(0.02f,0.95f); //text position ( top leftish )
glPrint("vPosition.x=%7.2f);

//3D STUFF
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f,(GLfloat)1024/(GLfloat)768,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity();
draw blah blah
rotate blah blah
translate blah blah

Structural
01-10-2003, 09:37 AM
You rock!
Both of you http://www.opengl.org/discussion_boards/ubb/smile.gif

It works now with the gluOrtho2D (and with glORtho too now) and I have a slight idea of what went wrong.

What eXor did (I think) in his example code is reset the GL_PROJECTION and GL_MODELVIEW both to the origin (swith to the one, load origin, to other, load origin). I missed the part where I set the projection to the origin.

But what's beyond me is whether the projection and modelview matrix are actually two seperate matrices? I thought there was one view matrix, and that it was overwritten if you switched.

I have to note this down:
1) switch to projection matrix
2) reset to origin
3) switch to modelview matrix
4) reset to origin

Am I correct in this?

T
01-10-2003, 09:56 AM
The projection and modelview matrices are separate matrices.

Its more like:
1. switch to projection
2. reset to identity
3. load a projection matrix (ortho or perspective)
4. switch to modelview
5. reset to identity
6. do stuff

T
01-10-2003, 09:58 AM
Oh, and remember that any matrix command always operates on the matrix you last "activated" (i.e. used in glMatrixMode).

01-10-2003, 11:00 AM
ortho mode is 2D There is no depth ( z axis ) that's why the text doesn't move.

Well it looks like its moving because when you move forward it's still there.

You can't outrun it http://www.opengl.org/discussion_boards/ubb/smile.gif

Structural
01-10-2003, 11:38 AM
Thanks people, I'm finally beginning to understand this ortho stuff, though I still have a long way to go.