Hi, I'm trying to make 3D clipping for my 3D projection program, and I am having trouble with the FOV calculations.
The problem is that I set FOV to 45, but a point placed at 45 degrees from the camera does not appear at the edge of the viewport.
I have included an example of my test project in VB6.0 with comments below to illustrate the problem
if you have vb6, you can create a standard .exe and paste all the code into project with default settings (one form), and it will work. Left click on the form in the "Top" viewport to set the test point's position. Putting the testpoint at the origin(since FOV = 90) should have it at 45 degrees from the camera, but it is NOT at the edge of the perspective viewport =/
Thanks, Nick.
The code:(I didnt use code tags because it makes it difficult to copy/paste sometimes)
------------------------------------------------
Const RAD As Double = 3.141592654 / 180 'Used to convert Degrees to Radians
Private Type Vector3D
X As Double
Y As Double
Z As Double
End Type
Dim Camera As Vector3D
Dim TestPoint As Vector3D
Dim FOV As Double 'Field of View
Dim FOCAL_LENGTH As Double 'Focal Length
Dim msdown As Boolean
Private Sub Form_Load()
Me.ScaleMode = vbPixels
Me.Width = 409 * Screen.TwipsPerPixelX
Me.Height = 228 * Screen.TwipsPerPixelY
'initial setup of camera and testpoint
Camera.X = -25
Camera.Y = 0
Camera.Z = -25
TestPoint.X = -25
TestPoint.Y = 0
TestPoint.Z = 25
FOV = 90
FOCAL_LENGTH = (200# / 2#) / Tan(FOV / 2#)
'FOCAL_LENGTH = (ViewHeight / 2) / tan(FOV / 2)
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
msdown = True
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'set the position of the test point...
If msdown Then
If X < 200 Then
TestPoint.X = 200 - 300
ElseIf X > 400 Then
TestPoint.X = 400 - 300
Else
TestPoint.X = X - 300
End If
If Y < 0 Then
TestPoint.Z = 100
ElseIf Y > 200 Then
TestPoint.Z = -100
Else
TestPoint.Z = -(Y - 100)
End If
Form_Paint
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
msdown = False
End Sub
Private Sub Form_Paint()
'clear screen
Cls
'size of lines for interface
Me.DrawWidth = 1
'draw interface
Me.Line (0, 0)-(400, 200), 0, B
Me.Line (200, 0)-(200, 200), 0
Me.Line (200, 100)-(400, 100), 0
Me.Line (300, 0)-(300, 200), 0
''
'draw view vector
Me.Line (Camera.X + 300, -Camera.Z + 100)-(Camera.X + 300, -Camera.Z + 80), vbRed
'size of points on right grid
Me.DrawWidth = 5
'draw camera position on right grid
Me.PSet (Camera.X + 300, -Camera.Z + 100), vbRed
'draw testpoint on right grid
Me.PSet (TestPoint.X + 300, -TestPoint.Z + 100), vbBlue
'incase of division by zero
On Error Resume Next
'set drawsize to create perspective perception
DrawWidth = 200 - ((TestPoint.Z + 100) - (Camera.Z + 100))
'if testpoint is infront of camera
If (TestPoint.Z - Camera.Z) > 0 Then
'draw the point in the left perspective viewport
Me.PSet ((TestPoint.X - Camera.X) / (TestPoint.Z - Camera.Z) * FOCAL_LENGTH + 100, (TestPoint.Y - Camera.Y) / (TestPoint.Z - Camera.Z) * FOCAL_LENGTH + 100), vbGreen
End If
'label the viewports
Me.CurrentX = 1
Me.CurrentY = 0
Me.Print "Perspective"
Me.CurrentX = 201
Me.CurrentY = 0
Me.Print "Top"
End Sub
-----------------------------------------