我目前处于将Gouraud和Phong简单着色器与PointLights结合使用的情况。但是,我想介绍多个SPOTLIGHT源。但是我不确定应该对新的碎片和顶点着色器进行哪些更改。
这是我当前的GLSL程序:
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;
}
在处理中,您最多可以使用t op 8光源。要访问光源,必须声明Uniform并使用数组类型,如Processing Light shaders tutorial中所述:
uniform vec4 lightPosition[8];
在应用程序中设置多个光源(例如2):
pointLight(255, 255, 255, width/2, height, 200);
pointLight(255, 255, 255, width, height/2, 200);
迭代(2)光源,并在着色器程序中求和。例如:
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;
}
}
带有绿色和红色点灯的示例:
应用程序
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;
}
顶点着色器
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;
}
}
片段着色器
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
varying vec4 vertColor;
void main() {
gl_FragColor = vertColor;
}