Jump to content
  • Advertisement
Sign in to follow this  
Jordi.ak22

OpenGL About text in OpenGl

This topic is 777 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

Hi i'm using OpenGL2 and i'm tryin to write calculated distance on the screen

this is my code:

in void display:

  drawNumber(distance_1,-0.95,0.9); //distance_1 is a string with one digit number in each box

 

in drawNumber:

void drawNumber(const char *message, float x, float y)
{
int i=0;
glRasterPos2f(x+0.013, y);
//write using bitmap and stroke chars
for (i = 0; i < 3; i++)
{
               glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, message);
}
}
 
it does not work, and if i send drawNumber("456",-0.95,0.9); it does work.
tnx for helping

 

 

Share this post


Link to post
Share on other sites
Advertisement

Can you be a bit more specific than 'does not work'? Do you see any text at all? Do you see anything at all? Are you able to draw other text correctly?

 

Can you show the code where you create distance_1? That will probably be the most useful thing to see since you say the drawNumber("456",-0.95,0.9); does work correctly, that suggests there's a problem with your string.

Share this post


Link to post
Share on other sites

to use that you need to setup ortho etc. anyway you use opengl 2.0 and you use fixed function pipeline when you should use shaders.


void glWriteTEXT(int X,int Y, AnsiString text)
{
  glPushAttrib(GL_DEPTH_TEST);  // Save the current Depth test settings (Used for blending )
  glDisable(GL_DEPTH_TEST);     // Turn off depth testing (otherwise we get no FPS)


  glMatrixMode(GL_PROJECTION);  // Switch to the projection matrix
  glPushMatrix();               // Save current projection matrix
  glLoadIdentity();

//  GetWindowRect(GetDesktopWindow(), drawRect);  // Get current window size
  glOrtho(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, -1, 1);  // Change the projection matrix using an orthgraphic projection
  glMatrixMode(GL_MODELVIEW);  // Return to the modelview matrix
  glPushMatrix();              // Save the current modelview matrix
  glLoadIdentity();
 // glColor3f(1.0, 1.0, 1.0);    // Text color

  glRasterPos2i(X, Y);                                // Position the Text
  glPushAttrib(GL_LIST_BIT);                          // Save's the current base list
  glListBase(base);                              // Set the base list to our character list
  glCallLists(text.Length(), GL_UNSIGNED_BYTE, text.c_str());  // Display the text
  glPopAttrib();                                      // Restore the old base list

  glMatrixMode(GL_PROJECTION);  //Switch to projection matrix
  glPopMatrix();                // Restore the old projection matrix
  glMatrixMode(GL_MODELVIEW);   // Return to modelview matrix
  glPopMatrix();                // Restore old modelview matrix
 // glEnable(GL_TEXTURE_2D);      // Turn on textures, don't want our text textured
 // glEnable(GL_LIGHTING);
  glPopAttrib();                // Restore depth testing


}

Share this post


Link to post
Share on other sites

I know this may be hard to understand because he's writing it in a custom game engine made in his tutorials, however, I think you may find this useful as it overall explains text rendering very well:

And the second part for quality:

 

Just make sure to uniform as many text parameters as possible to have more control;)

Share this post


Link to post
Share on other sites

WiredCat now that looks easy. No additional libs like glut needed? Thanks for the code, I'll try tomorrow.

 

Beware of unspecified dependencies.

 

AnsiString text
 

What is "AnsiString"?  A custom class?  Something from a library?  Which library?

 

glListBase(base);                              // Set the base list to our character list
glCallLists(text.Length(), GL_UNSIGNED_BYTE, text.c_str());  // Display the text

 

What is base?  Where does it's value come from?  What is used to generate the display lists?  Where are they generated?  Does this generation have any other dependencies?

Share this post


Link to post
Share on other sites

Seems to need glut, freetype etc., but I don't want dependencies. I'll try to link statically sdl_ttf now.

Share this post


Link to post
Share on other sites


void BuildFont(unsigned int  & Ufont)
{

HFONT font;

  Ufont = glGenLists(256);                  // Generate enough display lists to hold

  font = CreateFont(22,                           // height of font
			 0,                             // average character width
			 0,                             // angle of escapement
			 0,                             // base-line orientation angle
			 FW_BOLD,                       // font weight
			 0,			            // italic
			 0,                             // underline
			 0,			            // strikeout
			 DEFAULT_CHARSET,                  // character set
			 OUT_TT_PRECIS,	            // output precision
			 CLIP_DEFAULT_PRECIS,           // clipping precision
			 ANTIALIASED_QUALITY,           // output quality
			 FF_DONTCARE || DEFAULT_PITCH,  // pitch and family
			 "Courier New");                     // font

  SelectObject((*dc), font);               // Sets the new font as the current font in the device context
  wglUseFontBitmaps((*dc), 0, 256, Ufont);  // Creates a set display lists containing the bitmap fonts


}

AnsiString can be std::string

where BuildFont(base);

 

and where unsigned int base;

 

 

 

 

but my point was that you didnt se proper perspective projection for using glrasterpos

 

 

you should anyway use shaders, and one texture where you have all font characters, and quit using old stuff ;] ;p

Edited by WiredCat

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!