1
\$\begingroup\$

First off, this is a Uni assignment, but the lecturer is stumped too.

The light (attached to the mouse) only works when inside of the object (a sphere in this case). I've tried multiplying the normals of the sphere by -1 and the distance of the light to the sphere by -1 also. I have no idea what the cause of this is.

Here is the fragment shader:

const float     lightWeighting = 0.75;
const float     textureWeighting = 0.25;


const float     PI = 3.14;
const float     lightIntensity = 1.0;
const float     constFudge = 0.025;
const float     linearFudge = 0.025;
const float     quadraticFudge = 0.05;

float rend  = 250.0;
float rstart = 50.0;


uniform float       Intensity;
uniform sampler2D   grabTexture;
//varying   sampler2D   HeatValues

varying vec3        Normal;
varying vec3        Vertex;
varying vec2        texCoord;

void main(void)
{   
  vec3 pigPos = Vertex;
  vec3 normPigPos = normalize(pigPos);


  vec3 lightPos = vec3(gl_LightSource[0].position.xyz);

  vec3 normLightPos = normalize(lightPos);




  float effectiveIntesity;


  vec3 normNormal = normalize(vec3(Normal.xyz));

  vec3 t = vec3(pigPos - lightPos);

  float distToLight = length(t); //I belive this is where it is going wrong

  float d = dot(normNormal.xyz, normLightPos.xyz);
  if(d > 0.0)//facing the light
  {





    effectiveIntesity = d*lightIntensity * (1.0/(constFudge + (linearFudge*distToLight) +(quadraticFudge*distToLight*distToLight)));

  }
  else
  {
    effectiveIntesity = 0.0;
  }

  //TODO: change the colour of the pixel
  vec4 lightingColour = vec4(vec4( 1.0 ) * effectiveIntesity); //TODO: improve

  gl_FragColor = lightingColour;


}

Vertex Shader as requested:

//I am surprisingly important


uniform float   Intensity;
varying vec3    Normal;
varying vec3    Vertex;
varying vec2    texCoord;

void main(void)
{   
    gl_Position     = ftransform();
    texCoord    = gl_MultiTexCoord0.xy;

    Normal = vec3(gl_Normal.xyz);
    Vertex = vec3(gl_Vertex.xyz);

}

Normals are as loaded from the obj file which was produced with blender

\$\endgroup\$
4
  • \$\begingroup\$ Is there a vertex shader also? If so, please attach it. And if you're calculating normals on the CPU, please include the code that calculates them. \$\endgroup\$ Jul 15, 2017 at 18:28
  • \$\begingroup\$ vertex shader added, ~~one day I'll understand how the box thing works~~ 4 space indent \$\endgroup\$
    – Lupus590
    Jul 15, 2017 at 18:57
  • 1
    \$\begingroup\$ Please paste the shader you want to actually get working without all the extra spam test code like vec3 lP = vec3(abs(gl_LightSource[0].position.x/100), abs(gl_LightSource[0].position.y/100), abs(gl_LightSource[0].position.z/100)); \$\endgroup\$ Jul 16, 2017 at 2:23
  • \$\begingroup\$ removed all but lighting stuff from fragment shader \$\endgroup\$
    – Lupus590
    Jul 17, 2017 at 10:05

2 Answers 2

0
\$\begingroup\$

The distance from the light calculation was the issue.

I still get illumination from inside of the object, but I'm not bothered about that.

Here is the fragment shader with the change:

const float     lightWeighting = 0.75;
const float     textureWeighting = 0.25;


const float     PI = 3.14;
const float     lightIntensity = 1.0;
const float     constFudge = 0.025;
const float     linearFudge = 0.025;
const float     quadraticFudge = 0.05;

float rend  = 250.0;
float rstart = 50.0;


uniform float       Intensity;
uniform sampler2D   grabTexture;
//varying   sampler2D   HeatValues

varying vec3        Normal;
varying vec3        Vertex;
varying vec2        texCoord;

void main(void)
{   
  vec3 pigPos = Vertex;
  vec3 normPigPos = normalize(pigPos);


  vec3 lightPos = vec3(gl_LightSource[0].position.xyz);

  vec3 normLightPos = normalize(lightPos);




  float effectiveIntesity;


  vec3 normNormal = normalize(vec3(Normal.xyz));

  vec3 t = vec3(pigPos - lightPos);

  ////////////////////////////
  // THE FIX
  ////////////////////////////

  float distToLight = length(t)/100; //the divide by 100 fixed it (I probably need to make the light brighter)

  ////////////////////////////
  // END OF FIX
  ////////////////////////////

  float d = dot(normNormal.xyz, normLightPos.xyz);
  if(d > 0.0)//facing the light
  {





    effectiveIntesity = d*lightIntensity * (1.0/(constFudge + (linearFudge*distToLight) +(quadraticFudge*distToLight*distToLight)));

  }
  else
  {
    effectiveIntesity = 0.0;
  }

  //TODO: change the colour of the pixel
  vec4 lightingColour = vec4(vec4( 1.0 ) * effectiveIntesity); //TODO: improve

  gl_FragColor = lightingColour;


}
\$\endgroup\$
0
\$\begingroup\$

This may not be the whole issue, but I do see some problems

float d = dot(normNormal.xyz, normLightPos.xyz);

normLightPos looks pointless to me - it is the direction of the light from the center of the world

what you really want (I think) is

float d = dot(normNormal.xyz, normalize(t));

to compare the surface normal with the angle of the light.

I'd also strongly recommend trying this out in isolation from the distance/falloff check - as in, using "d" directly as the brightness of a pixel.

When you have good lighting from just the angle, add in linear brightness falloff - then afterwards upgrade to the more complex falloff you are attempting

It may help testing to have several spheres scattered around the scene, so you can see the effects of the lighting on nearby and far-off objects

\$\endgroup\$
3
  • \$\begingroup\$ I tried it just now, and it worked worse than the fix I managed to discover here: gamedev.stackexchange.com/a/145029/98617 \$\endgroup\$
    – Lupus590
    Jul 20, 2017 at 13:31
  • \$\begingroup\$ Lighting based on just the angle? or making just that change and leaving everything else in place? It really is important to get each step working correctly, without it you may be able to find a balance that works under some conditions, but try moving the light and/or the mesh and you may see issues \$\endgroup\$
    – Chozabu
    Jul 20, 2017 at 22:53
  • \$\begingroup\$ to explain a little more, your current dot-product treats the light angle as if it was at an infinite distance (in the direction of the lights position relative to 0,0,0) - good for simulating sunlight, but not so good for a point light \$\endgroup\$
    – Chozabu
    Jul 20, 2017 at 22:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .