# Voxel engine artifacts

There are white little dots between blocks at random places, mainly at very near blocks. They disappear when I move the mouse and change the view direction.

I use Vertex Arrays with glVertexAttributePointer to send the data directly to the shader. The shader only adds some fake light based on the face direction and draws the pixels.

What could be the reason for the small white artifacts?

Chunk render code:

public void renderChunk() {
glPushMatrix();

glTranslatef(pos.x*world.chunkSize.x, 0.0f, pos.z*world.chunkSize.z);

glBindBuffer(GL_ARRAY_BUFFER, vao_id);

glVertexAttribPointer(world.game.positionAtt, 3, GL_FLOAT, false, 32, 0);
glVertexAttribPointer(world.game.normalAtt, 3, GL_FLOAT, false, 32, 12);
glVertexAttribPointer(world.game.texCoordAtt, 2, GL_FLOAT, false, 32, 24);

glDrawArrays(GL_TRIANGLES, 0, arraySize / 8);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glPopMatrix();
}


attribute vec3 in_position;
attribute vec3 in_normal;
attribute vec3 in_texcoord;
varying vec2 texture_coordinate;
varying vec3 normal;

void main()
{
gl_Position = gl_ModelViewProjectionMatrix * vec4(in_position, 1.0);
normal = in_normal;
texture_coordinate = in_texcoord;
}


varying vec2 texture_coordinate;
varying vec3 normal;
uniform sampler2D texture;

void main()
{

if (normal.x == 1.0 || normal.z == 1.0) {
} else if (normal.y != 1.0) {
} else {
}
gl_FragColor = texture2D(texture, texture_coordinate) * add;
}


Enlarge the image by click it to notice the artifacts.

• Is there any particular reason that you're using the Minecraft default textures? It may be acceptable if it's just a placeholder whilst you do testing, but if you use those graphics anywhere outside of your computer there are copyright issues (as they aren't your graphics). – Polar Mar 16 '13 at 16:09
• If you are going to implement a deferred renderer, a post processing anti aliasing would blur these artifacts away. I am not sure what rendering technique you use, but I can't see any anti aliasing in your screenshot. – danijar Mar 16 '13 at 16:28
• Basaa, it would be very helpful if we had the code - at the minute all we can do is speculate. Help us help you, if you will. – Polar Mar 16 '13 at 16:33
• Yes, those textures are ofcourse just a placeholder. I'm just experimenting with Voxel Engines, this isn't even going to be a complete game. – Basaa Mar 16 '13 at 16:54
• Are you performing merging of the quads? – David Williams Mar 16 '13 at 20:19

I had the same problem with a recent game I'm working on. I guess you are using one big texture with all the block textures in it (like Minecraft does it)?

If so these white pixels appear because of rounding errors in the shader that lead to "empty" texture spots. This occurs even if you do not use texture interpolation (which is the case with your game obviously).

You can circumvent the problem by modifying the texture coordinates to add a little margin. This way the rounding error won't lead to a lookup outside the texture. This may not be the best solution but when it comes to float values you always have to take care of possible rounding problems.

Another way to fix that would be to use one of the more recent GLSL versions that offer a integer based texture lookup method.

If you build your meshes more carefully, ensuring you are reusing vertices wherever possible, you should be fine: That is, you must not duplicate vertices per face as then the GPU rasteriser will see them as two discrete objects and sometimes fail to rasterise the in-between space (thus defaulting to the GL's clear colour) due to floating point limitations; whereas had you shared the vertices between these same two faces, the rasteriser would have seen each fragment as either belonging to one face or to the other, but never to the space behind them (sky).

Polar rightly notes that Minecraft has these artifacts too. They are only noticeable when you are above a certain depth (y), because the sky(box) colour changes to grey/black once you hit the Deeps, so you won't notice fragments of blue sky shining through. That's one reason they're not as evident as they are in your engine.

The greater reason why your artifacts are more evident than Minecraft's is because the latter constructs RLE-based columnar meshes; that is, the stack of voxels (in y) at every (x,z) location is a single mesh rather than a mesh per block or per face (as you seem to have!), with the texture presumably procedurally generated for the the correct columnar sequence of voxel types. So in MC you won't experience noticeable sky pixels on the ground surface, or on the edges of columns, or vertically between cells; only between columns, and thus much less frequently than in your case, where you seem to have seams all over the place, including on cubes' upper edges.

(P.S. This should also greatly reduce your upload bandwidth and potentially number of draw calls.)

• Very nice! Shared vertices, that's certainly the way to go. – user1306322 Mar 17 '13 at 16:42

If you look closely at the dots, you'll see that they're all on the cube edges. The most likely explanation for this is that when you calculate the voxels positions in the viewport (screen), the rendering algorithm isn't seamless - that is, when they are converted from 3D to 2D, there are areas in the 2D which are never mapped to by the 3D world. If you're drawing the edges with a line, which is again quite likely, the line algorithm may miss out the odd pixel.

On the subject of Minecraft (see my comment about graphics and copyright on your post), Minecraft itself has these glitches from time to time - they aren't entirely unexpected. On the other hand, they can be quite annoying - I don't personally know a solution to this off the top of my head; if you really need to remove them, then you'll have to experiment, or ask someone else. Personally though, I wouldn't notice them if the game was any good - I'd be too absorbed in it to see such tiny graphical glitches.

• Instead I think the artifacts are caused by rounding error of floating point vertex data. – danijar Mar 16 '13 at 16:29
• @sharethis It's possible, I agree - it would probably be best to implement your suggestion above. On the other hand, my explanation matches it up with visual glitches I've seen before (without anti-aliasing) - who knows which one is right? Of course, it would help if we had the code. – Polar Mar 16 '13 at 16:33
• I agree with that. – danijar Mar 16 '13 at 17:05