PDA

View Full Version : Text Rendering Problem



shreddhead
01-13-2010, 03:22 PM
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...\n");

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(wWidth, wHeight);//imageW, imageH);
glutInitWindowPosition(100, 100);
glutCreateWindow("HeatGL");

printf("OpenGL window created.\n");

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);

kowal
01-13-2010, 04:23 PM
Hi,
(from your code)

glRasterPos2i(x, y); // place text position
in Your case x = 100, y = 200:

displayInfo(str, 100, 200, color, font);
and somewhere else You set:

gluOrtho2D( 100, 500, 200, 500);

I had similar problem some time ago:
When raster position is outside viewport, text is not drawn.
I don't know if it's also true for borders, but maybe that is Your case.
Try this:

displayInfo(str, 101, 201, color, font);

shreddhead
01-13-2010, 04:56 PM
Thanks for the reply kowal. I tried that and I've tried changing all the values from the functions you've mentioned above, but I'm still not getting anything.

whats really strange is I have an almost identical set up in another program with the exact same values... and it works!

Any other ideas :)

kowal
01-14-2010, 03:50 PM
I copy-pasted Your code, and small cyan text 'Hello1' appears to be displayed properly. I don't see any obvious errors in this code (except ones I made with textures, but it's only quick fix to make it work without texture loading code, and images)

(code below is slightly modified, because I don't have full source (kayboard, setcamera, mouseMotion etc), ant textures )



#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>


//some hacks to make program work:

static int gl_Tex[5] = { 0, 1, 2, 3 , 4 }; //this will produce a lot of gl errors, but
// I need this to run unmodified code
//
static int imageW = 256;
static int imageH = 256;
static int imageH2 = 256;
static float cameraDistance = 30;
static int wWidth = 256;
static int wHeight = 256;

#define BUFFER_DATA(A) NULL

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();
}

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 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);
}

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();
}



int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
printf("Initializing GLUT...\n");

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(wWidth, wHeight);//imageW, imageH);
glutInitWindowPosition(100, 100);
glutCreateWindow("HeatGL");

printf("OpenGL window created.\n");

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);
//

glutMainLoop();

return 0;
}

shreddhead
01-28-2010, 05:07 PM
Thanks once again for the response kowal. Sorry I disappeared for awhile, I got distracted with another project :D But this issue needs to be resolved.

So I've been playing around, and I believe I have isolated the problem. In my code I'm using a PBO and texture objects. I noticed when I comment out the initialization function of the PBOs and Texture Objects, the text will appear! ... but obviously I don't have texture to my polygons.



void initOpenGLBuffers()
{
glGenTextures(2, gl_Tex);

glBindTexture(GL_TEXTURE_2D, gl_Tex[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, imageW, imageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, h_Src);

glBindTexture(GL_TEXTURE_2D, gl_Tex[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, imageW2, imageH2, 0, GL_RGBA, GL_UNSIGNED_BYTE, color_bar);
printf("Textures created.\n");

printf("Creating PBO...\n");
glGenBuffers(1, &amp;gl_PBO);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, gl_PBO);
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, imageW * imageH * 4, h_Src, GL_DYNAMIC_COPY);//GL_STREAM_COPY);
cudaGLRegisterBufferObject(gl_PBO);

}



Is there some configuration I'm missing? Or do you think I may be handling my matrices wrong?

Thanks again

shreddhead
01-30-2010, 08:02 PM
I found the solution to my problem! :)

It was a simple mistake, in the midst of learning how to get cuda and opengl PBOs to work cooperatively, I omitted some important calls:

glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, gl_PBO);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);

These instructions needed to be called before and after I rendering my polygon respectively.

shreddhead