Sign in to follow this  
Miso

label points in space

Recommended Posts

Hello. Suppose you draw a point at [x,y,z] in space and would like to add a label to it, for example 'A'. The scene is rendered in perspective projection. Is there a way to achieve it without using Feedback ?? I have the label built as a bitmap font using wglUseFontBitmaps(...). The label should appear near the plotted point on the screen. Thanks in advance.

Share this post


Link to post
Share on other sites
Hi,
Briefly put, you could project the coordinates (using gluProject or by applying projection, modelview and viewport transformations manually) onto the screen to find out the screen coordinates. Then just render the string at that position (with a small offset to the right, I guess.)

(I can post a more detailed answer later, if you need one :))

Share this post


Link to post
Share on other sites
In my programs there doesn't seem to be anything special required to display the text at some specific location in 3D space - except maybe some billboarding. In my render routine I have three sections, 3D Perspective with lighting, materials, textures etc, then next the 3D perspective section without lighting, textures, etc, and then finally a 2D ortho section for things like overlay menus's, fps counters etc. In my second and third sections I do this to display my mesh coordinates in 3D and a menu is 2D, and these are fine when I move around in 3D space - although there is no billboarding so you have to look on directly to see the text properly :


Switch3D();
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);

// output grid references
glPushMatrix();
// stats
gridFont->SetRGB(0.0f,1.0f,0.0f);
for (int z=0; z<size; z++) { // X axis
gridFont->SetPos3D(CPoint[z][0].x-0.5f,CPoint[z][0].y,CPoint[z][0].z);
gridFont->Print("%d", z);
}
gridFont->SetRGB(0.0f,0.0f,1.0f);
for (int x=0; x<size; x++) { // X axis
gridFont->SetPos3D(CPoint[0][x].x,CPoint[0][x].y,CPoint[0][x].z-0.5f);
gridFont->Print("%d", x);
}
glPopMatrix();

.
.
.

// output statistics in orthographic mode
Switch2D();
glPushMatrix();
// stats
statsFont->SetRGB(1.0f,1.0f,1.0f);
statsFont->SetPos2D(0,24);
statsFont->Print("FPS : %3.2f", g_timer.GetCurrFPS());
statsFont->SetPos2D(0,54);
statsFont->Print("Time: %3.2f", g_timer.GetTotalSeconds());
glPopMatrix();



In my 2D ortho section I use another instance of the font below for displaying the FPS counter. All works fine together in my limited use scenario.

This might be of help... I use this class for fonts (can't remember where I got it from otherwise I would attribute the appropriate amount of credit). It makes displaying text in either 2D or 3D a breeze :

//******************************************************************************//
// Font class //
//******************************************************************************//
#ifndef FONT_H
#define FONT_H

//******************************************************************************//

class CFont
{
private:
unsigned int texID; // font texture id
unsigned int callList; // font display list
float r, g, b, a; // RGBA
int screenX, screenY; // screen coordinates
float xpos, ypos, zpos; // 3d coordinates
bool display2D; // flag for controlling display call

void LoadTexture(); // loads the TGA font texture
void CreateCallLists(); // creates the font display list

public:
CFont();
CFont(char *name, int size);
~CFont();

void Build(char *name, int size);
void ClearFont();

void Print(const char *str, ...);

void SetPos2D(int x, int y) { screenX = x; screenY = y; display2D=true;}
void SetPos3D(float x, float y, float z) { xpos = x; ypos = y; zpos = z; display2D=false;}

void SetRGB(float red, float green, float blue) { r = red; g = green; b = blue; a = 1.0; }
void SetRGBA(float red, float green, float blue, float alpha) { r = red; g = green; b = blue; a = alpha; }
};

//******************************************************************************//
// Implementation //
//******************************************************************************//

CFont::CFont()
{
screenX = 0;
screenY = 0;
xpos = 0.0;
ypos = 0.0;
zpos = 0.0;
display2D=true;
}

CFont::CFont(char *name, int size)
{
screenX = 0;
screenY = 0;
xpos = 0.0;
ypos = 0.0;
zpos = 0.0;
display2D=true;
Build(name, size);
}

CFont::~CFont()
{
glDeleteLists(callList, 96);
}

void CFont::Build(char *name, int size)
{
HFONT hFont; // font ID
HDC hDC; // device context

hDC = wglGetCurrentDC();
callList = glGenLists(96);

if (stricmp(name, "symbol") == 0)
{
hFont = CreateFont(-size, 0,0,0,FW_BOLD, FALSE, FALSE, FALSE, SYMBOL_CHARSET,
OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
FF_DONTCARE | DEFAULT_PITCH, name);
}
else
{
hFont = 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);
}

SelectObject(hDC, hFont);
wglUseFontBitmaps(hDC, 32, 96, callList);

}

void CFont::Print(const char *str, ...)
{
char text[256];
va_list args;

if (str == NULL)
return;

va_start(args, str);
vsprintf(text, str, args);
va_end(args);


glColor4f(r, g, b, a);
glPushMatrix();
//if (xpos == 0.0 && ypos == 0.0 && zpos == 0.0)
if (display2D==true) {
// Log << "Call to 2i\n";
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -1.0f); // translate one unit into the screen
glRasterPos2i(screenX, screenY);
} else {
// Log << "Call to 3f\n";
glRasterPos3f(xpos,ypos,zpos);
}
glPushAttrib(GL_LIST_BIT);
glListBase(callList - 32);
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
glPopAttrib();
glPopMatrix();
}

void CFont::ClearFont()
{
glDeleteLists(callList, 96);
}


#endif // FONT_H

//******************************************************************************//
// End of file








Setup your font like this:


// define fonts
CFont *gridFont, *statsFont;

// setup font for statistics display
statsFont = new CFont("Times New Roman", 24);
gridFont = new CFont("Courier",8);



hth
F451

Share this post


Link to post
Share on other sites
Thank you both for your replies.

TomasH, I tried your solution with gluProject(...) and it seems to work just fine for my application.
Once again, thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this