0

Here is a picture of the program running:

enter image description here

I can't figure out why my plane is getting a bullseye coloring, I'm pretty sure I'm doing something wrong with the shaders but I'm not entirely sure what's the problem.

this is my fragment shader.

#version 430 core

in vec4 color;
in vec4 position;
uniform float fTime;
uniform vec3 lookat;
out vec4 fColor;

vec4 calculateMyNormal(vec4 mposition)
{
   float dfdx = 2*(mposition.x) * 4 * cos(radians((mposition.x*mposition.x)+          (mposition.z*mposition.z)+fTime));
   float dfdz = 2*(mposition.z) * 4 * cos(radians((mposition.x*mposition.x)+(mposition.z*mposition.z)+fTime));

    vec3 a = vec3(1, dfdx, 0);
    vec3 b = vec3(0, dfdz, 1);

    vec3 normal = normalize(cross(a, b));

    return vec4(normal, 1.0);
}

vec4 ADSLightModel(vec4 myNormal, vec4 myPosition)
{
    const vec4 myLightPosition     = vec4(1.0, 0.5, 0.0, 1.0 );

    const vec4 myLightAmbient      = vec4( 0.2, 0.2, 0.2, 1.0 );
    const vec4 myLightDiffuse      = vec4( 1.0 , 1.0 , 1.0, 1.0 ); 
    const vec4 myLightSpecular     = vec4( 1.0 , 1.0 , 1.0 , 1.0);

    const vec4 myMaterialAmbient   = vec4( 1.0 , 0.5, 0.0, 1.0 ); 
    const vec4 myMaterialDiffuse   = vec4( 0.5 , 0.1, 0.5, 1.0 ); 
    const vec4 myMaterialSpecular  = vec4( 0.6, 0.6, 0.6, 1.0 );

    const float myMaterialShininess = 80;

    vec4 norm = normalize( myNormal );
    vec4 lightv = normalize( myLightPosition - myPosition );
    vec4 viewv  = normalize( vec4(lookat, 1.0) - myPosition );
    vec4 refl   = reflect( vec4(lookat, 1.0) - lightv, norm ); 
    vec4 ambient = myMaterialAmbient*myLightAmbient;

    vec4 diffuse = max(0.0, dot(lightv, norm)) * myMaterialDiffuse * myLightDiffuse;

    vec4 specular = vec4( 0.0, 0.0, 0.0, 1.0 );
    if( dot(lightv, viewv) > 0)
    {
        specular = pow(max(0.0, dot(viewv,refl)), myMaterialShininess)*myMaterialSpecular* myLightSpecular; 
    }

    return clamp(ambient + diffuse + specular, 0.0, 1.0);
}

void main()
{
    vec4 norml = calculateMyNormal(position);
    fColor = ADSLightModel(norml, position);
}

the plane moves and I do that in the vertex shader, I don't know if that might be the problem.

#version 430 core
layout (location = 0) in vec4 vPosition;

uniform float fTime;
uniform mat4 mTransform;
out vec4 color;
out vec4 position;

float calculaY(float x, float z, float time) 
{
    return  0.5 * sin(time + (x*x + z*z) / 50.0);
}

void main()
{
    vec4 vNewpos = vPosition;
    vNewpos.y = calculaY(vNewpos.x, vNewpos.z, fTime);  
    color = vec4(0.0, 0.0, 1.0, 1.0);
    position  = vNewpos;
    gl_Position = mTransform * vNewpos;
}

The last thing I can imagine being wrong, would be the normals, but I'm using a the code of my teacher to generate the plane and his plane had a solid color all over the plane so either he did something wrong and fixed it or as I think, the problem is in my shaders.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97

1 Answers1

0

Your reflection vector does not really make sense:

vec4 refl   = reflect( vec4(lookat, 1.0) - lightv, norm );

There are a couple of things which should make you suspicious:

  1. refl is not normalized. The reflect operation will preserve the length of the input vector, but the input vec4(lookat, 1.0) - lightv is not normalized.
  2. The value of vec4(lookat, 1.0) - lightv references a point, not a direction vector, since it is the difference between a point and another direction vector.
  3. The term vec4(lookat, 1.0) - lightv does not make sense geometrically. What you want is the reflection of the light incidence vector lightv around the normal. The viewing position is totally irrelevant for determining the direction an incident light ray will be reflected to at some surface point.

The reflection vector should just be:

refl = reflect(lightv, normal);
derhass
  • 43,833
  • 2
  • 57
  • 78