View Full Version : Weird flickering when rendering with lwjgl

04-04-2015, 04:00 AM
When i render some basic terrain with lwjgl and move the my mesh there are some weird artifacts, this only happens with windows 8 and a nvidia gpu.
On my old macbook the exact same code (except for natives) works fine.


I already tried to render on a texture but it didnt change.

Has this something to do with my code or is it a bug with the nvidia drivers.

fragment shader

#version 400core

in vec2 texCords;
in vec3 surfaceNormal;
in vec3 toLight;
in vec3 toCamera;
in vec3 position_out;
in float distanceToCamera_out;

uniform vec3 diffuseLightColor;
uniform vec3 specularLightColor;

out vec4 out_Color;

void main(void){

vec3 unitNormal = normalize(surfaceNormal);
vec3 unitToLight = normalize(toLight);

vec4 color = vec4(1,1,1,1);

//diffuse light
float diffuseFactor = max(dot(unitNormal, unitToLight),0);
vec3 diffuse = diffuseLightColor * diffuseFactor;

//specular light
vec3 reflected = reflect(-unitToLight,unitNormal);
float specularFactor = max(dot(reflected,normalize(toCamera)),0);
specularFactor = pow(specularFactor,50); //dampening
vec3 specular = specularLightColor * specularFactor;

//final light
vec3 finalLight = diffuse + specular;

//final color
out_Color = color * vec4(finalLight,1);

render function

public void render(Entity entity) {
TexturedModel texModel=entity.getModel();
RawModel model=texModel.getModel();
ModelTexture texture = texModel.getTexture();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getBaseId());


GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, model.getVboiId());
shader.loadTransformation(Maths.createTransformati onMatrix(entity.getPosition(), entity.getRx(), entity.getRy(), entity.getRz(), entity.getScale()));
GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);

Dark Photon
04-04-2015, 05:06 PM
...there are some weird artifacts
Perhaps you could call out what weird artifacts you're referring to.

There's no texture, but you're not reading any textures in your shader so that's not the artifact...

04-05-2015, 05:16 AM
Well with the artifacts i just meant those spots on the model where the color is too dark for just a few frames.

Dark Photon
04-05-2015, 04:57 PM
Ok. Guess I don't see that in your video. Might move your light so it's more orthogonal to the surface being shaded and retry. Everything I noticed was just normal diffuse lighting.

04-06-2015, 04:33 AM
I tried the thing with the lighting but the flickering remains. So I uploaded a video where it can be seen a bit better.


Anyways thanks for the help.

Dark Photon
04-06-2015, 06:06 AM
Ok, I think I see what you're talking about. It appears those might be tearing artifacts. That is, artifacts of the video card scanning out pieces of different different rendered video frames for display within the same scan-out frame.

You need to enable VSync (i.e. Sync-to-VBlank) for your rendering. See:

* https://www.opengl.org/wiki/Swap_Interval

Basically, call {wgl,glX}SwapInterval(1) in your program, and check your driver control panel to ensure that it's not overriding the swap interval that your program specifies.

04-06-2015, 08:37 AM
So when I call Display.setSwapInterval(1) and turn off V-Sync in my graphics controlpanel the artifacts still remain. So im not sure if the lwjgl Display.setSwapInterval method is equivalent to the wgl method you mentioned above.

Heres the method where I create my display

public static void setUpDisplay(){
ContextAttribs attribs = new ContextAttribs(3,2).withForwardCompatible(true).wi thProfileCore(true);
try {
Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
Display.create(new PixelFormat(), attribs);
} catch (LWJGLException e) {
glViewport(0, 0, WIDTH, HEIGHT);