I'm currently in a situation to use Gouraud and Phong simple shaders with PointLights. But, instead, I want to introduce multiple SPOTLIGHT sources. But I'm not sure what changes I'm supposed to make to my new frag and vertex shader.
This are my current GLSL program:
Fragment.glsl
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
varying vec4 col;
void main() {
gl_FragColor = col;
}
Vertex.glsl
#define PROCESSING_LIGHT_SHADER
uniform mat4 modelview;
uniform mat4 transform;
uniform mat3 normalMatrix;
uniform vec4 lightPosition;
attribute vec4 vertex;
attribute vec4 color;
attribute vec3 normal;
varying vec4 col;
void main(){
gl_Position = transform*vertex;
vec3 vertexCamera = vec3(modelview * vertex);
vec3 transformedNormal = normalize(normalMatrix * normal);
vec3 dir = normalize(lightPosition.xyz - vertexCamera);
float light = max(0.0, dot(dir, transformedNormal));
col = vec4(light, light, light, 1)*color;
}
In processing you can use up t op 8 light sources. To access the light sources, you've to declare a Uniform with an array type, as described in the Processing Light shaders tutorial :
uniform vec4 lightPosition[8];
Set multiple light sources in the application program (eg 2):
pointLight(255, 255, 255, width/2, height, 200);
pointLight(255, 255, 255, width, height/2, 200);
Iterate through the (2) light sources and sum up the light in the shader program. eg:
void main() {
gl_Position = transform * position;
vec3 ecPosition = vec3(modelview * position);
vec3 ecNormal = normalize(normalMatrix * normal);
vertColor = vec4(0.0);
for (int i=0; i < 2; ++i)
{
vec3 direction = normalize(lightPosition[i].xyz - ecPosition);
float intensity = max(0.0, dot(direction, ecNormal));
vertColor += vec4(intensity, intensity, intensity, 1) * color;
}
}
Example with a green and red point light:
Application program
PShape can;
float angle;
PShader lightShader;
void setup() {
size(640, 360, P3D);
can = createCan(100, 200, 32);
lightShader = loadShader("lightfrag.glsl", "lightvert.glsl");
}
void draw() {
background(0);
shader(lightShader);
pointLight(0, 255, 0, width/2, height, 200);
pointLight(255, 0, 0, width, height/2, 200);
translate(width/2, height/2);
rotateY(angle);
shape(can);
angle += 0.01;
}
PShape createCan(float r, float h, int detail) {
textureMode(NORMAL);
PShape sh = createShape();
sh.beginShape(QUAD_STRIP);
sh.noStroke();
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;
}
Vertex shader
uniform mat4 modelview;
uniform mat4 transform;
uniform mat3 normalMatrix;
uniform vec4 lightPosition[8];
uniform vec3 lightDiffuse[8];
attribute vec4 position;
attribute vec4 color;
attribute vec3 normal;
varying vec4 vertColor;
void main() {
gl_Position = transform * position;
vec3 ecPosition = vec3(modelview * position);
vec3 ecNormal = normalize(normalMatrix * normal);
vertColor = vec4(0.0);
for (int i=0; i < 2; ++i)
{
vec3 direction = normalize(lightPosition[i].xyz - ecPosition);
float intensity = max(0.0, dot(direction, ecNormal));
vertColor += vec4(intensity * lightDiffuse[i], 1.0) * color;
}
}
Fragment shader
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
varying vec4 vertColor;
void main() {
gl_FragColor = vertColor;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.