为什么这个着色器不起作用?

时间:2016-01-11 21:30:22

标签: glsl shader processing fragment-shader pixel-shader

我已经设法在Processing 3.0中实现了具有镜面光照效果的garoud着色器。现在我正在尝试使用片段Phong着色器,但无法使其工作。我找不到错误在哪里。

草图:

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;
    }
  }
}

顶点着色器:

#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;
}

0 个答案:

没有答案