qrad.h 17 KB

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