1

Despite passing equal (exactly equal) coordinates for 'adjacent' edges, I'm ending up with some strange lines between adjacent elements when scaling my grid of rendered tiles.

My tile grid rendering algorithm accepts scaled tiles, so that I can adjust the grid's visual size to match a chosen window size of the same aspect ratio, among other reasons. It seems to work correctly when scaled to exact integers, and a few non-integer values, but I get some inconsistent results for the others.

Some Screenshots:

The blue lines are the clear color showing through. The chosen texture has no transparent gaps in the tilesheet, as unused tiles are magenta and actual transparency is handled by the alpha layer. The neighboring tiles in the sheet have full opacity. Scaling is achieved by setting the scale to a normalized value obtained through a gamepad trigger between 1f and 2f, so I don't know what actual scale was applied when the shot was taken, with the exception of the max/min.

Attribute updates and entity drawing are synchronized between threads, so none of the values could have been applied mid-draw. This isn't transferred well through screenshots, but the lines don't flicker when the scale is sustained at that point, so it logically shouldn't be an issue with drawing between scale assignment (and thread locks prevent this).

Scaled to 1x:

1x

Scaled to A, 1x < Ax < Bx :

Ax

Scaled to B, Ax < Bx < Cx :

Bx

Scaled to C, Bx < Cx < 2x :

Cx

Scaled to 2x:

2x

Projection setup function

For setting up orthographic projection (changes only on screen size changes):

    .......
    float nw, nh;
    nh = Display.getHeight();
    nw = Display.getWidth();
    GL11.glOrtho(0, nw, nh, 0, 1, -1);
    orthocenter.setX(nw/2);  //this is a Vector2, floats for X and Y, direct assignment.
    orthocenter.setY(nh/2);
    .......

For the purposes of the screenshot, nw is 512, nh is 384 (implicitly casted from int). These never change throughout the example above.

General GL drawing code

After cutting irrelevant attributes that didn't fix the problem when cut:

@Override
public void draw(float xOffset, float yOffset, float width, float height,
        int glTex, float texX, float texY, float texWidth, float texHeight) {
    GL11.glLoadIdentity();
    GL11.glTranslatef(0.375f, 0.375f, 0f); //This is supposed to fix subpixel issues, but makes no difference here

    GL11.glTranslatef(xOffset, yOffset, 0f);

    if(glTex != lastTexture){
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, glTex);
        lastTexture = glTex;
    }
    GL11.glBegin(GL11.GL_QUADS);

    GL11.glTexCoord2f(texX,texY + texHeight);
    GL11.glVertex2f(-height/2, -width/2);

    GL11.glTexCoord2f(texX + texWidth,texY + texHeight);
    GL11.glVertex2f(-height/2, width/2);

    GL11.glTexCoord2f(texX + texWidth,texY);
    GL11.glVertex2f(height/2, width/2);

    GL11.glTexCoord2f(texX,texY);
    GL11.glVertex2f(height/2, -width/2);

    GL11.glEnd();
}

Grid drawing code (dropping the same parameters dropped from 'draw'):

//Externally there is tilesize, which contains tile pixel size, in this case 32x32
public void draw(Engine engine, Vector2 offset, Vector2 scale){
  int xp, yp;                                   //x and y position of individual tiles
  for(int c = 0; c<width; c++){                 //c as in column
      xp = (int) (c*tilesize.a*scale.getX());   //set distance from chunk x to column x
      for(int r = 0; r<height; r++){            //r as in row
        if(tiles[r*width+c] <0) continue;       //skip empty tiles ('air')
        yp = (int) (r*tilesize.b*scale.getY()); //set distance from chunk y to column y
        tileset.getFrame(tiles[r*width+c]).draw(                  //pull 'tile' frame from set, render.
            engine,                                               //drawing context
            new Vector2(offset.getX() + xp, offset.getY() + yp),  //location of tile
            scale                                                 //scale of tiles
          );
      }
    }
  }

Between the tiles and the platform specific code, vectors' components are retrieved and passed along to the general drawing code as pasted earlier.

My analysis

Mathematically, each position is an exact multiple of the scale*tilesize in either the x or y direction, or both, which is then added to the offset of the grid's location. It is then passed as an offset to the drawing code, which translates that offset with glTranslatef, then draws a tile centered at that location through halving the dimensions then drawing each plus-minus pair.

This should mean that when tile 1 is drawn at, say, origin, it has an offset of 0. Opengl then is instructed to draw a quad, with the left edge at -halfwidth, right edge at +halfwidth, top edge at -halfheight, and bottom edge at +halfheight. It then is told to draw the neighbor, tile 2, with an offset of one width, so it translates from 0 to that width, then draws left edge at -halfwidth, which should coordinate-wise be exactly the same as tile1's right edge. By itself, this should work, and it does. When considering a constant scale, it breaks somehow.

When a scale is applied, it is a constant multiple across all width/height values, and mathematically shouldn't make anything change. However, it does make a difference, for what I think could be one of two reasons:

  • OpenGL is having issues with subpixel filling, ie filling left of a vertex doesn't fill the vertex's containing pixel space, and filling right of that same vertex also doesn't fill the vertex's containing pixel space.
  • I'm running into float accuracy problems, where somehow X+width/2 does not equal X+width - width/2 where width = tilewidth*scale, tilewidth is an integer, and X is a float.

I'm not really sure about how to tell which one is the problem, or how to remedy it other than to simply avoid non-integer scale values, which I'd like to be able to support. The only clue I think might apply to finding the solution is how the pattern of line gaps isn't really consistant (see how it skips tiles in some cases, only has vertical or horizontal but not both, etc). However, I don't know what this implies.

4

1 回答 1

1

This looks like it's probably a floating point precision issue. The critical statement in your question is this:

Mathematically, each position is an exact multiple [..]

While that's mathematically true, you're dealing with limited floating point precision. Sequences of operations that should mathematically produce the same result can (and often do) produce slightly different results due to rounding errors during expression evaluation.

Specifically in your case, it looks like you're relying on identities of this form:

i * width + width/2 == (i + 1) * width - width/2

This is mathematically correct, but you can't expect to get exactly the same numbers when evaluating the values with limited floating point precision. Depending on how the small errors end up getting rounded to pixels, it can result in visual artifacts.

The only good way to avoid this is that you actually use the same values for coordinates that must be the same, instead of using calculations that mathematically produce the same results.

In the case of coordinates on a grid, you could calculate the coordinates for each grid line (tile boundary) once, and then use those values for all draw operations. Say if you have n tiles in the x-direction, you calculate all the x-values as:

x[i] = i * width;

and then when drawing tile i, use x[i] and x[i + 1] as the left and right x-coordinates.

于 2014-09-08T02:12:57.863 回答