From 7dbc5626fb3f66ffd9fa08e660a78c5ec1b68e48 Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Mon, 12 Feb 2018 18:19:05 -0800 Subject: [PATCH] Add the shader files. --- .../projectiveTextureInstancingPS.glsl | 72 ++++++++++++++ .../Shaders/projectiveTextureInstancingPS.h | 65 ++++++++++++ .../projectiveTextureInstancingVS.glsl | 99 +++++++++++++++++++ .../Shaders/projectiveTextureInstancingVS.h | 86 ++++++++++++++++ 4 files changed, 322 insertions(+) create mode 100644 examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl create mode 100644 examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h create mode 100644 examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl create mode 100644 examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl new file mode 100644 index 000000000..4e11e3762 --- /dev/null +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.glsl @@ -0,0 +1,72 @@ +#version 330 core +//precision highp float; + +in Fragment +{ + vec4 color; +} fragment; + +in Vert +{ + vec2 texcoord; +} vert; + +uniform sampler2D Diffuse; +uniform mat4 ViewMatrixInverse; + +in vec3 lightPos,cameraPosition, normal,ambient; +in vec4 vertexPos; +in float materialShininess; +in vec3 lightSpecularIntensity; +in vec3 materialSpecularColor; + +out vec4 color; + + + +void main(void) +{ + vec4 texel = fragment.color*texture(Diffuse,vert.texcoord.xy); + vec3 ct,cf; + float intensity,at,af; + if (fragment.color.w==0) + discard; + vec3 lightDir = normalize(lightPos); + + vec3 normalDir = normalize(normal); + + intensity = 0.5+0.5*clamp( dot( normalDir,lightDir ), -1,1 ); + + af = 1.0; + + ct = texel.rgb; + at = texel.a; + + //float bias = 0.005f; + + vec3 specularReflection; + + if (dot(normalDir, lightDir) < 0.0) + { + specularReflection = vec3(0.0, 0.0, 0.0); + } + else // light source on the right side + { + vec3 surfaceToLight = normalize(lightPos - vertexPos.xyz); + vec3 surfaceToCamera = normalize(cameraPosition - vertexPos.xyz); + + + float specularCoefficient = 0.0; + specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalDir))), materialShininess); + specularReflection = specularCoefficient * materialSpecularColor * lightSpecularIntensity; + + } + + + float visibility = 1.0; + + intensity = 0.7*intensity + 0.3*intensity*visibility; + + cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient+specularReflection*visibility; + color = vec4(ct * cf, fragment.color.w); +} diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h new file mode 100644 index 000000000..2bdb30b64 --- /dev/null +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingPS.h @@ -0,0 +1,65 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* projectiveTextureInstancingFragmentShader= \ +"#version 330 core\n" +"//precision highp float;\n" +"in Fragment\n" +"{\n" +" vec4 color;\n" +"} fragment;\n" +"in Vert\n" +"{\n" +" vec2 texcoord;\n" +"} vert;\n" +"uniform sampler2D Diffuse;\n" +"uniform mat4 ViewMatrixInverse;\n" +"in vec3 lightPos,cameraPosition, normal,ambient;\n" +"in vec4 vertexPos;\n" +"in float materialShininess;\n" +"in vec3 lightSpecularIntensity;\n" +"in vec3 materialSpecularColor;\n" +"out vec4 color;\n" +"void main(void)\n" +"{\n" +" vec4 texel = fragment.color*texture(Diffuse,vert.texcoord.xy);\n" +" vec3 ct,cf;\n" +" float intensity,at,af;\n" +" if (fragment.color.w==0)\n" +" discard;\n" +" vec3 lightDir = normalize(lightPos);\n" +" \n" +" vec3 normalDir = normalize(normal);\n" +" \n" +" intensity = 0.5+0.5*clamp( dot( normalDir,lightDir ), -1,1 );\n" +" \n" +" af = 1.0;\n" +" \n" +" ct = texel.rgb;\n" +" at = texel.a;\n" +" \n" +" //float bias = 0.005f;\n" +" \n" +" vec3 specularReflection;\n" +" \n" +" if (dot(normalDir, lightDir) < 0.0) \n" +" {\n" +" specularReflection = vec3(0.0, 0.0, 0.0);\n" +" }\n" +" else // light source on the right side\n" +" {\n" +" vec3 surfaceToLight = normalize(lightPos - vertexPos.xyz);\n" +" vec3 surfaceToCamera = normalize(cameraPosition - vertexPos.xyz);\n" +" \n" +" \n" +" float specularCoefficient = 0.0;\n" +" specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalDir))), materialShininess);\n" +" specularReflection = specularCoefficient * materialSpecularColor * lightSpecularIntensity;\n" +" \n" +" }\n" +" \n" +" float visibility = 1.0;\n" +" intensity = 0.7*intensity + 0.3*intensity*visibility;\n" +" \n" +" cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient+specularReflection*visibility;\n" +" color = vec4(ct * cf, fragment.color.w);\n" +"}\n" +; diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl new file mode 100644 index 000000000..01d73c2d4 --- /dev/null +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.glsl @@ -0,0 +1,99 @@ +#version 330 +precision highp float; + + +layout (location = 0) in vec4 position; +layout (location = 1) in vec4 instance_position; +layout (location = 2) in vec4 instance_quaternion; +layout (location = 3) in vec2 uvcoords; +layout (location = 4) in vec3 vertexnormal; +layout (location = 5) in vec4 instance_color; +layout (location = 6) in vec3 instance_scale; + + +uniform mat4 DepthBiasModelViewProjectionMatrix; +uniform mat4 MVP; +uniform vec3 lightPosIn; +uniform vec3 cameraPositionIn; +uniform mat4 ViewMatrixInverse; +uniform float materialShininessIn; +uniform vec3 lightSpecularIntensityIn; +uniform vec3 materialSpecularColorIn; + +out vec4 ShadowCoord; + +out Fragment +{ + vec4 color; +} fragment; + +out Vert +{ + vec2 texcoord; +} vert; + + +vec4 quatMul ( in vec4 q1, in vec4 q2 ) +{ + vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz ); + vec4 dt = q1 * q2; + float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) ); + return vec4 ( im, re ); +} + +vec4 quatFromAxisAngle(vec4 axis, in float angle) +{ + float cah = cos(angle*0.5); + float sah = sin(angle*0.5); + float d = inversesqrt(dot(axis,axis)); + vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah); + return q; +} +// +// vector rotation via quaternion +// +vec4 quatRotate3 ( in vec3 p, in vec4 q ) +{ + vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) ); + return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) ); +} +vec4 quatRotate ( in vec4 p, in vec4 q ) +{ + vec4 temp = quatMul ( q, p ); + return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) ); +} + +out vec3 lightPos,normal,ambient; +out vec4 vertexPos; +out vec3 cameraPosition; +out float materialShininess; +out vec3 lightSpecularIntensity; +out vec3 materialSpecularColor; + + +void main(void) +{ + vec4 q = instance_quaternion; + ambient = vec3(0.5,.5,0.5); + + vec4 worldNormal = (quatRotate3( vertexnormal,q)); + + normal = worldNormal.xyz; + + lightPos = lightPosIn; + cameraPosition = cameraPositionIn; + materialShininess = materialShininessIn; + lightSpecularIntensity = lightSpecularIntensityIn; + materialSpecularColor = materialSpecularColorIn; + + vec4 localcoord = quatRotate3( position.xyz*instance_scale,q); + vertexPos = vec4((instance_position+localcoord).xyz,1); + + vec4 vertexLoc = MVP* vec4((instance_position+localcoord).xyz,1); + gl_Position = vertexLoc; + + fragment.color = instance_color; + vec4 projcoords = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1); + vert.texcoord = projcoords.xy; +} + diff --git a/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h new file mode 100644 index 000000000..3f929e44b --- /dev/null +++ b/examples/OpenGLWindow/Shaders/projectiveTextureInstancingVS.h @@ -0,0 +1,86 @@ +//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project +static const char* projectiveTextureInstancingVertexShader= \ +"#version 330 \n" +"precision highp float;\n" +"layout (location = 0) in vec4 position;\n" +"layout (location = 1) in vec4 instance_position;\n" +"layout (location = 2) in vec4 instance_quaternion;\n" +"layout (location = 3) in vec2 uvcoords;\n" +"layout (location = 4) in vec3 vertexnormal;\n" +"layout (location = 5) in vec4 instance_color;\n" +"layout (location = 6) in vec3 instance_scale;\n" +"uniform mat4 DepthBiasModelViewProjectionMatrix;\n" +"uniform mat4 MVP;\n" +"uniform vec3 lightPosIn;\n" +"uniform vec3 cameraPositionIn;\n" +"uniform mat4 ViewMatrixInverse;\n" +"uniform float materialShininessIn;\n" +"uniform vec3 lightSpecularIntensityIn;\n" +"uniform vec3 materialSpecularColorIn;\n" +"out vec4 ShadowCoord;\n" +"out Fragment\n" +"{\n" +" vec4 color;\n" +"} fragment;\n" +"out Vert\n" +"{\n" +" vec2 texcoord;\n" +"} vert;\n" +"vec4 quatMul ( in vec4 q1, in vec4 q2 )\n" +"{\n" +" vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz );\n" +" vec4 dt = q1 * q2;\n" +" float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) );\n" +" return vec4 ( im, re );\n" +"}\n" +"vec4 quatFromAxisAngle(vec4 axis, in float angle)\n" +"{\n" +" float cah = cos(angle*0.5);\n" +" float sah = sin(angle*0.5);\n" +" float d = inversesqrt(dot(axis,axis));\n" +" vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah);\n" +" return q;\n" +"}\n" +"//\n" +"// vector rotation via quaternion\n" +"//\n" +"vec4 quatRotate3 ( in vec3 p, in vec4 q )\n" +"{\n" +" vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) );\n" +" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" +"}\n" +"vec4 quatRotate ( in vec4 p, in vec4 q )\n" +"{\n" +" vec4 temp = quatMul ( q, p );\n" +" return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );\n" +"}\n" +"out vec3 lightPos,normal,ambient;\n" +"out vec4 vertexPos;\n" +"out vec3 cameraPosition;\n" +"out float materialShininess;\n" +"out vec3 lightSpecularIntensity;\n" +"out vec3 materialSpecularColor;\n" +"void main(void)\n" +"{\n" +" vec4 q = instance_quaternion;\n" +" ambient = vec3(0.5,.5,0.5);\n" +" \n" +" vec4 worldNormal = (quatRotate3( vertexnormal,q));\n" +" \n" +" normal = worldNormal.xyz;\n" +" lightPos = lightPosIn;\n" +" cameraPosition = cameraPositionIn;\n" +" materialShininess = materialShininessIn;\n" +" lightSpecularIntensity = lightSpecularIntensityIn;\n" +" materialSpecularColor = materialSpecularColorIn;\n" +" \n" +" vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n" +" vertexPos = vec4((instance_position+localcoord).xyz,1);\n" +" \n" +" vec4 vertexLoc = MVP* vec4((instance_position+localcoord).xyz,1);\n" +" gl_Position = vertexLoc;\n" +" fragment.color = instance_color;\n" +" vec4 projcoords = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);\n" +" vert.texcoord = projcoords.xy;\n" +"}\n" +;