Jump to content
  • Advertisement
Sign in to follow this  
hellspawn198369

OpenGL Using text in OpenGL

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a simple game engine for openGL and i'm having some issues on text. I did some research on the subject, and i was able to implement the bitmap text algrathm from NEHE's Opengl tortutial, but it only works for a blank screen(ie, when there is no 3d drawing being done) Here's my code for drawing the scene(note, the everything up to this point has been initalized, including the buildfont and i've currently put the text output code at the bottom)
int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
{
	//bool once = false;  //used for old movement st
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
	glLoadIdentity();									// Reset The View

	clock_t time_elapsed;
 time_elapsed = clock();
	GLfloat x_m, y_m, z_m, u_m, v_m;
	GLfloat xtrans = -xpos;
	GLfloat ztrans = -zpos;
	GLfloat ytrans = -headbob-0.4f;
	GLfloat sceneroty = 360.0f - yrot;

	int numpolygons;
 // if (once) //old movement style
 // {
	//glRotatef(lookupdown,1.0f,0,0);//rotates scene up and down
	//glRotatef(sceneroty,0,1.0f,0);//rotates scene right and left 


	//glTranslatef(xtrans, ytrans, ztrans);//moves the scene to simulate camera movement
 // //once = false;
 // }
	

glLoadIdentity();
  gluLookAt( -xtrans, -ytrans, -ztrans, 
	     -xtrans-sin(yrot*piover180)*100,-tan(lookupdown*piover180)*100, -ztrans-cos(yrot*piover180)*100, 
	     0.0, 1.0, 0.0);

	


glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture[1]);
	
	numpolygons = sector1.numpolygons;
	
	//create the world
	// Process Each polygon 
	for (int loop_m = 0; loop_m < numpolygons; loop_m++)
	{
		if (loop_m == 2)
	glBindTexture(GL_TEXTURE_2D, texture[2]);
		if (loop_m == 3)
glBindTexture(GL_TEXTURE_2D, texture[0]);
		glBegin(GL_QUADS);
			glNormal3f( 0.0f, 0.0f, 1.0f);
			x_m = sector1.polygon[loop_m].vertex[0].x;
			y_m = sector1.polygon[loop_m].vertex[0].y;
			z_m = sector1.polygon[loop_m].vertex[0].z;
			u_m = sector1.polygon[loop_m].vertex[0].u;
			v_m = sector1.polygon[loop_m].vertex[0].v;
			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
			
			x_m = sector1.polygon[loop_m].vertex[1].x;
			y_m = sector1.polygon[loop_m].vertex[1].y;
			z_m = sector1.polygon[loop_m].vertex[1].z;
			u_m = sector1.polygon[loop_m].vertex[1].u;
			v_m = sector1.polygon[loop_m].vertex[1].v;
			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
			
			x_m = sector1.polygon[loop_m].vertex[2].x;
			y_m = sector1.polygon[loop_m].vertex[2].y;
			z_m = sector1.polygon[loop_m].vertex[2].z;
			u_m = sector1.polygon[loop_m].vertex[2].u;
			v_m = sector1.polygon[loop_m].vertex[2].v;
			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

			x_m = sector1.polygon[loop_m].vertex[3].x;
			y_m = sector1.polygon[loop_m].vertex[3].y;
			z_m = sector1.polygon[loop_m].vertex[3].z;
			u_m = sector1.polygon[loop_m].vertex[3].u;
			v_m = sector1.polygon[loop_m].vertex[3].v;
			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

		glEnd();
	}
//glLoadIdentity();
	for (int index = 0; index < 4; index++)
	{

		if (!painting[index].inventory)
		{
			glPushMatrix();
			
				glTranslatef(painting[index].posx, 0, painting[index].posz);
				glRotatef(time_elapsed/5,0,1.0,0);
		
		
				glBindTexture(GL_TEXTURE_2D, 1);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
				glScaled(.5, .5, .1);
				glCallList(painting[index].id);
			glPopMatrix();
		}
	}
	glDisable(GL_TEXTURE_2D);
///////////////////////////////////////////
//this is the testing ground for the text//
	//glPushMatrix();  //tried to seprate the matrix, but that did nothing
	
	//glTranslatef(0.0f,0.0f,-1.0f); // Move One Unit Into The Screen
	
	glColor3f(1.0f,1.0f,1.0f);
	
	glRasterPos2f(-0.2f, 0.22f);  // Position The Text On The Screen
 	glPrint("Here's some Text!!");
	//glPopMatrix();
////////////////////////////////////////////
	glFlush();
	return TRUE;										// Everything Went OK
}

now, all the 3d drawing goes to plan, but there's no text. Does anyone have any suggestions?? -steve

Share this post


Link to post
Share on other sites
Advertisement
I'm not the best to ask but maybe, with what I have to do sometimes , try puting
glprint on top of glColor3f and glRasterPos2f. It might work but I don't know. You might just want to try.

Share this post


Link to post
Share on other sites
Your code is almost as messy as mine :D and difficult to follow, but I think the most likely candidate is:

glDisable(GL_TEXTURE_2D);

It's probably unlikely that glEnable() for tex is called from within glprint, hence the text is not drawn.

Share this post


Link to post
Share on other sites
If you want the text to be overlayed on the screen - like FPS counters and other stats, then you need to switch from 3D perspective mode to a 2D orthographic project mode before rendering the text. If you intend to have the text in 3D space, then you need to supply x,y, and z coordinates for your text. At the moment you are only providing 2D coordinates (x,y). Using the bitmap font in 3D space is quirky. It will work, but the text will always remain the same height the further away or closer you get, which is not usually what you want. Bitmap fonts are designed to be used in 2D orthographic mode.

eg. For the overlay...


int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{

// Switch to 3D perspective mode
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
gluPerspective(45.0f,(double)rcreen_width/(double)screen_height,1.0,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//bool once = false; //used for old movement st
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View

clock_t time_elapsed;
time_elapsed = clock();
GLfloat x_m, y_m, z_m, u_m, v_m;
GLfloat xtrans = -xpos;
GLfloat ztrans = -zpos;
GLfloat ytrans = -headbob-0.4f;
GLfloat sceneroty = 360.0f - yrot;

int numpolygons;
// if (once) //old movement style
// {
//glRotatef(lookupdown,1.0f,0,0);//rotates scene up and down
//glRotatef(sceneroty,0,1.0f,0);//rotates scene right and left


//glTranslatef(xtrans, ytrans, ztrans);//moves the scene to simulate camera movement
// //once = false;
// }


glLoadIdentity();
gluLookAt( -xtrans, -ytrans, -ztrans,
-xtrans-sin(yrot*piover180)*100,-tan(lookupdown*piover180)*100, -ztrans-cos(yrot*piover180)*100,
0.0, 1.0, 0.0);




glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[1]);

numpolygons = sector1.numpolygons;

//create the world
// Process Each polygon
for (int loop_m = 0; loop_m < numpolygons; loop_m++)
{
if (loop_m == 2)
glBindTexture(GL_TEXTURE_2D, texture[2]);
if (loop_m == 3)
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f);
x_m = sector1.polygon[loop_m].vertex[0].x;
y_m = sector1.polygon[loop_m].vertex[0].y;
z_m = sector1.polygon[loop_m].vertex[0].z;
u_m = sector1.polygon[loop_m].vertex[0].u;
v_m = sector1.polygon[loop_m].vertex[0].v;
glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

x_m = sector1.polygon[loop_m].vertex[1].x;
y_m = sector1.polygon[loop_m].vertex[1].y;
z_m = sector1.polygon[loop_m].vertex[1].z;
u_m = sector1.polygon[loop_m].vertex[1].u;
v_m = sector1.polygon[loop_m].vertex[1].v;
glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

x_m = sector1.polygon[loop_m].vertex[2].x;
y_m = sector1.polygon[loop_m].vertex[2].y;
z_m = sector1.polygon[loop_m].vertex[2].z;
u_m = sector1.polygon[loop_m].vertex[2].u;
v_m = sector1.polygon[loop_m].vertex[2].v;
glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

x_m = sector1.polygon[loop_m].vertex[3].x;
y_m = sector1.polygon[loop_m].vertex[3].y;
z_m = sector1.polygon[loop_m].vertex[3].z;
u_m = sector1.polygon[loop_m].vertex[3].u;
v_m = sector1.polygon[loop_m].vertex[3].v;
glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

glEnd();
}
//glLoadIdentity();
for (int index = 0; index < 4; index++)
{

if (!painting[index].inventory)
{
glPushMatrix();

glTranslatef(painting[index].posx, 0, painting[index].posz);
glRotatef(time_elapsed/5,0,1.0,0);


glBindTexture(GL_TEXTURE_2D, 1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glScaled(.5, .5, .1);
glCallList(painting[index].id);
glPopMatrix();
}
}
glDisable(GL_TEXTURE_2D);
///////////////////////////////////////////
//this is the testing ground for the text//

// switch to 2D orthographic mode
// projection matrix mode and reset
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Switch to orthographic mode
glOrtho(0.0,(double)screen_width,(double)screen_height,0.0,-1.0,1.0f);
// Back to modelview
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// No depthtesting or blending (or lighting even)
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);


glPushMatrix(); // this might already be in the glPrint routine.. check it
glTranslatef(0.0f,0.0f,-1.0f); // Move One Unit Into The Screen
glColor3f(1.0f,1.0f,1.0f);
glRasterPos2f(-0.2f, 0.22f); // Position The Text On The Screen
glPrint("Here's some Text!!");
glPopMatrix();

////////////////////////////////////////////
glFlush();
return TRUE; // Everything Went OK
}





(I added a switch 3D section at the top of the routine, and a switch 2D section before the text print part. You will need to change variables/values to work with your program of course).
hth
F451

Share this post


Link to post
Share on other sites
allright, got it working. The solution was actually kinda simple. I added this code right before setting the color, raster position and calling the print command:

///////////////////////////////////////////
//this is the testing ground for the text//

glDisable(GL_TEXTURE_2D);
glClear(GL_DEPTH_BUFFER_BIT); // Clear Depth Buffer so text will be on top of everything on the screen
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f,0.0f,-1.0f); // Move Text One Unit Into The Screen so it isn't cliped

glColor3f(1.0f,1.0f,1.0f);
// Position The Text On The Screen
glRasterPos2f(-0.2f, 0.22f);
glPrint(message);
////////////////////////////////////////////

glFlush();
return TRUE; // Everything Went OK


thanks for the help, i was thinking in the wrong direction before your sugestions.

-Steve

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!