Original Post
I am trying to write a basic 3d renderer in Visual Basic (i know its not the language for it! but it allows me to focus on the theory and less on whether a pointer is null or not!) Using a derivation I found for the camera transformation I wrote the following code:
'' code
Public Sub CameraTransformation(T As Matrix)
Dim Trans(1 To 5) As Matrix
'' calculating t1
MakeIdentity Trans(1)
Trans(1).cell(4, 1) = -EyeX
Trans(1).cell(4, 2) = -EyeY
Trans(1).cell(4, 3) = -EyeZ
'' calculating t2
Dim t2_r As Single
Dim t2_stheta As Single
Dim t2_ctheta As Single
t2_r = Sqr(Square(EyeX - LookAtX) + Square(EyeY - LookAtY) + Square(EyeZ - LookAtZ))
t2_stheta = (EyeY - LookAtY) / (Sqr(Square(EyeX - LookAtX) + Square(EyeY - LookAtY)))
t2_ctheta = (EyeX - LookAtX) / (Sqr(Square(EyeX - LookAtX) + Square(EyeY - LookAtY)))
MakeIdentity Trans(2)
Trans(2).cell(1, 1) = t2_stheta
Trans(2).cell(1, 2) = t2_ctheta
Trans(2).cell(2, 1) = -t2_ctheta
Trans(2).cell(2, 2) = t2_stheta
'' calculating t3
Dim t3_r As Single
Dim t3_sphi As Single
Dim t3_cphi As Single
t3_r = t2_r
t3_sphi = Sqr(Square(EyeX - LookAtX) + Square(EyeY - LookAtY)) / t3_r
t3_cphi = (EyeZ - LookAtZ) / t3_r
MakeIdentity Trans(3)
Trans(3).cell(2, 2) = -t3_cphi
Trans(3).cell(2, 3) = -t3_sphi
Trans(3).cell(3, 2) = t3_sphi
Trans(3).cell(3, 3) = -t3_cphi
'' calculating t4
MakeIdentity Trans(4)
Trans(4).cell(1, 1) = -1
MatrixMatrixMult T, Trans(1), Trans(2)
MatrixMatrixMult T, T, Trans(3)
MatrixMatrixMult T, T, Trans(4)
''''MatrixMatrixMult T(), T(), t5() '' for roll - removed
End Sub
The problem is in the lines:
t2_stheta = (EyeY - LookAtY) / (Sqr(Square(EyeX - LookAtX) + Square(EyeY - LookAtY)))
t2_ctheta = (EyeX - LookAtX) / (Sqr(Square(EyeX - LookAtX) + Square(EyeY - LookAtY)))
When Eyex, Eyey, Lookatx, lookaty are all zero (looking down the z axis) there is division by zero. Does anyone have any idea''s on how I should fix this?
Also, is this a good formula for calculating the transformation matrix.
After this I multiply all the points (vectors) by the matrix. Should I be normalizing the vectors?
Thanks
Ian