Advertisement
Guest User

Untitled

a guest
Apr 27th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.93 KB | Source Code | 0 0
  1. typedef struct XMFLOAT4{
  2.     float x;
  3.     float y;
  4.     float z;
  5.     float w;
  6. }XMFLOAT4;
  7.  
  8. typedef struct XMFLOAT2{
  9.     float x;
  10.     float y;
  11. }XMFLOAT2;
  12.  
  13. typedef struct XMFLOAT3{
  14.     float x;
  15.     float y;
  16.     float z;
  17. }XMFLOAT3;
  18.  
  19. typedef struct WORKDATA{
  20.     XMFLOAT4 dir;
  21.     float dist;
  22.     int Index;
  23.     int Index2;
  24. }WORKDATA;
  25.  
  26. typedef struct Vertex
  27. {
  28.     XMFLOAT3 position;
  29.     XMFLOAT2 tc;
  30.     XMFLOAT3 normal;
  31. }Vertex;
  32.  
  33. typedef struct MODEL{
  34.     int index[3];
  35.     Vertex vects[3];
  36. }MODEL;
  37.  
  38. typedef struct RETURNDATA{
  39.     bool coll;
  40.     XMFLOAT3 pos;
  41. }RETURNDATA;
  42.  
  43. typedef struct Intersect{
  44. bool coplanar;
  45. float isect0;
  46. float isect1;
  47. XMFLOAT3 isectpoint0;
  48. XMFLOAT3 isectpoint1;
  49. }Intersect;
  50.  
  51. #define SORT(a,b)       \
  52. if (a > b)    \
  53.              {          \
  54.                float c; \
  55.                c = a;     \
  56.                a = b;     \
  57.                b = c;     \
  58.              }
  59.  
  60. #define POINT_IN_TRI(V0,U0,U1,U2)           \
  61. {                                           \
  62.   float a,b,c,d0,d1,d2;                     \
  63.   /* is T1 completly inside T2? */          \
  64.   /* check if V0 is inside tri(U0,U1,U2) */ \
  65.   a=U1[i1]-U0[i1];                          \
  66.   b=-(U1[i0]-U0[i0]);                       \
  67.   c=-a*U0[i0]-b*U0[i1];                     \
  68.   d0=a*V0[i0]+b*V0[i1]+c;                   \
  69.                                             \
  70.   a=U2[i1]-U1[i1];                          \
  71.   b=-(U2[i0]-U1[i0]);                       \
  72.   c=-a*U1[i0]-b*U1[i1];                     \
  73.   d1=a*V0[i0]+b*V0[i1]+c;                   \
  74.                                             \
  75.   a=U0[i1]-U2[i1];                          \
  76.   b=-(U0[i0]-U2[i0]);                       \
  77.   c=-a*U2[i0]-b*U2[i1];                     \
  78.   d2=a*V0[i0]+b*V0[i1]+c;                   \
  79.   if(d0*d1>0.0)                             \
  80.   {                                         \
  81.     if(d0*d2>0.0) return true;                 \
  82.   }                                         \
  83. }
  84.  
  85. #define EDGE_EDGE_TEST(V0,U0,U1)                      \
  86.   Bx=U0[i0]-U1[i0];                                   \
  87.   By=U0[i1]-U1[i1];                                   \
  88.   Cx=V0[i0]-U0[i0];                                   \
  89.   Cy=V0[i1]-U0[i1];                                   \
  90.   f=Ay*Bx-Ax*By;                                      \
  91.   d=By*Cx-Bx*Cy;                                      \
  92.   if((f>0 && d>=0 && d<=f) || (f<0 && d<=0 && d>=f))  \
  93.   {                                                   \
  94.     e=Ax*Cy-Ay*Cx;                                    \
  95.     if(f>0)                                           \
  96.     {                                                 \
  97.       if(e>=0 && e<=f) return true;                      \
  98.     }                                                 \
  99.     else                                              \
  100.     {                                                 \
  101.       if(e<=0 && e>=f) return true;                      \
  102.     }                                                 \
  103. }
  104.  
  105. #define EDGE_AGAINST_TRI_EDGES(V0,V1,U0,U1,U2) \
  106. {                                              \
  107.   float Ax,Ay,Bx,By,Cx,Cy,e,d,f;               \
  108.   Ax=V1[i0]-V0[i0];                            \
  109.   Ay=V1[i1]-V0[i1];                            \
  110.   /* test edge U0,U1 against V0,V1 */          \
  111.   EDGE_EDGE_TEST(V0,U0,U1);                    \
  112.   /* test edge U1,U2 against V0,V1 */          \
  113.   EDGE_EDGE_TEST(V0,U1,U2);                    \
  114.   /* test edge U2,U1 against V0,V1 */          \
  115.   EDGE_EDGE_TEST(V0,U2,U0);                    \
  116. }
  117.  
  118.  
  119.  
  120. float fDistance(XMFLOAT3 pos1, XMFLOAT3 pos2) {
  121.     float x = pow((pos2.x - pos1.x), 2);
  122.     float y = pow((pos2.y - pos1.y), 2);
  123.     float z = pow((pos2.z - pos1.z), 2);
  124.     return sqrt(x + y + z);
  125. }
  126.  
  127. XMFLOAT3 AddXMFLOAT3(XMFLOAT3 a, XMFLOAT3 b) {
  128.     XMFLOAT3 result = {0,0,0};
  129.  
  130.     result.x = a.x + b.x;
  131.     result.y = a.y + b.y;
  132.     result.z = a.z + b.z;
  133.  
  134.     return result;
  135. }
  136.  
  137. XMFLOAT3 SubXMFLOAT3(XMFLOAT3 a, XMFLOAT3 b) {
  138.     XMFLOAT3 result = {0,0,0};
  139.  
  140.     result.x = a.x - b.x;
  141.     result.y = a.y - b.y;
  142.     result.z = a.z - b.z;
  143.  
  144.     return result;
  145. }
  146.  
  147. XMFLOAT3 MulXMFLOAT3(XMFLOAT3 a, float b) {
  148.     XMFLOAT3 result = {0,0,0};
  149.  
  150.     result.x = a.x * b;
  151.     result.y = a.y * b;
  152.     result.z = a.z * b;
  153.  
  154.     return result;
  155. }
  156.  
  157. XMFLOAT3 XMVector3Cross(XMFLOAT3 a, XMFLOAT3 b) {
  158.     XMFLOAT3 result = {0,0,0};
  159.  
  160.     result.x = a.y * b.z - a.z * b.y;
  161.     result.y = a.z + b.x - a.x * b.z;
  162.     result.z = a.x + b.y - a.y * b.x;
  163.  
  164.     return result;
  165. }
  166. float XMVector3Dot(XMFLOAT3 a, XMFLOAT3 b) {
  167.     float result = 0;
  168.     result += a.x*b.x;
  169.     result += a.y*b.y;
  170.     result += a.z*b.z;
  171.     return result;
  172. }
  173.  
  174. bool CoplanTriTri(XMFLOAT3 XN, float V0[3], float V1[3], float V2[3],
  175.                      float U0[3], float U1[3], float U2[3])
  176. {
  177.     float A[3];
  178.     short i0, i1;
  179.     A[0] = fabs(XN.x);
  180.     A[1] = fabs(XN.y);
  181.     A[2] = fabs(XN.z);
  182.     if (A[0] > A[1])
  183.     {
  184.         if (A[0] > A[2])
  185.         {
  186.             i0 = 1;      /* A[0] is greatest */
  187.             i1 = 2;
  188.         }
  189.         else
  190.         {
  191.             i0 = 0;      /* A[2] is greatest */
  192.             i1 = 1;
  193.         }
  194.     }
  195.     else   /* A[0]<=A[1] */
  196.     {
  197.         if (A[2] > A[1])
  198.         {
  199.             i0 = 0;      /* A[2] is greatest */
  200.             i1 = 1;
  201.         }
  202.         else
  203.         {
  204.             i0 = 0;      /* A[1] is greatest */
  205.             i1 = 2;
  206.         }
  207.     }
  208.  
  209.     EDGE_AGAINST_TRI_EDGES(V0,V1,U0,U1,U2);
  210.     EDGE_AGAINST_TRI_EDGES(V1,V2,U0,U1,U2);
  211.     EDGE_AGAINST_TRI_EDGES(V2,V0,U0,U1,U2);
  212.  
  213.     POINT_IN_TRI(V0,U0,U1,U2);
  214.     POINT_IN_TRI(U0,V0,V1,V2);
  215.  
  216.     return false;
  217. }
  218.  
  219.  
  220. #define NEWCOMPUTE_INTERVALS(VV0,VV1,VV2,D0,D1,D2,D0D1,D0D2,A,B,C,X0,X1) \
  221. { \
  222.         if (D0D1 > 0.0f) \
  223.         { \
  224.                 /* here we know that D0D2<=0.0 */ \
  225.             /* that is D0, D1 are on the same side, D2 on the other or on the plane */ \
  226.                 A = VV2; B = (VV0 - VV2) * D2; C = (VV1 - VV2) * D2; X0 = D2 - D0; X1 = D2 - D1; \
  227.         } \
  228.         else if (D0D2 > 0.0f)\
  229.         { \
  230.                 /* here we know that d0d1<=0.0 */ \
  231.             A = VV1; B = (VV0 - VV1) * D1; C = (VV2 - VV1) * D1; X0 = D1 - D0; X1 = D1 - D2; \
  232.         } \
  233.         else if (D1 * D2 > 0.0f || D0 != 0.0f) \
  234.         { \
  235.                 /* here we know that d0d1<=0.0 or that D0!=0.0 */ \
  236.                 A = VV0; B = (VV1 - VV0) * D0; C = (VV2 - VV0) * D0; X0 = D0 - D1; X1 = D0 - D2; \
  237.         } \
  238.         else if (D1 != 0.0f) \
  239.         { \
  240.                 A = VV1; B = (VV0 - VV1) * D1; C = (VV2 - VV1) * D1; X0 = D1 - D0; X1 = D1 - D2; \
  241.         } \
  242.         else if (D2 != 0.0f) \
  243.         { \
  244.                 A = VV2; B = (VV0 - VV2) * D2; C = (VV1 - VV2) * D2; X0 = D2 - D0; X1 = D2 - D1; \
  245.         } \
  246.         else \
  247.         { \
  248.                 /* triangles are coplanar */ \
  249.                 return CoplanTriTri(NPlane1, V0, V1, V2, U0, U1, U2); \
  250.         } \
  251. }
  252.  
  253. bool Intersects(MODEL Tri1, MODEL Tri2)
  254. {
  255.     float xx, yy, xxyy, tmp;
  256.     float isect1[2], isect2[2];
  257.     float a, b, c, x0, x1;
  258.     float d, e, f, y0, y1;
  259.     XMFLOAT3 ZeroVert = {0,0,0};
  260.     XMFLOAT3 BVert0 = Tri2.vects[0].position;
  261.     XMFLOAT3 BVert1 = Tri2.vects[1].position;
  262.     XMFLOAT3 BVert2 = Tri2.vects[2].position;
  263.     XMFLOAT3 AVert0 = Tri1.vects[0].position;
  264.     XMFLOAT3 AVert1 = Tri1.vects[1].position;
  265.     XMFLOAT3 AVert2 = Tri1.vects[2].position;
  266.  
  267.  
  268.    
  269.     XMFLOAT3 NPlane1 = XMVector3Cross(SubXMFLOAT3(AVert1, AVert0), SubXMFLOAT3(AVert2, AVert0));
  270.     float ADist = -XMVector3Dot(NPlane1, AVert0);
  271.  
  272.     float BDist0 = XMVector3Dot(NPlane1, BVert0)+ADist;
  273.     float BDist1 = XMVector3Dot(NPlane1, BVert1)+ADist;
  274.     float BDist2 = XMVector3Dot(NPlane1, BVert2)+ADist;
  275.  
  276.  
  277.     float B0xB1 = BDist0*BDist1;
  278.     float B0xB2 = BDist0*BDist2;
  279.     if(B0xB1 > 0.0 && B0xB2 > 0.0){
  280.         return false;
  281.     }
  282.  
  283.     XMFLOAT3 NPlane2 = XMVector3Cross(SubXMFLOAT3(BVert1, BVert0), SubXMFLOAT3(BVert2, BVert0));
  284.     float BDist = -XMVector3Dot(NPlane2, BVert0);
  285.  
  286.     float ADist0 = XMVector3Dot(NPlane2, AVert0)+BDist;
  287.     float ADist1 = XMVector3Dot(NPlane2, AVert1)+BDist;
  288.     float ADist2 = XMVector3Dot(NPlane2, AVert2)+BDist;
  289.  
  290.  
  291.     float A0xA1 = ADist0*ADist1;
  292.     float A0xA2 = ADist0*ADist2;
  293.     if(A0xA1 > 0.0 && A0xA2 > 0.0){
  294.         return false;
  295.     }
  296.  
  297.  
  298.     XMFLOAT3 IntersectLineDir = XMVector3Cross(NPlane1, NPlane2);
  299.  
  300.  
  301.     int index = 0;
  302.     float max = fabs(IntersectLineDir.x);
  303.     float bb = fabs(IntersectLineDir.y);
  304.     float cc = fabs(IntersectLineDir.z);
  305.     if(bb>max) max=bb, index=1;
  306.     if(cc>max) max=cc, index=2;
  307.  
  308.     float V0[3];
  309.     float V1[3];
  310.     float V2[3];
  311.     float U0[3];
  312.     float U1[3];
  313.     float U2[3];
  314.  
  315.     V0[0] = AVert0.x;
  316.     V0[1] = AVert0.y;
  317.     V0[2] = AVert0.z;
  318.     V1[0] = AVert1.x;
  319.     V1[1] = AVert1.y;
  320.     V1[2] = AVert1.z;
  321.     V2[0] = AVert2.x;
  322.     V2[1] = AVert2.y;
  323.     V2[2] = AVert2.z;
  324.     U0[0] = BVert0.x;
  325.     U0[1] = BVert0.y;
  326.     U0[2] = BVert0.z;
  327.     U1[0] = BVert1.x;
  328.     U1[1] = BVert1.y;
  329.     U1[2] = BVert1.z;
  330.     U2[0] = BVert2.x;
  331.     U2[1] = BVert2.y;
  332.     U2[2] = BVert2.z;
  333.  
  334.  
  335.     float vp0 = 0.0;
  336.     float vp1 = 0.0;
  337.     float vp2 = 0.0;
  338.  
  339.     float up0 = 0.0;
  340.     float up1 = 0.0;
  341.     float up2 = 0.0;
  342.  
  343.     vp0 = V0[index];
  344.     vp1 = V1[index];
  345.     vp2 = V2[index];
  346.  
  347.     up0 = U0[index];
  348.     up1 = U1[index];
  349.     up2 = U2[index];
  350.    
  351.     NEWCOMPUTE_INTERVALS(vp0, vp1, vp2, ADist0, ADist1, ADist2, A0xA1, A0xA2, a, b, c, x0, x1);
  352.  
  353.     NEWCOMPUTE_INTERVALS(up0, up1, up2, BDist0, BDist1, BDist2, B0xB1, B0xB2, d, e, f, y0, y1);
  354.    
  355.  
  356.     xx = x0 * x1;
  357.     yy = y0 * y1;
  358.     xxyy = xx * yy;
  359.  
  360.    
  361.     tmp = a * xxyy;
  362.     isect1[0] = tmp + b * x1 * yy;
  363.     isect1[1] = tmp + c * x0 * yy;
  364.  
  365.     tmp = d * xxyy;
  366.     isect2[0] = tmp + e * xx * y1;
  367.     isect2[1] = tmp + f * xx * y0;
  368.    
  369.     SORT(isect1[0], isect1[1]);
  370.     SORT(isect2[0], isect2[1]);
  371.  
  372.     if (isect1[1] < isect2[0] || isect2[1] < isect1[0]) return false;
  373.  
  374.     return true;
  375. }
  376.  
  377.  
  378. void kernel coll(global const MODEL* WModel, global const MODEL* TModel, global XMFLOAT3* Wposition, global int* sizeT, global RETURNDATA* retdat){
  379.     int id = get_global_id(0);
  380.     bool temp = false;
  381.     MODEL Work1 = WModel[id];
  382.     Work1.vects[0].position = AddXMFLOAT3(Work1.vects[0].position, Wposition[1]);
  383.     Work1.vects[1].position = AddXMFLOAT3(Work1.vects[1].position, Wposition[1]);
  384.     Work1.vects[2].position = AddXMFLOAT3(Work1.vects[2].position, Wposition[1]);
  385.    
  386.     for(int x = 0; x < sizeT[1]; x++){
  387.         if(!temp){
  388.             MODEL Work = TModel[x];
  389.             Work.vects[0].position = AddXMFLOAT3(Work.vects[0].position, Wposition[0]);
  390.             Work.vects[1].position = AddXMFLOAT3(Work.vects[1].position, Wposition[0]);
  391.             Work.vects[2].position = AddXMFLOAT3(Work.vects[2].position, Wposition[0]);
  392.             temp = Intersects(WModel[id], Work);
  393.         }
  394.     }
  395.     retdat[id].coll = temp;
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement