943,733 Members | Top Members by Rank

Ad:
Jan 23rd, 2009
0

FOV question : VB6 Example of problem

Expand Post »
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 codeI 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

-----------------------------------------
Reputation Points: 14
Solved Threads: 4
Junior Poster
VBNick is offline Offline
151 posts
since Jan 2008
Jan 23rd, 2009
0

Re: FOV question : VB6 Example of problem

btw: program uses left handed coordinates system.

edit: Ugh...I can't believe it...I forgot to convert the FOV to radians...
so: FOV = 90
should be
FOV = 90 * RAD

well, thanks for looking, if you guys want an example of FOV in your code library...
Last edited by VBNick; Jan 23rd, 2009 at 1:47 pm.
Reputation Points: 14
Solved Threads: 4
Junior Poster
VBNick is offline Offline
151 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Game Development Forum Timeline: gaming mods
Next Thread in Game Development Forum Timeline: Will a naked Lara Croft save Tomb Raider?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC