aboutsummaryrefslogtreecommitdiffstats
path: root/src/classes/org
diff options
context:
space:
mode:
authorphil <[email protected]>2016-10-16 18:43:57 +1300
committerphil <[email protected]>2016-10-16 18:43:57 +1300
commitf6bba9ab4f959e7a5ae13445e030a4d92f4293db (patch)
treea96e9c7ef9ea3d6d768a2198d76da849a5cf257e /src/classes/org
parent6e59ac5f89bcd8cbb0b05653c56a0454963984c8 (diff)
GL2ES2 examples improved, the halfVector calculations and the sceneColor
are now closer to correctly reproducing the fixed pipeline values (though they are still not exactly identical)
Diffstat (limited to 'src/classes/org')
-rw-r--r--src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/SphereGLSL.java49
-rw-r--r--src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.frag6
-rw-r--r--src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.vert58
-rw-r--r--src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/phong.frag29
-rw-r--r--src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/phong.vert11
-rw-r--r--src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/simple.frag2
-rw-r--r--src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/simple.vert9
-rw-r--r--src/classes/org/jdesktop/j3d/examples/glsl_shader/SphereGLSL.java4
-rw-r--r--src/classes/org/jdesktop/j3d/examples/glsl_shader/simple.frag3
-rw-r--r--src/classes/org/jdesktop/j3d/examples/glsl_shader/simple.vert2
10 files changed, 132 insertions, 41 deletions
diff --git a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/SphereGLSL.java b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/SphereGLSL.java
index f49c42a..61069f7 100644
--- a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/SphereGLSL.java
+++ b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/SphereGLSL.java
@@ -81,7 +81,6 @@ import org.jogamp.java3d.Transform3D;
import org.jogamp.java3d.TransformGroup;
import org.jogamp.java3d.TriangleStripArray;
import org.jogamp.java3d.utils.geometry.Sphere;
-//import org.jogamp.java3d.utils.geometry.Sphere;
import org.jogamp.java3d.utils.shader.StringIO;
import org.jogamp.java3d.utils.universe.SimpleUniverse;
import org.jogamp.vecmath.Color3f;
@@ -103,7 +102,7 @@ public class SphereGLSL extends javax.swing.JFrame
// Flag indicates type of lights: directional, point, or spot
// lights. This flag is set based on command line argument
- private static int lightType = DIRECTIONAL_LIGHT;
+ private static int lightType = POINT_LIGHT;//DIRECTIONAL_LIGHT;
private SimpleUniverse univ = null;
private BranchGroup scene = null;
@@ -198,16 +197,19 @@ public class SphereGLSL extends javax.swing.JFrame
ColoringAttributes caL2 = new ColoringAttributes();
caL1.setColor(lColor1);
caL2.setColor(lColor2);
- Appearance appL1 = new Appearance();
- Appearance appL2 = new Appearance();
+
+ Appearance appL1 = makeGouraudShaderAppearance();
+ Appearance appL2 = makeGouraudShaderAppearance();
appL1.setColoringAttributes(caL1);
appL2.setColoringAttributes(caL2);
Sphere sph2 = new Sphere(0.05f, appL1);
makeNIO(sph2);
+
l1Trans.addChild(sph2);
Sphere sph3 = new Sphere(0.05f, appL2);
makeNIO(sph3);
+
l2Trans.addChild(sph3);
// Create lights
@@ -283,7 +285,40 @@ public class SphereGLSL extends javax.swing.JFrame
return objRoot;
}
-
+ /**
+ * GL2ES2: this should be a trivial emissive color shader, but gouraud will do for now
+ * @return
+ */
+ private Appearance makeGouraudShaderAppearance()
+ {
+ // Create a Sphere object, generate one copy of the sphere,
+ // and add it into the scene graph.
+ ShaderAppearance a = new ShaderAppearance();
+ Material m = new Material();
+ m.setLightingEnable(true);
+ String vertexProgram = null;
+ String fragmentProgram = null;
+ try
+ {
+ vertexProgram = StringIO.readFully(
+ new File(System.getProperty("user.dir") + "/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.vert"));
+ fragmentProgram = StringIO.readFully(
+ new File(System.getProperty("user.dir") + "/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.frag"));
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ Shader[] shaders = new Shader[2];
+ shaders[0] = new SourceCodeShader(Shader.SHADING_LANGUAGE_GLSL, Shader.SHADER_TYPE_VERTEX, vertexProgram);
+ shaders[1] = new SourceCodeShader(Shader.SHADING_LANGUAGE_GLSL, Shader.SHADER_TYPE_FRAGMENT, fragmentProgram);
+ ShaderProgram shaderProgram = new GLSLShaderProgram();
+ shaderProgram.setShaders(shaders);
+
+ a.setShaderProgram(shaderProgram);
+ a.setMaterial(m);
+ return a;
+ }
private Canvas3D createUniverse()
{
@@ -374,9 +409,7 @@ public class SphereGLSL extends javax.swing.JFrame
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel drawingPanel;
-
-
-
+
// End of variables declaration//GEN-END:variables
public static void makeNIO(Sphere sph)
{
diff --git a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.frag b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.frag
index 9169661..c02a268 100644
--- a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.frag
+++ b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.frag
@@ -44,7 +44,11 @@
// Simple GLSL fragment program to add the primary and secondary (specular) colors
+//GL2ES2: varying color data needs to be defined
+varying vec4 glFrontColor;
+varying vec4 glFrontSecondaryColor;
+
void main()
{
- gl_FragColor = gl_Color + gl_SecondaryColor;
+ gl_FragColor = glFrontColor + glFrontSecondaryColor;
}
diff --git a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.vert b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.vert
index 900f598..81d9c95 100644
--- a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.vert
+++ b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/gouraud.vert
@@ -54,15 +54,42 @@ uniform mat4 glModelViewMatrix;
uniform mat4 glModelViewProjectionMatrix;
uniform mat3 glNormalMatrix;
+uniform vec4 glFrontMaterialambient;
+uniform vec4 glFrontMaterialdiffuse;
+uniform vec4 glFrontMaterialemission;
+uniform vec3 glFrontMaterialspecular;
+uniform float glFrontMaterialshininess;
+
+uniform vec4 glLightModelambient;
+
+uniform vec4 glLightSource0position;
+uniform vec4 glLightSource0diffuse;
+
+//GL2ES2: varying color data needs to be defined
+varying vec4 glFrontColor;
+varying vec4 glFrontSecondaryColor;
+
void directionalLight0(
in vec3 normal,
inout vec4 ambient,
inout vec4 diffuse,
- inout vec4 specular)
+ inout vec3 specular)
{
// Normalized light direction and half vector
- vec3 lightDirection = normalize(vec3(gl_LightSource[0].position));
- vec3 halfVector = normalize(vec3(gl_LightSource[0].halfVector));
+ vec3 lightDirection = normalize(vec3(glLightSource0position));
+
+ //GL2ES2: half vector must be calculated
+ //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);
float nDotVP; // normal . light_direction
float nDotHV; // normal . light_half_vector
@@ -75,12 +102,12 @@ void directionalLight0(
pf = 0.0;
}
else {
- pf = pow(nDotHV, gl_FrontMaterial.shininess);
+ pf = pow(nDotHV, glFrontMaterialshininess);
}
- ambient += gl_LightSource[0].ambient;
- diffuse += gl_LightSource[0].diffuse * nDotVP;
- specular += gl_LightSource[0].specular * pf;
+ ambient += glLightModelambient;
+ diffuse += glLightSource0diffuse * nDotVP;
+ specular += glFrontMaterialspecular * pf;
}
@@ -89,21 +116,24 @@ void main()
vec3 tnorm = normalize(vec3(glNormalMatrix * glNormal));
vec4 amb = vec4(0.0);
vec4 diff = vec4(0.0);
- vec4 spec = vec4(0.0);
+ vec3 spec = vec3(0.0);
int i;
// Transform the vertex
vec4 outPosition = glModelViewProjectionMatrix * glVertex;
directionalLight0(tnorm, amb, diff, spec);
+
+ //GL2ES2: sceneColor Derived. Ecm + Acm * Acs (Acs is normal glLightModelambient)
+ vec4 sceneColor = glFrontMaterialemission + glFrontMaterialambient * glLightModelambient;
// Apply the result of the lighting equation
- vec4 outSecondaryColor = vec4(vec3(spec * gl_FrontMaterial.specular), 1.0);
- vec4 outColor = vec4(vec3(gl_FrontLightModelProduct.sceneColor +
- amb * gl_FrontMaterial.ambient +
- diff * gl_FrontMaterial.diffuse), 1.0);
+ vec4 outSecondaryColor = vec4(vec3(spec * glFrontMaterialspecular), 1.0);
+ vec4 outColor = vec4(vec3( sceneColor +
+ amb * glFrontMaterialambient +
+ diff * glFrontMaterialdiffuse), 1.0);
- gl_FrontColor = outColor;
- gl_FrontSecondaryColor = outSecondaryColor;
+ glFrontColor = outColor;
+ glFrontSecondaryColor = outSecondaryColor;
gl_Position = outPosition;
}
diff --git a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/phong.frag b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/phong.frag
index fdea058..230288a 100644
--- a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/phong.frag
+++ b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/phong.frag
@@ -45,6 +45,10 @@
// A GLSL fragment program for handling 1 directional light with specular.
// This implements per-pixel lighting (Phong shading)
+// GL2ES2: Java3D built-in uniforms, these are calculated and passsed in if declared here
+uniform mat4 glModelViewMatrix;
+
+
uniform vec4 glLightSource0position;
uniform vec4 glLightSource0diffuse;
@@ -56,18 +60,26 @@ uniform vec3 glFrontMaterialspecular;
varying vec3 worldPos;
+varying vec4 sceneColor;
+
void directionalLight0(in vec3 normal, inout vec4 ambient, inout vec4 diffuse, inout vec3 specular)
{
// Normalized light direction and half vector
vec3 lightDirection = normalize(vec3(glLightSource0position));
-
- // half vector requires a few calcs
- //vec3 halfVector = normalize(vec3(gl_LightSource[0].halfVector));
- vec3 L = normalize(glLightSource0position.xyz - worldPos);
- vec3 V = vec3(0,0,1);//eye position
- vec3 halfVector = (L + V);
+ //GL2ES2: half vector must be calculated
+ //vec3 halfVector = normalize(vec3(gl_LightSource[0].halfVector));
+ //http://stackoverflow.com/questions/3744038/what-is-half-vector-in-modern-glsl
+ vec3 ecPos = vec3(glModelViewMatrix * vec4(worldPos,1.0));
+ 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);
float nDotVP; // normal . light_direction
@@ -112,10 +124,9 @@ void main()
// Apply the result of the lighting equation
vec4 secondaryColor = vec4(spec * glFrontMaterialspecular, 1.0);
- // GL2ES2: need to look up the calculations on sceneColor
+ // GL2ES2: change to calc'ed sceneColor
//vec4 color = vec4(vec3(gl_FrontLightModelProduct.sceneColor +
- // amb * glFrontMaterialambient +
- vec4 color = vec4(vec3(glFrontMaterialdiffuse +
+ vec4 color = vec4(vec3(sceneColor +
amb * glLightModelambient +
diff * glFrontMaterialdiffuse), 1.0);
diff --git a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/phong.vert b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/phong.vert
index b436ee2..6d3aeb2 100644
--- a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/phong.vert
+++ b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/phong.vert
@@ -54,13 +54,24 @@ uniform mat4 glModelViewProjectionMatrix;
uniform mat4 glModelViewMatrix;
uniform mat3 glNormalMatrix;
+uniform vec4 glLightModelambient;
+
+uniform vec4 glFrontMaterialambient;
+uniform vec4 glFrontMaterialemission;
+
// Per-pixel normal (output to fragment shader)
varying vec3 Normal;
varying vec3 worldPos;
+varying vec4 sceneColor;
+
void main()
{
+
+ //GL2ES2: sceneColor Derived. Ecm + Acm * Acs (Acs is normal glLightModelambient)
+ sceneColor = glFrontMaterialemission + glFrontMaterialambient * glLightModelambient;
+
Normal = normalize(vec3(glNormalMatrix * glNormal));
worldPos = vec3(glModelViewMatrix * glVertex);
diff --git a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/simple.frag b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/simple.frag
index 96a6e64..092a110 100644
--- a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/simple.frag
+++ b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/simple.frag
@@ -64,5 +64,5 @@ void main()
float atten = invDist.x * invDist.y;
vec4 outcolor = (glFrontColor + glFrontSecondaryColor) * atten;
- gl_FragColor = outcolor;
+ gl_FragColor = outcolor;
}
diff --git a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/simple.vert b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/simple.vert
index 96ccf6b..93a61bf 100644
--- a/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/simple.vert
+++ b/src/classes/org/jdesktop/j3d/examples/gl2es2pipeline/simple.vert
@@ -55,11 +55,11 @@ uniform mat4 glModelViewMatrix;
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;
@@ -135,11 +135,8 @@ void main()
directionalLight(i, tnorm, amb, diff, spec);
}
- vec4 sceneColor;
- if( ignoreVertexColors != 0)
- sceneColor = glFrontMaterialdiffuse;
- else
- sceneColor = glColor;
+ //GL2ES2: sceneColor Derived. Ecm + Acm * Acs (Acs is normal glLightModelambient)
+ vec4 sceneColor = glFrontMaterialemission + glFrontMaterialambient * glLightModelambient;
// Apply the result of the lighting equation
vec4 outSecondaryColor = vec4(vec3(spec * glFrontMaterialspecular), 1.0);
diff --git a/src/classes/org/jdesktop/j3d/examples/glsl_shader/SphereGLSL.java b/src/classes/org/jdesktop/j3d/examples/glsl_shader/SphereGLSL.java
index 5d869c9..03a514e 100644
--- a/src/classes/org/jdesktop/j3d/examples/glsl_shader/SphereGLSL.java
+++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/SphereGLSL.java
@@ -95,7 +95,7 @@ public class SphereGLSL extends javax.swing.JFrame {
// Flag indicates type of lights: directional, point, or spot
// lights. This flag is set based on command line argument
- private static int lightType = DIRECTIONAL_LIGHT;
+ private static int lightType = POINT_LIGHT;//DIRECTIONAL_LIGHT;
private SimpleUniverse univ = null;
private BranchGroup scene = null;
@@ -173,7 +173,7 @@ public class SphereGLSL extends javax.swing.JFrame {
// Create transformations for the positional lights
t = new Transform3D();
- Vector3d lPos1 = new Vector3d(0.0, 0.0, 2.0);
+ Vector3d lPos1 = new Vector3d(0.0, 0.5, 2.0);
t.set(lPos1);
TransformGroup l1Trans = new TransformGroup(t);
l1RotTrans.addChild(l1Trans);
diff --git a/src/classes/org/jdesktop/j3d/examples/glsl_shader/simple.frag b/src/classes/org/jdesktop/j3d/examples/glsl_shader/simple.frag
index 6600694..51eaaf6 100644
--- a/src/classes/org/jdesktop/j3d/examples/glsl_shader/simple.frag
+++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/simple.frag
@@ -42,6 +42,9 @@
* $State$
*/
+
+varying vec4 glFrontColor;
+
// Simple GLSL fragment program to attenuate the input fragment color as a
// function of the distance of the fragment position from the center
// of the window
diff --git a/src/classes/org/jdesktop/j3d/examples/glsl_shader/simple.vert b/src/classes/org/jdesktop/j3d/examples/glsl_shader/simple.vert
index 64d1515..f90ef79 100644
--- a/src/classes/org/jdesktop/j3d/examples/glsl_shader/simple.vert
+++ b/src/classes/org/jdesktop/j3d/examples/glsl_shader/simple.vert
@@ -45,6 +45,8 @@
// A simple GLSL vertex program for handling 2 directional lights with
// separate specular
+varying vec4 glFrontColor;
+
void directionalLight(
in int i,
in vec3 normal,