3

I'm trying to draw a simple string (overlay) on the screen.

From what I've found over the internet, I'm using it this way:

void write(string text, int x, int y){
    glRasterPos2i(x,y);

    for(int i = 0; i < text.length(); i++){ 
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text.data()[i]);
    }
}

But it draws the string according to the world coordinates. Say, if x and y are set to 10, they are drawn at (10,10,0) coordinates in the world. But I simple need this string at window's (10,10) coordinates in 2D.

This is part of a small project, and the draw method is given as below. I don't want to change it much as it may break something else in the project.

void disp(){
 // set viewing translation and object rotations
 glMatrixMode( GL_MODELVIEW );
 glLoadIdentity ();
 glTranslatef( INIT_VIEW_X, INIT_VIEW_Y, INIT_VIEW_Z );
 glRotatef( xRot, 1.0, 0.0, 0.0 );
 glRotatef( zRot, 0.0, 0.0, 1.0 );
 glScalef( scaleFactor, scaleFactor, scaleFactor );
 glClear(GL_COLOR_BUFFER_BIT);
 draw();
 glFlush();
}

I also don't exactly know what they do, and I think drawing the text to world coordinates have to do with something in this code. I've also tried Using GLUT bitmap fonts but it doesn't work either.

How can I simple draw onto the screen. OpenGL is over complicating things; I try to simply write to the window, but it takes the whole thing and translates into 3D world. I just don't want this.

Community
  • 1
  • 1
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389

3 Answers3

7

From twall, yes you need to clear BOTH the modelview and projection matrices

//TEXT
glMatrixMode( GL_PROJECTION ) ;
glPushMatrix() ; // save
glLoadIdentity();// and clear
glMatrixMode( GL_MODELVIEW ) ;
glPushMatrix() ;
glLoadIdentity() ;

glDisable( GL_DEPTH_TEST ) ; // also disable the depth test so renders on top

glRasterPos2f( 0,0 ) ; // center of screen. (-1,0) is center left.
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
char buf[300];
sprintf( buf, "Oh hello" ) ;
const char * p = buf ;
do glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, *p ); while( *(++p) ) ;

glEnable( GL_DEPTH_TEST ) ; // Turn depth testing back on

glMatrixMode( GL_PROJECTION ) ;
glPopMatrix() ; // revert back to the matrix I had before.
glMatrixMode( GL_MODELVIEW ) ;
glPopMatrix() ;
bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • Thanks this is what I was looking for, I also disable lighting so it's rendered as the desired colour. `glDisable(GL_LIGHTING);//Ignore lighting, its text` – Robadob Mar 03 '14 at 13:09
0

you can step back to "2D" coordinates like so:

//save your current matrix on the stack, so you don't lose it
glPushMatrix();
//load identity matrix, so you don't have any 3d transformations
glLoadIdentity ();
//now you also can transform the text
//to the position just as you like
//glTransformf( ... )
//make sure the text is draw, even though it might be outside the viewing area
glDisable( GL_DEPTH_TEST )
drawMyFunkyText();
glEnable( GL_DEPTH_TEST )
//restore the matrix as it was before,
//so you can draw all your shapes as before
glPopMatrix();
devsnd
  • 7,382
  • 3
  • 42
  • 50
  • 1
    This will only reset whatever matrix mode is currently active. If OP used a perspective projectiom, the raster position would come out before the near clipping plane. – datenwolf Feb 24 '12 at 12:53
  • Thanks for the input, I've added `glDisable(GL_DEPTH_TEST)` to the code sample, to make sure the text is rendered anyway – devsnd Feb 24 '12 at 13:12
  • 1
    Disabling depth test won't help, the raster position will be clipped by the near clipping plane. And for bitmaps (which is used by glutBitmapChar) only the clipping of the raster position matters. If that one point gets clipped, it affects the whole thing. – datenwolf Feb 24 '12 at 13:17
0

From what I can gather, it seems what you're after is orthographic projection.

I'm not sure about OpenGL specifically (I'm used to higher-level graphics libraries), but here's a couple of links that could be a starting point:

Setting a orthographic projection matrix?

http://www.cprogramming.com/tutorial/opengl_projections.html

Community
  • 1
  • 1
Alex Z
  • 2,500
  • 2
  • 19
  • 23