Results 1 to 6 of 6

Thread: Text Rendering Problem

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2010
    Location
    Idaho
    Posts
    12

    Text Rendering Problem

    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:
    Code :
     
    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:
    Code :
    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:
    Code :
    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:
    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);
    shreddhead

  2. #2
    Senior Member Regular Contributor
    Join Date
    Aug 2009
    Location
    Poland
    Posts
    111

    Re: Text Rendering Problem

    Hi,
    (from your code)
    Code :
    glRasterPos2i(x, y);        // place text position
    in Your case x = 100, y = 200:
    Code :
     displayInfo(str, 100, 200, color, font);
    and somewhere else You set:
    Code :
    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:
    Code :
    displayInfo(str, 101, 201, color, font);

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2010
    Location
    Idaho
    Posts
    12

    Re: Text Rendering Problem

    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
    shreddhead

  4. #4
    Senior Member Regular Contributor
    Join Date
    Aug 2009
    Location
    Poland
    Posts
    111

    Re: Text Rendering Problem

    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 )

    Code :
    #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;
    }

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2010
    Location
    Idaho
    Posts
    12

    Re: Text Rendering Problem

    Thanks once again for the response kowal. Sorry I disappeared for awhile, I got distracted with another project 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.

    Code :
    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

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2010
    Location
    Idaho
    Posts
    12

    Re: Text Rendering Problem

    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
    shreddhead

Similar Threads

  1. Text rendering class does not draw text
    By th0masr0ss in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-23-2015, 09:10 PM
  2. Problem rendering text with a 3d enviroment
    By Winegums in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 04-25-2006, 03:53 AM
  3. Replies: 0
    Last Post: 02-05-2005, 07:20 PM
  4. Rendering text
    By martinho_ in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 09-03-2004, 02:48 PM
  5. Text rendering
    By blender in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 05-04-2002, 06:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean