-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathc3DEasyCam.cls
409 lines (286 loc) · 11.8 KB
/
c3DEasyCam.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "c3DEasyCam"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' NEEDS mVectors.bas
' 3D CAMERA by
' reexre - miorsoft - Roberto Mior
' WORKS ONLY with mVectorUP = 0, -1 , 0
' LEFT HAND RULES
' https://upload.wikimedia.org/wikipedia/commons/b/b2/3D_Cartesian_Coodinate_Handedness.jpg
Option Explicit
Private Const Epsilon As Single = 0.001
Attribute Epsilon.VB_VarUserMemId = 1610809345
Private Const Deg2Rad As Single = 1.74532925199433E-02 'Degrees to Radians
Attribute Deg2Rad.VB_VarUserMemId = 1073938435
Private Const Rad2Deg As Single = 57.2957795130823 'Radians to Degrees
Attribute Rad2Deg.VB_VarUserMemId = 1073938436
Private mPosition As tVec3 'Camera Position
Attribute mPosition.VB_VarUserMemId = 1073938438
Private mLookAt As tVec3 'Camera LookAT
Attribute mLookAt.VB_VarUserMemId = 1073938433
Private mScreenCenter As tVec3 'Center coords of screen
Attribute mScreenCenter.VB_VarUserMemId = 1073741830
Private camRIGHT As tVec3 'Cam Matrix
Attribute camRIGHT.VB_VarUserMemId = 1610809346
Private camUP As tVec3
Attribute camUP.VB_VarUserMemId = 1073938440
Private camFRONT As tVec3
Attribute camFRONT.VB_VarUserMemId = 1073938441
Private mVectorUP As tVec3 'Vector UP
Attribute mVectorUP.VB_VarUserMemId = 1073938442
Private mNearPlaneDist As Single
Attribute mNearPlaneDist.VB_VarUserMemId = 1073938443
Private mFarPlaneDist As Single
Attribute mFarPlaneDist.VB_VarUserMemId = 1073938444
Private mPitch As Single
Attribute mPitch.VB_VarUserMemId = 1073741837
Private mYaw As Single
Attribute mYaw.VB_VarUserMemId = 1073741838
Private mZOOM As Single
Attribute mZOOM.VB_VarUserMemId = 1610809348
Friend Property Let Position(V As tVec3)
mPosition = V
UPDATE
End Property
Friend Property Get Position() As tVec3
Position = mPosition
End Property
Friend Property Let lookat(V As tVec3)
mLookAt = V
UPDATE
End Property
Friend Property Get lookat() As tVec3
lookat = mLookAt
End Property
Friend Sub SetPositionAndLookAt(Pos As tVec3, Look As tVec3)
mPosition = Pos
mLookAt = Look
UPDATE
End Sub
Friend Property Get Direction() As tVec3
Direction = camFRONT
End Property
Friend Property Let VectorUP(UP As tVec3)
mVectorUP = Normalize3(UP)
UPDATE
End Property
Friend Property Get VectorUP() As tVec3
VectorUP = mVectorUP
End Property
Friend Sub GetRotation(ByRef Yaw As Single, ByRef Pitch As Single)
UPDATE True
Pitch = mPitch
Yaw = mYaw
End Sub
Friend Property Let Zoom(V As Single)
mZOOM = V
End Property
Friend Property Get Zoom() As Single
Zoom = mZOOM
End Property
Friend Property Let FarPlane(V As Single)
mFarPlaneDist = V
End Property
Friend Property Get FarPlane() As Single
FarPlane = mFarPlaneDist
End Property
Friend Property Let NearPlane(V As Single)
mNearPlaneDist = V
End Property
Friend Property Get NearPlane() As Single
NearPlane = mNearPlaneDist
End Property
Friend Sub Init(CameraFrom As tVec3, CameraTo As tVec3, ScreenCenter As tVec3, UP As tVec3)
mPosition = CameraFrom
mLookAt = CameraTo
mScreenCenter = ScreenCenter
mVectorUP = Normalize3(UP)
mNearPlaneDist = 5
mFarPlaneDist = 1E+32
mZOOM = 1
UPDATE
End Sub
Private Sub UPDATE(Optional UpdatePitchYawValues As Boolean = False)
' Call this every time you change Camera Position or Target !!!
' // camera matrix
Dim D As tVec3
' Left Hand
' camFRONT = Normalize3(DIFF3(mLookAt, mPosition))
' camRIGHT = Normalize3(CROSS3(mVectorUP, camFRONT))
' camUP = Normalize3(CROSS3(camRIGHT, camFRONT))
' Right Hand
camFRONT = Normalize3(DIFF3(mLookAt, mPosition))
camRIGHT = Normalize3(CROSS3(camFRONT, mVectorUP))
camUP = Normalize3(CROSS3(camFRONT, camRIGHT))
'Debug.Print ToString3(camFRONT)
'Debug.Print ToString3(camRIGHT)
'Debug.Print ToString3(camUP)
If UpdatePitchYawValues Then ' (GetRotation)
' ############ CAMERA mVectorUP MUST BE 0,-1,0
'https://math.stackexchange.com/questions/470112/calculate-camera-pitch-yaw-to-face-point
'D = DIFF3(Position, lookat)
D = camFRONT 'opposite sign than above
With D
mPitch = (-Atan2(Sqr(.X * .X + .Z * .Z), .Y)) * Rad2Deg
mYaw = (-Atan2(-.X, -.Z) + PIh) * Rad2Deg
End With
End If
End Sub
'******************************************************************
' TODO:
' http://paulbourke.net/geometry/rotate/
' http://paulbourke.net/geometry/rotate/source.c
'******************************************************************
Public Sub SetRotation(ByVal Yaw As Single, ByVal Pitch As Single)
Attribute SetRotation.VB_UserMemId = 1610809355
Dim D As Single
' Thanks to Passel:
' http://www.vbforums.com/showthread.php?870755-3D-Swimming-Fish-Algorithm&p=5356667&viewfull=1#post5356667
' ############ CAMERA mVectorUP MUST BE 0,-1,0
D = Length3(DIFF3(mPosition, mLookAt))
mPosition.X = mLookAt.X + D * (Sin(Yaw * Deg2Rad) * Cos(Pitch * Deg2Rad))
mPosition.Y = mLookAt.Y + D * (Sin(Pitch * Deg2Rad))
mPosition.Z = mLookAt.Z + D * (Cos(Yaw * Deg2Rad) * Cos(Pitch * Deg2Rad))
' ' 'cameraUP = Z
' D = Length3(DIFF3(.mPosition, .mLookAt))
' mPosition.X = mLookAt.X + D * (Sin(Yaw * Deg2Rad) * Cos(Pitch * Deg2Rad))
' mPosition.Y = mLookAt.Y + D * (Cos(Yaw * Deg2Rad) * Cos(Pitch * Deg2Rad))
' mPosition.Z = mLookAt.Z + D * (Sin(Pitch * Deg2Rad))
UPDATE
End Sub
Friend Function PointToScreenWDCam(WorldPos As tVec3, ProjectedDistFromCam As Single) As tVec3
Dim P As tVec3
Dim S As tVec3
Dim IZ As Single
S = DIFF3(WorldPos, mPosition)
P.X = DOT3(S, camRIGHT)
P.Y = DOT3(S, camUP)
P.Z = DOT3(S, camFRONT)
IZ = 1 / P.Z * mZOOM
PointToScreenWDCam.X = P.X * IZ * mScreenCenter.X + mScreenCenter.X
PointToScreenWDCam.Y = P.Y * IZ * mScreenCenter.X + mScreenCenter.Y
PointToScreenWDCam.Z = IZ ' if its negative point is behind camera
ProjectedDistFromCam = P.Z ' if its negative point is behind camera
End Function
Friend Sub PointToScreenCoords(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, _
rX As Single, rY As Single, rZ As Single, InvZ As Single)
Dim S As tVec3
Dim P As tVec3
' Dim InvZ As single
S = Vec3(X, Y, Z)
S = DIFF3(S, mPosition)
With P
.X = DOT3(S, camRIGHT)
.Y = DOT3(S, camUP)
.Z = DOT3(S, camFRONT)
InvZ = 1 / .Z
rX = .X * InvZ * mZOOM * mScreenCenter.X + mScreenCenter.X
rY = .Y * InvZ * mZOOM * mScreenCenter.X + mScreenCenter.Y
rZ = .Z ' if its negative point is behind camera
End With
End Sub
Friend Sub LineToScreen(P1 As tVec3, P2 As tVec3, Ret1 As tVec3, Ret2 As tVec3, Visible As Boolean)
Dim PlaneCenter As tVec3
Dim PlaneNormal As tVec3
Dim IntersectP1 As tVec3
Dim IntersectP2 As tVec3
Dim DfromCam1 As Single
Dim DfromCam2 As Single
Ret1 = PointToScreenWDCam(P1, DfromCam1)
Ret2 = PointToScreenWDCam(P2, DfromCam2)
Visible = False
If DfromCam1 < mNearPlaneDist Then
If DfromCam2 < mNearPlaneDist Then Exit Sub 'Both points behind camera so EXIT
'Just P1 Behind, So Find it's intersection To Near plane
PlaneNormal = camFRONT
PlaneCenter = SUM3(mPosition, MUL3(PlaneNormal, mNearPlaneDist))
IntersectP1 = RayPlaneIntersect(DIFF3(P2, P1), P1, PlaneNormal, PlaneCenter)
Ret1 = PointToScreenWDCam(IntersectP1, DfromCam1)
ElseIf DfromCam2 < mNearPlaneDist Then
If DfromCam1 < mNearPlaneDist Then Exit Sub 'Both points behind camera so EXIT
'Just P2 Behind, So Find it's intersection To Near plane
PlaneNormal = camFRONT
PlaneCenter = SUM3(mPosition, MUL3(PlaneNormal, mNearPlaneDist))
IntersectP2 = RayPlaneIntersect(DIFF3(P1, P2), P2, PlaneNormal, PlaneCenter)
Ret2 = PointToScreenWDCam(IntersectP2, DfromCam2)
End If
'Same for FAR PLANE
If DfromCam1 > mFarPlaneDist Then
If DfromCam2 > mFarPlaneDist Then Exit Sub 'Both points Too far from camera so EXIT
'Just P1 Too far from, So Find it's intersection To Far plane
PlaneNormal = camFRONT
PlaneCenter = SUM3(mPosition, MUL3(PlaneNormal, mFarPlaneDist))
IntersectP1 = RayPlaneIntersect(DIFF3(P2, P1), P1, PlaneNormal, PlaneCenter)
Ret1 = PointToScreenWDCam(IntersectP1, DfromCam1)
ElseIf DfromCam2 > mFarPlaneDist Then
If DfromCam1 > mFarPlaneDist Then Exit Sub 'Both points Too far from camera so EXIT
'Just P2 Too far from, So Find it's intersection To Far plane
PlaneNormal = camFRONT
PlaneCenter = SUM3(mPosition, MUL3(PlaneNormal, mFarPlaneDist))
IntersectP2 = RayPlaneIntersect(DIFF3(P1, P2), P2, PlaneNormal, PlaneCenter)
Ret2 = PointToScreenWDCam(IntersectP2, DfromCam2)
End If
Visible = True
End Sub
Friend Function IsPointVisibleCoords(ByVal X As Single, ByVal Y As Single, ByVal Z As Single) As Boolean
If Z < 0 Then Exit Function ' behind
If Z > mFarPlaneDist Then Exit Function ' too far
If X < 0 Then Exit Function ' check if outside screen
If Y < 0 Then Exit Function
If X > mScreenCenter.X * 2 Then Exit Function
If Y > mScreenCenter.Y * 2 Then Exit Function
IsPointVisibleCoords = True
End Function
Friend Function IsPointVisible(V As tVec3) As Boolean
With V
If .Z < 0 Then Exit Function ' behind
If .Z > mFarPlaneDist Then Exit Function ' too far
If .X < 0 Then Exit Function ' check if outside screen
If .Y < 0 Then Exit Function
If .X > mScreenCenter.X * 2 Then Exit Function
If .Y > mScreenCenter.Y * 2 Then Exit Function
End With
IsPointVisible = True
End Function
Friend Function IsPointVisibleGap(V As tVec3, Gap As Single) As Boolean
With V
If .Z < 0 Then Exit Function ' behind
If .Z - Gap > mFarPlaneDist Then Exit Function ' too far
If .X < -Gap Then Exit Function ' check if outside screen
If .Y < -Gap Then Exit Function
If .X - Gap > mScreenCenter.X * 2 Then Exit Function
If .Y - Gap > mScreenCenter.Y * 2 Then Exit Function
End With
IsPointVisibleGap = True
End Function
Friend Function IsPointVisibleGap2(X As Single, Y As Single, Z As Single, Gap As Single) As Boolean
If Z < 0 Then Exit Function ' behind
If Z - Gap > mFarPlaneDist Then Exit Function ' too far
If X < -Gap Then Exit Function ' check if outside screen
If Y < -Gap Then Exit Function
If X - Gap > mScreenCenter.X * 2 Then Exit Function
If Y - Gap > mScreenCenter.Y * 2 Then Exit Function
IsPointVisibleGap2 = True
End Function
Friend Function Follow(Target As tVec3, LookAtSpeed01 As Single, PositionSpeed01 As Single, MAXDist2 As Single, Optional MINDist2 As Single = 0, Optional PosMinY As Single = 0)
Dim D As Single
mLookAt = SUM3(MUL3(mLookAt, 1 - LookAtSpeed01), MUL3(Target, LookAtSpeed01))
D = Length32(DIFF3(mLookAt, mPosition))
If D > MAXDist2 Then
mPosition = SUM3(MUL3(mPosition, 1 - PositionSpeed01), MUL3(SUM3(Target, MUL3(camFRONT, -Sqr(MAXDist2))), PositionSpeed01))
ElseIf D < MINDist2 Then
mPosition = SUM3(MUL3(mPosition, 1 - PositionSpeed01), MUL3(SUM3(Target, MUL3(camFRONT, -Sqr(MINDist2))), PositionSpeed01))
End If
If PosMinY Then If mPosition.Y > PosMinY Then mPosition.Y = PosMinY
UPDATE
End Function