WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Setting up render node for shadowing (https://forum.worldviz.com/showthread.php?t=4558)

whaleeee 04-24-2013 01:28 PM

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)

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:

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)

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:
Code:

renderPoint.setPosition(0,5,-5)
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.

Thanks

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)


whaleeee 05-06-2013 10:20 AM

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.


All times are GMT -7. The time now is 01:26 AM.

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