summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorphil <[email protected]>2016-10-27 20:37:23 +1300
committerphil <[email protected]>2016-10-27 20:37:23 +1300
commit033a48d56007ece591b42eb1bc30d9f2d13350d0 (patch)
tree853652847eea95aeb1fb41555c392b4520005b07
parent57d6bb978ae0327f01dc098e434932d7e6d29766 (diff)
added the fixed_function_shader vert and frag so there is at least one
publish location for the examples
-rw-r--r--src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/fixed_function_shader.frag116
-rw-r--r--src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/fixed_function_shader.vert123
2 files changed, 239 insertions, 0 deletions
diff --git a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/fixed_function_shader.frag b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/fixed_function_shader.frag
new file mode 100644
index 0000000..bdc0fe0
--- /dev/null
+++ b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/fixed_function_shader.frag
@@ -0,0 +1,116 @@
+#version 140
+
+precision mediump float;
+
+
+uniform int alphaTestEnabled;
+uniform int alphaTestFunction;
+uniform float alphaTestValue;
+
+uniform int fogEnabled;
+uniform vec4 expColor;
+uniform float expDensity;
+uniform vec4 linearColor;
+uniform float linearStart;
+uniform float linearEnd;
+
+//End of FFP inputs
+in vec2 glTexCoord0;
+
+uniform sampler2D BaseMap;
+
+in vec3 LightDir;
+in vec3 ViewDir;
+
+in vec3 N;
+
+in vec4 A;
+in vec4 C;
+in vec4 D;
+
+
+in vec3 emissive;
+in vec3 specular;
+in float shininess;
+
+
+
+out vec4 glFragColor;
+
+void main( void )
+{
+ vec4 baseMap = texture( BaseMap, glTexCoord0.st );
+
+ //web says the keyword discard in a shader is bad
+ //I could just gl_FragColor=vec(0,0,0,0); return;
+ if(alphaTestEnabled != 0)
+ {
+ if(alphaTestFunction==516)//>
+ if(baseMap.a<=alphaTestValue)discard;
+ else if(alphaTestFunction==518)//>=
+ if(baseMap.a<alphaTestValue)discard;
+ else if(alphaTestFunction==514)//==
+ if(baseMap.a!=alphaTestValue)discard;
+ else if(alphaTestFunction==517)//!=
+ if(baseMap.a==alphaTestValue)discard;
+ else if(alphaTestFunction==513)//<
+ if(baseMap.a>=alphaTestValue)discard;
+ else if(alphaTestFunction==515)//<=
+ if(baseMap.a>alphaTestValue)discard;
+ else if(alphaTestFunction==512)//never
+ discard;
+ }
+
+ vec3 normal = N;
+
+ vec3 L = normalize(LightDir);
+ vec3 E = normalize(ViewDir);
+ vec3 R = reflect(-L, normal);
+ vec3 H = normalize( L + E );
+
+ float NdotL = max( dot(normal, L), 0.0 );
+ float NdotH = max( dot(normal, H), 0.0 );
+ float EdotN = max( dot(normal, E), 0.0 );
+ float NdotNegL = max( dot(normal, -L), 0.0 );
+
+ vec4 color;
+ vec3 albedo = baseMap.rgb * C.rgb;
+ vec3 diffuse = A.rgb + (D.rgb * NdotL);
+
+
+ // Specular
+ vec3 spec = specular * pow(NdotH, 0.3*shininess);
+ spec *= D.rgb;
+
+ color.rgb = albedo * (diffuse + emissive) + spec;
+ color.a = C.a * baseMap.a;
+
+ if(fogEnabled == 1)
+ {
+ //distance
+ float dist = 0.0;
+ float fogFactor = 0.0;
+
+ //compute distance used in fog equations
+ dist = length(ViewDir);
+
+ if(linearEnd > 0.0)//linear fog
+ {
+ fogFactor = (linearEnd - dist)/(linearEnd - linearStart);
+ fogFactor = clamp( fogFactor, 0.0, 1.0 );
+
+ //if you inverse color in glsl mix function you have to put 1.0 - fogFactor
+ color = mix(linearColor, color, fogFactor);
+ }
+ else if( expDensity > 0.0)// exponential fog
+ {
+ fogFactor = 1.0 /exp(dist * expDensity);
+ fogFactor = clamp( fogFactor, 0.0, 1.0 );
+
+ // mix function fogColor-(1-fogFactor) + lightColor-fogFactor
+ color = mix(expColor, color, fogFactor);
+ }
+ }
+
+ glFragColor = color;
+}
diff --git a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/fixed_function_shader.vert b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/fixed_function_shader.vert
new file mode 100644
index 0000000..184620e
--- /dev/null
+++ b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/fixed_function_shader.vert
@@ -0,0 +1,123 @@
+#version 150
+//#version 120 is not optional, trouble otherwise
+
+//Note don't put if else constructs on one line or trouble
+
+in vec4 glVertex;
+in vec4 glColor;
+in vec3 glNormal;
+in vec2 glMultiTexCoord0;
+
+uniform mat4 glProjectionMatrix;
+//uniform mat4 glProjectionMatrixInverse;
+uniform mat4 glViewMatrix;
+uniform mat4 glModelMatrix;
+//uniform mat4 glModelViewMatrix;
+//uniform mat4 glModelViewMatrixInverse;
+//uniform mat4 glModelViewProjectionMatrix;
+
+//uniform mat3 glNormalMatrix;
+
+//uniform vec4 glFrontMaterialambient;
+uniform vec4 glFrontMaterialdiffuse;
+uniform vec4 glFrontMaterialemission;
+uniform vec3 glFrontMaterialspecular;
+uniform float glFrontMaterialshininess;
+uniform int ignoreVertexColors;
+
+uniform vec4 glLightModelambient;
+
+uniform vec4 glLightSource0position;
+uniform vec4 glLightSource0diffuse;
+
+uniform mat4 textureTransform;
+
+//uniform int alphaTestEnabled;
+//uniform int alphaTestFunction;
+//uniform float alphaTestValue;
+
+
+//uniform int fogEnabled;
+//uniform vec4 expColor;
+//uniform float expDensity;
+//uniform vec4 linearColor;
+//uniform float linearStart;
+//uniform float linearEnd;
+
+//End of FFP inputs
+//The line above in not optional for parsing reasons
+
+//Fixed function pipeline pre-calculated values not available
+//vec3 halfVector = normalize(vec3(gl_LightSource[0].halfVector));
+//http://stackoverflow.com/questions/3744038/what-is-half-vector-in-modern-glsl
+// vec3 ecPos = vec3(glModelViewMatrix * glVertex);
+// vec3 ecL;
+// if( glLightSource0position.w == 0.0)
+// ecL = vec3(glLightSource0position.xyz);// no -ecPos in case of dir lights?
+// else
+// ecL = vec3(glLightSource0position.xyz - ecPos);
+// vec3 L = normalize(ecL.xyz);
+// vec3 V = -ecPos.xyz;
+// vec3 halfVector = normalize(L + V);
+
+// gl_FrontLightModelProduct.sceneColor // Derived. Ecm + Acm * Acs (Acs is normal glLightModelambient)
+// use vec4 sceneColor = glFrontMaterialemission + glFrontMaterialambient * glLightModelambient;
+
+
+//gl_LightSource[i].specular
+//use glFrontMaterialspecular
+
+//gl_LightSource[i].ambient
+//use glLightModelambient
+
+//gl_FrontLightProduct[i]
+//vec4 ambient; // Acm * Acli (Acli does not exist)
+//vec4 diffuse; // Dcm * Dcli
+//vec4 specular; // Scm * Scli (Scli does not exist)
+// calculate yourself
+
+
+
+
+out vec2 glTexCoord0;
+
+out vec3 LightDir;
+out vec3 ViewDir;
+
+out vec3 N;
+
+out vec4 A;
+out vec4 C;
+out vec4 D;
+
+out vec3 emissive;
+out vec3 specular;
+out float shininess;
+
+void main( void )
+{
+ mat4 glModelViewMatrix = glViewMatrix*glModelMatrix;
+ gl_Position = glProjectionMatrix*glModelViewMatrix * glVertex;//glModelViewProjectionMatrix * glVertex;
+
+ glTexCoord0 = (textureTransform * vec4(glMultiTexCoord0,0,1)).st;
+
+ mat3 glNormalMatrix = mat3(transpose(inverse(glModelViewMatrix)));
+ N = normalize(glNormalMatrix * glNormal);
+
+ vec3 v = vec3(glModelViewMatrix * glVertex);
+
+ ViewDir = -v.xyz;
+ LightDir = glLightSource0position.xyz;
+
+ A = glLightModelambient;
+ if( ignoreVertexColors != 0)
+ C = glFrontMaterialdiffuse; // objectColor should be used if it is no lighting
+ else
+ C = glColor;
+ D = glLightSource0diffuse * glFrontMaterialdiffuse;
+
+ emissive = glFrontMaterialemission.rgb;
+ specular = glFrontMaterialspecular;
+ shininess = glFrontMaterialshininess;
+
+}