PDA

View Full Version : Prioritize Director thread


aznan
05-08-2008, 09:00 AM
Hi!
I have some custom stereo code that runs in an eternal loop in a Director thread like so:
def updateView():
eye = False
IPD = 0.064
while True:
eye = not eye
pos = PPT.getPosition()

if eye: #right eye
pos[0] += IPD/2
else: #left eye
pos[0] -= IPD/2

window.update(viewpos = pos) #updates perspective
viz.waitframe(1)

viz.director(updateView)
This produces a fine stereo effect.

I have another function, newLayout(), that removes the objects in the world and replaces them with new objects in random locations. This function is called at the push of a button.

Problems arise when newLayout() is run however, as the adding of new objects demands quite a bit of CPU time. This keeps the stereo loop from executing, which might cause the stereo to be reversed - the right-eye image gets shown to the left eye and vice versa. But this shouldn't happen since updateView() is run in its own thread, right?

Is there a way to ensure that the stereo loop will always execute?

farshizzo
05-08-2008, 03:28 PM
What exactly are you trying to accomplish? Are you trying to manually implement field sequential stereo? It is generally not possible to do this, and you will need hardware to perform the swapping for you.

Either way, the viz.waitframe(1) command will wait for the next frame to be rendered by Vizard. So if you perform some long file loading operation then the frame will not be rendered until this is complete. So I'm not sure how increasing the priority of your thread will have any effect.

aznan
05-09-2008, 01:22 AM
I have this Window on World projection that is displayed on a horizontal table. This means that I have to change the direction of the stereo offset depending on the user's position relative to the table.

Simply offsetting pos[0] += IPD/2, like in the code above, is only there for you to get a hint at what I'm trying to accomplish. The acctual code used breaks down to finding a vector from pos to origo, imagine a plane with this vector as its normal vector, calculate the plane's side vector, then offset the position by IPD/2 along this side vector.

It would be great if I could do this through hardware, but viz.QUAD_BUFFER and the likes never change the direction of the offset. That's why I'm doing this manually.

farshizzo
05-09-2008, 02:33 PM
You should have a look at the vizcave module. You simply specify the physical locations of the projection corners and the users head position and it will automatically update the frustums to account for this. Either way, there is no reason to be using a director function for this. A simple timer will suffice.

aznan
05-11-2008, 08:53 AM
Thank you for directing me towards the cave module. I'm having some trouble with understanding quaternions for the cave's update function, though. I'm not familiar with quaternions, but a quick Google search indicates that they are used for describing a rotation, rather than a direction. How am I supposed to translate the direction of the eyes to a rotation?

Since I only keep track of one point, and not one for each eye, I assume that the user is always looking toward the center of the table. So I calculate a vector from the head's position to the table. Is it possible to somehow convert this vector into a quaternion? Or is there a better way to indicate where the user is looking? I will need this to get a proper stereo projection.

aznan
05-12-2008, 09:34 AM
I solved it by calculating my own side vector and moving the eyes' position along this vector, I then feed left/right positions into the update function instead of a position and an orientation. This works great.

aznan
05-13-2008, 12:47 AM
Dammit! Okay, one final question:
I run my cave, close it, and run it again several times in a row, not touching the code between the runs. Some runs the stereo projection is good, while on others the right and left image gets swapped. This swapping is no longer affected by things that steals CPU time, like before. It seems to occur completley at random. Here is the current code I'm using:

def updateView(self):
pos = ppt.getPosition()

# vector from head's position to the table
invec = origo - vec.vector(pos)
invec = vec.normalize(invec)

# z vector on the imaginary plane where invec is the y vector
upvec = [invec.data[0],math.sqrt(1-invec.data[1]*invec.data[1]),invec.data[2]]
upvec = vec.normalize(vec.vector(upvec))

# x vector on the imaginary plane where invec is the y vector
svec = vec.normalize(vec.cross(invec, upvec))

# calculate the eyes' positions by moving them along the x vector
lpos = move(pos, svec, self.IPD/2)
rpos = move(pos, svec, -self.IPD/2)

# update viewing positions
self.cave.update(leftPos=lpos, rightPos=rpos)
viz.MainView.translate(pos)

def move(point, vector, distance):
data = []
for i in range(3):
data.append(point[i] + vector[i]*distance)
return data

vizact.ontimer(0,view.updateView)

Do you have any idea what might be causing this?

farshizzo
05-13-2008, 10:51 AM
What stereo mode are you using in your script?

aznan
05-14-2008, 05:13 AM
Quad buffer. I currently have the users go through a simple test first to confirm whether they get the proper projection or not, so I guess I can live with it.

farshizzo
05-14-2008, 09:57 AM
Your hardware should ensure that the correct image is displayed to each eye. Your shutter glasses might not be properly synced with your graphics card. Your Vizard code looks fine, so I don't think there is anything wrong there.