PDA

View Full Version : Projection Matrix to Game Engine


Gibbz
02-14-2007, 08:04 AM
Ok the game engine has y and Z swapped around as per normal.

this is the code im running on a selected camera to get a projection matrix. It doesnt seem to be working.

If anyone has any experience with this could they give me some help?


-- Get Projection Matrix

clearListener()
myUnits = units.MetricType
units.MetricType = #centimeters

mapped fn fnMatrixMaxToEngine valMatrix = --takes a matrix output a string
(
mRow1 = valMatrix.row1

--for i = 1 to 3 do
--if mRow1[i] < 0.00001 do
-- mRow1[i] = 0

mRow2 = valMatrix.row2

--for i = 1 to 3 do
--if mRow2[i] < 0.00001 do
-- mRow2[i] = 0

mRow3 = -(valMatrix.row3) -- invert this as cam faces backwards!

--for i = 1 to 3 do
--if mRow3[i] < 0.00000001 do
-- mRow3[i] = 0

mTranslation = valMatrix.translation
print mTranslation

--for i = 1 to 3 do
--if mTranslation[i] < 0.00001 do
-- mTranslation[i] = 0

mString = (mRow1.X as string) + ", " + (mRow1.Z as string) + ", " + (mRow1.Y as string) + ", 0, " + (mRow3.X as string) + ", " + (mRow3.Z as string) + ", " + (mRow3.Y as string) + ", 0, " + (mRow2.X as string) + ", " + (mRow2.Z as string) + ", " + (mRow2.Y as string) + ", 0, " + (mTranslation.X as string) + ", " + (mTranslation.Z as string) + ", " + (mTranslation.Y as string) + ", 1"
return mString
)

for i = 1 to selection.count do
(
mObj = selection[i]
mMatrix = matrix3 1
--mMatrix = mObj.transform

mViewMatrix = inverse mObj.transform -- get camera view matrix
print mViewMatrix

-- cam shiz
mFov = mObj.fov
mDistanceToTarget = distance mObj.pos mObj.target.pos
mWidth = mDistanceToTarget * (tan (mFov/2))

mXScale = 1/(mWidth* 2) -- light:mObj.falloff
mYScale = mXScale * getRendImageAspect() -- light: mObj.aspect

mXScale = 1/1000.0
mYScale = 1/1000.0

mMatrix = scale mMatrix [mXScale,-mYScale,-mYScale]
--mMatrix = rotateX mMatrix 90
mMatrix.translation = [0.5, 0.5, 0.0]

mProj = mMatrix

mViewProj = mViewMatrix * mProj

--mMatrix.translation = mObj.transform.translation * 1/1000.0



--mMatrix = translate mMatrix [0.5, 0, 0.5]



mProjectionMatrix = fnMatrixMaxToEngine mViewProj


format "projectorMatrix = %\n" mProjectionMatrix
)

units.MetricType = myUnits

HalfVector
02-15-2007, 07:41 PM
Hi.

If what you want is the projection matrix only, you don't need the view matrix. Also, keep in mind that a projection transform needs a 4x4 matrix to be defined (Matrix3 is 4x3).

PS: ok, seems like you are trying to calculate the matrix needed to project something from the camera's POV?. And obviously, the 4th column is all zeros except (3,4) which is 1, so forget my comment about 4x4 matrix.

HalfVector
02-16-2007, 12:21 AM
This (http://sparks.discreet.com/knowledgebase/public/solutions/ConvertMaxCamToD3DCam.htm) may help.

It's C++ code but the math is the same...

CGTalk Moderation
02-16-2007, 12:21 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.