multiple lights vertex

edited April 2015 in GLSL / Shaders

Hello,

I have closer problem than http://forum.processing.org/two/discussion/comment/39054/#Comment_39054 my specific problem. i don't find a solution to split the position and the color of different lights. The result it' like an average position and color of lights.

Sketch
PShape can;
float angle;

PShader pixlightShader;

void setup() {
  size(640, 360, P3D);
  can = createCan(100, 200, 32);
  pixlightShader = loadShader("pixlightfrag.glsl", "pixlightvert.glsl");
}

void draw() {    
  background(0);

  shader(pixlightShader);

  // pointLight(0, 0, 0, mouseX, mouseY, 200);
  /*
  float redData = 255 *cos(frameCount *.01) ;
  float greenData = 255 *sin(frameCount *.02) ;
  float blueData = 255 *sin(frameCount *.001) ;
  */
    float redData = 255 ;
  float greenData = 255 ;
  float blueData = 255 ;
  Vec3 colorLight = new Vec3(redData, greenData, blueData) ;
  float posLightX = mouseX ;
  float posLightY = mouseY ;
  float posLightZ = 200 ;
  Vec3 posLight = new Vec3(posLightX, posLightY, posLightZ) ;
  float dirX = -1 ;
  float dirY = 0 ;
  float dirZ = 0 ;
  Vec3 dirLight = new Vec3(dirX, dirY, dirZ) ;
  float angle = PI/8 ;
  float concentration = 200; // try 1 > 1000

  spotLightShader(colorLight, posLight,dirLight, angle, concentration, pixlightShader) ;
  //pointLight(255, 255, 255, -mouseX, mouseY, 200);

  translate(width/2, height/2);
  rotateY(angle);  
  shape(can);  
  angle += 0.01;


}

//
void spotLightShader(Vec3 colorLight, Vec3 pos, Vec3 dir, float angle, float concentration, PShader s) {
   spotLight(0, 0, 0, pos.x, pos.y, pos.z, dir.x, dir.y, dir.z, angle, concentration) ;
    spotLight(0, 0, 0, pos.x, height -pos.y, pos.z, dir.x, dir.y, dir.z, angle, concentration) ;
      //transfert data color of light to shader
  PVector cyan = new PVector(colorLight.r,colorLight.g,colorLight.b/100) ;
  PVector yellow = new PVector(colorLight.r/100,colorLight.g,colorLight.b) ;
  s.set("colorLightZero", cyan) ;
  s.set("colorLightOne", yellow) ;
}





// object
PShape createCan(float r, float h, int detail) {
  textureMode(NORMAL);
  PShape sh = createShape();
  sh.beginShape(QUAD_STRIP);
  sh.noStroke();
  sh.fill(255,255,255) ;
  for (int i = 0; i <= detail; i++) {
    float angle = TWO_PI / detail;
    float x = sin(i * angle);
    float z = cos(i * angle);
    float u = float(i) / detail;
    sh.normal(x, 0, z);
    sh.vertex(x * r, -h/2, z * r, u, 0);
    sh.vertex(x * r, +h/2, z * r, u, 1);    
  }
  sh.endShape(); 
  return sh;
}


class Vec2 {

  // inspireted by GLSL code
  float x,y ;
  float s,t ;
  Vec2(float x, float y) {
    this.x = x ;
    this.y = y ;

    this.s = x ;
    this.t = y ;
  }
}

class Vec3 {
  // inspireted by GLSL code
  float x,y,z ;
  float r, g, b ;
  float s, t, p ;
  Vec3(float x, float y, float z) {
    this.x = x ;
    this.y = y ;
    this.z = z ;

    this.r = x ;
    this.g = y ;
    this.b = z ;

    this.s = x ;
    this.t = y ;
    this.p = z ;
  }
}

class Vec4 {

  // inspireted by GLSL code
  float x,y,z,w ;
  float r, g, b, a ;
  float s, t, p, q ;
  Vec4(float x, float y, float z, float w) {
    this.x = x ;
    this.y = y ;
    this.z = z ;
    this.w = w ;

    this.r = x ;
    this.g = y ;
    this.b = z ;
    this.a = w ;

    this.s = x ;
    this.t = y ;
    this.p = z ;
    this.q = w ;
  }
}

Frag

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


const int numLights = 2 ;

varying vec4 vertColor;
varying vec3 ecNormal;
varying vec3 lightDir[8];
void main() {

vec4 final_color ;
for(int i = 0 ; i <  numLights ; i++) {
  vec3 direction = normalize(lightDir[i]);
  vec3 normal = normalize(ecNormal);
  float intensity = max(0.0, dot(direction, normal));

  final_color += vec4(intensity, intensity, intensity, 1.)  ;

  }  
  gl_FragColor = final_color *vertColor;

}

Vert

#define PROCESSING_LIGHT_SHADER
const int numLights = 2 ;

//from Processing univers
uniform mat4 modelview;
uniform mat4 transform;
uniform mat3 normalMatrix;

//from Processing light
uniform int lightCount;
uniform vec4 lightPosition[8];
uniform vec3 lightNormal[8];


// from your sketch
uniform vec3 colorLightZero;
uniform vec3 colorLightOne;

vec3 colorLight[8] ;

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

varying vec4 vertColor;
varying vec3 ecNormal;
varying vec3 lightDir[8];

void colorLight() {
  colorLight[0] = colorLightZero ;
  colorLight[1] = colorLightOne ;
  }

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



  ecNormal = normalize(normalMatrix *normal);



  vec3 lightPositionFinal ;
  vec3 normColorLight ;
  vec4 finalColor ;
  colorLight() ;
  for(int i = 0 ;i < numLights ;i++) { 
    // lightPositionFinal =   vec3(lightPositionFinal +lightPosition[i].xyz ); 
    // lightDir = normalize(lightPositionFinal.xyz - ecVertex);
     lightDir[i] = normalize(lightPosition[i].xyz - ecVertex);
       //final position



  // color
  normColorLight += vec3(colorLight[i]/255.) ; // here, we divide by 255 because it's a max of the color data in the RGB world




  // final

    }

  normColorLight /= float(numLights) ;
  vec3 newColorRGB = vec3(color.rgb *normColorLight) ;
  finalColor = vec4(newColorRGB, color.a) ;
  vertColor = finalColor ;

}
Sign In or Register to comment.