Reputation: 361
I write a opengl code on ubuntu. I want to draw a text on the screen, but the output() function seems not work. Can you tell me why?
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;
}
Upvotes: 4
Views: 6977
Reputation: 52083
Try scaling the text down a bit first:
#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;
}
If you read the documentation you provided you'll find that GLUT_STROKE_ROMAN
is rather large by default, around 152 units tall.
If I recall correctly the default projection and modelview matrices that you're depending on give you a view area that only covers about (-1,-1) to (1,1).
Upvotes: 4