![]() |
#1
|
|||
|
|||
Setting up render node for shadowing
I'm trying to use a render texture which will be used in a shader to produce shadows from a single directional light. I set up my directional light as follows:
Code:
# Directional Light dLight = viz.addLight() dLight.setEuler(0,45,0) Code:
# add render texture [width,height] = viz.MainWindow.getSize(viz.WINDOW_PIXELS) depth = viz.addRenderTexture(size=[width,height],format=viz.TEX_DEPTH) # add render node rn = viz.addRenderNode(size=[width,height]) rn.setInheritView(False) rn.setRenderTexture(depth) # add render point renderPoint = viz.addGroup() renderPoint.setPosition(0,5,-5) renderPoint.setEuler(0,45,0) # link the render point and render node renderLink = viz.link(renderPoint,rn) 1. The line: Code:
renderPoint.setPosition(0,5,-5) 2. Does anyone have any tutorials or resources on how to do shadowing using a depth texture in Vizard? I've found several tutorials but they all use GLUT and newer versions of OpenGL. Thanks |
#2
|
|||
|
|||
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) |
#3
|
|||
|
|||
Very helpful, thank you.
It seems to me that the projectView function actually takes in the transform matrix and then computes the view matrix from that, no? Seems a bit misleading given the function name and variables. Anyway, thanks. |
![]() |
Thread Tools | |
Display Modes | Rate This Thread |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to render a texture of the transparent object and then blur it | whj | Vizard | 1 | 09-25-2012 04:15 PM |
To Masaki regarding render nodes | surf3rguy | Vizard | 1 | 04-17-2012 06:37 PM |
re: render nodes and scenes | nige777 | Vizard | 2 | 10-18-2010 02:14 PM |
How to apply shader and render texture to an object | whj | Vizard | 0 | 04-23-2010 01:23 PM |
Documentation for coding a new render node? | reedev | Plug-in development | 2 | 10-30-2008 11:06 AM |