News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Line-Tri Intersection

Started by Farabi, February 01, 2014, 12:33:40 PM

Previous topic - Next topic

Farabi

I've finally understood how it worked but I'll skip the explanation and get the code to you.

I think the code and how to use it is straight forward. If anyone found difficulty on how to use it, please leave me a message. If anyone want the full source code of the graphic rendering engine, I can upload it, the size is 1.2 MBytes, I cant upload it in here.


Tri struct
pt1 VERTEX <>
pt2 VERTEX <>
pt3 VERTEX <>
Tri ends

Ray struct
linept VERTEX <>
vector VERTEX <>
Ray ends

fCheckSameClockDir proc uses esi edi lpTri:dword,norm:dword
LOCAL testi, testj, testk:real4
LOCAL dotprod:real4
LOCAL tmp,tmp2,tmp3,tmp4:real4
LOCAL AA,BB:real4

mov esi,lpTri
mov edi,norm

fld [esi].Tri.pt2.y
fsub [esi].Tri.pt1.y
fstp tmp

fld [esi].Tri.pt3.z
fsub [esi].Tri.pt1.z
fstp tmp2

fld [esi].Tri.pt3.y
fsub [esi].Tri.pt1.y
fstp tmp3

fld [esi].Tri.pt2.z
fsub [esi].Tri.pt1.z
fstp tmp4

fld tmp
fmul tmp2
fstp AA

fld tmp3
fmul tmp4
fstp BB

fld AA
fsub BB
fstp testi
;--------------------------------------------------------------------------------

fld [esi].Tri.pt2.z
fsub [esi].Tri.pt1.z
fstp tmp

fld [esi].Tri.pt3.x
fsub [esi].Tri.pt1.x
fstp tmp2

fld [esi].Tri.pt3.z
fsub [esi].Tri.pt1.z
fstp tmp3

fld [esi].Tri.pt2.x
fsub [esi].Tri.pt1.x
fstp tmp4

fld tmp
fmul tmp2
fstp AA

fld tmp3
fmul tmp4
fstp BB

fld AA
fsub BB
fstp testj
;--------------------------------------------------------------------------------
fld [esi].Tri.pt2.x
fsub [esi].Tri.pt1.x
fstp tmp

fld [esi].Tri.pt3.y
fsub [esi].Tri.pt1.y
fstp tmp2

fld [esi].Tri.pt3.x
fsub [esi].Tri.pt1.x
fstp tmp3

fld [esi].Tri.pt2.y
fsub [esi].Tri.pt1.y
fstp tmp4

fld tmp
fmul tmp2
fstp AA

fld tmp3
fmul tmp4
fstp BB

fld AA
fsub BB
fstp testk
;--------------------------------------------------------------------------------

fld testi
fmul [edi].VERTEX.x
fstp tmp

fld testj
fmul [edi].VERTEX.y
fadd tmp
fstp tmp

fld testk
fmul [edi].VERTEX.z
fadd tmp
fstp dotprod


xor eax,eax
fldz
fld dotprod
fcomip st(0),st(1)
jb @f
inc eax
@@:


ret
fCheckSameClockDir endp

fRayTriIntersect proc uses esi edi ebx lpRay:dword,lpTri:dword,lpIntersect:dword,nMode:dword
LOCAL v1:VERTEX
LOCAL v2:VERTEX
LOCAL v3:VERTEX
LOCAL delta:VERTEX
LOCAL norm:VERTEX
LOCAL dotprod:real4
LOCAL t:real4
LOCAL tmp,tmp2:real4
LOCAL PCHECK:Tri
LOCAL q:qword
LOCAL buff[256]:dword
LOCAL dist1,dist2,ddist:dword
LOCAL vtest:VERTEX
LOCAL intersectpos:VERTEX

mov esi,lpTri
mov edi,lpRay
mov ebx,lpIntersect

invoke Vec_Sub,addr v1,addr [esi].Tri.pt2,addr [esi].Tri.pt1
invoke Vec_Sub,addr v2,addr [esi].Tri.pt3,addr [esi].Tri.pt2

invoke Vec_CrossProduct,addr norm,addr v1,addr v2
invoke Vec_DotProduct,addr norm,addr [edi].Ray.vector
fstp dotprod

; fld dotprod
; fstp q
; invoke FloatToStr2,q,addr buff
; invoke MessageBox,0,addr buff,0,0

fldz
fld dotprod
fcomip st(0),st(1)
je coplanar
ja @f
fstp st(0)
xor eax,eax
dec eax
ret
@@:
fstp st(0)

fld [edi].Ray.linept.x
fsub [esi].Tri.pt1.x
fmul norm.x
fstp tmp
fld [edi].Ray.linept.y
fsub [esi].Tri.pt1.y
fmul norm.y
fadd tmp
fstp tmp
fld [edi].Ray.linept.z
fsub [esi].Tri.pt1.z
fmul norm.z
fadd tmp
fchs
fstp tmp

fld norm.x
fmul [edi].Ray.vector.x
fstp tmp2
fld norm.y
fmul [edi].Ray.vector.y
fadd tmp2
fstp tmp2
fld norm.z
fmul [edi].Ray.vector.z
fadd tmp2
fstp tmp2

fld tmp
fdiv tmp2
fstp t

; fld t
; fstp q
; invoke FloatToStr2,q,addr buff
; invoke MessageBox,0,addr buff,0,0


fldz
fld t
fcomip st(0),st(1)
ja @f
fstp st(0)
xor eax,eax
ret
@@:
fstp st(0)

.if nMode==fLINE_MODE
fld1
fld t
fcomip st(0),st(1)
jbe @f
xor eax,eax
ret
@@:
.endif
;invoke MessageBox,0,CADD("T is above 0"),0,0

mov ebx,lpIntersect
fld [edi].Ray.vector.x
fmul t
fadd [edi].Ray.linept.x
fstp [ebx].VERTEX.x

fld [edi].Ray.vector.y
fmul t
fadd [edi].Ray.linept.y
fstp [ebx].VERTEX.y

fld [edi].Ray.vector.z
fmul t
fadd [edi].Ray.linept.z
fstp [ebx].VERTEX.z

invoke MemCopy,esi,addr PCHECK,sizeof Tri
invoke MemCopy,ebx,addr PCHECK.pt3,sizeof VERTEX
invoke fCheckSameClockDir,addr PCHECK,addr norm
.if eax==1
; invoke MessageBox,0,CADD("Stage 1"),0,0
invoke MemCopy,addr [esi].Tri.pt2,addr PCHECK.pt1,sizeof VERTEX
invoke MemCopy,addr [esi].Tri.pt3,addr PCHECK.pt2,sizeof VERTEX
invoke fCheckSameClockDir,addr PCHECK,addr norm
.if eax==1
; invoke MessageBox,0,CADD("Stage 2"),0,0
invoke MemCopy,addr [esi].Tri.pt3,addr PCHECK.pt1,sizeof VERTEX
invoke MemCopy,addr [esi].Tri.pt1,addr PCHECK.pt2,sizeof VERTEX
invoke fCheckSameClockDir,addr PCHECK,addr norm
.if eax==1
ret
.endif
.endif
.endif
ret
coplanar:

fstp st(0)

invoke Vec_Sub,addr v1,addr[edi].Ray.linept,addr [esi].Tri.pt1
invoke Vec_DotProduct,addr v1,addr v1
fstp dotprod

fld FP4(0.)
fld dotprod
fcomip st(0),st(1)
jz @F
fstp st(0)
xor eax,eax
ret
@@:
fstp st(0)

mov eax,1

ret
fRayTriIntersect endp
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165