qrad.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #ifndef HLRAD_H__
  2. #define HLRAD_H__
  3. #if _MSC_VER >= 1000
  4. #pragma once
  5. #endif
  6. #include "cmdlib.h"
  7. #include "messages.h"
  8. #include "win32fix.h"
  9. #include "log.h"
  10. #include "hlassert.h"
  11. #include "mathlib.h"
  12. #include "bspfile.h"
  13. #include "winding.h"
  14. #include "scriplib.h"
  15. #include "threads.h"
  16. #include "blockmem.h"
  17. #include "filelib.h"
  18. #include "winding.h"
  19. #ifdef SYSTEM_WIN32
  20. #pragma warning(disable:4142 4028)
  21. #include <io.h>
  22. #pragma warning(default: 4142 4028)
  23. #endif
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #ifdef HAVE_FCNTL_H
  28. #include <fcntl.h>
  29. #endif
  30. #ifdef STDC_HEADERS
  31. #include <ctype.h>
  32. #endif
  33. #ifdef SYSTEM_WIN32
  34. #include <direct.h>
  35. #endif
  36. #define DEFAULT_LERP_ENABLED true
  37. #define DEFAULT_FADE 1.0
  38. #define DEFAULT_FALLOFF 2
  39. #define DEFAULT_BOUNCE 1
  40. #define DEFAULT_BOUNCE_DYNAMIC true
  41. #define DEFAULT_DUMPPATCHES false
  42. #define DEFAULT_AMBIENT_RED 0.0
  43. #define DEFAULT_AMBIENT_GREEN 0.0
  44. #define DEFAULT_AMBIENT_BLUE 0.0
  45. #define DEFAULT_MAXLIGHT 256.0
  46. #define DEFAULT_TEXSCALE true
  47. #define DEFAULT_CHOP 64.0
  48. #define DEFAULT_TEXCHOP 32.0
  49. #define DEFAULT_LIGHTSCALE 1.0
  50. #define DEFAULT_DLIGHT_THRESHOLD 25.0
  51. #define DEFAULT_DLIGHT_SCALE 2.0
  52. #define DEFAULT_SMOOTHING_VALUE 50.0
  53. #define DEFAULT_INCREMENTAL false
  54. #ifdef ZHLT_PROGRESSFILE // AJM
  55. #define DEFAULT_PROGRESSFILE NULL // progress file is only used if g_progressfile is non-null
  56. #endif
  57. // ------------------------------------------------------------------------
  58. // Changes by Adam Foster - [email protected]
  59. // superseded by DEFAULT_COLOUR_LIGHTSCALE_*
  60. #ifndef HLRAD_WHOME
  61. #define DEFAULT_LIGHTSCALE 1.0
  62. #endif
  63. // superseded by DEFAULT_COLOUR_GAMMA_*
  64. #ifndef HLRAD_WHOME
  65. #define DEFAULT_GAMMA 0.5
  66. #endif
  67. // ------------------------------------------------------------------------
  68. #define DEFAULT_INDIRECT_SUN 1.0
  69. #define DEFAULT_EXTRA false
  70. #define DEFAULT_SKY_LIGHTING_FIX true
  71. #define DEFAULT_CIRCUS false
  72. #define DEFAULT_CORING 1.0
  73. #define DEFAULT_SUBDIVIDE true
  74. #define DEFAULT_CHART false
  75. #define DEFAULT_SKYCLIP true
  76. #define DEFAULT_INFO true
  77. #define DEFAULT_ALLOW_OPAQUES true
  78. // ------------------------------------------------------------------------
  79. // Changes by Adam Foster - [email protected]
  80. #ifdef HLRAD_WHOME
  81. #define DEFAULT_COLOUR_GAMMA_RED 0.5
  82. #define DEFAULT_COLOUR_GAMMA_GREEN 0.5
  83. #define DEFAULT_COLOUR_GAMMA_BLUE 0.5
  84. #define DEFAULT_COLOUR_LIGHTSCALE_RED 1.0
  85. #define DEFAULT_COLOUR_LIGHTSCALE_GREEN 1.0
  86. #define DEFAULT_COLOUR_LIGHTSCALE_BLUE 1.0
  87. #define DEFAULT_COLOUR_JITTER_HACK_RED 0.0
  88. #define DEFAULT_COLOUR_JITTER_HACK_GREEN 0.0
  89. #define DEFAULT_COLOUR_JITTER_HACK_BLUE 0.0
  90. #define DEFAULT_JITTER_HACK_RED 0.0
  91. #define DEFAULT_JITTER_HACK_GREEN 0.0
  92. #define DEFAULT_JITTER_HACK_BLUE 0.0
  93. #define DEFAULT_DIFFUSE_HACK true
  94. #define DEFAULT_SPOTLIGHT_HACK true
  95. #define DEFAULT_SOFTLIGHT_HACK_RED 0.0
  96. #define DEFAULT_SOFTLIGHT_HACK_GREEN 0.0
  97. #define DEFAULT_SOFTLIGHT_HACK_BLUE 0.0
  98. #define DEFAULT_SOFTLIGHT_HACK_DISTANCE 0.0
  99. #endif
  100. // ------------------------------------------------------------------------
  101. // O_o ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  102. // Changes by Jussi Kivilinna <[email protected]> [http://hullu.xtragaming.com/]
  103. #ifdef HLRAD_HULLU
  104. // Transparency light support for bounced light(transfers) is extreamly slow
  105. // for 'vismatrix' and 'sparse' atm.
  106. // Only recommended to be used with 'nomatrix' mode
  107. #define DEFAULT_CUSTOMSHADOW_WITH_BOUNCELIGHT false
  108. // RGB Transfers support for HLRAD .. to be used with -customshadowwithbounce
  109. #define DEFAULT_RGB_TRANSFERS false
  110. #endif
  111. // o_O ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  112. #ifdef SYSTEM_WIN32
  113. #define DEFAULT_ESTIMATE false
  114. #endif
  115. #ifdef SYSTEM_POSIX
  116. #define DEFAULT_ESTIMATE true
  117. #endif
  118. // Ideally matches what is in the FGD :)
  119. #define SPAWNFLAG_NOBLEEDADJUST (1 << 0)
  120. // DEFAULT_HUNT_OFFSET is how many units in front of the plane to place the samples
  121. // Unit of '1' causes the 1 unit crate trick to cause extra shadows
  122. #define DEFAULT_HUNT_OFFSET 0.5
  123. // DEFAULT_HUNT_SIZE number of iterations (one based) of radial search in HuntForWorld
  124. #define DEFAULT_HUNT_SIZE 11
  125. // DEFAULT_HUNT_SCALE amount to grow from origin point per iteration of DEFAULT_HUNT_SIZE in HuntForWorld
  126. #define DEFAULT_HUNT_SCALE 0.1
  127. // If patches are allowed to be closer, the light gets amplified (which looks really damn weird)
  128. #define MINIMUM_PATCH_DISTANCE 1.01
  129. //
  130. // LIGHTMAP.C STUFF
  131. //
  132. typedef enum
  133. {
  134. emit_surface,
  135. emit_point,
  136. emit_spotlight,
  137. emit_skylight
  138. }
  139. emittype_t;
  140. typedef struct directlight_s
  141. {
  142. struct directlight_s* next;
  143. emittype_t type;
  144. int style;
  145. vec3_t origin;
  146. vec3_t intensity;
  147. vec3_t normal; // for surfaces and spotlights
  148. float stopdot; // for spotlights
  149. float stopdot2; // for spotlights
  150. // 'Arghrad'-like features
  151. vec_t fade; // falloff scaling for linear and inverse square falloff 1.0 = normal, 0.5 = farther, 2.0 = shorter etc
  152. unsigned char falloff; // falloff style 0 = default (inverse square), 1 = inverse falloff, 2 = inverse square (arghrad compat)
  153. // -----------------------------------------------------------------------------------
  154. // Changes by Adam Foster - [email protected]
  155. // Diffuse light_environment light colour
  156. // Really horrible hack which probably won't work!
  157. #ifdef HLRAD_WHOME
  158. vec3_t diffuse_intensity;
  159. #endif
  160. // -----------------------------------------------------------------------------------
  161. } directlight_t;
  162. #define TRANSFER_SCALE_VAL (USHRT_MAX/4)
  163. #define TRANSFER_SCALE (1.0 / TRANSFER_SCALE_VAL)
  164. #define INVERSE_TRANSFER_SCALE (TRANSFER_SCALE_VAL)
  165. #define TRANSFER_SCALE_MAX (TRANSFER_SCALE_VAL * 4)
  166. typedef struct
  167. {
  168. unsigned size : 12;
  169. unsigned index : 20;
  170. } transfer_index_t;
  171. typedef unsigned transfer_raw_index_t;
  172. typedef float transfer_data_t;
  173. //Special RGB mode for transfers
  174. #ifdef HLRAD_HULLU
  175. #if defined(HLRAD_HULLU_48BIT_RGB_TRANSFERS) && defined(HLRAD_HULLU_96BIT_RGB_TRANSFERS)
  176. #error Conflict: Both HLRAD_HULLU_48BIT_RGB_TRANSFERS and HLRAD_HULLU_96BIT_RGB_TRANSFERS defined!
  177. #elif defined(HLRAD_HULLU_96BIT_RGB_TRANSFERS)
  178. //96bit (no noticeable difference to 48bit)
  179. typedef float rgb_transfer_t[3];
  180. #else
  181. //default.. 48bit
  182. typedef unsigned short rgb_transfer_t[3];
  183. #endif
  184. typedef rgb_transfer_t rgb_transfer_data_t;
  185. #endif
  186. #define MAX_COMPRESSED_TRANSFER_INDEX_SIZE ((1 << 12) - 1)
  187. #define MAX_PATCHES (65545*4) // hard limit
  188. #define MAX_VISMATRIX_PATCHES 65545 // hard limit
  189. #define MAX_SPARSE_VISMATRIX_PATCHES MAX_PATCHES
  190. typedef enum
  191. {
  192. ePatchFlagNull = 0,
  193. ePatchFlagOutside = 1
  194. } ePatchFlags;
  195. typedef struct patch_s
  196. {
  197. struct patch_s* next; // next in face
  198. vec3_t origin; // Center centroid of winding (cached info calculated from winding)
  199. vec_t area; // Surface area of this patch (cached info calculated from winding)
  200. Winding* winding; // Winding (patches are triangles, so its easy)
  201. vec_t scale; // Texture scale for this face (blend of S and T scale)
  202. vec_t chop; // Texture chop for this face factoring in S and T scale
  203. unsigned iIndex;
  204. unsigned iData;
  205. transfer_index_t* tIndex;
  206. transfer_data_t* tData;
  207. #ifdef HLRAD_HULLU
  208. rgb_transfer_data_t* tRGBData;
  209. #endif
  210. int faceNumber;
  211. ePatchFlags flags;
  212. #ifdef ZHLT_TEXLIGHT
  213. int totalstyle[MAXLIGHTMAPS]; //LRC - gives the styles for use by the new switchable totallight values
  214. vec3_t totallight[MAXLIGHTMAPS]; // accumulated by radiosity does NOT include light accounted for by direct lighting
  215. vec3_t directlight[MAXLIGHTMAPS]; // direct light only
  216. int emitstyle; //LRC - for switchable texlights
  217. vec3_t baselight; // emissivity only, uses emitstyle
  218. vec3_t samplelight[MAXLIGHTMAPS];
  219. int samples[MAXLIGHTMAPS]; // for averaging direct light
  220. #else
  221. vec3_t totallight; // accumulated by radiosity does NOT include light accounted for by direct lighting
  222. vec3_t baselight; // emissivity only
  223. vec3_t directlight; // direct light value
  224. vec3_t samplelight;
  225. int samples; // for averaging direct light
  226. #endif
  227. } patch_t;
  228. #ifdef ZHLT_TEXLIGHT
  229. //LRC
  230. vec3_t* GetTotalLight(patch_t* patch, int style);
  231. #endif
  232. typedef struct
  233. {
  234. dface_t* faces[2];
  235. vec3_t interface_normal;
  236. vec_t cos_normals_angle;
  237. bool coplanar;
  238. } edgeshare_t;
  239. extern edgeshare_t g_edgeshare[MAX_MAP_EDGES];
  240. //
  241. // lerp.c stuff
  242. //
  243. typedef struct lerprect_s
  244. {
  245. dplane_t plane; // all walls will be perpindicular to face normal in some direction
  246. vec3_t vertex[4];
  247. }
  248. lerpWall_t;
  249. typedef struct lerpdist_s
  250. {
  251. vec_t dist;
  252. unsigned patch;
  253. } lerpDist_t;
  254. // Valve's default was 2048 originally.
  255. // MAX_LERP_POINTS causes lerpTriangulation_t to consume :
  256. // 2048 : roughly 17.5Mb
  257. // 3072 : roughly 35Mb
  258. // 4096 : roughly 70Mb
  259. #define DEFAULT_MAX_LERP_POINTS 512
  260. #define DEFAULT_MAX_LERP_WALLS 128
  261. typedef struct
  262. {
  263. unsigned maxpoints;
  264. unsigned numpoints;
  265. unsigned maxwalls;
  266. unsigned numwalls;
  267. patch_t** points; // maxpoints
  268. lerpDist_t* dists; // numpoints after points is populated
  269. lerpWall_t* walls; // maxwalls
  270. unsigned facenum;
  271. const dface_t* face;
  272. const dplane_t* plane;
  273. }
  274. lerpTriangulation_t;
  275. // These are bitflags for lighting adjustments for special cases
  276. typedef enum
  277. {
  278. eModelLightmodeNull = 0,
  279. eModelLightmodeEmbedded = 0x01,
  280. eModelLightmodeOpaque = 0x02,
  281. eModelLightmodeConcave = 0x04
  282. }
  283. eModelLightmodes;
  284. typedef struct
  285. {
  286. Winding* winding;
  287. vec3_t mins;
  288. vec3_t maxs;
  289. dplane_t plane;
  290. unsigned facenum;
  291. #ifdef HLRAD_HULLU
  292. vec3_t transparency_scale;
  293. bool transparency;
  294. #endif
  295. } opaqueList_t;
  296. #define OPAQUE_ARRAY_GROWTH_SIZE 1024
  297. //
  298. // qrad globals
  299. //
  300. extern patch_t* g_face_patches[MAX_MAP_FACES];
  301. extern entity_t* g_face_entity[MAX_MAP_FACES];
  302. extern vec3_t g_face_offset[MAX_MAP_FACES]; // for models with origins
  303. extern eModelLightmodes g_face_lightmode[MAX_MAP_FACES];
  304. extern vec3_t g_face_centroids[MAX_MAP_EDGES];
  305. extern patch_t g_patches[MAX_PATCHES];
  306. extern unsigned g_num_patches;
  307. extern float g_lightscale;
  308. extern float g_dlight_threshold;
  309. extern float g_coring;
  310. extern int g_lerp_enabled;
  311. extern void MakeShadowSplits();
  312. //==============================================
  313. extern bool g_extra;
  314. extern vec3_t g_ambient;
  315. extern vec_t g_direct_scale;
  316. extern float g_maxlight;
  317. extern unsigned g_numbounce;
  318. extern float g_qgamma;
  319. extern float g_indirect_sun;
  320. extern float g_smoothing_threshold;
  321. extern float g_smoothing_value;
  322. extern bool g_estimate;
  323. extern char g_source[_MAX_PATH];
  324. extern vec_t g_fade;
  325. extern int g_falloff;
  326. extern bool g_incremental;
  327. extern bool g_circus;
  328. extern bool g_sky_lighting_fix;
  329. extern vec_t g_chop; // Chop value for normal textures
  330. extern vec_t g_texchop; // Chop value for texture lights
  331. extern opaqueList_t* g_opaque_face_list;
  332. extern unsigned g_opaque_face_count;
  333. extern unsigned g_max_opaque_face_count; // Current array maximum (used for reallocs)
  334. #ifdef ZHLT_PROGRESSFILE // AJM
  335. extern char* g_progressfile ;
  336. #endif
  337. // ------------------------------------------------------------------------
  338. // Changes by Adam Foster - [email protected]
  339. #ifdef HLRAD_WHOME
  340. extern vec3_t g_colour_qgamma;
  341. extern vec3_t g_colour_lightscale;
  342. extern vec3_t g_colour_jitter_hack;
  343. extern vec3_t g_jitter_hack;
  344. extern bool g_diffuse_hack;
  345. extern bool g_spotlight_hack;
  346. extern vec3_t g_softlight_hack;
  347. extern float g_softlight_hack_distance;
  348. #endif
  349. // ------------------------------------------------------------------------
  350. #ifdef HLRAD_HULLU
  351. extern bool g_customshadow_with_bouncelight;
  352. extern bool g_rgb_transfers;
  353. extern const vec3_t vec3_one;
  354. #endif
  355. extern void MakeTnodes(dmodel_t* bm);
  356. extern void PairEdges();
  357. extern void BuildFacelights(int facenum);
  358. extern void PrecompLightmapOffsets();
  359. extern void FinalLightFace(int facenum);
  360. extern int TestLine(const vec3_t start, const vec3_t stop);
  361. extern int TestLine_r(int node, const vec3_t start, const vec3_t stop);
  362. extern void CreateDirectLights();
  363. extern void DeleteDirectLights();
  364. extern void GetPhongNormal(int facenum, vec3_t spot, vec3_t phongnormal);
  365. #ifdef HLRAD_HULLU
  366. typedef bool (*funcCheckVisBit) (unsigned, unsigned, vec3_t&, unsigned int&);
  367. #else
  368. typedef bool (*funcCheckVisBit) (unsigned, unsigned);
  369. #endif
  370. extern funcCheckVisBit g_CheckVisBit;
  371. // qradutil.c
  372. extern vec_t PatchPlaneDist(const patch_t* const patch);
  373. extern dleaf_t* PointInLeaf(const vec3_t point);
  374. extern void MakeBackplanes();
  375. extern const dplane_t* getPlaneFromFace(const dface_t* const face);
  376. extern const dplane_t* getPlaneFromFaceNumber(unsigned int facenum);
  377. extern void getAdjustedPlaneFromFaceNumber(unsigned int facenum, dplane_t* plane);
  378. extern 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);
  379. // makescales.c
  380. extern void MakeScalesVismatrix();
  381. extern void MakeScalesSparseVismatrix();
  382. extern void MakeScalesNoVismatrix();
  383. // transfers.c
  384. extern unsigned g_total_transfer;
  385. extern bool readtransfers(const char* const transferfile, long numpatches);
  386. extern void writetransfers(const char* const transferfile, long total_patches);
  387. // vismatrixutil.c (shared between vismatrix.c and sparse.c)
  388. extern void SwapTransfers(int patchnum);
  389. extern void MakeScales(int threadnum);
  390. extern void DumpTransfersMemoryUsage();
  391. #ifdef HLRAD_HULLU
  392. extern void SwapRGBTransfers(int patchnum);
  393. extern void MakeRGBScales(int threadnum);
  394. // transparency.c (transparency array functions - shared between vismatrix.c and sparse.c)
  395. extern void GetTransparency(const unsigned p1, const unsigned p2, vec3_t &trans, unsigned int &next_index);
  396. extern void AddTransparencyToRawArray(const unsigned p1, const unsigned p2, const vec3_t trans);
  397. extern void CreateFinalTransparencyArrays(const char *print_name);
  398. extern void FreeTransparencyArrays();
  399. #endif
  400. // lerp.c
  401. #ifdef ZHLT_TEXLIGHT
  402. extern void SampleTriangulation(const lerpTriangulation_t* const trian, vec3_t point, vec3_t result, int style); //LRC
  403. #else
  404. extern void SampleTriangulation(const lerpTriangulation_t* const trian, vec3_t point, vec3_t result);
  405. #endif
  406. extern void DestroyTriangulation(lerpTriangulation_t* trian);
  407. extern lerpTriangulation_t* CreateTriangulation(unsigned int facenum);
  408. extern void FreeTriangulation(lerpTriangulation_t* trian);
  409. // mathutil.c
  410. #ifdef HLRAD_HULLU
  411. extern int TestSegmentAgainstOpaqueList(const vec_t* p1, const vec_t* p2, vec3_t &scaleout);
  412. #else
  413. extern int TestSegmentAgainstOpaqueList(const vec_t* p1, const vec_t* p2);
  414. #endif
  415. #ifdef HLRAD_FASTMATH
  416. extern void PlaneFromPoints(const vec_t* const p1, const vec_t* const p2, const vec_t* const p3, dplane_t* plane);
  417. extern bool LineSegmentIntersectsPlane(const dplane_t& plane, const vec_t* const p1, const vec_t* const p2, vec3_t& point);
  418. extern bool PointInWinding(const Winding* const W, const dplane_t* const plane, const vec_t* const point);
  419. extern bool PointInWall(const lerpWall_t* const wall, const vec_t* const point);
  420. extern bool PointInTri(const vec_t* const point, const dplane_t* const plane, const vec_t* const p1, const vec_t* const p2, const vec_t* const p3);
  421. extern void SnapToPlane(const dplane_t* const plane, vec_t* const point, const vec_t offset);
  422. #else
  423. extern bool intersect_line_plane(const dplane_t* const plane, const vec_t* const p1, const vec_t* const p2, vec3_t point);
  424. extern bool intersect_linesegment_plane(const dplane_t* const plane, const vec_t* const p1, const vec_t* const p2,vec3_t point);
  425. extern void plane_from_points(const vec3_t p1, const vec3_t p2, const vec3_t p3, dplane_t* plane);
  426. extern bool point_in_winding(const Winding& w, const dplane_t& plane, const vec_t* point);
  427. extern bool point_in_wall(const lerpWall_t* wall, vec3_t point);
  428. extern bool point_in_tri(const vec3_t point, const dplane_t* const plane, const vec3_t p1, const vec3_t p2, const vec3_t p3);
  429. extern void ProjectionPoint(const vec_t* const v, const vec_t* const p, vec_t* rval);
  430. extern void SnapToPlane(const dplane_t* const plane, vec_t* const point, vec_t offset);
  431. #endif
  432. #endif //HLRAD_H__