getting rendered text to stay in front of camera

ghettotek
Unregistered
 
Post: #1
got my code to draw text using the tutorial from NeHe, but i still have one problem. i cant get the text to stay in front of the "camera" (viewport?), no matter what i do (drawing the text before the "camera" is updated, various other things) i cant get it to stay stationary. basically what i want to do is use the text as a display of some sort, but when i rotate the "camera", the text moves in the opposite direction as the objects in the scene do.

heres my code for drawing text/rendering scene:

-(void)gameCycle
{
int i;

glMatrixMode(GL_MODELVIEW);

//text drawn here
glDisable(GL_LIGHTING);
glDepthFunc(GL_NOTEQUAL);
glColor3f(1.0,1.0,1.0);
glRasterPos2f(10,10);
[self glPrint:@"Running..."];
glDepthFunc( GL_LESS );
glEnable(GL_LIGHTING);

//update camera (glLoadIdentity(), glRotate(-direction), glTranslate(-position))
//update lights
//update/draw objects

}

thanks.
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #2
Check the NeHe tutorials for billboarding.

Time is running out!
Quote this message in a reply
ghettotek
Unregistered
 
Post: #3
couldnt find anything from a quick glance at the site. isnt billboarding the technique used to keep polygons facing the camera? i want to get polygons (text) to stay in place at whatever position they are drawn on the screen (meaning, i draw "Hello" at the top-left of the screen, and "Hello" stays at the top-left of the screen no matter what i do).
Quote this message in a reply
Member
Posts: 49
Joined: 2002.05
Post: #4
If you are using geometry to draw your text you may need to surround your text drawing like this:

glMatrixMode( GL_MODELVIEW );

glPushMatrix();

glLoadIdentity();

// .... draw text here

glPopMatrix();
Quote this message in a reply
ghettotek
Unregistered
 
Post: #5
ive tried all that. whenever i use glLoadIdentity(), it just disappears (the same for other objects in my scene, i think its because of my camera class). it seems that it should work if i draw the text before i do any translating/rotating with my camera class, but it doesnt.
Quote this message in a reply
Member
Posts: 49
Joined: 2002.05
Post: #6
If that doesn't work then I'd probably have to see the way you draw your text, because if you reset the model/view matrix with glLoadIdentity() it will discard anything that your camera class may have done to the model/view matrix.

Also note that the camera points down the negative z axis which means that to see your text after glLoadIdentity() you'll want to glTranslatef(0.0f, 0.0f, -1.0f) (or some other negative z-value that is far enough away from your near clipping plane).

Maybe someone else has some ideas...

-- Ian
Quote this message in a reply
ghettotek
Unregistered
 
Post: #7

ok heres what im doing.

first, here's my GL init code:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0, 640.0/480.0, 0.1, 5000.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glCullFace(GL_BACK);
glEnableClientState(GL_VERTEX_ARRAY);
glClearColor(0.0, 0.0, 0.0, 0.5);
glClearDepth(1.0);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);


the text im drawing is from display lists generated using the code from the MacOSX port of the tutorial on nehe.gamedev.net. i dont think this is relevant (or is it?)

now, this is the actual way the text is drawn:


glMatrixMode(GL_MODELVIEW);
glRasterPos2f(x,y);
glColor3f(1.0,1.0,1.0);
[self glPrint:@"THIS TEXT SHOULD STAY STILL!"];
//camera update
//objects/lights/etc update


and the glPrint() function


- (void) glPrint:(NSString *)fmt, ...
{
NSString *text;
va_list ap; // Pointer To List Of Arguments
unichar *uniBuffer;

if( fmt == nil || [ fmt length ] == 0 ) // If There's No Text
return; // Do Nothing

va_start( ap, fmt ); // Parses The String For Variables
text = [ [ [ NSString alloc ] initWithFormat:fmt arguments:ap ] autorelease ];
va_end( ap ); // Results Are Stored In Text

glPushAttrib( GL_LIST_BIT ); // Pushes The Display List Bits
glListBase( base - 32 ); // Sets The Base Character to 32
uniBuffer = calloc( [ text length ], sizeof( unichar ) );
[ text getCharacters:uniBuffer ];
// Draws The Display List Text
glCallLists( [ text length ], GL_UNSIGNED_SHORT, uniBuffer );
free( uniBuffer );
glPopAttrib(); // Pops The Display List Bits
}


thanks.
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #8
Apparently the raster position is actually set in 3D space, so it's being rotated along with the camera.

If you switch the projection matrix to an orthographic projection and clear the modelview matrix, 2D operations should do what you expect.
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #9
Those zNear and zFar values look like they may give you problems. Are you really going to have objects that move/scale from 0.1 to 5000.0?

This is supposed to be h.u.d type text - like showing the score above the action taking place - right? Before you draw text - turn off depth testing - you want the text drawn in front of everything else anyway.

Without seeing the rest of the drawRect: I dont know what to tell you.

but since you are using cocoa and using texture fonts allow me to plug my free utility Font Texture Maker <http://www.idevgames.com/content/download.php?id=397>.

-Codemattic
Quote this message in a reply
ghettotek
Unregistered
 
Post: #10
>> If you switch the projection matrix to an orthographic projection and clear the modelview matrix, 2D operations should do what you expect.

how do i go about doing that?
Quote this message in a reply
ghettotek
Unregistered
 
Post: #11
codemattic: does the zNear and zFar values have anything to do with keeping text synced with camera movements?

no, its not like showing the score above the action taking place. i can do that fine with glRasterPos3f(). what i want is something like "Player Health" to stay on the top-right of the view at all times.
Quote this message in a reply
Member
Posts: 49
Joined: 2002.05
Post: #12
Quote:>> If you switch the projection matrix to an orthographic projection and clear the modelview matrix, 2D operations should do what you expect.

how do i go about doing that?

[SOURCECODE]glMatrixMode( GL_PROJECTION );

glPushMatrix();

glLoadIdentity();

glOrtho( left, right, bottom, top, near, far );

glMatrixMode( GL_MODELVIEW );

glPushMatrix();

glLoadIdentity();

//.... draw text here

glPopMatrix();

glMatrixMode( GL_PROJECTION );

glPopMatrix();[/SOURCECODE]

I think that'll do what you want. Someone can correct me if I goofed.

-- Ian

P.S. I'm not sure about this one, so maybe someone can clear this up. gluPerspective(50.0, 640.0/480.0, 0.1, 5000.0);, Isn't it better to type 0.1f? Or does it make any difference?
Quote this message in a reply
Member
Posts: 49
Joined: 2002.05
Post: #13
Quote:Originally posted by ghettotek
codemattic: does the zNear and zFar values have anything to do with keeping text synced with camera movements?


No but if you enter the wrong values your text may get clipped (not displayed).

Quote:
no, its not like showing the score above the action taking place. i can do that fine with glRasterPos3f(). what i want is something like "Player Health" to stay on the top-right of the view at all times.

Something like "Player Health" will still be drawn over the action won't it? In that case codemattic is right, you'll want to glDisable( GL_DEPTH_TEST ); before you draw your text. Then you'll want to enable it again when you're done.

-- Ian
Quote this message in a reply
ghettotek
Unregistered
 
Post: #14
ok i read his statement wrong. but still, disabling depth testing is irrelevant. i already got the text to display above everything else by changing the depth func. ill try your code Ian.
Quote this message in a reply
ghettotek
Unregistered
 
Post: #15
Ian that code works, but now the text is the only thing visible. ill try playing around with it. thanks.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  volume rendered explosions? Falcor 2 4,162 Feb 16, 2006 08:17 PM
Last Post: Falcor