Модератор: Mikle
Public Function rayCollision(Start As D3DVECTOR, Direction As D3DVECTOR, Vertice1 As D3DVECTOR, Vertice2 As D3DVECTOR, Vertice3 As D3DVECTOR, t As Double, U As Double, V As Double, Optional NoCollideI As Boolean = False) As Byte
Dim edge1 As D3DVECTOR
Dim edge2 As D3DVECTOR
Dim tvec As D3DVECTOR, pvec As D3DVECTOR, qvec As D3DVECTOR
Dim inv_det As Double
Dim det As Double
On Local Error Resume Next
'Íàõîäèì ãðàíè
D3DXVec3Subtract edge1, Vertice2, Vertice1
D3DXVec3Subtract edge2, Vertice3, Vertice1
D3DXVec3Cross pvec, Direction, edge2
'Íàõîäèì äåòåðìèíàíò.Åñëè îí áëèçêî ê íóëþ, çíà÷èò òî÷êà âíå òðåóãîëüíèêà
det = D3DXVec3Dot(edge1, pvec)
If det > -EPSILON And det < EPSILON Then
rayCollision = 0
Exit Function
End If
inv_det = 1 / det
'Íàõîäèì u è ïðîâåðÿåì
D3DXVec3Subtract tvec, Start, Vertice1
U = D3DXVec3Dot(tvec, pvec) * inv_det
If U < 0 And U > 1 Then
rayCollision = 0
Exit Function
End If
'------""------- v è ïðîâåðÿåì
D3DXVec3Cross qvec, tvec, edge1
V = D3DXVec3Dot(Direction, qvec) * inv_det
If V < 0 And U + V > 1 Then
rayCollision = 0
Exit Function
End If
'ïîñ÷èòàåì t,òðåóãîëüíèê ïåðåñå÷åííûé ëó÷îì
t = D3DXVec3Dot(edge2, qvec) * inv_det
rayCollision = 1
End Function
Есть направление луча.Нужно найти полигон, где луч пересекает его на ландшафте.
он всё время стоит в центре экрана или формы
Dim t As Double, U As Double, V As Double, t1 As Double, u1 As Double
Dim vdir As D3DVECTOR
Dim v1 As Double, v2, v3, v4, v5, v6
Dim a As Long, b As Long, c As Long, d As Long, e As Long, f As Long
Dim maxx As Single, maxy As Single, tempv As D3DVECTOR
tempv = CamPos
maxx = LS.SizeX / 16
maxy = LS.SizeZ / 16
vdir = GetMousePickDirection(X, Y)
a = Int(tempv.X / 256)
b = Int(tempv.z / 256)
c = Int((tempv.X Mod 256) / 16)
d = Int((tempv.z Mod 256) / 16)
e = a * ((1 / 16) + 1)
f = b * ((1 / 16) + 1)
Dim i As Long
For i = 0 To 1 / 64
If d >= 0 And d < maxy - 1 And c >= 0 And c < maxx Then
v3 = f * maxy + e
v2 = (f + 1) * maxy + e
v1 = (f + 1) * maxy + e + 1
v6 = f * maxy + e
v5 = (f + 1) * maxy + e + 1
v4 = f * maxy + e + 1
Dim g As Byte, g1 As Byte
g = rayCollision(vec3(X, Y, 0), vdir, Vert(vBuf(v1, v1)), Vert(vBuf(v2, v2)), Vert(vBuf(v3, v3)), t, U, V)
g = rayCollision(vec3(X, Y, 0), vdir, Vert(vBuf(v4, v4)), Vert(vBuf(v5, v5)), Vert(vBuf(v6, v6)), t1, u1, v1)
If (g = 1 And t < 1 And t >= 0) Or (g1 = 1 And t1 < 1 And t1 >= 0) Then
Label1.Caption = Str(vBuf(v4, v4).Pos.z)
End If
End If
tempv = VAdd(tempv, VScale(vdir, 4))
Next i
Public Function GetMousePickDirection(X As Single, Y As Single) As D3DVECTOR
Dim Mtemp As D3DMATRIX
Dim Mtemp2 As D3DMATRIX
Dim view As D3DVIEWPORT8
d3dDevice.GetTransform D3DTS_WORLD, Mtemp
d3dDevice.GetViewport view
On Local Error Resume Next
Cursor.X = X
Cursor.Y = Y
Cursor.z = 0
d3dDevice.GetTransform D3DTS_VIEW, Mtemp2
D3DXVec3Unproject vNear, Cursor, view, pjm, Mtemp2, Mtemp
Cursor.z = 1
D3DXVec3Unproject vFar, Cursor, view, pjm, Mtemp2, Mtemp
D3DXVec3Subtract GetMousePickDirection, vFar, vNear
End Function
SetTexture 0,Main
DrawIndexedPrimitivesUP 'Рендеринг ландшафта
If DrawMultitexture Then
SetTexture 0,Add
DrawIndexedPrimitivesUP 'Рендеринг тех полигонов, которые 'политекстурировыны
End if
d3drm писал(а):DirectXManiac писал(а):Я прошу посмотрите d3drm ПОЖАЛУЙСТА!!!!
не надо меня смотреть....
Mikle писал(а):DirectXManiac
Только для тайлинга придется TriangleStrip переделать на TriangleList.
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 4