Render3DFX.cpp 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954
  1. #ifdef _3dfx
  2. #include "Hunt.h"
  3. #include <glide.h>
  4. #include <sst1vid.h>
  5. #include <stdio.h>
  6. #undef TCMAX
  7. #undef TCMIN
  8. #define TCMAX 255.5f
  9. #define TCMIN 0.5f
  10. #define _ZSCALE (16.f*65534.f)
  11. Vector2di ORList[2048];
  12. int ORLCount = 0;
  13. void RenderShadowClip (TModel*, float, float, float, float, float, float, int, float, float);
  14. GrVertex gvtx[16];
  15. int zs;
  16. float SunLight;
  17. float TraceK,SkyTraceK,FogYGrad,FogYBase;
  18. int SunScrX, SunScrY;
  19. int SkySumR, SkySumG, SkySumB;
  20. int LowHardMemory;
  21. float vFogT[1024];
  22. float vLight[1024];
  23. GrLfbInfo_t linfo;
  24. int lsw;
  25. BOOL SmallFont;
  26. BOOL WaterMode;
  27. //float mdlScale = 1.0f;
  28. FxU32 FXConstTstartAddress, FXConstTendAddress;
  29. int FxMemUsageCount;
  30. int FxMemLoaded;
  31. int FxLastTexture;
  32. int FxTMUNumber;
  33. typedef struct _fxmemmap {
  34. int cpuaddr, size, lastused;
  35. FxU32 FXTbaseaddr;
  36. GrTexInfo FXtexinfo;
  37. } Tfxmemmap;
  38. #define fxmemmapsize 128
  39. Tfxmemmap FxMemMap[fxmemmapsize+2];
  40. void CalcFogLevel_Gradient(Vector3d v)
  41. {
  42. FogYBase = CalcFogLevel(v);
  43. if (FogYBase>0) {
  44. v.y+=800;
  45. FogYGrad = (CalcFogLevel(v) - FogYBase) / 800.f;
  46. } else FogYGrad=0;
  47. }
  48. void Hardware_ZBuffer(BOOL bl)
  49. {
  50. if (bl) {
  51. grDepthBiasLevel(0);
  52. } else {
  53. grDepthBiasLevel(8000);
  54. }
  55. }
  56. void Init3DHardware()
  57. {
  58. PrintLog("Checking 3dfx:\n");
  59. GrHwConfiguration hwconfig;
  60. FxTMUNumber = 1;
  61. grGlideInit();
  62. grSstQueryHardware(&hwconfig);
  63. if (hwconfig.SSTs[0].type == GR_SSTTYPE_VOODOO) {
  64. FxTMUNumber = hwconfig.SSTs[0].sstBoard.VoodooConfig.nTexelfx;
  65. PrintLog("Voodoo card.\n");
  66. }
  67. if (hwconfig.SSTs[0].type == GR_SSTTYPE_Voodoo2) {
  68. FxTMUNumber = hwconfig.SSTs[0].sstBoard.Voodoo2Config.nTexelfx;
  69. PrintLog("Voodoo2 card.\n");
  70. }
  71. if (hwconfig.SSTs[0].type == GR_SSTTYPE_SST96) {
  72. FxTMUNumber = hwconfig.SSTs[0].sstBoard.VoodooConfig.nTexelfx;
  73. PrintLog("Voodoo Rush card.\n");
  74. }
  75. grSstSelect( 0 );
  76. HARD3D = TRUE;
  77. FXConstTstartAddress = grTexMinAddress(GR_TMU0);
  78. FXConstTendAddress = grTexMaxAddress(GR_TMU0);
  79. //wsprintf(logt, "Start Address: %d\n", FXConstTstartAddress); PrintLog(logt);
  80. //wsprintf(logt, "End Address: %d\n", FXConstTendAddress); PrintLog(logt);
  81. //if (FXConstTendAddress > 2097144) FXConstTendAddress = 2097144;
  82. }
  83. void Activate3DHardware()
  84. {
  85. if (WinW<512)
  86. SetVideoMode(512,384);
  87. SetVideoMode(WinW,WinH);
  88. int fxmode = GR_RESOLUTION_640x480;
  89. switch (WinW) {
  90. case 320: fxmode = GR_RESOLUTION_320x240; break;
  91. case 400: fxmode = GR_RESOLUTION_400x300; break;
  92. case 512: fxmode = GR_RESOLUTION_512x384; break;
  93. case 640: fxmode = GR_RESOLUTION_640x480; break;
  94. case 800: fxmode = GR_RESOLUTION_800x600; break;
  95. case 1024: fxmode = GR_RESOLUTION_1024x768; break;
  96. case 1280: fxmode = GR_RESOLUTION_1280x1024; break;
  97. case 1600: fxmode = GR_RESOLUTION_1600x1200; break;
  98. }
  99. RESET:
  100. if (!grSstWinOpen( (unsigned int) hwndMain, fxmode, GR_REFRESH_NONE, GR_COLORFORMAT_ABGR, GR_ORIGIN_UPPER_LEFT, 2, 1 ) )
  101. if (!grSstWinOpen( (unsigned int) hwndMain, fxmode, GR_REFRESH_60Hz, GR_COLORFORMAT_ABGR, GR_ORIGIN_UPPER_LEFT, 2, 1 ) ) {
  102. if (fxmode!=GR_RESOLUTION_640x480) {
  103. PrintLog("Voodoo: Can't set selected video mode\n");
  104. fxmode = GR_RESOLUTION_640x480;
  105. OptRes = 3;
  106. SetVideoMode(640,480);
  107. goto RESET;
  108. } else
  109. DoHalt("Voodoo: Can't set video mode");
  110. }
  111. grHints(GR_HINT_STWHINT, 0);
  112. grHints(GR_HINT_FPUPRECISION, 0);
  113. grHints(GR_HINT_ALLOW_MIPMAP_DITHER, 0);
  114. FXConstTstartAddress = grTexMinAddress(GR_TMU0);
  115. FXConstTendAddress = grTexMaxAddress(GR_TMU0);
  116. wsprintf(logt, "Start Address: %d\n", FXConstTstartAddress); PrintLog(logt);
  117. wsprintf(logt, "End Address: %d\n", FXConstTendAddress); PrintLog(logt);
  118. //if (FXConstTendAddress > 2097144) FXConstTendAddress = 2097144;
  119. grColorMask(FXTRUE, FXFALSE);
  120. grDepthBufferMode( GR_DEPTHBUFFER_ZBUFFER );
  121. grDepthBufferFunction( GR_CMP_GEQUAL );
  122. grDepthMask( FXTRUE );
  123. //grCullMode(GR_CULL_NEGATIVE);
  124. //======= setup gouraud ===========//
  125. guColorCombineFunction( GR_COLORCOMBINE_TEXTURE_TIMES_ITRGB );
  126. grTexCombineFunction(GR_TMU0, GR_TEXTURECOMBINE_DECAL);
  127. grTexClampMode(GR_TMU0, GR_TEXTURECLAMP_CLAMP, GR_TEXTURECLAMP_CLAMP);
  128. //====== setup alpha ==========//
  129. guAlphaSource(GR_ALPHASOURCE_CC_ALPHA);
  130. grConstantColorValue(0xFF000000);
  131. grAlphaBlendFunction(GR_BLEND_SRC_ALPHA,
  132. GR_BLEND_ONE_MINUS_SRC_ALPHA,
  133. GR_BLEND_ONE,
  134. GR_BLEND_ONE);
  135. grDitherMode(GR_DITHER_4x4);
  136. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
  137. //grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_POINT_SAMPLED, GR_TEXTUREFILTER_POINT_SAMPLED);
  138. grFogColorValue( (SkyB<<16) + (SkyG<<8) + SkyR);
  139. grFogMode(GR_FOG_DISABLE);
  140. grChromakeyValue(0x00);
  141. grChromakeyMode(GR_CHROMAKEY_ENABLE);
  142. grChromakeyMode(GR_CHROMAKEY_DISABLE);
  143. grBufferClear( 0xFF000000, 0, 0);
  144. grGammaCorrectionValue(1.2f);
  145. FxLastTexture = fxmemmapsize+1;
  146. }
  147. void ResetTextureMap()
  148. {
  149. FxMemUsageCount = 0;
  150. FxLastTexture = fxmemmapsize+1;
  151. for (int m=0; m<fxmemmapsize+2; m++) {
  152. FxMemMap[m].lastused = 0;
  153. FxMemMap[m].cpuaddr = 0;
  154. FxMemMap[m].FXTbaseaddr = 0;
  155. FxMemMap[m].size = 0;
  156. }
  157. while (grBufferNumPending() > 0);
  158. }
  159. void ShutDown3DHardware()
  160. {
  161. grSstWinClose();
  162. grGlideShutdown();
  163. ResetTextureMap();
  164. }
  165. int Get_GR_LOD(int TW)
  166. {
  167. if (TW==128) return GR_LOD_128;
  168. if (TW==256) return GR_LOD_256;
  169. if (TW==64) return GR_LOD_64;
  170. if (TW==32) return GR_LOD_32;
  171. return 0;
  172. }
  173. void guDownLoad(int fxm, int FXTstartAddress, LPVOID tptr, int w, int h)
  174. {
  175. int textureSize = w*w*2;
  176. /*
  177. if (FXTstartAddress<=2097152 && FXTstartAddress+textureSize>=2097152) {
  178. FxMemMap[fxm-1].size = 2097152 - FxMemMap[fxm-1].FXTbaseaddr;
  179. FXTstartAddress=2097152;
  180. }
  181. */
  182. /*
  183. if (FXTstartAddress + textureSize >= FXConstTendAddress)
  184. DoHalt("3DFX memory overflow.");
  185. */
  186. FxMemMap[fxm].FXtexinfo.smallLod = Get_GR_LOD(w);
  187. FxMemMap[fxm].FXtexinfo.largeLod = FxMemMap[fxm].FXtexinfo.smallLod;
  188. FxMemMap[fxm].FXtexinfo.aspectRatio = GR_ASPECT_1x1;
  189. FxMemMap[fxm].FXtexinfo.format = GR_TEXFMT_ARGB_1555;
  190. FxMemMap[fxm].FXtexinfo.data = tptr;
  191. FxMemMap[fxm].FXTbaseaddr = FXTstartAddress;
  192. FxMemMap[fxm].size = textureSize;
  193. FxMemMap[fxm].cpuaddr = (int)tptr;
  194. FxMemLoaded+=textureSize;
  195. grTexDownloadMipMap(GR_TMU0,
  196. FXTstartAddress + FXConstTstartAddress,
  197. GR_MIPMAPLEVELMASK_BOTH,
  198. &FxMemMap[fxm].FXtexinfo);
  199. }
  200. void InsertFxMM(int m)
  201. {
  202. /* memcpy(&FxMemMap[m+1],
  203. &FxMemMap[m],
  204. (fxmemmapsize-m) * sizeof(Tfxmemmap) ); */
  205. for (int mm=fxmemmapsize-1; mm>m; mm--)
  206. FxMemMap[m] = FxMemMap[m-1];
  207. }
  208. int FXDownLoadTexture(LPVOID tptr, int w, int h)
  209. {
  210. FxU32 FXTstartAddress = 0;
  211. FxU32 FXTendAddress = FXConstTendAddress;
  212. int textureSize = w*w*2;
  213. int fxm = 0;
  214. int m;
  215. //========== if no memory used ==========//
  216. if (!FxMemMap[0].cpuaddr) {
  217. guDownLoad(0, 0, tptr, w, h);
  218. return 0;
  219. }
  220. //======= search for hole in the memory usage ==========//
  221. for (m=0; m<fxmemmapsize; m++) {
  222. int nextbegin;
  223. int curend = FxMemMap[m].FXTbaseaddr + FxMemMap[m].size;
  224. if (!FxMemMap[m].cpuaddr) break;
  225. if (FxMemMap[m+1].cpuaddr) nextbegin = FxMemMap[m+1].FXTbaseaddr;
  226. else nextbegin = FXTendAddress;
  227. if (curend<=2097152 && curend+textureSize>=2097152)
  228. curend = 2097152;
  229. if (nextbegin - curend >= textureSize) {
  230. InsertFxMM(m+1);
  231. guDownLoad(m+1, curend, tptr, w, h);
  232. return m+1;
  233. }
  234. }
  235. //====== search for most older acceptable block =====================//
  236. fxm = -1;
  237. int unusedtime = 1;
  238. for (m=0; m<fxmemmapsize; m++) {
  239. if (!FxMemMap[m].cpuaddr) break;
  240. int ut = FxMemUsageCount - FxMemMap[m].lastused;
  241. if (FxMemMap[m].size==textureSize)
  242. if (ut >= unusedtime) {
  243. unusedtime = ut;
  244. fxm = m; }
  245. if (FxMemMap[m].size>textureSize)
  246. if (ut > unusedtime) {
  247. unusedtime = ut;
  248. fxm = m; }
  249. }
  250. if (fxm!=-1) {
  251. guDownLoad(fxm, FxMemMap[fxm].FXTbaseaddr, tptr, w, h);
  252. return fxm;
  253. }
  254. //=== not free or unused memory found => perfoming half-memory reset ======//
  255. ResetTextureMap();
  256. guDownLoad(0, 0, tptr, w, h);
  257. return 0;
  258. }
  259. void SetFXTexture(LPVOID tptr, int w, int h)
  260. {
  261. if (FxMemMap[FxLastTexture].cpuaddr == (int)tptr) return;
  262. int fxm = -1;
  263. for (int m=0; m<fxmemmapsize; m++) {
  264. if (FxMemMap[m].cpuaddr == (int)tptr) { fxm = m; break; }
  265. if (!FxMemMap[m].cpuaddr) break;
  266. }
  267. if (fxm==-1) fxm = FXDownLoadTexture(tptr, w, h);
  268. FxMemMap[fxm].lastused = FxMemUsageCount;
  269. grTexSource(GR_TMU0,
  270. FxMemMap[fxm].FXTbaseaddr + FXConstTstartAddress,
  271. GR_MIPMAPLEVELMASK_BOTH,
  272. &FxMemMap[fxm].FXtexinfo);
  273. FxLastTexture = fxm;
  274. }
  275. float GetTraceK(int x, int y)
  276. {
  277. if (x<8 || y<8 || x>WinW-8 || y>WinH-8) return 0.f;
  278. float k = 0;
  279. linfo.size = sizeof(GrLfbInfo_t);
  280. if (!grLfbLock(
  281. GR_LFB_READ_ONLY,
  282. GR_BUFFER_AUXBUFFER,
  283. GR_LFBWRITEMODE_ANY,
  284. GR_ORIGIN_UPPER_LEFT,
  285. FXFALSE,
  286. &linfo)) return 0.f;
  287. WORD CC = 0;
  288. int bw = (linfo.strideInBytes>>1);
  289. if ( *((WORD*)linfo.lfbPtr + (y+0)*bw + x+0) == CC ) k+=1.f;
  290. if ( *((WORD*)linfo.lfbPtr + (y+10)*bw + x+0) == CC ) k+=1.f;
  291. if ( *((WORD*)linfo.lfbPtr + (y-10)*bw + x+0) == CC ) k+=1.f;
  292. if ( *((WORD*)linfo.lfbPtr + (y+0)*bw + x+10) == CC ) k+=1.f;
  293. if ( *((WORD*)linfo.lfbPtr + (y+0)*bw + x-10) == CC ) k+=1.f;
  294. if ( *((WORD*)linfo.lfbPtr + (y+8)*bw + x+8) == CC ) k+=1.f;
  295. if ( *((WORD*)linfo.lfbPtr + (y+8)*bw + x-8) == CC ) k+=1.f;
  296. if ( *((WORD*)linfo.lfbPtr + (y-8)*bw + x+8) == CC ) k+=1.f;
  297. if ( *((WORD*)linfo.lfbPtr + (y-8)*bw + x-8) == CC ) k+=1.f;
  298. grLfbUnlock(GR_LFB_READ_ONLY, GR_BUFFER_AUXBUFFER);
  299. k/=9;
  300. DeltaFunc(TraceK, k, TimeDt / 1024.f);
  301. return TraceK;
  302. }
  303. void AddSkySum(WORD C)
  304. {
  305. int R = C>>11;
  306. int G = (C>>5) & 63;
  307. int B = C & 31;
  308. SkySumR += R*8;
  309. SkySumG += G*4;
  310. SkySumB += B*8;
  311. }
  312. float GetSkyK(int x, int y)
  313. {
  314. if (x<10 || y<10 || x>WinW-10 || y>WinH-10) return 0.5;
  315. SkySumR = 0;
  316. SkySumG = 0;
  317. SkySumB = 0;
  318. float k = 0;
  319. linfo.size = sizeof(GrLfbInfo_t);
  320. if (!grLfbLock(
  321. GR_LFB_READ_ONLY,
  322. GR_BUFFER_BACKBUFFER,
  323. GR_LFBWRITEMODE_ANY,
  324. GR_ORIGIN_UPPER_LEFT,
  325. FXFALSE,
  326. &linfo)) return 0.f;
  327. int bw = (linfo.strideInBytes>>1);
  328. AddSkySum(*((WORD*)linfo.lfbPtr + (y+0)*bw + x+0));
  329. AddSkySum(*((WORD*)linfo.lfbPtr + (y+6)*bw + x+0));
  330. AddSkySum(*((WORD*)linfo.lfbPtr + (y-6)*bw + x+0));
  331. AddSkySum(*((WORD*)linfo.lfbPtr + (y+0)*bw + x+6));
  332. AddSkySum(*((WORD*)linfo.lfbPtr + (y+0)*bw + x-6));
  333. AddSkySum(*((WORD*)linfo.lfbPtr + (y+4)*bw + x+4));
  334. AddSkySum(*((WORD*)linfo.lfbPtr + (y+4)*bw + x-4));
  335. AddSkySum(*((WORD*)linfo.lfbPtr + (y-4)*bw + x+4));
  336. AddSkySum(*((WORD*)linfo.lfbPtr + (y-4)*bw + x-4));
  337. grLfbUnlock(GR_LFB_READ_ONLY, GR_BUFFER_BACKBUFFER);
  338. SkySumR-=SkyTR*9;
  339. SkySumG-=SkyTG*9;
  340. SkySumB-=SkyTB*9;
  341. /*
  342. SkySumR-=55*9;
  343. SkySumG-=88*9;
  344. SkySumB-=122*9;
  345. */
  346. k = (float)sqrt(SkySumR*SkySumR + SkySumG*SkySumG + SkySumB*SkySumB) / 9;
  347. if (k>80) k = 80;
  348. if (k< 0) k = 0;
  349. k = 1.0f - k/80.f;
  350. if (k<0.2) k=0.2f;
  351. if (OptDayNight==2) k=0.3 + k/2.75;
  352. DeltaFunc(SkyTraceK, k, (0.07f + (float)fabs(k-SkyTraceK)) * (TimeDt / 512.f) );
  353. return SkyTraceK;
  354. }
  355. void TryHiResTx()
  356. {
  357. int UsedMem = 0;
  358. for (int m=0; m<fxmemmapsize; m++) {
  359. if (!FxMemMap[m].cpuaddr) break;
  360. if (FxMemMap[m].lastused+2>=FxMemUsageCount)
  361. UsedMem+= FxMemMap[m].size;
  362. }
  363. if (UsedMem*3 < (int)FXConstTendAddress)
  364. LOWRESTX = FALSE;
  365. }
  366. void RenderFSRect(DWORD color)
  367. {
  368. guColorCombineFunction( GR_COLORCOMBINE_CCRGB );
  369. grConstantColorValue(color);
  370. gvtx[0].x = 0;
  371. gvtx[0].y = 0;
  372. gvtx[0].tmuvtx[0].sow = 0.0;
  373. gvtx[0].tmuvtx[0].tow = 0.0;
  374. gvtx[0].z = (float)1.f;
  375. gvtx[0].ooz = (_ZSCALE/gvtx[0].z);
  376. gvtx[0].oow = 1.0f/gvtx[0].z;
  377. gvtx[1].x = WinW;
  378. gvtx[1].y = 0;
  379. gvtx[1].tmuvtx[0].sow = 0.0;
  380. gvtx[1].tmuvtx[0].tow = 0.0;
  381. gvtx[1].z = (float)1.f;
  382. gvtx[1].ooz = (_ZSCALE/gvtx[0].z);
  383. gvtx[1].oow = 1.0f/gvtx[0].z;
  384. gvtx[2].x = WinW;
  385. gvtx[2].y = WinH;
  386. gvtx[2].tmuvtx[0].sow = 0.0;
  387. gvtx[2].tmuvtx[0].tow = 0.0;
  388. gvtx[2].z = (float)1.f;
  389. gvtx[2].ooz = (_ZSCALE/gvtx[0].z);
  390. gvtx[2].oow = 1.0f/gvtx[0].z;
  391. gvtx[3].x = 0;
  392. gvtx[3].y = WinH;
  393. gvtx[3].tmuvtx[0].sow = 0.0;
  394. gvtx[3].tmuvtx[0].tow = 0.0;
  395. gvtx[3].z = (float)1.f;
  396. gvtx[3].ooz = (_ZSCALE/gvtx[0].z);
  397. gvtx[3].oow = 1.0f/gvtx[0].z;
  398. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  399. dFacesCount++;
  400. grDrawTriangle(&gvtx[0], &gvtx[2], &gvtx[3]);
  401. dFacesCount++;
  402. guColorCombineFunction( GR_COLORCOMBINE_TEXTURE_TIMES_ITRGB );
  403. grConstantColorValue(0xFF000000);
  404. }
  405. void RenderCircle(float cx, float cy, float z, float _R, DWORD RGBA, DWORD RGBA2)
  406. {
  407. float R = (float)((int)( _R*16.f)) / 16.f;
  408. float R2 = (float)((int)(0.65f*_R*16.f)) / 16.f;
  409. int r = (RGBA ) & 0xFF;
  410. int g = (RGBA>>8 ) & 0xFF;
  411. int b = (RGBA>>16) & 0xFF;
  412. int a = (RGBA>>24) & 0xFF;
  413. gvtx[0].r = r; gvtx[0].g = g; gvtx[0].b = b; gvtx[0].a = a;
  414. gvtx[0].z = -z;
  415. gvtx[0].ooz = (_ZSCALE/-z);
  416. gvtx[0].oow = 1.0f;
  417. r = (RGBA2 ) & 0xFF;
  418. g = (RGBA2>>8 ) & 0xFF;
  419. b = (RGBA2>>16) & 0xFF;
  420. a = (RGBA2>>24) & 0xFF;
  421. gvtx[1].r = r; gvtx[1].g = g; gvtx[1].b = b; gvtx[1].a = a;
  422. gvtx[1].z = -z;
  423. gvtx[1].ooz = (_ZSCALE/-z);
  424. gvtx[1].oow = 1.0f;
  425. gvtx[2].r = r; gvtx[2].g = g; gvtx[2].b = b; gvtx[2].a = a;
  426. gvtx[2].z = z;
  427. gvtx[2].ooz = (_ZSCALE/-z);
  428. gvtx[2].oow = 1.0f;
  429. gvtx[0].x = cx;
  430. gvtx[0].y = cy;
  431. gvtx[1].x = cx;
  432. gvtx[1].y = cy-R;
  433. gvtx[2].x = cx+R2;
  434. gvtx[2].y = cy-R2;
  435. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  436. gvtx[2].x = cx-R2;
  437. gvtx[2].y = cy-R2;
  438. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  439. gvtx[1].x = cx-R;
  440. gvtx[1].y = cy;
  441. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  442. gvtx[2].x = cx+R2;
  443. gvtx[2].y = cy-R2;
  444. gvtx[1].x = cx+R;
  445. gvtx[1].y = cy;
  446. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  447. gvtx[1].x = cx;
  448. gvtx[1].y = cy+R;
  449. gvtx[2].x = cx+R2;
  450. gvtx[2].y = cy+R2;
  451. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  452. gvtx[2].x = cx-R2;
  453. gvtx[2].y = cy+R2;
  454. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  455. gvtx[1].x = cx-R;
  456. gvtx[1].y = cy;
  457. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  458. gvtx[2].x = cx+R2;
  459. gvtx[2].y = cy+R2;
  460. gvtx[1].x = cx+R;
  461. gvtx[1].y = cy;
  462. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  463. }
  464. void ShowVideo()
  465. {
  466. if (FxMemLoaded > 1024*1024) LowHardMemory++;
  467. else LowHardMemory=0;
  468. if (LowHardMemory>2) {
  469. LOWRESTX = TRUE;
  470. LowHardMemory = 0;
  471. }
  472. if (OptText==0) LOWRESTX = TRUE;
  473. if (OptText==1) LOWRESTX = FALSE;
  474. if (OptText==2)
  475. if (LOWRESTX && (Takt & 63)==0) TryHiResTx();
  476. /*
  477. char t[128];
  478. wsprintf(t, "FX mem loaded: %dK", FxMemLoaded >> 10);
  479. if (FxMemLoaded) AddMessage(t); */
  480. FxMemLoaded = 0;
  481. gvtx[0].a = 0;
  482. gvtx[1].a = 0;
  483. if (UNDERWATER) RenderFSRect(0x80000000 + CurFogColor);
  484. if (OptDayNight!=2)
  485. if (!UNDERWATER && (SunLight>1.0f) ) {
  486. SunLight *= GetTraceK(SunScrX, SunScrY);
  487. RenderFSRect(0xC0FFFF + ((int)SunLight<<24));
  488. }
  489. RenderHealthBar();
  490. while (grBufferNumPending() > 2);
  491. grBufferSwap(0);
  492. grBufferClear( 0xFF000000+ (SkyB<<16) + (SkyG<<8) + SkyR,0,0);
  493. FxMemUsageCount++;
  494. }
  495. void CopyHARDToDIB()
  496. {
  497. grLfbReadRegion(
  498. GR_BUFFER_FRONTBUFFER,
  499. 0,0,WinW,WinH, 2048, lpVideoBuf);
  500. }
  501. void FXPutBitMapWithKey(int x0, int y0, int w, int h, int smw, LPVOID lpData)
  502. {
  503. //grLfbConstantDepth(65000);
  504. //grConstantColorValue(0xD0000000);
  505. linfo.size = sizeof(GrLfbInfo_t);
  506. if (!grLfbLock(
  507. GR_LFB_WRITE_ONLY,
  508. GR_BUFFER_BACKBUFFER,
  509. GR_LFBWRITEMODE_565,
  510. GR_ORIGIN_UPPER_LEFT,
  511. FXFALSE,
  512. &linfo)) return;
  513. for (int y=0; y<h; y++)
  514. for (int x=0; x<w; x++) {
  515. WORD c = *((WORD*)lpData + y*smw + x);
  516. if (c!=0xFFFF)
  517. *((WORD*)linfo.lfbPtr + (y+y0)*(linfo.strideInBytes>>1) + x+x0) = conv_565(c);
  518. }
  519. grLfbUnlock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER);
  520. }
  521. void FXPutBitMap(int x0, int y0, int w, int h, int smw, LPVOID lpData)
  522. {
  523. //grLfbConstantDepth(65000);
  524. //grConstantColorValue(0xD0000000);
  525. linfo.size = sizeof(GrLfbInfo_t);
  526. if (!grLfbLock(
  527. GR_LFB_WRITE_ONLY,
  528. GR_BUFFER_BACKBUFFER,
  529. GR_LFBWRITEMODE_565,
  530. GR_ORIGIN_UPPER_LEFT,
  531. FXFALSE,
  532. &linfo)) return;
  533. smw*=2; w*=2;
  534. linfo.lfbPtr = (void*) ( (WORD*)linfo.lfbPtr + x0);
  535. for (int y=0; y<h; y++)
  536. memcpy((BYTE*)linfo.lfbPtr + (y+y0)*(linfo.strideInBytes),
  537. (BYTE*)lpData + y*smw,
  538. w);
  539. grLfbUnlock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER);
  540. }
  541. void DrawPicture(int x, int y, TPicture &pic)
  542. {
  543. FXPutBitMap(x, y, pic.W, pic.H, pic.W, pic.lpImage);
  544. }
  545. void FXTextOut(int x, int y, LPSTR t, int color)
  546. {
  547. HDC _hdc = hdcCMain;
  548. HBITMAP hbmpOld = SelectObject(_hdc,hbmpVideoBuf);
  549. SetBkMode(_hdc, TRANSPARENT);
  550. HFONT oldfont;
  551. if (SmallFont) oldfont = SelectObject(_hdc, fnt_Small);
  552. int w = GetTextW(_hdc, t)+4;
  553. for (int h=0; h<18; h++)
  554. FillMemory((BYTE*)lpVideoBuf + h*2048, w*2, 0xFF);
  555. SetTextColor(_hdc, 0x00101010);
  556. TextOut(_hdc, 2, 1, t, strlen(t));
  557. SetTextColor(_hdc, color);
  558. TextOut(_hdc, 1, 0, t, strlen(t));
  559. if (SmallFont) SelectObject(_hdc, oldfont);
  560. SelectObject(_hdc,hbmpOld);
  561. FXPutBitMapWithKey(x, y, w, 18, 1024, lpVideoBuf);
  562. }
  563. void DrawTrophyText(int x0, int y0)
  564. {
  565. int x;
  566. SmallFont = TRUE;
  567. HFONT oldfont = SelectObject(hdcMain, fnt_Small);
  568. int tc = TrophyBody;
  569. int dtype = TrophyRoom.Body[tc].ctype;
  570. int time = TrophyRoom.Body[tc].time;
  571. int date = TrophyRoom.Body[tc].date;
  572. int wep = TrophyRoom.Body[tc].weapon;
  573. int score = TrophyRoom.Body[tc].score;
  574. float scale = TrophyRoom.Body[tc].scale;
  575. float range = TrophyRoom.Body[tc].range;
  576. char t[32];
  577. x0+=14; y0+=18;
  578. x = x0;
  579. FXTextOut(x, y0 , "Name: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Name: ");
  580. FXTextOut(x, y0 , DinoInfo[dtype].Name, 0x0000BFBF);
  581. x = x0;
  582. FXTextOut(x, y0+16, "Weight: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Weight: ");
  583. if (OptSys)
  584. sprintf(t,"%3.2ft ", DinoInfo[dtype].Mass * scale * scale / 0.907);
  585. else
  586. sprintf(t,"%3.2fT ", DinoInfo[dtype].Mass * scale * scale);
  587. FXTextOut(x, y0+16, t, 0x0000BFBF); x+=GetTextW(hdcMain,t);
  588. FXTextOut(x, y0+16, "Length: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Length: ");
  589. if (OptSys)
  590. sprintf(t,"%3.2fft", DinoInfo[dtype].Length * scale / 0.3);
  591. else
  592. sprintf(t,"%3.2fm", DinoInfo[dtype].Length * scale);
  593. FXTextOut(x, y0+16, t, 0x0000BFBF);
  594. x = x0;
  595. FXTextOut(x, y0+32, "Weapon: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Weapon: ");
  596. wsprintf(t,"%s ", WeapInfo[wep].Name);
  597. FXTextOut(x, y0+32, t, 0x0000BFBF); x+=GetTextW(hdcMain,t);
  598. FXTextOut(x, y0+32, "Score: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Score: ");
  599. wsprintf(t,"%d", score);
  600. FXTextOut(x, y0+32, t, 0x0000BFBF);
  601. x = x0;
  602. FXTextOut(x, y0+48, "Range of kill: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Range of kill: ");
  603. if (OptSys) sprintf(t,"%3.1fft", range / 0.3);
  604. else sprintf(t,"%3.1fm", range);
  605. FXTextOut(x, y0+48, t, 0x0000BFBF);
  606. x = x0;
  607. FXTextOut(x, y0+64, "Date: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Date: ");
  608. if (OptSys)
  609. wsprintf(t,"%d.%d.%d ", ((date>>10) & 255), (date & 255), date>>20);
  610. else
  611. wsprintf(t,"%d.%d.%d ", (date & 255), ((date>>10) & 255), date>>20);
  612. FXTextOut(x, y0+64, t, 0x0000BFBF); x+=GetTextW(hdcMain,t);
  613. FXTextOut(x, y0+64, "Time: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Time: ");
  614. wsprintf(t,"%d:%02d", ((time>>10) & 255), (time & 255));
  615. FXTextOut(x, y0+64, t, 0x0000BFBF);
  616. SmallFont = FALSE;
  617. SelectObject(hdcMain, oldfont);
  618. }
  619. void Render_LifeInfo(int li)
  620. {
  621. int x,y;
  622. SmallFont = TRUE;
  623. HFONT oldfont = SelectObject(hdcMain, fnt_Small);
  624. int ctype = Characters[li].CType;
  625. float scale = Characters[li].scale;
  626. char t[32];
  627. x = VideoCX + WinW / 64;
  628. y = VideoCY + (int)(WinH / 6.8);
  629. FXTextOut(x, y, DinoInfo[ctype].Name, 0x0000b000);
  630. if (OptSys) sprintf(t,"Weight: %3.2ft ", DinoInfo[ctype].Mass * scale * scale / 0.907);
  631. else sprintf(t,"Weight: %3.2fT ", DinoInfo[ctype].Mass * scale * scale);
  632. FXTextOut(x, y+16, t, 0x0000b000);
  633. int R = (int)(VectorLength( SubVectors(Characters[li].pos, PlayerPos) )*3 / 64.f);
  634. if (OptSys) sprintf(t,"Distance: %dft ", R);
  635. else sprintf(t,"Distance: %dm ", R/3);
  636. FXTextOut(x, y+32, t, 0x0000b000);
  637. SmallFont = FALSE;
  638. SelectObject(hdcMain, oldfont);
  639. }
  640. void RenderFXMMap()
  641. {
  642. WORD fxmm[32][256];
  643. byte mused[1024];
  644. FillMemory(fxmm, sizeof(fxmm), 0);
  645. FillMemory(mused, sizeof(mused), 4);
  646. FillMemory(mused, (FXConstTendAddress+8)>>13, 0);
  647. for (int m=0; m<fxmemmapsize; m++) {
  648. if (!FxMemMap[m].cpuaddr) break;
  649. int b0 = FxMemMap[m].FXTbaseaddr >> 13;
  650. int bc = FxMemMap[m].size >> 13;
  651. bc+=b0;
  652. for (int mm=b0; mm<=bc; mm++)
  653. if (FxMemMap[m].lastused+2<FxMemUsageCount) mused[mm] = 1; else mused[mm] = 2;
  654. }
  655. WORD c;
  656. for (int y=0; y<8; y++)
  657. for (int x=0; x<64; x++) {
  658. if (mused[y*64+x]==0) c=16<<5; else
  659. if (mused[y*64+x]==4) c=8*0x421; else
  660. if (mused[y*64+x]==1) c=10<<10;
  661. else c=18<<10;
  662. for (int yy=y*4; yy<y*4+3; yy++)
  663. for (int xx=x*4; xx<x*4+3; xx++)
  664. fxmm[yy][xx] = c;
  665. }
  666. FXPutBitMap(16, WinH-46, 256, 32, 256, fxmm);
  667. }
  668. void ShowControlElements()
  669. {
  670. //if (DEBUG) RenderFXMMap();
  671. char buf[128];
  672. if (TIMER) {
  673. wsprintf(buf,"msc: %d", TimeDt);
  674. FXTextOut(WinEX-81, 11, buf, 0x0020A0A0);
  675. wsprintf(buf,"polys: %d", dFacesCount);
  676. FXTextOut(WinEX-90, 24, buf, 0x0020A0A0);
  677. }
  678. if (MessageList.timeleft) {
  679. if (RealTime>MessageList.timeleft) MessageList.timeleft = 0;
  680. FXTextOut(10, 10, MessageList.mtext, 0x0020A0A0);
  681. }
  682. if (ExitTime) {
  683. int y = WinH / 3;
  684. wsprintf(buf,"Preparing for evacuation...");
  685. FXTextOut(VideoCX - GetTextW(hdcCMain, buf)/2, y, buf, 0x0060C0D0);
  686. wsprintf(buf,"%d seconds left.", 1 + ExitTime / 1000);
  687. FXTextOut(VideoCX - GetTextW(hdcCMain, buf)/2, y + 18, buf, 0x0060C0D0);
  688. }
  689. }
  690. void ClipVector(CLIPPLANE& C, int vn)
  691. {
  692. int ClipRes = 0;
  693. float s,s1,s2;
  694. int vleft = (vn-1); if (vleft <0) vleft=vused-1;
  695. int vright = (vn+1); if (vright>=vused) vright=0;
  696. MulVectorsScal(cp[vn].ev.v, C.nv, s); /*s=SGN(s-0.01f);*/
  697. if (s>=0) return;
  698. MulVectorsScal(cp[vleft ].ev.v, C.nv, s1); /* s1=SGN(s1+0.01f); */ //s1+=0.0001f;
  699. MulVectorsScal(cp[vright].ev.v, C.nv, s2); /* s2=SGN(s2+0.01f); */ //s2+=0.0001f;
  700. if (s1>0) {
  701. ClipRes+=1;
  702. /*
  703. CalcHitPoint(C,cp[vn].ev.v,
  704. cp[vleft].ev.v, hleft.ev.v);
  705. float ll = VectorLength(SubVectors(cp[vleft].ev.v, cp[vn].ev.v));
  706. float lc = VectorLength(SubVectors(hleft.ev.v, cp[vn].ev.v));
  707. lc = lc / ll;
  708. */
  709. float lc = -s / (s1-s);
  710. hleft.ev.v.x = cp[vn].ev.v.x + ((cp[vleft].ev.v.x - cp[vn].ev.v.x) * lc);
  711. hleft.ev.v.y = cp[vn].ev.v.y + ((cp[vleft].ev.v.y - cp[vn].ev.v.y) * lc);
  712. hleft.ev.v.z = cp[vn].ev.v.z + ((cp[vleft].ev.v.z - cp[vn].ev.v.z) * lc);
  713. hleft.tx = cp[vn].tx + ((cp[vleft].tx - cp[vn].tx) * lc);
  714. hleft.ty = cp[vn].ty + ((cp[vleft].ty - cp[vn].ty) * lc);
  715. hleft.ev.Light = cp[vn].ev.Light + (int)((cp[vleft].ev.Light - cp[vn].ev.Light) * lc);
  716. hleft.ev.ALPHA = cp[vn].ev.ALPHA + (int)((cp[vleft].ev.ALPHA - cp[vn].ev.ALPHA) * lc);
  717. hleft.ev.Fog = cp[vn].ev.Fog + (int)((cp[vleft].ev.Fog - cp[vn].ev.Fog) * lc);
  718. }
  719. if (s2>0) {
  720. ClipRes+=2;
  721. /*
  722. CalcHitPoint(C,cp[vn].ev.v,
  723. cp[vright].ev.v, hright.ev.v);
  724. float ll = VectorLength(SubVectors(cp[vright].ev.v, cp[vn].ev.v));
  725. float lc = VectorLength(SubVectors(hright.ev.v, cp[vn].ev.v));
  726. lc = lc / ll;
  727. */
  728. float lc = -s / (s2-s);
  729. hright.ev.v.x = cp[vn].ev.v.x + ((cp[vright].ev.v.x - cp[vn].ev.v.x) * lc);
  730. hright.ev.v.y = cp[vn].ev.v.y + ((cp[vright].ev.v.y - cp[vn].ev.v.y) * lc);
  731. hright.ev.v.z = cp[vn].ev.v.z + ((cp[vright].ev.v.z - cp[vn].ev.v.z) * lc);
  732. hright.tx = cp[vn].tx + ((cp[vright].tx - cp[vn].tx) * lc);
  733. hright.ty = cp[vn].ty + ((cp[vright].ty - cp[vn].ty) * lc);
  734. hright.ev.Light = cp[vn].ev.Light + (int)((cp[vright].ev.Light - cp[vn].ev.Light) * lc);
  735. hright.ev.ALPHA = cp[vn].ev.ALPHA + (int)((cp[vright].ev.ALPHA - cp[vn].ev.ALPHA) * lc);
  736. hright.ev.Fog = cp[vn].ev.Fog + (int)((cp[vright].ev.Fog - cp[vn].ev.Fog) * lc);
  737. }
  738. if (ClipRes == 0) {
  739. u--; vused--;
  740. cp[vn] = cp[vn+1];
  741. cp[vn+1] = cp[vn+2];
  742. cp[vn+2] = cp[vn+3];
  743. cp[vn+3] = cp[vn+4];
  744. cp[vn+4] = cp[vn+5];
  745. cp[vn+5] = cp[vn+6];
  746. //memcpy(&cp[vn], &cp[vn+1], (15-vn)*sizeof(ClipPoint));
  747. }
  748. if (ClipRes == 1) {cp[vn] = hleft; }
  749. if (ClipRes == 2) {cp[vn] = hright;}
  750. if (ClipRes == 3) {
  751. u++; vused++;
  752. //memcpy(&cp[vn+1], &cp[vn], (15-vn)*sizeof(ClipPoint));
  753. cp[vn+6] = cp[vn+5];
  754. cp[vn+5] = cp[vn+4];
  755. cp[vn+4] = cp[vn+3];
  756. cp[vn+3] = cp[vn+2];
  757. cp[vn+2] = cp[vn+1];
  758. cp[vn+1] = cp[vn];
  759. cp[vn] = hleft;
  760. cp[vn+1] = hright;
  761. }
  762. }
  763. void _fillmapping(BOOL SECOND)
  764. {
  765. if (ReverseOn)
  766. if (SECOND) {
  767. switch (TDirection) {
  768. case 0:
  769. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  770. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  771. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  772. break;
  773. case 1:
  774. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  775. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  776. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  777. break;
  778. case 2:
  779. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  780. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  781. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  782. break;
  783. case 3:
  784. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  785. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  786. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  787. break;
  788. }
  789. } else {
  790. switch (TDirection) {
  791. case 0:
  792. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  793. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  794. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  795. break;
  796. case 1:
  797. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  798. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  799. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  800. break;
  801. case 2:
  802. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  803. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  804. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  805. break;
  806. case 3:
  807. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  808. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  809. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  810. break;
  811. }
  812. }
  813. else
  814. if (SECOND) {
  815. switch (TDirection) {
  816. case 0:
  817. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  818. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  819. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  820. break;
  821. case 1:
  822. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  823. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  824. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  825. break;
  826. case 2:
  827. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  828. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  829. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  830. break;
  831. case 3:
  832. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  833. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  834. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  835. break;
  836. }
  837. } else {
  838. switch (TDirection) {
  839. case 0:
  840. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  841. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  842. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  843. break;
  844. case 1:
  845. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  846. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  847. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  848. break;
  849. case 2:
  850. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  851. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  852. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  853. break;
  854. case 3:
  855. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  856. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  857. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  858. break;
  859. }
  860. }
  861. }
  862. void _fillmappingclip(BOOL SECOND)
  863. {
  864. if (ReverseOn)
  865. if (SECOND) {
  866. switch (TDirection) {
  867. case 0:
  868. cp[0].tx = TCMIN; cp[0].ty = TCMAX;
  869. cp[1].tx = TCMAX; cp[1].ty = TCMIN;
  870. cp[2].tx = TCMAX; cp[2].ty = TCMAX;
  871. break;
  872. case 1:
  873. cp[0].tx = TCMAX; cp[0].ty = TCMAX;
  874. cp[1].tx = TCMIN; cp[1].ty = TCMIN;
  875. cp[2].tx = TCMAX; cp[2].ty = TCMIN;
  876. break;
  877. case 2:
  878. cp[0].tx = TCMAX; cp[0].ty = TCMIN;
  879. cp[1].tx = TCMIN; cp[1].ty = TCMAX;
  880. cp[2].tx = TCMIN; cp[2].ty = TCMIN;
  881. break;
  882. case 3:
  883. cp[0].tx = TCMIN; cp[0].ty = TCMIN;
  884. cp[1].tx = TCMAX; cp[1].ty = TCMAX;
  885. cp[2].tx = TCMIN; cp[2].ty = TCMAX;
  886. break;
  887. }
  888. } else {
  889. switch (TDirection) {
  890. case 0:
  891. cp[0].tx = TCMIN; cp[0].ty = TCMIN;
  892. cp[1].tx = TCMAX; cp[1].ty = TCMIN;
  893. cp[2].tx = TCMIN; cp[2].ty = TCMAX;
  894. break;
  895. case 1:
  896. cp[0].tx = TCMIN; cp[0].ty = TCMAX;
  897. cp[1].tx = TCMIN; cp[1].ty = TCMIN;
  898. cp[2].tx = TCMAX; cp[2].ty = TCMAX;
  899. break;
  900. case 2:
  901. cp[0].tx = TCMAX; cp[0].ty = TCMAX;
  902. cp[1].tx = TCMIN; cp[1].ty = TCMAX;
  903. cp[2].tx = TCMAX; cp[2].ty = TCMIN;
  904. break;
  905. case 3:
  906. cp[0].tx = TCMAX; cp[0].ty = TCMIN;
  907. cp[1].tx = TCMAX; cp[1].ty = TCMAX;
  908. cp[2].tx = TCMIN; cp[2].ty = TCMIN;
  909. break;
  910. }
  911. }
  912. else
  913. if (SECOND) {
  914. switch (TDirection) {
  915. case 0:
  916. cp[0].tx = TCMIN; cp[0].ty = TCMIN;
  917. cp[1].tx = TCMAX; cp[1].ty = TCMAX;
  918. cp[2].tx = TCMIN; cp[2].ty = TCMAX;
  919. break;
  920. case 1:
  921. cp[0].tx = TCMIN; cp[0].ty = TCMAX;
  922. cp[1].tx = TCMAX; cp[1].ty = TCMIN;
  923. cp[2].tx = TCMAX; cp[2].ty = TCMAX;
  924. break;
  925. case 2:
  926. cp[0].tx = TCMAX; cp[0].ty = TCMAX;
  927. cp[1].tx = TCMIN; cp[1].ty = TCMIN;
  928. cp[2].tx = TCMAX; cp[2].ty = TCMIN;
  929. break;
  930. case 3:
  931. cp[0].tx = TCMAX; cp[0].ty = TCMIN;
  932. cp[1].tx = TCMIN; cp[1].ty = TCMAX;
  933. cp[2].tx = TCMIN; cp[2].ty = TCMIN;
  934. break;
  935. }
  936. } else {
  937. switch (TDirection) {
  938. case 0:
  939. cp[0].tx = TCMIN; cp[0].ty = TCMIN;
  940. cp[1].tx = TCMAX; cp[1].ty = TCMIN;
  941. cp[2].tx = TCMAX; cp[2].ty = TCMAX;
  942. break;
  943. case 1:
  944. cp[0].tx = TCMIN; cp[0].ty = TCMAX;
  945. cp[1].tx = TCMIN; cp[1].ty = TCMIN;
  946. cp[2].tx = TCMAX; cp[2].ty = TCMIN;
  947. break;
  948. case 2:
  949. cp[0].tx = TCMAX; cp[0].ty = TCMAX;
  950. cp[1].tx = TCMIN; cp[1].ty = TCMAX;
  951. cp[2].tx = TCMIN; cp[2].ty = TCMIN;
  952. break;
  953. case 3:
  954. cp[0].tx = TCMAX; cp[0].ty = TCMIN;
  955. cp[1].tx = TCMAX; cp[1].ty = TCMAX;
  956. cp[2].tx = TCMIN; cp[2].ty = TCMAX;
  957. break;
  958. }
  959. }
  960. }
  961. void DrawTPlaneClip(BOOL SECOND)
  962. {
  963. MulVectorsVect(SubVectors(ev[1].v, ev[0].v), SubVectors(ev[2].v, ev[0].v), nv);
  964. if (nv.x*ev[0].v.x + nv.y*ev[0].v.y + nv.z*ev[0].v.z<0) return;
  965. cp[0].ev = ev[0]; cp[1].ev = ev[1]; cp[2].ev = ev[2];
  966. _fillmappingclip(SECOND);
  967. vused = 3;
  968. for (u=0; u<vused; u++) cp[u].ev.v.z+=12.0f;
  969. for (u=0; u<vused; u++) ClipVector(ClipZ,u);
  970. for (u=0; u<vused; u++) cp[u].ev.v.z-=12.0f;
  971. if (vused<3) return;
  972. for (u=0; u<vused; u++) ClipVector(ClipA,u); if (vused<3) return;
  973. for (u=0; u<vused; u++) ClipVector(ClipB,u); if (vused<3) return;
  974. for (u=0; u<vused; u++) ClipVector(ClipC,u); if (vused<3) return;
  975. for (u=0; u<vused; u++) ClipVector(ClipD,u); if (vused<3) return;
  976. for (u=0; u<vused; u++) {
  977. cp[u].ev.scrx = (VideoCX)*16 - (int)(cp[u].ev.v.x*16 / cp[u].ev.v.z * CameraW);
  978. cp[u].ev.scry = (VideoCY)*16 + (int)(cp[u].ev.v.y*16 / cp[u].ev.v.z * CameraH);
  979. }
  980. gvtx[0].a = (float)cp[0].ev.Fog;
  981. gvtx[0].x = (float)cp[0].ev.scrx/16.f;
  982. gvtx[0].y = (float)cp[0].ev.scry/16.f;
  983. gvtx[0].z = (float)-cp[0].ev.v.z;
  984. gvtx[0].r = (float)(cp[0].ev.Light); gvtx[0].g = gvtx[0].r; gvtx[0].b = gvtx[0].r;
  985. gvtx[0].ooz = (_ZSCALE/gvtx[0].z);
  986. gvtx[0].oow = 1.0f/gvtx[0].z;
  987. gvtx[0].tmuvtx[0].sow = (float)cp[0].tx * gvtx[0].oow;
  988. gvtx[0].tmuvtx[0].tow = (float)cp[0].ty * gvtx[0].oow;
  989. for (u=0; u<vused-2; u++) {
  990. gvtx[1].a = (float)cp[1+u].ev.Fog;
  991. gvtx[1].x = (float)cp[1+u].ev.scrx/16.f;
  992. gvtx[1].y = (float)cp[1+u].ev.scry/16.f;
  993. gvtx[1].z = -cp[1+u].ev.v.z;
  994. gvtx[1].r = (float)(cp[1+u].ev.Light); gvtx[1].g = gvtx[1].r; gvtx[1].b = gvtx[1].r;
  995. gvtx[1].ooz = (_ZSCALE/gvtx[1].z);
  996. gvtx[1].oow = 1.0f/gvtx[1].z;
  997. gvtx[1].tmuvtx[0].sow = (float)cp[1+u].tx * gvtx[1].oow;
  998. gvtx[1].tmuvtx[0].tow = (float)cp[1+u].ty * gvtx[1].oow;
  999. gvtx[2].a = (float)cp[2+u].ev.Fog;
  1000. gvtx[2].x = (float)cp[2+u].ev.scrx/16.f;
  1001. gvtx[2].y = (float)cp[2+u].ev.scry/16.f;
  1002. gvtx[2].z = -cp[2+u].ev.v.z;
  1003. gvtx[2].r = (float)(cp[2+u].ev.Light); gvtx[2].g = gvtx[2].r; gvtx[2].b = gvtx[2].r;
  1004. gvtx[2].ooz = (_ZSCALE/gvtx[2].z);
  1005. gvtx[2].oow = 1.0f/gvtx[2].z;
  1006. gvtx[2].tmuvtx[0].sow = (float)cp[2+u].tx * gvtx[2].oow;
  1007. gvtx[2].tmuvtx[0].tow = (float)cp[2+u].ty * gvtx[2].oow;
  1008. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  1009. dFacesCount++;
  1010. }
  1011. }
  1012. void DrawTPlaneClipW(BOOL SECOND)
  1013. {
  1014. cp[0].ev = ev[0]; cp[1].ev = ev[1]; cp[2].ev = ev[2];
  1015. _fillmappingclip(SECOND);
  1016. vused = 3;
  1017. for (u=0; u<vused; u++) cp[u].ev.v.z+=12.0f;
  1018. for (u=0; u<vused; u++) ClipVector(ClipZ,u);
  1019. for (u=0; u<vused; u++) cp[u].ev.v.z-=12.0f;
  1020. if (vused<3) return;
  1021. for (u=0; u<vused; u++) ClipVector(ClipA,u); if (vused<3) return;
  1022. for (u=0; u<vused; u++) ClipVector(ClipB,u); if (vused<3) return;
  1023. for (u=0; u<vused; u++) ClipVector(ClipC,u); if (vused<3) return;
  1024. for (u=0; u<vused; u++) ClipVector(ClipD,u); if (vused<3) return;
  1025. for (u=0; u<vused; u++) {
  1026. cp[u].ev.scrx = (VideoCX)*16 - (int)(cp[u].ev.v.x*16 / cp[u].ev.v.z * CameraW);
  1027. cp[u].ev.scry = (VideoCY)*16 + (int)(cp[u].ev.v.y*16 / cp[u].ev.v.z * CameraH);
  1028. }
  1029. gvtx[0].a = (float)cp[0].ev.ALPHA;
  1030. gvtx[0].x = (float)cp[0].ev.scrx/16.f;
  1031. gvtx[0].y = (float)cp[0].ev.scry/16.f;
  1032. gvtx[0].z = (float)-cp[0].ev.v.z;
  1033. gvtx[0].r = (float)(cp[0].ev.Light); gvtx[0].g = gvtx[0].r; gvtx[0].b = gvtx[0].r;
  1034. gvtx[0].ooz = (_ZSCALE/gvtx[0].z);
  1035. gvtx[0].oow = 1.0f/gvtx[0].z;
  1036. gvtx[0].tmuvtx[0].sow = (float)cp[0].tx * gvtx[0].oow;
  1037. gvtx[0].tmuvtx[0].tow = (float)cp[0].ty * gvtx[0].oow;
  1038. for (u=0; u<vused-2; u++) {
  1039. gvtx[1].a = (float)cp[1+u].ev.ALPHA;
  1040. gvtx[1].x = (float)cp[1+u].ev.scrx/16.f;
  1041. gvtx[1].y = (float)cp[1+u].ev.scry/16.f;
  1042. gvtx[1].z = -cp[1+u].ev.v.z;
  1043. gvtx[1].r = (float)(cp[1+u].ev.Light); gvtx[1].g = gvtx[1].r; gvtx[1].b = gvtx[1].r;
  1044. gvtx[1].ooz = (_ZSCALE/gvtx[1].z);
  1045. gvtx[1].oow = 1.0f/gvtx[1].z;
  1046. gvtx[1].tmuvtx[0].sow = (float)cp[1+u].tx * gvtx[1].oow;
  1047. gvtx[1].tmuvtx[0].tow = (float)cp[1+u].ty * gvtx[1].oow;
  1048. gvtx[2].a = (float)cp[2+u].ev.ALPHA;
  1049. gvtx[2].x = (float)cp[2+u].ev.scrx/16.f;
  1050. gvtx[2].y = (float)cp[2+u].ev.scry/16.f;
  1051. gvtx[2].z = -cp[2+u].ev.v.z;
  1052. gvtx[2].r = (float)(cp[2+u].ev.Light); gvtx[2].g = gvtx[2].r; gvtx[2].b = gvtx[2].r;
  1053. gvtx[2].ooz = (_ZSCALE/gvtx[2].z);
  1054. gvtx[2].oow = 1.0f/gvtx[2].z;
  1055. gvtx[2].tmuvtx[0].sow = (float)cp[2+u].tx * gvtx[2].oow;
  1056. gvtx[2].tmuvtx[0].tow = (float)cp[2+u].ty * gvtx[2].oow;
  1057. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  1058. dFacesCount++;
  1059. }
  1060. }
  1061. void DrawTPlane(BOOL SECOND)
  1062. {
  1063. int n;
  1064. BOOL SecondPass = FALSE;
  1065. MulVectorsVect(SubVectors(ev[1].v, ev[0].v), SubVectors(ev[2].v, ev[0].v), nv);
  1066. if (nv.x*ev[0].v.x + nv.y*ev[0].v.y + nv.z*ev[0].v.z<0) return;
  1067. Mask1=0x007F;
  1068. for (n=0; n<3; n++) {
  1069. if (ev[n].DFlags & 128) return;
  1070. Mask1=Mask1 & ev[n].DFlags; }
  1071. if (Mask1>0) return;
  1072. _fillmapping(SECOND);
  1073. gvtx[0].a = (float)ev[0].Fog;
  1074. gvtx[1].a = (float)ev[1].Fog;
  1075. gvtx[2].a = (float)ev[2].Fog;
  1076. if (zs > (ctViewR-8)<<8) {
  1077. guAlphaSource(GR_ALPHASOURCE_ITERATED_ALPHA);
  1078. grFogMode(GR_FOG_DISABLE);
  1079. SecondPass = TRUE;
  1080. float zz;
  1081. zz = VectorLength(ev[0].v) - 256 * (ctViewR-4);
  1082. if (zz > 0) gvtx[0].a = max(0.f,255.f - zz / 3.f); else gvtx[0].a = 255.f;
  1083. zz = VectorLength(ev[1].v) - 256 * (ctViewR-4);
  1084. if (zz > 0) gvtx[1].a = max(0.f, 255.f - zz / 3.f); else gvtx[1].a = 255.f;
  1085. zz = VectorLength(ev[2].v) - 256 * (ctViewR-4);
  1086. if (zz > 0) gvtx[2].a = max(0,255.f - zz / 3.f); else gvtx[2].a = 255.f;
  1087. }
  1088. gvtx[0].x = (float)ev[0].scrx/16.f;
  1089. gvtx[0].y = (float)ev[0].scry/16.f;
  1090. gvtx[0].z = -ev[0].v.z;
  1091. gvtx[0].r = (float)(ev[0].Light); gvtx[0].g = gvtx[0].r; gvtx[0].b = gvtx[0].r;
  1092. gvtx[0].ooz = (_ZSCALE/gvtx[0].z);
  1093. gvtx[0].oow = 1.0f/gvtx[0].z;
  1094. gvtx[0].tmuvtx[0].sow = (float)scrp[0].tx * gvtx[0].oow;
  1095. gvtx[0].tmuvtx[0].tow = (float)scrp[0].ty * gvtx[0].oow;
  1096. gvtx[1].x = (float)ev[1].scrx/16.f;
  1097. gvtx[1].y = (float)ev[1].scry/16.f;
  1098. gvtx[1].z = -ev[1].v.z;
  1099. gvtx[1].r = (float)(ev[1].Light); gvtx[1].g = gvtx[1].r; gvtx[1].b = gvtx[1].r;
  1100. gvtx[1].ooz = (_ZSCALE/gvtx[1].z);
  1101. gvtx[1].oow = 1.0f/gvtx[1].z;
  1102. gvtx[1].tmuvtx[0].sow = (float)scrp[1].tx * gvtx[1].oow;
  1103. gvtx[1].tmuvtx[0].tow = (float)scrp[1].ty * gvtx[1].oow;
  1104. gvtx[2].x = (float)ev[2].scrx/16.f;
  1105. gvtx[2].y = (float)ev[2].scry/16.f;
  1106. gvtx[2].z = -ev[2].v.z;
  1107. gvtx[2].r = (float)(ev[2].Light); gvtx[2].g = gvtx[2].r; gvtx[2].b = gvtx[2].r;
  1108. gvtx[2].ooz = (_ZSCALE/gvtx[2].z);
  1109. gvtx[2].oow = 1.0f/gvtx[2].z;
  1110. gvtx[2].tmuvtx[0].sow = (float)scrp[2].tx * gvtx[2].oow;
  1111. gvtx[2].tmuvtx[0].tow = (float)scrp[2].ty * gvtx[2].oow;
  1112. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  1113. dFacesCount++;
  1114. if (SecondPass) {
  1115. if (FOGON) {
  1116. gvtx[0].a*= min(1.f, ev[0].Fog / 255.f);
  1117. gvtx[1].a*= min(1.f, ev[1].Fog / 255.f);
  1118. gvtx[2].a*= min(1.f, ev[2].Fog / 255.f);
  1119. guColorCombineFunction(GR_COLORCOMBINE_CCRGB);
  1120. grConstantColorValue(CurFogColor);
  1121. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  1122. grFogMode(GR_FOG_WITH_ITERATED_ALPHA);
  1123. }
  1124. guAlphaSource(GR_ALPHASOURCE_CC_ALPHA);
  1125. grConstantColorValue(0xFF000000);
  1126. guColorCombineFunction( GR_COLORCOMBINE_TEXTURE_TIMES_ITRGB );
  1127. }
  1128. }
  1129. void DrawTPlaneW(BOOL SECOND)
  1130. {
  1131. int n;
  1132. BOOL SecondPass = FALSE;
  1133. Mask1=0x007F;
  1134. for (n=0; n<3; n++) {
  1135. if (ev[n].DFlags & 128) return;
  1136. Mask1=Mask1 & ev[n].DFlags; }
  1137. if (Mask1>0) return;
  1138. _fillmapping(SECOND);
  1139. if (!UNDERWATER)
  1140. if (zs > (ctViewR-8)<<8) {
  1141. float zz;
  1142. zz = VectorLength(ev[0].v) - 256 * (ctViewR-4);
  1143. if (zz > 0) ev[0].ALPHA = max(0.f,255.f - zz / 3.f);
  1144. zz = VectorLength(ev[1].v) - 256 * (ctViewR-4);
  1145. if (zz > 0) ev[1].ALPHA = max(0.f, 255.f - zz / 3.f);
  1146. zz = VectorLength(ev[2].v) - 256 * (ctViewR-4);
  1147. if (zz > 0) ev[2].ALPHA = max(0,255.f - zz / 3.f);
  1148. }
  1149. gvtx[0].x = (float)ev[0].scrx/16.f;
  1150. gvtx[0].y = (float)ev[0].scry/16.f;
  1151. gvtx[0].z = -ev[0].v.z;
  1152. gvtx[0].r = (float)(ev[0].Light); gvtx[0].g = gvtx[0].r; gvtx[0].b = gvtx[0].r;
  1153. gvtx[0].a = (float)ev[0].ALPHA;
  1154. gvtx[0].ooz = (_ZSCALE/gvtx[0].z);
  1155. gvtx[0].oow = 1.0f/gvtx[0].z;
  1156. gvtx[0].tmuvtx[0].sow = (float)scrp[0].tx * gvtx[0].oow;
  1157. gvtx[0].tmuvtx[0].tow = (float)scrp[0].ty * gvtx[0].oow;
  1158. gvtx[1].x = (float)ev[1].scrx/16.f;
  1159. gvtx[1].y = (float)ev[1].scry/16.f;
  1160. gvtx[1].z = -ev[1].v.z;
  1161. gvtx[1].r = (float)(ev[1].Light); gvtx[1].g = gvtx[1].r; gvtx[1].b = gvtx[1].r;
  1162. gvtx[1].a = (float)ev[1].ALPHA;
  1163. gvtx[1].ooz = (_ZSCALE/gvtx[1].z);
  1164. gvtx[1].oow = 1.0f/gvtx[1].z;
  1165. gvtx[1].tmuvtx[0].sow = (float)scrp[1].tx * gvtx[1].oow;
  1166. gvtx[1].tmuvtx[0].tow = (float)scrp[1].ty * gvtx[1].oow;
  1167. gvtx[2].x = (float)ev[2].scrx/16.f;
  1168. gvtx[2].y = (float)ev[2].scry/16.f;
  1169. gvtx[2].z = -ev[2].v.z;
  1170. gvtx[2].r = (float)(ev[2].Light); gvtx[2].g = gvtx[2].r; gvtx[2].b = gvtx[2].r;
  1171. gvtx[2].a = (float)ev[2].ALPHA;
  1172. gvtx[2].ooz = (_ZSCALE/gvtx[2].z);
  1173. gvtx[2].oow = 1.0f/gvtx[2].z;
  1174. gvtx[2].tmuvtx[0].sow = (float)scrp[2].tx * gvtx[2].oow;
  1175. gvtx[2].tmuvtx[0].tow = (float)scrp[2].ty * gvtx[2].oow;
  1176. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  1177. dFacesCount++;
  1178. }
  1179. void RenderObject(int x, int y)
  1180. {
  1181. if (OMap[y][x]==255) return;
  1182. if (!MODELS) return;
  1183. if (ORLCount>2000) return;
  1184. ORList[ORLCount].x = x;
  1185. ORList[ORLCount].y = y;
  1186. ORLCount++;
  1187. }
  1188. void _RenderObject(int x, int y)
  1189. {
  1190. int ob = OMap[y][x];
  1191. if (!MObjects[ob].model) {
  1192. //return;
  1193. wsprintf(logt,"Incorrect model at [%d][%d]!", x, y);
  1194. DoHalt(logt);
  1195. }
  1196. int FI = (FMap[y][x] >> 2) & 3;
  1197. float fi = CameraAlpha + (float)(FI * 2.f*pi / 4.f);
  1198. int mlight;
  1199. if (MObjects[ob].info.flags & ofDEFLIGHT)
  1200. mlight = MObjects[ob].info.DefLight; else
  1201. if (MObjects[ob].info.flags & ofGRNDLIGHT)
  1202. {
  1203. mlight = 128;
  1204. CalcModelGroundLight(MObjects[ob].model, x*256+128, y*256+128, FI);
  1205. FI = 0;
  1206. }
  1207. else
  1208. mlight = -(RandomMap[y & 31][x & 31] >> 5) + (LMap[y][x]>>1) + 96;
  1209. if (mlight >192) mlight =192;
  1210. if (mlight < 64) mlight = 64;
  1211. v[0].x = x*256+128 - CameraX;
  1212. v[0].z = y*256+128 - CameraZ;
  1213. v[0].y = (float)(HMapO[y][x]) * ctHScale - CameraY;
  1214. float zs = VectorLength(v[0]);
  1215. //if (v[0].y + MObjects[ob].info.YHi < (int)(HMap[y][x]+HMap[y+1][x+1]) / 2 * ctHScale - CameraY) return;
  1216. CalcFogLevel_Gradient(v[0]);
  1217. gvtx[0].a = FogYBase;
  1218. gvtx[1].a = gvtx[0].a;
  1219. gvtx[2].a = gvtx[0].a;
  1220. v[0] = RotateVector(v[0]);
  1221. GlassL = 0;
  1222. if (zs > 256 * (ctViewR-8))
  1223. GlassL=min(255,(zs - 256 * (ctViewR-8)) / 4);
  1224. if (GlassL==255) return;
  1225. grConstantColorValue( (255-GlassL) << 24);
  1226. if (MObjects[ob].info.flags & ofANIMATED)
  1227. if (MObjects[ob].info.LastAniTime!=RealTime) {
  1228. MObjects[ob].info.LastAniTime=RealTime;
  1229. CreateMorphedObject(MObjects[ob].model,
  1230. MObjects[ob].vtl,
  1231. RealTime % MObjects[ob].vtl.AniTime);
  1232. }
  1233. if (MObjects[ob].info.flags & ofNOBMP) zs = 0;
  1234. /*
  1235. if (!NeedWater && zs>ctViewRM*256 && zs<(ctViewRM+1)*256) {
  1236. GlassL = zs - ctViewRM*256;
  1237. grConstantColorValue(GlassL << 24);
  1238. RenderBMPModel(&MObjects[ob].bmpmodel, v[0].x, v[0].y, v[0].z, mlight-32);
  1239. grConstantColorValue( (255-GlassL) << 24);
  1240. RenderModel(MObjects[ob].model, v[0].x, v[0].y, v[0].z, mlight, FI, fi, CameraBeta);
  1241. } else */
  1242. if (zs>ctViewRM*256)
  1243. RenderBMPModel(&MObjects[ob].bmpmodel, v[0].x, v[0].y, v[0].z, mlight-16);
  1244. else
  1245. if (v[0].z<-256*12)
  1246. RenderModel(MObjects[ob].model, v[0].x, v[0].y, v[0].z, mlight, FI, fi, CameraBeta);
  1247. else
  1248. RenderModelClip(MObjects[ob].model, v[0].x, v[0].y, v[0].z, mlight, FI, fi, CameraBeta);
  1249. }
  1250. void ProcessMapW(int x, int y, int r)
  1251. {
  1252. if (!( (FMap[y ][x ] & fmWaterA) &&
  1253. (FMap[y ][x+1] & fmWaterA) &&
  1254. (FMap[y+1][x ] & fmWaterA) &&
  1255. (FMap[y+1][x+1] & fmWaterA) )) return;
  1256. int t1 = WaterList[ WMap[y][x] ].tindex;
  1257. ev[0] = VMap2[y-CCY+128][x-CCX+128];
  1258. if (ev[0].v.z>BackViewR) return;
  1259. ReverseOn = FALSE;
  1260. TDirection = 0;
  1261. x = x - CCX + 128;
  1262. y = y - CCY + 128;
  1263. ev[1] = VMap2[y][x+1];
  1264. ev[2] = VMap2[y+1][x+1];
  1265. float xx = (ev[0].v.x + VMap2[y+1][x+1].v.x) / 2;
  1266. float yy = (ev[0].v.y + VMap2[y+1][x+1].v.y) / 2;
  1267. float zz = (ev[0].v.z + VMap2[y+1][x+1].v.z) / 2;
  1268. if ( fabs(xx*FOVK) > -zz + BackViewR) return;
  1269. zs = (int)sqrt( xx*xx + zz*zz + yy*yy);
  1270. if (zs > ctViewR*256) return;
  1271. if (MIPMAP && (zs > 256 * 10 && t1 || LOWRESTX)) SetFXTexture(Textures[t1]->DataB, 64, 64);
  1272. else SetFXTexture(Textures[t1]->DataA, 128, 128);
  1273. if (r>8) DrawTPlaneW(FALSE);
  1274. else DrawTPlaneClipW(FALSE);
  1275. ev[1] = ev[2]; ev[2] = VMap2[y+1][x];
  1276. if (r>8) DrawTPlaneW(TRUE);
  1277. else DrawTPlaneClipW(TRUE);
  1278. }
  1279. void ProcessMapW2(int x, int y, int r)
  1280. {
  1281. if (!( (FMap[y ][x ] & fmWaterA) &&
  1282. (FMap[y ][x+2] & fmWaterA) &&
  1283. (FMap[y+2][x ] & fmWaterA) &&
  1284. (FMap[y+2][x+2] & fmWaterA) )) return;
  1285. int t1 = WaterList[ WMap[y][x] ].tindex;
  1286. ev[0] = VMap2[y-CCY+128][x-CCX+128];
  1287. if (ev[0].v.z>BackViewR) return;
  1288. ReverseOn = FALSE;
  1289. TDirection = 0;
  1290. x = x - CCX + 128;
  1291. y = y - CCY + 128;
  1292. ev[1] = VMap2[y][x+2];
  1293. ev[2] = VMap2[y+2][x+2];
  1294. float xx = (ev[0].v.x + VMap2[y+2][x+2].v.x) / 2;
  1295. float yy = (ev[0].v.y + VMap2[y+2][x+2].v.y) / 2;
  1296. float zz = (ev[0].v.z + VMap2[y+2][x+2].v.z) / 2;
  1297. if ( fabs(xx*FOVK) > -zz + BackViewR) return;
  1298. zs = (int)sqrt( xx*xx + zz*zz + yy*yy);
  1299. if (zs > ctViewR*256) return;
  1300. SetFXTexture(Textures[t1]->DataB, 64, 64);
  1301. DrawTPlaneW(FALSE);
  1302. ev[1] = ev[2]; ev[2] = VMap2[y+2][x];
  1303. DrawTPlaneW(TRUE);
  1304. }
  1305. void RenderGround()
  1306. {
  1307. for (r=ctViewR; r>=ctViewR1; r-=2) {
  1308. for (int x=r; x>0; x-=2) {
  1309. ProcessMap2(CCX-x, CCY+r, r);
  1310. ProcessMap2(CCX+x, CCY+r, r);
  1311. ProcessMap2(CCX-x, CCY-r, r);
  1312. ProcessMap2(CCX+x, CCY-r, r);
  1313. }
  1314. ProcessMap2(CCX, CCY-r, r);
  1315. ProcessMap2(CCX, CCY+r, r);
  1316. for (int y=r-2; y>0; y-=2) {
  1317. ProcessMap2(CCX+r, CCY-y, r);
  1318. ProcessMap2(CCX+r, CCY+y, r);
  1319. ProcessMap2(CCX-r, CCY+y, r);
  1320. ProcessMap2(CCX-r, CCY-y, r);
  1321. }
  1322. ProcessMap2(CCX-r, CCY, r);
  1323. ProcessMap2(CCX+r, CCY, r);
  1324. }
  1325. r = ctViewR1-1;
  1326. for (int x=r; x>-r; x--) {
  1327. ProcessMap(CCX+r, CCY+x, r);
  1328. ProcessMap(CCX+x, CCY+r, r);
  1329. }
  1330. for (r=ctViewR1-2; r>0; r--) {
  1331. for (int x=r; x>0; x--) {
  1332. ProcessMap(CCX-x, CCY+r, r);
  1333. ProcessMap(CCX+x, CCY+r, r);
  1334. ProcessMap(CCX-x, CCY-r, r);
  1335. ProcessMap(CCX+x, CCY-r, r);
  1336. }
  1337. ProcessMap(CCX, CCY-r, r);
  1338. ProcessMap(CCX, CCY+r, r);
  1339. for (int y=r-1; y>0; y--) {
  1340. ProcessMap(CCX+r, CCY-y, r);
  1341. ProcessMap(CCX+r, CCY+y, r);
  1342. ProcessMap(CCX-r, CCY+y, r);
  1343. ProcessMap(CCX-r, CCY-y, r);
  1344. }
  1345. ProcessMap(CCX-r, CCY, r);
  1346. ProcessMap(CCX+r, CCY, r);
  1347. }
  1348. ProcessMap(CCX, CCY, 0);
  1349. }
  1350. void RenderWCircles()
  1351. {
  1352. guAlphaSource(GR_ALPHASOURCE_CC_ALPHA);
  1353. grFogMode(GR_FOG_WITH_ITERATED_ALPHA);
  1354. grAlphaBlendFunction(GR_BLEND_SRC_ALPHA,
  1355. GR_BLEND_ONE,
  1356. GR_BLEND_ONE,
  1357. GR_BLEND_ONE);
  1358. TWCircle *wptr;
  1359. Vector3d rpos;
  1360. for (int c=0; c<WCCount; c++) {
  1361. wptr = &WCircles[c];
  1362. rpos.x = wptr->pos.x - CameraX;
  1363. rpos.y = wptr->pos.y - CameraY;
  1364. rpos.z = wptr->pos.z - CameraZ;
  1365. float r = (float)max( fabs(rpos.x), fabs(rpos.z) );
  1366. int ri = -1 + (int)(r / 256.f + 0.4f);
  1367. if (ri < 0) ri = 0;
  1368. if (ri > ctViewR) continue;
  1369. rpos = RotateVector(rpos);
  1370. if ( rpos.z > BackViewR) continue;
  1371. if ( fabs(rpos.x) > -rpos.z + BackViewR ) continue;
  1372. if ( fabs(rpos.y) > -rpos.z + BackViewR ) continue;
  1373. grConstantColorValue( ((2000-wptr->FTime) / 38) << 24 );
  1374. CreateMorphedModel(WCircleModel.mptr, &WCircleModel.Animation[0], (int)(wptr->FTime), wptr->scale);
  1375. if ( fabs(rpos.z) + fabs(rpos.x) < 1000)
  1376. RenderModelClip(WCircleModel.mptr,
  1377. rpos.x, rpos.y, rpos.z, 250, 0, 0, CameraBeta);
  1378. else
  1379. RenderModel(WCircleModel.mptr,
  1380. rpos.x, rpos.y, rpos.z, 250, 0, 0, CameraBeta);
  1381. }
  1382. }
  1383. void RenderWater()
  1384. {
  1385. grDepthMask( FXFALSE );
  1386. grConstantColorValue(0xFF000000);
  1387. guAlphaSource(GR_ALPHASOURCE_ITERATED_ALPHA);
  1388. grFogMode(GR_FOG_DISABLE);
  1389. for (int r=ctViewR; r>=ctViewR1; r-=2) {
  1390. for (int x=r; x>0; x-=2) {
  1391. ProcessMapW2(CCX-x, CCY+r, r);
  1392. ProcessMapW2(CCX+x, CCY+r, r);
  1393. ProcessMapW2(CCX-x, CCY-r, r);
  1394. ProcessMapW2(CCX+x, CCY-r, r);
  1395. }
  1396. ProcessMapW2(CCX, CCY-r, r);
  1397. ProcessMapW2(CCX, CCY+r, r);
  1398. for (int y=r-2; y>0; y-=2) {
  1399. ProcessMapW2(CCX+r, CCY-y, r);
  1400. ProcessMapW2(CCX+r, CCY+y, r);
  1401. ProcessMapW2(CCX-r, CCY+y, r);
  1402. ProcessMapW2(CCX-r, CCY-y, r);
  1403. }
  1404. ProcessMapW2(CCX-r, CCY, r);
  1405. ProcessMapW2(CCX+r, CCY, r);
  1406. }
  1407. for (int y=-ctViewR1+2; y<ctViewR1; y++)
  1408. for (int x=-ctViewR1+2; x<ctViewR1; x++)
  1409. ProcessMapW(CCX+x, CCY+y, max(abs(x), abs(y)));
  1410. RenderWCircles();
  1411. grConstantColorValue(0xFF000000);
  1412. grAlphaBlendFunction(GR_BLEND_SRC_ALPHA,
  1413. GR_BLEND_ONE_MINUS_SRC_ALPHA,
  1414. GR_BLEND_ONE,
  1415. GR_BLEND_ONE);
  1416. grDepthMask( FXTRUE );
  1417. }
  1418. void RenderModelsList()
  1419. {
  1420. for (int o=0; o<ORLCount; o++)
  1421. _RenderObject(ORList[o].x, ORList[o].y);
  1422. ORLCount=0;
  1423. }
  1424. void ProcessMap(int x, int y, int r)
  1425. {
  1426. if (x>=ctMapSize-1 || y>=ctMapSize-1 ||
  1427. x<0 || y<0) return;
  1428. float BackR = BackViewR;
  1429. if (OMap[y][x]!=255) BackR+=MObjects[OMap[y][x]].info.BoundR;
  1430. ev[0] = VMap[y-CCY+128][x-CCX+128];
  1431. if (ev[0].v.z>BackR) return;
  1432. if (FOGON) {
  1433. int cf = FogsMap[y>>1][x>>1];
  1434. if (UNDERWATER) cf=127;
  1435. if (cf) {
  1436. CurFogColor = FogsList[ cf ].fogRGB;
  1437. grFogColorValue( CurFogColor );
  1438. } else if (CAMERAINFOG) {
  1439. cf = CameraFogI;
  1440. CurFogColor = FogsList[ cf ].fogRGB;
  1441. grFogColorValue( CurFogColor );
  1442. }
  1443. }
  1444. int t1 = TMap1[y][x];
  1445. ReverseOn = (FMap[y][x] & fmReverse);
  1446. TDirection = (FMap[y][x] & 3);
  1447. x = x - CCX + 128;
  1448. y = y - CCY + 128;
  1449. ev[1] = VMap[y][x+1];
  1450. if (ReverseOn) ev[2] = VMap[y+1][x];
  1451. else ev[2] = VMap[y+1][x+1];
  1452. float xx = (ev[0].v.x + VMap[y+1][x+1].v.x) / 2;
  1453. float yy = (ev[0].v.y + VMap[y+1][x+1].v.y) / 2;
  1454. float zz = (ev[0].v.z + VMap[y+1][x+1].v.z) / 2;
  1455. //wsprintf(logt, "%d", (int) (FOVK*100));
  1456. //AddMessage(logt);
  1457. if ( fabs(xx*FOVK) > -zz + BackR) return;
  1458. zs = (int)sqrt( xx*xx + zz*zz + yy*yy);
  1459. grConstantColorValue( 0xFF000000);
  1460. if (MIPMAP && (zs > 256 * 10 || LOWRESTX)) SetFXTexture(Textures[t1]->DataB, 64, 64);
  1461. else SetFXTexture(Textures[t1]->DataA, 128, 128);
  1462. if (r>8) DrawTPlane(FALSE);
  1463. else DrawTPlaneClip(FALSE);
  1464. if (ReverseOn) { ev[0] = ev[2]; ev[2] = VMap[y+1][x+1]; }
  1465. else { ev[1] = ev[2]; ev[2] = VMap[y+1][x]; }
  1466. if (r>8) DrawTPlane(TRUE);
  1467. else DrawTPlaneClip(TRUE);
  1468. x = x + CCX - 128;
  1469. y = y + CCY - 128;
  1470. RenderObject(x, y);
  1471. }
  1472. void ProcessMap2(int x, int y, int r)
  1473. {
  1474. if (x>=ctMapSize-1 || y>=ctMapSize-1 ||
  1475. x<0 || y<0) return;
  1476. ev[0] = VMap[y-CCY+128][x-CCX+128];
  1477. if (ev[0].v.z>BackViewR) return;
  1478. if (FOGON) {
  1479. int cf = FogsMap[y>>1][x>>1];
  1480. if (UNDERWATER) cf=127;
  1481. if (cf) {
  1482. CurFogColor = FogsList[ cf ].fogRGB;
  1483. grFogColorValue( CurFogColor );
  1484. } else if (CAMERAINFOG) {
  1485. cf = CameraFogI;
  1486. CurFogColor = FogsList[ cf ].fogRGB;
  1487. grFogColorValue( CurFogColor );
  1488. }
  1489. }
  1490. int t1 = TMap2[y][x];
  1491. TDirection = ((FMap[y][x]>>8) & 3);
  1492. ReverseOn = FALSE;
  1493. x = x - CCX + 128;
  1494. y = y - CCY + 128;
  1495. ev[1] = VMap[y][x+2];
  1496. if (ReverseOn) ev[2] = VMap[y+2][x];
  1497. else ev[2] = VMap[y+2][x+2];
  1498. float xx = (ev[0].v.x + VMap[y+2][x+2].v.x) / 2;
  1499. float yy = (ev[0].v.y + VMap[y+2][x+2].v.y) / 2;
  1500. float zz = (ev[0].v.z + VMap[y+2][x+2].v.z) / 2;
  1501. if ( fabs(xx*FOVK) > -zz + BackViewR) return;
  1502. zs = (int)sqrt( xx*xx + zz*zz + yy*yy);
  1503. if (zs > ctViewR*256) return;
  1504. grConstantColorValue( 0xFF000000);
  1505. SetFXTexture(Textures[t1]->DataB, 64, 64);
  1506. DrawTPlane(FALSE);
  1507. if (ReverseOn) { ev[0] = ev[2]; ev[2] = VMap[y+2][x+2]; }
  1508. else { ev[1] = ev[2]; ev[2] = VMap[y+2][x]; }
  1509. DrawTPlane(TRUE);
  1510. x = x + CCX - 128;
  1511. y = y + CCY - 128;
  1512. RenderObject(x , y);
  1513. RenderObject(x+1, y);
  1514. RenderObject(x , y+1);
  1515. RenderObject(x+1, y+1);
  1516. }
  1517. void BuildTreeNoSort()
  1518. {
  1519. Vector2di v[3];
  1520. Current = -1;
  1521. int LastFace = -1;
  1522. TFace* fptr;
  1523. int sg;
  1524. for (int f=0; f<mptr->FCount; f++)
  1525. {
  1526. fptr = &mptr->gFace[f];
  1527. v[0] = gScrp[fptr->v1];
  1528. v[1] = gScrp[fptr->v2];
  1529. v[2] = gScrp[fptr->v3];
  1530. if (v[0].x == 0xFFFFFF) continue;
  1531. if (v[1].x == 0xFFFFFF) continue;
  1532. if (v[2].x == 0xFFFFFF) continue;
  1533. if (fptr->Flags & (sfDarkBack+sfNeedVC)) {
  1534. sg = (v[1].x-v[0].x)*(v[2].y-v[1].y) - (v[1].y-v[0].y)*(v[2].x-v[1].x);
  1535. if (sg<0) continue;
  1536. }
  1537. fptr->Next=-1;
  1538. if (Current==-1) { Current=f; LastFace = f; } else
  1539. { mptr->gFace[LastFace].Next=f; LastFace=f; }
  1540. }
  1541. }
  1542. int BuildTreeClipNoSort()
  1543. {
  1544. Current = -1;
  1545. int fc = 0;
  1546. int LastFace = -1;
  1547. TFace* fptr;
  1548. for (int f=0; f<mptr->FCount; f++)
  1549. {
  1550. fptr = &mptr->gFace[f];
  1551. if (fptr->Flags & (sfDarkBack + sfNeedVC) ) {
  1552. MulVectorsVect(SubVectors(rVertex[fptr->v2], rVertex[fptr->v1]), SubVectors(rVertex[fptr->v3], rVertex[fptr->v1]), nv);
  1553. if (nv.x*rVertex[fptr->v1].x + nv.y*rVertex[fptr->v1].y + nv.z*rVertex[fptr->v1].z<0) continue;
  1554. }
  1555. fc++;
  1556. fptr->Next=-1;
  1557. if (Current==-1) { Current=f; LastFace = f; } else
  1558. { mptr->gFace[LastFace].Next=f; LastFace=f; }
  1559. }
  1560. return fc;
  1561. }
  1562. void RenderModel(TModel* _mptr, float x0, float y0, float z0, int light, int VT, float al, float bt)
  1563. {
  1564. int f;
  1565. if (fabs(y0) > -(z0-256*6)) return;
  1566. mptr = _mptr;
  1567. float ca = (float)cos(al);
  1568. float sa = (float)sin(al);
  1569. float cb = (float)cos(bt);
  1570. float sb = (float)sin(bt);
  1571. int minx = 10241024;
  1572. int maxx =-10241024;
  1573. int miny = 10241024;
  1574. int maxy =-10241024;
  1575. float ml = (float)(light);
  1576. /*
  1577. gvtx[0].r = ml; gvtx[0].g = ml; gvtx[0].b = ml;
  1578. gvtx[1].r = ml; gvtx[1].g = ml; gvtx[1].b = ml;
  1579. gvtx[2].r = ml; gvtx[2].g = ml; gvtx[2].b = ml;
  1580. gvtx[0].z = 1.0f;
  1581. gvtx[1].z = 1.0f;
  1582. gvtx[2].z = 1.0f;
  1583. */
  1584. gvtx[0].oow = 1.0f;
  1585. gvtx[1].oow = 1.0f;
  1586. gvtx[2].oow = 1.0f;
  1587. BOOL FOGACTIVE = (FOGON && (FogYBase>0));
  1588. if (!FOGACTIVE) {
  1589. gvtx[0].a = 0;
  1590. gvtx[1].a = 0;
  1591. gvtx[2].a = 0;
  1592. }
  1593. TPoint3d p;
  1594. for (int s=0; s<mptr->VCount; s++) {
  1595. p = mptr->gVertex[s];
  1596. if (FOGACTIVE) {
  1597. vFogT[s] = FogYBase + p.y * FogYGrad;
  1598. if (vFogT[s]<0.f ) vFogT[s] = 0.f;
  1599. if (vFogT[s]>250.f) vFogT[s]=250.f;
  1600. }
  1601. vLight[s] = ml + mptr->VLight[VT][s];
  1602. rVertex[s].x = (p.x * ca + p.z * sa) + x0;
  1603. float vz = p.z * ca - p.x * sa;
  1604. rVertex[s].y = (p.y * cb - vz * sb) + y0;
  1605. rVertex[s].z = (vz * cb + p.y * sb) + z0;
  1606. if (rVertex[s].z>-64) rVertex[s].z=-64;
  1607. gScrp[s].x = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  1608. gScrp[s].y = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH);
  1609. if (gScrp[s].x > maxx) maxx = gScrp[s].x;
  1610. if (gScrp[s].x < minx) minx = gScrp[s].x;
  1611. if (gScrp[s].y > maxy) maxy = gScrp[s].y;
  1612. if (gScrp[s].y < miny) miny = gScrp[s].y;
  1613. }
  1614. if (minx == 10241024) return;
  1615. if (minx>WinW || maxx<0 || miny>WinH || maxy<0) return;
  1616. BuildTreeNoSort();
  1617. float d = (float) sqrt(x0*x0 + y0*y0 + z0*z0);
  1618. if (LOWRESTX) d = 14*256;
  1619. if (MIPMAP && (d > 12*256)) SetFXTexture(mptr->lpTexture2, 128, 128);
  1620. else SetFXTexture(mptr->lpTexture, 256, 256);
  1621. int PrevOpacity = 0;
  1622. int NewOpacity = 0;
  1623. int PrevTransparent = 0;
  1624. int NewTransparent = 0;
  1625. f = Current;
  1626. while( f!=-1 ) {
  1627. TFace *fptr = & mptr->gFace[f];
  1628. f = mptr->gFace[f].Next;
  1629. if (minx<0)
  1630. if (gScrp[fptr->v1].x<0 && gScrp[fptr->v2].x<0 && gScrp[fptr->v3].x<0) continue;
  1631. if (maxx>WinW)
  1632. if (gScrp[fptr->v1].x>WinW && gScrp[fptr->v2].x>WinW && gScrp[fptr->v3].x>WinW) continue;
  1633. if (miny<0)
  1634. if (gScrp[fptr->v1].y<0 && gScrp[fptr->v2].y<0 && gScrp[fptr->v3].y<0) continue;
  1635. if (maxy>WinH)
  1636. if (gScrp[fptr->v1].y>WinH && gScrp[fptr->v2].y>WinH && gScrp[fptr->v3].y>WinH) continue;
  1637. NewOpacity = (fptr->Flags & sfOpacity);
  1638. NewTransparent = (fptr->Flags & sfTransparent);
  1639. if (NewOpacity != PrevOpacity) {
  1640. grChromakeyMode(GR_CHROMAKEY_ENABLE);
  1641. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_POINT_SAMPLED, GR_TEXTUREFILTER_POINT_SAMPLED);
  1642. PrevOpacity = NewOpacity;
  1643. }
  1644. if (NewTransparent != PrevTransparent)
  1645. {
  1646. grConstantColorValue(0x80000000);
  1647. //grDepthMask( FXFALSE );
  1648. PrevTransparent = NewTransparent;
  1649. }
  1650. if (FOGACTIVE) {
  1651. gvtx[0].a = vFogT[fptr->v1];
  1652. gvtx[1].a = vFogT[fptr->v2];
  1653. gvtx[2].a = vFogT[fptr->v3];
  1654. }
  1655. gvtx[0].x = (float)gScrp[fptr->v1].x;
  1656. gvtx[0].y = (float)gScrp[fptr->v1].y;
  1657. gvtx[0].ooz = _ZSCALE / -rVertex[fptr->v1].z;
  1658. gvtx[0].tmuvtx[0].sow = fptr->tax;
  1659. gvtx[0].tmuvtx[0].tow = fptr->tay;
  1660. gvtx[0].b = gvtx[0].g = gvtx[0].r = vLight[fptr->v1];
  1661. gvtx[1].x = (float)gScrp[fptr->v2].x;
  1662. gvtx[1].y = (float)gScrp[fptr->v2].y;
  1663. gvtx[1].ooz = _ZSCALE / -rVertex[fptr->v2].z;
  1664. gvtx[1].tmuvtx[0].sow = fptr->tbx;
  1665. gvtx[1].tmuvtx[0].tow = fptr->tby;
  1666. gvtx[1].b = gvtx[1].g = gvtx[1].r = vLight[fptr->v2];
  1667. gvtx[2].x = (float)gScrp[fptr->v3].x;
  1668. gvtx[2].y = (float)gScrp[fptr->v3].y;
  1669. gvtx[2].ooz = _ZSCALE / -rVertex[fptr->v3].z;
  1670. gvtx[2].tmuvtx[0].sow = fptr->tcx;
  1671. gvtx[2].tmuvtx[0].tow = fptr->tcy;
  1672. gvtx[2].b = gvtx[2].g = gvtx[2].r = vLight[fptr->v3];
  1673. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  1674. dFacesCount++;
  1675. }
  1676. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
  1677. grChromakeyMode(GR_CHROMAKEY_DISABLE);
  1678. grConstantColorValue(0xFF000000);
  1679. /* if (NewTransparent)
  1680. grDepthMask( FXTRUE ); */
  1681. }
  1682. void RenderBMPModel(TBMPModel* mptr, float x0, float y0, float z0, int light)
  1683. {
  1684. int f;
  1685. if (fabs(y0) > -(z0-256*6)) return;
  1686. int minx = 10241024;
  1687. int maxx =-10241024;
  1688. int miny = 10241024;
  1689. int maxy =-10241024;
  1690. float ml = (float)(light);
  1691. BOOL FOGACTIVE = (FOGON && (FogYBase>0));
  1692. TPoint3d p;
  1693. for (int s=0; s<4; s++) {
  1694. if (FOGACTIVE) {
  1695. vFogT[s] = FogYBase + mptr->gVertex[s].y * FogYGrad;
  1696. if (vFogT[s]<0.f ) vFogT[s] = 0.f;
  1697. if (vFogT[s]>250.f) vFogT[s]=250.f;
  1698. }
  1699. rVertex[s].x = mptr->gVertex[s].x + x0;
  1700. rVertex[s].y = mptr->gVertex[s].y * cb + y0;
  1701. rVertex[s].z = mptr->gVertex[s].y * sb + z0;
  1702. if (rVertex[s].z<-256) {
  1703. gScrp[s].x = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  1704. gScrp[s].y = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH);
  1705. } else return;
  1706. if (gScrp[s].x > maxx) maxx = gScrp[s].x;
  1707. if (gScrp[s].x < minx) minx = gScrp[s].x;
  1708. if (gScrp[s].y > maxy) maxy = gScrp[s].y;
  1709. if (gScrp[s].y < miny) miny = gScrp[s].y;
  1710. }
  1711. if (minx == 10241024) return;
  1712. if (minx>WinW || maxx<0 || miny>WinH || maxy<0) return;
  1713. float d = (float) sqrt(x0*x0 + y0*y0 + z0*z0);
  1714. SetFXTexture(mptr->lpTexture, 128, 128);
  1715. grChromakeyMode(GR_CHROMAKEY_ENABLE);
  1716. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_POINT_SAMPLED, GR_TEXTUREFILTER_POINT_SAMPLED);
  1717. if (FOGACTIVE) {
  1718. gvtx[0].a = vFogT[0];
  1719. gvtx[1].a = vFogT[1];
  1720. gvtx[2].a = vFogT[2];
  1721. gvtx[3].a = vFogT[3];
  1722. } else {
  1723. gvtx[0].a = 0;
  1724. gvtx[1].a = 0;
  1725. gvtx[2].a = 0;
  1726. gvtx[3].a = 0;
  1727. }
  1728. gvtx[0].x = (float)gScrp[0].x;
  1729. gvtx[0].y = (float)gScrp[0].y;
  1730. gvtx[0].ooz = _ZSCALE / -rVertex[0].z;
  1731. gvtx[0].oow = -1.0f/ rVertex[0].z;
  1732. gvtx[0].tmuvtx[0].sow = 0.5f * gvtx[0].oow;
  1733. gvtx[0].tmuvtx[0].tow = 0.5f * gvtx[0].oow;
  1734. gvtx[0].r = ml; gvtx[0].g = ml; gvtx[0].b = ml;
  1735. gvtx[1].x = (float)gScrp[1].x;
  1736. gvtx[1].y = (float)gScrp[1].y;
  1737. gvtx[1].ooz = _ZSCALE / -rVertex[1].z;
  1738. gvtx[1].oow = -1.0f/ rVertex[1].z;
  1739. gvtx[1].tmuvtx[0].sow = 254.5f * gvtx[1].oow;
  1740. gvtx[1].tmuvtx[0].tow = 0.5f * gvtx[1].oow;
  1741. gvtx[1].r = ml; gvtx[1].g = ml; gvtx[1].b = ml;
  1742. gvtx[2].x = (float)gScrp[2].x;
  1743. gvtx[2].y = (float)gScrp[2].y;
  1744. gvtx[2].ooz = _ZSCALE / -rVertex[2].z;
  1745. gvtx[2].oow = -1.0f/ rVertex[2].z;
  1746. gvtx[2].tmuvtx[0].sow = 254.5f * gvtx[2].oow;
  1747. gvtx[2].tmuvtx[0].tow = 254.5f * gvtx[2].oow;
  1748. gvtx[2].r = ml; gvtx[2].g = ml; gvtx[2].b = ml;
  1749. gvtx[3].x = (float)gScrp[3].x;
  1750. gvtx[3].y = (float)gScrp[3].y;
  1751. gvtx[3].ooz = _ZSCALE / -rVertex[3].z;
  1752. gvtx[3].oow = -1.0f/ rVertex[3].z;
  1753. gvtx[3].tmuvtx[0].sow = 0.5f * gvtx[3].oow;
  1754. gvtx[3].tmuvtx[0].tow = 254.5f * gvtx[3].oow;
  1755. gvtx[3].r = ml; gvtx[3].g = ml; gvtx[3].b = ml;
  1756. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  1757. dFacesCount++;
  1758. grDrawTriangle(&gvtx[0], &gvtx[2], &gvtx[3]);
  1759. dFacesCount++;
  1760. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
  1761. grChromakeyMode(GR_CHROMAKEY_DISABLE);
  1762. grConstantColorValue(0xFF000000);
  1763. }
  1764. void RenderShadowClip(TModel* _mptr,
  1765. float xm0, float ym0, float zm0,
  1766. float x0, float y0, float z0, float cal, float al, float bt)
  1767. {
  1768. int f,CMASK;
  1769. mptr = _mptr;
  1770. float cla = (float)cos(cal);
  1771. float sla = (float)sin(cal);
  1772. float ca = (float)cos(al);
  1773. float sa = (float)sin(al);
  1774. float cb = (float)cos(bt);
  1775. float sb = (float)sin(bt);
  1776. float flight = 0;
  1777. BOOL BL = FALSE;
  1778. for (int s=0; s<mptr->VCount; s++) {
  1779. float mrx = mptr->gVertex[s].x * cla + mptr->gVertex[s].z * sla;
  1780. float mrz = mptr->gVertex[s].z * cla - mptr->gVertex[s].x * sla;
  1781. float shx = mrx + mptr->gVertex[s].y * SunShadowK;
  1782. float shz = mrz + mptr->gVertex[s].y * SunShadowK;
  1783. float shy = GetLandH(shx + xm0, shz + zm0) - ym0;
  1784. rVertex[s].x = (shx * ca + shz * sa) + x0;
  1785. float vz = shz * ca - shx * sa;
  1786. rVertex[s].y = (shy * cb - vz * sb) + y0;
  1787. rVertex[s].z = (vz * cb + shy * sb) + z0;
  1788. if (rVertex[s].z<0) BL=TRUE;
  1789. if (rVertex[s].z>-256) { gScrp[s].x = 0xFFFFFF; gScrp[s].y = 0xFF; }
  1790. else {
  1791. int f = 0;
  1792. int sx = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  1793. int sy = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH);
  1794. if (sx>=WinEX) f+=1;
  1795. if (sx<=0 ) f+=2;
  1796. if (sy>=WinEY) f+=4;
  1797. if (sy<=0 ) f+=8;
  1798. gScrp[s].y = f;
  1799. }
  1800. }
  1801. if (!BL) return;
  1802. float d = (float) sqrt(x0*x0 + y0*y0 + z0*z0);
  1803. if (LOWRESTX) d = 14*256;
  1804. if (MIPMAP && (d > 12*256)) SetFXTexture(mptr->lpTexture2, 128, 128);
  1805. else SetFXTexture(mptr->lpTexture, 256, 256);
  1806. int BiasValue = BuildTreeClipNoSort();
  1807. f = Current;
  1808. while( f!=-1 ) {
  1809. vused = 3;
  1810. TFace *fptr = &mptr->gFace[f];
  1811. CMASK = 0;
  1812. CMASK|=gScrp[fptr->v1].y;
  1813. CMASK|=gScrp[fptr->v2].y;
  1814. CMASK|=gScrp[fptr->v3].y;
  1815. cp[0].ev.v = rVertex[fptr->v1]; cp[0].tx = fptr->tax; cp[0].ty = fptr->tay;
  1816. cp[1].ev.v = rVertex[fptr->v2]; cp[1].tx = fptr->tbx; cp[1].ty = fptr->tby;
  1817. cp[2].ev.v = rVertex[fptr->v3]; cp[2].tx = fptr->tcx; cp[2].ty = fptr->tcy;
  1818. if (CMASK == 0xFF) {
  1819. for (u=0; u<vused; u++) cp[u].ev.v.z+=12.0f;
  1820. for (u=0; u<vused; u++) ClipVector(ClipZ,u);
  1821. for (u=0; u<vused; u++) cp[u].ev.v.z-=12.0f;
  1822. if (vused<3) goto LNEXT;
  1823. }
  1824. if (CMASK & 1) for (u=0; u<vused; u++) ClipVector(ClipA,u); if (vused<3) goto LNEXT;
  1825. if (CMASK & 2) for (u=0; u<vused; u++) ClipVector(ClipC,u); if (vused<3) goto LNEXT;
  1826. if (CMASK & 4) for (u=0; u<vused; u++) ClipVector(ClipB,u); if (vused<3) goto LNEXT;
  1827. if (CMASK & 8) for (u=0; u<vused; u++) ClipVector(ClipD,u); if (vused<3) goto LNEXT;
  1828. for (u=0; u<vused; u++) {
  1829. gvtx[u].x = (float)(VideoCX - (int)(cp[u].ev.v.x / cp[u].ev.v.z * CameraW));
  1830. gvtx[u].y = (float)(VideoCY + (int)(cp[u].ev.v.y / cp[u].ev.v.z * CameraH));
  1831. gvtx[u].z = -cp[u].ev.v.z - 16.f;
  1832. gvtx[u].ooz = (_ZSCALE/gvtx[u].z);
  1833. gvtx[u].oow = 1.0f/gvtx[u].z;
  1834. gvtx[u].tmuvtx[0].sow = (float)cp[u].tx/65534.f * gvtx[u].oow;
  1835. gvtx[u].tmuvtx[0].tow = (float)cp[u].ty/65534.f * gvtx[u].oow;
  1836. gvtx[u].r = flight; gvtx[u].g = gvtx[u].r; gvtx[u].b = gvtx[u].r;
  1837. }
  1838. if (fptr->Flags & sfOpacity) {
  1839. grChromakeyMode(GR_CHROMAKEY_ENABLE);
  1840. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_POINT_SAMPLED, GR_TEXTUREFILTER_POINT_SAMPLED);
  1841. } else {
  1842. grChromakeyMode(GR_CHROMAKEY_DISABLE);
  1843. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
  1844. }
  1845. //grDepthBiasLevel( (BiasValue--)>>5);
  1846. for (u=0; u<vused-2; u++) {
  1847. grDrawTriangle(&gvtx[0], &gvtx[u+1], &gvtx[u+2]);
  1848. dFacesCount++;
  1849. }
  1850. LNEXT:
  1851. f = mptr->gFace[f].Next;
  1852. }
  1853. grConstantColorValue(0xFF000000);
  1854. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
  1855. grChromakeyMode(GR_CHROMAKEY_DISABLE);
  1856. //grDepthBiasLevel(0);
  1857. //grDepthMask( FXTRUE );
  1858. }
  1859. void RenderModelClip(TModel* _mptr, float x0, float y0, float z0, int light, int VT, float al, float bt)
  1860. {
  1861. int f,CMASK;
  1862. //if (fabs(y0) > -(z0-256*6)) return;
  1863. mptr = _mptr;
  1864. float ca = (float)cos(al);
  1865. float sa = (float)sin(al);
  1866. float cb = (float)cos(bt);
  1867. float sb = (float)sin(bt);
  1868. float flight = (float)(light);
  1869. BOOL BL = FALSE;
  1870. BOOL FOGACTIVE = (FOGON && (FogYBase>0));
  1871. for (int s=0; s<mptr->VCount; s++) {
  1872. if (FOGACTIVE) {
  1873. vFogT[s] = FogYBase + mptr->gVertex[s].y * FogYGrad;
  1874. if (vFogT[s]<0.f) vFogT[s] = 0.f;
  1875. if (vFogT[s]>250.f) vFogT[s]=250.f;
  1876. } else vFogT[s]=0;
  1877. rVertex[s].x = (mptr->gVertex[s].x * ca + mptr->gVertex[s].z * sa) /* * mdlScale */ + x0;
  1878. float vz = mptr->gVertex[s].z * ca - mptr->gVertex[s].x * sa;
  1879. rVertex[s].y = (mptr->gVertex[s].y * cb - vz * sb) /* * mdlScale */ + y0;
  1880. rVertex[s].z = (vz * cb + mptr->gVertex[s].y * sb) /* * mdlScale */ + z0;
  1881. if (rVertex[s].z<0) BL=TRUE;
  1882. if (rVertex[s].z>-256) { gScrp[s].x = 0xFFFFFF; gScrp[s].y = 0xFF; }
  1883. else {
  1884. int f = 0;
  1885. int sx = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  1886. int sy = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH);
  1887. if (sx>=WinEX) f+=1;
  1888. if (sx<=0 ) f+=2;
  1889. if (sy>=WinEY) f+=4;
  1890. if (sy<=0 ) f+=8;
  1891. gScrp[s].y = f;
  1892. }
  1893. }
  1894. if (!BL) return;
  1895. if (!FOGACTIVE) {
  1896. gvtx[0].a = 0;
  1897. gvtx[1].a = 0;
  1898. gvtx[2].a = 0;
  1899. }
  1900. gvtx[3].a = FogYBase;
  1901. gvtx[4].a = FogYBase;
  1902. gvtx[5].a = FogYBase;
  1903. gvtx[6].a = FogYBase;
  1904. gvtx[7].a = FogYBase;
  1905. if (LOWRESTX) SetFXTexture(mptr->lpTexture2, 128, 128);
  1906. else SetFXTexture(mptr->lpTexture, 256, 256);
  1907. BuildTreeClipNoSort();
  1908. int NewOpacity = 0;
  1909. int PrevOpacity = 0;
  1910. int NewTransparent = 0;
  1911. int PrevTransparent = 0;
  1912. ca = (float)cos(CameraAlpha);
  1913. sa = (float)sin(CameraAlpha);
  1914. cb = (float)cos(CameraBeta);
  1915. sb = (float)sin(CameraBeta);
  1916. f = Current;
  1917. while( f!=-1 ) {
  1918. vused = 3;
  1919. TFace *fptr = &mptr->gFace[f];
  1920. CMASK = 0;
  1921. CMASK|=gScrp[fptr->v1].y;
  1922. CMASK|=gScrp[fptr->v2].y;
  1923. CMASK|=gScrp[fptr->v3].y;
  1924. cp[0].ev.v = rVertex[fptr->v1]; cp[0].ev.Fog = vFogT[fptr->v1]; cp[0].tx = fptr->tax; cp[0].ty = fptr->tay; cp[0].ev.Light = (int)mptr->VLight[VT][fptr->v1];
  1925. cp[1].ev.v = rVertex[fptr->v2]; cp[1].ev.Fog = vFogT[fptr->v2]; cp[1].tx = fptr->tbx; cp[1].ty = fptr->tby; cp[1].ev.Light = (int)mptr->VLight[VT][fptr->v2];
  1926. cp[2].ev.v = rVertex[fptr->v3]; cp[2].ev.Fog = vFogT[fptr->v3]; cp[2].tx = fptr->tcx; cp[2].ty = fptr->tcy; cp[2].ev.Light = (int)mptr->VLight[VT][fptr->v3];
  1927. //if (CMASK == 0xFF)
  1928. {
  1929. for (u=0; u<vused; u++) cp[u].ev.v.z+=12.0f;
  1930. for (u=0; u<vused; u++) ClipVector(ClipZ,u);
  1931. for (u=0; u<vused; u++) cp[u].ev.v.z-=12.0f;
  1932. if (vused<3) goto LNEXT;
  1933. }
  1934. if (CMASK & 1) for (u=0; u<vused; u++) ClipVector(ClipA,u); if (vused<3) goto LNEXT;
  1935. if (CMASK & 2) for (u=0; u<vused; u++) ClipVector(ClipC,u); if (vused<3) goto LNEXT;
  1936. if (CMASK & 4) for (u=0; u<vused; u++) ClipVector(ClipB,u); if (vused<3) goto LNEXT;
  1937. if (CMASK & 8) for (u=0; u<vused; u++) ClipVector(ClipD,u); if (vused<3) goto LNEXT;
  1938. for (u=0; u<vused; u++) {
  1939. gvtx[u].a = cp[u].ev.Fog;
  1940. gvtx[u].x = (float)(VideoCX - (int)(cp[u].ev.v.x / cp[u].ev.v.z * CameraW));
  1941. gvtx[u].y = (float)(VideoCY + (int)(cp[u].ev.v.y / cp[u].ev.v.z * CameraH));
  1942. gvtx[u].z = -cp[u].ev.v.z;
  1943. gvtx[u].ooz = (_ZSCALE/gvtx[u].z);
  1944. gvtx[u].oow = 1.0f / gvtx[u].z;
  1945. gvtx[u].tmuvtx[0].sow = (float)cp[u].tx * gvtx[u].oow;
  1946. gvtx[u].tmuvtx[0].tow = (float)cp[u].ty * gvtx[u].oow;
  1947. float _flight = flight + cp[u].ev.Light;
  1948. gvtx[u].r = _flight; gvtx[u].g = gvtx[u].r; gvtx[u].b = gvtx[u].r;
  1949. }
  1950. NewOpacity = (fptr->Flags & sfOpacity);
  1951. NewTransparent = (fptr->Flags & sfTransparent);
  1952. if (NewOpacity != PrevOpacity) {
  1953. grChromakeyMode(GR_CHROMAKEY_ENABLE);
  1954. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_POINT_SAMPLED, GR_TEXTUREFILTER_POINT_SAMPLED);
  1955. PrevOpacity = NewOpacity;
  1956. }
  1957. if (NewTransparent != PrevTransparent) {
  1958. grConstantColorValue(0x80000000);
  1959. PrevTransparent = NewTransparent;
  1960. }
  1961. for (u=0; u<vused-2; u++) {
  1962. grDrawTriangle(&gvtx[0], &gvtx[u+1], &gvtx[u+2]);
  1963. dFacesCount++;
  1964. }
  1965. LNEXT:
  1966. f = mptr->gFace[f].Next;
  1967. }
  1968. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
  1969. grChromakeyMode(GR_CHROMAKEY_DISABLE);
  1970. grConstantColorValue(0xFF000000);
  1971. /*if (NewTransparent)
  1972. grDepthMask( FXTRUE );*/
  1973. }
  1974. void RenderModelClipEnvMap(TModel* _mptr, float x0, float y0, float z0, float al, float bt)
  1975. {
  1976. int f,CMASK;
  1977. BOOL BL = FALSE;
  1978. mptr = _mptr;
  1979. float ca = (float)cos(al);
  1980. float sa = (float)sin(al);
  1981. float cb = (float)cos(bt);
  1982. float sb = (float)sin(bt);
  1983. for (int s=0; s<mptr->VCount; s++) {
  1984. rVertex[s].x = (mptr->gVertex[s].x * ca + mptr->gVertex[s].z * sa) + x0;
  1985. float vz = mptr->gVertex[s].z * ca - mptr->gVertex[s].x * sa;
  1986. rVertex[s].y = (mptr->gVertex[s].y * cb - vz * sb) + y0;
  1987. rVertex[s].z = (vz * cb + mptr->gVertex[s].y * sb) + z0;
  1988. if (rVertex[s].z<0) BL=TRUE;
  1989. if (rVertex[s].z>-256) { gScrp[s].x = 0xFFFFFF; gScrp[s].y = 0xFF; }
  1990. else {
  1991. int f = 0;
  1992. int sx = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  1993. int sy = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH);
  1994. if (sx>=WinEX) f+=1;
  1995. if (sx<=0 ) f+=2;
  1996. if (sy>=WinEY) f+=4;
  1997. if (sy<=0 ) f+=8;
  1998. gScrp[s].y = f;
  1999. }
  2000. }
  2001. if (!BL) return;
  2002. gvtx[0].a = 0; gvtx[1].a = 0; gvtx[2].a = 0; gvtx[4].a = 0; gvtx[5].a = 0;
  2003. SetFXTexture(TFX_ENVMAP.lpImage, TFX_ENVMAP.W, TFX_ENVMAP.W);
  2004. grTexClampMode(GR_TMU0, GR_TEXTURECLAMP_WRAP, GR_TEXTURECLAMP_WRAP);
  2005. grConstantColorValue(0xFF000000);
  2006. grAlphaBlendFunction(GR_BLEND_SRC_ALPHA, GR_BLEND_ONE, GR_BLEND_ONE, GR_BLEND_ONE);
  2007. BuildTreeClipNoSort();
  2008. ca = (float)cos(CameraAlpha);
  2009. sa = (float)sin(CameraAlpha);
  2010. cb = (float)cos(CameraBeta);
  2011. sb = (float)sin(CameraBeta);
  2012. f = Current;
  2013. while( f!=-1 ) {
  2014. vused = 3;
  2015. TFace *fptr = &mptr->gFace[f];
  2016. if (!(fptr->Flags & sfEnvMap)) goto LNEXT;
  2017. CMASK = 0;
  2018. CMASK|=gScrp[fptr->v1].y;
  2019. CMASK|=gScrp[fptr->v2].y;
  2020. CMASK|=gScrp[fptr->v3].y;
  2021. cp[0].ev.v = rVertex[fptr->v1]; cp[0].tx = PhongMapping[fptr->v1].x; cp[0].ty = PhongMapping[fptr->v1].y;
  2022. cp[1].ev.v = rVertex[fptr->v2]; cp[1].tx = PhongMapping[fptr->v2].x; cp[1].ty = PhongMapping[fptr->v2].y;
  2023. cp[2].ev.v = rVertex[fptr->v3]; cp[2].tx = PhongMapping[fptr->v3].x; cp[2].ty = PhongMapping[fptr->v3].y;
  2024. for (u=0; u<vused; u++) cp[u].ev.v.z+=12.0f;
  2025. for (u=0; u<vused; u++) ClipVector(ClipZ,u);
  2026. for (u=0; u<vused; u++) cp[u].ev.v.z-=12.0f;
  2027. if (vused<3) goto LNEXT;
  2028. if (CMASK & 1) for (u=0; u<vused; u++) ClipVector(ClipA,u); if (vused<3) goto LNEXT;
  2029. if (CMASK & 2) for (u=0; u<vused; u++) ClipVector(ClipC,u); if (vused<3) goto LNEXT;
  2030. if (CMASK & 4) for (u=0; u<vused; u++) ClipVector(ClipB,u); if (vused<3) goto LNEXT;
  2031. if (CMASK & 8) for (u=0; u<vused; u++) ClipVector(ClipD,u); if (vused<3) goto LNEXT;
  2032. for (u=0; u<vused; u++) {
  2033. gvtx[u].a = 0x00;
  2034. gvtx[u].x = (float)(VideoCX - (int)(cp[u].ev.v.x / cp[u].ev.v.z * CameraW));
  2035. gvtx[u].y = (float)(VideoCY + (int)(cp[u].ev.v.y / cp[u].ev.v.z * CameraH));
  2036. gvtx[u].z = -cp[u].ev.v.z;
  2037. gvtx[u].ooz = (_ZSCALE/gvtx[u].z);
  2038. gvtx[u].oow = 1.0f / gvtx[u].z;
  2039. gvtx[u].tmuvtx[0].sow = (float)cp[u].tx * gvtx[u].oow;
  2040. gvtx[u].tmuvtx[0].tow = (float)cp[u].ty * gvtx[u].oow;
  2041. gvtx[u].r = 255; gvtx[u].g = gvtx[u].r; gvtx[u].b = gvtx[u].r;
  2042. }
  2043. for (u=0; u<vused-2; u++) {
  2044. grDrawTriangle(&gvtx[0], &gvtx[u+1], &gvtx[u+2]);
  2045. dFacesCount++; }
  2046. LNEXT:
  2047. f = mptr->gFace[f].Next;
  2048. }
  2049. grDepthBiasLevel(8000);
  2050. grAlphaBlendFunction(GR_BLEND_SRC_ALPHA, GR_BLEND_ONE_MINUS_SRC_ALPHA, GR_BLEND_ONE, GR_BLEND_ONE);
  2051. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
  2052. grChromakeyMode(GR_CHROMAKEY_DISABLE);
  2053. grConstantColorValue(0xFF000000);
  2054. }
  2055. void RenderModelClipPhongMap(TModel* _mptr, float x0, float y0, float z0, float al, float bt)
  2056. {
  2057. int f,CMASK;
  2058. BOOL BL = FALSE;
  2059. mptr = _mptr;
  2060. float rv = SkyR +64; if (rv>255) rv = 255;
  2061. float gv = SkyG +64; if (gv>255) gv = 255;
  2062. float bv = SkyB +64; if (bv>255) bv = 255;
  2063. float ca = (float)cos(al);
  2064. float sa = (float)sin(al);
  2065. float cb = (float)cos(bt);
  2066. float sb = (float)sin(bt);
  2067. for (int s=0; s<mptr->VCount; s++) {
  2068. rVertex[s].x = (mptr->gVertex[s].x * ca + mptr->gVertex[s].z * sa) + x0;
  2069. float vz = mptr->gVertex[s].z * ca - mptr->gVertex[s].x * sa;
  2070. rVertex[s].y = (mptr->gVertex[s].y * cb - vz * sb) + y0;
  2071. rVertex[s].z = (vz * cb + mptr->gVertex[s].y * sb) + z0;
  2072. if (rVertex[s].z<0) BL=TRUE;
  2073. if (rVertex[s].z>-256) { gScrp[s].x = 0xFFFFFF; gScrp[s].y = 0xFF; }
  2074. else {
  2075. int f = 0;
  2076. int sx = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  2077. int sy = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH);
  2078. if (sx>=WinEX) f+=1;
  2079. if (sx<=0 ) f+=2;
  2080. if (sy>=WinEY) f+=4;
  2081. if (sy<=0 ) f+=8;
  2082. gScrp[s].y = f;
  2083. }
  2084. }
  2085. if (!BL) return;
  2086. for (s=0; s<6; s++) { gvtx[s].r = rv; gvtx[s].g = gv; gvtx[s].b = bv; gvtx[s].a = 0; }
  2087. SetFXTexture(TFX_SPECULAR.lpImage, TFX_SPECULAR.W, TFX_SPECULAR.W);
  2088. grTexClampMode(GR_TMU0, GR_TEXTURECLAMP_WRAP, GR_TEXTURECLAMP_WRAP);
  2089. //grDepthBiasLevel(8002);
  2090. grConstantColorValue(0xFF000000);
  2091. grAlphaBlendFunction(GR_BLEND_SRC_ALPHA, GR_BLEND_ONE, GR_BLEND_ONE, GR_BLEND_ONE);
  2092. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
  2093. BuildTreeClipNoSort();
  2094. ca = (float)cos(CameraAlpha);
  2095. sa = (float)sin(CameraAlpha);
  2096. cb = (float)cos(CameraBeta);
  2097. sb = (float)sin(CameraBeta);
  2098. f = Current;
  2099. while( f!=-1 ) {
  2100. vused = 3;
  2101. TFace *fptr = &mptr->gFace[f];
  2102. if (!(fptr->Flags & sfPhong)) goto LNEXT;
  2103. CMASK = 0;
  2104. CMASK|=gScrp[fptr->v1].y;
  2105. CMASK|=gScrp[fptr->v2].y;
  2106. CMASK|=gScrp[fptr->v3].y;
  2107. cp[0].ev.v = rVertex[fptr->v1]; cp[0].tx = PhongMapping[fptr->v1].x; cp[0].ty = PhongMapping[fptr->v1].y;
  2108. cp[1].ev.v = rVertex[fptr->v2]; cp[1].tx = PhongMapping[fptr->v2].x; cp[1].ty = PhongMapping[fptr->v2].y;
  2109. cp[2].ev.v = rVertex[fptr->v3]; cp[2].tx = PhongMapping[fptr->v3].x; cp[2].ty = PhongMapping[fptr->v3].y;
  2110. for (u=0; u<vused; u++) cp[u].ev.v.z+=12.0f;
  2111. for (u=0; u<vused; u++) ClipVector(ClipZ,u);
  2112. for (u=0; u<vused; u++) cp[u].ev.v.z-=12.0f;
  2113. if (vused<3) goto LNEXT;
  2114. if (CMASK & 1) for (u=0; u<vused; u++) ClipVector(ClipA,u); if (vused<3) goto LNEXT;
  2115. if (CMASK & 2) for (u=0; u<vused; u++) ClipVector(ClipC,u); if (vused<3) goto LNEXT;
  2116. if (CMASK & 4) for (u=0; u<vused; u++) ClipVector(ClipB,u); if (vused<3) goto LNEXT;
  2117. if (CMASK & 8) for (u=0; u<vused; u++) ClipVector(ClipD,u); if (vused<3) goto LNEXT;
  2118. for (u=0; u<vused; u++) {
  2119. gvtx[u].a = 0x00;
  2120. gvtx[u].x = (float)(VideoCX - (int)(cp[u].ev.v.x / cp[u].ev.v.z * CameraW));
  2121. gvtx[u].y = (float)(VideoCY + (int)(cp[u].ev.v.y / cp[u].ev.v.z * CameraH));
  2122. gvtx[u].z = -cp[u].ev.v.z;
  2123. gvtx[u].ooz = (_ZSCALE/gvtx[u].z);
  2124. gvtx[u].oow = 1.0f / gvtx[u].z;
  2125. gvtx[u].tmuvtx[0].sow = (float)cp[u].tx * gvtx[u].oow;
  2126. gvtx[u].tmuvtx[0].tow = (float)cp[u].ty * gvtx[u].oow;
  2127. }
  2128. for (u=0; u<vused-2; u++) {
  2129. grDrawTriangle(&gvtx[0], &gvtx[u+1], &gvtx[u+2]);
  2130. dFacesCount++; }
  2131. LNEXT:
  2132. f = mptr->gFace[f].Next;
  2133. }
  2134. grDepthBiasLevel(8000);
  2135. grAlphaBlendFunction(GR_BLEND_SRC_ALPHA, GR_BLEND_ONE_MINUS_SRC_ALPHA, GR_BLEND_ONE, GR_BLEND_ONE);
  2136. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
  2137. grChromakeyMode(GR_CHROMAKEY_DISABLE);
  2138. grConstantColorValue(0xFF000000);
  2139. }
  2140. void RenderModelSun(TModel* _mptr, float x0, float y0, float z0, int Alpha)
  2141. {
  2142. int f;
  2143. mptr = _mptr;
  2144. int minx = 10241024;
  2145. int maxx =-10241024;
  2146. int miny = 10241024;
  2147. int maxy =-10241024;
  2148. gvtx[0].r = 255.f; gvtx[0].g = gvtx[0].r; gvtx[0].b = gvtx[0].r;
  2149. gvtx[1].r = gvtx[0].r; gvtx[1].g = gvtx[0].g; gvtx[1].b = gvtx[0].b;
  2150. gvtx[2].r = gvtx[0].r; gvtx[2].g = gvtx[0].g; gvtx[2].b = gvtx[0].b;
  2151. gvtx[0].z = 1.0f;
  2152. gvtx[1].z = 1.0f;
  2153. gvtx[2].z = 1.0f;
  2154. gvtx[0].oow = 1.0f;
  2155. gvtx[1].oow = 1.0f;
  2156. gvtx[2].oow = 1.0f;
  2157. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
  2158. for (int s=0; s<mptr->VCount; s++) {
  2159. rVertex[s].x = mptr->gVertex[s].x + x0;
  2160. rVertex[s].y = mptr->gVertex[s].y + y0;
  2161. rVertex[s].z = mptr->gVertex[s].z + z0;
  2162. if (rVertex[s].z>-64) gScrp[s].x = 0xFFFFFF; else {
  2163. gScrp[s].x = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  2164. gScrp[s].y = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH); }
  2165. if (gScrp[s].x > maxx) maxx = gScrp[s].x;
  2166. if (gScrp[s].x < minx) minx = gScrp[s].x;
  2167. if (gScrp[s].y > maxy) maxy = gScrp[s].y;
  2168. if (gScrp[s].y < miny) miny = gScrp[s].y;
  2169. }
  2170. if (minx == 10241024) return;
  2171. if (minx>WinW || maxx<0 || miny>WinH || maxy<0) return;
  2172. BuildTreeNoSort();
  2173. /*if (LOWRESTX) SetFXTexture(mptr->lpTexture2, 128, 128);
  2174. else SetFXTexture(mptr->lpTexture, 256, 256);*/
  2175. SetFXTexture(mptr->lpTexture2, 128, 128);
  2176. grConstantColorValue(Alpha<<24);
  2177. grAlphaBlendFunction(GR_BLEND_SRC_ALPHA,
  2178. GR_BLEND_ONE,
  2179. GR_BLEND_ONE,
  2180. GR_BLEND_ONE);
  2181. f = Current;
  2182. while( f!=-1 ) {
  2183. TFace *fptr = & mptr->gFace[f];
  2184. gvtx[0].x = (float)gScrp[fptr->v1].x;
  2185. gvtx[0].y = (float)gScrp[fptr->v1].y;
  2186. gvtx[0].ooz = _ZSCALE/-rVertex[fptr->v1].z;
  2187. gvtx[0].tmuvtx[0].sow = fptr->tax;
  2188. gvtx[0].tmuvtx[0].tow = fptr->tay;
  2189. gvtx[1].x = (float)gScrp[fptr->v2].x;
  2190. gvtx[1].y = (float)gScrp[fptr->v2].y;
  2191. gvtx[1].ooz = _ZSCALE / -rVertex[fptr->v2].z;
  2192. gvtx[1].tmuvtx[0].sow = fptr->tbx;
  2193. gvtx[1].tmuvtx[0].tow = fptr->tby;
  2194. gvtx[2].x = (float)gScrp[fptr->v3].x;
  2195. gvtx[2].y = (float)gScrp[fptr->v3].y;
  2196. gvtx[2].ooz = _ZSCALE/-rVertex[fptr->v3].z;
  2197. gvtx[2].tmuvtx[0].sow = fptr->tcx;
  2198. gvtx[2].tmuvtx[0].tow = fptr->tcy;
  2199. grDrawTriangle(&gvtx[0], &gvtx[1], &gvtx[2]);
  2200. dFacesCount++;
  2201. f = mptr->gFace[f].Next;
  2202. }
  2203. grTexFilterMode(GR_TMU0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
  2204. grChromakeyMode(GR_CHROMAKEY_DISABLE);
  2205. grConstantColorValue(0xFF000000);
  2206. grAlphaBlendFunction(GR_BLEND_SRC_ALPHA,
  2207. GR_BLEND_ONE_MINUS_SRC_ALPHA,
  2208. GR_BLEND_ONE,
  2209. GR_BLEND_ONE);
  2210. }
  2211. void RenderNearModel(TModel* _mptr, float x0, float y0, float z0, int light, float al, float bt)
  2212. {
  2213. BOOL bl = LOWRESTX;
  2214. Vector3d v;
  2215. v.x = 0; v.y =-128; v.z = 0;
  2216. CalcFogLevel_Gradient(v);
  2217. grFogColorValue(CurFogColor);
  2218. FogYGrad = 0;
  2219. LOWRESTX = FALSE;
  2220. RenderModelClip(_mptr, x0, y0, z0, light, 0, al, bt);
  2221. LOWRESTX = bl;
  2222. }
  2223. void RenderModelClipWater(TModel* _mptr, float x0, float y0, float z0, int light, int VT, float al, float bt)
  2224. {
  2225. }
  2226. void RenderCharacter(int index)
  2227. {
  2228. }
  2229. void RenderExplosion(int index)
  2230. {
  2231. }
  2232. void RenderShip()
  2233. {
  2234. }
  2235. void RenderCharacterPost(TCharacter *cptr)
  2236. {
  2237. //mdlScale = 1.0f;
  2238. CreateChMorphedModel(cptr);
  2239. float zs = (float)sqrt( cptr->rpos.x*cptr->rpos.x + cptr->rpos.y*cptr->rpos.y + cptr->rpos.z*cptr->rpos.z);
  2240. if (zs > ctViewR*256) return;
  2241. GlassL = 0;
  2242. if (zs > 256 * (ctViewR-8)) {
  2243. GlassL = (int)(zs - 256 * (ctViewR-8)) / 8;
  2244. if (GlassL>255) GlassL=255;
  2245. }
  2246. waterclip = FALSE;
  2247. grConstantColorValue( (255-GlassL) << 24);
  2248. if ( cptr->rpos.z >-256*10)
  2249. RenderModelClip(cptr->pinfo->mptr,
  2250. cptr->rpos.x, cptr->rpos.y, cptr->rpos.z, 210, 0,
  2251. -cptr->alpha + pi / 2 + CameraAlpha,
  2252. CameraBeta );
  2253. else
  2254. RenderModel(cptr->pinfo->mptr,
  2255. cptr->rpos.x, cptr->rpos.y, cptr->rpos.z, 210, 0,
  2256. -cptr->alpha + pi / 2 + CameraAlpha,
  2257. CameraBeta );
  2258. grConstantColorValue(0xFF000000);
  2259. if (!SHADOWS3D) return;
  2260. if (zs > 256 * (ctViewR-8)) return;
  2261. int Al = 0x50;
  2262. if (cptr->Health==0) {
  2263. int at = cptr->pinfo->Animation[cptr->Phase].AniTime;
  2264. if (Tranq) return;
  2265. if (cptr->FTime==at-1) return;
  2266. Al = Al * (at-cptr->FTime) / at; }
  2267. if (cptr->AI==0) Al = 0x50;
  2268. grConstantColorValue(Al<<24);
  2269. RenderShadowClip(cptr->pinfo->mptr,
  2270. cptr->pos.x, cptr->pos.y, cptr->pos.z,
  2271. cptr->rpos.x, cptr->rpos.y, cptr->rpos.z,
  2272. pi/2-cptr->alpha,
  2273. CameraAlpha,
  2274. CameraBeta );
  2275. grConstantColorValue(0xFF000000);
  2276. }
  2277. void RenderShipPost()
  2278. {
  2279. if (Ship.State==-1) return;
  2280. GlassL = 0;
  2281. zs = (int)VectorLength(Ship.rpos);
  2282. if (zs > 256 * (ctViewR)) return;
  2283. if (zs > 256 * (ctViewR-4))
  2284. GlassL = min(255,(int)(zs - 256 * (ctViewR-4)) / 4);
  2285. grConstantColorValue( (255-GlassL) << 24);
  2286. CreateMorphedModel(ShipModel.mptr, &ShipModel.Animation[0], Ship.FTime, 1.0);
  2287. if ( fabs(Ship.rpos.z) < 4000)
  2288. RenderModelClip(ShipModel.mptr,
  2289. Ship.rpos.x, Ship.rpos.y, Ship.rpos.z, 210, 0, -Ship.alpha -pi/2 + CameraAlpha, CameraBeta);
  2290. else
  2291. RenderModel(ShipModel.mptr,
  2292. Ship.rpos.x, Ship.rpos.y, Ship.rpos.z, 210, 0, -Ship.alpha -pi/2+ CameraAlpha, CameraBeta);
  2293. grConstantColorValue( 0xFF000000);
  2294. }
  2295. float GetLandUH(float x, float y, int s)
  2296. {
  2297. int CX = (int)x / 256;
  2298. int CY = (int)y / 256;
  2299. int dx = (int)x % 256;
  2300. int dy = (int)y % 256;
  2301. /*
  2302. int h1 = HMap[CY][CX];
  2303. int h2 = HMap[CY][CX+1];
  2304. int h3 = HMap[CY+1][CX+1];
  2305. int h4 = HMap[CY+1][CX];
  2306. */
  2307. float h = (float)
  2308. (HMap[CY][CX] * (256-dx) + HMap[CY][CX+1] * dx) * (256-dy) +
  2309. (HMap[CY+1][CX] * (256-dx) + HMap[CY+1][CX+1] * dx) * dy;
  2310. return h / 256.f / 256.f * ctHScale;
  2311. }
  2312. /*
  2313. void RenderVoxelEnd()
  2314. {
  2315. Vector3d lv, gv, cp, p;
  2316. int sy, _sy, t, l;
  2317. DWORD alpha;
  2318. //if (!RunMode) return;
  2319. guColorCombineFunction( GR_COLORCOMBINE_ITRGB );
  2320. for (int x=0; x<=WinW; x+=2) {
  2321. lv.y = 0;
  2322. lv.z = -1;
  2323. lv.x = (x-VideoCX)/CameraW *(1+cb)/2;
  2324. NormVector(lv, 256.0f);
  2325. gv.z = lv.z * ca + lv.x * sa;
  2326. gv.x = lv.x * ca - lv.z * sa;
  2327. gv.y = 0;
  2328. lv = gv;
  2329. NormVector(lv, (ctViewR-10)*256.0f);
  2330. cp = AddVectors(CameraPos, lv);
  2331. p = cp; p.y = GetLandUpH(p.x, p.z);
  2332. t = TMap1[(int)(p.z)>>8][(int)(p.x)>>8];
  2333. l = 256-GetLandLt(p.x, p.z)*4;
  2334. p.x -= CameraX;
  2335. p.y -= CameraY;
  2336. p.z -= CameraZ;
  2337. p = RotateVector(p);
  2338. _sy = VideoCY + CameraH * p.y / p.z;
  2339. gvtx[0].x=x;
  2340. gvtx[1].x=x;
  2341. gvtx[0].a=16;
  2342. gvtx[1].a=16;
  2343. gvtx[0].r = Textures[t]->mR*l/256;
  2344. gvtx[0].g = Textures[t]->mG*l/256;
  2345. gvtx[0].b = Textures[t]->mB*l/256;
  2346. gvtx[0].ooz = (-1.0f/p.z)*4.f*65534.f;
  2347. gvtx[0].oow = 1.0f;
  2348. gvtx[0].y = _sy;
  2349. gvtx[1] = gvtx[0];
  2350. gv.y = 0;
  2351. for (int s=0; s<30; s++) {
  2352. //if (s==15) NormVector(gv, 512.0f);
  2353. cp.y = GetLandUH(p.x, p.z, s);
  2354. if (s>4) {
  2355. cp = AddVectors(cp, gv);
  2356. cp.y = max(cp.y,GetLandUH(cp.x, cp.z, s));
  2357. }
  2358. if (s>15) {
  2359. cp = AddVectors(cp, gv);
  2360. cp.y = max(cp.y,GetLandUH(cp.x, cp.z, s));
  2361. cp = AddVectors(cp, gv);
  2362. cp.y = max(cp.y,GetLandUH(cp.x, cp.z, s));
  2363. }
  2364. p = cp;
  2365. t = TMap1[(int)(cp.z)>>8][(int)(cp.x)>>8];
  2366. p.x -= CameraX;
  2367. p.y -= CameraY;
  2368. p.z -= CameraZ;
  2369. p = RotateVector(p);
  2370. sy = VideoCY + CameraH * p.y / p.z;
  2371. if (sy < _sy-1)
  2372. {
  2373. //l = 256-LMap[(int)cp.z/256][(int)cp.x/256]*4;
  2374. l = 256-GetLandLt(cp.x, cp.z)*4;
  2375. gvtx[1].a=s*4+16;
  2376. gvtx[1].ooz = (-1.0f/p.z)*4.f*65534.f;
  2377. gvtx[1].oow = 1.0f;
  2378. gvtx[1].y = sy;
  2379. gvtx[1].r = Textures[t]->mR*l/256;
  2380. gvtx[1].g = Textures[t]->mG*l/256;
  2381. gvtx[1].b = Textures[t]->mB*l/256;
  2382. grConstantColorValue(alpha +
  2383. (Textures[t]->mR*l/256) +
  2384. ((Textures[t]->mG*l/256)<<8) +
  2385. ((Textures[t]->mB*l/256)<<16) );
  2386. gvtx[0].x = x; gvtx[1].x = x;
  2387. grDrawLine(&gvtx[0], &gvtx[1]);
  2388. gvtx[0].x = x+1; gvtx[1].x = x+1;
  2389. grDrawLine(&gvtx[0], &gvtx[1]);
  2390. dFacesCount++;
  2391. gvtx[0] = gvtx[1];
  2392. _sy = sy;
  2393. } else gvtx[0].a=s*4+16;
  2394. cp = AddVectors(cp, gv);
  2395. }
  2396. }
  2397. guColorCombineFunction( GR_COLORCOMBINE_TEXTURE_TIMES_ITRGB );
  2398. grConstantColorValue(0xFF000000);
  2399. }
  2400. */
  2401. void RenderElements()
  2402. {
  2403. guColorCombineFunction( GR_COLORCOMBINE_ITRGB );
  2404. guAlphaSource(GR_ALPHASOURCE_ITERATED_ALPHA);
  2405. grFogMode(GR_FOG_DISABLE);
  2406. grDepthMask( FXFALSE );
  2407. for (int eg = 0; eg<ElCount; eg++) {
  2408. for (int e = 0; e<Elements[eg].ECount; e++) {
  2409. Vector3d rpos;
  2410. TElement *el = &Elements[eg].EList[e];
  2411. rpos.x = el->pos.x - CameraX;
  2412. rpos.y = el->pos.y - CameraY;
  2413. rpos.z = el->pos.z - CameraZ;
  2414. float r = el->R;
  2415. rpos = RotateVector(rpos);
  2416. if (rpos.z > -64) continue;
  2417. if ( fabs(rpos.x) > -rpos.z) continue;
  2418. if ( fabs(rpos.y) > -rpos.z) continue;
  2419. float sx = VideoCX - (int)(CameraW * rpos.x / rpos.z * 16) / 16.f;
  2420. float sy = VideoCY + (int)(CameraH * rpos.y / rpos.z * 16) / 16.f;
  2421. RenderCircle(sx, sy, rpos.z, -r*CameraW*0.64 / rpos.z, Elements[eg].RGBA, Elements[eg].RGBA2);
  2422. }
  2423. }
  2424. for (int b=0; b<BloodTrail.Count; b++) {
  2425. Vector3d rpos = BloodTrail.Trail[b].pos;
  2426. DWORD A1 = (0xE0 * BloodTrail.Trail[b].LTime / 20000); if (A1>0xE0) A1=0xE0;
  2427. DWORD A2 = (0x20 * BloodTrail.Trail[b].LTime / 20000); if (A2>0x20) A2=0x20;
  2428. rpos.x = rpos.x - CameraX;
  2429. rpos.y = rpos.y - CameraY;
  2430. rpos.z = rpos.z - CameraZ;
  2431. rpos = RotateVector(rpos);
  2432. if (rpos.z > -64) continue;
  2433. if ( fabs(rpos.x) > -rpos.z) continue;
  2434. if ( fabs(rpos.y) > -rpos.z) continue;
  2435. float sx = VideoCX - (int)(CameraW * rpos.x / rpos.z * 16) / 16.f;
  2436. float sy = VideoCY + (int)(CameraH * rpos.y / rpos.z * 16) / 16.f;
  2437. RenderCircle(sx, sy, rpos.z, -12*CameraW*0.64 / rpos.z, (A1<<24)+conv_xGx(0x000070), (A2<<24)+conv_xGx(0x000030));
  2438. }
  2439. guAlphaSource(GR_ALPHASOURCE_CC_ALPHA);
  2440. guColorCombineFunction( GR_COLORCOMBINE_TEXTURE_TIMES_ITRGB );
  2441. if (FOGON) grFogMode(GR_FOG_WITH_ITERATED_ALPHA);
  2442. grDepthMask( FXTRUE );
  2443. }
  2444. void Render3DHardwarePosts()
  2445. {
  2446. TCharacter *cptr;
  2447. for (int c=0; c<ChCount; c++) {
  2448. //if (!RunMode) continue;
  2449. cptr = &Characters[c];
  2450. cptr->rpos.x = cptr->pos.x - CameraX;
  2451. cptr->rpos.y = cptr->pos.y - CameraY;
  2452. cptr->rpos.z = cptr->pos.z - CameraZ;
  2453. float r = (float)max( fabs(cptr->rpos.x), fabs(cptr->rpos.z) );
  2454. int ri = -1 + (int)(r / 256.f + 0.5f);
  2455. if (ri < 0) ri = 0;
  2456. if (ri > ctViewR) continue;
  2457. if (FOGON) {
  2458. CalcFogLevel_Gradient(cptr->rpos);
  2459. grFogColorValue(CurFogColor);
  2460. }
  2461. cptr->rpos = RotateVector(cptr->rpos);
  2462. float br = BackViewR + DinoInfo[cptr->CType].Radius;
  2463. if (cptr->rpos.z > br) continue;
  2464. if ( fabs(cptr->rpos.x) > -cptr->rpos.z + br ) continue;
  2465. if ( fabs(cptr->rpos.y) > -cptr->rpos.z + br ) continue;
  2466. RenderCharacterPost(cptr);
  2467. }
  2468. Ship.rpos.x = Ship.pos.x - CameraX;
  2469. Ship.rpos.y = Ship.pos.y - CameraY;
  2470. Ship.rpos.z = Ship.pos.z - CameraZ;
  2471. float r = (float)max( fabs(Ship.rpos.x), fabs(Ship.rpos.z) );
  2472. int ri = -1 + (int)(r / 256.f + 0.2f);
  2473. if (ri < 0) ri = 0;
  2474. if (ri < ctViewR) {
  2475. if (FOGON) {
  2476. CalcFogLevel_Gradient(Ship.rpos);
  2477. grFogColorValue(CurFogColor);
  2478. }
  2479. Ship.rpos = RotateVector(Ship.rpos);
  2480. if (Ship.rpos.z > BackViewR) goto NOSHIP;
  2481. if ( fabs(Ship.rpos.x) > -Ship.rpos.z + BackViewR ) goto NOSHIP;
  2482. RenderShipPost();
  2483. }
  2484. NOSHIP: ;
  2485. }
  2486. void ClearVideoBuf()
  2487. {
  2488. //grBufferClear( 0xFF000000, 0, 0);
  2489. }
  2490. int CircleCX, CircleCY;
  2491. WORD _CRCOLOR;
  2492. void PutPixel(int x, int y)
  2493. { *((WORD*)linfo.lfbPtr + y*lsw + x) = _CRCOLOR; }
  2494. void Put8pix(int X,int Y)
  2495. {
  2496. PutPixel(CircleCX + X, CircleCY + Y);
  2497. PutPixel(CircleCX + X, CircleCY - Y);
  2498. PutPixel(CircleCX - X, CircleCY + Y);
  2499. PutPixel(CircleCX - X, CircleCY - Y);
  2500. PutPixel(CircleCX + Y, CircleCY + X);
  2501. PutPixel(CircleCX + Y, CircleCY - X);
  2502. PutPixel(CircleCX - Y, CircleCY + X);
  2503. PutPixel(CircleCX - Y, CircleCY - X);
  2504. }
  2505. void DrawCircle(int cx, int cy, int R)
  2506. {
  2507. int d = 3 - (2 * R);
  2508. int x = 0;
  2509. int y = R;
  2510. CircleCX=cx;
  2511. CircleCY=cy;
  2512. do {
  2513. Put8pix(x,y); x++;
  2514. if (d < 0) d = d + (x<<2) + 6; else
  2515. { d = d + (x - y) * 4 + 10; y--; }
  2516. } while (x<y);
  2517. Put8pix(x,y);
  2518. }
  2519. void DrawBox( WORD *lfbPtr, int lsw, int xx, int yy, WORD c)
  2520. {
  2521. yy = yy * lsw + xx;
  2522. *(lfbPtr + yy) = c;
  2523. *(lfbPtr + yy + 1) = c;
  2524. yy+=lsw;
  2525. *(lfbPtr + yy) = c;
  2526. *(lfbPtr + yy + 1) = c;
  2527. }
  2528. void DrawHMap()
  2529. {
  2530. int c;
  2531. DrawPicture(VideoCX-MapPic.W/2, VideoCY - MapPic.H/2-6, MapPic);
  2532. linfo.size = sizeof(GrLfbInfo_t);
  2533. if (!grLfbLock(
  2534. GR_LFB_WRITE_ONLY,
  2535. GR_BUFFER_BACKBUFFER,
  2536. GR_LFBWRITEMODE_565,
  2537. GR_ORIGIN_UPPER_LEFT,
  2538. FXFALSE,
  2539. &linfo)) return;
  2540. lsw = linfo.strideInBytes / 2;
  2541. int xx = VideoCX - 128 + (CCX>>2);
  2542. int yy = VideoCY - 128 + (CCY>>2);
  2543. if (yy<0 || yy>=WinH) goto endmap;
  2544. if (xx<0 || xx>=WinW) goto endmap;
  2545. DrawBox( (WORD*)linfo.lfbPtr, lsw, xx+1, yy+1, 8<<11);
  2546. DrawBox( (WORD*)linfo.lfbPtr, lsw, xx, yy, 30<<11);
  2547. _CRCOLOR = 4<<6; DrawCircle(xx+1, yy+1, (ctViewR/4));
  2548. _CRCOLOR = 18<<6; DrawCircle(xx, yy, (ctViewR/4));
  2549. if (RadarMode)
  2550. for (c=0; c<ChCount; c++) {
  2551. if (Characters[c].AI<10) continue;
  2552. if (! (TargetDino & (1<<Characters[c].AI)) ) continue;
  2553. if (!Characters[c].Health) continue;
  2554. xx = VideoCX - 128 + (int)Characters[c].pos.x / 1024;
  2555. yy = VideoCY - 128 + (int)Characters[c].pos.z / 1024;
  2556. if (yy<=0 || yy>=WinH) goto endmap;
  2557. if (xx<=0 || xx>=WinW) goto endmap;
  2558. DrawBox( (WORD*)linfo.lfbPtr, lsw, xx, yy, 30<<6);
  2559. }
  2560. endmap:
  2561. grLfbUnlock(GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER);
  2562. }
  2563. void RenderSun(float x, float y, float z)
  2564. {
  2565. SunScrX = VideoCX + (int)(x / (-z) * CameraW);
  2566. SunScrY = VideoCY - (int)(y / (-z) * CameraH);
  2567. GetSkyK(SunScrX, SunScrY);
  2568. float d = (float)sqrt(x*x + y*y);
  2569. if (d<2048) {
  2570. SunLight = (220.f- d*220.f/2048.f);
  2571. if (SunLight>140) SunLight = 140;
  2572. SunLight*=SkyTraceK;
  2573. }
  2574. if (d>812.f) d = 812.f;
  2575. d = (2048.f + d) / 3048.f;
  2576. d+=(1.f-SkyTraceK)/2.f;
  2577. if (OptDayNight==2) d=1.5;
  2578. RenderModelSun(SunModel, x*d, y*d, z*d, (int)(200.f*SkyTraceK));
  2579. }
  2580. void RotateVVector(Vector3d& v)
  2581. {
  2582. float x = v.x * ca - v.z * sa;
  2583. float y = v.y;
  2584. float z = v.z * ca + v.x * sa;
  2585. float xx = x;
  2586. float xy = y * cb + z * sb;
  2587. float xz = z * cb - y * sb;
  2588. v.x = xx; v.y = xy; v.z = xz;
  2589. }
  2590. void RenderSkyPlane2()
  2591. {
  2592. Vector3d v,vbase;
  2593. Vector3d tx,ty,nv;
  2594. float p,q, qx, qy, qz, px, py, pz, rx, ry, rz, ddx, ddy;
  2595. float lastdt = 0.f;
  2596. nv.x = 512; nv.y = 4024; nv.z=0;
  2597. int FogBase = (int)CalcFogLevel(nv);
  2598. cb = (float)cos(CameraBeta);
  2599. sb = (float)sin(CameraBeta);
  2600. //SKYDTime = RealTime & ((1<<16) - 1);
  2601. float sh = - CameraY;
  2602. if (MapMinY==10241024) MapMinY=0;
  2603. sh = (float)((int)MapMinY)*ctHScale - CameraY;
  2604. v.x = 0;
  2605. v.z = (ctViewR*4.f)/5.f*256.f;
  2606. v.y = sh;
  2607. vbase.x = v.x;
  2608. vbase.y = v.y * cb + v.z * sb;
  2609. vbase.z = v.z * cb - v.y * sb;
  2610. if (vbase.z < 128) vbase.z = 128;
  2611. int scry = VideoCY - (int)(vbase.y / vbase.z * CameraH);
  2612. if (scry<0) return;
  2613. if (scry>WinEY+1) scry = WinEY+1;
  2614. cb = (float)cos(CameraBeta-0.15);
  2615. sb = (float)sin(CameraBeta-0.15);
  2616. tx.x=0.0025f; tx.y=0; tx.z=0;
  2617. ty.x=0.0f; ty.y=0; ty.z=0.0025f;
  2618. nv.x=0; nv.y=-1.f; nv.z=0;
  2619. RotateVVector(tx);
  2620. RotateVVector(ty);
  2621. RotateVVector(nv);
  2622. sh = 4*512*16;
  2623. vbase.x = -CameraX;
  2624. vbase.y = sh;
  2625. vbase.z = +CameraZ;
  2626. RotateVVector(vbase);
  2627. //============= calc render params =================//
  2628. p = nv.x * vbase.x + nv.y * vbase.y + nv.z * vbase.z;
  2629. ddx = vbase.x * tx.x + vbase.y * tx.y + vbase.z * tx.z;
  2630. ddy = vbase.x * ty.x + vbase.y * ty.y + vbase.z * ty.z;
  2631. qx = CameraH * nv.x; qy = CameraW * nv.y; qz = CameraW*CameraH * nv.z;
  2632. px = p*CameraH*tx.x; py = p*CameraW*tx.y; pz = p*CameraW*CameraH* tx.z;
  2633. rx = p*CameraH*ty.x; ry = p*CameraW*ty.y; rz = p*CameraW*CameraH* ty.z;
  2634. px=px - ddx*qx; py=py - ddx*qy; pz=pz - ddx*qz;
  2635. rx=rx - ddy*qx; ry=ry - ddy*qy; rz=rz - ddy*qz;
  2636. int sx1 = - VideoCX;
  2637. int sx2 = + VideoCX;
  2638. float qx1 = qx * sx1 + qz;
  2639. float qx2 = qx * sx2 + qz;
  2640. float qyy;
  2641. grConstantColorValue(0xFF000000);
  2642. grTexClampMode(GR_TMU0, GR_TEXTURECLAMP_WRAP, GR_TEXTURECLAMP_WRAP);
  2643. guAlphaSource(GR_ALPHASOURCE_ITERATED_ALPHA);
  2644. grFogMode(GR_FOG_DISABLE);
  2645. float l = 255.f;
  2646. gvtx[0].r = l; gvtx[0].g = l; gvtx[0].b = l;
  2647. gvtx[1].r = l; gvtx[1].g = l; gvtx[1].b = l;
  2648. gvtx[0].a = 0.5f;
  2649. gvtx[1].a = 0.5f;
  2650. gvtx[0].x = 0;
  2651. gvtx[1].x = (float)WinEX+1;
  2652. gvtx[0].z = (float)1.f;
  2653. gvtx[1].z = (float)1.f;
  2654. gvtx[0].oow = 1.0f;
  2655. gvtx[1].oow = 1.0f;
  2656. for (int sky=0; sky<=scry; sky++) {
  2657. int sy = VideoCY - sky;
  2658. qyy = qy * sy;
  2659. q = qx1 + qyy;
  2660. float fxa = (px * sx1 + py * sy + pz) / q;
  2661. float fya = (rx * sx1 + ry * sy + rz) / q;
  2662. q = qx2 + qyy;
  2663. float fxb = (px * sx2 + py * sy + pz) / q;
  2664. float fyb = (rx * sx2 + ry * sy + rz) / q;
  2665. float dtt = (float)(SKYDTime) / 256.f;
  2666. float dt = ((float)sqrt( (fxb-fxa)*(fxb-fxa) + (fyb-fya)*(fyb-fya) ) / 0x40 ) - 4.f;
  2667. if (UNDERWATER) dt=6 + dt*3;
  2668. if (dt>10.f) dt = 10.f;
  2669. if (dt<lastdt) dt = lastdt;
  2670. lastdt = dt;
  2671. gvtx[0].a = max(164-dt*245.f/10.f, 0);
  2672. gvtx[1].a = gvtx[0].a;
  2673. gvtx[0].y = (float)sky;
  2674. gvtx[0].ooz = (_ZSCALE/gvtx[0].z);
  2675. gvtx[0].tmuvtx[0].sow = fxa + dtt;
  2676. gvtx[0].tmuvtx[0].tow = fya - dtt;
  2677. gvtx[1].y = (float)sky;
  2678. gvtx[1].ooz = (_ZSCALE/gvtx[1].z);
  2679. gvtx[1].tmuvtx[0].sow = fxb + dtt;
  2680. gvtx[1].tmuvtx[0].tow = fyb - dtt;
  2681. grDrawLine(&gvtx[0], &gvtx[1]);
  2682. dFacesCount++;
  2683. }
  2684. cb = (float)cos(CameraBeta);
  2685. sb = (float)sin(CameraBeta);
  2686. guAlphaSource(GR_ALPHASOURCE_CC_ALPHA);
  2687. }
  2688. void RenderSkyPlane()
  2689. {
  2690. Vector3d v,vbase;
  2691. Vector3d tx,ty,nv;
  2692. float p,q, qx, qy, qz, px, py, pz, rx, ry, rz, ddx, ddy;
  2693. float lastdt = 0.f;
  2694. SetFXTexture(SkyPic, 256, 256);
  2695. nv.x = 512; nv.y = 4024; nv.z=0;
  2696. int FogBase = (int)CalcFogLevel(nv);
  2697. cb = (float)cos(CameraBeta);
  2698. sb = (float)sin(CameraBeta);
  2699. SKYDTime = RealTime & ((1<<18) - 1);
  2700. float sh = - CameraY;
  2701. if (MapMinY==10241024) MapMinY=0;
  2702. sh = (float)((int)MapMinY)*ctHScale - CameraY;
  2703. v.x = 0;
  2704. v.z = (ctViewR*4.f)/5.f*256.f;
  2705. v.y = sh;
  2706. vbase.x = v.x;
  2707. vbase.y = v.y * cb + v.z * sb;
  2708. vbase.z = v.z * cb - v.y * sb;
  2709. if (vbase.z < 128) vbase.z = 128;
  2710. int scry = VideoCY - (int)(vbase.y / vbase.z * CameraH);
  2711. if (scry<0) return;
  2712. if (scry>WinEY+1) scry = WinEY+1;
  2713. cb = (float)cos(CameraBeta-0.15);
  2714. sb = (float)sin(CameraBeta-0.15);
  2715. tx.x=0.002f; tx.y=0; tx.z=0;
  2716. ty.x=0.0f; ty.y=0; ty.z=0.002f;
  2717. nv.x=0; nv.y=-1.f; nv.z=0;
  2718. RotateVVector(tx);
  2719. RotateVVector(ty);
  2720. RotateVVector(nv);
  2721. sh = 4*512*16;
  2722. vbase.x = -CameraX;
  2723. vbase.y = sh;
  2724. vbase.z = +CameraZ;
  2725. RotateVVector(vbase);
  2726. //============= calc render params =================//
  2727. p = nv.x * vbase.x + nv.y * vbase.y + nv.z * vbase.z;
  2728. ddx = vbase.x * tx.x + vbase.y * tx.y + vbase.z * tx.z;
  2729. ddy = vbase.x * ty.x + vbase.y * ty.y + vbase.z * ty.z;
  2730. qx = CameraH * nv.x; qy = CameraW * nv.y; qz = CameraW*CameraH * nv.z;
  2731. px = p*CameraH*tx.x; py = p*CameraW*tx.y; pz = p*CameraW*CameraH* tx.z;
  2732. rx = p*CameraH*ty.x; ry = p*CameraW*ty.y; rz = p*CameraW*CameraH* ty.z;
  2733. px=px - ddx*qx; py=py - ddx*qy; pz=pz - ddx*qz;
  2734. rx=rx - ddy*qx; ry=ry - ddy*qy; rz=rz - ddy*qz;
  2735. int sx1 = - VideoCX;
  2736. int sx2 = + VideoCX;
  2737. float qx1 = qx * sx1 + qz;
  2738. float qx2 = qx * sx2 + qz;
  2739. float qyy;
  2740. grDepthMask( FXFALSE );
  2741. grFogMode(GR_FOG_WITH_ITERATED_ALPHA);
  2742. grConstantColorValue(0xFF000000);
  2743. grTexClampMode(GR_TMU0, GR_TEXTURECLAMP_WRAP, GR_TEXTURECLAMP_WRAP);
  2744. if (UNDERWATER) grFogColorValue( CurFogColor );
  2745. else grFogColorValue( (SkyB<<16) + (SkyG<<8) + SkyR);
  2746. float l = 255.f;
  2747. gvtx[0].r = l; gvtx[0].g = l; gvtx[0].b = l;
  2748. gvtx[1].r = l; gvtx[1].g = l; gvtx[1].b = l;
  2749. gvtx[0].a = 0.f;
  2750. gvtx[1].a = 0.f;
  2751. gvtx[0].x = 0;
  2752. gvtx[1].x = (float)WinEX+1;
  2753. gvtx[0].z = (float)1.f;
  2754. gvtx[1].z = (float)1.f;
  2755. gvtx[0].oow = 1.0f;
  2756. gvtx[1].oow = 1.0f;
  2757. for (int sky=0; sky<=scry; sky++) {
  2758. int sy = VideoCY - sky;
  2759. qyy = qy * sy;
  2760. q = qx1 + qyy;
  2761. float fxa = (px * sx1 + py * sy + pz) / q;
  2762. float fya = (rx * sx1 + ry * sy + rz) / q;
  2763. q = qx2 + qyy;
  2764. float fxb = (px * sx2 + py * sy + pz) / q;
  2765. float fyb = (rx * sx2 + ry * sy + rz) / q;
  2766. float dtt = (float)(SKYDTime) / 512.f;
  2767. float dt = ((float)sqrt( (fxb-fxa)*(fxb-fxa) + (fyb-fya)*(fyb-fya) ) / 0x40 ) - 5.f;
  2768. if (UNDERWATER) dt=6 + dt*3;
  2769. if (dt>10.f) dt = 10.f;
  2770. if (dt<lastdt) dt = lastdt;
  2771. lastdt = dt;
  2772. gvtx[0].a = max(dt*245.f/10.f, FogBase);
  2773. gvtx[1].a = gvtx[0].a;
  2774. gvtx[0].y = (float)sky;
  2775. gvtx[0].ooz = (_ZSCALE/gvtx[0].z);
  2776. gvtx[0].tmuvtx[0].sow = fxa + dtt;
  2777. gvtx[0].tmuvtx[0].tow = fya - dtt;
  2778. gvtx[1].y = (float)sky;
  2779. gvtx[1].ooz = (_ZSCALE/gvtx[1].z);
  2780. gvtx[1].tmuvtx[0].sow = fxb + dtt;
  2781. gvtx[1].tmuvtx[0].tow = fyb - dtt;
  2782. grDrawLine(&gvtx[0], &gvtx[1]);
  2783. dFacesCount++;
  2784. }
  2785. //if (RunMode)
  2786. // RenderSkyPlane2();
  2787. if (!FOGON) grFogMode(GR_FOG_DISABLE);
  2788. else grFogMode(GR_FOG_WITH_ITERATED_ALPHA);
  2789. grTexClampMode(GR_TMU0, GR_TEXTURECLAMP_CLAMP, GR_TEXTURECLAMP_CLAMP);
  2790. cb = (float)cos(CameraBeta);
  2791. sb = (float)sin(CameraBeta);
  2792. gvtx[0].a = 0;
  2793. gvtx[1].a = 0;
  2794. gvtx[2].a = 0;
  2795. nv = RotateVector(Sun3dPos);
  2796. SunLight = 0;
  2797. if (nv.z < -2024) RenderSun(nv.x, nv.y, nv.z);
  2798. grDepthMask( FXTRUE );
  2799. grFogColorValue(CurFogColor);
  2800. }
  2801. void RenderHealthBar()
  2802. {
  2803. if (MyHealth >= 100000) return;
  2804. if (MyHealth == 000000) return;
  2805. int L = WinW / 4;
  2806. int x0 = WinW - (WinW / 20) - L;
  2807. int y0 = WinH / 40;
  2808. int G = min( (MyHealth * 240 / 100000), 160);
  2809. int R = min( ( (100000 - MyHealth) * 240 / 100000), 160);
  2810. int L0 = (L * MyHealth) / 100000;
  2811. int H = WinH / 200;
  2812. gvtx[0].tmuvtx[0].sow = 0.0;
  2813. gvtx[0].tmuvtx[0].tow = 0.0;
  2814. gvtx[0].z = (float)1.f;
  2815. gvtx[0].ooz = (_ZSCALE/gvtx[0].z);
  2816. gvtx[0].oow = 1.0f/gvtx[0].z;
  2817. gvtx[1].tmuvtx[0].sow = 0.0;
  2818. gvtx[1].tmuvtx[0].tow = 0.0;
  2819. gvtx[1].z = (float)1.f;
  2820. gvtx[1].ooz = (_ZSCALE/gvtx[0].z);
  2821. gvtx[1].oow = 1.0f/gvtx[0].z;
  2822. guColorCombineFunction( GR_COLORCOMBINE_CCRGB );
  2823. grConstantColorValue(0xD0000000);
  2824. for (int y=-1; y<3; y++) {
  2825. gvtx[0].x = (float)x0-1; gvtx[0].y = (float)y0+y;
  2826. gvtx[1].x = (float)x0+L0+1; gvtx[1].y = (float)y0+y;
  2827. grDrawLine(&gvtx[0], &gvtx[1]);
  2828. }
  2829. grConstantColorValue(0xFF000000 + (G<<8) + R);
  2830. gvtx[0].x = (float)x0; gvtx[0].y = (float)y0;
  2831. gvtx[1].x = (float)x0+L0; gvtx[1].y = (float)y0;
  2832. grDrawLine(&gvtx[0], &gvtx[1]);
  2833. gvtx[0].x = (float)x0; gvtx[0].y = (float)y0+1.f;
  2834. gvtx[1].x = (float)x0+L0; gvtx[1].y = (float)y0+1.f;
  2835. grDrawLine(&gvtx[0], &gvtx[1]);
  2836. guColorCombineFunction( GR_COLORCOMBINE_TEXTURE_TIMES_ITRGB );
  2837. grConstantColorValue(0xFF000000);
  2838. }
  2839. void Render_Cross(int sx, int sy)
  2840. {
  2841. guColorCombineFunction( GR_COLORCOMBINE_CCRGB );
  2842. grConstantColorValue(0x60000000);
  2843. gvtx[0].tmuvtx[0].sow = 0.0;
  2844. gvtx[0].tmuvtx[0].tow = 0.0;
  2845. gvtx[0].z = (float)1.f;
  2846. gvtx[0].ooz = (_ZSCALE/gvtx[0].z);
  2847. gvtx[0].oow = 1.0f/gvtx[0].z;
  2848. gvtx[1].tmuvtx[0].sow = 0.0;
  2849. gvtx[1].tmuvtx[0].tow = 0.0;
  2850. gvtx[1].z = (float)1.f;
  2851. gvtx[1].ooz = (_ZSCALE/gvtx[0].z);
  2852. gvtx[1].oow = 1.0f/gvtx[0].z;
  2853. float w = (float) WinW / 12.f;
  2854. gvtx[0].x = (float)(sx - w);
  2855. gvtx[0].y = (float)sy;
  2856. gvtx[1].x = (float)(sx + w);
  2857. gvtx[1].y = (float)sy;
  2858. grDrawLine(&gvtx[0], &gvtx[1]);
  2859. gvtx[0].x = (float)(sx );
  2860. gvtx[0].y = (float)(sy - w);
  2861. gvtx[1].x = (float)(sx );
  2862. gvtx[1].y = (float)(sy + w);
  2863. grDrawLine(&gvtx[0], &gvtx[1]);
  2864. guColorCombineFunction( GR_COLORCOMBINE_TEXTURE_TIMES_ITRGB );
  2865. grConstantColorValue(0xFF000000);
  2866. }
  2867. #endif