I am using this code to draw a text in openGL with GLUT:
Code (init part) which sets my origin (0,0) into top-left corner and maintains proper screen-resolution based coordination system :
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, x, y, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
Code (printing part) :
void drawtext(GLfloat x, GLfloat y, const char* string )
{
glRasterPos2f(x,y);
while ( *string != '\0' )
glutBitmapCharacter(p_glutfont,static_cast<int>(*string++));
}
And everything would be perfect if not only the fact that when X and Y starts at negative coordinations (due to the fact that objects with text printed as a tag are partially outside of the view, then the whole text is not being drawn. lets imagine that I have a object with text tag on top of it and it enters the view from left top corner at x = -40, y = 40 :
X axis window
axis Y ----------------------------
| y=40 |
x=-40__|_________ |
|Text to print| |
|_____________| |
| |
| |
|____________________________|
The text is not being printed until is drawn from exactly X => 0 coordination. Also this problem doesn't exist when text has to be clipped on RIGHT side - then it is printed properly - this is where I I realized that it has to do with initial position of the text.
My question is - how can I EXTEND properly my viewport / screen mapping to :
1) preserve screen coordinations from top-left corner (0,0) to bottom-right (height,width) 2) with internally extended those values just during the printing the text - so the text would get clipped instead of disappearing ?
Although I did my research, I couldn't for 2 days now come to any solution. I tried to play with glViewport and glOrtho but none of changes and approaches I tried (like for example setting glViewport to -x,-y,x*2,y*2) would do the trick - those always mess with my screen coordination system which is essential to keep at window-resolution based.
PS: I did further research and I came across this article http://www.opengl.org/archives/resources/faq/technical/clipping.htm.
10.070 How do I draw glBitmap() or glDrawPixels() primitives that have an initial glRasterPos() outside the window's left or bottom edge?
When the raster position is set outside the window, it's often outside the view volume and subsequently marked as invalid. Rendering the glBitmap and glDrawPixels primitives won't occur with an invalid raster position. Because glBitmap/glDrawPixels produce pixels up and to the right of the raster position, it appears impossible to render this type of primitive clipped by the left and/or bottom edges of the window.
However, here's an often-used trick: Set the raster position to a valid value inside the view volume. Then make the following call:
glBitmap (0, 0, 0, 0, xMove, yMove, NULL);
This tells OpenGL to render a no-op bitmap, but move the current raster position by (xMove,yMove). Your application will supply (xMove,yMove) values that place the raster position outside the view volume. Follow this call with the glBitmap() or glDrawPixels() to do the rendering you desire.
Sadly - I can't put this advice in work - and once I use it - the text is completely missing.
this is where I I realized that it has to do with initial position of the text.
Almost. glRasterPos is a raster drawing operation command and whatever position you put into it goes through the transformation pipeline. If the point falls outside the clip volume (not the viewport!) it gets discarded and all further raster drawing operations are disabled until a new glRasterPos specified that is not clipped.
The easy and recommended solution would be not to use raster drawing operations at all, because they're slow, poorly supported and have been removed from later OpenGL alltogether. glutBitmapCharacter is using raster drawing ops.
Use texture mapped fonts or stroked characters instead. Google finds uncountable amounts of information on these.