qradutil.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "qrad.h"
  2. static dplane_t backplanes[MAX_INTERNAL_MAP_PLANES];
  3. dleaf_t* PointInLeaf(const vec3_t point)
  4. {
  5. int nodenum;
  6. vec_t dist;
  7. dnode_t* node;
  8. dplane_t* plane;
  9. nodenum = 0;
  10. while (nodenum >= 0)
  11. {
  12. node = &g_dnodes[nodenum];
  13. plane = &g_dplanes[node->planenum];
  14. dist = DotProduct(point, plane->normal) - plane->dist;
  15. if (dist >= 0.0)
  16. {
  17. nodenum = node->children[0];
  18. }
  19. else
  20. {
  21. nodenum = node->children[1];
  22. }
  23. }
  24. return &g_dleafs[-nodenum - 1];
  25. }
  26. /*
  27. * ==============
  28. * PatchPlaneDist
  29. * Fixes up patch planes for brush models with an origin brush
  30. * ==============
  31. */
  32. vec_t PatchPlaneDist(const patch_t* const patch)
  33. {
  34. const dplane_t* plane = getPlaneFromFaceNumber(patch->faceNumber);
  35. return plane->dist + DotProduct(g_face_offset[patch->faceNumber], plane->normal);
  36. }
  37. void MakeBackplanes()
  38. {
  39. int i;
  40. for (i = 0; i < g_numplanes; i++)
  41. {
  42. backplanes[i].dist = -g_dplanes[i].dist;
  43. VectorSubtract(vec3_origin, g_dplanes[i].normal, backplanes[i].normal);
  44. }
  45. }
  46. const dplane_t* getPlaneFromFace(const dface_t* const face)
  47. {
  48. if (!face)
  49. {
  50. Error("getPlaneFromFace() face was NULL\n");
  51. }
  52. if (face->side)
  53. {
  54. return &backplanes[face->planenum];
  55. }
  56. else
  57. {
  58. return &g_dplanes[face->planenum];
  59. }
  60. }
  61. const dplane_t* getPlaneFromFaceNumber(const unsigned int faceNumber)
  62. {
  63. dface_t* face = &g_dfaces[faceNumber];
  64. if (face->side)
  65. {
  66. return &backplanes[face->planenum];
  67. }
  68. else
  69. {
  70. return &g_dplanes[face->planenum];
  71. }
  72. }
  73. // Returns plane adjusted for face offset (for origin brushes, primarily used in the opaque code)
  74. void getAdjustedPlaneFromFaceNumber(unsigned int faceNumber, dplane_t* plane)
  75. {
  76. dface_t* face = &g_dfaces[faceNumber];
  77. const vec_t* face_offset = g_face_offset[faceNumber];
  78. plane->type = (planetypes)0;
  79. if (face->side)
  80. {
  81. vec_t dist;
  82. VectorCopy(backplanes[face->planenum].normal, plane->normal);
  83. dist = DotProduct(plane->normal, face_offset);
  84. plane->dist = backplanes[face->planenum].dist + dist;
  85. }
  86. else
  87. {
  88. vec_t dist;
  89. VectorCopy(g_dplanes[face->planenum].normal, plane->normal);
  90. dist = DotProduct(plane->normal, face_offset);
  91. plane->dist = g_dplanes[face->planenum].dist + dist;
  92. }
  93. }
  94. // Will modify the plane with the new dist
  95. void TranslatePlane(dplane_t* plane, const vec_t* delta)
  96. {
  97. #ifdef HLRAD_FASTMATH
  98. plane->dist += DotProduct(plane->normal,delta);
  99. #else
  100. vec3_t proj;
  101. vec_t magnitude;
  102. ProjectionPoint(delta, plane->normal, proj);
  103. magnitude = VectorLength(proj);
  104. if (DotProduct(plane->normal, delta) > 0) //if zero, magnitude will be zero.
  105. {
  106. plane->dist += magnitude;
  107. }
  108. else
  109. {
  110. plane->dist -= magnitude;
  111. }
  112. #endif
  113. }
  114. // HuntForWorld will never return CONTENTS_SKY or CONTENTS_SOLID leafs
  115. dleaf_t* HuntForWorld(vec_t* point, const vec_t* plane_offset, const dplane_t* plane, int hunt_size, vec_t hunt_scale, vec_t hunt_offset)
  116. {
  117. dleaf_t* leaf;
  118. int x, y, z;
  119. int a;
  120. vec3_t current_point;
  121. vec3_t original_point;
  122. vec3_t best_point;
  123. dleaf_t* best_leaf = NULL;
  124. vec_t best_dist = 99999999.0;
  125. vec3_t scales;
  126. dplane_t new_plane = *plane;
  127. if (hunt_scale < 0.1)
  128. {
  129. hunt_scale = 0.1;
  130. }
  131. scales[0] = 0.0;
  132. scales[1] = -hunt_scale;
  133. scales[2] = hunt_scale;
  134. VectorCopy(point, best_point);
  135. VectorCopy(point, original_point);
  136. TranslatePlane(&new_plane, plane_offset);
  137. if (!hunt_size)
  138. {
  139. hunt_size = DEFAULT_HUNT_SIZE;
  140. }
  141. for (a = 1; a < hunt_size; a++)
  142. {
  143. for (x = 0; x < 3; x++)
  144. {
  145. current_point[0] = original_point[0] + (scales[x % 3] * a);
  146. for (y = 0; y < 3; y++)
  147. {
  148. current_point[1] = original_point[1] + (scales[y % 3] * a);
  149. for (z = 0; z < 3; z++)
  150. {
  151. vec3_t delta;
  152. vec_t dist;
  153. current_point[2] = original_point[2] + (scales[z % 3] * a);
  154. SnapToPlane(&new_plane, current_point, hunt_offset);
  155. VectorSubtract(current_point, original_point, delta);
  156. dist = VectorLength(delta);
  157. if (dist < best_dist)
  158. {
  159. if ((leaf = PointInLeaf(current_point)) != g_dleafs)
  160. {
  161. if ((leaf->contents != CONTENTS_SKY) && (leaf->contents != CONTENTS_SOLID))
  162. {
  163. if (x || y || z)
  164. {
  165. //dist = best_dist;
  166. best_leaf = leaf;
  167. VectorCopy(current_point, best_point);
  168. continue;
  169. }
  170. else
  171. {
  172. VectorCopy(current_point, point);
  173. return leaf;
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. if (best_leaf)
  182. {
  183. break;
  184. }
  185. }
  186. VectorCopy(best_point, point);
  187. return best_leaf;
  188. }