qrad.cpp 91 KB

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