I’m working on a BlinnPhong shader and it crashes. The way it is written below, it works. However, if you remove the line “SpecularHighlightAmount = 0.2f;” it will crash. This line of code is unwanted and is just there to prove it runs. I have no idea what’s going on here.
The problem would appear to be in the line before it " SpecularHighlightAmount = max(0.0, dot(ReflectedLightDirection, CameraDirection));". But that line does not crash as long as you then change the value as shown. So, it appears to be calculating an invalid floating point number.
I’ve been hacking at this all day and the code’s just getting worse as I continue to desperately try changing pretty much random things.
Since the problem is in the shader, I don’t seem to have any way to get an error message and I can’t step through it with the debugger.
#version 450 core
smooth in vec2 TextureCoordinates;
smooth in vec3 VertexNormal;
smooth in vec4 RGBAColor;
in vec3 WorldSpacePosition;
layout (location = 0) out vec4 OutputColor;
uniform vec4 AmbientLightColor;
uniform vec3 DiffuseLightDirection;
uniform vec3 DiffuseLightColor;
uniform vec3 CameraPosition;
uniform sampler2D Texture0;
vec3 PhongSpecular(in vec3 LightDirection, in vec3 LightColor, in vec3 PixelNormal, in vec3 CameraDirection, in float SpecularPower)
{
vec3 ReflectedLightDirection;
vec3 SpecularLight;
float SpecularHighlightAmount;
//SpecularLight = vec3(1.0f, 1.0f, 1.0f);
ReflectedLightDirection = 2.0f * PixelNormal * clamp(dot(PixelNormal, LightDirection), 0.0f, 1.0f) - LightDirection;
SpecularHighlightAmount = max(0.0, dot(ReflectedLightDirection, CameraDirection));
SpecularHighlightAmount = 0.2f;
//SpecularHighlightAmount = clamp(SpecularHighlightAmount, 0.0, 1.0);
SpecularLight = vec3(SpecularHighlightAmount, SpecularHighlightAmount, SpecularHighlightAmount);
//(SpecularHighlightAmount * LightColor;
return (SpecularLight);
}
void main()
{
vec3 LightDirection;
float DiffuseLightPercentage;
vec3 SpecularColor;
vec3 CameraDirection; //Float3 because the w component really doesn't belong in a 3D vector normal.
vec4 AmbientLight;
vec3 DiffuseLight;
vec4 InputColor;
//OutputColor = mix(texture(Texture0, TextureCoordinates), texture(Texture1, TextureCoordinates), MixTextures.r);
InputColor = texture(Texture0, TextureCoordinates);
LightDirection = -DiffuseLightDirection; //Normal must face into the light, rather than WITH the light to be lit up.
//DiffuseLightPercentage = max(dot(VertexNormal, LightDirection), 0.0); //Percentage is based on angle between the direction of light and the vertex's normal.
DiffuseLightPercentage = dot(VertexNormal, LightDirection); //Percentage is based on angle between the direction of light and the vertex's normal.
DiffuseLight = clamp((DiffuseLightColor * InputColor.xyz) * DiffuseLightPercentage, 0.0, 1.0); //Apply only the percentage of the diffuse color. Saturate clamps output between 0.0 and 1.0.
CameraDirection = normalize(CameraPosition - WorldSpacePosition); //Create a normal that points in the direction from the pixel to the camera.
if (DiffuseLightPercentage == 0.0f)
{
SpecularColor = vec3(0.0f, 0.0f, 0.0f);
}
else
{
SpecularColor = PhongSpecular(LightDirection, DiffuseLightColor, VertexNormal, CameraDirection, 0.3f);
}
OutputColor = RGBAColor * (AmbientLightColor * InputColor) + vec4(DiffuseLight, 1.0f) + vec4(SpecularColor, 1.0f);
//OutputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
};
Actually, it is throwing a “GL_INVALID_OPERATION” error when it attempts to compile (not as written but when I remove the magic line of code).
message: GL_INVALID_OPERATION error generated. Object is not a program or shader object.
type: ERROR
id: 1282
severity: HIGH