我在ubuntu上写了一个opengl代码。我想在屏幕上绘制文本,但output()函数似乎不起作用。你能告诉我为什么吗?
http://pyopengl.sourceforge.net/documentation/manual/glutStrokeCharacter.3GLUT.html
void output(GLfloat x, GLfloat y, char *text)
{
char *p;
glPushMatrix();
glTranslatef(x, y, 0);
for (p = text; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glPopMatrix();
}
void myDraw(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,1.0f,0.0f);
glBegin(GL_POLYGON);
glVertex2f(-0.8,0.8);
glVertex2f(-0.2,0.8);
glVertex2f(-0.2,0.5);
glVertex2f(-0.8,0.5);
glEnd();
glFlush();
glColor3f(1.0f,0.0f,0.0f);
output(-0.8,0.8,"hello"); // why not show text
glFlush();
}
int main(int argc,char ** argv)
{
printf("init\n");
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(SCALE,SCALE);
glutInitWindowPosition(100,100);
glutCreateWindow("MENU");
glutDisplayFunc(myDraw);
glutMouseFunc(processMouse);
glutMainLoop();
return 0;
}
答案 0 :(得分:4)
首先尝试缩小文本:
#include <GL/glut.h>
double aspect_ratio = 0;
void reshape(int w, int h)
{
aspect_ratio = (double)w / (double)h;
glViewport(0, 0, w, h);
}
void output(GLfloat x, GLfloat y, char* text)
{
glPushMatrix();
glTranslatef(x, y, 0);
glScalef(1/152.38, 1/152.38, 1/152.38);
for( char* p = text; *p; p++)
{
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
}
glPopMatrix();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10*aspect_ratio, 10*aspect_ratio, -10, 10, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3ub(255,0,0);
output(0,0,"hello");
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutCreateWindow("Text");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
如果您阅读了自己提供的文档,则会发现默认情况下GLUT_STROKE_ROMAN
相当大,大约152个单位。
如果我没记错的话,你依赖的默认投影和模型视图矩阵会给你一个只覆盖(-1,-1)到(1,1)的视图区域。