farshizzo |
05-06-2013 09:23 AM |
1) You will typically use an orthographic projection matrix when rendering for directional lights. With regards to where to place the camera, it depends on your scene. In general, you will center the camera about the center of the scene, point it in the light direction, then move it back until all the shadow casting geometry is visible. If you know ahead of time that all your geometry will be confined to a particular area, then you can just pre-compute the position of the camera.
2) Here is a sample script that implements depth shadows:
Code:
import viz
import vizact
import vizshape
viz.go()
group = viz.addGroup()
#Add ground plane
ground = vizshape.addPlane(parent=group)
#Add object above ground
logo = viz.add('logo.ive', parent=group, scale=(0.3,0.3,0.3), pos=(0,0.5,8))
logo.addAction(vizact.spin(0,1,0,45))
#Add object above object
torus = vizshape.addTorus(radius=0.2, tubeRadius=0.05, parent=group, pos=(0,1.5,8))
torus.addAction(vizact.spin(1,0,0,45))
# Shadow light position and direction
size = 1024
area = [2,2]
pos = [0,10,8]
euler = [0,90,0]
proj = viz.Matrix.ortho(-area[0]/2.0,area[0]/2.0,-area[1]/2.0,area[1]/2.0,1,10)
# Create light
light = viz.addDirectionalLight(euler=euler)
#Add depth texture
depthTex = viz.addRenderTexture(format=viz.TEX_DEPTH,wrap=viz.CLAMP_TO_BORDER,borderColor=viz.WHITE,shadow=True)
#Create render node to render shadow caster to texture
shadowPass = viz.addRenderNode(size=[size,size],pos=pos,euler=euler,inheritView=False,autoClip=False)
shadowPass.setRenderTexture(depthTex)
shadowPass.setProjectionMatrix(proj)
shadowPass.setScene(None)
shadowPass.enable(viz.CULL_FACE,op=viz.OP_OVERRIDE)
shadowPass.disable(viz.COLOR_WRITE,op=viz.OP_OVERRIDE)
shadowPass.zoffset(6,0,op=viz.OP_OVERRIDE)
shadowPass.disable(viz.LIGHTING,op=viz.OP_OVERRIDE)
#Add objects to shadow pass
logo.addParent(shadowPass)
torus.addParent(shadowPass)
#Apply shadow texture to entire group
group.texture(depthTex)
view = viz.Matrix()
view.setEuler(euler)
view.setPosition(pos)
tg = viz.TextureGen()
tg.projectView(view,proj)
group.texGen(tg)
vert = """
uniform mat4 osg_ViewMatrixInverse;
varying vec3 normal;
varying vec3 lightDir;
void main(void)
{
normal = normalize(gl_NormalMatrix * gl_Normal);
lightDir = gl_LightSource[1].position.xyz;
gl_Position = ftransform();
vec4 pos = gl_ModelViewMatrix * gl_Vertex;
gl_TexCoord[0].s = dot( pos, gl_EyePlaneS[0] );
gl_TexCoord[0].t = dot( pos, gl_EyePlaneT[0] );
gl_TexCoord[0].p = dot( pos, gl_EyePlaneR[0] );
gl_TexCoord[0].q = dot( pos, gl_EyePlaneQ[0] );
}
"""
frag = """
uniform sampler2DShadow shadowTexture;
varying vec3 normal;
varying vec3 lightDir;
void main(void)
{
float d = max( dot(normalize(lightDir),normalize(normal)) , 0.0 );
float shadow = shadow2DProj( shadowTexture, gl_TexCoord[0] ).r;
vec4 color = gl_FrontMaterial.diffuse;
gl_FragColor.rgb = shadow * d * color.rgb;
gl_FragColor.rgb += 0.6 * color.rgb;
gl_FragColor.a = color.a;
}
"""
shader = viz.addShader(vert=vert,frag=frag)
shader.attach(viz.addUniformInt('shadowTexture',0))
group.apply(shader)
viz.clearcolor(viz.GRAY)
import vizcam
cam = vizcam.PivotNavigate(center=[0,1,8],distance=10)
|