4
$\begingroup$

I have wirtten a fragment shader that works just fine with a single light. Now I am trying to adpat 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...

enter image description here

////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;
}
$\endgroup$
  • $\begingroup$ show us what your output is and describe what you expect it to be $\endgroup$ – ratchet freak Jan 8 '16 at 14:33
  • $\begingroup$ It just wont compile. It is a version of a basic pixel ilumination shader. The previous version just supports 1 light. I am trying to adapt it so that it can process 8 lights. $\endgroup$ – eneko Jan 8 '16 at 14:44
  • $\begingroup$ we'll need to see the info log from the failed compilation/linking. It's a good practice to always log it while developing. $\endgroup$ – ratchet freak Jan 8 '16 at 14:55
  • $\begingroup$ I am pretty much just starting with shaders... I do 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) ... $\endgroup$ – eneko Jan 8 '16 at 15:01
2
$\begingroup$

From the error message the problem is in line 24 of your fragment shader:

vec3 transformedNormal = normalize(normalMatrix * normal);

normalMatrix is a mat4 but normal is a vec3.

The solution is to make normalMatrix a mat3 instead.

$\endgroup$
  • $\begingroup$ I do not get matrix errors anymore but It just wont compile. I have posted the original working code for a single light. How should I approach it? $\endgroup$ – eneko Jan 8 '16 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.