glutBitmapCharacter位置文本错误 [英] glutBitmapCharacter positions text wrong
问题描述
我正在尝试在屏幕上绘制一个简单的字符串(覆盖).
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]);
}
}
但是它根据世界坐标绘制字符串.假设x和y设置为10,则会在世界上的(10,10,0)坐标处绘制它们.但是我只需要在2D窗口的(10,10)坐标处使用此字符串.
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();
}
我也不完全知道它们的作用,我认为将文本绘制为世界坐标与该代码中的某些内容有关.我还尝试了使用GLUT位图字体,但是它也不起作用.
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.
如何简单地在屏幕上绘制. OpenGL使事情变得复杂了.我尝试简单地写到窗口中,但是它会占用整个内容并转换为3D世界.我只是不想这样.
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.
推荐答案
从twall,是的,您需要同时清除模型视图和投影矩阵
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() ;
这篇关于glutBitmapCharacter位置文本错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!