Skip to main content
gamedev.net
🔒 Locked

Problem with glPrint

Started by cnjcombo 6 replies 2.6k views
Original Post
cnjcombo
cnjcombo
I was reading a post about using glPrint in order to have text messages popup at mouse coords on middle mouse click. Below is the mouse function and glPrint method I have included in my source code. All it does for some reason is change the color of an object in my background to the specified color in glColorf. Any ideas?

void mouse(int button, int state, int x, int y)
{
if (button == GLUT_MIDDLE_BUTTON)
   {
      if (state == GLUT_DOWN)
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
      // Clear Screen And Depth Buffer
      
      glLoadIdentity(); // Reset The View
      glTranslatef(0.0f,0.0f,-1.0f); // Move One Unit Into The Screen

      glColor3f(1.0f,0.0f,0.5f); // Colour text - bright green

      glRasterPos2f(x, y); // Position The Text On The Screen
      glPrint("woooo");	// Print GL Text To The Screen
   }
}


GLvoid glPrint(const char *fmt, ...)					// Custom GL "Print" Routine
{
	char		text[256];								// Holds Our String
	va_list		ap;										// Pointer To List Of Arguments

	if (fmt == NULL)									// If There's No Text
		return;											// Do Nothing

	va_start(ap, fmt);									// Parses The String For Variables
	    vsprintf(text, fmt, ap);						// And Converts Symbols To Actual Numbers
	va_end(ap);											// Results Are Stored In Text

	glPushAttrib(GL_LIST_BIT);							// Pushes The Display List Bits
	glListBase(base - 32);								// Sets The Base Character to 32
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text
	glPopAttrib();										// Pops The Display List Bits
}

cybergolem
cybergolem
From my experience i would say your mouse - coordinates are to high.
cause in the NeHe example he put the glRasterPos to something like -0.5 ,-0.4 to get the text in the upper left corner.
Try first to make the text appear at 0.0,0.0 for example for every mouseclick.
and after that you can put the coordinates of mouse-x and mouse-y in your print-line.
hope that helps, see my sig :)
Excuse my english, I am only German
cnjcombo
cnjcombo
Tried changing glTranslate to all zero's and same result.
Kalidor
Kalidor
Quote:
Original post by cnjcombo
I was reading a post about using glPrint in order to have text messages popup at mouse coords on middle mouse click. Below is the mouse function and glPrint method I have included in my source code. All it does for some reason is change the color of an object in my background to the specified color in glColorf. Any ideas?

*** Source Snippet Removed ***

*** Source Snippet Removed ***
I notice that you aren't changing your projection matrix at all. That may be a problem. glRasterPos* is treated just like glVertex*, ie: the modelview and projection matrices are applied to it. It seems like you are wanting to position the text in screen coordinates, so you need a projection matrix that allows you to do that. There's tons of posts here on how to enter "2d mode" in OpenGL so search for that.

Note: That is all assuming you don't already have a proper projection matrix set up for it. If you do we may need to see more code.
cnjcombo
cnjcombo
I changed the matrix mode to projection and on middle mouse click the whole screen goes blank.
Kalidor
Kalidor
Quote:
Original post by cnjcombo
I changed the matrix mode to projection and on middle mouse click the whole screen goes blank.
You can't just switch to projection matrix mode and expect it to work, you need to change the actual projection matrix. As I said, there are many posts about this here(even one linked in the OpenGL Forum FAQ).

If you follow all those steps and are still having trouble, post your new code and tell us exactly what's going wrong.
cnjcombo
cnjcombo
glPrint code is still the same, I am still having issues with the scene going blank when the mouse button is clicked. I read Nehe's tutorial before I even tried integrating this into my scene. I looked at his total code and it doesn't look like he is using his method in any special way or making any special changes. I am stumped.

void mouse(int button, int state, int x, int y){if (button == GLUT_MIDDLE_BUTTON)   {      if (state == GLUT_DOWN)      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);       // Clear Screen And Depth Buffer      glMatrixMode(GL_PROJECTION);      glLoadIdentity(); // Reset The View      glTranslatef(0.0f,0.0f,-1.0f); // Move One Unit Into The Screen      glColor3f(1.0f,0.0f,0.5f); // Colour text - bright green      glRasterPos2f(x, y); // Position The Text On The Screen      glPrint("woooo");	// Print GL Text To The Screen   }}

Falken42
Falken42
For anything 2D, I personally use an orthographic projection mode, and avoid glRasterPos.

After changing the matrix mode to projection, setup a 1:1 screen matrix with glOrtho, where left and top are zero, bottom and right are the size of the window, and znear and zfar are 1.0f and 3000.0f (although the Z doesn't really matter). Then switch the matrix mode back to model view and load an identity matrix.

From there, all rendering should map to screen coordinates. Pass your glRasterPos2f X and Y coordinates into the glTranslatef call and your text should appear in the right place.

Also, don't forget to push and pop your projection and modelview matrices (which it sounds like you're not doing).

Good luck.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.