Friday, July 19, 2019

opengl - 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();
}

Vertex Shader:


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;
}


Fragment Shader:


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

void main()
{
float add;

if (normal.x == 1.0 || normal.z == 1.0) {

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

Voxel Engine Artifacts (Dots)



Enlarge the image by click it to notice the artifacts.



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...