Moving objects around the screen per pixel using glRasterPos ()

I have the following code for rendering text in my application, first I get the coordinates of the mouse in the world, and then use these coordinates to place my text in the world, so it will follow my mouse position:

Edit: Added buildfont () function in the sample code:

GLvoid BuildFont(GLvoid) // Build Our Bitmap Font { HFONT font; // Windows Font ID HFONT oldfont; // Used For Good House Keeping base = glGenLists(96); // Storage For 96 Characters font = CreateFont( -12, // Height Of Font 0, // Width Of Font 0, // Angle Of Escapement 0, // Orientation Angle FW_NORMAL, // Font Weight FALSE, // Italic FALSE, // Underline FALSE, // Strikeout ANSI_CHARSET, // Character Set Identifier OUT_TT_PRECIS, // Output Precision CLIP_DEFAULT_PRECIS, // Clipping Precision ANTIALIASED_QUALITY, // Output Quality FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch "Verdana"); // Font Name (if not found, its using some other font) oldfont = (HFONT)SelectObject(hDC, font); // Selects The Font We Want wglUseFontBitmaps(hDC, 32, 96, base); // Builds 96 Characters Starting At Character 32 SelectObject(hDC, oldfont); // Selects The Font We Want DeleteObject(font); // Delete The Font } GLvoid glPrint(const char *fmt, ...){ char text[256]; va_list ap; if (fmt == NULL) return; va_start(ap, fmt); vsprintf(text, fmt, ap); va_end(ap); glPushAttrib(GL_LIST_BIT); glListBase(base - 32); glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); glPopAttrib(); } ... glPushMatrix(); glColor4f(0,0,0,1); // X-1 wont work because these are the world coordinates: glRasterPos2d(MousePosX-1, MousePosY); glPrint("TEST"); glColor4f(1,1,0,1); glRasterPos2d(MousePosX, MousePosY); glPrint("TEST"); glPopMatrix(); 

But I want to display multiline texts or texts with "borders" (for example, in the previous code I tried) or text with a background (so that I can distinguish them better from the background) So, how do I do this?

I just need to know how I can move it in pixels, so I could accurately change the position on my screen WITHOUT using a 2-dimensional projection view on top of my 3D rendering ... I just want to make it as simple as possible.

I tried to draw a square under the text, but of course it does not work, since it still uses the coordinates of the world ... so when I rotate the camera, the text does not move along the background of the text ... I'm afraid the only solution is to create another one projection over 3D projection ...

+4
source share
1 answer

Here is a small piece of code that I use to render debugging text in a small application:

 void renderText(float x, float y, const char* text) { int viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(viewport[0], viewport[2], viewport[1], viewport[3], -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRasterPos2f(x, viewport[3] - y); const int length = (int)strlen(text); for (int i = 0; i < length; ++i) { glutBitmapCharacter(GLUT_BITMAP_9_BY_15, text[i]); } glMatrixMode( GL_PROJECTION ); glPopMatrix(); glMatrixMode( GL_MODELVIEW ); glPopMatrix(); } 

x and y are the desired coordinates of the row window. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ...) is just a utility function from GLUT that displays a large bitmap symbol measuring 9 x 15 pixels.

+2
source

All Articles