Thank 's A pow(Ton,100) Thomas & Egon

I googled for around 2 days but it didnt point to this stuff. It's really very useful.

Now I am stuck @ here (as it's pure GL) and i am using JPCT as the renderer.

I have the VS & FS for creating the Depth texture :

`// Vertex shader to generate the Depth Map`

// Used for shadow mapping - generates depth map from the light's viewpoint

// model-view projection matrix

uniform mat4 uMVPMatrix;

// position of the vertices

attribute vec4 aPosition;

varying vec4 position;

void main() {

position = uMVPMatrix * aPosition;

gl_Position = uMVPMatrix * aPosition;

}

`// Pixel shader to generate the Depth Map`

// Used for shadow mapping - generates depth map from the light's viewpoint

precision highp float;

varying vec4 position;

// taken from Fabien Sangalard's DEngine

vec4 pack (float depth)

{

const vec4 bitSh = vec4(256.0 * 256.0 * 256.0,

256.0 * 256.0,

256.0,

1.0);

const vec4 bitMsk = vec4(0,

1.0 / 256.0,

1.0 / 256.0,

1.0 / 256.0);

vec4 comp = fract(depth * bitSh);

comp -= comp.xxyz * bitMsk;

return comp;

}

void main() {

// the depth

float normalizedDistance = position.z / position.w;

normalizedDistance = (normalizedDistance + 1.0) / 2.0;

// bias

normalizedDistance += 0.0005;

// pack value into 32-bit RGBA texture

gl_FragColor = pack(normalizedDistance); // I am stuck here

}

And then I have the Phong shader that takes Input Texture from the above shader to create shadows.

`// Vertex shader Phong Shading - Per-pixel lighting`

uniform mat4 uMVPMatrix;

uniform mat4 normalMatrix;

// the shadow projection matrix

uniform mat4 shadowProjMatrix;

// eye pos

uniform vec3 eyePos;

// position and normal of the vertices

attribute vec4 aPosition;

attribute vec3 aNormal;

// texture variables

uniform float hasTexture;

varying float tex;

attribute vec2 textureCoord;

varying vec2 tCoord;

// lighting

uniform vec4 lightPos;

uniform vec4 lightColor;

// material

uniform vec4 matAmbient;

uniform vec4 matDiffuse;

uniform vec4 matSpecular;

uniform float matShininess;

// to pass on

varying vec3 vNormal;

varying vec3 EyespaceNormal;

varying vec4 shadowCoord;

varying vec3 lightDir, eyeVec;

void main() {

// pass on texture variables

tex = hasTexture;

tCoord = textureCoord;

// normal

EyespaceNormal = vec3(normalMatrix * vec4(aNormal, 1.0));

// the vertex position

vec4 position = uMVPMatrix * aPosition;

// light dir

lightDir = lightPos.xyz - position.xyz;

eyeVec = -position.xyz;

// shadow coordinates

mat4 bias2 = mat4(0.5, 0.0, 0.0, 0.5,

0.0, 0.5, 0.0, 0.5,

0.0, 0.0, 0.0, 0.5,

0.0, 0.0, 0.0, 1.0);

mat4 bias = mat4(0.5, 0.0, 0.0, 0.0,

0.0, 0.5, 0.0, 0.0,

0.0, 0.0, 0.0, 0.5,

0.5, 0.5, 0.5, 1.0);

mat4 shadowProjMatrix2 = bias * shadowProjMatrix;

shadowCoord = shadowProjMatrix * aPosition;

gl_Position = position;//uMVPMatrix * aPosition;

}

`// Frag shader Phong Shading - Per-pixel lighting`

precision highp float;

// texture variables

uniform sampler2D shadowTexture; // depth texture

uniform sampler2D texture1; // color texture

varying float tex;

varying vec2 tCoord;

varying vec3 vNormal;

varying vec3 EyespaceNormal;

// light

uniform vec4 lightPos;

uniform vec4 lightColor;

// shadow projection matrix

uniform mat4 shadowProjMatrix;

// material

uniform vec4 matAmbient;

uniform vec4 matDiffuse;

uniform vec4 matSpecular;

uniform float matShininess;

// eye pos

uniform vec3 eyePos;

// from vertex s

varying vec3 lightDir, eyeVec;

// shadow coordinates

varying vec4 shadowCoord;

float getShadowFactor(vec4 lightZ)

{

vec4 packedZValue = texture2D(shadowTexture, lightZ.st);

// unpack

const vec4 bitShifts = vec4(1.0 / (256.0 * 256.0 * 256.0),

1.0 / (256.0 * 256.0),

1.0 / 256.0,

1);

float shadow = dot(packedZValue , bitShifts);

return float(shadow > lightZ.z);

}

void main() {

// Just to show them being used

//vec4 a = lightPos;

vec4 b = lightColor;

vec4 c = matAmbient;

vec4 d = matDiffuse;

vec4 e = matSpecular;

vec3 g = eyePos;

float f = matShininess;

vec3 N = normalize(EyespaceNormal);

vec3 E = normalize(eyeVec);

vec3 L = normalize(lightDir);

// Reflect the vector. Use this or reflect(incidentV, N);

vec3 reflectV = reflect(-L, N);

// Get lighting terms

vec4 ambientTerm;

if (tex >= 1.0) {

ambientTerm = texture2D(texture1, tCoord);

}

else

ambientTerm = matAmbient * lightColor;

vec4 diffuseTerm = matDiffuse * max(dot(N, L), 0.0);

vec4 specularTerm = matSpecular * pow(max(dot(reflectV, E), 0.0), matShininess);

// Shadow

float sValue = 1.0;

float sValue2 = 1.0;

if (shadowCoord.w > 0.0) {

vec4 lightZ = shadowCoord / shadowCoord.w;

lightZ = (lightZ + 1.0) /2.0;

sValue = getShadowFactor(lightZ);

// scale the value from 0.5-1.0 to get a "softer" shadow for ambient

float newMin = 0.5;

float v1 = (1.0)/(1.0 - newMin);

float v2 = sValue/v1;

sValue2 = sValue + newMin;//v2 + newMin;

}

gl_FragColor = ( ambientTerm * sValue2 + (diffuseTerm + specularTerm) * sValue) ;

}

Issues I am facing :

1. How do I pass the Depth Texture to the Phong shader so that it processes the shadow.

2. Which shader should be set for which object. (I have a Plane , a Cube , a Sphere and a Cone. The cone , the sphere , and the cube lies on the plane.)

3. Do I have to use world.setglobalshader(shader)