Advertisement
Guest User

Untitled

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