WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Thread Tools Rate Thread Display Modes
Old 04-24-2013, 01:28 PM
whaleeee whaleeee is offline
Join Date: Feb 2013
Posts: 5
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:

# Directional Light
dLight = viz.addLight()
I then used the code from the third post in this thread to set up my render node and render texture and display it on the screen. Then to fix the position of the render node my code looks like this:

# 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])

# add render point
renderPoint = viz.addGroup()

# link the render point and render node
renderLink =,rn)
This seems to work, and I have the depth texture displayed on a texQuad on my screen. I have two questions to start with:

1. The line:
is kind of puzzling to me, and in fact I just put it in as a quick fix so that the render node was located somewhere along the light vectors. My question is where exactly should I put the render point? Logically it would be at the source of the light, but since it is a directional light it is infinitely far away.

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.

Reply With Quote
Old 05-06-2013, 09:23 AM
farshizzo farshizzo is offline
WorldViz Team Member
Join Date: Mar 2003
Posts: 2,849
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:
import viz
import vizact
import vizshape

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))

#Add object above object
torus = vizshape.addTorus(radius=0.2, tubeRadius=0.05, parent=group, pos=(0,1.5,8))

# 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)

#Add objects to shadow pass

#Apply shadow texture to entire group

view = viz.Matrix()

tg = viz.TextureGen()

vert = """
uniform mat4 osg_ViewMatrixInverse;
varying vec3 normal;
varying vec3 lightDir;
void main(void)
	normal = normalize(gl_NormalMatrix * gl_Normal);
	lightDir = gl_LightSource[1];
	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)


import vizcam
cam = vizcam.PivotNavigate(center=[0,1,8],distance=10)
Reply With Quote
Old 05-06-2013, 10:20 AM
whaleeee whaleeee is offline
Join Date: Feb 2013
Posts: 5
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.
Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
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 03:15 PM
To Masaki regarding render nodes surf3rguy Vizard 1 04-17-2012 05:37 PM
re: render nodes and scenes nige777 Vizard 2 10-18-2010 01:14 PM
How to apply shader and render texture to an object whj Vizard 0 04-23-2010 12:23 PM
Documentation for coding a new render node? reedev Plug-in development 2 10-30-2008 10:06 AM

All times are GMT -7. The time now is 11:29 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC