-1

i have written a fragment shader that works just fine with a single light. Now I am trying to adapt it to work with 8 lights, the implement it in Processing. Clearly I am doing something wrong in the math and I cannot see what it is... I have read other posts about this and try to adapt the answer to my problem, no luck though...

Working with only 1 light

////Fragment/////

    #ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

varying vec4 vertColor;
varying vec3 ecNormal;
varying vec3 lightDir;

void main() {  
  vec3 direction = normalize(lightDir);
  vec3 normal = normalize(ecNormal);
  float intensity = max(0.0, dot(direction, normal));
  gl_FragColor = vec4(intensity, intensity, intensity, 1) * vertColor;
}

////vertex/////

        #define PROCESSING_LIGHT_SHADER

uniform mat4 modelview;
uniform mat4 transform;
uniform mat3 normalMatrix;

uniform vec4 lightPosition;
uniform vec3 lightNormal;

attribute vec4 vertex;
attribute vec4 color;
attribute vec3 normal;

varying vec4 vertColor;
varying vec3 ecNormal;
varying vec3 lightDir;

void main() {
  gl_Position = transform * vertex;    
  vec3 ecVertex = vec3(modelview * vertex);  

  ecNormal = normalize(normalMatrix * normal);
  lightDir = normalize(lightPosition.xyz - ecVertex);  
  vertColor = color;
}
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
eneko
  • 333
  • 4
  • 14
  • Why do you say that you've clearly done something wrong? What does this code do? Are you getting any errors? Some unwanted behavior? Try being more specific. – Kevin Workman Jan 08 '16 at 14:25
  • It is a simple ilumination shader, per pixel. I have a working one, wich only supports 1 light. I am trying to make it support 8 lights. Seems like I am putting the loops where they shoud not be, or I am messing myself with the variables. If I run this, it just wont compile. – eneko Jan 08 '16 at 14:43
  • @eneko maybe try modifying the loop to `for(int i = 0 ; i<8;i++){ if(i – PeterT Jan 08 '16 at 14:52
  • @eneko If it doesn't compile, what specific errors do you get? – Kevin Workman Jan 08 '16 at 14:53
  • I get; Cannot compile frgament shader: 0(24) : error C1101: ambiguous overloaded function reference "mul(mat4, vec3") (0): mat3x4 mul(mat3x1, mat1x4) (0): mat3 mul(mat3x1, mat1x3) (0): mat3x2 mul(mat3x1, mat1x2) ... – eneko Jan 08 '16 at 14:56

1 Answers1

1

Just making it compile real quick with an online shader tool (http://shdr.bkcore.com/).

You might need to pass the attributes from the veretex shader to varyings for the fragment shader, but I'm not sure, been a while since I wrote shaders.

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

uniform mat4 modelview;
uniform mat4 normalMatrix;

uniform int lightCount;
uniform vec4 lightPosition[8];

varying vec4 vertex; //was attribute, no such thing in frag shaders
varying vec3 normal; //was attribute

varying vec4 vertColor;



void main() {  

    vec3 vertexCamera = vec3(modelview * vertex);
    vec3 transformedNormal = normalize(normalMatrix * vec4(normal,1)).xyz; //was vec3 = normalize(mat4*vec3);

float intensity = 0.0;

 for(int i = 0 ; i<8;i++){ //can't loop over a non-constant variable
    if(lightCount<i)
    { 
      vec3 direction = normalize(lightPosition[i].xyz - vertexCamera);
      intensity += max(0.0, dot(direction, transformedNormal));
    }
 }

 gl_FragColor = vec4(intensity, intensity, intensity, 1) * vertColor;
}
PeterT
  • 7,981
  • 1
  • 26
  • 34
  • Thanks! but not working... I have aposted the original working code for a single light. How sould I approach to get it working with 8 lights? – eneko Jan 08 '16 at 15:33