Skip to main content
GameDev.net gamedev.net
🔒 Locked

Textures distortion

Started by MajinMusashi Sep 25, 2005 at 2:59 PM 2 replies 5.7k views
Original Post
MajinMusashi
MajinMusashi
Hello! CONTEXT: I'm finishing writing in Cpp a text rendering library based on FreeType2 and OpenGL, and a little problem raised when I was revising the text alignment code. PROBLEM: When the text is vertically or horizontally centered (or even worse, both), the textures get distorted. Please take a look at the alignment code below:
// Adjust horizontal alignment
if ( (hAlign != 0) && (coord[0] != coord[1]) ) {
  if ( hAlign == 1 ) {
    // Centered
    glTranslatef( (coord[1] - coord[0] - width) / 2.0f, 0.0f, 0.0f );	// PROBLEM HERE
  } else {
    // Right
    glTranslatef( coord[1] - coord[0] - width, 0.0f, 0.0f );
  }
}

// Adjust vertical alignment
if ( (vAlign != 0) && (coord[2] != coord[3]) ) {
  if ( vAlign == 1 ) {
    // Centered
    glTranslatef( 0.0f, (coord[3] - coord[2] - font.height) / 2.0f, 0.0f );  // PROBLEM HERE
  } else {
    // Top
    glTranslatef( 0.0f, coord[3] - coord[2] - font.height, 0.0f );
  }
}


CAUSES: As far as I can imagine, the problem is in the second argument passed to glTranslatef. For some reason I can't understand, when this value is not an integer (what is most of time), the textures get distorted. Converting the value to an integer (and converting again to float to avoid compiler warnings) everything seems to work fine (with some precision loss): glTranslatef( 0.0f, (float)((int)((coord[3] - coord[2] - font.height) / 2.0f)), 0.0f ); // UGLY!!! And these are the screenshots for the UNROUNDED and ROUNDED values. QUESTION: Is this an OpenGL "feature" relating to non-integer coordinates? Can you tell me what is going on? Any kind of help is very welcome! Thanks a lot! [Edited by - MajinMusashi on September 27, 2005 11:07:47 PM]
Ro_Akira
Ro_Akira
The links to the images didn't work for me. - nvm They work now :S

Okay, perhaps it is because when you do not use an interger value, you are not getting as good a pixel to pixel match as normal, and bilinear filtering does it's thing resulting in the blurry dot on the 'i's in the images supplied, for example.

For the sake of experimentation, try scrolling some text 0.1 units at a time across the screen, using a key to increment, and note whether the 'it looks right' and 'whole number' properties are in phase.
Halma
Halma
Have you tried setting the texture magnification+minification filters to GL_NEAREST?
MajinMusashi
MajinMusashi
Quote:
Original post by Halma
Have you tried setting the texture magnification+minification filters to GL_NEAREST?

Hehehe, I was faster than you, Halma! This morning (here in Brazil) I tried replacing GL_LINEAR with GL_NEAREST and everything worked fine. Thanks anyway!

Topic Locked

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

Sign in to reply to this topic.