new lighting shaders

This commit is contained in:
Jakub Valtar
2014-07-05 03:37:25 +02:00
parent 6774d8996b
commit aee3d3c49c
5 changed files with 129 additions and 37 deletions
+31
View File
@@ -0,0 +1,31 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2011-13 Ben Fry and Casey Reas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
varying vec4 vertColor;
varying vec4 backVertColor;
void main() {
gl_FragColor = gl_FrontFacing ? vertColor : backVertColor;
}
+27 -17
View File
@@ -43,6 +43,7 @@ attribute vec4 emissive;
attribute float shininess;
varying vec4 vertColor;
varying vec4 backVertColor;
const float zero_float = 0.0;
const float one_float = 1.0;
@@ -82,17 +83,17 @@ void main() {
// Normal vector in eye coordinates
vec3 ecNormal = normalize(normalMatrix * normal);
if (dot(-one_float * ecVertex, ecNormal) < zero_float) {
// If normal is away from camera, choose its opposite.
// If we add backface culling, this will be backfacing
ecNormal *= -one_float;
}
vec3 ecNormalInv = ecNormal * -one_float;
// Light calculations
vec3 totalAmbient = vec3(0, 0, 0);
vec3 totalDiffuse = vec3(0, 0, 0);
vec3 totalSpecular = vec3(0, 0, 0);
vec3 totalFrontDiffuse = vec3(0, 0, 0);
vec3 totalFrontSpecular = vec3(0, 0, 0);
vec3 totalBackDiffuse = vec3(0, 0, 0);
vec3 totalBackSpecular = vec3(0, 0, 0);
for (int i = 0; i < 8; i++) {
if (lightCount == i) break;
@@ -118,24 +119,33 @@ void main() {
: one_float;
if (any(greaterThan(lightAmbient[i], zero_vec3))) {
totalAmbient += lightAmbient[i] * falloff;
totalAmbient += lightAmbient[i] * falloff;
}
if (any(greaterThan(lightDiffuse[i], zero_vec3))) {
totalDiffuse += lightDiffuse[i] * falloff * spotf *
lambertFactor(lightDir, ecNormal);
totalFrontDiffuse += lightDiffuse[i] * falloff * spotf *
lambertFactor(lightDir, ecNormal);
totalBackDiffuse += lightDiffuse[i] * falloff * spotf *
lambertFactor(lightDir, ecNormalInv);
}
if (any(greaterThan(lightSpecular[i], zero_vec3))) {
totalSpecular += lightSpecular[i] * falloff * spotf *
blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess);
totalFrontSpecular += lightSpecular[i] * falloff * spotf *
blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess);
totalBackSpecular += lightSpecular[i] * falloff * spotf *
blinnPhongFactor(lightDir, ecVertex, ecNormalInv, shininess);
}
}
// Calculating final color as result of all lights (plus emissive term).
// Transparency is determined exclusively by the diffuse component.
vertColor = vec4(totalAmbient, 0) * ambient +
vec4(totalDiffuse, 1) * color +
vec4(totalSpecular, 0) * specular +
vec4(emissive.rgb, 0);
vertColor = vec4(totalAmbient, 0) * ambient +
vec4(totalFrontDiffuse, 1) * color +
vec4(totalFrontSpecular, 0) * specular +
vec4(emissive.rgb, 0);
backVertColor = vec4(totalAmbient, 0) * ambient +
vec4(totalBackDiffuse, 1) * color +
vec4(totalBackSpecular, 0) * specular +
vec4(emissive.rgb, 0);
}
@@ -160,6 +160,11 @@ public class PGraphicsOpenGL extends PGraphics {
PGraphicsOpenGL.class.getResource("ColorFrag.glsl");
static protected URL defTextureShaderFragURL =
PGraphicsOpenGL.class.getResource("TextureFrag.glsl");
static protected URL defLightShaderFragURL =
PGraphicsOpenGL.class.getResource("LightFrag.glsl");
static protected URL defTexlightShaderFragURL =
PGraphicsOpenGL.class.getResource("TexlightFrag.glsl");
static protected URL defLineShaderVertURL =
PGraphicsOpenGL.class.getResource("LineVert.glsl");
static protected URL defLineShaderFragURL =
@@ -6740,7 +6745,7 @@ public class PGraphicsOpenGL extends PGraphics {
if (useDefault || !polyShader.checkPolyType(PShader.TEXLIGHT)) {
if (ppg.defTexlightShader == null) {
String[] vertSource = pgl.loadVertexShader(defTexlightShaderVertURL, 120);
String[] fragSource = pgl.loadFragmentShader(defTextureShaderFragURL, 120);
String[] fragSource = pgl.loadFragmentShader(defTexlightShaderFragURL, 120);
ppg.defTexlightShader = new PShader(parent, vertSource, fragSource);
}
shader = ppg.defTexlightShader;
@@ -6751,7 +6756,7 @@ public class PGraphicsOpenGL extends PGraphics {
if (useDefault || !polyShader.checkPolyType(PShader.LIGHT)) {
if (ppg.defLightShader == null) {
String[] vertSource = pgl.loadVertexShader(defLightShaderVertURL, 120);
String[] fragSource = pgl.loadFragmentShader(defColorShaderFragURL, 120);
String[] fragSource = pgl.loadFragmentShader(defLightShaderFragURL, 120);
ppg.defLightShader = new PShader(parent, vertSource, fragSource);
}
shader = ppg.defLightShader;
@@ -0,0 +1,36 @@
/*
Part of the Processing project - http://processing.org
Copyright (c) 2011-13 Ben Fry and Casey Reas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
uniform sampler2D texture;
uniform vec2 texOffset;
varying vec4 vertColor;
varying vec4 backVertColor;
varying vec4 vertTexCoord;
void main() {
gl_FragColor = texture2D(texture, vertTexCoord.st) * (gl_FrontFacing ? vertColor : backVertColor);
}
+28 -18
View File
@@ -45,6 +45,7 @@ attribute vec4 emissive;
attribute float shininess;
varying vec4 vertColor;
varying vec4 backVertColor;
varying vec4 vertTexCoord;
const float zero_float = 0.0;
@@ -85,17 +86,17 @@ void main() {
// Normal vector in eye coordinates
vec3 ecNormal = normalize(normalMatrix * normal);
if (dot(-one_float * ecVertex, ecNormal) < zero_float) {
// If normal is away from camera, choose its opposite.
// If we add backface culling, this will be backfacing
ecNormal *= -one_float;
}
vec3 ecNormalInv = ecNormal * -one_float;
// Light calculations
vec3 totalAmbient = vec3(0, 0, 0);
vec3 totalDiffuse = vec3(0, 0, 0);
vec3 totalSpecular = vec3(0, 0, 0);
vec3 totalFrontDiffuse = vec3(0, 0, 0);
vec3 totalFrontSpecular = vec3(0, 0, 0);
vec3 totalBackDiffuse = vec3(0, 0, 0);
vec3 totalBackSpecular = vec3(0, 0, 0);
for (int i = 0; i < 8; i++) {
if (lightCount == i) break;
@@ -121,27 +122,36 @@ void main() {
: one_float;
if (any(greaterThan(lightAmbient[i], zero_vec3))) {
totalAmbient += lightAmbient[i] * falloff;
totalAmbient += lightAmbient[i] * falloff;
}
if (any(greaterThan(lightDiffuse[i], zero_vec3))) {
totalDiffuse += lightDiffuse[i] * falloff * spotf *
lambertFactor(lightDir, ecNormal);
totalFrontDiffuse += lightDiffuse[i] * falloff * spotf *
lambertFactor(lightDir, ecNormal);
totalBackDiffuse += lightDiffuse[i] * falloff * spotf *
lambertFactor(lightDir, ecNormalInv);
}
if (any(greaterThan(lightSpecular[i], zero_vec3))) {
totalSpecular += lightSpecular[i] * falloff * spotf *
blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess);
}
totalFrontSpecular += lightSpecular[i] * falloff * spotf *
blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess);
totalBackSpecular += lightSpecular[i] * falloff * spotf *
blinnPhongFactor(lightDir, ecVertex, ecNormalInv, shininess);
}
}
// Calculating final color as result of all lights (plus emissive term).
// Transparency is determined exclusively by the diffuse component.
vertColor = vec4(totalAmbient, 0) * ambient +
vec4(totalDiffuse, 1) * color +
vec4(totalSpecular, 0) * specular +
vec4(emissive.rgb, 0);
vertColor = vec4(totalAmbient, 0) * ambient +
vec4(totalFrontDiffuse, 1) * color +
vec4(totalFrontSpecular, 0) * specular +
vec4(emissive.rgb, 0);
backVertColor = vec4(totalAmbient, 0) * ambient +
vec4(totalBackDiffuse, 1) * color +
vec4(totalBackSpecular, 0) * specular +
vec4(emissive.rgb, 0);
// Calculating texture coordinates, with r and q set both to one
vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0);
}