Sub-texture accessing in fragment shader.

Started by
2 comments, last by MarcinTheBitEater 14 years, 5 months ago
Hello everyone. I have following problem to solve: how to access appropriate sub-texture texel in fragment program? In fact my texture is a shadow map. It may be, for instance, object of size 1024x1024 holding inside 4 smaller shadow maps (512x512) as a 2x2 matrix. Inside fragment shader I'm iterating through lights corresponding to shadow maps, transform fragment coordinates to light-space, divide x and y by horizontal and vertical number of sub maps and pass this coordinates to shadow2DProj function. It works as expected for first light but as I go further shadows appear erroneously... Maybe someone did something similar before or can point errors in my approach?
Advertisement
In 3x4 :
for(int LY=0;LY<4;LY++){	for(int LX=0;LX<3;LX++){		vec2 final;		final.x = coord.x/3 + (1.0/3)*LX;		final.y = coord.y/4 + (1.0/4)*LY;				// here use textureXXX(..,final);	}}
Still it doesn't work correctly and I don't know what's broken :/

In first pass I create shadow maps for each light and pack them all in one big texture. I've checked shadow map and it is ok.

This is how I do it in fragment shader:

uniform sampler2DShadow depthMap;   //! shadow map for all lightsuniform mat4 lproj;                 //! light projection matrixuniform mat4 camIMV;                //! inverse of camera modelview matrixuniform sampler2D lights;           //! texture container for light positionsvoid main(){    vec4 I, Id, Is, shadowCoord, Ia;    vec3 n, lv, rv, vv, lightV;    float NdotL, RdotV, power;    vec2 coords;    float shadow;    mat4 transform, lightMV, temp;	    //! Bias matrix    mat4 bias = mat4( 0.5, 0.0, 0.0, 0.0,	              0.0, 0.5, 0.0, 0.0,	              0.0, 0.0, 0.5, 0.0,	              0.5, 0.5, 0.5, 1.0 );	              	temp = bias * lproj;		n = normalize(normal);	vv = normalize( viewerVec );	I = vec4( 0.0, 0.0, 0.0, 0.0 );		for( int i = 1; i &lt; 2; ++i )	{		for( int j = 0; j &lt; 1; ++j )		{			float x = float(i);			coords = vec2(x/4.0, y/1.0);			lightV = vec3( texture2D( lights, coords ) );							lv = normalize(lightV);			NdotL = max(dot(n,lv),0.0);				rv = 2.0 * NdotL * n - lv;			Id = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * NdotL;			Is = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow( max( dot(rv, vv), 0.0), gl_FrontMaterial.shininess );			I += Id + Is;							lightMV = lookAt( vec3(lightV), vec3(0.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0) );						transform = temp * lightMV * camIMV;			shadowCoord = transform * texCoord;						shadowCoord.x /= 4.0; shadowCoord.x = shadowCoord.x + (1.0/4.0)*float(i); 			shadow = shadow2DProj(depthMap, shadowCoord).z;			if(shadow &lt; (shadowCoord.z/25.0+0.03) )				shadow = 0.5;			else				shadow = 1.0;							I = I * shadow;		}    }}


Any clue? I wonder why shadowCoord.x and shadowCoord.y doesn/t fall into range [0,1]...
I found that there was an issue with reading lights from texture. I've fixed it but there is still problem with shadows. Currently Phong lighting work properly but only first shadow is displayed correctly. Other are skewed and appear when I don't want them. There still must be some kind of problem with scaling coordinates or projection with shadow2dproj...

I hope someone can help.

This topic is closed to new replies.

Advertisement