diff options
author | Sven Gothel <[email protected]> | 2008-08-11 14:26:52 +0000 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2008-08-11 14:26:52 +0000 |
commit | 28d62d086afefaf752b38ce5c2c67bc826b5a286 (patch) | |
tree | ac7b720db745c2ab872da484c28e197065866fc3 /src | |
parent | 1ef16ee89df4d0dd4df0c1356e5b52eba20f3850 (diff) |
FixedFunction shader as files, prepared for binary shader files
git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/branches/JOGL_2_SANDBOX@1748 232f8b59-042b-4e1e-8c03-345bb8c30851
Diffstat (limited to 'src')
17 files changed, 430 insertions, 276 deletions
diff --git a/src/classes/com/sun/opengl/impl/glsl/FixedFuncPipeline.java b/src/classes/com/sun/opengl/impl/glsl/FixedFuncPipeline.java index 91a11094e..dc7ebaae6 100644 --- a/src/classes/com/sun/opengl/impl/glsl/FixedFuncPipeline.java +++ b/src/classes/com/sun/opengl/impl/glsl/FixedFuncPipeline.java @@ -332,19 +332,45 @@ public class FixedFuncPipeline { this.pmvMatrix=pmvMatrix; this.shaderState=new ShaderState(); this.shaderState.setVerbose(verbose); + ShaderCode vertexColor, vertexColorLight, fragmentColor, fragmentColorTexture; - ShaderCode vertexColor = new ShaderCode( gl.GL_VERTEX_SHADER, 1, -1, null, - FixedFuncShaderVertexColor.vertShaderSource); + // FIXME: Proper evaluation for binary format types .. + int binaryFormat = GLProfile.isGLES2()?GLES2.GL_NVIDIA_PLATFORM_BINARY_NV:-1; - ShaderCode vertexColorLight = new ShaderCode( gl.GL_VERTEX_SHADER, 1, -1, null, - FixedFuncShaderVertexColorLight.vertShaderSource); + switch(binaryFormat) { + case GLES2.GL_NVIDIA_PLATFORM_BINARY_NV: + vertexColor = ShaderCode.create( gl.GL_VERTEX_SHADER, 1, this.getClass(), + binaryFormat, "binary_nvidia/FixedFuncShaderVertexColor.nvbv", + vertexColorSrcFile); - ShaderCode fragmentColor = new ShaderCode( gl.GL_FRAGMENT_SHADER, 1, -1, null, - FixedFuncShaderFragmentColor.fragShaderSource); + vertexColorLight = ShaderCode.create( gl.GL_VERTEX_SHADER, 1, this.getClass(), + binaryFormat, "binary_nvidia/FixedFuncShaderVertexColorLight.nvbv", + vertexColorLightSrcFile); - ShaderCode fragmentColorTexture = new ShaderCode( gl.GL_FRAGMENT_SHADER, 1, -1, null, - FixedFuncShaderFragmentColorTexture.fragShaderSource); + fragmentColor = ShaderCode.create( gl.GL_FRAGMENT_SHADER, 1, this.getClass(), + binaryFormat, "binary_nvidia/FixedFuncShaderFragmentColor.nvbv", + fragmentColorSrcFile); + fragmentColorTexture = ShaderCode.create( gl.GL_FRAGMENT_SHADER, 1, this.getClass(), + binaryFormat, "binary_nvidia/FixedFuncShaderFragmentColorTexture.nvbv", + fragmentColorTextureSrcFile); + + break; + default: + vertexColor = ShaderCode.create( gl.GL_VERTEX_SHADER, 1, this.getClass(), -1, null, + vertexColorSrcFile); + + vertexColorLight = ShaderCode.create( gl.GL_VERTEX_SHADER, 1, this.getClass(), -1, null, + vertexColorLightSrcFile); + + fragmentColor = ShaderCode.create( gl.GL_FRAGMENT_SHADER, 1, this.getClass(), -1, null, + fragmentColorSrcFile); + + fragmentColorTexture = ShaderCode.create( gl.GL_FRAGMENT_SHADER, 1, this.getClass(), -1, null, + fragmentColorTextureSrcFile); + } + + shaderProgramColor = new ShaderProgram(); shaderProgramColor.add(vertexColor); shaderProgramColor.add(fragmentColor); @@ -464,5 +490,10 @@ public class FixedFuncPipeline { public static final FloatBuffer defMatSpecular= BufferUtil.newFloatBuffer(new float[] { 0f, 0f, 0f, 1f}); public static final FloatBuffer defMatEmission= BufferUtil.newFloatBuffer(new float[] { 0f, 0f, 0f, 1f}); public static final float defMatShininess = 0f; + + protected static final String[] vertexColorSrcFile = new String[] { "source/FixedFuncShaderVertexColor.vp" }; + protected static final String[] vertexColorLightSrcFile = new String[] { "source/FixedFuncShaderVertexColorLight.vp" }; + protected static final String[] fragmentColorSrcFile = new String[] { "source/FixedFuncShaderFragmentColor.fp" } ; + protected static final String[] fragmentColorTextureSrcFile = new String[] { "source/FixedFuncShaderFragmentColorTexture.fp" } ; } diff --git a/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderFragmentColor.java b/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderFragmentColor.java deleted file mode 100644 index 95176bbb9..000000000 --- a/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderFragmentColor.java +++ /dev/null @@ -1,27 +0,0 @@ - -package com.sun.opengl.impl.glsl; - -import javax.media.opengl.util.*; -import javax.media.opengl.*; -import java.nio.*; - -public class FixedFuncShaderFragmentColor { - - public static final String[][] fragShaderSource = new String[][] { { - "#ifdef GL_ES\n"+ - " #define MEDIUMP mediump\n"+ - " #define HIGHP highp\n"+ - "#else\n"+ - " #define MEDIUMP\n"+ - " #define HIGHP\n"+ - "#endif\n"+ - "\n"+ - "varying HIGHP vec4 frontColor;\n"+ - "\n"+ - "void main (void)\n"+ - "{\n"+ - " gl_FragColor = frontColor;\n"+ - "}\n" } } ; - -} - diff --git a/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderFragmentColorTexture.java b/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderFragmentColorTexture.java deleted file mode 100644 index e456d472d..000000000 --- a/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderFragmentColorTexture.java +++ /dev/null @@ -1,39 +0,0 @@ - -package com.sun.opengl.impl.glsl; - -import javax.media.opengl.util.*; -import javax.media.opengl.*; -import java.nio.*; - -public class FixedFuncShaderFragmentColorTexture { - - public static final String[][] fragShaderSource = new String[][] { { - "#ifdef GL_ES\n"+ - " #define MEDIUMP mediump\n"+ - " #define HIGHP highp\n"+ - "#else\n"+ - " #define MEDIUMP\n"+ - " #define HIGHP\n"+ - "#endif\n"+ - "\n"+ - "const MEDIUMP int MAX_TEXTURE_UNITS = 8; // <=gl_MaxTextureImageUnits \n"+ - "const MEDIUMP int MAX_LIGHTS = 8; \n"+ - "const HIGHP vec4 zero = vec4(0.0, 0.0, 0.0, 0.0);\n"+ - "\n"+ - "uniform HIGHP sampler2D mgl_ActiveTexture;\n"+ - "uniform MEDIUMP int mgl_ActiveTextureIdx;\n"+ - "\n"+ - "varying HIGHP vec4 frontColor;\n"+ - "varying HIGHP vec4 mgl_TexCoord[MAX_TEXTURE_UNITS];\n"+ - "\n"+ - "void main (void)\n"+ - "{\n"+ - " vec4 texColor = texture2D(mgl_ActiveTexture,mgl_TexCoord[mgl_ActiveTextureIdx].st);\n"+ - " if(greaterThan(texColor, zero)) {\n"+ - " gl_FragColor = vec4(frontColor.rgb*texColor.rgb, frontColor.a * texColor.a) ; \n"+ - " } else {\n"+ - " gl_FragColor = frontColor;\n"+ - " }\n"+ - "}\n" } } ; -} - diff --git a/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderVertexColor.java b/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderVertexColor.java deleted file mode 100644 index 3d0bed73d..000000000 --- a/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderVertexColor.java +++ /dev/null @@ -1,65 +0,0 @@ - -package com.sun.opengl.impl.glsl; - -import javax.media.opengl.util.*; -import javax.media.opengl.*; -import java.nio.*; - -public class FixedFuncShaderVertexColor { - - public static final String[][] vertShaderSource = new String[][] { { - "#ifdef GL_ES\n"+ - " #define MEDIUMP mediump\n"+ - " #define HIGHP highp\n"+ - "#else\n"+ - " #define MEDIUMP\n"+ - " #define HIGHP\n"+ - "#endif\n"+ - "\n"+ - "const MEDIUMP int MAX_TEXTURE_UNITS = 8; // <=gl_MaxTextureImageUnits \n"+ - "const MEDIUMP int MAX_LIGHTS = 8; \n"+ - "\n"+ - "uniform HIGHP mat4 mgl_PMVMatrix[3]; // P, Mv, and Mvi\n"+ - "uniform MEDIUMP int mgl_ColorEnabled;\n"+ - "uniform HIGHP vec4 mgl_ColorStatic;\n"+ - "uniform MEDIUMP int mgl_TexCoordEnabled;\n"+ - "\n"+ - "attribute HIGHP vec4 mgl_Vertex;\n"+ - "attribute HIGHP vec4 mgl_Color;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord0;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord1;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord2;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord3;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord4;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord5;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord6;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord7;\n"+ - "\n"+ - "varying HIGHP vec4 frontColor;\n"+ - "varying HIGHP vec4 mgl_TexCoord[MAX_TEXTURE_UNITS];\n"+ - "\n"+ - "void setTexCoord(in HIGHP vec4 defpos) {\n"+ - " mgl_TexCoord[0] = ( 0 != (mgl_TexCoordEnabled & 1) ) ? mgl_MultiTexCoord0 : defpos;\n"+ - " mgl_TexCoord[1] = ( 0 != (mgl_TexCoordEnabled & 2) ) ? mgl_MultiTexCoord1 : defpos;\n"+ - " mgl_TexCoord[2] = ( 0 != (mgl_TexCoordEnabled & 4) ) ? mgl_MultiTexCoord2 : defpos;\n"+ - " mgl_TexCoord[3] = ( 0 != (mgl_TexCoordEnabled & 8) ) ? mgl_MultiTexCoord3 : defpos;\n"+ - " mgl_TexCoord[4] = ( 0 != (mgl_TexCoordEnabled & 16) ) ? mgl_MultiTexCoord4 : defpos;\n"+ - " mgl_TexCoord[5] = ( 0 != (mgl_TexCoordEnabled & 32) ) ? mgl_MultiTexCoord5 : defpos;\n"+ - " mgl_TexCoord[6] = ( 0 != (mgl_TexCoordEnabled & 64) ) ? mgl_MultiTexCoord6 : defpos;\n"+ - " mgl_TexCoord[7] = ( 0 != (mgl_TexCoordEnabled & 128) ) ? mgl_MultiTexCoord7 : defpos;\n"+ - "}\n"+ - "\n"+ - "void main(void)\n"+ - "{\n"+ - " if(mgl_ColorEnabled>0) {\n"+ - " frontColor=mgl_Color;\n"+ - " } else {\n"+ - " frontColor=mgl_ColorStatic;\n"+ - " }\n"+ - "\n"+ - " gl_Position = mgl_PMVMatrix[0] * mgl_PMVMatrix[1] * mgl_Vertex;\n"+ - "\n"+ - " setTexCoord(gl_Position);\n"+ - "}\n" } } ; -} - diff --git a/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderVertexColorLight.java b/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderVertexColorLight.java deleted file mode 100644 index 2034c2acd..000000000 --- a/src/classes/com/sun/opengl/impl/glsl/FixedFuncShaderVertexColorLight.java +++ /dev/null @@ -1,136 +0,0 @@ - -package com.sun.opengl.impl.glsl; - -import javax.media.opengl.util.*; -import javax.media.opengl.*; -import java.nio.*; - -public class FixedFuncShaderVertexColorLight { - - public static final String[][] vertShaderSource = new String[][] { { - "#ifdef GL_ES\n"+ - " #define MEDIUMP mediump\n"+ - " #define HIGHP highp\n"+ - "#else\n"+ - " #define MEDIUMP\n"+ - " #define HIGHP\n"+ - "#endif\n"+ - "\n"+ - "struct mgl_LightSourceParameters {\n"+ - " vec4 ambient; \n"+ - " vec4 diffuse; \n"+ - " vec4 specular; \n"+ - " vec4 position; \n"+ - " // vec4 halfVector; // is computed here\n"+ - " vec3 spotDirection; \n"+ - " float spotExponent; \n"+ - " float spotCutoff; // (range: [0.0,90.0], 180.0)\n"+ - " //float spotCosCutoff; // (range: [1.0,0.0],-1.0)\n"+ - " float constantAttenuation; \n"+ - " float linearAttenuation; \n"+ - " float quadraticAttenuation; \n"+ - "};\n"+ - "struct mgl_MaterialParameters {\n"+ - " vec4 ambient; \n"+ - " vec4 diffuse; \n"+ - " vec4 specular; \n"+ - " vec4 emission; \n"+ - " float shininess; \n"+ - "};\n"+ - "\n"+ - "\n"+ - "const MEDIUMP int MAX_TEXTURE_UNITS = 8; // <=gl_MaxTextureImageUnits \n"+ - "const MEDIUMP int MAX_LIGHTS = 8; \n"+ - "\n"+ - "uniform HIGHP mat4 mgl_PMVMatrix[3]; // P, Mv, and Mvi\n"+ - "uniform HIGHP mat3 mgl_NormalMatrix; // transpose(inverse(ModelView)).3x3\n"+ - "uniform MEDIUMP int mgl_ColorEnabled;\n"+ - "uniform HIGHP vec4 mgl_ColorStatic;\n"+ - "uniform MEDIUMP int mgl_TexCoordEnabled;\n"+ - "uniform MEDIUMP int mgl_LightsEnabled;\n"+ - "uniform mgl_LightSourceParameters mgl_LightSource[MAX_LIGHTS];\n"+ - "uniform mgl_MaterialParameters mgl_FrontMaterial;\n"+ - "\n"+ - "attribute HIGHP vec4 mgl_Vertex;\n"+ - "attribute HIGHP vec3 mgl_Normal;\n"+ - "attribute HIGHP vec4 mgl_Color;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord0;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord1;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord2;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord3;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord4;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord5;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord6;\n"+ - "attribute HIGHP vec4 mgl_MultiTexCoord7;\n"+ - "\n"+ - "varying HIGHP vec4 frontColor;\n"+ - "varying HIGHP vec4 mgl_TexCoord[MAX_TEXTURE_UNITS];\n"+ - "\n"+ - "void setTexCoord(in HIGHP vec4 defpos) {\n"+ - " mgl_TexCoord[0] = ( 0 != (mgl_TexCoordEnabled & 1) ) ? mgl_MultiTexCoord0 : defpos;\n"+ - " mgl_TexCoord[1] = ( 0 != (mgl_TexCoordEnabled & 2) ) ? mgl_MultiTexCoord1 : defpos;\n"+ - " mgl_TexCoord[2] = ( 0 != (mgl_TexCoordEnabled & 4) ) ? mgl_MultiTexCoord2 : defpos;\n"+ - " mgl_TexCoord[3] = ( 0 != (mgl_TexCoordEnabled & 8) ) ? mgl_MultiTexCoord3 : defpos;\n"+ - " mgl_TexCoord[4] = ( 0 != (mgl_TexCoordEnabled & 16) ) ? mgl_MultiTexCoord4 : defpos;\n"+ - " mgl_TexCoord[5] = ( 0 != (mgl_TexCoordEnabled & 32) ) ? mgl_MultiTexCoord5 : defpos;\n"+ - " mgl_TexCoord[6] = ( 0 != (mgl_TexCoordEnabled & 64) ) ? mgl_MultiTexCoord6 : defpos;\n"+ - " mgl_TexCoord[7] = ( 0 != (mgl_TexCoordEnabled & 128) ) ? mgl_MultiTexCoord7 : defpos;\n"+ - "}\n"+ - "\n"+ - "void main(void)\n"+ - "{\n"+ - " vec4 position;\n"+ - " vec3 normal, lightDir, cameraDir, halfDir;\n"+ - " vec4 ambient, diffuse, specular;\n"+ - " float NdotL, NdotHV, dist, attenuation;\n"+ - " int i;\n"+ - "\n"+ - " position = mgl_PMVMatrix[1] * mgl_Vertex; // vertex eye position \n"+ - "\n"+ - " normal = normalize(mgl_NormalMatrix * mgl_Normal); \n"+ - " // cameraPosition: (mgl_PMVMatrix[2] * vec4(0,0,0,1.0)).xyz \n"+ - " cameraDir = normalize( (mgl_PMVMatrix[2] * vec4(0,0,0,1.0)).xyz - mgl_Vertex.xyz ); \n"+ - "\n"+ - " ambient = vec4(0,0,0,0);\n"+ - " diffuse = vec4(0,0,0,0);\n"+ - " specular = vec4(0,0,0,0);\n"+ - "\n"+ - " for(i=0; i<MAX_LIGHTS; i++) {\n"+ - " if( 0!= (mgl_LightsEnabled & (1<<i)) ) {\n"+ - " ambient += mgl_LightSource[i].ambient;\n"+ - " lightDir = mgl_LightSource[i].position.xyz - position.xyz;\n"+ - " dist = length(lightDir);\n"+ - " lightDir = normalize(lightDir);\n"+ - " attenuation = 1.0 / ( \n"+ - " mgl_LightSource[i].constantAttenuation+ \n"+ - " mgl_LightSource[i].linearAttenuation * dist + \n"+ - " mgl_LightSource[i].quadraticAttenuation * dist * dist );\n"+ - " NdotL = max(0.0, dot(normal, lightDir));\n"+ - " diffuse += mgl_LightSource[i].diffuse * NdotL * attenuation;\n"+ - " if (NdotL != 0.0) {\n"+ - " halfDir = normalize (lightDir + cameraDir); \n"+ - " NdotHV = max(0.0, dot(normal, halfDir));\n"+ - " specular += mgl_LightSource[i].specular * \n"+ - " pow(NdotHV,gl_FrontMaterial.shininess) * attenuation;\n"+ - " }\n"+ - " }\n"+ - " }\n"+ - " ambient += mgl_FrontMaterial.ambient;\n"+ - " diffuse *= mgl_FrontMaterial.diffuse;\n"+ - " specular *= mgl_FrontMaterial.specular;\n"+ - "\n"+ - " if(mgl_ColorEnabled>0) {\n"+ - " frontColor=mgl_Color;\n"+ - " } else {\n"+ - " frontColor=mgl_ColorStatic;\n"+ - " }\n"+ - " if( 0!= mgl_LightsEnabled ) {\n"+ - " frontColor *= ambient + diffuse + specular;\n"+ - " }\n"+ - "\n"+ - " gl_Position = mgl_PMVMatrix[0] * position;\n"+ - "\n"+ - " setTexCoord(gl_Position);\n"+ - "}\n" } } ; -} - diff --git a/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderFragmentColor.fp b/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderFragmentColor.fp new file mode 100644 index 000000000..71b39f45c --- /dev/null +++ b/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderFragmentColor.fp @@ -0,0 +1,9 @@ +#include es_precision.glsl + +#include mgl_varying.glsl + +void main (void) +{ + gl_FragColor = frontColor; +} + diff --git a/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderFragmentColorTexture.fp b/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderFragmentColorTexture.fp new file mode 100644 index 000000000..d91019a81 --- /dev/null +++ b/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderFragmentColorTexture.fp @@ -0,0 +1,18 @@ + +#include es_precision.glsl +#include mgl_lightdef.glsl + +#include mgl_const.glsl +#include mgl_uniform.glsl +#include mgl_varying.glsl + +void main (void) +{ + vec4 texColor = texture2D(mgl_ActiveTexture,mgl_TexCoord[mgl_ActiveTextureIdx].st); + if(greaterThan(texColor, zero)) { + gl_FragColor = vec4(frontColor.rgb*texColor.rgb, frontColor.a * texColor.a) ; + } else { + gl_FragColor = frontColor; + } +} + diff --git a/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderVertexColor.vp b/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderVertexColor.vp new file mode 100644 index 000000000..3162f0f46 --- /dev/null +++ b/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderVertexColor.vp @@ -0,0 +1,23 @@ +#include es_precision.glsl +#include mgl_lightdef.glsl + +#include mgl_const.glsl +#include mgl_uniform.glsl +#include mgl_attribute.glsl +#include mgl_varying.glsl + +#include mgl_settexcoord.vp + +void main(void) +{ + if(mgl_ColorEnabled>0) { + frontColor=mgl_Color; + } else { + frontColor=mgl_ColorStatic; + } + + gl_Position = mgl_PMVMatrix[0] * mgl_PMVMatrix[1] * mgl_Vertex; + + setTexCoord(gl_Position); +} + diff --git a/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderVertexColorLight.vp b/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderVertexColorLight.vp new file mode 100644 index 000000000..9a849ea11 --- /dev/null +++ b/src/classes/com/sun/opengl/impl/glsl/source/FixedFuncShaderVertexColorLight.vp @@ -0,0 +1,66 @@ +#include es_precision.glsl +#include mgl_lightdef.glsl + +#include mgl_const.glsl +#include mgl_uniform.glsl +#include mgl_attribute.glsl +#include mgl_varying.glsl + +#include mgl_settexcoord.vp + +void main(void) +{ + vec4 position; + vec3 normal, lightDir, cameraDir, halfDir; + vec4 ambient, diffuse, specular; + float NdotL, NdotHV, dist, attenuation; + int i; + + position = mgl_PMVMatrix[1] * mgl_Vertex; // vertex eye position + + normal = normalize(mgl_NormalMatrix * mgl_Normal); + // cameraPosition: (mgl_PMVMatrix[2] * vec4(0,0,0,1.0)).xyz + cameraDir = normalize( (mgl_PMVMatrix[2] * vec4(0,0,0,1.0)).xyz - mgl_Vertex.xyz ); + + ambient = vec4(0,0,0,0); + diffuse = vec4(0,0,0,0); + specular = vec4(0,0,0,0); + + for(i=0; i<MAX_LIGHTS; i++) { + if( 0!= (mgl_LightsEnabled & (1<<i)) ) { + ambient += mgl_LightSource[i].ambient; + lightDir = mgl_LightSource[i].position.xyz - position.xyz; + dist = length(lightDir); + lightDir = normalize(lightDir); + attenuation = 1.0 / ( + mgl_LightSource[i].constantAttenuation+ + mgl_LightSource[i].linearAttenuation * dist + + mgl_LightSource[i].quadraticAttenuation * dist * dist ); + NdotL = max(0.0, dot(normal, lightDir)); + diffuse += mgl_LightSource[i].diffuse * NdotL * attenuation; + if (NdotL != 0.0) { + halfDir = normalize (lightDir + cameraDir); + NdotHV = max(0.0, dot(normal, halfDir)); + specular += mgl_LightSource[i].specular * + pow(NdotHV,gl_FrontMaterial.shininess) * attenuation; + } + } + } + ambient += mgl_FrontMaterial.ambient; + diffuse *= mgl_FrontMaterial.diffuse; + specular *= mgl_FrontMaterial.specular; + + if(mgl_ColorEnabled>0) { + frontColor=mgl_Color; + } else { + frontColor=mgl_ColorStatic; + } + if( 0!= mgl_LightsEnabled ) { + frontColor *= ambient + diffuse + specular; + } + + gl_Position = mgl_PMVMatrix[0] * position; + + setTexCoord(gl_Position); +} + diff --git a/src/classes/com/sun/opengl/impl/glsl/source/es_precision.glsl b/src/classes/com/sun/opengl/impl/glsl/source/es_precision.glsl new file mode 100644 index 000000000..7659df56c --- /dev/null +++ b/src/classes/com/sun/opengl/impl/glsl/source/es_precision.glsl @@ -0,0 +1,12 @@ +#ifndef es_precision_glsl +#define es_precision_glsl + +#ifdef GL_ES + #define MEDIUMP mediump + #define HIGHP highp +#else + #define MEDIUMP + #define HIGHP +#endif + +#endif diff --git a/src/classes/com/sun/opengl/impl/glsl/source/mgl_attribute.glsl b/src/classes/com/sun/opengl/impl/glsl/source/mgl_attribute.glsl new file mode 100644 index 000000000..7f72ccfbd --- /dev/null +++ b/src/classes/com/sun/opengl/impl/glsl/source/mgl_attribute.glsl @@ -0,0 +1,19 @@ + +#ifndef mgl_attribute_glsl +#define mgl_attribute_glsl + +#include es_precision.glsl + +attribute HIGHP vec4 mgl_Vertex; +attribute HIGHP vec3 mgl_Normal; +attribute HIGHP vec4 mgl_Color; +attribute HIGHP vec4 mgl_MultiTexCoord0; +attribute HIGHP vec4 mgl_MultiTexCoord1; +attribute HIGHP vec4 mgl_MultiTexCoord2; +attribute HIGHP vec4 mgl_MultiTexCoord3; +attribute HIGHP vec4 mgl_MultiTexCoord4; +attribute HIGHP vec4 mgl_MultiTexCoord5; +attribute HIGHP vec4 mgl_MultiTexCoord6; +attribute HIGHP vec4 mgl_MultiTexCoord7; + +#endif diff --git a/src/classes/com/sun/opengl/impl/glsl/source/mgl_const.glsl b/src/classes/com/sun/opengl/impl/glsl/source/mgl_const.glsl new file mode 100644 index 000000000..b227aa257 --- /dev/null +++ b/src/classes/com/sun/opengl/impl/glsl/source/mgl_const.glsl @@ -0,0 +1,11 @@ + +#ifndef mgl_const_glsl +#define mgl_const_glsl + +#include es_precision.glsl + +const MEDIUMP int MAX_TEXTURE_UNITS = 8; // <=gl_MaxTextureImageUnits +const MEDIUMP int MAX_LIGHTS = 8; +const HIGHP vec4 zero = vec4(0.0, 0.0, 0.0, 0.0); + +#endif diff --git a/src/classes/com/sun/opengl/impl/glsl/source/mgl_lightdef.glsl b/src/classes/com/sun/opengl/impl/glsl/source/mgl_lightdef.glsl new file mode 100644 index 000000000..ef79b76c8 --- /dev/null +++ b/src/classes/com/sun/opengl/impl/glsl/source/mgl_lightdef.glsl @@ -0,0 +1,26 @@ +#ifndef mgl_lightdef_glsl +#define mgl_lightdef_glsl + +struct mgl_LightSourceParameters { + vec4 ambient; + vec4 diffuse; + vec4 specular; + vec4 position; + // vec4 halfVector; // is computed here + vec3 spotDirection; + float spotExponent; + float spotCutoff; // (range: [0.0,90.0], 180.0) + //float spotCosCutoff; // (range: [1.0,0.0],-1.0) + float constantAttenuation; + float linearAttenuation; + float quadraticAttenuation; +}; +struct mgl_MaterialParameters { + vec4 ambient; + vec4 diffuse; + vec4 specular; + vec4 emission; + float shininess; +}; + +#endif diff --git a/src/classes/com/sun/opengl/impl/glsl/source/mgl_settexcoord.vp b/src/classes/com/sun/opengl/impl/glsl/source/mgl_settexcoord.vp new file mode 100644 index 000000000..39a2b7e64 --- /dev/null +++ b/src/classes/com/sun/opengl/impl/glsl/source/mgl_settexcoord.vp @@ -0,0 +1,22 @@ +#ifndef mgl_settexcoord_vp +#define mgl_settexcoord_vp + +#include es_precision.glsl + +#include mgl_const.glsl +#include mgl_uniform.glsl +#include mgl_attribute.glsl +#include mgl_varying.glsl + +void setTexCoord(in HIGHP vec4 defpos) { + mgl_TexCoord[0] = ( 0 != (mgl_TexCoordEnabled & 1) ) ? mgl_MultiTexCoord0 : defpos; + mgl_TexCoord[1] = ( 0 != (mgl_TexCoordEnabled & 2) ) ? mgl_MultiTexCoord1 : defpos; + mgl_TexCoord[2] = ( 0 != (mgl_TexCoordEnabled & 4) ) ? mgl_MultiTexCoord2 : defpos; + mgl_TexCoord[3] = ( 0 != (mgl_TexCoordEnabled & 8) ) ? mgl_MultiTexCoord3 : defpos; + mgl_TexCoord[4] = ( 0 != (mgl_TexCoordEnabled & 16) ) ? mgl_MultiTexCoord4 : defpos; + mgl_TexCoord[5] = ( 0 != (mgl_TexCoordEnabled & 32) ) ? mgl_MultiTexCoord5 : defpos; + mgl_TexCoord[6] = ( 0 != (mgl_TexCoordEnabled & 64) ) ? mgl_MultiTexCoord6 : defpos; + mgl_TexCoord[7] = ( 0 != (mgl_TexCoordEnabled & 128) ) ? mgl_MultiTexCoord7 : defpos; +} + +#endif diff --git a/src/classes/com/sun/opengl/impl/glsl/source/mgl_uniform.glsl b/src/classes/com/sun/opengl/impl/glsl/source/mgl_uniform.glsl new file mode 100644 index 000000000..e713b3ba9 --- /dev/null +++ b/src/classes/com/sun/opengl/impl/glsl/source/mgl_uniform.glsl @@ -0,0 +1,21 @@ + +#ifndef mgl_uniform_glsl +#define mgl_uniform_glsl + +#include es_precision.glsl + +#include mgl_const.glsl +#include mgl_lightdef.glsl + +uniform HIGHP mat4 mgl_PMVMatrix[3]; // P, Mv, and Mvi +uniform HIGHP mat3 mgl_NormalMatrix; // transpose(inverse(ModelView)).3x3 +uniform MEDIUMP int mgl_ColorEnabled; +uniform HIGHP vec4 mgl_ColorStatic; +uniform MEDIUMP int mgl_TexCoordEnabled; +uniform MEDIUMP int mgl_LightsEnabled; +uniform mgl_LightSourceParameters mgl_LightSource[MAX_LIGHTS]; +uniform mgl_MaterialParameters mgl_FrontMaterial; +uniform HIGHP sampler2D mgl_ActiveTexture; +uniform MEDIUMP int mgl_ActiveTextureIdx; + +#endif diff --git a/src/classes/com/sun/opengl/impl/glsl/source/mgl_varying.glsl b/src/classes/com/sun/opengl/impl/glsl/source/mgl_varying.glsl new file mode 100644 index 000000000..23528f3ee --- /dev/null +++ b/src/classes/com/sun/opengl/impl/glsl/source/mgl_varying.glsl @@ -0,0 +1,12 @@ + +#ifndef mgl_varying_glsl +#define mgl_varying_glsl + +#include es_precision.glsl + +#include mgl_const.glsl + +varying HIGHP vec4 frontColor; +varying HIGHP vec4 mgl_TexCoord[MAX_TEXTURE_UNITS]; + +#endif diff --git a/src/classes/javax/media/opengl/util/glsl/ShaderCode.java b/src/classes/javax/media/opengl/util/glsl/ShaderCode.java index 46ead1b30..b512f5912 100644 --- a/src/classes/javax/media/opengl/util/glsl/ShaderCode.java +++ b/src/classes/javax/media/opengl/util/glsl/ShaderCode.java @@ -5,7 +5,8 @@ import javax.media.opengl.util.*; import javax.media.opengl.*; import java.nio.*; -import java.io.PrintStream; +import java.io.*; +import java.net.*; public class ShaderCode { public ShaderCode(int type, int number, @@ -25,6 +26,29 @@ public class ShaderCode { id = getNextID(); } + public static ShaderCode create(int type, int number, + Class context, int binFormat, String binaryFile, String[] sourceFiles) { + String[][] shaderSources = null; + ByteBuffer shaderBinary = null; + if(null!=sourceFiles) { + shaderSources = new String[sourceFiles.length][1]; + for(int i=0; i<sourceFiles.length; i++) { + shaderSources[i][0] = readShaderSource(context, sourceFiles[i]); + if(null == shaderSources[i][0]) { + throw new RuntimeException("Can't find shader source " + sourceFiles[i]); + } + } + } + if(null!=binaryFile && 0<=binFormat) { + shaderBinary = readShaderBinary(context, binaryFile); + if(null == shaderBinary) { + System.err.println("Can't find shader binary " + binaryFile + " - ignored"); + binFormat = -1; + } + } + return new ShaderCode(type, number, binFormat, shaderBinary, shaderSources); + } + /** * returns the uniq shader id as an integer * @see #key() @@ -105,6 +129,133 @@ public class ShaderCode { return buf.toString(); } + public static void readShaderSource(ClassLoader context, String path, URL url, StringBuffer result) { + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); + String line = null; + while ((line = reader.readLine()) != null) { + if (line.startsWith("#include ")) { + String includeFile = line.substring(9).trim(); + // Try relative path first + String next = makeRelative(path, includeFile); + URL nextURL = getResource(context, next); + if (nextURL == null) { + // Try absolute path + next = includeFile; + nextURL = getResource(context, next); + } + if (nextURL == null) { + // Fail + throw new FileNotFoundException("Can't find include file " + includeFile); + } + readShaderSource(context, next, nextURL, result); + } else { + result.append(line + "\n"); + } + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static String readShaderSource(Class context, String path) { + URL url = getResource(context.getClassLoader(), path); + if (url == null) { + // Try again by scoping the path within the class's package + String className = context.getName().replace('.', '/'); + int lastSlash = className.lastIndexOf('/'); + if (lastSlash >= 0) { + String tmpPath = className.substring(0, lastSlash + 1) + path; + url = getResource(context.getClassLoader(), tmpPath); + if (url != null) { + path = tmpPath; + } + } + } + if (url == null) { + return null; + } + StringBuffer result = new StringBuffer(); + readShaderSource(context.getClassLoader(), path, url, result); + return result.toString(); + } + + public static ByteBuffer readShaderBinary(Class context, String path) { + try { + URL url = getResource(context.getClassLoader(), path); + if (url == null) { + // Try again by scoping the path within the class's package + String className = context.getName().replace('.', '/'); + int lastSlash = className.lastIndexOf('/'); + if (lastSlash >= 0) { + String tmpPath = className.substring(0, lastSlash + 1) + path; + url = getResource(context.getClassLoader(), tmpPath); + if (url != null) { + path = tmpPath; + } + } + } + if (url == null) { + return null; + } + return readAll(new BufferedInputStream(url.openStream())); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + protected static URL getResource(ClassLoader context, String path) { + if (context != null) { + return context.getResource(path); + } else { + return ClassLoader.getSystemResource(path); + } + } + + protected static String makeRelative(String context, String includeFile) { + File file = new File(context); + file = file.getParentFile(); + while (file != null && includeFile.startsWith("../")) { + file = file.getParentFile(); + includeFile = includeFile.substring(3); + } + if (file != null) { + String res = new File(file, includeFile).getPath(); + // Handle things on Windows + return res.replace('\\', '/'); + } else { + return includeFile; + } + } + + private static ByteBuffer readAll(InputStream stream) throws IOException { + byte[] data = new byte[1024]; + int numRead = 0; + int pos = 0; + do { + int avail = data.length - pos; + if (avail == 0) { + int newSize = 2 * data.length; + byte[] newData = new byte[newSize]; + System.arraycopy(data, 0, newData, 0, data.length); + data = newData; + avail = data.length - pos; + } + numRead = stream.read(data, pos, avail); + if (numRead > 0) { + pos += numRead; + } + } while (numRead >= 0); + ByteBuffer res = ByteBuffer.wrap(data); + if (data.length != pos) { + res.limit(pos); + } + return res; + } protected String[][] shaderSource = null; protected Buffer shaderBinary = null; protected int shaderBinaryFormat = -1; |