- #OPENGL JAVA LWJGL FRAGMENT SHADER WITH LIGHTING DRIVER#
- #OPENGL JAVA LWJGL FRAGMENT SHADER WITH LIGHTING CODE#
I didn't try to follow all of the code, but high exponents and division by almost-zero are potential sources for weirdness. Cannot add 1000 + 0.0001 and expect to get 1000.0001, since 32-bit space isn't sufficient for that precision (or is at the limit). Potentially you add a big value with a small value in float32 space - this may be impossible, as you only have 7 significants to use. I may rather write the different parts separately. You calculate lightDirection twice for the spot light, once in calcSpotlight and once in calcPointlight. It is a lot more complex the glEnable(LIGHTINGANDSHADOWING).
#OPENGL JAVA LWJGL FRAGMENT SHADER WITH LIGHTING DRIVER#
Or maybe the GPU driver goes nuts there :-). You will want to go down the path of writing your own shaders, understanding what OpenGl provides for shadowing and lighting and not relying on older, deprecated lighting models. Maybe you simply produce black where the dot product is 0.71 - may happen at only a few pixels, where the spotlight values are much on negative. Will be: 1 - (1 - 0.6) / (1 - 0.71) -> -0.379310345Īnd the breakpoint is ofc where the dot product and spotLight.cutoff are equal or close to equal.Īfter this i cannot guess what happens. Then color goes negative here (as you said spotLight.cutoff is 0.71): color = calcPointLight(spotLight.pointLight, normal) * If spotFactor becomes 0.6 at a specific pixel, from this float spotFactor = dot(lightDirection, spotLight.direction)
![opengl java lwjgl fragment shader with lighting opengl java lwjgl fragment shader with lighting](https://i.ytimg.com/vi/8D1EET8fUtE/maxresdefault.jpg)
Here, the black pixels are mostly at the edge of the spotlight Here, the black pixels can be seen at the bottom of the spotlight Here are some example images of the bug in game: And since debugging in GLSL is nearly impossible, I've decided that my last possible option is to post my question here. I checked to see if there was maybe some division by zero problem or something of the like, but I can't for the life of me figure out what the problem actually is. TotalLight += calcSpotLight(spotLights,normal) TotalLight += calcPointLight(pointLights,normal) TotalLight += calcDirectionalLight(directionalLight, normal) Vec4 textureColor = texture(sampler, texCoord0.xy) This is likely the problematic code, but I'm not 100 percent sureĬolor = calcPointLight(spotLight.pointLight, normal) * Vec3 lightDirection = normalize(worldPos0 - ) įloat spotFactor = dot(lightDirection, spotLight.direction) Vec4 calcSpotLight(SpotLight spotLight, vec3 normal) Vec4 color = calcLight(pointLight.base, lightDirection, normal) įloat attenuation = + LightDirection = normalize(lightDirection)
![opengl java lwjgl fragment shader with lighting opengl java lwjgl fragment shader with lighting](https://i.stack.imgur.com/JsAG0.png)
Vec3 lightDirection = worldPos0 - pointLight.position įloat distanceToPoint = length(lightDirection) Vec4 calcPointLight(PointLight pointLight, vec3 normal) Return calcLight(directionalLight.base, -directionalLight.direction, normal) Vec4 calcDirectionalLight(DirectionalLight directionalLight, vec3 normal) SpecularColor = vec4(lor, 1.0) * specularIntensity * specularFactor SpecularFactor = pow(specularFactor, specularPower) Vec3 reflectDirection = normalize(reflect(direction, normal)) įloat specularFactor = dot(directionToEye, reflectDirection) Vec3 directionToEye = normalize(eyePos - worldPos0) If you fail to specify an output color in your fragment shader, the color buffer output for those fragments will be undefined (which usually means OpenGL will render them either black or white). Vec4 calcLight(BaseLight base, vec3 direction, vec3 normal)įloat diffuseFactor = dot(normal, -direction) ĭiffuseColor = vec4(lor, 1.0) * base.intensity * diffuseFactor The other exception is that the fragment shader requires a vec4 color output variable, since the fragment shaders needs to generate a final output color. Uniform DirectionalLight directionalLight I still don't know what the exact problem is, but that should narrow down the possible errors. It appears that the problem is indeed in the calcSpotLight function, but within the calcSpotLight function, the problem is the calcPointLight function. I just updated the driver and reran the program and indeed received no black pixels. Update: I have found that the problem was indeed in my graphics card's driver.
#OPENGL JAVA LWJGL FRAGMENT SHADER WITH LIGHTING CODE#
What I believe to be the problematic code is marked with a comment below in the function calcSpotLight. This can only mean that there is a bug in the fragment shader itself. This has only occurred when I modified the fragment shader to include spotlights. When I run the game, when viewing the mesh at certain distances and angles, namely close distances and sharp angles, I run into this interesting bug when certain pixels just go black. The example game currently running in the engine is a spotlight that follows the camera and a plane mesh with a tile texture.
![opengl java lwjgl fragment shader with lighting opengl java lwjgl fragment shader with lighting](https://i.stack.imgur.com/9kwuh.png)
I followed the tutorial for writing a fragment shader that utilizes spotlights in GLSL. Three consecutive floats give a 3D vertex Three consecutive vertices give a triangle.I've recently been following a tutorial for writing a game engine in Java using LWJGL 2.