View Single Post
  #1  
Old 05-07-2008, 09:05 AM
Frank Verberne Frank Verberne is offline
Member
 
Join Date: Mar 2008
Location: Netherlands
Posts: 148
Eyes that keep looking at the mirror

Hi all,

I'm trying to adjust my experiment so that the avatar that will be animated by a participant will keep looking at the mirror with its eyes. For testing purposes, I tried the 'biohead_eyes.vzf' file for a head with eyes that can move but I just can't seem to get the reflection right (so that the eyes in the mirror are looking at the participant when the participant looks in the mirror). So what I want is that the eyes will keep looking at the mirror even when the head is turned. With the Live Character plugin, the height of the eyes will be automatically adjusted to the height of the head I presume (I'm not sure)? See below for the code so far.
Code:
import viz

viz.go()

def addMirror(mirror,mat=None):
	#If mirror matrix is not specifed, get matrix of mirror object
	if mat is None:
		mat = mirror.getMatrix()
	#Position of mirror
	pos = viz.Vector(mat.getPosition())
	#Direction mirror is pointing
	dir = viz.Vector(mat.getForward())
	dir.normalize()
	#Quaternion rotation of mirror
	quat = mat.getQuat()
	#Create render texture
	tex = viz.addRenderTexture()
	#Create render node for rendering reflection
	lens = viz.addRenderNode(size=[1024, 1024]) #set to [1024,1024] in the lab!
	lens.attachTexture(tex)
	lens.setInheritView(True,viz.POST_MULT)
	lens.disable(viz.CULL_FACE,op=viz.OP_SET_OVERRIDE)
	#Setup reflection matrix
	rot = viz.Matrix.quat(quat)
	invRot = rot.inverse()
	lens.setMatrix(viz.Matrix.translate(pos*-1.0)*invRot*viz.Matrix.scale(1,1,-1)*rot*viz.Matrix.translate(pos))
	#Setup reflection clip plane
	s = -viz.sign(viz.Vector(dir) * viz.Vector(pos))
	plane = vizmat.Plane(pos=pos,normal=dir)
	dist = plane.distance([0,0,0])
	lens.clipPlane([-dir[0],-dir[1],-dir[2],s*dist+0.001])
	#Project reflection texture onto mirror
	mirror.texture(tex)
	mirror.texGen(viz.TEXGEN_PROJECT_EYE)

gallery = viz.add('gallery.ive')
avatar = viz.add('vcc_female.cfg')
light_avatar = viz.addLight()
face = avatar.face('biohead_eyes.vzf')
l_eye = face.getchild('geom_0')
r_eye = face.getchild('geom_1')

#Mirror surface
mirrorsurface = viz.addTexQuad()
mirrorsurfaceposition = [0, 1.25, .5] #Y,Z,X
mirrorsurface.setPosition(mirrorsurfaceposition)
mirrorsurfacerotation = [0,0,0] #Z,Y,X
mirrorsurface.rotate(mirrorsurfacerotation)
mirrorsurface.scale(2,3)
viz.MainView.getHeadLight().disable()
light_mirror = viz.addLight()
light_mirror.position(mirrorsurfaceposition[0],mirrorsurfaceposition[1],mirrorsurfaceposition[2]+2,0)
light_mirror.direction(mirrorsurfacerotation[0]/180, mirrorsurfacerotation[1]/180, mirrorsurfacerotation[2]/180)
	
#Apply reflection to mirrorsurface
m = viz.Matrix()
m.setPosition(mirrorsurface.getPosition(viz.ABS_GLOBAL))
m.setEuler(mirrorsurfacerotation[0]-180,-mirrorsurfacerotation[1], mirrorsurfacerotation[2]) #Z,X,Y
addMirror(mirrorsurface,m)

def mytimer(num):
	bone = avatar.getBone('Bip01 Head')
	pos = bone.getPosition()
	r_eye.lookat(1,pos[1],3 ,0,viz.ABSOLUTE_WORLD)
	l_eye.lookat(1,pos[1],3 ,0,viz.ABSOLUTE_WORLD)

viz.callback(viz.TIMER_EVENT,mytimer)
viz.starttimer(0,0,viz.FOREVER)
Reply With Quote