View Full Version : projecting a texture
03-09-2007, 09:00 AM
i had been using stencil shadows so far, but i'd like to give shadow mapping a try. ok, so i render the scene depth from the light's point of view into a fbo. i'm now at the next step, projecting the texture onto my geometry. i've applied this shader to the floor:
uniform mat4 u_ViewMatrix;
uniform mat4 u_ViewMatrixInverse;
uniform mat4 u_ProjectionMatrix;
uniform mat4 u_ModelMatrix;
uniform mat4 u_LightMatrix;
varying vec4 v_Projection;
gl_Position = u_ProjectionMatrix*(u_ViewMatrix*u_ModelMatrix*gl_ Vertex);
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 );
v_Projection = bias*u_ProjectionMatrix*u_LightMatrix*u_ViewMatrix Inverse*(u_ViewMatrix*u_ModelMatrix*gl_Vertex);
uniform sampler2D u_Texture0;
varying vec4 v_Projection;
gl_FragColor = texture2DProj( u_Texture0, v_Projection );
the texture is projected onto the floor, but it seems to be aligned to the camera rather than the floor's surface :p what am i doing wrong? thanks!
03-09-2007, 09:19 AM
It's because you are using the camera projection/modelview matrix, what you really need is the light projection and modelview matrix.
I wrote a small tutorial about this, i am sure you can extract the relevant bit's. http://www.flashbang.se/index.php?id=35
03-09-2007, 09:46 AM
ARGH i'm an idiot! reading your tutorial i realized that i forgot to force recomputation of the matrices -.- thanks!
03-09-2007, 05:47 PM
Also, for performance reasons you should consider adding parantheses to your matrix multiplications above. Keep in mind that evaluation is left to right and matrix by matrix multiplication is a lot more expensive than matrix by vector multiplication.
03-10-2007, 08:23 AM
ok how should i place them?
03-10-2007, 08:58 AM
Originally posted by Vexator:
ok how should i place them? for example like this:
03-10-2007, 06:22 PM
I would multiply all that matrices into one in the mainprogram (CPU side). That will be much faster than doing 5 Matrix * Vector multiplication for each Vertex.
03-12-2007, 02:40 AM
this is what it looks like with a 4096² shadow map. the shadows themselves are fine, but there are still a lot of self shadowing artifacts.
i'll get rid of them somehow, but first there are two more important iusses:
1) what's the right way to setup the light's projection matrix? should i use an orthographic or and perspective one? and should i use the screen's aspect ratio (which is 4:3) or the shadow map's (which is 1:1 of course)? atm i'm using the shadow map's ratio, which displays the shadows correctly, but the spotlight texture is stretched horizontally. if i use the shadow map's ratio, the spotlight texture is perfectly round, but the shadows are misplaced *sigh*
2) i would like to blur the shadow to improve visual quality. i know how to blur a vec2, but how should i setup an appropiate kernel for a vec4?
03-12-2007, 10:26 AM
1) orthographic or perspective projection, it all depends on what the lightsource is, for the sun orthographic might be the best and projection is best for spotlights, use whatever looks the best.
a 1:1 ratio is the best ratio, you should also change your viewport to fit this ratio when rendering the depth map.
2) the same way as a vec 2(when reading from the texture, only the two first ones really matter for blurring), but you do need to test the depth values individually for each sample.
You could also use light jittering, but that takes a lot of time and memory.
And finally, a lot of self shadowing artifacts can be hidden by multiplying the shadow value with the diffuse value
03-12-2007, 04:28 PM
thanks overlord! :D i got rid of most artifacts and i managed to soften the shadows :D i have not solved the problem with my projection matrix, although i am setting up the viewport correctly, though. i'll figure it out somehow.
next question - can i perform the deopth values comparison myself, i.e. using sampler2D instead of sampler2DShadow? i would like to soften the shadows according to the distance from their casters, so i need to be able to access the original depth values.
03-13-2007, 05:37 AM
Yes, you can sample a depth texture with sampler2D and get the actual depth.
03-13-2007, 06:29 AM
and then? what do i have to compare it with? and how? a simple less/greater test?
//edit: k i got it:
float Shadow = texture2DProj( u_Texture2, v_Projection ).r < ( v_Projection.z/v_Projection.w ) ? 0.0 : 1.0;does the trick. however, the shadow map is not being anti-alisased then. mpfh.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.