0

I have managed to implement a garoud shader with specular lighting efects in Processing 3.0 . Now I am trying with a fragment Phong shader but cannot make it work. I can´t find where is the error.

Sketch:

import java.io.*;

PShader phongShader;//Shader declaratiosns
int depth; //Deph of Z position for the light  

void settings() {
  size(640, 480, P3D);
}

void setup() {
  phongShader = loadShader("phongFrag.glsl", "phongVert.glsl");
  depth = 150;
}

void draw() {

  background(0);
  shader(phongShader);

  pointLight(255, 0, 0, mouseX, mouseY, depth); //Point light following mouse
  pushMatrix();  
  translate(width/2, height/2, 0);
  sphere(100);
  popMatrix();
  depth();
}

void depth() {

  if (keyPressed) {
    if (key == 'z') {
      depth = depth +5;
    } else if (key == 'x') {
      depth = depth -5;
    }
  }
}

Vertex Shader:

#define PROCESSING_LIGHT_SHADER

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



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

varying vec4 vertColor;

varying vec3 transformedNormal;
varying vec3 vertexCamera;

void main() {
  gl_Position = transform * vertex;    

  vertexCamera = vec3(modelview * vertex);  
  transformedNormal = normalize(normalMatrix * normal);
  vertColor = color;
}

Fragement Shader:

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

uniform vec3 lightPosition;
uniform vec3 eye_Position;

varying vec4 vertColor;
varying vec3 lightDir;

varying vec3 transformedNormal; //world pos
varying vec3 vertexCamera; // world normal

void main() {  

 vec3 normalizedPos =  normalize(lightPosition.xyz -vertexCamera);
 vec3 direction = normalize(eye_Position - vertexCamera); 


 float intensity = 0.0;
 float specular = 0.0;


 float LdotN = max(0, dot(normalizedPos,direction));
 float diffuse = 1 * LdotN;

 vec3 R = -normalize(reflect(normalizedPos,transformedNormal));
 specular = pow( max(0, dot( R, direction)), 16);


  intensity += (diffuse + specular);

  gl_FragColor = vec4(intensity, intensity, intensity, 1) * vertColor;
}
eneko
  • 333
  • 4
  • 14
  • What exactly does this code do? What exactly do you mean when you say it doesn't work? Are you getting an error? What does this program do? – Kevin Workman Jan 11 '16 at 21:34
  • It is a shader implementing the phong illimination model, diffuse and specular components of light. It compiles and no errors do appear. It just does nothing.... – eneko Jan 11 '16 at 21:36
  • 1
    Can you post your sketch, preferably as an [MCVE](http://stackoverflow.com/help/mcve)? – Kevin Workman Jan 11 '16 at 21:37
  • Posted. It is a pretty straight forward sketch... I don't think the problem has something to do with it. – eneko Jan 11 '16 at 21:40

0 Answers0