Textured Text & Billboard text
by Peter Simard · 02/23/2006 (10:51 am) · 25 comments
Download Code File
This resource is based on the textured font resource. You can create the font images you need using www.lmnopc.com/bitmapfontbuilder/. Simply add the 2 attached files to your project and compile. Below is sample code you can place at the end of your player.cc's RenderImage method to have true names above players heads.
Examples:
Name from far away
Name close up
Name partially hidden
player.cc
player.h
This resource is based on the textured font resource. You can create the font images you need using www.lmnopc.com/bitmapfontbuilder/. Simply add the 2 attached files to your project and compile. Below is sample code you can place at the end of your player.cc's RenderImage method to have true names above players heads.
Examples:
Name from far away
Name close up
Name partially hidden// Render Name
if(!nameBitmap && getShapeName() != NULL)
{
generateNameBitmap();
}
if(nameBitmap)
{
MatrixF ModelView;
Point4F Position;
const Point4F XRotation(1,0,0,0);
const Point4F YRotation(0,1,0,0);
const Point4F ZRotation(0,0,1,0);
F32 LeftTexPos;
F32 RightTexPos;
glEnable ( GL_TEXTURE_2D );
glBindTexture ( GL_TEXTURE_2D, nameTexture.getGLName() );
glEnable ( GL_BLEND );
glBlendFunc ( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
glTexEnvi ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
ColorF nameColor;
nameColor.set(0, 1, 0, 0.2f);
glColor4f(nameColor.red, nameColor.green, nameColor.blue, nameColor.alpha);
glEnable ( GL_ALPHA_TEST );
glEnable ( GL_CULL_FACE );
glAlphaFunc ( GL_GREATER, 0.1f );
glPushMatrix();
// Perform Spherical Billboarding.
MatrixF posMatrix = getRenderTransform();
Point3F pos = posMatrix.getPosition();
pos.z += 2.5;
posMatrix.setPosition(pos);
dglMultMatrix(&posMatrix);
dglGetModelview(&ModelView);
ModelView.setColumn(0, XRotation);
ModelView.setColumn(1, YRotation);
ModelView.setColumn(2, ZRotation);
dglLoadMatrix(&ModelView);
// Change the number it divides by to change the font size
F32 Width = nameTexture.getWidth() / 140.0f;
F32 Height = nameTexture.getHeight() / 70.0f;
LeftTexPos = 0.0f;
RightTexPos = 1.0f - LeftTexPos;
// Draw Billboard.
glBegin(GL_QUADS);
// Draw Top part of billboard.
glTexCoord2f (LeftTexPos,0);
glVertex3f (-Width,0,Height);
glTexCoord2f (RightTexPos,0);
glVertex3f (+Width,0,Height);
glTexCoord2f (RightTexPos,1);
glVertex3f (+Width,0,0);
glTexCoord2f (LeftTexPos,1);
glVertex3f (-Width,0,0);
glEnd();
// Restore our Modelview.
glPopMatrix();
glTexEnvi ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
glDisable ( GL_CULL_FACE );
glDisable ( GL_ALPHA_TEST );
glDisable ( GL_BLEND );
glDisable ( GL_TEXTURE_2D );
}player.cc
void Player::generateNameBitmap()
{
nameBitmap = new BitmapText();
nameBitmap->setText(getShapeName());
nameTexture = TextureHandle(NULL, nameBitmap, BitmapKeepTexture);
}
Player::Player()
{
nameBitmap = 0;
nameTexture = 0;
....
}player.h
#ifndef _GBITMAPTEXT_H_
#include "dgl/bitmapText.h"
#endif
BitmapText* nameBitmap;
TextureHandle nameTexture;
void generateNameBitmap();
#22
I have come across another issue with the line:
which gives me an error in SceneRenderImage... which I assume doesn't like nameBitmap being deleted while it's using it to render or something.
It does beg the question though - is this needed? If your TextureHandle code updates the texture OK, surely it is OK to proceed with the same BitmapText object that holds it, assuming you are updating the graphics within it?
Gords
10/10/2006 (6:59 am)
Ron, thanks for the code, it seems to compile great, although I don't have any mem leak check tools at the moment :)I have come across another issue with the line:
if(nameBitmap) {
delete nameBitmap;
}which gives me an error in SceneRenderImage... which I assume doesn't like nameBitmap being deleted while it's using it to render or something.
It does beg the question though - is this needed? If your TextureHandle code updates the texture OK, surely it is OK to proceed with the same BitmapText object that holds it, assuming you are updating the graphics within it?
Gords
#23
Also, how would I use this to show a 'damage amount' pop up over the head of a "$selected" target when they receive damage?
09/20/2007 (9:58 am)
Does this code slow down the engine at all? I mean, how is it as far as resource consumption?Also, how would I use this to show a 'damage amount' pop up over the head of a "$selected" target when they receive damage?
#24
10/16/2007 (6:30 am)
Is the text position static, or can it be manipulated? I'd like to have damage numbers show over the head of the player who received the damage and it's possible to take damage from multiple sources at once, so they should be sort of animated starting right above their head and float upwards on the Z axis for about .1 meters or so (this will allow the player to see the multiple numbers as each one will float upward and out of the way of the subsequent damage numbers showing). Of course we can limit the number showing at once or something idk..
#25
03/27/2009 (2:54 am)
Has this been ported to TGEA? 
Torque Owner Ron Hasson
I managed to solve it by passing in the same texture name (1st parameter) in the call to
TextureHandle(textureName, nameBitmap, BitmapKeepTexture).
Doing this will allow the engine to delete the previous bitmap in TextureManager::registerTexture (dgl/gTexManager.cc)
And yes, you can now do nameBitmap->setText(mBitmapText) repeatedly without crashing.