qrad.cpp 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013
  1. /*
  2. R A D I O S I T Y -aka- R A D
  3. Code based on original code from Valve Software,
  4. Modified by Sean "Zoner" Cavanaugh ([email protected]) with permission.
  5. Modified by Tony "Merl" Moore ([email protected]) [AJM]
  6. */
  7. #ifdef SYSTEM_WIN32
  8. #define WIN32_LEAN_AND_MEAN
  9. #include <windows.h>
  10. #pragma warning(disable:4996)
  11. #endif
  12. #include <vector>
  13. #include <string>
  14. #include "qrad.h"
  15. #include "cmdlib.h"
  16. /*
  17. * NOTES
  18. * -----
  19. * every surface must be divided into at least two g_patches each axis
  20. */
  21. typedef enum
  22. {
  23. eMethodVismatrix,
  24. eMethodSparseVismatrix,
  25. eMethodNoVismatrix
  26. }
  27. eVisMethods;
  28. eVisMethods g_method = eMethodVismatrix;
  29. vec_t g_fade = DEFAULT_FADE;
  30. int g_falloff = DEFAULT_FALLOFF;
  31. patch_t* g_face_patches[MAX_MAP_FACES];
  32. entity_t* g_face_entity[MAX_MAP_FACES];
  33. eModelLightmodes g_face_lightmode[MAX_MAP_FACES];
  34. patch_t g_patches[MAX_PATCHES];
  35. unsigned g_num_patches;
  36. bool g_warned_direct = false;
  37. #ifdef ZHLT_TEXLIGHT
  38. static vec3_t emitlight[MAX_PATCHES][MAXLIGHTMAPS]; //LRC
  39. static vec3_t addlight[MAX_PATCHES][MAXLIGHTMAPS]; //LRC
  40. #else
  41. static vec3_t emitlight[MAX_PATCHES];
  42. static vec3_t addlight[MAX_PATCHES];
  43. #endif
  44. vec3_t g_face_offset[MAX_MAP_FACES]; // for rotating bmodels
  45. vec_t g_direct_scale = DEFAULT_DLIGHT_SCALE;
  46. unsigned g_numbounce = DEFAULT_BOUNCE; // 3; /* Originally this was 8 */
  47. bool g_bounce_dynamic = DEFAULT_BOUNCE_DYNAMIC; //false
  48. static bool g_dumppatches = DEFAULT_DUMPPATCHES;
  49. vec3_t g_ambient = { DEFAULT_AMBIENT_RED, DEFAULT_AMBIENT_GREEN, DEFAULT_AMBIENT_BLUE };
  50. float g_maxlight = DEFAULT_MAXLIGHT; // 196 /* Originally this was 196 */
  51. float g_lightscale = DEFAULT_LIGHTSCALE;
  52. float g_dlight_threshold = DEFAULT_DLIGHT_THRESHOLD; // was DIRECT_LIGHT constant
  53. char g_source[_MAX_PATH] = "";
  54. char g_vismatfile[_MAX_PATH] = "";
  55. bool g_incremental = DEFAULT_INCREMENTAL;
  56. #ifndef HLRAD_WHOME
  57. float g_qgamma = DEFAULT_GAMMA;
  58. #endif
  59. float g_indirect_sun = DEFAULT_INDIRECT_SUN;
  60. bool g_extra = DEFAULT_EXTRA;
  61. bool g_texscale = DEFAULT_TEXSCALE;
  62. float g_smoothing_threshold;
  63. float g_smoothing_value = DEFAULT_SMOOTHING_VALUE;
  64. bool g_circus = DEFAULT_CIRCUS;
  65. bool g_allow_opaques = DEFAULT_ALLOW_OPAQUES;
  66. // --------------------------------------------------------------------------
  67. // Changes by Adam Foster - [email protected]
  68. #ifdef HLRAD_WHOME
  69. vec3_t g_colour_qgamma = { DEFAULT_COLOUR_GAMMA_RED, DEFAULT_COLOUR_GAMMA_GREEN, DEFAULT_COLOUR_GAMMA_BLUE };
  70. vec3_t g_colour_lightscale = { DEFAULT_COLOUR_LIGHTSCALE_RED, DEFAULT_COLOUR_LIGHTSCALE_GREEN, DEFAULT_COLOUR_LIGHTSCALE_BLUE };
  71. vec3_t g_colour_jitter_hack = { DEFAULT_COLOUR_JITTER_HACK_RED, DEFAULT_COLOUR_JITTER_HACK_GREEN, DEFAULT_COLOUR_JITTER_HACK_BLUE };
  72. vec3_t g_jitter_hack = { DEFAULT_JITTER_HACK_RED, DEFAULT_JITTER_HACK_GREEN, DEFAULT_JITTER_HACK_BLUE };
  73. bool g_diffuse_hack = DEFAULT_DIFFUSE_HACK;
  74. bool g_spotlight_hack = DEFAULT_SPOTLIGHT_HACK;
  75. vec3_t g_softlight_hack = { DEFAULT_SOFTLIGHT_HACK_RED, DEFAULT_SOFTLIGHT_HACK_GREEN, DEFAULT_SOFTLIGHT_HACK_BLUE };
  76. float g_softlight_hack_distance = DEFAULT_SOFTLIGHT_HACK_DISTANCE;
  77. #endif
  78. // --------------------------------------------------------------------------
  79. #ifdef HLRAD_HULLU
  80. bool g_customshadow_with_bouncelight = DEFAULT_CUSTOMSHADOW_WITH_BOUNCELIGHT;
  81. bool g_rgb_transfers = DEFAULT_RGB_TRANSFERS;
  82. #endif
  83. // Cosine of smoothing angle(in radians)
  84. float g_coring = DEFAULT_CORING; // Light threshold to force to blackness(minimizes lightmaps)
  85. bool g_chart = DEFAULT_CHART;
  86. bool g_estimate = DEFAULT_ESTIMATE;
  87. bool g_info = DEFAULT_INFO;
  88. #ifdef ZHLT_PROGRESSFILE // AJM
  89. char* g_progressfile = DEFAULT_PROGRESSFILE; // "-progressfile path"
  90. #endif
  91. // Patch creation and subdivision criteria
  92. bool g_subdivide = DEFAULT_SUBDIVIDE;
  93. vec_t g_chop = DEFAULT_CHOP;
  94. vec_t g_texchop = DEFAULT_TEXCHOP;
  95. // Opaque faces
  96. opaqueList_t* g_opaque_face_list = NULL;
  97. unsigned g_opaque_face_count = 0;
  98. unsigned g_max_opaque_face_count = 0; // Current array maximum (used for reallocs)
  99. // Misc
  100. int leafparents[MAX_MAP_LEAFS];
  101. int nodeparents[MAX_MAP_NODES];
  102. #ifdef ZHLT_INFO_COMPILE_PARAMETERS
  103. // =====================================================================================
  104. // GetParamsFromEnt
  105. // this function is called from parseentity when it encounters the
  106. // info_compile_parameters entity. each tool should have its own version of this
  107. // to handle its own specific settings.
  108. // =====================================================================================
  109. void GetParamsFromEnt(entity_t* mapent)
  110. {
  111. int iTmp;
  112. float flTmp;
  113. char szTmp[256];
  114. const char* pszTmp;
  115. Log("\nCompile Settings detected from info_compile_parameters entity\n");
  116. // lightdata(string) : "Lighting Data Memory" : "8192"
  117. iTmp = IntForKey(mapent, "lightdata") * 1024;
  118. if (iTmp > g_max_map_miptex)
  119. {
  120. g_max_map_lightdata = iTmp;
  121. }
  122. sprintf_s(szTmp, "%i", g_max_map_lightdata);
  123. Log("%30s [ %-9s ]\n", "Lighting Data Memory", szTmp);
  124. // verbose(choices) : "Verbose compile messages" : 0 = [ 0 : "Off" 1 : "On" ]
  125. iTmp = IntForKey(mapent, "verbose");
  126. if (iTmp == 1)
  127. {
  128. g_verbose = true;
  129. }
  130. else if (iTmp == 0)
  131. {
  132. g_verbose = false;
  133. }
  134. Log("%30s [ %-9s ]\n", "Compile Option", "setting");
  135. Log("%30s [ %-9s ]\n", "Verbose Compile Messages", g_verbose ? "on" : "off");
  136. // estimate(choices) :"Estimate Compile Times?" : 0 = [ 0: "Yes" 1: "No" ]
  137. if (IntForKey(mapent, "estimate"))
  138. {
  139. g_estimate = true;
  140. }
  141. else
  142. {
  143. g_estimate = false;
  144. }
  145. Log("%30s [ %-9s ]\n", "Estimate Compile Times", g_estimate ? "on" : "off");
  146. // priority(choices) : "Priority Level" : 0 = [ 0 : "Normal" 1 : "High" -1 : "Low" ]
  147. if (!strcmp(ValueForKey(mapent, "priority"), "1"))
  148. {
  149. g_threadpriority = eThreadPriorityHigh;
  150. Log("%30s [ %-9s ]\n", "Thread Priority", "high");
  151. }
  152. else if (!strcmp(ValueForKey(mapent, "priority"), "-1"))
  153. {
  154. g_threadpriority = eThreadPriorityLow;
  155. Log("%30s [ %-9s ]\n", "Thread Priority", "low");
  156. }
  157. // bounce(integer) : "Number of radiosity bounces" : 0
  158. iTmp = IntForKey(mapent, "bounce");
  159. if (iTmp)
  160. {
  161. g_numbounce = abs(iTmp);
  162. Log("%30s [ %-9s ]\n", "Number of radiosity bounces", ValueForKey(mapent, "bounce"));
  163. }
  164. if(IntForKey(mapent,"nodynbounce"))
  165. {
  166. g_bounce_dynamic = false;
  167. }
  168. else
  169. {
  170. g_bounce_dynamic = true;
  171. }
  172. Log("%30s [ %-9s ]\n", "Bounce dynamic lights", g_bounce_dynamic ? "on" : "off");
  173. #ifdef HLRAD_HULLU
  174. iTmp = IntForKey(mapent, "customshadowwithbounce");
  175. if (iTmp)
  176. {
  177. g_customshadow_with_bouncelight = true;
  178. Log("%30s [ %-9s ]\n", "Custom Shadow with Bounce Light", ValueForKey(mapent, "customshadowwithbounce"));
  179. }
  180. iTmp = IntForKey(mapent, "rgbtransfers");
  181. if (iTmp)
  182. {
  183. g_rgb_transfers = true;
  184. Log("%30s [ %-9s ]\n", "RGB Transfers", ValueForKey(mapent, "rgbtransfers"));
  185. }
  186. #endif
  187. // ambient(string) : "Ambient world light (0.0 to 1.0, R G B)" : "0 0 0"
  188. //vec3_t g_ambient = { DEFAULT_AMBIENT_RED, DEFAULT_AMBIENT_GREEN, DEFAULT_AMBIENT_BLUE };
  189. pszTmp = ValueForKey(mapent, "ambient");
  190. if (pszTmp)
  191. {
  192. float red = 0, green = 0, blue = 0;
  193. if (sscanf_s(pszTmp, "%f %f %f", &red, &green, &blue))
  194. {
  195. if (red < 0 || red > 1 || green < 0 || green > 1 || blue < 0 || blue > 1)
  196. {
  197. Error("info_compile_parameters: Ambient World Light (ambient) all 3 values must be within the range of 0.0 to 1.0\n"
  198. "Parsed values:\n"
  199. " red [ %1.3f ] %s\n"
  200. " green [ %1.3f ] %s\n"
  201. " blue [ %1.3f ] %s\n"
  202. , red, (red < 0 || red > 1) ? "OUT OF RANGE" : ""
  203. , green, (green < 0 || green > 1) ? "OUT OF RANGE" : ""
  204. , blue, (blue < 0 || blue > 1) ? "OUT OF RANGE" : "" );
  205. }
  206. if (red == 0 && green == 0 && blue == 0)
  207. {} // dont bother setting null values
  208. else
  209. {
  210. g_ambient[0] = red * 128;
  211. g_ambient[1] = green * 128;
  212. g_ambient[2] = blue * 128;
  213. Log("%30s [ %1.3f %1.3f %1.3f ]\n", "Ambient world light (R G B)", red, green, blue);
  214. }
  215. }
  216. else
  217. {
  218. Error("info_compile_parameters: Ambient World Light (ambient) has unrecognised value\n"
  219. "This keyvalue accepts 3 numeric values from 0.000 to 1.000, use \"0 0 0\" if in doubt");
  220. }
  221. }
  222. // smooth(integer) : "Smoothing threshold (in degrees)" : 0
  223. flTmp = FloatForKey(mapent, "smooth");
  224. if (flTmp)
  225. {
  226. g_smoothing_threshold = flTmp;
  227. Log("%30s [ %-9s ]\n", "Smoothing threshold", ValueForKey(mapent, "smooth"));
  228. }
  229. // dscale(integer) : "Direct Lighting Scale" : 1
  230. flTmp = FloatForKey(mapent, "dscale");
  231. if (flTmp)
  232. {
  233. g_direct_scale = flTmp;
  234. Log("%30s [ %-9s ]\n", "Direct Lighting Scale", ValueForKey(mapent, "dscale"));
  235. }
  236. // chop(integer) : "Chop Size" : 64
  237. iTmp = IntForKey(mapent, "chop");
  238. if (iTmp)
  239. {
  240. g_chop = iTmp;
  241. Log("%30s [ %-9s ]\n", "Chop Size", ValueForKey(mapent, "chop"));
  242. }
  243. // texchop(integer) : "Texture Light Chop Size" : 32
  244. flTmp = FloatForKey(mapent, "texchop");
  245. if (flTmp)
  246. {
  247. g_texchop = flTmp;
  248. Log("%30s [ %-9s ]\n", "Texture Light Chop Size", ValueForKey(mapent, "texchop"));
  249. }
  250. /*
  251. hlrad(choices) : "HLRAD" : 0 =
  252. [
  253. 0 : "Off"
  254. 1 : "Normal"
  255. 2 : "Extra"
  256. ]
  257. */
  258. iTmp = IntForKey(mapent, "hlrad");
  259. if (iTmp == 0)
  260. {
  261. Fatal(assume_TOOL_CANCEL,
  262. "%s flag was not checked in info_compile_parameters entity, execution of %s cancelled", g_Program, g_Program);
  263. CheckFatal();
  264. }
  265. else if (iTmp == 1)
  266. {
  267. g_extra = false;
  268. }
  269. else if (iTmp == 2)
  270. {
  271. g_extra = true;
  272. }
  273. Log("%30s [ %-9s ]\n", "Extra RAD", g_extra ? "on" : "off");
  274. /*
  275. sparse(choices) : "Vismatrix Method" : 2 =
  276. [
  277. 0 : "No Vismatrix"
  278. 1 : "Sparse Vismatrix"
  279. 2 : "Normal"
  280. ]
  281. */
  282. iTmp = IntForKey(mapent, "sparse");
  283. if (iTmp == 1)
  284. {
  285. g_method = eMethodSparseVismatrix;
  286. }
  287. else if (iTmp == 0)
  288. {
  289. g_method = eMethodNoVismatrix;
  290. }
  291. else if (iTmp == 2)
  292. {
  293. g_method = eMethodVismatrix;
  294. }
  295. Log("%30s [ %-9s ]\n", "Sparse Vismatrix", g_method == eMethodSparseVismatrix ? "on" : "off");
  296. Log("%30s [ %-9s ]\n", "NoVismatrix", g_method == eMethodNoVismatrix ? "on" : "off");
  297. /*
  298. circus(choices) : "Circus RAD lighting" : 0 =
  299. [
  300. 0 : "Off"
  301. 1 : "On"
  302. ]
  303. */
  304. iTmp = IntForKey(mapent, "circus");
  305. if (iTmp == 0)
  306. {
  307. g_circus = false;
  308. }
  309. else if (iTmp == 1)
  310. {
  311. g_circus = true;
  312. }
  313. Log("%30s [ %-9s ]\n", "Circus Lighting Mode", g_circus ? "on" : "off");
  314. ////////////////////
  315. Log("\n");
  316. }
  317. #endif
  318. // =====================================================================================
  319. // Extract File stuff (ExtractFile | ExtractFilePath | ExtractFileBase)
  320. //
  321. // With VS 2005 - and the 64 bit build, i had to pull 3 classes over from
  322. // cmdlib.cpp even with the proper includes to get rid of the lnk2001 error
  323. //
  324. // amckern - [email protected]
  325. // =====================================================================================
  326. #define PATHSEPARATOR(c) ((c) == '\\' || (c) == '/')
  327. void ExtractFileBase(const char* const path, char* dest)
  328. {
  329. hlassert (path != dest);
  330. const char* src;
  331. src = path + strlen(path) - 1;
  332. //
  333. // back up until a \ or the start
  334. //
  335. while (src != path && !PATHSEPARATOR(*(src - 1)))
  336. src--;
  337. while (*src && *src != '.')
  338. {
  339. *dest++ = *src++;
  340. }
  341. *dest = 0;
  342. }
  343. void ExtractFilePath(const char* const path, char* dest)
  344. {
  345. hlassert (path != dest);
  346. const char* src;
  347. src = path + strlen(path) - 1;
  348. //
  349. // back up until a \ or the start
  350. //
  351. while (src != path && !PATHSEPARATOR(*(src - 1)))
  352. src--;
  353. memcpy(dest, path, src - path);
  354. dest[src - path] = 0;
  355. }
  356. void ExtractFile(const char* const path, char* dest)
  357. {
  358. hlassert (path != dest);
  359. const char* src;
  360. src = path + strlen(path) - 1;
  361. while (src != path && !PATHSEPARATOR(*(src - 1)))
  362. src--;
  363. while (*src)
  364. {
  365. *dest++ = *src++;
  366. }
  367. *dest = 0;
  368. }
  369. // =====================================================================================
  370. // MakeParents
  371. // blah
  372. // =====================================================================================
  373. static void MakeParents(const int nodenum, const int parent)
  374. {
  375. int i;
  376. int j;
  377. dnode_t* node;
  378. nodeparents[nodenum] = parent;
  379. node = g_dnodes + nodenum;
  380. for (i = 0; i < 2; i++)
  381. {
  382. j = node->children[i];
  383. if (j < 0)
  384. {
  385. leafparents[-j - 1] = nodenum;
  386. }
  387. else
  388. {
  389. MakeParents(j, nodenum);
  390. }
  391. }
  392. }
  393. // =====================================================================================
  394. //
  395. // TEXTURE LIGHT VALUES
  396. //
  397. // =====================================================================================
  398. // misc
  399. typedef struct
  400. {
  401. std::string name;
  402. vec3_t value;
  403. const char* filename;
  404. }
  405. texlight_t;
  406. static std::vector< texlight_t > s_texlights;
  407. typedef std::vector< texlight_t >::iterator texlight_i;
  408. // =====================================================================================
  409. // ReadLightFile
  410. // =====================================================================================
  411. static void ReadLightFile(const char* const filename)
  412. {
  413. FILE* f;
  414. char scan[MAXTOKEN];
  415. short argCnt;
  416. unsigned int file_texlights = 0;
  417. f = fopen(filename, "r");
  418. if (!f)
  419. {
  420. Warning("Could not open texlight file %s", filename);
  421. return;
  422. }
  423. else
  424. {
  425. Log("[Reading texlights from '%s']\n", filename);
  426. }
  427. while (fgets(scan, sizeof(scan), f))
  428. {
  429. char* comment;
  430. char szTexlight[_MAX_PATH];
  431. vec_t r, g, b, i = 1;
  432. comment = strstr(scan, "//");
  433. if (comment)
  434. {
  435. // Newline and Null terminate the string early if there is a c++ style single line comment
  436. comment[0] = '\n';
  437. comment[1] = 0;
  438. }
  439. argCnt = sscanf/*_s*/(scan, "%s %f %f %f %f", szTexlight, &r, &g, &b, &i);
  440. if (argCnt == 2)
  441. {
  442. // With 1+1 args, the R,G,B values are all equal to the first value
  443. g = b = r;
  444. }
  445. else if (argCnt == 5)
  446. {
  447. // With 1+4 args, the R,G,B values are "scaled" by the fourth numeric value i;
  448. r *= i / 255.0;
  449. g *= i / 255.0;
  450. b *= i / 255.0;
  451. }
  452. else if (argCnt != 4)
  453. {
  454. if (strlen(scan) > 4)
  455. {
  456. Warning("ignoring bad texlight '%s' in %s", scan, filename);
  457. }
  458. continue;
  459. }
  460. texlight_i it;
  461. for (it = s_texlights.begin(); it != s_texlights.end(); it++)
  462. {
  463. if (strcmp(it->name.c_str(), szTexlight) == 0)
  464. {
  465. if (strcmp(it->filename, filename) == 0)
  466. {
  467. Warning("Duplication of texlight '%s' in file '%s'!", it->name.c_str(), it->filename);
  468. }
  469. else if (it->value[0] != r || it->value[1] != g || it->value[2] != b)
  470. {
  471. Warning("Overriding '%s' from '%s' with '%s'!", it->name.c_str(), it->filename, filename);
  472. }
  473. else
  474. {
  475. Warning("Redundant '%s' def in '%s' AND '%s'!", it->name.c_str(), it->filename, filename);
  476. }
  477. s_texlights.erase(it);
  478. break;
  479. }
  480. }
  481. texlight_t texlight;
  482. texlight.name = szTexlight;
  483. texlight.value[0] = r;
  484. texlight.value[1] = g;
  485. texlight.value[2] = b;
  486. texlight.filename = filename;
  487. file_texlights++;
  488. s_texlights.push_back(texlight);
  489. }
  490. Log("[%u texlights parsed from '%s']\n\n", file_texlights, filename);
  491. }
  492. // =====================================================================================
  493. // LightForTexture
  494. // =====================================================================================
  495. static void LightForTexture(const char* const name, vec3_t result)
  496. {
  497. texlight_i it;
  498. for (it = s_texlights.begin(); it != s_texlights.end(); it++)
  499. {
  500. if (!strcasecmp(name, it->name.c_str()))
  501. {
  502. VectorCopy(it->value, result);
  503. return;
  504. }
  505. }
  506. VectorClear(result);
  507. }
  508. // =====================================================================================
  509. //
  510. // MAKE FACES
  511. //
  512. // =====================================================================================
  513. // =====================================================================================
  514. // BaseLightForFace
  515. // =====================================================================================
  516. static void BaseLightForFace(const dface_t* const f, vec3_t light)
  517. {
  518. texinfo_t* tx;
  519. miptex_t* mt;
  520. int ofs;
  521. //
  522. // check for light emited by texture
  523. //
  524. tx = &g_texinfo[f->texinfo];
  525. ofs = ((dmiptexlump_t*)g_dtexdata)->dataofs[tx->miptex];
  526. mt = (miptex_t*)((byte*) g_dtexdata + ofs);
  527. LightForTexture(mt->name, light);
  528. }
  529. // =====================================================================================
  530. // IsSpecial
  531. // =====================================================================================
  532. static bool IsSpecial(const dface_t* const f)
  533. {
  534. return g_texinfo[f->texinfo].flags & TEX_SPECIAL;
  535. }
  536. // =====================================================================================
  537. // PlacePatchInside
  538. // =====================================================================================
  539. static bool PlacePatchInside(patch_t* patch)
  540. {
  541. const dplane_t* plane;
  542. const vec_t* face_offset = g_face_offset[patch->faceNumber];
  543. plane = getPlaneFromFaceNumber(patch->faceNumber);
  544. if (!HuntForWorld(patch->origin, face_offset, plane, 11, 0.1, 0.01) &&
  545. !HuntForWorld(patch->origin, face_offset, plane, 11, 0.1, 0.1) &&
  546. !HuntForWorld(patch->origin, face_offset, plane, 11, 0.1, 0.5) &&
  547. !HuntForWorld(patch->origin, face_offset, plane, 11, 0.1, -0.01) &&
  548. !HuntForWorld(patch->origin, face_offset, plane, 11, 0.1, -0.1))
  549. {
  550. // Try offsetting it by the plane normal (1 unit away) and try again
  551. VectorAdd(plane->normal, patch->origin, patch->origin); // Original offset-into-world method
  552. if (PointInLeaf(patch->origin) == g_dleafs)
  553. {
  554. if (!HuntForWorld(patch->origin, face_offset, plane, 11, 0.1, 0.01) &&
  555. !HuntForWorld(patch->origin, face_offset, plane, 11, 0.1, 0.1) &&
  556. !HuntForWorld(patch->origin, face_offset, plane, 11, 0.1, 0.5) &&
  557. !HuntForWorld(patch->origin, face_offset, plane, 11, 0.1, -0.01) &&
  558. !HuntForWorld(patch->origin, face_offset, plane, 11, 0.1, -0.1))
  559. {
  560. patch->flags = (ePatchFlags)(patch->flags | ePatchFlagOutside);
  561. Developer(DEVELOPER_LEVEL_MESSAGE, "Patch @ (%4.3f %4.3f %4.3f) outside world\n",
  562. patch->origin[0], patch->origin[1], patch->origin[2]);
  563. return false;
  564. }
  565. }
  566. }
  567. return true;
  568. }
  569. // =====================================================================================
  570. //
  571. // SUBDIVIDE PATCHES
  572. //
  573. // =====================================================================================
  574. // misc
  575. #define MAX_SUBDIVIDE 16384
  576. static Winding* windingArray[MAX_SUBDIVIDE];
  577. static unsigned g_numwindings = 0;
  578. // =====================================================================================
  579. // AddWindingToArray
  580. // =====================================================================================
  581. static void AddWindingToArray(Winding* winding)
  582. {
  583. unsigned x;
  584. Winding** wA = windingArray;
  585. for (x = 0; x < g_numwindings; x++, wA++)
  586. {
  587. if (*wA == winding)
  588. {
  589. return;
  590. }
  591. }
  592. windingArray[g_numwindings++] = winding;
  593. }
  594. static void CreateStrips_r(Winding* winding, const vec3_t plane_normal, const vec_t plane_dist, vec_t step)
  595. {
  596. Winding* A;
  597. Winding* B;
  598. vec_t areaA;
  599. vec_t areaB;
  600. winding->Clip(plane_normal, plane_dist + step, &A, &B);
  601. if (A && B)
  602. {
  603. areaA = A->getArea();
  604. areaB = B->getArea();
  605. if ((areaA > 1.0) && (areaB > 1.0))
  606. {
  607. delete winding;
  608. CreateStrips_r(A, plane_normal, plane_dist + step, step);
  609. CreateStrips_r(B, plane_normal, plane_dist + step, step);
  610. return;
  611. }
  612. }
  613. else
  614. { // Try the other direction
  615. if (A)
  616. {
  617. delete A;
  618. }
  619. if (B)
  620. {
  621. delete B;
  622. }
  623. winding->Clip(plane_normal, plane_dist - step, &A, &B);
  624. if (A && B)
  625. {
  626. areaA = A->getArea();
  627. areaB = B->getArea();
  628. if ((areaA > 1.0) && (areaB > 1.0))
  629. {
  630. delete winding;
  631. CreateStrips_r(A, plane_normal, plane_dist - step, step);
  632. CreateStrips_r(B, plane_normal, plane_dist - step, step);
  633. return;
  634. }
  635. }
  636. }
  637. // Last recursion, save it into the list
  638. if (A)
  639. {
  640. delete A;
  641. }
  642. if (B)
  643. {
  644. delete B;
  645. }
  646. AddWindingToArray(winding);
  647. hlassume(g_numwindings < MAX_SUBDIVIDE, assume_GENERIC);
  648. }
  649. // =====================================================================================
  650. // CreateStrips
  651. // =====================================================================================
  652. static bool CreateStrips(Winding* winding, const dplane_t* plane, vec_t step)
  653. {
  654. Winding* A;
  655. Winding* B;
  656. vec_t areaA;
  657. vec_t areaB;
  658. winding->Clip(plane->normal, plane->dist, &A, &B);
  659. if (A && B)
  660. {
  661. areaA = A->getArea();
  662. areaB = B->getArea();
  663. if ((areaA > 1.0) && (areaB > 1.0))
  664. {
  665. CreateStrips_r(A, (vec_t*)plane->normal, plane->dist, step);
  666. CreateStrips_r(B, (vec_t*)plane->normal, plane->dist, step);
  667. return true;
  668. }
  669. }
  670. if (A)
  671. {
  672. delete A;
  673. }
  674. if (B)
  675. {
  676. delete B;
  677. }
  678. AddWindingToArray(winding);
  679. hlassume(g_numwindings < MAX_SUBDIVIDE, assume_GENERIC);
  680. return false;
  681. }
  682. // =====================================================================================
  683. // cutWindingWithGrid
  684. // Caller must free this returned value at some point
  685. // =====================================================================================
  686. static void cutWindingWithGrid(patch_t* patch, const dplane_t* const plA, const dplane_t* const plB)
  687. {
  688. Winding** winding;
  689. unsigned int count;
  690. unsigned int x;
  691. g_numwindings = 0;
  692. if (CreateStrips(patch->winding, plA, patch->chop))
  693. {
  694. delete patch->winding;
  695. patch->winding = NULL; // Invalidated by CreateStrips routine
  696. }
  697. count = g_numwindings;
  698. for (x = 0, winding = windingArray; x < count; x++, winding++)
  699. {
  700. if (CreateStrips(*winding, plB, patch->chop))
  701. {
  702. delete *winding;
  703. *winding = NULL;
  704. }
  705. }
  706. }
  707. // =====================================================================================
  708. // getGridPlanes
  709. // From patch, determine perpindicular grid planes to subdivide with (returned in planeA and planeB)
  710. // assume S and T is perpindicular (they SHOULD be in worldcraft 3.3 but aren't always . . .)
  711. // =====================================================================================
  712. static void getGridPlanes(const patch_t* const p, dplane_t* const pl)
  713. {
  714. const patch_t* patch = p;
  715. dplane_t* planes = pl;
  716. const dface_t* f = g_dfaces + patch->faceNumber;
  717. texinfo_t* tx = &g_texinfo[f->texinfo];
  718. dplane_t* plane = planes;
  719. const dplane_t* faceplane = getPlaneFromFaceNumber(patch->faceNumber);
  720. int x;
  721. for (x = 0; x < 2; x++, plane++)
  722. {
  723. vec3_t a, b, c;
  724. vec3_t delta1, delta2;
  725. VectorCopy(patch->origin, a);
  726. VectorAdd(patch->origin, faceplane->normal, b);
  727. VectorAdd(patch->origin, tx->vecs[x], c);
  728. VectorSubtract(b, a, delta1);
  729. VectorSubtract(c, a, delta2);
  730. CrossProduct(delta1, delta2, plane->normal);
  731. VectorNormalize(plane->normal);
  732. plane->dist = DotProduct(plane->normal, patch->origin);
  733. }
  734. }
  735. // =====================================================================================
  736. // SubdividePatch
  737. // =====================================================================================
  738. static void SubdividePatch(patch_t* patch)
  739. {
  740. dplane_t planes[2];
  741. dplane_t* plA = &planes[0];
  742. dplane_t* plB = &planes[1];
  743. Winding** winding;
  744. unsigned x;
  745. patch_t* new_patch;
  746. memset(windingArray, 0, sizeof(windingArray));
  747. g_numwindings = 0;
  748. getGridPlanes(patch, planes);
  749. cutWindingWithGrid(patch, plA, plB);
  750. x = 0;
  751. patch->next = NULL;
  752. winding = windingArray;
  753. while (*winding == NULL)
  754. {
  755. winding++;
  756. x++;
  757. }
  758. patch->winding = *winding;
  759. winding++;
  760. x++;
  761. patch->area = patch->winding->getArea();
  762. patch->winding->getCenter(patch->origin);
  763. PlacePatchInside(patch);
  764. new_patch = g_patches + g_num_patches;
  765. for (; x < g_numwindings; x++, winding++)
  766. {
  767. if (*winding)
  768. {
  769. memcpy(new_patch, patch, sizeof(patch_t));
  770. new_patch->winding = *winding;
  771. new_patch->area = new_patch->winding->getArea();
  772. new_patch->winding->getCenter(new_patch->origin);
  773. PlacePatchInside(new_patch);
  774. new_patch++;
  775. g_num_patches++;
  776. hlassume(g_num_patches < MAX_PATCHES, assume_MAX_PATCHES);
  777. }
  778. }
  779. // ATTENTION: We let SortPatches relink all the ->next correctly! instead of doing it here too which is somewhat complicated
  780. }
  781. // =====================================================================================
  782. // MakePatchForFace
  783. static float totalarea = 0;
  784. // =====================================================================================
  785. static vec_t getScale(const patch_t* const patch)
  786. {
  787. dface_t* f = g_dfaces + patch->faceNumber;
  788. texinfo_t* tx = &g_texinfo[f->texinfo];
  789. if (g_texscale)
  790. {
  791. vec_t scale[2];
  792. scale[0] = 0.0;
  793. scale[1] = 0.0;
  794. scale[0] += tx->vecs[0][0] * tx->vecs[0][0];
  795. scale[0] += tx->vecs[0][1] * tx->vecs[0][1];
  796. scale[0] += tx->vecs[0][2] * tx->vecs[0][2];
  797. scale[1] += tx->vecs[1][0] * tx->vecs[1][0];
  798. scale[1] += tx->vecs[1][1] * tx->vecs[1][1];
  799. scale[1] += tx->vecs[1][2] * tx->vecs[1][2];
  800. scale[0] = sqrt(scale[0]);
  801. scale[1] = sqrt(scale[1]);
  802. return 2.0 / ((scale[0] + scale[1]));
  803. }
  804. else
  805. {
  806. return 1.0;
  807. }
  808. }
  809. // =====================================================================================
  810. // getChop
  811. // =====================================================================================
  812. static vec_t getChop(const patch_t* const patch)
  813. {
  814. vec_t rval;
  815. if (VectorCompare(patch->baselight, vec3_origin))
  816. {
  817. rval = g_chop * getScale(patch);
  818. }
  819. else
  820. {
  821. rval = g_texchop * getScale(patch);
  822. if (g_extra)
  823. {
  824. rval *= 0.5;
  825. }
  826. }
  827. return rval;
  828. }
  829. // =====================================================================================
  830. // MakePatchForFace
  831. // =====================================================================================
  832. #ifdef ZHLT_TEXLIGHT
  833. static void MakePatchForFace(const int fn, Winding* w, int style) //LRC
  834. #else
  835. static void MakePatchForFace(const int fn, Winding* w)
  836. #endif
  837. {
  838. const dface_t* f = g_dfaces + fn;
  839. // No g_patches at all for the sky!
  840. if (!IsSpecial(f))
  841. {
  842. patch_t* patch;
  843. vec3_t light;
  844. vec3_t centroid = { 0, 0, 0 };
  845. int numpoints = w->m_NumPoints;
  846. if (numpoints < 3) // WTF! (Actually happens in real-world maps too)
  847. {
  848. Developer(DEVELOPER_LEVEL_WARNING, "Face %d only has %d points on winding\n", fn, numpoints);
  849. return;
  850. }
  851. if (numpoints > MAX_POINTS_ON_WINDING)
  852. {
  853. Error("numpoints %d > MAX_POINTS_ON_WINDING", numpoints);
  854. return;
  855. }
  856. patch = &g_patches[g_num_patches];
  857. hlassume(g_num_patches < MAX_PATCHES, assume_MAX_PATCHES);
  858. memset(patch, 0, sizeof(patch_t));
  859. patch->winding = w;
  860. patch->area = patch->winding->getArea();
  861. patch->winding->getCenter(patch->origin);
  862. patch->faceNumber = fn;
  863. totalarea += patch->area;
  864. PlacePatchInside(patch);
  865. BaseLightForFace(f, light);
  866. #ifdef ZHLT_TEXLIGHT
  867. //LRC VectorCopy(light, patch->totallight);
  868. #else
  869. VectorCopy(light, patch->totallight);
  870. #endif
  871. VectorCopy(light, patch->baselight);
  872. #ifdef ZHLT_TEXLIGHT
  873. //LRC
  874. int i;
  875. patch->totalstyle[0] = 0;
  876. for (i = 1; i < MAXLIGHTMAPS; i++)
  877. {
  878. patch->totalstyle[i] = 255;
  879. }
  880. if (style)
  881. {
  882. patch->emitstyle = patch->totalstyle[1] = style;
  883. }
  884. //LRC (ends)
  885. #endif
  886. patch->scale = getScale(patch);
  887. patch->chop = getChop(patch);
  888. g_face_patches[fn] = patch;
  889. g_num_patches++;
  890. // Per-face data
  891. {
  892. int j;
  893. // Centroid of face for nudging samples in direct lighting pass
  894. for (j = 0; j < f->numedges; j++)
  895. {
  896. int edge = g_dsurfedges[f->firstedge + j];
  897. if (edge > 0)
  898. {
  899. VectorAdd(g_dvertexes[g_dedges[edge].v[0]].point, centroid, centroid);
  900. VectorAdd(g_dvertexes[g_dedges[edge].v[1]].point, centroid, centroid);
  901. }
  902. else
  903. {
  904. VectorAdd(g_dvertexes[g_dedges[-edge].v[1]].point, centroid, centroid);
  905. VectorAdd(g_dvertexes[g_dedges[-edge].v[0]].point, centroid, centroid);
  906. }
  907. }
  908. // Fixup centroid for anything with an altered origin (rotating models/turrets mostly)
  909. // Save them for moving direct lighting points towards the face center
  910. VectorScale(centroid, 1.0 / (f->numedges * 2), centroid);
  911. VectorAdd(centroid, g_face_offset[fn], g_face_centroids[fn]);
  912. }
  913. {
  914. vec3_t mins;
  915. vec3_t maxs;
  916. patch->winding->getBounds(mins, maxs);
  917. if (g_subdivide)
  918. {
  919. vec_t amt;
  920. vec_t length;
  921. vec3_t delta;
  922. VectorSubtract(maxs, mins, delta);
  923. length = VectorLength(delta);
  924. if (VectorCompare(patch->baselight, vec3_origin))
  925. {
  926. amt = g_chop;
  927. }
  928. else
  929. {
  930. amt = g_texchop;
  931. }
  932. if (length > amt)
  933. {
  934. if (patch->area < 1.0)
  935. {
  936. Developer(DEVELOPER_LEVEL_WARNING,
  937. "Patch at (%4.3f %4.3f %4.3f) (face %d) tiny area (%4.3f) not subdividing \n",
  938. patch->origin[0], patch->origin[1], patch->origin[2], patch->faceNumber, patch->area);
  939. }
  940. else
  941. {
  942. SubdividePatch(patch);
  943. }
  944. }
  945. }
  946. }
  947. }
  948. }
  949. // =====================================================================================
  950. // AddFaceToOpaqueList
  951. // =====================================================================================
  952. #ifdef HLRAD_HULLU
  953. static void AddFaceToOpaqueList(const unsigned facenum, const Winding* const winding, const vec3_t &transparency_scale, const bool transparency)
  954. #else
  955. static void AddFaceToOpaqueList(const unsigned facenum, const Winding* const winding)
  956. #endif
  957. {
  958. if (g_opaque_face_count == g_max_opaque_face_count)
  959. {
  960. g_max_opaque_face_count += OPAQUE_ARRAY_GROWTH_SIZE;
  961. g_opaque_face_list = (opaqueList_t*)realloc(g_opaque_face_list, sizeof(opaqueList_t) * g_max_opaque_face_count);
  962. }
  963. {
  964. opaqueList_t* opaque = &g_opaque_face_list[g_opaque_face_count];
  965. g_opaque_face_count++;
  966. #ifdef HLRAD_HULLU
  967. VectorCopy(transparency_scale, opaque->transparency_scale);
  968. opaque->transparency = transparency;
  969. #endif
  970. opaque->facenum = facenum;
  971. getAdjustedPlaneFromFaceNumber(facenum, &opaque->plane);
  972. opaque->winding = new Winding(*winding);
  973. //build bounding box
  974. opaque->winding->getBounds(opaque->mins,opaque->maxs);
  975. }
  976. }
  977. // =====================================================================================
  978. // FreeOpaqueFaceList
  979. // =====================================================================================
  980. static void FreeOpaqueFaceList()
  981. {
  982. unsigned x;
  983. opaqueList_t* opaque = g_opaque_face_list;
  984. for (x = 0; x < g_opaque_face_count; x++, opaque++)
  985. {
  986. delete opaque->winding;
  987. opaque->winding = NULL;
  988. }
  989. free(g_opaque_face_list);
  990. g_opaque_face_list = NULL;
  991. g_opaque_face_count = 0;
  992. g_max_opaque_face_count = 0;
  993. }
  994. // =====================================================================================
  995. // MakePatches
  996. // =====================================================================================
  997. static void MakePatches()
  998. {
  999. int i;
  1000. int j;
  1001. unsigned int k;
  1002. dface_t* f;
  1003. int fn;
  1004. Winding* w;
  1005. dmodel_t* mod;
  1006. vec3_t origin;
  1007. entity_t* ent;
  1008. const char* s;
  1009. vec3_t light_origin;
  1010. vec3_t model_center;
  1011. bool b_light_origin;
  1012. bool b_model_center;
  1013. eModelLightmodes lightmode;
  1014. #ifdef ZHLT_TEXLIGHT
  1015. int style; //LRC
  1016. #endif
  1017. #ifdef HLRAD_HULLU
  1018. vec3_t d_transparency;
  1019. bool b_transparency;
  1020. #endif
  1021. Log("Faces: %i\n", g_numfaces);
  1022. Log("Create Patches: ");
  1023. for (i = 0; i < g_nummodels; i++)
  1024. {
  1025. b_light_origin = false;
  1026. b_model_center = false;
  1027. lightmode = eModelLightmodeNull;
  1028. #ifdef HLRAD_OPACITY // AJM
  1029. float l_opacity = 0.0f; // decimal percentage
  1030. #endif
  1031. mod = g_dmodels + i;
  1032. ent = EntityForModel(i);
  1033. VectorCopy(vec3_origin, origin);
  1034. if (*(s = ValueForKey(ent, "zhlt_lightflags")))
  1035. {
  1036. lightmode = (eModelLightmodes)atoi(s);
  1037. }
  1038. // models with origin brushes need to be offset into their in-use position
  1039. if (*(s = ValueForKey(ent, "origin")))
  1040. {
  1041. double v1, v2, v3;
  1042. if (sscanf_s(s, "%lf %lf %lf", &v1, &v2, &v3) == 3)
  1043. {
  1044. origin[0] = v1;
  1045. origin[1] = v2;
  1046. origin[2] = v3;
  1047. }
  1048. }
  1049. // Allow models to be lit in an alternate location (pt1)
  1050. if (*(s = ValueForKey(ent, "light_origin")))
  1051. {
  1052. entity_t* e = FindTargetEntity(s);
  1053. if (e)
  1054. {
  1055. if (*(s = ValueForKey(e, "origin")))
  1056. {
  1057. double v1, v2, v3;
  1058. if (sscanf_s(s, "%lf %lf %lf", &v1, &v2, &v3) == 3)
  1059. {
  1060. light_origin[0] = v1;
  1061. light_origin[1] = v2;
  1062. light_origin[2] = v3;
  1063. b_light_origin = true;
  1064. }
  1065. }
  1066. }
  1067. }
  1068. // Allow models to be lit in an alternate location (pt2)
  1069. if (*(s = ValueForKey(ent, "model_center")))
  1070. {
  1071. double v1, v2, v3;
  1072. if (sscanf_s(s, "%lf %lf %lf", &v1, &v2, &v3) == 3)
  1073. {
  1074. model_center[0] = v1;
  1075. model_center[1] = v2;
  1076. model_center[2] = v3;
  1077. b_model_center = true;
  1078. }
  1079. }
  1080. #ifdef HLRAD_HULLU
  1081. // Check for colored transparency/custom shadows
  1082. VectorFill(d_transparency, 1.0);
  1083. b_transparency = false;
  1084. if (*(s = ValueForKey(ent, "zhlt_customshadow")))
  1085. {
  1086. double r1 = 1.0, g1 = 1.0, b1 = 1.0, tmp = 1.0;
  1087. if (sscanf_s(s, "%lf %lf %lf", &r1, &g1, &b1) == 3) //RGB version
  1088. {
  1089. if(r1<0.0) r1 = 0.0;
  1090. if(g1<0.0) g1 = 0.0;
  1091. if(b1<0.0) b1 = 0.0;
  1092. d_transparency[0] = r1;
  1093. d_transparency[1] = g1;
  1094. d_transparency[2] = b1;
  1095. b_transparency = true;
  1096. }
  1097. else if (sscanf_s(s, "%lf", &tmp) == 1) //Greyscale version
  1098. {
  1099. if(tmp<0.0) tmp = 0.0;
  1100. VectorFill(d_transparency, tmp);
  1101. b_transparency = true;
  1102. }
  1103. }
  1104. #endif
  1105. // Allow models to be lit in an alternate location (pt3)
  1106. if (b_light_origin && b_model_center)
  1107. {
  1108. VectorSubtract(light_origin, model_center, origin);
  1109. }
  1110. #ifdef ZHLT_TEXLIGHT
  1111. //LRC:
  1112. /* ummmmm..... something is very, very wrong here. */
  1113. if (*(s = ValueForKey(ent, "style")))
  1114. {
  1115. style = atoi(s);
  1116. if (style < 0)
  1117. style = -style;
  1118. }
  1119. else
  1120. {
  1121. style = 0;
  1122. }
  1123. //LRC (ends)
  1124. #endif
  1125. for (j = 0; j < mod->numfaces; j++)
  1126. {
  1127. fn = mod->firstface + j;
  1128. g_face_entity[fn] = ent;
  1129. VectorCopy(origin, g_face_offset[fn]);
  1130. g_face_lightmode[fn] = lightmode;
  1131. f = g_dfaces + fn;
  1132. w = new Winding(*f);
  1133. for (k = 0; k < w->m_NumPoints; k++)
  1134. {
  1135. VectorAdd(w->m_Points[k], origin, w->m_Points[k]);
  1136. }
  1137. if (g_allow_opaques)
  1138. {
  1139. if (lightmode & eModelLightmodeOpaque)
  1140. {
  1141. #ifdef HLRAD_HULLU
  1142. AddFaceToOpaqueList(fn, w, d_transparency, b_transparency);
  1143. #else
  1144. AddFaceToOpaqueList(fn, w);
  1145. #endif
  1146. }
  1147. }
  1148. #ifdef ZHLT_TEXLIGHT
  1149. MakePatchForFace(fn, w, style); //LRC
  1150. #else
  1151. MakePatchForFace(fn, w);
  1152. #endif
  1153. }
  1154. }
  1155. Log("%i base patches\n", g_num_patches);
  1156. Log("Opaque faces: %i\n", g_opaque_face_count);
  1157. Log("Square feet: %i [%.2f square inches]\n", (int)(totalarea / 144), totalarea);
  1158. }
  1159. // =====================================================================================
  1160. // patch_sorter
  1161. // =====================================================================================
  1162. static int CDECL patch_sorter(const void* p1, const void* p2)
  1163. {
  1164. patch_t* patch1 = (patch_t*)p1;
  1165. patch_t* patch2 = (patch_t*)p2;
  1166. if (patch1->faceNumber < patch2->faceNumber)
  1167. {
  1168. return -1;
  1169. }
  1170. else if (patch1->faceNumber > patch2->faceNumber)
  1171. {
  1172. return 1;
  1173. }
  1174. else
  1175. {
  1176. return 0;
  1177. }
  1178. }
  1179. // =====================================================================================
  1180. // patch_sorter
  1181. // This sorts the patches by facenumber, which makes their runs compress even better
  1182. // =====================================================================================
  1183. static void SortPatches()
  1184. {
  1185. qsort((void*)g_patches, (size_t) g_num_patches, sizeof(patch_t), patch_sorter);
  1186. // Fixup g_face_patches & Fixup patch->next
  1187. memset(g_face_patches, 0, sizeof(g_face_patches));
  1188. {
  1189. unsigned x;
  1190. patch_t* patch = g_patches + 1;
  1191. patch_t* prev = g_patches;
  1192. g_face_patches[0] = g_patches;
  1193. for (x = 1; x < g_num_patches; x++, patch++)
  1194. {
  1195. if (patch->faceNumber != prev->faceNumber)
  1196. {
  1197. prev->next = NULL;
  1198. g_face_patches[patch->faceNumber] = patch;
  1199. }
  1200. else
  1201. {
  1202. prev->next = patch;
  1203. }
  1204. prev = patch;
  1205. }
  1206. }
  1207. }
  1208. // =====================================================================================
  1209. // FreePatches
  1210. // =====================================================================================
  1211. static void FreePatches()
  1212. {
  1213. unsigned x;
  1214. patch_t* patch = g_patches;
  1215. // AJM EX
  1216. //Log("patches: %i of %i (%2.2lf percent)\n", g_num_patches, MAX_PATCHES, (double)((double)g_num_patches / (double)MAX_PATCHES));
  1217. for (x = 0; x < g_num_patches; x++, patch++)
  1218. {
  1219. delete patch->winding;
  1220. }
  1221. memset(g_patches, 0, sizeof(patch_t) * g_num_patches);
  1222. }
  1223. //=====================================================================
  1224. // =====================================================================================
  1225. // WriteWorld
  1226. // =====================================================================================
  1227. static void WriteWorld(const char* const name)
  1228. {
  1229. unsigned i;
  1230. unsigned j;
  1231. FILE* out;
  1232. patch_t* patch;
  1233. Winding* w;
  1234. out = fopen(name, "w");
  1235. if (!out)
  1236. Error("Couldn't open %s", name);
  1237. for (j = 0, patch = g_patches; j < g_num_patches; j++, patch++)
  1238. {
  1239. w = patch->winding;
  1240. Log("%i\n", w->m_NumPoints);
  1241. for (i = 0; i < w->m_NumPoints; i++)
  1242. {
  1243. #ifdef ZHLT_TEXLIGHT
  1244. Log("%5.2f %5.2f %5.2f %5.3f %5.3f %5.3f\n",
  1245. w->m_Points[i][0],
  1246. w->m_Points[i][1],
  1247. w->m_Points[i][2], patch->totallight[0][0] / 256, patch->totallight[0][1] / 256, patch->totallight[0][2] / 256); //LRC
  1248. #else
  1249. Log("%5.2f %5.2f %5.2f %5.3f %5.3f %5.3f\n",
  1250. w->m_Points[i][0],
  1251. w->m_Points[i][1],
  1252. w->m_Points[i][2], patch->totallight[0] / 256, patch->totallight[1] / 256, patch->totallight[2] / 256);
  1253. #endif
  1254. }
  1255. Log("\n");
  1256. }
  1257. fclose(out);
  1258. }
  1259. // =====================================================================================
  1260. // CollectLight
  1261. // =====================================================================================
  1262. static void CollectLight()
  1263. {
  1264. #ifdef ZHLT_TEXLIGHT
  1265. unsigned j; //LRC
  1266. #endif
  1267. unsigned i;
  1268. patch_t* patch;
  1269. for (i = 0, patch = g_patches; i < g_num_patches; i++, patch++)
  1270. {
  1271. #ifdef ZHLT_TEXLIGHT
  1272. //LRC
  1273. for (j = 0; j < MAXLIGHTMAPS && patch->totalstyle[j] != 255; j++)
  1274. {
  1275. VectorAdd(patch->totallight[j], addlight[i][j], patch->totallight[j]);
  1276. VectorScale(addlight[i][j], TRANSFER_SCALE, emitlight[i][j]);
  1277. VectorClear(addlight[i][j]);
  1278. }
  1279. #else
  1280. VectorAdd(patch->totallight, addlight[i], patch->totallight);
  1281. VectorScale(addlight[i], TRANSFER_SCALE, emitlight[i]);
  1282. VectorClear(addlight[i]);
  1283. #endif
  1284. }
  1285. }
  1286. // =====================================================================================
  1287. // GatherLight
  1288. // Get light from other g_patches
  1289. // Run multi-threaded
  1290. // =====================================================================================
  1291. #ifdef SYSTEM_WIN32
  1292. #pragma warning(push)
  1293. #pragma warning(disable:4100) // unreferenced formal parameter
  1294. #endif
  1295. static void GatherLight(int threadnum)
  1296. {
  1297. int j;
  1298. patch_t* patch;
  1299. #ifdef ZHLT_TEXLIGHT
  1300. unsigned k,m; //LRC
  1301. //LRC vec3_t sum;
  1302. #else
  1303. unsigned k;
  1304. vec3_t sum;
  1305. #endif
  1306. unsigned iIndex;
  1307. transfer_data_t* tData;
  1308. transfer_index_t* tIndex;
  1309. while (1)
  1310. {
  1311. j = GetThreadWork();
  1312. if (j == -1)
  1313. {
  1314. break;
  1315. }
  1316. patch = &g_patches[j];
  1317. tData = patch->tData;
  1318. tIndex = patch->tIndex;
  1319. iIndex = patch->iIndex;
  1320. #ifdef ZHLT_TEXLIGHT
  1321. //LRC
  1322. for (m = 0; m < MAXLIGHTMAPS && patch->totalstyle[m] != 255; m++)
  1323. {
  1324. VectorClear(addlight[j][m]);
  1325. }
  1326. #else
  1327. VectorClear(sum);
  1328. #endif
  1329. for (k = 0; k < iIndex; k++, tIndex++)
  1330. {
  1331. unsigned l;
  1332. unsigned size = (tIndex->size + 1);
  1333. unsigned patchnum = tIndex->index;
  1334. for (l = 0; l < size; l++, tData++, patchnum++)
  1335. {
  1336. vec3_t v;
  1337. #ifdef ZHLT_TEXLIGHT
  1338. //LRC:
  1339. patch_t* emitpatch = &g_patches[patchnum];
  1340. unsigned emitstyle;
  1341. // for each style on the emitting patch
  1342. for (emitstyle = 0; emitstyle < MAXLIGHTMAPS && emitpatch->totalstyle[emitstyle] != 255; emitstyle++)
  1343. {
  1344. //if dynamic bounce has been turned off, we ignore dynamic lightstyles
  1345. if(!g_bounce_dynamic && emitstyle != 0)
  1346. { break; }
  1347. // find the matching style on this (destination) patch
  1348. for (m = 0; m < MAXLIGHTMAPS && patch->totalstyle[m] != 255; m++)
  1349. {
  1350. if (patch->totalstyle[m] == emitpatch->totalstyle[emitstyle])
  1351. {
  1352. break;
  1353. }
  1354. }
  1355. if (m == MAXLIGHTMAPS)
  1356. {
  1357. if(!g_warned_direct || g_verbose)
  1358. {
  1359. Warning("Too many light styles on a face(%f,%f,%f)",patch->origin[0],patch->origin[1],patch->origin[2]);
  1360. g_warned_direct = true;
  1361. }
  1362. }
  1363. else
  1364. {
  1365. if (patch->totalstyle[m] == 255)
  1366. {
  1367. patch->totalstyle[m] = emitpatch->totalstyle[emitstyle];
  1368. }
  1369. VectorScale(emitlight[patchnum][emitstyle], (*tData), v);
  1370. if (isPointFinite(v))
  1371. {
  1372. VectorAdd(addlight[j][m], v, addlight[j][m]);
  1373. }
  1374. else
  1375. {
  1376. Verbose("GatherLight, v (%4.3f %4.3f %4.3f)@(%4.3f %4.3f %4.3f)\n",
  1377. v[0], v[1], v[2], patch->origin[0], patch->origin[1], patch->origin[2]);
  1378. }
  1379. }
  1380. }
  1381. #else
  1382. VectorScale(emitlight[patchnum], (*tData), v);
  1383. if (isPointFinite(v))
  1384. {
  1385. VectorAdd(sum, v, sum);
  1386. }
  1387. else
  1388. {
  1389. Verbose("GatherLight, v (%4.3f %4.3f %4.3f)@(%4.3f %4.3f %4.3f)\n",
  1390. v[0], v[1], v[2], patch->origin[0], patch->origin[1], patch->origin[2]);
  1391. }
  1392. #endif
  1393. }
  1394. }
  1395. #ifdef ZHLT_TEXLIGHT
  1396. //LRC VectorCopy(sum, addlight[j]);
  1397. #else
  1398. VectorCopy(sum, addlight[j]);
  1399. #endif
  1400. }
  1401. }
  1402. // RGB Transfer version
  1403. #ifdef HLRAD_HULLU
  1404. static void GatherRGBLight(int threadnum)
  1405. {
  1406. int j;
  1407. patch_t* patch;
  1408. #ifdef ZHLT_TEXLIGHT
  1409. unsigned k,m; //LRC
  1410. //LRC vec3_t sum;
  1411. #else
  1412. unsigned k;
  1413. vec3_t sum;
  1414. #endif
  1415. unsigned iIndex;
  1416. rgb_transfer_data_t* tRGBData;
  1417. transfer_index_t* tIndex;
  1418. while (1)
  1419. {
  1420. j = GetThreadWork();
  1421. if (j == -1)
  1422. {
  1423. break;
  1424. }
  1425. patch = &g_patches[j];
  1426. tRGBData = patch->tRGBData;
  1427. tIndex = patch->tIndex;
  1428. iIndex = patch->iIndex;
  1429. #ifdef ZHLT_TEXLIGHT
  1430. //LRC
  1431. for (m = 0; m < MAXLIGHTMAPS && patch->totalstyle[m] != 255; m++)
  1432. {
  1433. VectorClear(addlight[j][m]);
  1434. }
  1435. #else
  1436. VectorClear(sum);
  1437. #endif
  1438. for (k = 0; k < iIndex; k++, tIndex++)
  1439. {
  1440. unsigned l;
  1441. unsigned size = (tIndex->size + 1);
  1442. unsigned patchnum = tIndex->index;
  1443. for (l = 0; l < size; l++, tRGBData++, patchnum++)
  1444. {
  1445. vec3_t v;
  1446. #ifdef ZHLT_TEXLIGHT
  1447. //LRC:
  1448. patch_t* emitpatch = &g_patches[patchnum];
  1449. unsigned emitstyle;
  1450. // for each style on the emitting patch
  1451. for (emitstyle = 0; emitstyle < MAXLIGHTMAPS && emitpatch->totalstyle[emitstyle] != 255; emitstyle++)
  1452. {
  1453. //if dynamic bounce has been turned off, we ignore nonzero lightstyles
  1454. if(!g_bounce_dynamic && emitstyle != 0)
  1455. { break; }
  1456. // find the matching style on this (destination) patch
  1457. for (m = 0; m < MAXLIGHTMAPS && patch->totalstyle[m] != 255; m++)
  1458. {
  1459. if (patch->totalstyle[m] == emitpatch->totalstyle[emitstyle])
  1460. {
  1461. break;
  1462. }
  1463. }
  1464. if (m == MAXLIGHTMAPS)
  1465. {
  1466. if(!g_warned_direct || g_verbose)
  1467. {
  1468. Warning("Too many light styles on a face(%f,%f,%f)",patch->origin[0],patch->origin[1],patch->origin[2]);
  1469. g_warned_direct = true;
  1470. }
  1471. }
  1472. else
  1473. {
  1474. if (patch->totalstyle[m] == 255)
  1475. {
  1476. patch->totalstyle[m] = emitpatch->totalstyle[emitstyle];
  1477. // Log("Granting new style %d to patch at idx %d\n", patch->totalstyle[m], m);
  1478. }
  1479. VectorMultiply(emitlight[patchnum][emitstyle], (*tRGBData), v);
  1480. if (isPointFinite(v))
  1481. {
  1482. VectorAdd(addlight[j][m], v, addlight[j][m]);
  1483. }
  1484. else
  1485. {
  1486. Verbose("GatherLight, v (%4.3f %4.3f %4.3f)@(%4.3f %4.3f %4.3f)\n",
  1487. v[0], v[1], v[2], patch->origin[0], patch->origin[1], patch->origin[2]);
  1488. }
  1489. }
  1490. }
  1491. //LRC (ends)
  1492. #else
  1493. VectorMultiply(emitlight[patchnum], (*tRGBData), v);
  1494. if (isPointFinite(v))
  1495. {
  1496. VectorAdd(sum, v, sum);
  1497. }
  1498. else
  1499. {
  1500. Verbose("GatherLight, v (%4.3f %4.3f %4.3f)@(%4.3f %4.3f %4.3f)\n",
  1501. v[0], v[1], v[2], patch->origin[0], patch->origin[1], patch->origin[2]);
  1502. }
  1503. #endif
  1504. }
  1505. }
  1506. #ifdef ZHLT_TEXLIGHT
  1507. //LRC VectorCopy(sum, addlight[j]);
  1508. #else
  1509. VectorCopy(sum, addlight[j]);
  1510. #endif
  1511. }
  1512. }
  1513. #endif
  1514. #ifdef SYSTEM_WIN32
  1515. #pragma warning(pop)
  1516. #endif
  1517. // =====================================================================================
  1518. // BounceLight
  1519. // =====================================================================================
  1520. static void BounceLight()
  1521. {
  1522. unsigned i;
  1523. char name[64];
  1524. #ifdef ZHLT_TEXLIGHT
  1525. unsigned j; //LRC
  1526. #endif
  1527. for (i = 0; i < g_num_patches; i++)
  1528. {
  1529. #ifdef ZHLT_TEXLIGHT
  1530. //LRC
  1531. for (j = 0; j < MAXLIGHTMAPS && g_patches[i].totalstyle[j] != 255; j++)
  1532. {
  1533. VectorScale(g_patches[i].totallight[j], TRANSFER_SCALE, emitlight[i][j]);
  1534. }
  1535. #else
  1536. VectorScale(g_patches[i].totallight, TRANSFER_SCALE, emitlight[i]);
  1537. #endif
  1538. }
  1539. for (i = 0; i < g_numbounce; i++)
  1540. {
  1541. printf("Bounce %u ", i + 1);
  1542. #ifdef HLRAD_HULLU
  1543. if(g_rgb_transfers)
  1544. {
  1545. NamedRunThreadsOn(g_num_patches, g_estimate, GatherRGBLight);
  1546. }
  1547. else
  1548. {
  1549. NamedRunThreadsOn(g_num_patches, g_estimate, GatherLight);
  1550. }
  1551. #else
  1552. NamedRunThreadsOn(g_num_patches, g_estimate, GatherLight);
  1553. #endif
  1554. CollectLight();
  1555. if (g_dumppatches)
  1556. {
  1557. sprintf_s(name, "bounce%u.txt", i);
  1558. WriteWorld(name);
  1559. }
  1560. }
  1561. }
  1562. // =====================================================================================
  1563. // CheckMaxPatches
  1564. // =====================================================================================
  1565. static void CheckMaxPatches()
  1566. {
  1567. //SILENCER: Added more logging
  1568. switch (g_method) {
  1569. case eMethodVismatrix: {
  1570. Log("Base patches created: %i out of %i max. (%.1f%%)\n", g_num_patches, MAX_VISMATRIX_PATCHES, g_num_patches * 100.0 / MAX_VISMATRIX_PATCHES);
  1571. if(g_num_patches > MAX_VISMATRIX_PATCHES)
  1572. hlassume(g_num_patches < MAX_VISMATRIX_PATCHES, assume_MAX_PATCHES);
  1573. break;
  1574. }
  1575. case eMethodSparseVismatrix: {
  1576. Log("Base patches created: %i out of %i max - sparse is on. (%.1f%%)\n", g_num_patches, MAX_SPARSE_VISMATRIX_PATCHES, g_num_patches * 100.0 / MAX_SPARSE_VISMATRIX_PATCHES);
  1577. if(g_num_patches > MAX_SPARSE_VISMATRIX_PATCHES)
  1578. hlassume(g_num_patches < MAX_SPARSE_VISMATRIX_PATCHES, assume_MAX_PATCHES);
  1579. break;
  1580. }
  1581. case eMethodNoVismatrix: {
  1582. Log("Base patches created: %i out of %i max - vismatrix is off. (%.1f%%)\n", g_num_patches, MAX_PATCHES, g_num_patches * 100.0 / MAX_PATCHES);
  1583. if(g_num_patches > MAX_PATCHES)
  1584. hlassume(g_num_patches < MAX_PATCHES, assume_MAX_PATCHES);
  1585. break;
  1586. }
  1587. }
  1588. }
  1589. // =====================================================================================
  1590. // MakeScalesStub
  1591. // =====================================================================================
  1592. static void MakeScalesStub()
  1593. {
  1594. switch (g_method)
  1595. {
  1596. case eMethodVismatrix:
  1597. MakeScalesVismatrix();
  1598. break;
  1599. case eMethodSparseVismatrix:
  1600. MakeScalesSparseVismatrix();
  1601. break;
  1602. case eMethodNoVismatrix:
  1603. MakeScalesNoVismatrix();
  1604. break;
  1605. }
  1606. }
  1607. // =====================================================================================
  1608. // FreeTransfers
  1609. // =====================================================================================
  1610. static void FreeTransfers()
  1611. {
  1612. unsigned x;
  1613. patch_t* patch = g_patches;
  1614. for (x = 0; x < g_num_patches; x++, patch++)
  1615. {
  1616. if (patch->tData)
  1617. {
  1618. FreeBlock(patch->tData);
  1619. patch->tData = NULL;
  1620. }
  1621. #ifdef HLRAD_HULLU
  1622. if (patch->tRGBData)
  1623. {
  1624. FreeBlock(patch->tRGBData);
  1625. patch->tRGBData = NULL;
  1626. }
  1627. #endif
  1628. if (patch->tIndex)
  1629. {
  1630. FreeBlock(patch->tIndex);
  1631. patch->tIndex = NULL;
  1632. }
  1633. }
  1634. }
  1635. // =====================================================================================
  1636. // RadWorld
  1637. // =====================================================================================
  1638. static void RadWorld()
  1639. {
  1640. unsigned i;
  1641. #ifdef ZHLT_TEXLIGHT
  1642. unsigned j;
  1643. #endif
  1644. MakeBackplanes();
  1645. MakeParents(0, -1);
  1646. MakeTnodes(&g_dmodels[0]);
  1647. // turn each face into a single patch
  1648. MakePatches();
  1649. CheckMaxPatches(); // Check here for exceeding max patches, to prevent a lot of work from occuring before an error occurs
  1650. SortPatches(); // Makes the runs in the Transfer Compression really good
  1651. PairEdges();
  1652. // create directlights out of g_patches and lights
  1653. CreateDirectLights();
  1654. Log("\n");
  1655. // build initial facelights
  1656. NamedRunThreadsOnIndividual(g_numfaces, g_estimate, BuildFacelights);
  1657. // free up the direct lights now that we have facelights
  1658. DeleteDirectLights();
  1659. if (g_numbounce > 0)
  1660. {
  1661. // build transfer lists
  1662. MakeScalesStub();
  1663. // spread light around
  1664. BounceLight();
  1665. for (i = 0; i < g_num_patches; i++)
  1666. {
  1667. #ifdef ZHLT_TEXLIGHT// AJM
  1668. for (j = 0; j < MAXLIGHTMAPS && g_patches[i].totalstyle[j] != 255; j++)
  1669. {
  1670. VectorSubtract(g_patches[i].totallight[j], g_patches[i].directlight[j], g_patches[i].totallight[j]);
  1671. }
  1672. #else
  1673. VectorSubtract(g_patches[i].totallight, g_patches[i].directlight, g_patches[i].totallight);
  1674. #endif
  1675. }
  1676. }
  1677. FreeTransfers();
  1678. // blend bounced light into direct light and save
  1679. PrecompLightmapOffsets();
  1680. NamedRunThreadsOnIndividual(g_numfaces, g_estimate, FinalLightFace);
  1681. }
  1682. // =====================================================================================
  1683. // Usage
  1684. // =====================================================================================
  1685. static void Usage()
  1686. {
  1687. Banner();
  1688. Log("\n-= %s Options =-\n\n", g_Program);
  1689. Log(" -sparse : Enable low memory vismatrix algorithm\n");
  1690. Log(" -nomatrix : Disable usage of vismatrix entirely\n\n");
  1691. Log(" -extra : Improve lighting quality by doing 9 point oversampling\n");
  1692. Log(" -bounce # : Set number of radiosity bounces\n");
  1693. Log(" -ambient r g b : Set ambient world light (0.0 to 1.0, r g b)\n");
  1694. Log(" -maxlight # : Set maximum light intensity value\n");
  1695. Log(" -circus : Enable 'circus' mode for locating unlit lightmaps\n");
  1696. Log(" -nopaque : Disable the opaque zhlt_lightflags for this compile\n\n");
  1697. Log(" -smooth # : Set smoothing threshold for blending (in degrees)\n");
  1698. Log(" -chop # : Set radiosity patch size for normal textures\n");
  1699. Log(" -texchop # : Set radiosity patch size for texture light faces\n\n");
  1700. Log(" -notexscale # : Do not scale radiosity patches with texture scale\n");
  1701. Log(" -coring # : Set lighting threshold before blackness\n");
  1702. Log(" -dlight # : Set direct lighting threshold\n");
  1703. Log(" -nolerp : Disable radiosity interpolation, nearest point instead\n\n");
  1704. Log(" -fade # : Set global fade (larger values = shorter lights)\n");
  1705. Log(" -falloff # : Set global falloff mode (1 = inv linear, 2 = inv square)\n");
  1706. Log(" -scale # : Set global light scaling value\n");
  1707. Log(" -gamma # : Set global gamma value\n\n");
  1708. Log(" -sky # : Set ambient sunlight contribution in the shade outside\n");
  1709. Log(" -lights file : Manually specify a lights.rad file to use\n");
  1710. Log(" -noskyfix : Disable light_environment being global\n");
  1711. Log(" -incremental : Use or create an incremental transfer list file\n\n");
  1712. Log(" -dump : Dumps light patches to a file for hlrad debugging info\n\n");
  1713. Log(" -texdata # : Alter maximum texture memory limit (in kb)\n");
  1714. Log(" -lightdata # : Alter maximum lighting memory limit (in kb)\n");
  1715. Log(" -chart : display bsp statitics\n");
  1716. Log(" -low | -high : run program an altered priority level\n");
  1717. Log(" -nolog : Do not generate the compile logfiles\n");
  1718. Log(" -threads # : manually specify the number of threads to run\n");
  1719. Log(" -noconcolors : Disable Windows console text coloring\n");
  1720. #ifdef SYSTEM_WIN32
  1721. Log(" -estimate : display estimated time during compile\n");
  1722. #endif
  1723. #ifdef ZHLT_PROGRESSFILE // AJM
  1724. Log(" -progressfile path : specify the path to a file for progress estimate output\n");
  1725. #endif
  1726. #ifdef SYSTEM_POSIX
  1727. Log(" -noestimate : Do not display continuous compile time estimates\n");
  1728. #endif
  1729. Log(" -verbose : compile with verbose messages\n");
  1730. Log(" -noinfo : Do not show tool configuration information\n");
  1731. Log(" -dev # : compile with developer message\n\n");
  1732. // ------------------------------------------------------------------------
  1733. // Changes by Adam Foster - [email protected]
  1734. #ifdef HLRAD_WHOME
  1735. // AJM: we dont need this extra crap
  1736. //Log("-= Unofficial features added by Adam Foster ([email protected]) =-\n\n");
  1737. Log(" -colourgamma r g b : Sets different gamma values for r, g, b\n" );
  1738. Log(" -colourscale r g b : Sets different lightscale values for r, g ,b\n" );
  1739. Log(" -colourjitter r g b : Adds noise, independent colours, for dithering\n");
  1740. Log(" -jitter r g b : Adds noise, monochromatic, for dithering\n");
  1741. Log(" -nodiffuse : Disables light_environment diffuse hack\n");
  1742. Log(" -nospotpoints : Disables light_spot spherical point sources\n");
  1743. Log(" -softlight r g b d : Scaling values for backwards-light hack\n\n");
  1744. //Log("-= End of unofficial features! =-\n\n" );
  1745. #endif
  1746. // ------------------------------------------------------------------------
  1747. #ifdef HLRAD_HULLU
  1748. Log(" -customshadowwithbounce : Enables custom shadows with bounce light\n");
  1749. Log(" -rgbtransfers : Enables RGB Transfers (for custom shadows)\n\n");
  1750. #endif
  1751. Log(" mapfile : The mapfile to compile\n\n");
  1752. exit(1);
  1753. }
  1754. // =====================================================================================
  1755. // Settings
  1756. // =====================================================================================
  1757. static void Settings()
  1758. {
  1759. char* tmp;
  1760. char buf1[1024];
  1761. char buf2[1024];
  1762. if (!g_info)
  1763. {
  1764. return;
  1765. }
  1766. Log("\n-= Current %s Settings =-\n", g_Program);
  1767. Log("Name | Setting | Default\n"
  1768. "--------------------|---------------------|-------------------------\n");
  1769. // ZHLT Common Settings
  1770. if (DEFAULT_NUMTHREADS == -1)
  1771. {
  1772. Log("threads [ %17d ] [ Varies ]\n", g_numthreads);
  1773. }
  1774. else
  1775. {
  1776. Log("threads [ %17d ] [ %17d ]\n", g_numthreads, DEFAULT_NUMTHREADS);
  1777. }
  1778. Log("verbose [ %17s ] [ %17s ]\n", g_verbose ? "on" : "off", DEFAULT_VERBOSE ? "on" : "off");
  1779. Log("log [ %17s ] [ %17s ]\n", g_log ? "on" : "off", DEFAULT_LOG ? "on" : "off");
  1780. Log("developer [ %17d ] [ %17d ]\n", g_developer, DEFAULT_DEVELOPER);
  1781. Log("chart [ %17s ] [ %17s ]\n", g_chart ? "on" : "off", DEFAULT_CHART ? "on" : "off");
  1782. Log("estimate [ %17s ] [ %17s ]\n", g_estimate ? "on" : "off", DEFAULT_ESTIMATE ? "on" : "off");
  1783. Log("max texture memory [ %17d ] [ %17d ]\n", g_max_map_miptex, DEFAULT_MAX_MAP_MIPTEX);
  1784. Log("max lighting memory [ %17d ] [ %17d ]\n", g_max_map_lightdata, DEFAULT_MAX_MAP_LIGHTDATA);
  1785. switch (g_threadpriority)
  1786. {
  1787. case eThreadPriorityNormal:
  1788. default:
  1789. tmp = "Normal";
  1790. break;
  1791. case eThreadPriorityLow:
  1792. tmp = "Low";
  1793. break;
  1794. case eThreadPriorityHigh:
  1795. tmp = "High";
  1796. break;
  1797. }
  1798. Log("priority [ %17s ] [ %17s ]\n", tmp, "Normal");
  1799. Log("\n");
  1800. // HLRAD Specific Settings
  1801. switch (g_method)
  1802. {
  1803. default:
  1804. tmp = "Unknown";
  1805. break;
  1806. case eMethodVismatrix:
  1807. tmp = "Original";
  1808. break;
  1809. case eMethodSparseVismatrix:
  1810. tmp = "Sparse";
  1811. break;
  1812. case eMethodNoVismatrix:
  1813. tmp = "NoMatrix";
  1814. break;
  1815. }
  1816. Log("vismatrix algorithm [ %17s ] [ %17s ]\n", tmp, "Original");
  1817. Log("oversampling (-extra)[ %17s ] [ %17s ]\n", g_extra ? "on" : "off", DEFAULT_EXTRA ? "on" : "off");
  1818. Log("bounces [ %17d ] [ %17d ]\n", g_numbounce, DEFAULT_BOUNCE);
  1819. Log("bounce dynamic light [ %17s ] [ %17s ]\n", g_bounce_dynamic ? "on" : "off", DEFAULT_BOUNCE_DYNAMIC ? "on" : "off");
  1820. safe_snprintf(buf1, sizeof(buf1), "%1.3f %1.3f %1.3f", g_ambient[0], g_ambient[1], g_ambient[2]);
  1821. safe_snprintf(buf2, sizeof(buf2), "%1.3f %1.3f %1.3f", DEFAULT_AMBIENT_RED, DEFAULT_AMBIENT_GREEN, DEFAULT_AMBIENT_BLUE);
  1822. Log("ambient light [ %17s ] [ %17s ]\n", buf1, buf2);
  1823. safe_snprintf(buf1, sizeof(buf1), "%3.3f", g_maxlight);
  1824. safe_snprintf(buf2, sizeof(buf2), "%3.3f", DEFAULT_MAXLIGHT);
  1825. Log("maximum light [ %17s ] [ %17s ]\n", buf1, buf2);
  1826. Log("circus mode [ %17s ] [ %17s ]\n", g_circus ? "on" : "off", DEFAULT_CIRCUS ? "on" : "off");
  1827. Log("\n");
  1828. safe_snprintf(buf1, sizeof(buf1), "%3.3f", g_smoothing_value);
  1829. safe_snprintf(buf2, sizeof(buf2), "%3.3f", DEFAULT_SMOOTHING_VALUE);
  1830. Log("smoothing threshold [ %17s ] [ %17s ]\n", buf1, buf2);
  1831. safe_snprintf(buf1, sizeof(buf1), "%3.3f", g_dlight_threshold);
  1832. safe_snprintf(buf2, sizeof(buf2), "%3.3f", DEFAULT_DLIGHT_THRESHOLD);
  1833. Log("direct threshold [ %17s ] [ %17s ]\n", buf1, buf2);
  1834. safe_snprintf(buf1, sizeof(buf1), "%3.3f", g_direct_scale);
  1835. safe_snprintf(buf2, sizeof(buf2), "%3.3f", DEFAULT_DLIGHT_SCALE);
  1836. Log("direct light scale [ %17s ] [ %17s ]\n", buf1, buf2);
  1837. safe_snprintf(buf1, sizeof(buf1), "%3.3f", g_coring);
  1838. safe_snprintf(buf2, sizeof(buf2), "%3.3f", DEFAULT_CORING);
  1839. Log("coring threshold [ %17s ] [ %17s ]\n", buf1, buf2);
  1840. Log("patch interpolation [ %17s ] [ %17s ]\n", g_lerp_enabled ? "on" : "off", DEFAULT_LERP_ENABLED ? "on" : "off");
  1841. Log("\n");
  1842. Log("texscale [ %17s ] [ %17s ]\n", g_texscale ? "on" : "off", DEFAULT_TEXSCALE ? "on" : "off");
  1843. Log("patch subdividing [ %17s ] [ %17s ]\n", g_subdivide ? "on" : "off", DEFAULT_SUBDIVIDE ? "on" : "off");
  1844. safe_snprintf(buf1, sizeof(buf1), "%3.3f", g_chop);
  1845. safe_snprintf(buf2, sizeof(buf2), "%3.3f", DEFAULT_CHOP);
  1846. Log("chop value [ %17s ] [ %17s ]\n", buf1, buf2);
  1847. safe_snprintf(buf1, sizeof(buf1), "%3.3f", g_texchop);
  1848. safe_snprintf(buf2, sizeof(buf2), "%3.3f", DEFAULT_TEXCHOP);
  1849. Log("texchop value [ %17s ] [ %17s ]\n", buf1, buf2);
  1850. Log("\n");
  1851. safe_snprintf(buf1, sizeof(buf1), "%3.3f", g_fade);
  1852. safe_snprintf(buf2, sizeof(buf2), "%3.3f", DEFAULT_FADE);
  1853. Log("global fade [ %17s ] [ %17s ]\n", buf1, buf2);
  1854. Log("global falloff [ %17d ] [ %17d ]\n", g_falloff, DEFAULT_FALLOFF);
  1855. // ------------------------------------------------------------------------
  1856. // Changes by Adam Foster - [email protected]
  1857. // replaces the old stuff for displaying current values for gamma and lightscale
  1858. #ifdef HLRAD_WHOME
  1859. safe_snprintf(buf1, sizeof(buf1), "%1.3f %1.3f %1.3f", g_colour_lightscale[0], g_colour_lightscale[1], g_colour_lightscale[2]);
  1860. safe_snprintf(buf2, sizeof(buf2), "%1.3f %1.3f %1.3f", DEFAULT_COLOUR_LIGHTSCALE_RED, DEFAULT_COLOUR_LIGHTSCALE_GREEN, DEFAULT_COLOUR_LIGHTSCALE_BLUE);
  1861. Log("global light scale [ %17s ] [ %17s ]\n", buf1, buf2);
  1862. safe_snprintf(buf1, sizeof(buf1), "%1.3f %1.3f %1.3f", g_colour_qgamma[0], g_colour_qgamma[1], g_colour_qgamma[2]);
  1863. safe_snprintf(buf2, sizeof(buf2), "%1.3f %1.3f %1.3f", DEFAULT_COLOUR_GAMMA_RED, DEFAULT_COLOUR_GAMMA_GREEN, DEFAULT_COLOUR_GAMMA_BLUE);
  1864. Log("global gamma [ %17s ] [ %17s ]\n", buf1, buf2);
  1865. #endif
  1866. // ------------------------------------------------------------------------
  1867. safe_snprintf(buf1, sizeof(buf1), "%3.3f", g_lightscale);
  1868. safe_snprintf(buf2, sizeof(buf2), "%3.3f", DEFAULT_LIGHTSCALE);
  1869. Log("global light scale [ %17s ] [ %17s ]\n", buf1, buf2);
  1870. #ifndef HLRAD_WHOME
  1871. safe_snprintf(buf1, sizeof(buf1), "%3.3f", g_qgamma);
  1872. safe_snprintf(buf2, sizeof(buf2), "%3.3f", DEFAULT_GAMMA);
  1873. Log("global gamma amount [ %17s ] [ %17s ]\n", buf1, buf2);
  1874. #endif
  1875. safe_snprintf(buf1, sizeof(buf1), "%3.3f", g_indirect_sun);
  1876. safe_snprintf(buf2, sizeof(buf2), "%3.3f", DEFAULT_INDIRECT_SUN);
  1877. Log("global sky diffusion [ %17s ] [ %17s ]\n", buf1, buf2);
  1878. Log("\n");
  1879. Log("opaque entities [ %17s ] [ %17s ]\n", g_allow_opaques ? "on" : "off", DEFAULT_ALLOW_OPAQUES ? "on" : "off");
  1880. Log("sky lighting fix [ %17s ] [ %17s ]\n", g_sky_lighting_fix ? "on" : "off", DEFAULT_SKY_LIGHTING_FIX ? "on" : "off");
  1881. Log("incremental [ %17s ] [ %17s ]\n", g_incremental ? "on" : "off", DEFAULT_INCREMENTAL ? "on" : "off");
  1882. Log("dump [ %17s ] [ %17s ]\n", g_dumppatches ? "on" : "off", DEFAULT_DUMPPATCHES ? "on" : "off");
  1883. // ------------------------------------------------------------------------
  1884. // Changes by Adam Foster - [email protected]
  1885. // displays information on all the brand-new features :)
  1886. #ifdef HLRAD_WHOME
  1887. Log("\n");
  1888. safe_snprintf(buf1, sizeof(buf1), "%3.1f %3.1f %3.1f", g_colour_jitter_hack[0], g_colour_jitter_hack[1], g_colour_jitter_hack[2]);
  1889. safe_snprintf(buf2, sizeof(buf2), "%3.1f %3.1f %3.1f", DEFAULT_COLOUR_JITTER_HACK_RED, DEFAULT_COLOUR_JITTER_HACK_GREEN, DEFAULT_COLOUR_JITTER_HACK_BLUE);
  1890. Log("colour jitter [ %17s ] [ %17s ]\n", buf1, buf2);
  1891. safe_snprintf(buf1, sizeof(buf1), "%3.1f %3.1f %3.1f", g_jitter_hack[0], g_jitter_hack[1], g_jitter_hack[2]);
  1892. safe_snprintf(buf2, sizeof(buf2), "%3.1f %3.1f %3.1f", DEFAULT_JITTER_HACK_RED, DEFAULT_JITTER_HACK_GREEN, DEFAULT_JITTER_HACK_BLUE);
  1893. Log("monochromatic jitter [ %17s ] [ %17s ]\n", buf1, buf2);
  1894. safe_snprintf(buf1, sizeof(buf1), "%2.1f %2.1f %2.1f %2.1f", g_softlight_hack[0], g_softlight_hack[1], g_softlight_hack[2], g_softlight_hack_distance);
  1895. safe_snprintf(buf2, sizeof(buf2), "%2.1f %2.1f %2.1f %2.1f", DEFAULT_SOFTLIGHT_HACK_RED, DEFAULT_SOFTLIGHT_HACK_GREEN, DEFAULT_SOFTLIGHT_HACK_BLUE, DEFAULT_SOFTLIGHT_HACK_DISTANCE);
  1896. Log("softlight hack [ %17s ] [ %17s ]\n", buf1, buf2);
  1897. Log("diffuse hack [ %17s ] [ %17s ]\n", g_diffuse_hack ? "on" : "off", DEFAULT_DIFFUSE_HACK ? "on" : "off");
  1898. Log("spotlight points [ %17s ] [ %17s ]\n", g_spotlight_hack ? "on" : "off", DEFAULT_SPOTLIGHT_HACK ? "on" : "off");
  1899. #endif
  1900. // ------------------------------------------------------------------------
  1901. #ifdef HLRAD_HULLU
  1902. Log("\n");
  1903. Log("custom shadows with bounce light\n"
  1904. " [ %17s ] [ %17s ]\n", g_customshadow_with_bouncelight ? "on" : "off", DEFAULT_CUSTOMSHADOW_WITH_BOUNCELIGHT ? "on" : "off");
  1905. Log("rgb transfers [ %17s ] [ %17s ]\n", g_rgb_transfers ? "on" : "off", DEFAULT_RGB_TRANSFERS ? "on" : "off");
  1906. #endif
  1907. Log("\n\n");
  1908. }
  1909. #ifdef HLRAD_INFO_TEXLIGHTS
  1910. // AJM: added in
  1911. // =====================================================================================
  1912. // ReadInfoTexlights
  1913. // try and parse texlight info from the info_texlights entity
  1914. // =====================================================================================
  1915. void ReadInfoTexlights()
  1916. {
  1917. int k;
  1918. int values;
  1919. int numtexlights = 0;
  1920. float r, g, b, i;
  1921. entity_t* mapent;
  1922. epair_t* ep;
  1923. texlight_t texlight;
  1924. for (k = 0; k < g_numentities; k++)
  1925. {
  1926. mapent = &g_entities[k];
  1927. if (strcmp(ValueForKey(mapent, "classname"), "info_texlights"))
  1928. continue;
  1929. Log("[Reading texlights from info_texlights map entity]\n");
  1930. for (ep = mapent->epairs; ep; ep = ep->next)
  1931. {
  1932. if ( !strcmp(ep->key, "classname")
  1933. || !strcmp(ep->key, "origin")
  1934. )
  1935. continue; // we dont care about these keyvalues
  1936. values = sscanf_s(ep->value, "%f %f %f %f", &r, &g, &b, &i);
  1937. if (values == 1)
  1938. {
  1939. g = b = r;
  1940. }
  1941. else if (values == 4) // use brightness value.
  1942. {
  1943. r *= i / 255.0;
  1944. g *= i / 255.0;
  1945. b *= i / 255.0;
  1946. }
  1947. else if (values != 3)
  1948. {
  1949. Warning("Ignoring bad texlight '%s' in info_texlights entity", ep->key);
  1950. continue;
  1951. }
  1952. texlight.name = ep->key;
  1953. texlight.value[0] = r;
  1954. texlight.value[1] = g;
  1955. texlight.value[2] = b;
  1956. texlight.filename = "info_texlights";
  1957. s_texlights.push_back(texlight);
  1958. numtexlights++;
  1959. }
  1960. Log("[%i texlights parsed from info_texlights map entity]\n\n", numtexlights);
  1961. }
  1962. }
  1963. #endif
  1964. const char* lights_rad = "lights.rad";
  1965. const char* ext_rad = ".rad";
  1966. // =====================================================================================
  1967. // LoadRadFiles
  1968. // =====================================================================================
  1969. void LoadRadFiles(const char* const mapname, const char* const user_rad, const char* argv0)
  1970. {
  1971. char global_lights[_MAX_PATH];
  1972. char mapname_lights[_MAX_PATH];
  1973. char mapfile[_MAX_PATH];
  1974. char mapdir[_MAX_PATH];
  1975. char appdir[_MAX_PATH];
  1976. // Get application directory (only an approximation on posix systems)
  1977. // try looking in the directory we were run from
  1978. {
  1979. char tmp[_MAX_PATH];
  1980. memset(tmp, 0, sizeof(tmp));
  1981. #ifdef SYSTEM_WIN32
  1982. GetModuleFileName(NULL, tmp, _MAX_PATH);
  1983. #else
  1984. safe_strncpy(tmp, argv0, _MAX_PATH);
  1985. #endif
  1986. ExtractFilePath(tmp, appdir);
  1987. }
  1988. // Get map directory
  1989. ExtractFilePath(mapname, mapdir);
  1990. ExtractFileBase(mapname, mapfile);
  1991. // Look for lights.rad in mapdir
  1992. safe_strncpy(global_lights, mapdir, _MAX_PATH);
  1993. safe_strncat(global_lights, lights_rad, _MAX_PATH);
  1994. if (q_exists(global_lights))
  1995. {
  1996. ReadLightFile(global_lights);
  1997. }
  1998. else
  1999. {
  2000. // Look for lights.rad in appdir
  2001. safe_strncpy(global_lights, appdir, _MAX_PATH);
  2002. safe_strncat(global_lights, lights_rad, _MAX_PATH);
  2003. if (q_exists(global_lights))
  2004. {
  2005. ReadLightFile(global_lights);
  2006. }
  2007. else
  2008. {
  2009. // Look for lights.rad in current working directory
  2010. safe_strncpy(global_lights, lights_rad, _MAX_PATH);
  2011. /* SILENCER -->
  2012. // This only confuses people.
  2013. // "WHERE DOES IT GET THAT FILE FROM??"
  2014. if (q_exists(global_lights))
  2015. {
  2016. ReadLightFile(global_lights);
  2017. }
  2018. // <-- SILENCER */
  2019. }
  2020. }
  2021. // Look for mapname.rad in mapdir
  2022. safe_strncpy(mapname_lights, mapdir, _MAX_PATH);
  2023. safe_strncat(mapname_lights, mapfile, _MAX_PATH);
  2024. DefaultExtension(mapname_lights, ext_rad);
  2025. if (q_exists(mapname_lights))
  2026. {
  2027. ReadLightFile(mapname_lights);
  2028. }
  2029. if (user_rad)
  2030. {
  2031. char user_lights[_MAX_PATH];
  2032. char userfile[_MAX_PATH];
  2033. ExtractFile(user_rad, userfile);
  2034. // Look for user.rad from command line (raw)
  2035. safe_strncpy(user_lights, user_rad, _MAX_PATH);
  2036. if (q_exists(user_lights))
  2037. {
  2038. ReadLightFile(user_lights);
  2039. }
  2040. else
  2041. {
  2042. // Try again with .rad enforced as extension
  2043. DefaultExtension(user_lights, ext_rad);
  2044. if (q_exists(user_lights))
  2045. {
  2046. ReadLightFile(user_lights);
  2047. }
  2048. else
  2049. {
  2050. // Look for user.rad in mapdir
  2051. safe_strncpy(user_lights, mapdir, _MAX_PATH);
  2052. safe_strncat(user_lights, userfile, _MAX_PATH);
  2053. DefaultExtension(user_lights, ext_rad);
  2054. if (q_exists(user_lights))
  2055. {
  2056. ReadLightFile(user_lights);
  2057. }
  2058. else
  2059. {
  2060. // Look for user.rad in appdir
  2061. safe_strncpy(user_lights, appdir, _MAX_PATH);
  2062. safe_strncat(user_lights, userfile, _MAX_PATH);
  2063. DefaultExtension(user_lights, ext_rad);
  2064. if (q_exists(user_lights))
  2065. {
  2066. ReadLightFile(user_lights);
  2067. }
  2068. else
  2069. {
  2070. // Look for user.rad in current working directory
  2071. safe_strncpy(user_lights, userfile, _MAX_PATH);
  2072. DefaultExtension(user_lights, ext_rad);
  2073. /* SILENCER -->
  2074. // This only confuses people.
  2075. // "WHERE DOES IT GET THAT FILE FROM??"
  2076. if (q_exists(user_lights))
  2077. {
  2078. ReadLightFile(user_lights);
  2079. }
  2080. // <-- SILENCER */
  2081. }
  2082. }
  2083. }
  2084. }
  2085. }
  2086. #ifdef HLRAD_INFO_TEXLIGHTS
  2087. ReadInfoTexlights(); // AJM
  2088. #endif
  2089. }
  2090. // =====================================================================================
  2091. // main
  2092. // =====================================================================================
  2093. int main(const int argc, char** argv)
  2094. {
  2095. int i;
  2096. double start, end;
  2097. const char* mapname_from_arg = NULL;
  2098. const char* user_lights = NULL;
  2099. g_Program = "hlrad";
  2100. if (argc == 1)
  2101. Usage();
  2102. for (i = 1; i < argc; i++)
  2103. {
  2104. if (!strcasecmp(argv[i], "-dump"))
  2105. {
  2106. g_dumppatches = true;
  2107. }
  2108. else if (!strcasecmp(argv[i], "-bounce"))
  2109. {
  2110. if (i < argc)
  2111. {
  2112. g_numbounce = atoi(argv[++i]);
  2113. if (g_numbounce > 1000)
  2114. {
  2115. Log("Unexpectedly large value (>1000) for '-bounce'\n");
  2116. Usage();
  2117. }
  2118. }
  2119. else
  2120. {
  2121. Usage();
  2122. }
  2123. }
  2124. else if (!strcasecmp(argv[i], "-dev"))
  2125. {
  2126. if (i < argc)
  2127. {
  2128. g_developer = (developer_level_t)atoi(argv[++i]);
  2129. }
  2130. else
  2131. {
  2132. Usage();
  2133. }
  2134. }
  2135. else if (!strcasecmp(argv[i], "-verbose"))
  2136. {
  2137. g_verbose = true;
  2138. }
  2139. else if (!strcasecmp(argv[i], "-noinfo"))
  2140. {
  2141. g_info = false;
  2142. }
  2143. else if (!strcasecmp(argv[i], "-threads"))
  2144. {
  2145. if (i < argc)
  2146. {
  2147. g_numthreads = atoi(argv[++i]);
  2148. if (g_numthreads < 1)
  2149. {
  2150. Log("Expected value of at least 1 for '-threads'\n");
  2151. Usage();
  2152. }
  2153. }
  2154. else
  2155. {
  2156. Usage();
  2157. }
  2158. }
  2159. #ifdef SYSTEM_WIN32
  2160. else if (!strcasecmp(argv[i], "-estimate"))
  2161. {
  2162. g_estimate = true;
  2163. }
  2164. #endif
  2165. #ifdef SYSTEM_POSIX
  2166. else if (!strcasecmp(argv[i], "-noestimate"))
  2167. {
  2168. g_estimate = false;
  2169. }
  2170. #endif
  2171. #ifdef ZHLT_NETVIS
  2172. else if (!strcasecmp(argv[i], "-client"))
  2173. {
  2174. if (i < argc)
  2175. {
  2176. g_clientid = atoi(argv[++i]);
  2177. }
  2178. else
  2179. {
  2180. Usage();
  2181. }
  2182. }
  2183. #endif
  2184. else if (!strcasecmp(argv[i], "-nolerp"))
  2185. {
  2186. g_lerp_enabled = false;
  2187. }
  2188. else if (!strcasecmp(argv[i], "-chop"))
  2189. {
  2190. if (i < argc)
  2191. {
  2192. g_chop = atof(argv[++i]);
  2193. if (g_chop < 1)
  2194. {
  2195. Log("expected value greater than 1 for '-chop'\n");
  2196. Usage();
  2197. }
  2198. if (g_chop < 32)
  2199. {
  2200. Log("Warning: Chop values below 32 are not recommended.");
  2201. }
  2202. }
  2203. else
  2204. {
  2205. Usage();
  2206. }
  2207. }
  2208. else if (!strcasecmp(argv[i], "-texchop"))
  2209. {
  2210. if (i < argc)
  2211. {
  2212. g_texchop = atof(argv[++i]);
  2213. if (g_texchop < 1)
  2214. {
  2215. Log("expected value greater than 1 for '-texchop'\n");
  2216. Usage();
  2217. }
  2218. if (g_texchop < 32)
  2219. {
  2220. Log("Warning: texchop values below 16 are not recommended.");
  2221. }
  2222. }
  2223. else
  2224. {
  2225. Usage();
  2226. }
  2227. }
  2228. else if (!strcasecmp(argv[i], "-nodynbounce"))
  2229. {
  2230. g_bounce_dynamic = false;
  2231. }
  2232. else if (!strcasecmp(argv[i], "-notexscale"))
  2233. {
  2234. g_texscale = false;
  2235. }
  2236. else if (!strcasecmp(argv[i], "-nosubdivide"))
  2237. {
  2238. if (i < argc)
  2239. {
  2240. g_subdivide = false;
  2241. }
  2242. else
  2243. {
  2244. Usage();
  2245. }
  2246. }
  2247. else if (!strcasecmp(argv[i], "-scale"))
  2248. {
  2249. if (i < argc)
  2250. {
  2251. // ------------------------------------------------------------------------
  2252. // Changes by Adam Foster - [email protected]
  2253. // Munge monochrome lightscale into colour one
  2254. #ifdef HLRAD_WHOME
  2255. i++;
  2256. g_colour_lightscale[0] = (float)atof(argv[i]);
  2257. g_colour_lightscale[1] = (float)atof(argv[i]);
  2258. g_colour_lightscale[2] = (float)atof(argv[i]);
  2259. #else
  2260. g_lightscale = (float)atof(argv[++i]);
  2261. #endif
  2262. // ------------------------------------------------------------------------
  2263. }
  2264. else
  2265. {
  2266. Usage();
  2267. }
  2268. }
  2269. else if (!strcasecmp(argv[i], "-falloff"))
  2270. {
  2271. if (i < argc)
  2272. {
  2273. g_falloff = (int)atoi(argv[++i]);
  2274. if ((g_falloff != 1) && (g_falloff != 2))
  2275. {
  2276. Log("-falloff must be 1 or 2\n");
  2277. Usage();
  2278. }
  2279. }
  2280. else
  2281. {
  2282. Usage();
  2283. }
  2284. }
  2285. else if (!strcasecmp(argv[i], "-fade"))
  2286. {
  2287. if (i < argc)
  2288. {
  2289. g_fade = (float)atof(argv[++i]);
  2290. if (g_fade < 0.0)
  2291. {
  2292. Log("-fade must be a positive number\n");
  2293. Usage();
  2294. }
  2295. }
  2296. else
  2297. {
  2298. Usage();
  2299. }
  2300. }
  2301. else if (!strcasecmp(argv[i], "-ambient"))
  2302. {
  2303. if (i + 3 < argc)
  2304. {
  2305. g_ambient[0] = (float)atof(argv[++i]) * 128;
  2306. g_ambient[1] = (float)atof(argv[++i]) * 128;
  2307. g_ambient[2] = (float)atof(argv[++i]) * 128;
  2308. }
  2309. else
  2310. {
  2311. Error("expected three color values after '-ambient'\n");
  2312. }
  2313. }
  2314. else if (!strcasecmp(argv[i], "-maxlight"))
  2315. {
  2316. if (i < argc)
  2317. {
  2318. g_maxlight = (float)atof(argv[++i]) * 128;
  2319. if (g_maxlight <= 0)
  2320. {
  2321. Log("expected positive value after '-maxlight'\n");
  2322. Usage();
  2323. }
  2324. }
  2325. else
  2326. {
  2327. Usage();
  2328. }
  2329. }
  2330. else if (!strcasecmp(argv[i], "-lights"))
  2331. {
  2332. if (i < argc)
  2333. {
  2334. user_lights = argv[++i];
  2335. }
  2336. else
  2337. {
  2338. Usage();
  2339. }
  2340. }
  2341. else if (!strcasecmp(argv[i], "-circus"))
  2342. {
  2343. g_circus = true;
  2344. }
  2345. else if (!strcasecmp(argv[i], "-noskyfix"))
  2346. {
  2347. g_sky_lighting_fix = false;
  2348. }
  2349. else if (!strcasecmp(argv[i], "-incremental"))
  2350. {
  2351. g_incremental = true;
  2352. }
  2353. else if (!strcasecmp(argv[i], "-chart"))
  2354. {
  2355. g_chart = true;
  2356. }
  2357. else if (!strcasecmp(argv[i], "-low"))
  2358. {
  2359. g_threadpriority = eThreadPriorityLow;
  2360. }
  2361. else if (!strcasecmp(argv[i], "-high"))
  2362. {
  2363. g_threadpriority = eThreadPriorityHigh;
  2364. }
  2365. else if (!strcasecmp(argv[i], "-nolog"))
  2366. {
  2367. g_log = false;
  2368. }
  2369. else if (!strcasecmp(argv[i], "-gamma"))
  2370. {
  2371. if (i < argc)
  2372. {
  2373. // ------------------------------------------------------------------------
  2374. // Changes by Adam Foster - [email protected]
  2375. // Munge values from original, monochrome gamma into colour gamma
  2376. #ifdef HLRAD_WHOME
  2377. i++;
  2378. g_colour_qgamma[0] = (float)atof(argv[i]);
  2379. g_colour_qgamma[1] = (float)atof(argv[i]);
  2380. g_colour_qgamma[2] = (float)atof(argv[i]);
  2381. #else
  2382. g_qgamma = (float)atof(argv[++i]);
  2383. #endif
  2384. // ------------------------------------------------------------------------
  2385. }
  2386. else
  2387. {
  2388. Usage();
  2389. }
  2390. }
  2391. else if (!strcasecmp(argv[i], "-dlight"))
  2392. {
  2393. if (i < argc)
  2394. {
  2395. g_dlight_threshold = (float)atof(argv[++i]);
  2396. }
  2397. else
  2398. {
  2399. Usage();
  2400. }
  2401. }
  2402. else if (!strcasecmp(argv[i], "-extra"))
  2403. {
  2404. g_extra = true;
  2405. }
  2406. else if (!strcasecmp(argv[i], "-sky"))
  2407. {
  2408. if (i < argc)
  2409. {
  2410. g_indirect_sun = (float)atof(argv[++i]);
  2411. }
  2412. else
  2413. {
  2414. Usage();
  2415. }
  2416. }
  2417. else if (!strcasecmp(argv[i], "-smooth"))
  2418. {
  2419. if (i < argc)
  2420. {
  2421. g_smoothing_value = atof(argv[++i]);
  2422. }
  2423. else
  2424. {
  2425. Usage();
  2426. }
  2427. }
  2428. else if (!strcasecmp(argv[i], "-coring"))
  2429. {
  2430. if (i < argc)
  2431. {
  2432. g_coring = (float)atof(argv[++i]);
  2433. }
  2434. else
  2435. {
  2436. Usage();
  2437. }
  2438. }
  2439. else if (!strcasecmp(argv[i], "-texdata"))
  2440. {
  2441. if (i < argc)
  2442. {
  2443. int x = atoi(argv[++i]) * 1024;
  2444. if (x > g_max_map_miptex)
  2445. {
  2446. g_max_map_miptex = x;
  2447. }
  2448. }
  2449. else
  2450. {
  2451. Usage();
  2452. }
  2453. }
  2454. else if (!strcasecmp(argv[i], "-lightdata"))
  2455. {
  2456. if (i < argc)
  2457. {
  2458. int x = atoi(argv[++i]) * 1024;
  2459. if (x > g_max_map_lightdata)
  2460. {
  2461. g_max_map_lightdata = x;
  2462. }
  2463. }
  2464. else
  2465. {
  2466. Usage();
  2467. }
  2468. }
  2469. else if (!strcasecmp(argv[i], "-sparse"))
  2470. {
  2471. g_method = eMethodSparseVismatrix;
  2472. }
  2473. else if (!strcasecmp(argv[i], "-nomatrix"))
  2474. {
  2475. g_method = eMethodNoVismatrix;
  2476. }
  2477. else if (!strcasecmp(argv[i], "-nopaque"))
  2478. {
  2479. g_allow_opaques = false;
  2480. }
  2481. else if (!strcasecmp(argv[i], "-dscale"))
  2482. {
  2483. if (i < argc)
  2484. {
  2485. g_direct_scale = (float)atof(argv[++i]);
  2486. }
  2487. else
  2488. {
  2489. Usage();
  2490. }
  2491. }
  2492. // ------------------------------------------------------------------------
  2493. // Changes by Adam Foster - [email protected]
  2494. #ifdef HLRAD_WHOME
  2495. else if (!strcasecmp(argv[i], "-colourgamma"))
  2496. {
  2497. if (i + 3 < argc)
  2498. {
  2499. g_colour_qgamma[0] = (float)atof(argv[++i]);
  2500. g_colour_qgamma[1] = (float)atof(argv[++i]);
  2501. g_colour_qgamma[2] = (float)atof(argv[++i]);
  2502. }
  2503. else
  2504. {
  2505. Error("expected three color values after '-colourgamma'\n");
  2506. }
  2507. }
  2508. else if (!strcasecmp(argv[i], "-colourscale"))
  2509. {
  2510. if (i + 3 < argc)
  2511. {
  2512. g_colour_lightscale[0] = (float)atof(argv[++i]);
  2513. g_colour_lightscale[1] = (float)atof(argv[++i]);
  2514. g_colour_lightscale[2] = (float)atof(argv[++i]);
  2515. }
  2516. else
  2517. {
  2518. Error("expected three color values after '-colourscale'\n");
  2519. }
  2520. }
  2521. else if (!strcasecmp(argv[i], "-colourjitter"))
  2522. {
  2523. if (i + 3 < argc)
  2524. {
  2525. g_colour_jitter_hack[0] = (float)atof(argv[++i]);
  2526. g_colour_jitter_hack[1] = (float)atof(argv[++i]);
  2527. g_colour_jitter_hack[2] = (float)atof(argv[++i]);
  2528. }
  2529. else
  2530. {
  2531. Error("expected three color values after '-colourjitter'\n");
  2532. }
  2533. }
  2534. else if (!strcasecmp(argv[i], "-jitter"))
  2535. {
  2536. if (i + 3 < argc)
  2537. {
  2538. g_jitter_hack[0] = (float)atof(argv[++i]);
  2539. g_jitter_hack[1] = (float)atof(argv[++i]);
  2540. g_jitter_hack[2] = (float)atof(argv[++i]);
  2541. }
  2542. else
  2543. {
  2544. Error("expected three color values after '-jitter'\n");
  2545. }
  2546. }
  2547. else if (!strcasecmp(argv[i], "-nodiffuse"))
  2548. {
  2549. g_diffuse_hack = false;
  2550. }
  2551. else if (!strcasecmp(argv[i], "-nospotpoints"))
  2552. {
  2553. g_spotlight_hack = false;
  2554. }
  2555. else if (!strcasecmp(argv[i], "-softlight"))
  2556. {
  2557. if (i + 4 < argc)
  2558. {
  2559. g_softlight_hack[0] = (float)atof(argv[++i]);
  2560. g_softlight_hack[1] = (float)atof(argv[++i]);
  2561. g_softlight_hack[2] = (float)atof(argv[++i]);
  2562. g_softlight_hack_distance = (float)atof(argv[++i]);
  2563. }
  2564. else
  2565. {
  2566. Error("expected three color scalers and a distance after '-softlight'\n");
  2567. }
  2568. }
  2569. #endif
  2570. // ------------------------------------------------------------------------
  2571. #ifdef HLRAD_HULLU
  2572. else if (!strcasecmp(argv[i], "-customshadowwithbounce"))
  2573. {
  2574. g_customshadow_with_bouncelight = true;
  2575. }
  2576. else if (!strcasecmp(argv[i], "-rgbtransfers"))
  2577. {
  2578. g_rgb_transfers = true;
  2579. }
  2580. #endif
  2581. #ifdef ZHLT_PROGRESSFILE // AJM
  2582. else if (!strcasecmp(argv[i], "-progressfile"))
  2583. {
  2584. if (i < argc)
  2585. {
  2586. g_progressfile = argv[++i];
  2587. }
  2588. else
  2589. {
  2590. Log("Error: -progressfile: expected path to progress file following parameter\n");
  2591. Usage();
  2592. }
  2593. }
  2594. #endif
  2595. #ifdef HLRAD_FASTMATH
  2596. else if (!strcasecmp(argv[i], "-oldmath"))
  2597. {
  2598. Warning("-oldmath was introduced as a temporary workaround to a bug in HLRAD and is no longer supported.\n Please remove it from your command line.\n");
  2599. }
  2600. #endif
  2601. // SILENCER -->
  2602. else if (!strcasecmp(argv[i], "-noconcolors"))
  2603. {
  2604. g_noConColors = true;
  2605. }
  2606. // <-- SILENCER
  2607. else if (argv[i][0] == '-')
  2608. {
  2609. Log("Unknown option \"%s\"\n", argv[i]);
  2610. Usage();
  2611. }
  2612. else if (!mapname_from_arg)
  2613. {
  2614. mapname_from_arg = argv[i];
  2615. }
  2616. else
  2617. {
  2618. Log("Unknown option \"%s\"\n", argv[i]);
  2619. Usage();
  2620. }
  2621. }
  2622. if (!mapname_from_arg)
  2623. {
  2624. Log("No mapname specified\n");
  2625. Usage();
  2626. }
  2627. g_smoothing_threshold = (float)cos(g_smoothing_value * (Q_PI / 180.0));
  2628. safe_strncpy(g_Mapname, mapname_from_arg, _MAX_PATH);
  2629. FlipSlashes(g_Mapname);
  2630. StripExtension(g_Mapname);
  2631. OpenLog(g_clientid);
  2632. atexit(CloseLog);
  2633. ThreadSetDefault();
  2634. ThreadSetPriority(g_threadpriority);
  2635. LogStart(argc, argv);
  2636. CheckForErrorLog();
  2637. dtexdata_init();
  2638. atexit(dtexdata_free);
  2639. // END INIT
  2640. // BEGIN RAD
  2641. start = I_FloatTime();
  2642. // normalise maxlight
  2643. if (g_maxlight > 255)
  2644. g_maxlight = 255;
  2645. strcpy_s(g_source, mapname_from_arg);
  2646. StripExtension(g_source);
  2647. DefaultExtension(g_source, ".bsp");
  2648. LoadBSPFile(g_source);
  2649. ParseEntities();
  2650. Settings();
  2651. LoadRadFiles(g_Mapname, user_lights, argv[0]);
  2652. if (!g_visdatasize)
  2653. {
  2654. Warning("No vis information, direct lighting only.");
  2655. g_numbounce = 0;
  2656. g_ambient[0] = g_ambient[1] = g_ambient[2] = 0.1f;
  2657. }
  2658. RadWorld();
  2659. FreeOpaqueFaceList();
  2660. FreePatches();
  2661. if (g_chart)
  2662. PrintBSPFileSizes();
  2663. WriteBSPFile(g_source);
  2664. end = I_FloatTime();
  2665. LogTimeElapsed(end - start);
  2666. // END RAD
  2667. return 0;
  2668. }