Hello, I’m having a problem rendering text in a particular program. I can render text with the exact same method in another program but for some specific reason the text is not rendering for the one I need it to. I’m curious if it has something to do with the gluOrtho2D() function in my displayInfo() function?
I think I have all the relevant functions, please let me know if you need more information.
Thanks in advance!
Display Function:
void display()
{
drawScene();
void *font = GLUT_BITMAP_TIMES_ROMAN_10;
float color[4] = {0, 1, 1, 0};
char * str = "Hello1";
displayInfo(str, 100, 200, color, font);
glutSwapBuffers();
}
Draw Scene:
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, cameraDistance);
glBindTexture(GL_TEXTURE_2D, gl_Tex[0]);
//Draw Surface
glTranslatef(0.0, 0.0, -4.0);
glRotatef(cameraAngleX, 1, 0, 0); // pitch
glRotatef(cameraAngleY, 0, 1, 0); // heading
{
// copy pixels from PBO to texture object. Use offset instead of ponter.
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, imageW, imageH, GL_RGBA, GL_UNSIGNED_BYTE, BUFFER_DATA(0) );
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
}
glBindTexture(GL_TEXTURE_2D, gl_Tex[1]);
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, imageW, imageH2, GL_RGBA, GL_UNSIGNED_BYTE, BUFFER_DATA(1) );
//Draw Color Bar
{
glTranslatef(1.1, 0.0, 0.0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.05f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.05f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.05f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.05f, 1.0f, 0.0f);
glEnd();
}
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
Text Rendering Functions:
void displayInfo(char *str,int x, int y, float color[4], void *font)
{
// backup current model-view matrix
glPushMatrix(); // save current modelview matrix
glLoadIdentity(); // reset modelview matrix
// set to 2D orthogonal projection
glMatrixMode(GL_PROJECTION); // switch to projection matrix
glPushMatrix(); // save current projection matrix
glLoadIdentity();
gluOrtho2D( 100, 500, 200, 500);
drawString(str, x, y, color, font);
glPopMatrix(); // restore to previous projection matrix
// restore modelview matrix
glMatrixMode(GL_MODELVIEW); // switch to modelview matrix
glPopMatrix();
}
void drawString(const char *str, int x, int y, float color[4], void *font)
{
glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT); // lighting and color mask
glDisable(GL_LIGHTING); // need to disable lighting for proper text color
glDisable(GL_TEXTURE_2D);
glColor4fv(color); // set text color
glRasterPos2i(x, y); // place text position
// loop all characters in the string
while(*str)
{
glutBitmapCharacter(font, *str);
++str;
}
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glPopAttrib();
}
Initialization Code:
glutInit(&argc, argv);
printf("Initializing GLUT...
");
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(wWidth, wHeight);//imageW, imageH);
glutInitWindowPosition(100, 100);
glutCreateWindow("HeatGL");
printf("OpenGL window created.
");
glutIdleFunc(display);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouseCB);
glutMotionFunc(mouseMotionCB);
glewInit();
glViewport( 0, wWidth, 0, wHeight );
glMatrixMode( GL_PROJECTION );
gluPerspective( 45, (float)wWidth/wHeight, .1, 100 );
glMatrixMode( GL_MODELVIEW );
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_FLAT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);//glDepthFunc(GL_ALWAYS);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // 4-byte pixel alignment
// glHint(GL_CLIP_VOLUME_CLIPPING_HINT_EXT,GL_FASTEST);
glDisable(GL_LIGHTING);
//glEnable(GL_CULL_FACE); //Eliminates Rendering behind objects
//glClearStencil(0);
//track material ambient and diffuse from surface color, call it before glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
setCamera(0, 0, 3, 0, 0, 0);