Link to home
Start Free TrialLog in
Avatar of KiwiFreaK
KiwiFreaK

asked on

Ugly TGA textures

Hi All,

I've little OpenGL problems!

I am building an OpenGL application in Delphi using TGA textures files(480x480). I use my BuildList function to create pieces of this big texture. Every thing works fine! But the problem is: "when i've loaded the textures and paint them on screen, (1.) there are some (ugly) lines around my texture (2.) and my texture is smooth (I DONT LIKE THAT)"

See my screenshot here :
http://www.kiwifreak.com/help.gif

Load Texture with:
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Draw the tile with:
  glBegin(GL_POLYGON);
    glTexCoord2f [...]
    glVertex2f [...]
  glEnd;


Thanks for your veryvery mucht for help!

Sander
Avatar of KiwiFreaK
KiwiFreaK

ASKER

NOTE: I use GL_BLEND
Please help me!
Does glShadeModel(GL_FLAT); have any affect?
Also, have you tried using GL_QUADS and making sure it looks right or the same(bad) on a quad the exact size of the texture?

what are the vertices you're using in that polygon?
Haai,

Thanks for your reply!
I've tried "glShadeModel(GL_FLAT); ", no effect!
Yes, i've also tried GL_QUADS, it doen't matter; the same smooth textures!


This is my BuildList Function:

function BuildList(baseofbase,t_row,t_col,dem : integer; t_resource : Gluint) : integer;
var
  loop : integer;
  cx,cy,demension,tile_border,texture_borderY,texture_borderX : single;
  maxlist : Integer;
begin
  base := baseofbase;
  maxlist := t_row*t_col;
  demension := (dem / 2);
  tile_border := 0.0;
  texture_borderY := 0.001;
  texture_borderX := 0.001;
  for loop := 0 to maxlist-1 do
  begin
    cx := (loop mod t_col) / t_col;
    if (loop mod t_col = 0) then
      cy := (loop / t_row) / t_row;
    glNewList(base+loop,GL_COMPILE);
    glBindTexture(GL_TEXTURE_2D, t_resource);
      glBegin(GL_QUADS);
        glTexCoord2f(texture_borderX+cx                  ,texture_borderY+((1.0-cy)-(1.0 / t_row)));  glVertex2f(-demension-tile_border, +demension+tile_border);
        glTexCoord2f((cx+(1.0 / t_col))-texture_borderX  ,texture_borderY+((1.0-cy)-(1.0 / t_row)));  glVertex2f(+demension+tile_border, +demension+tile_border);
        glTexCoord2f((cx+(1.0 / t_col))-texture_borderX  ,(1.0-cy)-texture_borderY);                  glVertex2f(+demension+tile_border, -demension-tile_border);
        glTexCoord2f(texture_borderX+cx                  ,(1.0-cy)-texture_borderY);                  glVertex2f(-demension-tile_border, -demension-tile_border);
      glEnd();
    glEndList();
  end;
  result := base;
end;
to get non smooth textures :

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

To fix the border pixels, consider the following.

1. textures are addressed with coordinates x and y where 0 <= x < 1 and 0 <= y < 1.
2. When a polygon is rasterized from x1 to x2, then the same rule is used, namely x1 <= pixelpos < x2 (This means that the last pixel, the one on x2 is NOT drawn).
3. Combine 1 and 2 to see that the texture at 1.00 is not referenced because the pixel at x2 is not drawn.
4. When using gl textures, you have to tell gl what to do with pixels that have texture coordinates not in the range specified above. The basic options are :
      - wrap .. texture is repeated.
      - clamp .. texture border pixel is repeated.
    You tell this to the gl by :
   
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    or if you want clamping :
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

Ok, also keep in mind that the glTexParameterf calls change the texture state for the currently bound texture object !
Also, notice that GL_CLAMP means something different for textures that are specified with a border (at the glteximage2d command).
If you have a border, the border color (which you set using glTexParamterfv(GL_TEXTURE_2D, GL_BORDER_COLOR, &color)) is repeated.
If you don't have a border, the texture image last pixel is repeated.

A simple code example to draw a quad with a texture looks something like this :

// Bind texture target.
  glBindTexture(GL_TEXTURE_2D, mytexture);
// Disable texture interpolation
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Set up projection matrix.
  glPushMatrix();  
  glLoadIdentity();
     glTranslatef(0,0,-10);
   // Draw textured quad.
     glBegin(GL_QUADS);
       glTexCoord2f(0,0); glVertex2f(-2,-2);
       glTexCoord2f(1,0); glVertex2f(2,-2);
       glTexCoord2f(1,1); glVertex2f(2,2);
       glTexCoord2f(0,1); glVertex2f(-2,2);
     glEnd();
  glPopMatrix();




ASKER CERTIFIED SOLUTION
Avatar of davebytes
davebytes
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you dont want to, you dont have to resample your picture ..

You can easily call glTexImage2D with 512x512, and NIL as the data pointer.
then call gltexsubimage2d to put your 400x400 bitmap in the upper left corner of this texture.

Then scale your texture coordinates. for example like this ..

glmatrixmode(gl_texture);
  glscalef(1*400/512,1*400/512,1);
glmatrixmode(gl_modelview);

or by passing scaled coordinates off course ..
This destroys wrapping behaviour off course ..
I specifically said to do pow2 so that you get full wrapping behavior, no odd edges.  If this is a sprite-based game, then he'll be clipping coordinates anyway as part of the polygon rendering, no need to play with the texture matrix.  Otherwise, the move to pow2 textures will save a lot of pain in the future, especially on older hardware, when wrapping, etc.

d