Reputation: 1352
I have my window cordinate, and I need to draw text here.
I use this function :
void displayText(std::string const& text, float x, float y)
{
int j = text.size();
glColor3f(1, 1, 1);
// std::cout << x << " " << y << std::endl;
// std::cout << (x / 900) * (2.0 - 1.0) <<" " <<(y / 900) * (2.0 - 1.0) << std::endl;
x = x / (float)900; //900 is my window size
y = y / (float)900;
x-=0.5f;
y-=0.5f;// std::cout << x << " "<<y << std::endl;
glRasterPos2f(x, y);
for(int i = 0; i < j; i++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, text[i]);
}
but it draws text in the wrong coordinate, upside down, have any idea ?
Upvotes: 1
Views: 1092
Reputation: 45332
glRasterPos2f
will go through the full transformation pipeline, so it is in particulary affected by:
This means that your question title is misleading, because neither window space nor NDC coordinates are relevant here, but object space coordinates.
For drawing bitmap-based text, it is best to set up some pixel-exact ortho matrix (matching the size of your viewport) as projection while setting modelView to identity:
glMatrixMode(GL_PROJECTION);
glOrtho(0, width, 0, height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRasterPos2f(pixel_coords);
// draw bitmaps here
Note that GL uses mathematic conventions, so the origin will be at the bottom left corner of the screen. YOu can flip that in the ortho matrix, but the Bitmaps will still be drawn so that their local bottom left edge will appear at the raster position.
You should be aware that all of that stuff - drawing bitmaps, the fixed function coordinate transformations, the builtin GL matrix functions and so on - are completely deprecated since almost a decade by now. Modern core profiles of OpenGL do not support that any more, and you will have to use a texture-based approach for font rendering. Using signed distance fields is a quite good option to get good looking fonts onto the screen.
Upvotes: 2