我有一个使用GLUT(不是freeglut)的OpenGL项目,其中我想在视口的固定位置显示2D文本.其余的物体都在3D世界坐标中.
This answer到一个相关的老问题说,
the bitmap fonts which ship with GLUT are simple 2D fonts and are not suitable for display inside your 3D environment. However,they’re perfect for text that needs to be overlayed on the display window.
我已经尝试了接受的答案中概述的方法,但它没有给我所需的输出.以下是显示功能的代码片段:
void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0,0.0,-(dis+ddis)); //Translate the camera glRotated(elev+delev,1.0,0.0); //Rotate the camera glRotated(azim+dazim,0.0); //Rotate the camera . . . draw 3D scene . . . glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0.0,win_width,win_height); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glRasterPos2i(10,10); string s = "Some text"; void * font = GLUT_BITMAP_9_BY_15; for (string::iterator i = s.begin(); i != s.end(); ++i) { char c = *i; glColor3d(1.0,0.0); glutBitmapCharacter(font,c); } glMatrixMode(GL_MODELVIEW); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glFlush(); glSwapBuffers(); }
似乎已经提出了我想要的similar question,但作者没有接受答案.
一般来说,答案似乎建议使用其他库(主要是特定于操作系统)来实现叠加.然而,没有明确的迹象表明这是否可以单独用GLUT实现.可以吗?
解决方法
正如我所说,你发布的代码完全正常.这是我用来测试它的代码:
#ifdef __APPLE__ #include <GLUT/glut.h> #else #include <GL/glut.h> #endif #include "GL/glu.h" //glm not required //#include <glm.hpp> //#include <gtc/matrix_transform.hpp> #include <string> int win_width = 500,win_height = 500; void renderScene(void) { static float dis=0,ddis=0,elev=0,delev=0,azim=0,dazim=0; azim += 0.5f; if (azim >= 360.0f){ azim -= 360.0f; } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f,0.0f,-(dis + ddis)); glRotated(elev + delev,0.0); glRotated(azim + dazim,0.0); glScalef(2.0f,2.0f,2.0f); glBegin(GL_TRIANGLES); glColor3f(1.0f,1.0f,0.0f); glVertex3f(-0.5f,-0.5f,0.0f); glColor3f(0.0f,0.0f); glVertex3f(0.5f,1.0f); glVertex3f(0.0f,0.5f,0.0f); glEnd(); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); //I like to use glm because glu is deprecated //glm::mat4 orth= glm::ortho(0.0f,(float)win_width,(float)win_height); //glMultMatrixf(&(orth[0][0])); gluOrtho2D(0.0,win_height); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glColor3f(1.0f,0.0f);//needs to be called before RasterPos glRasterPos2i(10,10); std::string s = "Some text"; void * font = GLUT_BITMAP_9_BY_15; for (std::string::iterator i = s.begin(); i != s.end(); ++i) { char c = *i; //this does nothing,color is fixed for Bitmaps when calling glRasterPos //glColor3f(1.0,1.0); glutBitmapCharacter(font,c); } glMatrixMode(GL_MODELVIEW); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glEnable(GL_TEXTURE_2D); glutSwapBuffers(); glutPostRedisplay(); } int main(int argc,char **argv) { // init GLUT and create Window glutInit(&argc,argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(win_width,win_height); glutCreateWindow("GLUT Test"); // register callbacks glutDisplayFunc(renderScene); // enter GLUT event processing cycle glutMainLoop(); return 1; }
关于弃用固定功能管道和其他管道的通常免责声明适用.