Notifications
Clear all

[Closed] Projection Matrix to Game Engine

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 = %
" mProjectionMatrix 
)

units.MetricType = myUnits


2 Replies

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 4×4 matrix to be defined (Matrix3 is 4×3).

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 4×4 matrix.

This may help.

It’s C++ code but the math is the same…