PDA

View Full Version : Displaying text - little problem



TheGecko
04-14-2000, 11:18 AM
Hey all!

Ok suppose I have this function:

glDrawText(LPCTSTR Text)

Now,in my main renedering loop I'd write

glRasterPos(1.0f,1.0f);
glDrawText("Hello");

The text is drawn but it it sort of stays at the origin.I want the text to be drawn,say, at the top left corner of my screen and it STAYS there when I move the camera around in 3D space. Any suggestions? Or does anyone have any text functions they can give me instead? Mine is a bit messy http://www.opengl.org/discussion_boards/ubb/smile.gif Some simple function like glText(LPCTSTR MyText,int x,int y) sould be nice. I'm still trying to get this whole no-2D issue in OpenGL solved http://www.opengl.org/discussion_boards/ubb/frown.gif

Gorg
04-14-2000, 11:27 AM
change the raster pos coordinates.

0,0 is the middle of your screen and the corner are the size of frustum boundaries(if this a word in english http://www.opengl.org/discussion_boards/ubb/smile.gif)

TheGecko
04-14-2000, 12:10 PM
Oh I have.That's not the problem because after you move the camera around in the 3D world the text sort of "stays" at that 3D point in the world.So when I rotate the camera to the left,the text moves to the right.Get what I mean?

MikeC
04-14-2000, 12:57 PM
Hi Gecko (we meet again...)

Simple enough.

1) Push an orthographic matrix onto the projection matrix stack.

2) Push an identity matrix onto the modelview stack.

3) Draw your text.

4) Pop both matrices.

I wouldn't recommend putting the matrix stuff inside the DrawText function; if you were drawing more than one string you'd get a lot of unnecessary duplication.

Generally speaking, most 2D work in screen space can be done very easily with this combination of ortho projection and identity modelview. Plus, many drivers will detect that you're using an identity matrix and optimize away the transform step.

dmy
04-14-2000, 02:46 PM
i have written a text class wich is very simple to use: it uses textured fonts and it don't need nothing else.

plus, you'll get a general image load/save class.

do you want it?

Dolo/\/\ightY

TheGecko
04-14-2000, 03:12 PM
Thanx for the help MikeC.I'll try that today. And yes dmy,send me your code.I'd also like to take a look at that as well.

Thanx for the help.

Humus
04-15-2000, 12:22 PM
... and if you would have problems with the other solutions given you may also want to try my Enter2d() and Exit2d() functions which simpifies things a little.

TheGecko
04-15-2000, 01:15 PM
Sure! Send that to me as well! The more 2D functions I get the less problems I have with OpenGL! http://www.opengl.org/discussion_boards/ubb/smile.gif

Humus
04-15-2000, 02:44 PM
void Enter2dMode(){
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0,1,1,0,1,-1);
}

void Exit2dMode(){
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}

After calling Enter2d() your upper left corner is at (0,0) and the lower right corner is (1,1). So you can use the glVertex2f( ... ) function to specify 2d coords. After calling Exit2d() the previous state is set back again.


[This message has been edited by Humus (edited 04-15-2000).]

TheGecko
04-15-2000, 05:05 PM
Kewl! That just about takes care of my fake 2D problems.

Check out my OpenGl terrain engine at:
www.fortunecity.com/skyscraper/epson/1181 (http://www.fortunecity.com/skyscraper/epson/1181)

Tell me what you think.

Thanx again for the help guys.