Fire Lancer
Fire Lancer

Reputation: 30145

OpenGL Pixel Gap between translated squares

I have run into an issue that there is occasionally one pixel holes along the join between 2 objects which have the same set of vertices, but translated to be next to each other. I managed to reproduce this with a line of simple squares.

The squares are 1 unit by 1 unit with 0,0 centre. I positioned a number of these squares in a line, using glTranslatef to move each square into position (the values I provide are always integers in this case). I found that when the camera is at certain positions OpenGL leaves a single pixel gap between these squares through which I can see whatever is behind (e.g. the colour I cleared the back buffer to). Changing the squares vertices to have 0,0 in a corner did not solve the issue.

Example code:

glPushMatrix();
glTranslatef(0, 0, 0);
drawSquare();
glPopMatrix();

glPushMatrix();
glTranslatef(0, 0, 1);
drawSquare();
glPopMatrix();

glPushMatrix();
glTranslatef(0, 0, 2);
drawSquare();
glPopMatrix();

The image shows such a case. I added a red circle to the screen shot around the problem pixel.

Image

I have no idea why this is the case, and while specifying the final "world" coordinates eliminating the per square translations for simple squares is possible (and solves the problem), doing so with the far more complicated geometry where I first noticed the issue is not really practical.

Upvotes: 3

Views: 1358

Answers (1)

Michael Slade
Michael Slade

Reputation: 13877

I don't know what the effect is called, but I know how it happens.

If two polygons "share" an edge, but the endpoints of that edge are calculated from different values, there will invariably be some error in the resulting translated vertex coordinates. A slight error in the vertex coordinates changes the points that make up edges that uses it, and so you get (1) overlapping pixels (not such a big deal usually) and (2) missing pixels, as shown in your example.

While you can usually make some effort to minimise duplication of common vertices, in general the problem can get Hard.

for example, minecraft has the same problem.

One possible quick fix is to apply a scale transform to the affected objects with a very small magnification factor, e.g. 1.00001. This will cause the objects to overlap each other every so slightly and should remove any neglected pixels. Your Mileage May Vary.

Upvotes: 3

Related Questions