Hi,
I’m trying to make a texture camera w/a custom MatrixLens. As a test, I’ve been trying to create a MatrixLens that mimics the lens of the default camera created by “base.makeCamera”.
I pulled the perspective projection calculations directly out of perspectiveLens.cxx. I also set all of the available lens variables to match that of the default camera. However, when I toggle to my MatrixLens I always get a blank screen.
Have I totally bungled something here or is this an issue w/MatrixLens.
My code is below,
Thanks
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
from pandac.PandaModules import LensNode
from pandac.PandaModules import Point3,Vec3,Vec4
from pandac.PandaModules import Mat4
from pandac.PandaModules import MatrixLens
class LensTest(DirectObject):
def __init__(self):
base.disableMouse()
#setup the texture buffer viewer
self.accept("v", base.bufferViewer.toggleEnable)
self.accept("V", base.bufferViewer.toggleEnable)
self.accept("m", self.matrixLens) #Press M to see the matrixlens
self.accept("d", self.defaultLens) #press D to see the default lens
base.bufferViewer.setPosition("llcorner")
base.bufferViewer.setCardSize(1.0, 0.0)
#we get a handle to the default window
mainWindow=base.win
#we now get buffer thats going to hold the texture of our new scene
self.altBuffer=mainWindow.makeTextureBuffer("perspectiveSurface", 512, 512)
#now we have to setup a new scene graph to make this scene
self.altRender=NodePath("perspectiveRender")
#this takes care of setting up ther camera properly
self.altCam=base.makeCamera(self.altBuffer)
self.altCam.reparentTo(self.altRender)
#setup something to look at in the texture view
#This is just a basic plane w/a texture on it
self._backdropTex = loader.loadModel("assets/models/sets/GypsyCamp/Backdrop.egg")
self._backdropTex.reparentTo(self.altRender)
#put the default camera and the texture camera at the same
#relative position
self.altCam.setPos(0,-100,0)
#Fetch the default lens to use as referenc
self._defaultLens = self.altCam.node().getLens()
#setup something to look at in the main view
#This is just a basic plane w/a texture on it
#This part is really just so you're not seeing a blank screen when you start up
self._backdrop = loader.loadModel("assets/models/sets/GypsyCamp/Backdrop.egg")
self._backdrop.reparentTo(render)
camera.setPos(0,-100,0)
print "AspectRatio: "+str(self._defaultLens.getAspectRatio())
print "Default Film Size: "+str(self._defaultLens.getFilmSize())
print "Default Focal Length: "+str(self._defaultLens.getFocalLength())
print "Default Coordinate system: "+str(self._defaultLens.getCoordinateSystem())
print "Default Far: "+str(self._defaultLens.getFar())
print "Default Near: "+str(self._defaultLens.getNear())
print "Default FOV: "+str(self._defaultLens.getFov())
print "Default Keystone: "+str(self._defaultLens.getKeystone())
print "Default ViewMat: "
print str(self._defaultLens.getViewMat())
print "Default Projection Matrix"
print (str(self._defaultLens.getProjectionMat()))
#Compute the projection matrix as done in perspectiveLens.cxx
fl = self._defaultLens.getFocalLength()
fFar = self._defaultLens.getFar();
fNear = self._defaultLens.getNear();
far_minus_near = fFar-fNear;
a = (fFar + fNear);
b = -2.0 * fFar * fNear;
newProj =Mat4( fl, 0.0, 0.0, 0.0,
0.0, 0.0, a, 1.0,
0.0, fl, 0.0, 0.0,
0.0, 0.0, b, 0.0)
#create a new MatrixLens and set it up to copy the default camera
self._matLens = MatrixLens()
self._matLens.setUserMat(newProj)
self._matLens.setFilmSize(self._defaultLens.getFilmSize())
self._matLens.setAspectRatio(self._defaultLens.getAspectRatio())
self._matLens.setFocalLength(self._defaultLens.getFocalLength())
self._matLens.setFov(self._defaultLens.getHfov())
#print out all the custom camera values and compare them w/the default camera
#they should match up perfectly
print "Custom Aspect Ratio: "+str(self._matLens.getAspectRatio())
print "Custom Film Size: "+str(self._matLens.getFilmSize())
print "Custom Focal Length: "+str(self._matLens.getFocalLength())
print "Custom Coordinate system: "+str(self._matLens.getCoordinateSystem())
print "Custom Far: "+str(self._matLens.getFar())
print "Custom Near: "+str(self._matLens.getNear())
print "Custom FOV: "+str(self._matLens.getFov())
print "Custom Keystone: "+str(self._matLens.getKeystone())
print "Custom ViewMat: "
print str(self._matLens.getViewMat())
print "User projection Matrix"
print (str(self._matLens.getProjectionMat()))
#Switching to the matrix lens makes the scene disappear
#Even though it has exactly the same parameters as the default lens
def matrixLens(self):
print("Matrix lens")
self.altCam.node().setLens(self._matLens)
#The default lens looks just fine
def defaultLens(self):
print("Default lens")
self.altCam.node().setLens(self._defaultLens)
lensTest = LensTest()
run()