icons and text on screen

This topic is 4761 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

if i have my screen with my models on and they move and do their own thing and so on. can i place text and images on top as if it was a 2d screen or do i have to place in 3d space as with everything else. what i'm trying to do is display stuff such as high score and number of lives left and so forth, what would be the best way to approach this and is there source code out there that has such things, icons ofr lives left maybe and so on. thanks everyone

Share on other sites
There is a simple trick. First do all your drawings (models, background and so on) and in the end, load the idenitity matrix and translate a litte 'into the screen', like in the first tutorials of NeHe.

For example:
void RenderGameScene (){    //here do all your drawing and translations    //now everything is rendered, load identity matrix    glLoadIdentity();    //disable depth testing, as you always want to display your HUD    glDisable(GL_DEPTH_TEST);    //move 'into' the screen    glTranslatef(0.0f,0.0f,-10.0f);  //depends on the size of the things you want to draw    //now draw your images and your text    //...    //re-enable depth testing    glEnable(GL_DEPTH_TEST);    //swap buffers if you use double buffering    //...};

Hope that helps.

Share on other sites
Try using ortho mode.
use something like this:
void CreateOrthoScreen(){	glDisable(GL_DEPTH_TEST);    // Disables Depth Testing	glMatrixMode(GL_PROJECTION); // Select The Projection Matrix	glPushMatrix();	// Store The Projection Matrix	glLoadIdentity();	glOrtho(0,640,0,480,-1,1);	glMatrixMode(GL_MODELVIEW);	glPushMatrix();	glLoadIdentity();}

This creates a screen which has its origin in the bottom left corner while the coordinates of the top right one are (640,480).
Now you can draw whatever you want (as long as it is 2D) and it will stay on top of everything else you have previously drawn.
Once you're done with your HUD you can switch back to "normal" mode:
void DestroyOrthoScreen(){	glMatrixMode(GL_PROJECTION);	glPopMatrix();	glMatrixMode(GL_MODELVIEW);	glPopMatrix();	glEnable(GL_DEPTH_TEST);}

This is how NeHe does it in one of his tutorials, I've just copied the code here. Perhaps some of the function called are useless or wrong. If so, let me know, so that I can improve my code, too.

Share on other sites
cheers for the replies, will probably try suing ortho mode.
is there free source code out there though for games that have high scores etc displayed on the screen, so i have something to refer to
thanks

Share on other sites
Try Nehe's Lesson 21.
If you skip those parts of the code that do not interest you, you may find something useful.

Share on other sites
i realize that i can only draw 2d things when i use the ortho view but are there ways that i can make the text, scores and so on look impressive without it being 3d??

Share on other sites
Sure there are. Use texture mapped fonts for scores etc, looks very impressive. Good textures go a long way in making things look good. Also its very much possible to draw 3d models/3d stuffs in ortho view, its just not perspective correct. Its 1:1 ratio.

Share on other sites
Just consider that most of the games use a 2D HUD, and some manage to look really impressive.

Share on other sites
texture mapped fonts are in lesson 15 of nehe right? unfortunately i'm coding in lwjgl and nobody has been able to convert the lesson into lwjgl. can lesson 17 be useful at all as that has been converted, sorry to drag on about the bloody fonts, i really appreciate the replies though, thanks

Share on other sites

Lazork357 is correct about weaving in and out of the projection matrix from glu perspective to ortho, which is better suited for 2d text and bitmaps, ect.

For a 2d bitmap for a radar or something similar, try:

glMatrixMode( GL_PROJECTION );
glOrtho( 0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f );
glMatrixMode( GL_MODELVIEW );
glEnable( GL_TEXTURE_2D ); //enable your bitmaps texture
glDisable( GL_LIGHTING ); // disable lighting
glDisable( GL_DEPTH_TEST ); // disable depth testing

// White is a great color for material to texture
glColor3f( 1.0, 1.0, 1.0 );

// draw a quad to texture with
glTexCoord2f( 0.0, 0.0 );
glVertex2f( 0.0, 0.0 );
glTexCoord2f( 1.0, 0.0 );
glVertex2f( 0.3, 0.0 );
glTexCoord2f( 1.0, 1.0 );
glVertex2f( 0.3, 0.2 );
glTexCoord2f( 0.0, 1.0 );
glVertex2f( 0.0, 0.2 );
glEnd( );

Then.....destroy the ortho projection similar to lazork357' s approach
and disable glBlend

Also, in your init() function set up for blending :
glEnable(GL_BLEND);

// give blending translucensy to your bitmaps by calling this function
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);