PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/game/q_math.c

https://github.com/xzero450/revolution
C | 1477 lines | 1018 code | 175 blank | 284 comment | 73 complexity | 40f0500e3ddd91e1d1c836b46c6d7c90 MD5 | raw file
  1. // Copyright (C) 1999-2000 Id Software, Inc.
  2. //
  3. // q_math.c -- stateless support routines that are included in each code module
  4. #include "q_shared.h"
  5. #pragma warning( push )
  6. #pragma warning(disable : 4305) //shut up the Visual Studio compiler for the color table
  7. vec3_t vec3_origin = {0,0,0};
  8. vec3_t axisDefault[3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
  9. vec4_t colorBlack = {0, 0, 0, 1};
  10. vec4_t colorRed = {1, 0, 0, 1};
  11. vec4_t colorGreen = {0, 1, 0, 1};
  12. vec4_t colorBlue = {0, 0, 1, 1};
  13. vec4_t colorYellow = {1, 1, 0, 1};
  14. vec4_t colorMagenta= {1, 0, 1, 1};
  15. vec4_t colorCyan = {0, 1, 1, 1};
  16. vec4_t colorWhite = {1, 1, 1, 1};
  17. vec4_t colorOrange = {1, 0.5, 0, 1};
  18. vec4_t colorMdGrey = {0.5, 0.5, 0.5, 1.0};
  19. vec4_t color_a = {1.00, 0.00, 0.00, 1.00};
  20. vec4_t color_b = {1.00, 0.23, 0.00, 1.00};
  21. vec4_t color_c = {1.00, 0.47, 0.00, 1.00};
  22. vec4_t color_d = {1.00, 0.70, 0.00, 1.00};
  23. vec4_t color_e = {1.00, 0.93, 0.00, 1.00};
  24. vec4_t color_f = {0.83, 1.00, 0.00, 1.00};
  25. vec4_t color_g = {0.60, 1.00, 0.00, 1.00};
  26. vec4_t color_h = {0.37, 1.00, 0.00, 1.00};
  27. vec4_t color_i = {0.13, 1.00, 0.00, 1.00};
  28. vec4_t color_j = {0.00, 1.00, 0.33, 1.00};
  29. vec4_t color_k = {0.00, 1.00, 0.57, 1.00};
  30. vec4_t color_l = {0.00, 1.00, 0.80, 1.00};
  31. vec4_t color_m = {0.00, 0.97, 1.00, 1.00};
  32. vec4_t color_n = {0.00, 0.73, 1.00, 1.00};
  33. vec4_t color_o = {0.00, 0.50, 1.00, 1.00};
  34. vec4_t color_p = {0.00, 0.27, 1.00, 1.00};
  35. vec4_t color_q = {0.00, 0.03, 1.00, 1.00};
  36. vec4_t color_r = {0.20, 0.00, 1.00, 1.00};
  37. vec4_t color_s = {0.43, 0.00, 1.00, 1.00};
  38. vec4_t color_t = {0.67, 0.00, 1.00, 1.00};
  39. vec4_t color_u = {0.90, 0.00, 1.00, 1.00};
  40. vec4_t color_v = {1.00, 0.00, 0.87, 1.00};
  41. vec4_t color_w = {1.00, 0.00, 0.63, 1.00};
  42. vec4_t color_x = {1.00, 0.00, 0.40, 1.00};
  43. vec4_t color_y = {1.00, 1.00, 1.00, 1.00};
  44. vec4_t color_z = {0.25, 0.25, 0.25, 1.00};
  45. vec4_t g_color_table[62] =
  46. {
  47. {0.0, 0.0, 0.0, 1.0},
  48. {1.0, 0.0, 0.0, 1.0},
  49. {0.0, 1.0, 0.0, 1.0},
  50. {1.0, 1.0, 0.0, 1.0},
  51. {0.0, 0.0, 1.0, 1.0},
  52. {0.0, 1.0, 1.0, 1.0},
  53. {1.0, 0.0, 1.0, 1.0},
  54. {1.0, 1.0, 1.0, 1.0},
  55. {1.0, 0.5, 0.0, 1.0},
  56. {0.5, 0.5, 0.5, 1.0},
  57. {1.000, 0.000, 0.000, 1.000},
  58. {1.000, 0.117, 0.000, 1.000},
  59. {1.000, 0.233, 0.000, 1.000},
  60. {1.000, 0.350, 0.000, 1.000},
  61. {1.000, 0.467, 0.000, 1.000},
  62. {1.000, 0.583, 0.000, 1.000},
  63. {1.000, 0.700, 0.000, 1.000},
  64. {1.000, 0.817, 0.000, 1.000},
  65. {1.000, 0.933, 0.000, 1.000},
  66. {0.950, 1.000, 0.000, 1.000},
  67. {0.833, 1.000, 0.000, 1.000},
  68. {0.717, 1.000, 0.000, 1.000},
  69. {0.600, 1.000, 0.000, 1.000},
  70. {0.483, 1.000, 0.000, 1.000},
  71. {0.367, 1.000, 0.000, 1.000},
  72. {0.250, 1.000, 0.000, 1.000},
  73. {0.133, 1.000, 0.000, 1.000},
  74. {0.017, 1.000, 0.000, 1.000},
  75. {0.000, 1.000, 0.100, 1.000},
  76. {0.000, 1.000, 0.217, 1.000},
  77. {0.000, 1.000, 0.333, 1.000},
  78. {0.000, 1.000, 0.450, 1.000},
  79. {0.000, 1.000, 0.567, 1.000},
  80. {0.000, 1.000, 0.683, 1.000},
  81. {0.000, 1.000, 0.800, 1.000},
  82. {0.000, 1.000, 0.917, 1.000},
  83. {0.000, 0.967, 1.000, 1.000},
  84. {0.000, 0.850, 1.000, 1.000},
  85. {0.000, 0.733, 1.000, 1.000},
  86. {0.000, 0.617, 1.000, 1.000},
  87. {0.000, 0.500, 1.000, 1.000},
  88. {0.000, 0.383, 1.000, 1.000},
  89. {0.000, 0.267, 1.000, 1.000},
  90. {0.000, 0.150, 1.000, 1.000},
  91. {0.000, 0.033, 1.000, 1.000},
  92. {0.083, 0.000, 1.000, 1.000},
  93. {0.200, 0.000, 1.000, 1.000},
  94. {0.317, 0.000, 1.000, 1.000},
  95. {0.433, 0.000, 1.000, 1.000},
  96. {0.550, 0.000, 1.000, 1.000},
  97. {0.667, 0.000, 1.000, 1.000},
  98. {0.783, 0.000, 1.000, 1.000},
  99. {0.900, 0.000, 1.000, 1.000},
  100. {1.000, 0.000, 0.983, 1.000},
  101. {1.000, 0.000, 0.867, 1.000},
  102. {1.000, 0.000, 0.750, 1.000},
  103. {1.000, 0.000, 0.633, 1.000},
  104. {1.000, 0.000, 0.517, 1.000},
  105. {1.00, 1.00, 1.00, 1.00},
  106. {0.25, 0.25, 0.25, 1.00},
  107. {0.75, 0.75, 0.75, 1.00},
  108. {0.50, 0.50, 0.50, 1.00}
  109. //};
  110. /*{1.00, 0.00, 0.00, 1.00},
  111. {1.00, 0.23, 0.00, 1.00},
  112. {1.00, 0.47, 0.00, 1.00},
  113. {1.00, 0.70, 0.00, 1.00},
  114. {1.00, 0.93, 0.00, 1.00},
  115. {0.83, 1.00, 0.00, 1.00},
  116. {0.60, 1.00, 0.00, 1.00},
  117. {0.37, 1.00, 0.00, 1.00},
  118. {0.13, 1.00, 0.00, 1.00},
  119. {0.00, 1.00, 0.33, 1.00},
  120. {0.00, 1.00, 0.57, 1.00},
  121. {0.00, 1.00, 0.80, 1.00},
  122. {0.00, 0.97, 1.00, 1.00},
  123. {0.00, 0.73, 1.00, 1.00},
  124. {0.00, 0.50, 1.00, 1.00},
  125. {0.00, 0.27, 1.00, 1.00},
  126. {0.00, 0.03, 1.00, 1.00},
  127. {0.20, 0.00, 1.00, 1.00},
  128. {0.43, 0.00, 1.00, 1.00},
  129. {0.67, 0.00, 1.00, 1.00},
  130. {0.90, 0.00, 1.00, 1.00},
  131. {1.00, 0.00, 0.87, 1.00},
  132. {1.00, 0.00, 0.63, 1.00},
  133. {1.00, 0.00, 0.40, 1.00},
  134. {1.00, 1.00, 1.00, 1.00},
  135. {0.25, 0.25, 0.25, 1.00}*/
  136. /*
  137. {0.75, 0.1, 0.1, 1.0},
  138. {0.9, 0.25, 0.1, 1.0},
  139. {0.8, 0.55, 0.25, 1.0},
  140. {0.85, 0.7, 0.2, 1.0},
  141. {0.95, 0.95, 0.4, 1.0},
  142. {0.7, 0.85, 0.4, 1.0},
  143. {0.6, 0.8, 0.25, 1.0},
  144. {0.4, 0.95, 0.3, 1.0},
  145. {0.2, 0.75, 0.2, 1.0},
  146. {0.35, 0.75, 0.5, 1.0},
  147. {0.2, 0.95, 0.55, 1.0},
  148. {0.45, 0.8, 0.7, 1.0},
  149. {0.1, 0.9, 0.9, 1.0},
  150. {0.1, 0.6, 0.9, 1.0},
  151. {0.15, 0.45, 0.7, 1.0},
  152. {0.15, 0.25, 0.75, 1.0},
  153. {0.0, 0.0, 0.7, 1.0},
  154. {0.25, 0.05, 0.85, 1.0},
  155. {0.4, 0.1, 0.65, 1.0},
  156. {0.5, 0.15, 0.75, 1.0},
  157. {0.7, 0.15, 0.7, 1.0},
  158. {0.75, 0.2, 0.55, 1.0},
  159. {1.0, 0.1, 0.45, 1.0},
  160. {0.8, 0.1, 0.25, 1.0},
  161. {1.0, 1.0, 1.0, 1.0},
  162. {0.25, 0.25, 0.25, 1.0},
  163. {0.75, 0.1, 0.1, 1.0},
  164. {0.9, 0.25, 0.1, 1.0},
  165. {0.8, 0.55, 0.25, 1.0},
  166. {0.85, 0.7, 0.2, 1.0},
  167. {0.95, 0.95, 0.4, 1.0},
  168. {0.7, 0.85, 0.4, 1.0},
  169. {0.6, 0.8, 0.25, 1.0},
  170. {0.4, 0.95, 0.3, 1.0},
  171. {0.2, 0.75, 0.2, 1.0},
  172. {0.35, 0.75, 0.5, 1.0},
  173. {0.2, 0.95, 0.55, 1.0},
  174. {0.45, 0.8, 0.7, 1.0},
  175. {0.1, 0.9, 0.9, 1.0},
  176. {0.1, 0.6, 0.9, 1.0},
  177. {0.15, 0.45, 0.7, 1.0},
  178. {0.15, 0.25, 0.75, 1.0},
  179. {0.0, 0.0, 0.7, 1.0},
  180. {0.25, 0.05, 0.85, 1.0},
  181. {0.4, 0.1, 0.65, 1.0},
  182. {0.5, 0.15, 0.75, 1.0},
  183. {0.7, 0.15, 0.7, 1.0},
  184. {0.75, 0.2, 0.55, 1.0},
  185. {1.0, 0.1, 0.45, 1.0},
  186. {0.8, 0.1, 0.25, 1.0},
  187. {1.0, 1.0, 1.0, 1.0},
  188. {0.25, 0.25, 0.25, 1.0}*/
  189. /*
  190. //{0.5, 0.5, 0.5, 1.0},
  191. //{0.0, 0.0, 0.75, 1.0},
  192. //{0.75, 0.0, 0.0, 1.0},{1.0, 0.0, 0.5, 1.0},
  193. {0.25, 0.0, 0.5, 1.0},
  194. {1.0, 0.0, 0.25,1},
  195. {1.0, 1.0, 0.0, 1.0},
  196. {1.0, 0.5, 0.0, 1.0},
  197. {1.0, 0.25, 0.0, 1.0},
  198. {0.25, 0.0, 1.0, 1.0},
  199. {0.5, 1.0, 0.0, 1.0},
  200. {1.0, 0.0, 0.5, 1.0},
  201. {0.5, 0.0, 1.0, 1.0},
  202. {0.25, 1.0, 0.0, 1.0},
  203. {0.75, 0.75, 0.75, 1.0},
  204. {0.25, 1.0, 0.0, 1.0},
  205. {0.0, 0.5, 1.0, 1.0},
  206. {0.0, 0.75, 1.0, 1.0},
  207. {0.0, 0.25, 0.75, 1.0},
  208. {0.0, 0.25, 0.25, 1.0},
  209. {0.0, 1.0, 0.0, 1.0},
  210. {0.0, 0.5, 0.75, 1.0},
  211. {0.5, 0.5, 0.5, 1.0},
  212. {0.0, 0.0, 0.75, 1.0},
  213. {0.0, 0.75, 0.75, 1.0},
  214. {1.0, 0.0, 0.5, 1.0},*/
  215. };
  216. #pragma warning( pop ) //shut up the Visual Studio compiler for the color table
  217. vec3_t bytedirs[NUMVERTEXNORMALS] =
  218. {
  219. {-0.525731f, 0.000000f, 0.850651f}, {-0.442863f, 0.238856f, 0.864188f},
  220. {-0.295242f, 0.000000f, 0.955423f}, {-0.309017f, 0.500000f, 0.809017f},
  221. {-0.162460f, 0.262866f, 0.951056f}, {0.000000f, 0.000000f, 1.000000f},
  222. {0.000000f, 0.850651f, 0.525731f}, {-0.147621f, 0.716567f, 0.681718f},
  223. {0.147621f, 0.716567f, 0.681718f}, {0.000000f, 0.525731f, 0.850651f},
  224. {0.309017f, 0.500000f, 0.809017f}, {0.525731f, 0.000000f, 0.850651f},
  225. {0.295242f, 0.000000f, 0.955423f}, {0.442863f, 0.238856f, 0.864188f},
  226. {0.162460f, 0.262866f, 0.951056f}, {-0.681718f, 0.147621f, 0.716567f},
  227. {-0.809017f, 0.309017f, 0.500000f},{-0.587785f, 0.425325f, 0.688191f},
  228. {-0.850651f, 0.525731f, 0.000000f},{-0.864188f, 0.442863f, 0.238856f},
  229. {-0.716567f, 0.681718f, 0.147621f},{-0.688191f, 0.587785f, 0.425325f},
  230. {-0.500000f, 0.809017f, 0.309017f}, {-0.238856f, 0.864188f, 0.442863f},
  231. {-0.425325f, 0.688191f, 0.587785f}, {-0.716567f, 0.681718f, -0.147621f},
  232. {-0.500000f, 0.809017f, -0.309017f}, {-0.525731f, 0.850651f, 0.000000f},
  233. {0.000000f, 0.850651f, -0.525731f}, {-0.238856f, 0.864188f, -0.442863f},
  234. {0.000000f, 0.955423f, -0.295242f}, {-0.262866f, 0.951056f, -0.162460f},
  235. {0.000000f, 1.000000f, 0.000000f}, {0.000000f, 0.955423f, 0.295242f},
  236. {-0.262866f, 0.951056f, 0.162460f}, {0.238856f, 0.864188f, 0.442863f},
  237. {0.262866f, 0.951056f, 0.162460f}, {0.500000f, 0.809017f, 0.309017f},
  238. {0.238856f, 0.864188f, -0.442863f},{0.262866f, 0.951056f, -0.162460f},
  239. {0.500000f, 0.809017f, -0.309017f},{0.850651f, 0.525731f, 0.000000f},
  240. {0.716567f, 0.681718f, 0.147621f}, {0.716567f, 0.681718f, -0.147621f},
  241. {0.525731f, 0.850651f, 0.000000f}, {0.425325f, 0.688191f, 0.587785f},
  242. {0.864188f, 0.442863f, 0.238856f}, {0.688191f, 0.587785f, 0.425325f},
  243. {0.809017f, 0.309017f, 0.500000f}, {0.681718f, 0.147621f, 0.716567f},
  244. {0.587785f, 0.425325f, 0.688191f}, {0.955423f, 0.295242f, 0.000000f},
  245. {1.000000f, 0.000000f, 0.000000f}, {0.951056f, 0.162460f, 0.262866f},
  246. {0.850651f, -0.525731f, 0.000000f},{0.955423f, -0.295242f, 0.000000f},
  247. {0.864188f, -0.442863f, 0.238856f}, {0.951056f, -0.162460f, 0.262866f},
  248. {0.809017f, -0.309017f, 0.500000f}, {0.681718f, -0.147621f, 0.716567f},
  249. {0.850651f, 0.000000f, 0.525731f}, {0.864188f, 0.442863f, -0.238856f},
  250. {0.809017f, 0.309017f, -0.500000f}, {0.951056f, 0.162460f, -0.262866f},
  251. {0.525731f, 0.000000f, -0.850651f}, {0.681718f, 0.147621f, -0.716567f},
  252. {0.681718f, -0.147621f, -0.716567f},{0.850651f, 0.000000f, -0.525731f},
  253. {0.809017f, -0.309017f, -0.500000f}, {0.864188f, -0.442863f, -0.238856f},
  254. {0.951056f, -0.162460f, -0.262866f}, {0.147621f, 0.716567f, -0.681718f},
  255. {0.309017f, 0.500000f, -0.809017f}, {0.425325f, 0.688191f, -0.587785f},
  256. {0.442863f, 0.238856f, -0.864188f}, {0.587785f, 0.425325f, -0.688191f},
  257. {0.688191f, 0.587785f, -0.425325f}, {-0.147621f, 0.716567f, -0.681718f},
  258. {-0.309017f, 0.500000f, -0.809017f}, {0.000000f, 0.525731f, -0.850651f},
  259. {-0.525731f, 0.000000f, -0.850651f}, {-0.442863f, 0.238856f, -0.864188f},
  260. {-0.295242f, 0.000000f, -0.955423f}, {-0.162460f, 0.262866f, -0.951056f},
  261. {0.000000f, 0.000000f, -1.000000f}, {0.295242f, 0.000000f, -0.955423f},
  262. {0.162460f, 0.262866f, -0.951056f}, {-0.442863f, -0.238856f, -0.864188f},
  263. {-0.309017f, -0.500000f, -0.809017f}, {-0.162460f, -0.262866f, -0.951056f},
  264. {0.000000f, -0.850651f, -0.525731f}, {-0.147621f, -0.716567f, -0.681718f},
  265. {0.147621f, -0.716567f, -0.681718f}, {0.000000f, -0.525731f, -0.850651f},
  266. {0.309017f, -0.500000f, -0.809017f}, {0.442863f, -0.238856f, -0.864188f},
  267. {0.162460f, -0.262866f, -0.951056f}, {0.238856f, -0.864188f, -0.442863f},
  268. {0.500000f, -0.809017f, -0.309017f}, {0.425325f, -0.688191f, -0.587785f},
  269. {0.716567f, -0.681718f, -0.147621f}, {0.688191f, -0.587785f, -0.425325f},
  270. {0.587785f, -0.425325f, -0.688191f}, {0.000000f, -0.955423f, -0.295242f},
  271. {0.000000f, -1.000000f, 0.000000f}, {0.262866f, -0.951056f, -0.162460f},
  272. {0.000000f, -0.850651f, 0.525731f}, {0.000000f, -0.955423f, 0.295242f},
  273. {0.238856f, -0.864188f, 0.442863f}, {0.262866f, -0.951056f, 0.162460f},
  274. {0.500000f, -0.809017f, 0.309017f}, {0.716567f, -0.681718f, 0.147621f},
  275. {0.525731f, -0.850651f, 0.000000f}, {-0.238856f, -0.864188f, -0.442863f},
  276. {-0.500000f, -0.809017f, -0.309017f}, {-0.262866f, -0.951056f, -0.162460f},
  277. {-0.850651f, -0.525731f, 0.000000f}, {-0.716567f, -0.681718f, -0.147621f},
  278. {-0.716567f, -0.681718f, 0.147621f}, {-0.525731f, -0.850651f, 0.000000f},
  279. {-0.500000f, -0.809017f, 0.309017f}, {-0.238856f, -0.864188f, 0.442863f},
  280. {-0.262866f, -0.951056f, 0.162460f}, {-0.864188f, -0.442863f, 0.238856f},
  281. {-0.809017f, -0.309017f, 0.500000f}, {-0.688191f, -0.587785f, 0.425325f},
  282. {-0.681718f, -0.147621f, 0.716567f}, {-0.442863f, -0.238856f, 0.864188f},
  283. {-0.587785f, -0.425325f, 0.688191f}, {-0.309017f, -0.500000f, 0.809017f},
  284. {-0.147621f, -0.716567f, 0.681718f}, {-0.425325f, -0.688191f, 0.587785f},
  285. {-0.162460f, -0.262866f, 0.951056f}, {0.442863f, -0.238856f, 0.864188f},
  286. {0.162460f, -0.262866f, 0.951056f}, {0.309017f, -0.500000f, 0.809017f},
  287. {0.147621f, -0.716567f, 0.681718f}, {0.000000f, -0.525731f, 0.850651f},
  288. {0.425325f, -0.688191f, 0.587785f}, {0.587785f, -0.425325f, 0.688191f},
  289. {0.688191f, -0.587785f, 0.425325f}, {-0.955423f, 0.295242f, 0.000000f},
  290. {-0.951056f, 0.162460f, 0.262866f}, {-1.000000f, 0.000000f, 0.000000f},
  291. {-0.850651f, 0.000000f, 0.525731f}, {-0.955423f, -0.295242f, 0.000000f},
  292. {-0.951056f, -0.162460f, 0.262866f}, {-0.864188f, 0.442863f, -0.238856f},
  293. {-0.951056f, 0.162460f, -0.262866f}, {-0.809017f, 0.309017f, -0.500000f},
  294. {-0.864188f, -0.442863f, -0.238856f}, {-0.951056f, -0.162460f, -0.262866f},
  295. {-0.809017f, -0.309017f, -0.500000f}, {-0.681718f, 0.147621f, -0.716567f},
  296. {-0.681718f, -0.147621f, -0.716567f}, {-0.850651f, 0.000000f, -0.525731f},
  297. {-0.688191f, 0.587785f, -0.425325f}, {-0.587785f, 0.425325f, -0.688191f},
  298. {-0.425325f, 0.688191f, -0.587785f}, {-0.425325f, -0.688191f, -0.587785f},
  299. {-0.587785f, -0.425325f, -0.688191f}, {-0.688191f, -0.587785f, -0.425325f}
  300. };
  301. //==============================================================
  302. int Q_rand( int *seed ) {
  303. *seed = (69069 * *seed + 1);
  304. return *seed;
  305. }
  306. float Q_random( int *seed ) {
  307. return ( Q_rand( seed ) & 0xffff ) / (float)0x10000;
  308. }
  309. float Q_crandom( int *seed ) {
  310. return 2.0 * ( Q_random( seed ) - 0.5 );
  311. }
  312. #ifdef __LCC__
  313. int VectorCompare( const vec3_t v1, const vec3_t v2 ) {
  314. if (v1[0] != v2[0] || v1[1] != v2[1] || v1[2] != v2[2]) {
  315. return 0;
  316. }
  317. return 1;
  318. }
  319. vec_t VectorLength( const vec3_t v ) {
  320. return (vec_t)sqrt (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  321. }
  322. vec_t VectorLengthSquared( const vec3_t v ) {
  323. return (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  324. }
  325. vec_t Distance( const vec3_t p1, const vec3_t p2 ) {
  326. vec3_t v;
  327. VectorSubtract (p2, p1, v);
  328. return VectorLength( v );
  329. }
  330. vec_t DistanceSquared( const vec3_t p1, const vec3_t p2 ) {
  331. vec3_t v;
  332. VectorSubtract (p2, p1, v);
  333. return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  334. }
  335. // fast vector normalize routine that does not check to make sure
  336. // that length != 0, nor does it return length, uses rsqrt approximation
  337. void VectorNormalizeFast( vec3_t v )
  338. {
  339. float ilength;
  340. ilength = Q_rsqrt( DotProduct( v, v ) );
  341. v[0] *= ilength;
  342. v[1] *= ilength;
  343. v[2] *= ilength;
  344. }
  345. void VectorInverse( vec3_t v ){
  346. v[0] = -v[0];
  347. v[1] = -v[1];
  348. v[2] = -v[2];
  349. }
  350. void CrossProduct( const vec3_t v1, const vec3_t v2, vec3_t cross ) {
  351. cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  352. cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  353. cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  354. }
  355. #endif
  356. //=======================================================
  357. signed char ClampChar( int i ) {
  358. if ( i < -128 ) {
  359. return -128;
  360. }
  361. if ( i > 127 ) {
  362. return 127;
  363. }
  364. return i;
  365. }
  366. signed short ClampShort( int i ) {
  367. if ( i < -32768 ) {
  368. return -32768;
  369. }
  370. if ( i > 0x7fff ) {
  371. return 0x7fff;
  372. }
  373. return i;
  374. }
  375. // this isn't a real cheap function to call!
  376. int DirToByte( vec3_t dir ) {
  377. int i, best;
  378. float d, bestd;
  379. if ( !dir ) {
  380. return 0;
  381. }
  382. bestd = 0;
  383. best = 0;
  384. for (i=0 ; i<NUMVERTEXNORMALS ; i++)
  385. {
  386. d = DotProduct (dir, bytedirs[i]);
  387. if (d > bestd)
  388. {
  389. bestd = d;
  390. best = i;
  391. }
  392. }
  393. return best;
  394. }
  395. void ByteToDir( int b, vec3_t dir ) {
  396. if ( b < 0 || b >= NUMVERTEXNORMALS ) {
  397. VectorCopy( vec3_origin, dir );
  398. return;
  399. }
  400. VectorCopy (bytedirs[b], dir);
  401. }
  402. unsigned ColorBytes3 (float r, float g, float b) {
  403. unsigned i;
  404. ( (byte *)&i )[0] = r * 255;
  405. ( (byte *)&i )[1] = g * 255;
  406. ( (byte *)&i )[2] = b * 255;
  407. return i;
  408. }
  409. unsigned ColorBytes4 (float r, float g, float b, float a) {
  410. unsigned i;
  411. ( (byte *)&i )[0] = r * 255;
  412. ( (byte *)&i )[1] = g * 255;
  413. ( (byte *)&i )[2] = b * 255;
  414. ( (byte *)&i )[3] = a * 255;
  415. return i;
  416. }
  417. float NormalizeColor( const vec3_t in, vec3_t out ) {
  418. float max;
  419. max = in[0];
  420. if ( in[1] > max ) {
  421. max = in[1];
  422. }
  423. if ( in[2] > max ) {
  424. max = in[2];
  425. }
  426. if ( !max ) {
  427. VectorClear( out );
  428. } else {
  429. out[0] = in[0] / max;
  430. out[1] = in[1] / max;
  431. out[2] = in[2] / max;
  432. }
  433. return max;
  434. }
  435. /*
  436. =====================
  437. PlaneFromPoints
  438. Returns false if the triangle is degenrate.
  439. The normal will point out of the clock for clockwise ordered points
  440. =====================
  441. */
  442. qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
  443. vec3_t d1, d2;
  444. VectorSubtract( b, a, d1 );
  445. VectorSubtract( c, a, d2 );
  446. CrossProduct( d2, d1, plane );
  447. if ( VectorNormalize( plane ) == 0 ) {
  448. return qfalse;
  449. }
  450. plane[3] = DotProduct( a, plane );
  451. return qtrue;
  452. }
  453. /*
  454. ===============
  455. RotatePointAroundVector
  456. This is not implemented very well...
  457. ===============
  458. */
  459. void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
  460. float degrees ) {
  461. float m[3][3];
  462. float im[3][3];
  463. float zrot[3][3];
  464. float tmpmat[3][3];
  465. float rot[3][3];
  466. int i;
  467. vec3_t vr, vup, vf;
  468. float rad;
  469. vf[0] = dir[0];
  470. vf[1] = dir[1];
  471. vf[2] = dir[2];
  472. PerpendicularVector( vr, dir );
  473. CrossProduct( vr, vf, vup );
  474. m[0][0] = vr[0];
  475. m[1][0] = vr[1];
  476. m[2][0] = vr[2];
  477. m[0][1] = vup[0];
  478. m[1][1] = vup[1];
  479. m[2][1] = vup[2];
  480. m[0][2] = vf[0];
  481. m[1][2] = vf[1];
  482. m[2][2] = vf[2];
  483. memcpy( im, m, sizeof( im ) );
  484. im[0][1] = m[1][0];
  485. im[0][2] = m[2][0];
  486. im[1][0] = m[0][1];
  487. im[1][2] = m[2][1];
  488. im[2][0] = m[0][2];
  489. im[2][1] = m[1][2];
  490. memset( zrot, 0, sizeof( zrot ) );
  491. zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
  492. rad = DEG2RAD( degrees );
  493. zrot[0][0] = cos( rad );
  494. zrot[0][1] = sin( rad );
  495. zrot[1][0] = -sin( rad );
  496. zrot[1][1] = cos( rad );
  497. MatrixMultiply( m, zrot, tmpmat );
  498. MatrixMultiply( tmpmat, im, rot );
  499. for ( i = 0; i < 3; i++ ) {
  500. dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
  501. }
  502. }
  503. /*
  504. ===============
  505. RotateAroundDirection
  506. ===============
  507. */
  508. void RotateAroundDirection( vec3_t axis[3], float yaw ) {
  509. // create an arbitrary axis[1]
  510. PerpendicularVector( axis[1], axis[0] );
  511. // rotate it around axis[0] by yaw
  512. if ( yaw ) {
  513. vec3_t temp;
  514. VectorCopy( axis[1], temp );
  515. RotatePointAroundVector( axis[1], axis[0], temp, yaw );
  516. }
  517. // cross to get axis[2]
  518. CrossProduct( axis[0], axis[1], axis[2] );
  519. }
  520. void vectoangles( const vec3_t value1, vec3_t angles ) {
  521. float forward;
  522. float yaw, pitch;
  523. if ( value1[1] == 0 && value1[0] == 0 ) {
  524. yaw = 0;
  525. if ( value1[2] > 0 ) {
  526. pitch = 90;
  527. }
  528. else {
  529. pitch = 270;
  530. }
  531. }
  532. else {
  533. if ( value1[0] ) {
  534. yaw = ( atan2 ( value1[1], value1[0] ) * 180 / M_PI );
  535. }
  536. else if ( value1[1] > 0 ) {
  537. yaw = 90;
  538. }
  539. else {
  540. yaw = 270;
  541. }
  542. if ( yaw < 0 ) {
  543. yaw += 360;
  544. }
  545. forward = sqrt ( value1[0]*value1[0] + value1[1]*value1[1] );
  546. pitch = ( atan2(value1[2], forward) * 180 / M_PI );
  547. if ( pitch < 0 ) {
  548. pitch += 360;
  549. }
  550. }
  551. angles[PITCH] = -pitch;
  552. angles[YAW] = yaw;
  553. angles[ROLL] = 0;
  554. }
  555. /*
  556. =================
  557. AnglesToAxis
  558. =================
  559. */
  560. void AnglesToAxis( const vec3_t angles, vec3_t axis[3] ) {
  561. vec3_t right;
  562. // angle vectors returns "right" instead of "y axis"
  563. AngleVectors( angles, axis[0], right, axis[2] );
  564. VectorSubtract( vec3_origin, right, axis[1] );
  565. }
  566. void AxisClear( vec3_t axis[3] ) {
  567. axis[0][0] = 1;
  568. axis[0][1] = 0;
  569. axis[0][2] = 0;
  570. axis[1][0] = 0;
  571. axis[1][1] = 1;
  572. axis[1][2] = 0;
  573. axis[2][0] = 0;
  574. axis[2][1] = 0;
  575. axis[2][2] = 1;
  576. }
  577. void AxisCopy( vec3_t in[3], vec3_t out[3] ) {
  578. VectorCopy( in[0], out[0] );
  579. VectorCopy( in[1], out[1] );
  580. VectorCopy( in[2], out[2] );
  581. }
  582. void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
  583. {
  584. float d;
  585. vec3_t n;
  586. float inv_denom;
  587. inv_denom = DotProduct( normal, normal );
  588. #ifndef Q3_VM
  589. assert( Q_fabs(inv_denom) != 0.0f ); // bk010122 - zero vectors get here
  590. #endif
  591. inv_denom = 1.0f / inv_denom;
  592. d = DotProduct( normal, p ) * inv_denom;
  593. n[0] = normal[0] * inv_denom;
  594. n[1] = normal[1] * inv_denom;
  595. n[2] = normal[2] * inv_denom;
  596. dst[0] = p[0] - d * n[0];
  597. dst[1] = p[1] - d * n[1];
  598. dst[2] = p[2] - d * n[2];
  599. }
  600. /*
  601. ================
  602. MakeNormalVectors
  603. Given a normalized forward vector, create two
  604. other perpendicular vectors
  605. ================
  606. */
  607. void MakeNormalVectors( const vec3_t forward, vec3_t right, vec3_t up) {
  608. float d;
  609. // this rotate and negate guarantees a vector
  610. // not colinear with the original
  611. right[1] = -forward[0];
  612. right[2] = forward[1];
  613. right[0] = forward[2];
  614. d = DotProduct (right, forward);
  615. VectorMA (right, -d, forward, right);
  616. VectorNormalize (right);
  617. CrossProduct (right, forward, up);
  618. }
  619. void VectorRotate( vec3_t in, vec3_t matrix[3], vec3_t out )
  620. {
  621. out[0] = DotProduct( in, matrix[0] );
  622. out[1] = DotProduct( in, matrix[1] );
  623. out[2] = DotProduct( in, matrix[2] );
  624. }
  625. //============================================================================
  626. #if !idppc
  627. /*
  628. ** float q_rsqrt( float number )
  629. */
  630. float Q_rsqrt( float number )
  631. {
  632. long i;
  633. float x2, y;
  634. const float threehalfs = 1.5F;
  635. x2 = number * 0.5F;
  636. y = number;
  637. i = * ( long * ) &y; // evil floating point bit level hacking
  638. i = 0x5f3759df - ( i >> 1 ); // what the fuck?
  639. y = * ( float * ) &i;
  640. y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  641. // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
  642. #ifndef Q3_VM
  643. #ifdef __linux__
  644. assert( !isnan(y) ); // bk010122 - FPE?
  645. #endif
  646. #endif
  647. return y;
  648. }
  649. float Q_fabs( float f ) {
  650. int tmp = * ( int * ) &f;
  651. tmp &= 0x7FFFFFFF;
  652. return * ( float * ) &tmp;
  653. }
  654. #endif
  655. //============================================================
  656. /*
  657. ===============
  658. LerpAngle
  659. ===============
  660. */
  661. float LerpAngle (float from, float to, float frac) {
  662. float a;
  663. if ( to - from > 180 ) {
  664. to -= 360;
  665. }
  666. if ( to - from < -180 ) {
  667. to += 360;
  668. }
  669. a = from + frac * (to - from);
  670. return a;
  671. }
  672. /*
  673. =================
  674. AngleSubtract
  675. Always returns a value from -180 to 180
  676. =================
  677. */
  678. float AngleSubtract( float a1, float a2 ) {
  679. float a;
  680. a = a1 - a2;
  681. while ( a > 180 ) {
  682. a -= 360;
  683. }
  684. while ( a < -180 ) {
  685. a += 360;
  686. }
  687. return a;
  688. }
  689. void AnglesSubtract( vec3_t v1, vec3_t v2, vec3_t v3 ) {
  690. v3[0] = AngleSubtract( v1[0], v2[0] );
  691. v3[1] = AngleSubtract( v1[1], v2[1] );
  692. v3[2] = AngleSubtract( v1[2], v2[2] );
  693. }
  694. float AngleMod(float a) {
  695. a = (360.0/65536) * ((int)(a*(65536/360.0)) & 65535);
  696. return a;
  697. }
  698. /*
  699. =================
  700. AngleNormalize360
  701. returns angle normalized to the range [0 <= angle < 360]
  702. =================
  703. */
  704. float AngleNormalize360 ( float angle ) {
  705. return (360.0 / 65536) * ((int)(angle * (65536 / 360.0)) & 65535);
  706. }
  707. /*
  708. =================
  709. AngleNormalize180
  710. returns angle normalized to the range [-180 < angle <= 180]
  711. =================
  712. */
  713. float AngleNormalize180 ( float angle ) {
  714. angle = AngleNormalize360( angle );
  715. if ( angle > 180.0 ) {
  716. angle -= 360.0;
  717. }
  718. return angle;
  719. }
  720. /*
  721. =================
  722. AngleDelta
  723. returns the normalized delta from angle1 to angle2
  724. =================
  725. */
  726. float AngleDelta ( float angle1, float angle2 ) {
  727. return AngleNormalize180( angle1 - angle2 );
  728. }
  729. //============================================================
  730. /*
  731. =================
  732. SetPlaneSignbits
  733. =================
  734. */
  735. void SetPlaneSignbits (cplane_t *out) {
  736. int bits, j;
  737. // for fast box on planeside test
  738. bits = 0;
  739. for (j=0 ; j<3 ; j++) {
  740. if (out->normal[j] < 0) {
  741. bits |= 1<<j;
  742. }
  743. }
  744. out->signbits = bits;
  745. }
  746. /*
  747. ==================
  748. BoxOnPlaneSide
  749. Returns 1, 2, or 1 + 2
  750. // this is the slow, general version
  751. int BoxOnPlaneSide2 (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  752. {
  753. int i;
  754. float dist1, dist2;
  755. int sides;
  756. vec3_t corners[2];
  757. for (i=0 ; i<3 ; i++)
  758. {
  759. if (p->normal[i] < 0)
  760. {
  761. corners[0][i] = emins[i];
  762. corners[1][i] = emaxs[i];
  763. }
  764. else
  765. {
  766. corners[1][i] = emins[i];
  767. corners[0][i] = emaxs[i];
  768. }
  769. }
  770. dist1 = DotProduct (p->normal, corners[0]) - p->dist;
  771. dist2 = DotProduct (p->normal, corners[1]) - p->dist;
  772. sides = 0;
  773. if (dist1 >= 0)
  774. sides = 1;
  775. if (dist2 < 0)
  776. sides |= 2;
  777. return sides;
  778. }
  779. ==================
  780. */
  781. #if !( (defined __linux__ || __FreeBSD__) && (defined __i386__) && (!defined C_ONLY)) // rb010123
  782. #if defined __LCC__ || defined C_ONLY || !id386 || defined __VECTORC
  783. int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  784. {
  785. float dist1, dist2;
  786. int sides;
  787. // fast axial cases
  788. if (p->type < 3)
  789. {
  790. if (p->dist <= emins[p->type])
  791. return 1;
  792. if (p->dist >= emaxs[p->type])
  793. return 2;
  794. return 3;
  795. }
  796. // general case
  797. switch (p->signbits)
  798. {
  799. case 0:
  800. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  801. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  802. break;
  803. case 1:
  804. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  805. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  806. break;
  807. case 2:
  808. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  809. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  810. break;
  811. case 3:
  812. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  813. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  814. break;
  815. case 4:
  816. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  817. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  818. break;
  819. case 5:
  820. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  821. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  822. break;
  823. case 6:
  824. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  825. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  826. break;
  827. case 7:
  828. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  829. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  830. break;
  831. default:
  832. dist1 = dist2 = 0; // shut up compiler
  833. break;
  834. }
  835. sides = 0;
  836. if (dist1 >= p->dist)
  837. sides = 1;
  838. if (dist2 < p->dist)
  839. sides |= 2;
  840. return sides;
  841. }
  842. #else
  843. #pragma warning( disable: 4035 )
  844. __declspec( naked ) int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  845. {
  846. static int bops_initialized;
  847. static int Ljmptab[8];
  848. __asm {
  849. push ebx
  850. cmp bops_initialized, 1
  851. je initialized
  852. mov bops_initialized, 1
  853. mov Ljmptab[0*4], offset Lcase0
  854. mov Ljmptab[1*4], offset Lcase1
  855. mov Ljmptab[2*4], offset Lcase2
  856. mov Ljmptab[3*4], offset Lcase3
  857. mov Ljmptab[4*4], offset Lcase4
  858. mov Ljmptab[5*4], offset Lcase5
  859. mov Ljmptab[6*4], offset Lcase6
  860. mov Ljmptab[7*4], offset Lcase7
  861. initialized:
  862. mov edx,dword ptr[4+12+esp]
  863. mov ecx,dword ptr[4+4+esp]
  864. xor eax,eax
  865. mov ebx,dword ptr[4+8+esp]
  866. mov al,byte ptr[17+edx]
  867. cmp al,8
  868. jge Lerror
  869. fld dword ptr[0+edx]
  870. fld st(0)
  871. jmp dword ptr[Ljmptab+eax*4]
  872. Lcase0:
  873. fmul dword ptr[ebx]
  874. fld dword ptr[0+4+edx]
  875. fxch st(2)
  876. fmul dword ptr[ecx]
  877. fxch st(2)
  878. fld st(0)
  879. fmul dword ptr[4+ebx]
  880. fld dword ptr[0+8+edx]
  881. fxch st(2)
  882. fmul dword ptr[4+ecx]
  883. fxch st(2)
  884. fld st(0)
  885. fmul dword ptr[8+ebx]
  886. fxch st(5)
  887. faddp st(3),st(0)
  888. fmul dword ptr[8+ecx]
  889. fxch st(1)
  890. faddp st(3),st(0)
  891. fxch st(3)
  892. faddp st(2),st(0)
  893. jmp LSetSides
  894. Lcase1:
  895. fmul dword ptr[ecx]
  896. fld dword ptr[0+4+edx]
  897. fxch st(2)
  898. fmul dword ptr[ebx]
  899. fxch st(2)
  900. fld st(0)
  901. fmul dword ptr[4+ebx]
  902. fld dword ptr[0+8+edx]
  903. fxch st(2)
  904. fmul dword ptr[4+ecx]
  905. fxch st(2)
  906. fld st(0)
  907. fmul dword ptr[8+ebx]
  908. fxch st(5)
  909. faddp st(3),st(0)
  910. fmul dword ptr[8+ecx]
  911. fxch st(1)
  912. faddp st(3),st(0)
  913. fxch st(3)
  914. faddp st(2),st(0)
  915. jmp LSetSides
  916. Lcase2:
  917. fmul dword ptr[ebx]
  918. fld dword ptr[0+4+edx]
  919. fxch st(2)
  920. fmul dword ptr[ecx]
  921. fxch st(2)
  922. fld st(0)
  923. fmul dword ptr[4+ecx]
  924. fld dword ptr[0+8+edx]
  925. fxch st(2)
  926. fmul dword ptr[4+ebx]
  927. fxch st(2)
  928. fld st(0)
  929. fmul dword ptr[8+ebx]
  930. fxch st(5)
  931. faddp st(3),st(0)
  932. fmul dword ptr[8+ecx]
  933. fxch st(1)
  934. faddp st(3),st(0)
  935. fxch st(3)
  936. faddp st(2),st(0)
  937. jmp LSetSides
  938. Lcase3:
  939. fmul dword ptr[ecx]
  940. fld dword ptr[0+4+edx]
  941. fxch st(2)
  942. fmul dword ptr[ebx]
  943. fxch st(2)
  944. fld st(0)
  945. fmul dword ptr[4+ecx]
  946. fld dword ptr[0+8+edx]
  947. fxch st(2)
  948. fmul dword ptr[4+ebx]
  949. fxch st(2)
  950. fld st(0)
  951. fmul dword ptr[8+ebx]
  952. fxch st(5)
  953. faddp st(3),st(0)
  954. fmul dword ptr[8+ecx]
  955. fxch st(1)
  956. faddp st(3),st(0)
  957. fxch st(3)
  958. faddp st(2),st(0)
  959. jmp LSetSides
  960. Lcase4:
  961. fmul dword ptr[ebx]
  962. fld dword ptr[0+4+edx]
  963. fxch st(2)
  964. fmul dword ptr[ecx]
  965. fxch st(2)
  966. fld st(0)
  967. fmul dword ptr[4+ebx]
  968. fld dword ptr[0+8+edx]
  969. fxch st(2)
  970. fmul dword ptr[4+ecx]
  971. fxch st(2)
  972. fld st(0)
  973. fmul dword ptr[8+ecx]
  974. fxch st(5)
  975. faddp st(3),st(0)
  976. fmul dword ptr[8+ebx]
  977. fxch st(1)
  978. faddp st(3),st(0)
  979. fxch st(3)
  980. faddp st(2),st(0)
  981. jmp LSetSides
  982. Lcase5:
  983. fmul dword ptr[ecx]
  984. fld dword ptr[0+4+edx]
  985. fxch st(2)
  986. fmul dword ptr[ebx]
  987. fxch st(2)
  988. fld st(0)
  989. fmul dword ptr[4+ebx]
  990. fld dword ptr[0+8+edx]
  991. fxch st(2)
  992. fmul dword ptr[4+ecx]
  993. fxch st(2)
  994. fld st(0)
  995. fmul dword ptr[8+ecx]
  996. fxch st(5)
  997. faddp st(3),st(0)
  998. fmul dword ptr[8+ebx]
  999. fxch st(1)
  1000. faddp st(3),st(0)
  1001. fxch st(3)
  1002. faddp st(2),st(0)
  1003. jmp LSetSides
  1004. Lcase6:
  1005. fmul dword ptr[ebx]
  1006. fld dword ptr[0+4+edx]
  1007. fxch st(2)
  1008. fmul dword ptr[ecx]
  1009. fxch st(2)
  1010. fld st(0)
  1011. fmul dword ptr[4+ecx]
  1012. fld dword ptr[0+8+edx]
  1013. fxch st(2)
  1014. fmul dword ptr[4+ebx]
  1015. fxch st(2)
  1016. fld st(0)
  1017. fmul dword ptr[8+ecx]
  1018. fxch st(5)
  1019. faddp st(3),st(0)
  1020. fmul dword ptr[8+ebx]
  1021. fxch st(1)
  1022. faddp st(3),st(0)
  1023. fxch st(3)
  1024. faddp st(2),st(0)
  1025. jmp LSetSides
  1026. Lcase7:
  1027. fmul dword ptr[ecx]
  1028. fld dword ptr[0+4+edx]
  1029. fxch st(2)
  1030. fmul dword ptr[ebx]
  1031. fxch st(2)
  1032. fld st(0)
  1033. fmul dword ptr[4+ecx]
  1034. fld dword ptr[0+8+edx]
  1035. fxch st(2)
  1036. fmul dword ptr[4+ebx]
  1037. fxch st(2)
  1038. fld st(0)
  1039. fmul dword ptr[8+ecx]
  1040. fxch st(5)
  1041. faddp st(3),st(0)
  1042. fmul dword ptr[8+ebx]
  1043. fxch st(1)
  1044. faddp st(3),st(0)
  1045. fxch st(3)
  1046. faddp st(2),st(0)
  1047. LSetSides:
  1048. faddp st(2),st(0)
  1049. fcomp dword ptr[12+edx]
  1050. xor ecx,ecx
  1051. fnstsw ax
  1052. fcomp dword ptr[12+edx]
  1053. and ah,1
  1054. xor ah,1
  1055. add cl,ah
  1056. fnstsw ax
  1057. and ah,1
  1058. add ah,ah
  1059. add cl,ah
  1060. pop ebx
  1061. mov eax,ecx
  1062. ret
  1063. Lerror:
  1064. int 3
  1065. }
  1066. }
  1067. #pragma warning( default: 4035 )
  1068. #endif
  1069. #endif
  1070. /*
  1071. =================
  1072. RadiusFromBounds
  1073. =================
  1074. */
  1075. float RadiusFromBounds( const vec3_t mins, const vec3_t maxs ) {
  1076. int i;
  1077. vec3_t corner;
  1078. float a, b;
  1079. for (i=0 ; i<3 ; i++) {
  1080. a = fabs( mins[i] );
  1081. b = fabs( maxs[i] );
  1082. corner[i] = a > b ? a : b;
  1083. }
  1084. return VectorLength (corner);
  1085. }
  1086. void ClearBounds( vec3_t mins, vec3_t maxs ) {
  1087. mins[0] = mins[1] = mins[2] = 99999;
  1088. maxs[0] = maxs[1] = maxs[2] = -99999;
  1089. }
  1090. void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs ) {
  1091. if ( v[0] < mins[0] ) {
  1092. mins[0] = v[0];
  1093. }
  1094. if ( v[0] > maxs[0]) {
  1095. maxs[0] = v[0];
  1096. }
  1097. if ( v[1] < mins[1] ) {
  1098. mins[1] = v[1];
  1099. }
  1100. if ( v[1] > maxs[1]) {
  1101. maxs[1] = v[1];
  1102. }
  1103. if ( v[2] < mins[2] ) {
  1104. mins[2] = v[2];
  1105. }
  1106. if ( v[2] > maxs[2]) {
  1107. maxs[2] = v[2];
  1108. }
  1109. }
  1110. vec_t VectorNormalize( vec3_t v ) {
  1111. float length, ilength;
  1112. length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  1113. length = sqrt (length);
  1114. if ( length ) {
  1115. ilength = 1/length;
  1116. v[0] *= ilength;
  1117. v[1] *= ilength;
  1118. v[2] *= ilength;
  1119. }
  1120. return length;
  1121. }
  1122. vec_t VectorNormalize2( const vec3_t v, vec3_t out) {
  1123. float length, ilength;
  1124. length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  1125. length = sqrt (length);
  1126. if (length)
  1127. {
  1128. #ifndef Q3_VM // bk0101022 - FPE related
  1129. // assert( ((Q_fabs(v[0])!=0.0f) || (Q_fabs(v[1])!=0.0f) || (Q_fabs(v[2])!=0.0f)) );
  1130. #endif
  1131. ilength = 1/length;
  1132. out[0] = v[0]*ilength;
  1133. out[1] = v[1]*ilength;
  1134. out[2] = v[2]*ilength;
  1135. } else {
  1136. #ifndef Q3_VM // bk0101022 - FPE related
  1137. // assert( ((Q_fabs(v[0])==0.0f) && (Q_fabs(v[1])==0.0f) && (Q_fabs(v[2])==0.0f)) );
  1138. #endif
  1139. VectorClear( out );
  1140. }
  1141. return length;
  1142. }
  1143. void _VectorMA( const vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc) {
  1144. vecc[0] = veca[0] + scale*vecb[0];
  1145. vecc[1] = veca[1] + scale*vecb[1];
  1146. vecc[2] = veca[2] + scale*vecb[2];
  1147. }
  1148. vec_t _DotProduct( const vec3_t v1, const vec3_t v2 ) {
  1149. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  1150. }
  1151. void _VectorSubtract( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
  1152. out[0] = veca[0]-vecb[0];
  1153. out[1] = veca[1]-vecb[1];
  1154. out[2] = veca[2]-vecb[2];
  1155. }
  1156. void _VectorAdd( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
  1157. out[0] = veca[0]+vecb[0];
  1158. out[1] = veca[1]+vecb[1];
  1159. out[2] = veca[2]+vecb[2];
  1160. }
  1161. void _VectorCopy( const vec3_t in, vec3_t out ) {
  1162. out[0] = in[0];
  1163. out[1] = in[1];
  1164. out[2] = in[2];
  1165. }
  1166. void _VectorScale( const vec3_t in, vec_t scale, vec3_t out ) {
  1167. out[0] = in[0]*scale;
  1168. out[1] = in[1]*scale;
  1169. out[2] = in[2]*scale;
  1170. }
  1171. void Vector4Scale( const vec4_t in, vec_t scale, vec4_t out ) {
  1172. out[0] = in[0]*scale;
  1173. out[1] = in[1]*scale;
  1174. out[2] = in[2]*scale;
  1175. out[3] = in[3]*scale;
  1176. }
  1177. int Q_log2( int val ) {
  1178. int answer;
  1179. answer = 0;
  1180. while ( ( val>>=1 ) != 0 ) {
  1181. answer++;
  1182. }
  1183. return answer;
  1184. }
  1185. /*
  1186. =================
  1187. PlaneTypeForNormal
  1188. =================
  1189. */
  1190. /*
  1191. int PlaneTypeForNormal (vec3_t normal) {
  1192. if ( normal[0] == 1.0 )
  1193. return PLANE_X;
  1194. if ( normal[1] == 1.0 )
  1195. return PLANE_Y;
  1196. if ( normal[2] == 1.0 )
  1197. return PLANE_Z;
  1198. return PLANE_NON_AXIAL;
  1199. }
  1200. */
  1201. /*
  1202. ================
  1203. MatrixMultiply
  1204. ================
  1205. */
  1206. void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
  1207. out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
  1208. in1[0][2] * in2[2][0];
  1209. out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
  1210. in1[0][2] * in2[2][1];
  1211. out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
  1212. in1[0][2] * in2[2][2];
  1213. out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
  1214. in1[1][2] * in2[2][0];
  1215. out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
  1216. in1[1][2] * in2[2][1];
  1217. out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
  1218. in1[1][2] * in2[2][2];
  1219. out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
  1220. in1[2][2] * in2[2][0];
  1221. out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
  1222. in1[2][2] * in2[2][1];
  1223. out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
  1224. in1[2][2] * in2[2][2];
  1225. }
  1226. void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up) {
  1227. float angle;
  1228. static float sr, sp, sy, cr, cp, cy;
  1229. // static to help MS compiler fp bugs
  1230. angle = angles[YAW] * (M_PI*2 / 360);
  1231. sy = sin(angle);
  1232. cy = cos(angle);
  1233. angle = angles[PITCH] * (M_PI*2 / 360);
  1234. sp = sin(angle);
  1235. cp = cos(angle);
  1236. angle = angles[ROLL] * (M_PI*2 / 360);
  1237. sr = sin(angle);
  1238. cr = cos(angle);
  1239. if (forward)
  1240. {
  1241. forward[0] = cp*cy;
  1242. forward[1] = cp*sy;
  1243. forward[2] = -sp;
  1244. }
  1245. if (right)
  1246. {
  1247. right[0] = (-1*sr*sp*cy+-1*cr*-sy);
  1248. right[1] = (-1*sr*sp*sy+-1*cr*cy);
  1249. right[2] = -1*sr*cp;
  1250. }
  1251. if (up)
  1252. {
  1253. up[0] = (cr*sp*cy+-sr*-sy);
  1254. up[1] = (cr*sp*sy+-sr*cy);
  1255. up[2] = cr*cp;
  1256. }
  1257. }
  1258. /*
  1259. ** assumes "src" is normalized
  1260. */
  1261. void PerpendicularVector( vec3_t dst, const vec3_t src )
  1262. {
  1263. int pos;
  1264. int i;
  1265. float minelem = 1.0F;
  1266. vec3_t tempvec;
  1267. /*
  1268. ** find the smallest magnitude axially aligned vector
  1269. */
  1270. for ( pos = 0, i = 0; i < 3; i++ )
  1271. {
  1272. if ( fabs( src[i] ) < minelem )
  1273. {
  1274. pos = i;
  1275. minelem = fabs( src[i] );
  1276. }
  1277. }
  1278. tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
  1279. tempvec[pos] = 1.0F;
  1280. /*
  1281. ** project the point onto the plane defined by src
  1282. */
  1283. ProjectPointOnPlane( dst, tempvec, src );
  1284. /*
  1285. ** normalize the result
  1286. */
  1287. VectorNormalize( dst );
  1288. }