I have a glutStrokeCharacter glitch problem, but the code does not work?

I am writing opengl code on ubuntu. I want to draw text on the screen, but the output () function does not seem to 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; } 
+4
source share
1 answer

First try copying the text down 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 will find that GLUT_STROKE_ROMAN is large by default, about 152 units.

If I remember correctly the default projection matrices and model matrices, which you depending on giving you a viewport that covers only (-1, -1) - (1,1).

+4
source

All Articles