renderd3d.cpp 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334
  1. #ifdef _d3d
  2. #include "Hunt.h"
  3. #include "stdio.h"
  4. #define d3debvcG 400
  5. #define d3debvc 1024
  6. #undef TCMAX
  7. #undef TCMIN
  8. #define TCMAX (((128<<16)-62024) / 128.f / 65535.f)
  9. #define TCMIN (((000<<16)+62024) / 128.f / 65535.f)
  10. #define _ZSCALE - 16.f
  11. #define _AZSCALE (1.f / 16.f);
  12. Vector2di ORList[2048];
  13. int ORLCount = 0;
  14. LPDIRECTDRAWSURFACE lpddPrimary = NULL;
  15. LPDIRECTDRAWSURFACE lpddBack = NULL;
  16. LPDIRECTDRAWSURFACE lpddZBuffer = NULL;
  17. LPDIRECTDRAWSURFACE lpddTexture = NULL;
  18. DDSURFACEDESC ddsd;
  19. LPDIRECT3D lpd3d = NULL;
  20. LPDIRECT3DDEVICE lpd3dDevice = NULL;
  21. LPDIRECT3DVIEWPORT lpd3dViewport = NULL;
  22. LPDIRECT3DEXECUTEBUFFER lpd3dExecuteBuffer = NULL;
  23. LPDIRECT3DEXECUTEBUFFER lpd3dExecuteBufferG = NULL;
  24. LPD3DTLVERTEX lpVertex, lpVertexG;
  25. WORD *lpwTriCount;
  26. HRESULT hRes;
  27. D3DEXECUTEBUFFERDESC d3dExeBufDesc;
  28. D3DEXECUTEBUFFERDESC d3dExeBufDescG;
  29. LPD3DINSTRUCTION lpInstruction, lpInstructionG;
  30. LPD3DPROCESSVERTICES lpProcessVertices;
  31. LPD3DTRIANGLE lpTriangle;
  32. LPD3DLINE lpLine;
  33. LPD3DSTATE lpState;
  34. BOOL fDeviceFound = FALSE;
  35. DWORD dwDeviceBitDepth = 0UL;
  36. GUID guidDevice;
  37. char szDeviceName[256];
  38. char szDeviceDesc[256];
  39. D3DDEVICEDESC d3dHWDeviceDesc;
  40. D3DTEXTUREHANDLE hTexture, hGTexture;
  41. HDC ddBackDC;
  42. BOOL D3DACTIVE;
  43. BOOL VMFORMAT565;
  44. BOOL STARTCONV555;
  45. BOOL HANDLECHANGED;
  46. #define NUM_INSTRUCTIONS 5UL
  47. #define NUM_STATES 20UL
  48. #define NUM_PROCESSVERTICES 1UL
  49. void RenderFSRect(DWORD Color);
  50. void d3dDownLoadTexture(int i, int w, int h, LPVOID tptr);
  51. BOOL d3dAllocTexture(int i, int w, int h);
  52. void d3dSetTexture(LPVOID tptr, int w, int h);
  53. void RenderShadowClip (TModel*, float, float, float, float, float, float, int, float, float);
  54. void ResetTextureMap();
  55. int WaterAlphaL = 255;
  56. int d3dTexturesMem;
  57. int d3dLastTexture;
  58. int d3dMemUsageCount;
  59. int d3dMemLoaded;
  60. int GVCnt;
  61. BOOL LINEARFILTER = TRUE;
  62. int zs;
  63. float SunLight;
  64. float TraceK,SkyTraceK,FogYGrad,FogYBase;;
  65. int SunScrX, SunScrY;
  66. int SkySumR, SkySumG, SkySumB;
  67. int LowHardMemory;
  68. int lsw;
  69. int vFogT[1024];
  70. BOOL SmallFont;
  71. typedef struct _d3dmemmap {
  72. int cpuaddr, size, lastused;
  73. LPDIRECTDRAWSURFACE lpddTexture;
  74. D3DTEXTUREHANDLE hTexture;
  75. } Td3dmemmap;
  76. #define d3dmemmapsize 128
  77. Td3dmemmap d3dMemMap[d3dmemmapsize+2];
  78. WORD conv_555(WORD c)
  79. {
  80. return (c & 31) + ( (c & 0xFFE0) >> 1 );
  81. }
  82. void conv_pic555(TPicture &pic)
  83. {
  84. if (!HARD3D) return;
  85. for (int y=0; y<pic.H; y++)
  86. for (int x=0; x<pic.W; x++)
  87. *(pic.lpImage + x + y*pic.W) = conv_555(*(pic.lpImage + x + y*pic.W));
  88. }
  89. void CalcFogLevel_Gradient(Vector3d v)
  90. {
  91. FogYBase = CalcFogLevel(v);
  92. if (FogYBase>0) {
  93. v.y+=800;
  94. FogYGrad = (CalcFogLevel(v) - FogYBase) / 800.f;
  95. } else FogYGrad=0;
  96. }
  97. void Hardware_ZBuffer(BOOL bl)
  98. {
  99. if (!bl) {
  100. DDBLTFX ddbltfx;
  101. ddbltfx.dwSize = sizeof( DDBLTFX );
  102. ddbltfx.dwFillDepth = 0x0000;
  103. lpddZBuffer->Blt( NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &ddbltfx );
  104. }
  105. }
  106. void d3dClearBuffers()
  107. {
  108. DDBLTFX ddbltfx;
  109. ddbltfx.dwSize = sizeof( DDBLTFX );
  110. if (VMFORMAT565) ddbltfx.dwFillColor = (SkyR>>3)*32*32*2 + (SkyG>>2)*32 + (SkyB>>3);
  111. else ddbltfx.dwFillColor = (SkyR>>3)*32*32 + (SkyG>>3)*32 + (SkyB>>3);
  112. lpddBack->Blt( NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx );
  113. ddbltfx.dwSize = sizeof( DDBLTFX );
  114. ddbltfx.dwFillDepth = 0x0000;
  115. lpddZBuffer->Blt( NULL, NULL, NULL, DDBLT_DEPTHFILL | DDBLT_WAIT, &ddbltfx );
  116. }
  117. void d3dStartBuffer()
  118. {
  119. ZeroMemory(&d3dExeBufDesc, sizeof(d3dExeBufDesc));
  120. d3dExeBufDesc.dwSize = sizeof(d3dExeBufDesc);
  121. hRes = lpd3dExecuteBuffer->Lock( &d3dExeBufDesc );
  122. if (FAILED(hRes)) DoHalt("Error locking execute buffer");
  123. lpVertex = (LPD3DTLVERTEX)d3dExeBufDesc.lpData;
  124. }
  125. void d3dStartBufferG()
  126. {
  127. ZeroMemory(&d3dExeBufDescG, sizeof(d3dExeBufDescG));
  128. d3dExeBufDescG.dwSize = sizeof(d3dExeBufDescG);
  129. hRes = lpd3dExecuteBufferG->Lock( &d3dExeBufDescG );
  130. if (FAILED(hRes)) DoHalt("Error locking execute buffer");
  131. GVCnt = 0;
  132. hGTexture = -1;
  133. lpVertexG = (LPD3DTLVERTEX)d3dExeBufDescG.lpData;
  134. lpInstructionG = (LPD3DINSTRUCTION) ((LPD3DTLVERTEX)d3dExeBufDescG.lpData + d3debvcG*3);
  135. lpInstructionG->bOpcode = D3DOP_PROCESSVERTICES;
  136. lpInstructionG->bSize = sizeof(D3DPROCESSVERTICES);
  137. lpInstructionG->wCount = 1U;
  138. lpInstructionG++;
  139. lpProcessVertices = (LPD3DPROCESSVERTICES)lpInstructionG;
  140. lpProcessVertices->dwFlags = D3DPROCESSVERTICES_COPY;
  141. lpProcessVertices->wStart = 0U;
  142. lpProcessVertices->wDest = 0U;
  143. lpProcessVertices->dwCount = d3debvcG*3;
  144. lpProcessVertices->dwReserved = 0UL;
  145. lpProcessVertices++;
  146. lpInstructionG = (LPD3DINSTRUCTION)lpProcessVertices;
  147. if (!LINEARFILTER) {
  148. lpInstructionG->bOpcode = D3DOP_STATERENDER;
  149. lpInstructionG->bSize = sizeof(D3DSTATE);
  150. lpInstructionG->wCount = 2;
  151. lpInstructionG++;
  152. lpState = (LPD3DSTATE)lpInstructionG;
  153. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMAG;
  154. lpState->dwArg[0] = D3DFILTER_LINEAR;
  155. lpState++;
  156. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMIN;
  157. lpState->dwArg[0] = D3DFILTER_LINEAR;
  158. lpState++;
  159. lpInstructionG = (LPD3DINSTRUCTION)lpState;
  160. }
  161. if (FOGENABLE) {
  162. lpInstructionG->bOpcode = D3DOP_STATERENDER;
  163. lpInstructionG->bSize = sizeof(D3DSTATE);
  164. lpInstructionG->wCount = 1;
  165. lpInstructionG++;
  166. lpState = (LPD3DSTATE)lpInstructionG;
  167. lpState->drstRenderStateType = D3DRENDERSTATE_FOGCOLOR;
  168. lpState->dwArg[0] = CurFogColor;
  169. lpState++;
  170. lpInstructionG = (LPD3DINSTRUCTION)lpState;
  171. }
  172. }
  173. void d3dStartBufferGBMP()
  174. {
  175. ZeroMemory(&d3dExeBufDescG, sizeof(d3dExeBufDescG));
  176. d3dExeBufDescG.dwSize = sizeof(d3dExeBufDescG);
  177. hRes = lpd3dExecuteBufferG->Lock( &d3dExeBufDescG );
  178. if (FAILED(hRes)) DoHalt("Error locking execute buffer");
  179. GVCnt = 0;
  180. hGTexture = -1;
  181. lpVertexG = (LPD3DTLVERTEX)d3dExeBufDescG.lpData;
  182. lpInstructionG = (LPD3DINSTRUCTION) ((LPD3DTLVERTEX)d3dExeBufDescG.lpData + d3debvcG*3);
  183. lpInstructionG->bOpcode = D3DOP_PROCESSVERTICES;
  184. lpInstructionG->bSize = sizeof(D3DPROCESSVERTICES);
  185. lpInstructionG->wCount = 1U;
  186. lpInstructionG++;
  187. lpProcessVertices = (LPD3DPROCESSVERTICES)lpInstructionG;
  188. lpProcessVertices->dwFlags = D3DPROCESSVERTICES_COPY;
  189. lpProcessVertices->wStart = 0U;
  190. lpProcessVertices->wDest = 0U;
  191. lpProcessVertices->dwCount = d3debvcG*3;
  192. lpProcessVertices->dwReserved = 0UL;
  193. lpProcessVertices++;
  194. lpInstructionG = (LPD3DINSTRUCTION)lpProcessVertices;
  195. lpInstructionG->bOpcode = D3DOP_STATERENDER;
  196. lpInstructionG->bSize = sizeof(D3DSTATE);
  197. if (FOGENABLE) lpInstructionG->wCount = 6;
  198. else lpInstructionG->wCount = 5;
  199. lpInstructionG++;
  200. lpState = (LPD3DSTATE)lpInstructionG;
  201. lpState->drstRenderStateType = D3DRENDERSTATE_COLORKEYENABLE;
  202. lpState->dwArg[0] = TRUE;
  203. lpState++;
  204. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHATESTENABLE;
  205. lpState->dwArg[0] = TRUE;
  206. lpState++;
  207. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHAFUNC;
  208. lpState->dwArg[0] = D3DCMP_GREATER;
  209. lpState++;
  210. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMAG;
  211. lpState->dwArg[0] = D3DFILTER_NEAREST;
  212. lpState++;
  213. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMIN;
  214. lpState->dwArg[0] = D3DFILTER_NEAREST;
  215. lpState++;
  216. if (FOGENABLE) {
  217. lpState->drstRenderStateType = D3DRENDERSTATE_FOGCOLOR;
  218. lpState->dwArg[0] = CurFogColor;
  219. /*if (UNDERWATER) lpState->dwArg[0] = 0x00004560;
  220. else lpState->dwArg[0] = 0x00606065;*/
  221. lpState++;
  222. }
  223. lpInstructionG = (LPD3DINSTRUCTION)lpState;
  224. LINEARFILTER = FALSE;
  225. }
  226. void d3dEndBufferG(BOOL ColorKey)
  227. {
  228. if (!lpVertexG) return;
  229. if (ColorKey) {
  230. lpInstructionG->bOpcode = D3DOP_STATERENDER;
  231. lpInstructionG->bSize = sizeof(D3DSTATE);
  232. lpInstructionG->wCount = 5;
  233. lpInstructionG++;
  234. lpState = (LPD3DSTATE)lpInstructionG;
  235. lpState->drstRenderStateType = D3DRENDERSTATE_COLORKEYENABLE;
  236. lpState->dwArg[0] = FALSE;
  237. lpState++;
  238. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHATESTENABLE;
  239. lpState->dwArg[0] = FALSE;
  240. lpState++;
  241. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHAFUNC;
  242. lpState->dwArg[0] = D3DCMP_GREATEREQUAL;
  243. lpState++;
  244. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMAG;
  245. lpState->dwArg[0] = D3DFILTER_LINEAR;
  246. lpState++;
  247. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMIN;
  248. lpState->dwArg[0] = D3DFILTER_LINEAR;
  249. lpState++;
  250. lpInstructionG = (LPD3DINSTRUCTION)lpState;
  251. }
  252. lpInstructionG->bOpcode = D3DOP_EXIT;
  253. lpInstructionG->bSize = 0UL;
  254. lpInstructionG->wCount = 0U;
  255. lpInstructionG = (LPD3DINSTRUCTION) ((LPD3DTLVERTEX)d3dExeBufDescG.lpData + d3debvcG*3);
  256. lpInstructionG->bOpcode = D3DOP_PROCESSVERTICES;
  257. lpInstructionG->bSize = sizeof(D3DPROCESSVERTICES);
  258. lpInstructionG->wCount = 1U;
  259. lpInstructionG++;
  260. lpProcessVertices = (LPD3DPROCESSVERTICES)lpInstructionG;
  261. lpProcessVertices->dwFlags = D3DPROCESSVERTICES_COPY;
  262. lpProcessVertices->wStart = 0U;
  263. lpProcessVertices->wDest = 0U;
  264. lpProcessVertices->dwCount = GVCnt;
  265. lpProcessVertices->dwReserved = 0UL;
  266. lpProcessVertices++;
  267. lpd3dExecuteBufferG->Unlock( );
  268. dFacesCount+=GVCnt/3;
  269. lpInstructionG = NULL;
  270. lpVertexG = NULL;
  271. GVCnt = 0;
  272. hRes = lpd3dDevice->Execute(lpd3dExecuteBufferG, lpd3dViewport, D3DEXECUTE_UNCLIPPED);
  273. LINEARFILTER = TRUE;
  274. }
  275. void d3dFlushBuffer(int fproc1, int fproc2)
  276. {
  277. BOOL ColorKey = (fproc2>0);
  278. lpInstruction = (LPD3DINSTRUCTION) ((LPD3DTLVERTEX)d3dExeBufDesc.lpData + d3debvc*3);
  279. lpInstruction->bOpcode = D3DOP_STATERENDER;
  280. lpInstruction->bSize = sizeof(D3DSTATE);
  281. lpInstruction->wCount = 3;
  282. lpInstruction++;
  283. lpState = (LPD3DSTATE)lpInstruction;
  284. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREHANDLE;
  285. lpState->dwArg[0] = hTexture;
  286. lpState++;
  287. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMAG;
  288. lpState->dwArg[0] = D3DFILTER_LINEAR;
  289. lpState++;
  290. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMIN;
  291. lpState->dwArg[0] = D3DFILTER_LINEAR;
  292. lpState++;
  293. lpInstruction = (LPD3DINSTRUCTION)lpState;
  294. lpInstruction->bOpcode = D3DOP_PROCESSVERTICES;
  295. lpInstruction->bSize = sizeof(D3DPROCESSVERTICES);
  296. lpInstruction->wCount = 1U;
  297. lpInstruction++;
  298. lpProcessVertices = (LPD3DPROCESSVERTICES)lpInstruction;
  299. lpProcessVertices->dwFlags = D3DPROCESSVERTICES_COPY;
  300. lpProcessVertices->wStart = 0U;
  301. lpProcessVertices->wDest = 0U;
  302. lpProcessVertices->dwCount = (fproc1+fproc2)*3;
  303. lpProcessVertices->dwReserved = 0UL;
  304. lpProcessVertices++;
  305. lpInstruction = (LPD3DINSTRUCTION)lpProcessVertices;
  306. lpInstruction->bOpcode = D3DOP_TRIANGLE;
  307. lpInstruction->bSize = sizeof(D3DTRIANGLE);
  308. lpInstruction->wCount = fproc1;
  309. lpInstruction++;
  310. lpTriangle = (LPD3DTRIANGLE)lpInstruction;
  311. int ii = 0;
  312. for (int i=0; i<fproc1; i++) {
  313. lpTriangle->wV1 = ii++;
  314. lpTriangle->wV2 = ii++;
  315. lpTriangle->wV3 = ii++;
  316. lpTriangle->wFlags = 0;
  317. lpTriangle++;
  318. }
  319. lpInstruction = (LPD3DINSTRUCTION)lpTriangle;
  320. if (ColorKey) {
  321. lpInstruction->bOpcode = D3DOP_STATERENDER;
  322. lpInstruction->bSize = sizeof(D3DSTATE);
  323. lpInstruction->wCount = 5;
  324. lpInstruction++;
  325. lpState = (LPD3DSTATE)lpInstruction;
  326. lpState->drstRenderStateType = D3DRENDERSTATE_COLORKEYENABLE;
  327. lpState->dwArg[0] = TRUE;
  328. lpState++;
  329. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHATESTENABLE;
  330. lpState->dwArg[0] = TRUE;
  331. lpState++;
  332. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHAFUNC;
  333. lpState->dwArg[0] = D3DCMP_GREATER;
  334. lpState++;
  335. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMAG;
  336. lpState->dwArg[0] = D3DFILTER_NEAREST;
  337. lpState++;
  338. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMIN;
  339. lpState->dwArg[0] = D3DFILTER_NEAREST;
  340. lpState++;
  341. lpInstruction = (LPD3DINSTRUCTION)lpState;
  342. lpInstruction->bOpcode = D3DOP_TRIANGLE;
  343. lpInstruction->bSize = sizeof(D3DTRIANGLE);
  344. lpInstruction->wCount = fproc2;
  345. lpInstruction++;
  346. lpTriangle = (LPD3DTRIANGLE)lpInstruction;
  347. for (i=0; i<fproc2; i++) {
  348. lpTriangle->wV1 = ii++;
  349. lpTriangle->wV2 = ii++;
  350. lpTriangle->wV3 = ii++;
  351. lpTriangle->wFlags = 0;
  352. lpTriangle++;
  353. }
  354. lpInstruction = (LPD3DINSTRUCTION)lpTriangle;
  355. lpInstruction->bOpcode = D3DOP_STATERENDER;
  356. lpInstruction->bSize = sizeof(D3DSTATE);
  357. lpInstruction->wCount = 5;
  358. lpInstruction++;
  359. lpState = (LPD3DSTATE)lpInstruction;
  360. lpState->drstRenderStateType = D3DRENDERSTATE_COLORKEYENABLE;
  361. lpState->dwArg[0] = FALSE;
  362. lpState++;
  363. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHATESTENABLE;
  364. lpState->dwArg[0] = FALSE;
  365. lpState++;
  366. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHAFUNC;
  367. lpState->dwArg[0] = D3DCMP_GREATEREQUAL;
  368. lpState++;
  369. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMAG;
  370. lpState->dwArg[0] = D3DFILTER_LINEAR;
  371. lpState++;
  372. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMIN;
  373. lpState->dwArg[0] = D3DFILTER_LINEAR;
  374. lpState++;
  375. lpInstruction = (LPD3DINSTRUCTION)lpState;
  376. }
  377. lpInstruction->bOpcode = D3DOP_EXIT;
  378. lpInstruction->bSize = 0UL;
  379. lpInstruction->wCount = 0U;
  380. lpd3dExecuteBuffer->Unlock( );
  381. LINEARFILTER = TRUE;
  382. hRes = lpd3dDevice->Execute(lpd3dExecuteBuffer, lpd3dViewport, D3DEXECUTE_UNCLIPPED);
  383. //if (FAILED(hRes)) DoHalt("Error execute buffer");
  384. dFacesCount+=fproc1+fproc2;
  385. }
  386. DWORD BitDepthToFlags( DWORD dwBitDepth )
  387. {
  388. switch( dwBitDepth ) {
  389. case 1UL: return DDBD_1;
  390. case 2UL: return DDBD_2;
  391. case 4UL: return DDBD_4;
  392. case 8UL: return DDBD_8;
  393. case 16UL: return DDBD_16;
  394. case 24UL: return DDBD_24;
  395. case 32UL: return DDBD_32;
  396. default : return 0UL;
  397. }
  398. }
  399. DWORD FlagsToBitDepth( DWORD dwFlags )
  400. {
  401. if (dwFlags & DDBD_1) return 1UL;
  402. else if (dwFlags & DDBD_2) return 2UL;
  403. else if (dwFlags & DDBD_4) return 4UL;
  404. else if (dwFlags & DDBD_8) return 8UL;
  405. else if (dwFlags & DDBD_16) return 16UL;
  406. else if (dwFlags & DDBD_24) return 24UL;
  407. else if (dwFlags & DDBD_32) return 32UL;
  408. else return 0UL;
  409. }
  410. HRESULT FillExecuteBuffer_State( LPDIRECT3DEXECUTEBUFFER lpd3dExecuteBuffer)
  411. {
  412. HRESULT hRes;
  413. D3DEXECUTEBUFFERDESC d3dExeBufDesc;
  414. LPD3DINSTRUCTION lpInstruction;
  415. LPD3DSTATE lpState;
  416. ZeroMemory(&d3dExeBufDesc, sizeof(d3dExeBufDesc));
  417. d3dExeBufDesc.dwSize = sizeof(d3dExeBufDesc);
  418. hRes = lpd3dExecuteBuffer->Lock( &d3dExeBufDesc );
  419. if (FAILED(hRes)) {
  420. PrintLog( "Error locking execute buffer");
  421. return hRes;
  422. }
  423. lpInstruction = (LPD3DINSTRUCTION) ((LPD3DTLVERTEX)d3dExeBufDesc.lpData + d3debvc*3);
  424. lpInstruction->bOpcode = D3DOP_STATERENDER;
  425. lpInstruction->bSize = sizeof(D3DSTATE);
  426. lpInstruction->wCount = 24;
  427. lpInstruction++;
  428. lpState = (LPD3DSTATE)lpInstruction;
  429. lpState->drstRenderStateType = D3DRENDERSTATE_ZENABLE;
  430. lpState->dwArg[0] = TRUE;
  431. lpState++;
  432. lpState->drstRenderStateType = D3DRENDERSTATE_ZWRITEENABLE;
  433. lpState->dwArg[0] = TRUE;
  434. lpState++;
  435. lpState->drstRenderStateType = D3DRENDERSTATE_ZFUNC;
  436. lpState->dwArg[0] = D3DCMP_GREATEREQUAL;
  437. lpState++;
  438. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREPERSPECTIVE;
  439. lpState->dwArg[0] = TRUE;
  440. lpState++;
  441. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMAPBLEND;
  442. if (OPT_ALPHA_COLORKEY) lpState->dwArg[0] = D3DTBLEND_MODULATEALPHA;
  443. else lpState->dwArg[0] = D3DTBLEND_MODULATE;
  444. lpState++;
  445. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMAG;
  446. lpState->dwArg[0] = D3DFILTER_LINEAR;
  447. lpState++;
  448. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMIN;
  449. lpState->dwArg[0] = D3DFILTER_LINEAR;
  450. lpState++;
  451. lpState->drstRenderStateType = D3DRENDERSTATE_SHADEMODE;
  452. lpState->dwArg[0] = D3DSHADE_GOURAUD;
  453. lpState++;
  454. lpState->drstRenderStateType = D3DRENDERSTATE_DITHERENABLE;
  455. lpState->dwArg[0] = TRUE;
  456. lpState++;
  457. lpState->drstRenderStateType = D3DRENDERSTATE_CULLMODE;
  458. lpState->dwArg[0] = D3DCULL_NONE;
  459. lpState++;
  460. lpState->drstRenderStateType = D3DRENDERSTATE_BLENDENABLE;
  461. lpState->dwArg[0] = TRUE;
  462. lpState++;
  463. lpState->drstRenderStateType = D3DRENDERSTATE_COLORKEYENABLE;
  464. lpState->dwArg[0] = FALSE;
  465. lpState++;
  466. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHABLENDENABLE;
  467. lpState->dwArg[0] = TRUE;
  468. lpState++;
  469. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHATESTENABLE;
  470. lpState->dwArg[0] = FALSE;
  471. lpState++;
  472. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHAREF;
  473. lpState->dwArg[0] = 0;
  474. lpState++;
  475. lpState->drstRenderStateType = D3DRENDERSTATE_ALPHAFUNC;
  476. lpState->dwArg[0] = D3DCMP_GREATEREQUAL;
  477. lpState++;
  478. lpState->drstRenderStateType = D3DRENDERSTATE_SPECULARENABLE;
  479. lpState->dwArg[0] = FALSE;
  480. lpState++;
  481. lpState->drstRenderStateType = D3DRENDERSTATE_FOGENABLE;
  482. lpState->dwArg[0] = FOGENABLE;
  483. lpState++;
  484. lpState->drstRenderStateType = D3DRENDERSTATE_FOGCOLOR;
  485. lpState->dwArg[0] = 0x00606070;
  486. lpState++;
  487. lpState->drstRenderStateType = D3DRENDERSTATE_SRCBLEND;
  488. lpState->dwArg[0] = D3DBLEND_SRCALPHA;
  489. lpState++;
  490. lpState->drstRenderStateType = D3DRENDERSTATE_DESTBLEND;
  491. lpState->dwArg[0] = D3DBLEND_INVSRCALPHA;
  492. lpState++;
  493. lpState->drstRenderStateType = D3DRENDERSTATE_STIPPLEDALPHA;
  494. lpState->dwArg[0] = FALSE;
  495. lpState++;
  496. lpState->drstRenderStateType = D3DRENDERSTATE_WRAPU;
  497. lpState->dwArg[0] = FALSE;
  498. lpState++;
  499. lpState->drstRenderStateType = D3DRENDERSTATE_WRAPV;
  500. lpState->dwArg[0] = FALSE;
  501. lpState++;
  502. lpInstruction = (LPD3DINSTRUCTION)lpState;
  503. lpInstruction->bOpcode = D3DOP_EXIT;
  504. lpInstruction->bSize = 0UL;
  505. lpInstruction->wCount = 0U;
  506. lpd3dExecuteBuffer->Unlock( );
  507. //PrintLog( "Execute buffer filled successfully\n" );
  508. return DD_OK;
  509. }
  510. void SetRenderStates(BOOL ZWRITE, int DST_BLEND)
  511. {
  512. HRESULT hRes;
  513. D3DEXECUTEBUFFERDESC d3dExeBufDesc;
  514. LPD3DINSTRUCTION lpInstruction;
  515. LPD3DSTATE lpState;
  516. ZeroMemory(&d3dExeBufDesc, sizeof(d3dExeBufDesc));
  517. d3dExeBufDesc.dwSize = sizeof(d3dExeBufDesc);
  518. hRes = lpd3dExecuteBuffer->Lock( &d3dExeBufDesc );
  519. if (FAILED(hRes)) {
  520. PrintLog( "Error locking execute buffer");
  521. return;
  522. }
  523. lpInstruction = (LPD3DINSTRUCTION) ((LPD3DTLVERTEX)d3dExeBufDesc.lpData + d3debvc*3);
  524. lpInstruction->bOpcode = D3DOP_STATERENDER;
  525. lpInstruction->bSize = sizeof(D3DSTATE);
  526. lpInstruction->wCount = 2;
  527. lpInstruction++;
  528. lpState = (LPD3DSTATE)lpInstruction;
  529. lpState->drstRenderStateType = D3DRENDERSTATE_ZWRITEENABLE;
  530. lpState->dwArg[0] = ZWRITE;
  531. lpState++;
  532. lpState->drstRenderStateType = D3DRENDERSTATE_DESTBLEND;
  533. lpState->dwArg[0] = DST_BLEND;
  534. lpState++;
  535. lpInstruction = (LPD3DINSTRUCTION)lpState;
  536. lpInstruction->bOpcode = D3DOP_EXIT;
  537. lpInstruction->bSize = 0UL;
  538. lpInstruction->wCount = 0U;
  539. lpd3dExecuteBuffer->Unlock( );
  540. lpd3dDevice->Execute( lpd3dExecuteBuffer,
  541. lpd3dViewport,
  542. D3DEXECUTE_UNCLIPPED );
  543. }
  544. HRESULT WINAPI EnumDeviceCallback(
  545. LPGUID lpGUID,
  546. LPSTR lpszDeviceDesc,
  547. LPSTR lpszDeviceName,
  548. LPD3DDEVICEDESC lpd3dHWDeviceDesc,
  549. LPD3DDEVICEDESC lpd3dSWDeviceDesc,
  550. LPVOID lpUserArg )
  551. {
  552. LPD3DDEVICEDESC lpd3dDeviceDesc;
  553. wsprintf(logt,"ENUMERATE: DDesc: %s DName: %s\n", lpszDeviceDesc, lpszDeviceName);
  554. //PrintLog(logt);
  555. if( !lpd3dHWDeviceDesc->dcmColorModel )
  556. return D3DENUMRET_OK; // we don't need SW rasterizer
  557. lpd3dDeviceDesc = lpd3dHWDeviceDesc;
  558. if( (lpd3dDeviceDesc->dwDeviceRenderBitDepth & dwDeviceBitDepth) == 0UL )
  559. return D3DENUMRET_OK;
  560. if( !(lpd3dDeviceDesc->dpcTriCaps.dwShadeCaps & D3DPSHADECAPS_COLORGOURAUDRGB) )
  561. return D3DENUMRET_OK;
  562. fDeviceFound = TRUE;
  563. CopyMemory( &guidDevice, lpGUID, sizeof(GUID) );
  564. strcpy( szDeviceDesc, lpszDeviceDesc );
  565. strcpy( szDeviceName, lpszDeviceName );
  566. CopyMemory( &d3dHWDeviceDesc, lpd3dHWDeviceDesc, sizeof(D3DDEVICEDESC) );
  567. return D3DENUMRET_CANCEL;
  568. }
  569. HRESULT CreateDirect3D( HWND hwnd )
  570. {
  571. HRESULT hRes;
  572. PrintLog("\n");
  573. PrintLog("=== Init Direct3D ===\n" );
  574. hRes = lpDD->SetCooperativeLevel( hwnd, DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN );
  575. if (FAILED(hRes)) DoHalt("Error setting cooperative level\n");
  576. hRes = lpDD->SetDisplayMode( WinW, WinH, 16 );
  577. if (FAILED(hRes)) {
  578. PrintLog("DDraw: can't set selected video mode\n");
  579. WinW = 640;
  580. WinH = 480;
  581. OptRes = 3;
  582. hRes = lpDD->SetDisplayMode( WinW, WinH, 16 );
  583. }
  584. if (FAILED(hRes)) DoHalt("Error setting display mode\n");
  585. wsprintf(logt, "Set Display mode %dx%d, 16bpp\n", WinW, WinH);
  586. PrintLog(logt);
  587. hRes = lpDD->QueryInterface( IID_IDirect3D, (LPVOID*) &lpd3d);
  588. if (FAILED(hRes)) DoHalt("Error quering Direct3D interface\n");
  589. PrintLog("QueryInterface: Ok. (IID_IDirect3D)\n");
  590. DDSURFACEDESC ddsd;
  591. ZeroMemory(&ddsd, sizeof(ddsd));
  592. ddsd.dwSize = sizeof(ddsd);
  593. ddsd.dwFlags = DDSD_CAPS;
  594. ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  595. hRes = lpDD->CreateSurface( &ddsd, &lpddPrimary, NULL );
  596. if (FAILED(hRes)) DoHalt( "Error creating primary surface\n");
  597. ZeroMemory(&ddsd, sizeof(ddsd) );
  598. ddsd.dwSize = sizeof(ddsd);
  599. hRes = lpddPrimary->GetSurfaceDesc( &ddsd);
  600. if (FAILED(hRes)) DoHalt("Error getting surface description\n");
  601. PrintLog("CreateSurface: Ok. (Primary)\n");
  602. dwDeviceBitDepth = BitDepthToFlags(ddsd.ddpfPixelFormat.dwRGBBitCount);
  603. fDeviceFound = FALSE;
  604. hRes = lpd3d->EnumDevices( EnumDeviceCallback, &fDeviceFound);
  605. if (FAILED(hRes) ) DoHalt("EnumDevices failed.\n");
  606. if (!fDeviceFound ) DoHalt("No devices found.\n");
  607. PrintLog("EnumDevices: Ok.\n");
  608. return DD_OK;
  609. }
  610. HRESULT CreateDevice(DWORD dwWidth, DWORD dwHeight)
  611. {
  612. DDSURFACEDESC ddsd;
  613. HRESULT hRes;
  614. DWORD dwZBufferBitDepth;
  615. ZeroMemory(&ddsd, sizeof(ddsd));
  616. ddsd.dwSize = sizeof(ddsd);
  617. ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
  618. ddsd.dwWidth = dwWidth;
  619. ddsd.dwHeight = dwHeight;
  620. ddsd.ddsCaps.dwCaps = DDSCAPS_3DDEVICE | DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
  621. hRes = lpDD->CreateSurface( &ddsd, &lpddBack, NULL);
  622. if (FAILED(hRes)) DoHalt("Error creating back buffer surface\n");
  623. PrintLog("CreateSurface: Ok. (BackBuffer)\n");
  624. if( d3dHWDeviceDesc.dwDeviceZBufferBitDepth != 0UL ) {
  625. dwZBufferBitDepth = FlagsToBitDepth( d3dHWDeviceDesc.dwDeviceZBufferBitDepth );
  626. ZeroMemory(&ddsd, sizeof(ddsd));
  627. ddsd.dwSize = sizeof(ddsd);
  628. ddsd.dwFlags = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT|DDSD_ZBUFFERBITDEPTH;
  629. ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY;
  630. ddsd.dwWidth = dwWidth;
  631. ddsd.dwHeight = dwHeight;
  632. ddsd.dwZBufferBitDepth = 16;//dwZBufferBitDepth;
  633. hRes = lpDD->CreateSurface( &ddsd, &lpddZBuffer, NULL);
  634. /*
  635. if (FAILED(hRes)) {
  636. ddsd.dwZBufferBitDepth = 16;//dwZBufferBitDepth;
  637. hRes = lpDD->CreateSurface( &ddsd, &lpddZBuffer, NULL);
  638. PrintLog("Z-Buffer: 16 bit\n");
  639. } else
  640. PrintLog("Z-Buffer: 32 bit\n");
  641. */
  642. if (FAILED(hRes)) DoHalt("Error creating z-buffer\n");
  643. hRes = lpddBack->AddAttachedSurface( lpddZBuffer );
  644. if (FAILED(hRes)) DoHalt("Error attaching z-buffer\n");
  645. PrintLog("CreateSurface: Ok. (Z-buffer)\n");
  646. }
  647. hRes = lpddBack->QueryInterface( guidDevice, (LPVOID*) &lpd3dDevice);
  648. if (FAILED(hRes)) DoHalt("Error quering device interface\n");
  649. PrintLog("QueryInterface: Ok. (lpd3dDevice)\n");
  650. return DD_OK;
  651. }
  652. HRESULT CreateScene(void)
  653. {
  654. HRESULT hRes;
  655. DWORD dwVertexSize;
  656. DWORD dwInstructionSize;
  657. DWORD dwExecuteBufferSize;
  658. D3DEXECUTEBUFFERDESC d3dExecuteBufferDesc;
  659. D3DEXECUTEDATA d3dExecuteData;
  660. hRes = lpd3d->CreateViewport( &lpd3dViewport, NULL );
  661. if (FAILED(hRes)) DoHalt("Error creating viewport\n");
  662. PrintLog("CreateViewport: Ok.\n");
  663. hRes = lpd3dDevice->AddViewport( lpd3dViewport );
  664. if (FAILED(hRes)) DoHalt("Error adding viewport\n");
  665. //PrintLog("AddViewport: Ok.\n");
  666. D3DVIEWPORT d3dViewport;
  667. ZeroMemory(&d3dViewport, sizeof(d3dViewport));
  668. d3dViewport.dwSize = sizeof(d3dViewport);
  669. d3dViewport.dwX = 0UL;
  670. d3dViewport.dwY = 0UL;
  671. d3dViewport.dwWidth = (DWORD)WinW;
  672. d3dViewport.dwHeight = (DWORD)WinH;
  673. d3dViewport.dvScaleX = D3DVAL((float)d3dViewport.dwWidth / 2.0);
  674. d3dViewport.dvScaleY = D3DVAL((float)d3dViewport.dwHeight / 2.0);
  675. d3dViewport.dvMaxX = D3DVAL(1.0);
  676. d3dViewport.dvMaxY = D3DVAL(1.0);
  677. lpd3dViewport->SetViewport( &d3dViewport);
  678. //=========== CREATING EXECUTE BUFFER ======================//
  679. dwVertexSize = ((d3debvc*3) * sizeof(D3DVERTEX));
  680. dwInstructionSize = (NUM_INSTRUCTIONS * sizeof(D3DINSTRUCTION)) +
  681. (NUM_STATES * sizeof(D3DSTATE)) +
  682. (NUM_PROCESSVERTICES * sizeof(D3DPROCESSVERTICES)) +
  683. ((d3debvc) * sizeof(D3DTRIANGLE));
  684. dwExecuteBufferSize = dwVertexSize + dwInstructionSize;
  685. ZeroMemory(&d3dExecuteBufferDesc, sizeof(d3dExecuteBufferDesc));
  686. d3dExecuteBufferDesc.dwSize = sizeof(d3dExecuteBufferDesc);
  687. d3dExecuteBufferDesc.dwFlags = D3DDEB_BUFSIZE;
  688. d3dExecuteBufferDesc.dwBufferSize = dwExecuteBufferSize;
  689. hRes = lpd3dDevice->CreateExecuteBuffer( &d3dExecuteBufferDesc, &lpd3dExecuteBuffer, NULL);
  690. if (FAILED(hRes)) DoHalt( "Error creating execute buffer\n");
  691. PrintLog("CreateExecuteBuffer: Ok.\n");
  692. ZeroMemory(&d3dExecuteData, sizeof(d3dExecuteData));
  693. d3dExecuteData.dwSize = sizeof(d3dExecuteData);
  694. d3dExecuteData.dwVertexCount = d3debvc;
  695. d3dExecuteData.dwInstructionOffset = dwVertexSize;
  696. d3dExecuteData.dwInstructionLength = dwInstructionSize;
  697. hRes = lpd3dExecuteBuffer->SetExecuteData( &d3dExecuteData );
  698. if (FAILED(hRes)) DoHalt("Error setting execute data\n");
  699. //=========== CREATING EXECUTE BUFFER ======================//
  700. dwVertexSize = ((d3debvcG*3) * sizeof(D3DVERTEX));
  701. dwInstructionSize = (300 * sizeof(D3DINSTRUCTION)) +
  702. (300 * sizeof(D3DSTATE)) +
  703. (10 * sizeof(D3DPROCESSVERTICES)) +
  704. ((d3debvcG) * sizeof(D3DTRIANGLE));
  705. dwExecuteBufferSize = dwVertexSize + dwInstructionSize;
  706. ZeroMemory(&d3dExecuteBufferDesc, sizeof(d3dExecuteBufferDesc));
  707. d3dExecuteBufferDesc.dwSize = sizeof(d3dExecuteBufferDesc);
  708. d3dExecuteBufferDesc.dwFlags = D3DDEB_BUFSIZE;
  709. d3dExecuteBufferDesc.dwBufferSize = dwExecuteBufferSize;
  710. hRes = lpd3dDevice->CreateExecuteBuffer( &d3dExecuteBufferDesc, &lpd3dExecuteBufferG, NULL);
  711. if (FAILED(hRes)) DoHalt( "Error creating execute buffer\n");
  712. PrintLog("CreateExecuteBuffer: Ok.\n");
  713. ZeroMemory(&d3dExecuteData, sizeof(d3dExecuteData));
  714. d3dExecuteData.dwSize = sizeof(d3dExecuteData);
  715. d3dExecuteData.dwVertexCount = d3debvcG;
  716. d3dExecuteData.dwInstructionOffset = dwVertexSize;
  717. d3dExecuteData.dwInstructionLength = dwInstructionSize;
  718. hRes = lpd3dExecuteBufferG->SetExecuteData( &d3dExecuteData );
  719. if (FAILED(hRes)) DoHalt("Error setting execute data\n");
  720. FillExecuteBuffer_State(lpd3dExecuteBuffer);
  721. hRes = lpd3dDevice->Execute( lpd3dExecuteBuffer, lpd3dViewport, D3DEXECUTE_UNCLIPPED);
  722. return DD_OK;
  723. }
  724. void Init3DHardware()
  725. {
  726. HARD3D = TRUE;
  727. PrintLog("\n");
  728. PrintLog("==Init Direct Draw==\n");
  729. HRESULT hres;
  730. hres = DirectDrawCreate( NULL, &lpDD, NULL );
  731. if( hres != DD_OK ) {
  732. wsprintf(logt, "DirectDrawCreate Error: %Xh\n", hres);
  733. DoHalt(logt); }
  734. PrintLog("DirectDrawCreate: Ok\n");
  735. DirectActive = TRUE;
  736. }
  737. void d3dDetectCaps()
  738. {
  739. for (int t=0; t<d3dmemmapsize; t++) {
  740. if (!d3dAllocTexture(t, 256, 256)) break;
  741. }
  742. d3dTexturesMem = t*256*256*2;
  743. d3dDownLoadTexture(0, 256, 256, SkyPic);
  744. DWORD T;
  745. T = timeGetTime();
  746. for (t=0; t<10; t++) d3dDownLoadTexture(0, 256, 256, SkyPic);
  747. T = timeGetTime() - T;
  748. wsprintf(logt, "DETECTED: Texture memory : %dK.\n", d3dTexturesMem>>10);
  749. PrintLog(logt);
  750. ResetTextureMap();
  751. wsprintf(logt, "DETECTED: Texture transfer speed: %dK/sec.\n", 128*10000 / T);
  752. PrintLog(logt);
  753. DDSURFACEDESC ddsd;
  754. ZeroMemory( &ddsd, sizeof(DDSURFACEDESC) );
  755. ddsd.dwSize = sizeof(DDSURFACEDESC);
  756. if( lpddBack->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) != DD_OK ) return;
  757. lpddBack->Unlock(ddsd.lpSurface);
  758. if (ddsd.ddpfPixelFormat.dwGBitMask == 0x3E0) VMFORMAT565=FALSE; else VMFORMAT565=TRUE;
  759. if (VMFORMAT565)
  760. PrintLog("DETECTED: PixelFormat RGB565\n");
  761. else {
  762. PrintLog("DETECTED: PixelFormat RGB555\n");
  763. if (!STARTCONV555) {
  764. STARTCONV555 = TRUE;
  765. conv_pic555(PausePic);
  766. conv_pic555(ExitPic);
  767. conv_pic555(TrophyExit);
  768. conv_pic555(MapPic);
  769. conv_pic555(TrophyPic);
  770. for (int c=0; c<20; c++)
  771. if (DinoInfo[c].CallIcon.W)
  772. conv_pic555(DinoInfo[c].CallIcon);
  773. for (int w=0; w<TotalW; w++)
  774. if (Weapon.BulletPic[w].W)
  775. conv_pic555(Weapon.BulletPic[w]);
  776. }
  777. }
  778. }
  779. void d3dTestDrawTri(DWORD color, float tx)
  780. {
  781. d3dStartBuffer();
  782. lpVertex->sx = 0;
  783. lpVertex->sy = 0;
  784. lpVertex->sz = _ZSCALE / 10;
  785. lpVertex->rhw = 1.f;
  786. lpVertex->color = color;
  787. lpVertex->specular = 0xFF000000;
  788. lpVertex->tu = tx;
  789. lpVertex->tv = tx;
  790. lpVertex++;
  791. lpVertex->sx = 100;
  792. lpVertex->sy = 0;
  793. lpVertex->sz = _ZSCALE / 10;
  794. lpVertex->rhw = 1.f;
  795. lpVertex->color = color;
  796. lpVertex->specular = 0xFF000000;
  797. lpVertex->tu = tx;
  798. lpVertex->tv = tx;
  799. lpVertex++;
  800. lpVertex->sx = 0;
  801. lpVertex->sy = 100;
  802. lpVertex->sz = _ZSCALE / 10;
  803. lpVertex->rhw = 1.f;
  804. lpVertex->color = color;
  805. lpVertex->specular = 0xFF000000;
  806. lpVertex->tu = tx;
  807. lpVertex->tv = tx;
  808. lpVertex++;
  809. d3dFlushBuffer(0, 1);
  810. }
  811. int d3dTestAlpha()
  812. {
  813. int RF = 0;
  814. lpd3dDevice->BeginScene( );
  815. // RenderSkyPlane();
  816. d3dSetTexture(TFX_SPECULAR.lpImage, TFX_SPECULAR.W, TFX_SPECULAR.W);
  817. DDBLTFX ddbltfx;
  818. ddbltfx.dwSize = sizeof( DDBLTFX );
  819. ddbltfx.dwFillColor = 0xFFFF;
  820. lpddBack->Blt( NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx );
  821. FillExecuteBuffer_State(lpd3dExecuteBuffer);
  822. hRes = lpd3dDevice->Execute( lpd3dExecuteBuffer, lpd3dViewport, D3DEXECUTE_UNCLIPPED);
  823. //========= test opacity ==========//
  824. //d3dSetTexture(TFX_SPECULAR.lpImage, TFX_SPECULAR.W, TFX_SPECULAR.W);
  825. SetRenderStates(FALSE, D3DBLEND_INVSRCALPHA);
  826. d3dTestDrawTri(0xFFFFFFFF, 0.0);
  827. CopyBackToDIB();
  828. if (*((WORD*)lpVideoBuf)==0) PrintLog("[OPACITY: Failed] ");
  829. else { PrintLog("[OPACITY: Ok.] "); RF+=4; }
  830. lpd3dDevice->EndScene( );
  831. lpd3dDevice->BeginScene( );
  832. /*
  833. ShowVideo();
  834. lpd3dDevice->BeginScene( );
  835. Sleep(1000);
  836. */
  837. //=========== test transparent mode =============//
  838. lpd3dDevice->BeginScene( );
  839. ddbltfx.dwSize = sizeof( DDBLTFX );
  840. ddbltfx.dwFillColor = 0x0000;
  841. lpddBack->Blt( NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx );
  842. d3dTestDrawTri(0x00FFFFFF, 0.5);
  843. CopyBackToDIB();
  844. if (*((WORD*)lpVideoBuf)!=0) PrintLog("[TRANSPARENT: Failed] ");
  845. else { PrintLog("[TRANSPARENT: Ok.] "); RF+=2; }
  846. lpd3dDevice->EndScene( );
  847. lpd3dDevice->BeginScene( );
  848. /*
  849. ShowVideo();
  850. lpd3dDevice->BeginScene( );
  851. Sleep(1000);
  852. */
  853. //=========== test env map ==================//
  854. SetRenderStates(FALSE, D3DBLEND_ONE);
  855. lpd3dDevice->BeginScene( );
  856. ddbltfx.dwSize = sizeof( DDBLTFX );
  857. ddbltfx.dwFillColor = 0x0000;
  858. lpddBack->Blt( NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx );
  859. d3dTestDrawTri(0x00FFFFFF, 0.5);
  860. CopyBackToDIB();
  861. if (*((WORD*)lpVideoBuf)!=0) PrintLog("[ENV.BLEND : Failed]\n");
  862. else { PrintLog("[ENV.BLEND : Ok.]\n"); RF+=1; }
  863. lpd3dDevice->EndScene( );
  864. lpd3dDevice->BeginScene( );
  865. /*
  866. ShowVideo();
  867. lpd3dDevice->BeginScene( );
  868. Sleep(1000);
  869. */
  870. SetRenderStates(TRUE, D3DBLEND_INVSRCALPHA);
  871. lpd3dDevice->EndScene( );
  872. return RF;
  873. }
  874. void Activate3DHardware()
  875. {
  876. SetVideoMode(WinW,WinH);
  877. HRESULT hRes = CreateDirect3D(hwndMain);
  878. if (FAILED(hRes)) DoHalt("CreateDirect3D Failed.\n");
  879. hRes = CreateDevice((DWORD)WinW, (DWORD)WinH);
  880. if (FAILED(hRes)) DoHalt("Create Device Failed.\n");
  881. d3dClearBuffers();
  882. hRes = CreateScene();
  883. if (FAILED(hRes)) DoHalt("CreateScene Failed.\n");
  884. d3dDetectCaps();
  885. OPT_ALPHA_COLORKEY=FALSE;
  886. PrintLog("TEST COLOR KEY: ");
  887. int r1 = d3dTestAlpha();
  888. OPT_ALPHA_COLORKEY=TRUE;
  889. PrintLog("TEST ALPHA KEY: ");
  890. int r2 = d3dTestAlpha();
  891. OPT_ALPHA_COLORKEY = (r2>=r1);
  892. if (OPT_ALPHA_COLORKEY) PrintLog("ALPHA KEY mode selected.\n");
  893. else PrintLog("COLOR KEY mode selected.\n");
  894. hRes = lpd3dDevice->BeginScene( );
  895. if (OptText==0) LOWRESTX = TRUE;
  896. if (OptText==1) LOWRESTX = FALSE;
  897. if (OptText==2) LOWRESTX = FALSE;
  898. d3dMemLoaded = 0;
  899. D3DACTIVE = TRUE;
  900. d3dLastTexture = d3dmemmapsize+1;
  901. PrintLog("=== Direct3D started === \n");
  902. PrintLog("\n");
  903. }
  904. void ResetTextureMap()
  905. {
  906. d3dEndBufferG(FALSE);
  907. d3dMemUsageCount = 0;
  908. //d3dMemLoaded = 0;
  909. d3dLastTexture = d3dmemmapsize+1;
  910. for (int m=0; m<d3dmemmapsize+2; m++) {
  911. d3dMemMap[m].lastused = 0;
  912. d3dMemMap[m].cpuaddr = 0;
  913. if (d3dMemMap[m].lpddTexture) {
  914. d3dMemMap[m].lpddTexture->Release();
  915. d3dMemMap[m].lpddTexture = NULL;
  916. }
  917. }
  918. }
  919. void ShutDown3DHardware()
  920. {
  921. D3DACTIVE = FALSE;
  922. if (lpd3dDevice)
  923. hRes = lpd3dDevice->EndScene();
  924. ResetTextureMap();
  925. lpInstructionG = NULL;
  926. lpVertexG = NULL;
  927. if (NULL != lpd3dExecuteBuffer) {
  928. lpd3dExecuteBuffer->Release( );
  929. lpd3dExecuteBufferG->Release( );
  930. lpd3dExecuteBuffer = NULL;
  931. }
  932. if (NULL != lpd3dViewport) {
  933. lpd3dViewport->Release( );
  934. lpd3dViewport = NULL;
  935. }
  936. if (NULL != lpd3dDevice) {
  937. lpd3dDevice->Release( );
  938. lpd3dDevice = NULL;
  939. }
  940. if (NULL != lpddZBuffer) {
  941. lpddZBuffer->Release( );
  942. lpddZBuffer = NULL;
  943. }
  944. if (NULL != lpddBack) {
  945. lpddBack->Release();
  946. lpddBack = NULL;
  947. }
  948. if (NULL != lpddPrimary) {
  949. lpddPrimary->Release( );
  950. lpddPrimary = NULL;
  951. }
  952. if (NULL != lpd3d) {
  953. lpd3d->Release( );
  954. lpd3d = NULL;
  955. }
  956. lpDD->SetCooperativeLevel( hwndMain, DDSCL_NORMAL);
  957. }
  958. void InsertFxMM(int m)
  959. {
  960. for (int mm=d3dmemmapsize-1; mm>m; mm--)
  961. d3dMemMap[m] = d3dMemMap[m-1];
  962. }
  963. BOOL d3dAllocTexture(int i, int w, int h)
  964. {
  965. DDSURFACEDESC ddsd;
  966. DDPIXELFORMAT ddpf;
  967. ZeroMemory( &ddpf, sizeof(DDPIXELFORMAT) );
  968. ddpf.dwSize = sizeof(DDPIXELFORMAT);
  969. ddpf.dwFlags = DDPF_RGB;
  970. if (OPT_ALPHA_COLORKEY) {
  971. ddpf.dwFlags |= DDPF_ALPHAPIXELS;
  972. ddpf.dwRGBAlphaBitMask = 0x8000;
  973. }
  974. ddpf.dwRGBBitCount = 16;
  975. ddpf.dwRBitMask = 0x7c00;
  976. ddpf.dwGBitMask = 0x3e0;
  977. ddpf.dwBBitMask = 0x1f;
  978. ZeroMemory(&ddsd, sizeof(ddsd));
  979. ddsd.dwSize = sizeof(ddsd);
  980. ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
  981. ddsd.dwWidth = w;
  982. ddsd.dwHeight = h;
  983. CopyMemory( &ddsd.ddpfPixelFormat, &ddpf, sizeof(DDPIXELFORMAT) );
  984. ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY;
  985. hRes = lpDD->CreateSurface( &ddsd, &d3dMemMap[i].lpddTexture, NULL);
  986. if (FAILED(hRes)) {
  987. d3dMemMap[i].lpddTexture = NULL;
  988. return FALSE; }
  989. DDCOLORKEY ddck;
  990. ddck.dwColorSpaceLowValue = ddck.dwColorSpaceHighValue = 0x0000;
  991. d3dMemMap[i].lpddTexture->SetColorKey(DDCKEY_SRCBLT, &ddck);
  992. return TRUE;
  993. }
  994. void d3dDownLoadTexture(int i, int w, int h, LPVOID tptr)
  995. {
  996. DDSURFACEDESC ddsd;
  997. ZeroMemory( &ddsd, sizeof(DDSURFACEDESC) );
  998. ddsd.dwSize = sizeof(DDSURFACEDESC);
  999. if( d3dMemMap[i].lpddTexture->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) != DD_OK ) return;
  1000. CopyMemory( ddsd.lpSurface, tptr, w*h*2 );
  1001. d3dMemMap[i].lpddTexture->Unlock( ddsd.lpSurface );
  1002. IDirect3DTexture* Texture;
  1003. if( d3dMemMap[i].lpddTexture->QueryInterface( IID_IDirect3DTexture, (LPVOID*)&Texture ) != S_OK ) return;
  1004. if( Texture->GetHandle( lpd3dDevice, &d3dMemMap[i].hTexture ) != D3D_OK ) return;
  1005. Texture->Release( );
  1006. d3dMemMap[i].cpuaddr = (int) tptr;
  1007. d3dMemMap[i].size = w*h*2;
  1008. d3dMemLoaded+=w*h*2;
  1009. //---------------------------------------------------------------------------
  1010. }
  1011. int DownLoadTexture(LPVOID tptr, int w, int h)
  1012. {
  1013. int textureSize = w*w*2;
  1014. int fxm = 0;
  1015. int m;
  1016. //========== if no memory used ==========//
  1017. if (!d3dMemMap[0].cpuaddr) {
  1018. d3dAllocTexture(0, w, h);
  1019. d3dDownLoadTexture(0, w, h, tptr);
  1020. return 0;
  1021. }
  1022. //====== search for last used block and try to alloc next ============//
  1023. for (m = 0; m < d3dmemmapsize; m++)
  1024. if (!d3dMemMap[m].cpuaddr)
  1025. if (d3dAllocTexture(m, w, h)) {
  1026. d3dDownLoadTexture(m, w, h, tptr);
  1027. return m;
  1028. } else break;
  1029. //====== search for unused texture and replace it with new ============//
  1030. int unusedtime = 2;
  1031. int rt = -1;
  1032. for (m = 0; m < d3dmemmapsize; m++) {
  1033. if (!d3dMemMap[m].cpuaddr) break;
  1034. if (d3dMemMap[m].size != w*h*2) continue;
  1035. int ut = d3dMemUsageCount - d3dMemMap[m].lastused;
  1036. if (ut >= unusedtime) {
  1037. unusedtime = ut;
  1038. rt = m; }
  1039. }
  1040. if (rt!=-1) {
  1041. d3dDownLoadTexture(rt, w, h, tptr);
  1042. return rt;
  1043. }
  1044. ResetTextureMap();
  1045. d3dAllocTexture(0, w, h);
  1046. d3dDownLoadTexture(0, w, h, tptr);
  1047. return 0;
  1048. }
  1049. void d3dSetTexture(LPVOID tptr, int w, int h)
  1050. {
  1051. if (d3dMemMap[d3dLastTexture].cpuaddr == (int)tptr) return;
  1052. int fxm = -1;
  1053. for (int m=0; m<d3dmemmapsize; m++) {
  1054. if (d3dMemMap[m].cpuaddr == (int)tptr) { fxm = m; break; }
  1055. if (!d3dMemMap[m].cpuaddr) break;
  1056. }
  1057. if (fxm==-1) fxm = DownLoadTexture(tptr, w, h);
  1058. d3dMemMap[fxm].lastused = d3dMemUsageCount;
  1059. hTexture = d3dMemMap[fxm].hTexture;
  1060. d3dLastTexture = fxm;
  1061. }
  1062. float GetTraceK(int x, int y)
  1063. {
  1064. if (x<8 || y<8 || x>WinW-8 || y>WinH-8) return 0.f;
  1065. float k = 0;
  1066. DDSURFACEDESC ddsd;
  1067. ZeroMemory( &ddsd, sizeof(DDSURFACEDESC) );
  1068. ddsd.dwSize = sizeof(DDSURFACEDESC);
  1069. if( lpddZBuffer->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) != DD_OK ) {
  1070. return 0;
  1071. }
  1072. WORD CC = 200;
  1073. int bw = (ddsd.lPitch>>1);
  1074. if ( *((WORD*)ddsd.lpSurface + (y+0)*bw + x+0) < CC ) k+=1.f;
  1075. if ( *((WORD*)ddsd.lpSurface + (y+10)*bw + x+0) < CC ) k+=1.f;
  1076. if ( *((WORD*)ddsd.lpSurface + (y-10)*bw + x+0) < CC ) k+=1.f;
  1077. if ( *((WORD*)ddsd.lpSurface + (y+0)*bw + x+10) < CC ) k+=1.f;
  1078. if ( *((WORD*)ddsd.lpSurface + (y+0)*bw + x-10) < CC ) k+=1.f;
  1079. if ( *((WORD*)ddsd.lpSurface + (y+8)*bw + x+8) < CC ) k+=1.f;
  1080. if ( *((WORD*)ddsd.lpSurface + (y+8)*bw + x-8) < CC ) k+=1.f;
  1081. if ( *((WORD*)ddsd.lpSurface + (y-8)*bw + x+8) < CC ) k+=1.f;
  1082. if ( *((WORD*)ddsd.lpSurface + (y-8)*bw + x-8) < CC ) k+=1.f;
  1083. lpddZBuffer->Unlock(ddsd.lpSurface);
  1084. k/=9.f;
  1085. DeltaFunc(TraceK, k, TimeDt / 1024.f);
  1086. return TraceK;
  1087. }
  1088. void AddSkySum(WORD C)
  1089. {
  1090. int R,G,B;
  1091. if (VMFORMAT565) {
  1092. R = C>>11; G = (C>>5) & 63; B = C & 31;
  1093. } else {
  1094. R = C>>10; G = (C>>5) & 31; B = C & 31; C=C*2;
  1095. }
  1096. SkySumR += R*8;
  1097. SkySumG += G*4;
  1098. SkySumB += B*8;
  1099. }
  1100. float GetSkyK(int x, int y)
  1101. {
  1102. if (x<10 || y<10 || x>WinW-10 || y>WinH-10) return 0.5;
  1103. SkySumR = 0;
  1104. SkySumG = 0;
  1105. SkySumB = 0;
  1106. float k = 0;
  1107. DDSURFACEDESC ddsd;
  1108. ZeroMemory( &ddsd, sizeof(DDSURFACEDESC) );
  1109. ddsd.dwSize = sizeof(DDSURFACEDESC);
  1110. if( lpddBack->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) != DD_OK ) {
  1111. return 0;
  1112. }
  1113. int bw = (ddsd.lPitch>>1);
  1114. AddSkySum(*((WORD*)ddsd.lpSurface + (y+0)*bw + x+0));
  1115. AddSkySum(*((WORD*)ddsd.lpSurface + (y+6)*bw + x+0));
  1116. AddSkySum(*((WORD*)ddsd.lpSurface + (y-6)*bw + x+0));
  1117. AddSkySum(*((WORD*)ddsd.lpSurface + (y+0)*bw + x+6));
  1118. AddSkySum(*((WORD*)ddsd.lpSurface + (y+0)*bw + x-6));
  1119. AddSkySum(*((WORD*)ddsd.lpSurface + (y+4)*bw + x+4));
  1120. AddSkySum(*((WORD*)ddsd.lpSurface + (y+4)*bw + x-4));
  1121. AddSkySum(*((WORD*)ddsd.lpSurface + (y-4)*bw + x+4));
  1122. AddSkySum(*((WORD*)ddsd.lpSurface + (y-4)*bw + x-4));
  1123. lpddBack->Unlock(ddsd.lpSurface);
  1124. SkySumR-=SkyTR*9;
  1125. SkySumG-=SkyTG*9;
  1126. SkySumB-=SkyTB*9;
  1127. k = (float)sqrt(SkySumR*SkySumR + SkySumG*SkySumG + SkySumB*SkySumB) / 9;
  1128. if (k>80) k = 80;
  1129. if (k< 0) k = 0;
  1130. k = 1.0f - k/80.f;
  1131. if (k<0.2) k=0.2f;
  1132. if (OptDayNight==2) k=0.3 + k/2.75;
  1133. DeltaFunc(SkyTraceK, k, (0.07f + (float)fabs(k-SkyTraceK)) * (TimeDt / 512.f) );
  1134. return SkyTraceK;
  1135. }
  1136. void TryHiResTx()
  1137. {
  1138. int UsedMem = 0;
  1139. for (int m=0; m<d3dmemmapsize; m++) {
  1140. if (!d3dMemMap[m].cpuaddr) break;
  1141. if (d3dMemMap[m].lastused+2>=d3dMemUsageCount)
  1142. UsedMem+= d3dMemMap[m].size;
  1143. }
  1144. /*
  1145. wsprintf(logt, "TOTALL: %d USED: %d", d3dTexturesMem, UsedMem);
  1146. AddMessage(logt);
  1147. */
  1148. if (UsedMem*4 < (int)d3dTexturesMem)
  1149. LOWRESTX = FALSE;
  1150. }
  1151. void ShowVideo()
  1152. {
  1153. /*
  1154. char t[128];
  1155. wsprintf(t, "T-mem loaded: %dK", d3dMemLoaded >> 10);
  1156. if (d3dMemLoaded) AddMessage(t);
  1157. */
  1158. if (d3dMemLoaded > 200*1024) LowHardMemory++;
  1159. else LowHardMemory=0;
  1160. if (LowHardMemory>2) {
  1161. LOWRESTX = TRUE;
  1162. LowHardMemory = 0; }
  1163. if (OptText==0) LOWRESTX = TRUE;
  1164. if (OptText==1) LOWRESTX = FALSE;
  1165. if (OptText==2)
  1166. if (LOWRESTX && (Takt & 63)==0) TryHiResTx();
  1167. if (UNDERWATER)
  1168. RenderFSRect(CurFogColor+0x70000000);
  1169. if (OptDayNight!=2)
  1170. if (!UNDERWATER && (SunLight>1.0f) ) {
  1171. RenderFSRect(0xFFFFC0 + ((int)SunLight<<24));
  1172. }
  1173. RenderHealthBar();
  1174. d3dMemUsageCount++;
  1175. d3dMemLoaded = 0;
  1176. hRes = lpd3dDevice->EndScene();
  1177. hRes = lpddPrimary->Blt( NULL, lpddBack, NULL, DDBLT_WAIT, NULL );
  1178. d3dClearBuffers();
  1179. hRes = lpd3dDevice->BeginScene( );
  1180. }
  1181. void CopyBackToDIB()
  1182. {
  1183. DDSURFACEDESC ddsd;
  1184. ZeroMemory( &ddsd, sizeof(DDSURFACEDESC) );
  1185. ddsd.dwSize = sizeof(DDSURFACEDESC);
  1186. if( lpddBack->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) != DD_OK ) return;
  1187. WORD *lpVMem = (WORD*) ddsd.lpSurface;
  1188. ddsd.lPitch/=2;
  1189. for (int y=0; y<=256; y++)
  1190. CopyMemory( (WORD*)lpVideoBuf + y*1024,
  1191. lpVMem + y*ddsd.lPitch,
  1192. 256*2);
  1193. lpddBack->Unlock(ddsd.lpSurface);
  1194. }
  1195. void CopyHARDToDIB()
  1196. {
  1197. DDSURFACEDESC ddsd;
  1198. ZeroMemory( &ddsd, sizeof(DDSURFACEDESC) );
  1199. ddsd.dwSize = sizeof(DDSURFACEDESC);
  1200. if( lpddPrimary->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) != DD_OK ) return;
  1201. WORD *lpVMem = (WORD*) ddsd.lpSurface;
  1202. ddsd.lPitch/=2;
  1203. for (int y=0; y<=WinH; y++)
  1204. CopyMemory( (WORD*)lpVideoBuf + y*1024,
  1205. lpVMem + y*ddsd.lPitch,
  1206. WinW*2);
  1207. lpddPrimary->Unlock(ddsd.lpSurface);
  1208. }
  1209. void FXPutBitMap(int x0, int y0, int w, int h, int smw, LPVOID lpData)
  1210. {
  1211. DDSURFACEDESC ddsd;
  1212. ZeroMemory( &ddsd, sizeof(DDSURFACEDESC) );
  1213. ddsd.dwSize = sizeof(DDSURFACEDESC);
  1214. if( lpddBack->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) != DD_OK ) return;
  1215. WORD *lpVMem = (WORD*) ddsd.lpSurface;
  1216. ddsd.lPitch/=2;
  1217. lpVMem+=x0+y0 * ddsd.lPitch;
  1218. for (int y=0; y<h; y++)
  1219. CopyMemory( lpVMem + y*ddsd.lPitch, ((WORD*)lpData)+y*smw, w*2);
  1220. lpddBack->Unlock(ddsd.lpSurface);
  1221. }
  1222. void DrawPicture(int x, int y, TPicture &pic)
  1223. {
  1224. FXPutBitMap(x, y, pic.W, pic.H, pic.W, pic.lpImage);
  1225. }
  1226. void ddTextOut(int x, int y, LPSTR t, int color)
  1227. {
  1228. lpddBack->GetDC( &ddBackDC );
  1229. SetBkMode( ddBackDC, TRANSPARENT );
  1230. HFONT oldfont;
  1231. if (SmallFont) oldfont = (HFONT)SelectObject(ddBackDC, fnt_Small);
  1232. SetTextColor(ddBackDC, 0x00101010);
  1233. TextOut(ddBackDC, x+2, y+1, t, strlen(t));
  1234. SetTextColor(ddBackDC, color);
  1235. TextOut(ddBackDC, x+1, y, t, strlen(t));
  1236. if (SmallFont) SelectObject(ddBackDC, oldfont);
  1237. lpddBack->ReleaseDC( ddBackDC );
  1238. }
  1239. void DrawTrophyText(int x0, int y0)
  1240. {
  1241. int x;
  1242. SmallFont = TRUE;
  1243. HFONT oldfont = (HFONT)SelectObject(hdcMain, fnt_Small);
  1244. int tc = TrophyBody;
  1245. int dtype = TrophyRoom.Body[tc].ctype;
  1246. int time = TrophyRoom.Body[tc].time;
  1247. int date = TrophyRoom.Body[tc].date;
  1248. int wep = TrophyRoom.Body[tc].weapon;
  1249. int score = TrophyRoom.Body[tc].score;
  1250. float scale = TrophyRoom.Body[tc].scale;
  1251. float range = TrophyRoom.Body[tc].range;
  1252. char t[32];
  1253. x0+=14; y0+=18;
  1254. x = x0;
  1255. ddTextOut(x, y0 , "Name: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Name: ");
  1256. ddTextOut(x, y0 , DinoInfo[dtype].Name, 0x0000BFBF);
  1257. x = x0;
  1258. ddTextOut(x, y0+16, "Weight: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Weight: ");
  1259. if (OptSys)
  1260. sprintf(t,"%3.2ft ", DinoInfo[dtype].Mass * scale * scale / 0.907);
  1261. else
  1262. sprintf(t,"%3.2fT ", DinoInfo[dtype].Mass * scale * scale);
  1263. ddTextOut(x, y0+16, t, 0x0000BFBF); x+=GetTextW(hdcMain,t);
  1264. ddTextOut(x, y0+16, "Length: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Length: ");
  1265. if (OptSys)
  1266. sprintf(t,"%3.2fft", DinoInfo[dtype].Length * scale / 0.3);
  1267. else
  1268. sprintf(t,"%3.2fm", DinoInfo[dtype].Length * scale);
  1269. ddTextOut(x, y0+16, t, 0x0000BFBF);
  1270. x = x0;
  1271. ddTextOut(x, y0+32, "Weapon: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Weapon: ");
  1272. wsprintf(t,"%s ", WeapInfo[wep].Name);
  1273. ddTextOut(x, y0+32, t, 0x0000BFBF); x+=GetTextW(hdcMain,t);
  1274. ddTextOut(x, y0+32, "Score: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Score: ");
  1275. wsprintf(t,"%d", score);
  1276. ddTextOut(x, y0+32, t, 0x0000BFBF);
  1277. x = x0;
  1278. ddTextOut(x, y0+48, "Range of kill: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Range of kill: ");
  1279. if (OptSys) sprintf(t,"%3.1fft", range / 0.3);
  1280. else sprintf(t,"%3.1fm", range);
  1281. ddTextOut(x, y0+48, t, 0x0000BFBF);
  1282. x = x0;
  1283. ddTextOut(x, y0+64, "Date: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Date: ");
  1284. if (OptSys)
  1285. wsprintf(t,"%d.%d.%d ", ((date>>10) & 255), (date & 255), date>>20);
  1286. else
  1287. wsprintf(t,"%d.%d.%d ", (date & 255), ((date>>10) & 255), date>>20);
  1288. ddTextOut(x, y0+64, t, 0x0000BFBF); x+=GetTextW(hdcMain,t);
  1289. ddTextOut(x, y0+64, "Time: ", 0x00BFBFBF); x+=GetTextW(hdcMain,"Time: ");
  1290. wsprintf(t,"%d:%02d", ((time>>10) & 255), (time & 255));
  1291. ddTextOut(x, y0+64, t, 0x0000BFBF);
  1292. SmallFont = FALSE;
  1293. SelectObject(hdcMain, oldfont);
  1294. }
  1295. void Render_LifeInfo(int li)
  1296. {
  1297. int x,y;
  1298. SmallFont = TRUE;
  1299. //HFONT oldfont = SelectObject(hdcMain, fnt_Small);
  1300. int ctype = Characters[li].CType;
  1301. float scale = Characters[li].scale;
  1302. char t[32];
  1303. x = VideoCX + WinW / 64;
  1304. y = VideoCY + (int)(WinH / 6.8);
  1305. ddTextOut(x, y, DinoInfo[ctype].Name, 0x0000b000);
  1306. if (OptSys) sprintf(t,"Weight: %3.2ft ", DinoInfo[ctype].Mass * scale * scale / 0.907);
  1307. else sprintf(t,"Weight: %3.2fT ", DinoInfo[ctype].Mass * scale * scale);
  1308. ddTextOut(x, y+16, t, 0x0000b000);
  1309. int R = (int)(VectorLength( SubVectors(Characters[li].pos, PlayerPos) )*3 / 64.f);
  1310. if (OptSys) sprintf(t,"Distance: %dft ", R);
  1311. else sprintf(t,"Distance: %dm ", R/3);
  1312. ddTextOut(x, y+32, t, 0x0000b000);
  1313. SmallFont = FALSE;
  1314. //SelectObject(hdcMain, oldfont);
  1315. }
  1316. void RenderFXMMap()
  1317. {
  1318. }
  1319. void ShowControlElements()
  1320. {
  1321. char buf[128];
  1322. //ddTextOut(100, 100, "!", 0x0020A0A0);
  1323. lpddBack->GetDC( &ddBackDC );
  1324. lpddBack->ReleaseDC( ddBackDC );
  1325. if (TIMER) {
  1326. wsprintf(buf,"msc: %d", TimeDt);
  1327. ddTextOut(WinEX-81, 11, buf, 0x0020A0A0);
  1328. wsprintf(buf,"polys: %d", dFacesCount);
  1329. ddTextOut(WinEX-90, 24, buf, 0x0020A0A0);
  1330. wsprintf(buf,"%d", Env);
  1331. ddTextOut(10, 24, buf, 0x0020A0A0);
  1332. }
  1333. if (MessageList.timeleft) {
  1334. if (RealTime>MessageList.timeleft) MessageList.timeleft = 0;
  1335. ddTextOut(10, 10, MessageList.mtext, 0x0020A0A0);
  1336. }
  1337. if (ExitTime) {
  1338. int y = WinH / 3;
  1339. wsprintf(buf,"Preparing for evacuation...");
  1340. ddTextOut(VideoCX - GetTextW(hdcCMain, buf)/2, y, buf, 0x0060C0D0);
  1341. wsprintf(buf,"%d seconds left.", 1 + ExitTime / 1000);
  1342. ddTextOut(VideoCX - GetTextW(hdcCMain, buf)/2, y + 18, buf, 0x0060C0D0);
  1343. }
  1344. }
  1345. void ClipVector(CLIPPLANE& C, int vn)
  1346. {
  1347. int ClipRes = 0;
  1348. float s,s1,s2;
  1349. int vleft = (vn-1); if (vleft <0) vleft=vused-1;
  1350. int vright = (vn+1); if (vright>=vused) vright=0;
  1351. MulVectorsScal(cp[vn].ev.v, C.nv, s); /*s=SGN(s-0.01f);*/
  1352. if (s>=0) return;
  1353. MulVectorsScal(cp[vleft ].ev.v, C.nv, s1); /* s1=SGN(s1+0.01f); */ //s1+=0.0001f;
  1354. MulVectorsScal(cp[vright].ev.v, C.nv, s2); /* s2=SGN(s2+0.01f); */ //s2+=0.0001f;
  1355. if (s1>0) {
  1356. ClipRes+=1;
  1357. /*
  1358. CalcHitPoint(C,cp[vn].ev.v,
  1359. cp[vleft].ev.v, hleft.ev.v);
  1360. float ll = VectorLength(SubVectors(cp[vleft].ev.v, cp[vn].ev.v));
  1361. float lc = VectorLength(SubVectors(hleft.ev.v, cp[vn].ev.v));
  1362. lc = lc / ll;
  1363. */
  1364. float lc = -s / (s1-s);
  1365. hleft.ev.v.x = cp[vn].ev.v.x + ((cp[vleft].ev.v.x - cp[vn].ev.v.x) * lc);
  1366. hleft.ev.v.y = cp[vn].ev.v.y + ((cp[vleft].ev.v.y - cp[vn].ev.v.y) * lc);
  1367. hleft.ev.v.z = cp[vn].ev.v.z + ((cp[vleft].ev.v.z - cp[vn].ev.v.z) * lc);
  1368. hleft.tx = cp[vn].tx + ((cp[vleft].tx - cp[vn].tx) * lc);
  1369. hleft.ty = cp[vn].ty + ((cp[vleft].ty - cp[vn].ty) * lc);
  1370. hleft.ev.Light = cp[vn].ev.Light + (int)((cp[vleft].ev.Light - cp[vn].ev.Light) * lc);
  1371. hleft.ev.ALPHA = cp[vn].ev.ALPHA + (int)((cp[vleft].ev.ALPHA - cp[vn].ev.ALPHA) * lc);
  1372. hleft.ev.Fog = cp[vn].ev.Fog + ((cp[vleft].ev.Fog - cp[vn].ev.Fog ) * lc);
  1373. }
  1374. if (s2>0) {
  1375. ClipRes+=2;
  1376. /*
  1377. CalcHitPoint(C,cp[vn].ev.v,
  1378. cp[vright].ev.v, hright.ev.v);
  1379. float ll = VectorLength(SubVectors(cp[vright].ev.v, cp[vn].ev.v));
  1380. float lc = VectorLength(SubVectors(hright.ev.v, cp[vn].ev.v));
  1381. lc = lc / ll;*/
  1382. float lc = -s / (s2-s);
  1383. hright.ev.v.x = cp[vn].ev.v.x + ((cp[vright].ev.v.x - cp[vn].ev.v.x) * lc);
  1384. hright.ev.v.y = cp[vn].ev.v.y + ((cp[vright].ev.v.y - cp[vn].ev.v.y) * lc);
  1385. hright.ev.v.z = cp[vn].ev.v.z + ((cp[vright].ev.v.z - cp[vn].ev.v.z) * lc);
  1386. hright.tx = cp[vn].tx + ((cp[vright].tx - cp[vn].tx) * lc);
  1387. hright.ty = cp[vn].ty + ((cp[vright].ty - cp[vn].ty) * lc);
  1388. hright.ev.Light = cp[vn].ev.Light + (int)((cp[vright].ev.Light - cp[vn].ev.Light) * lc);
  1389. hright.ev.ALPHA = cp[vn].ev.ALPHA + (int)((cp[vright].ev.ALPHA - cp[vn].ev.ALPHA) * lc);
  1390. hright.ev.Fog = cp[vn].ev.Fog + ((cp[vright].ev.Fog - cp[vn].ev.Fog ) * lc);
  1391. }
  1392. if (ClipRes == 0) {
  1393. u--; vused--;
  1394. cp[vn] = cp[vn+1];
  1395. cp[vn+1] = cp[vn+2];
  1396. cp[vn+2] = cp[vn+3];
  1397. cp[vn+3] = cp[vn+4];
  1398. cp[vn+4] = cp[vn+5];
  1399. cp[vn+5] = cp[vn+6];
  1400. //memcpy(&cp[vn], &cp[vn+1], (15-vn)*sizeof(ClipPoint));
  1401. }
  1402. if (ClipRes == 1) {cp[vn] = hleft; }
  1403. if (ClipRes == 2) {cp[vn] = hright;}
  1404. if (ClipRes == 3) {
  1405. u++; vused++;
  1406. //memcpy(&cp[vn+1], &cp[vn], (15-vn)*sizeof(ClipPoint));
  1407. cp[vn+6] = cp[vn+5];
  1408. cp[vn+5] = cp[vn+4];
  1409. cp[vn+4] = cp[vn+3];
  1410. cp[vn+3] = cp[vn+2];
  1411. cp[vn+2] = cp[vn+1];
  1412. cp[vn+1] = cp[vn];
  1413. cp[vn] = hleft;
  1414. cp[vn+1] = hright;
  1415. }
  1416. }
  1417. void DrawTPlaneClip(BOOL SECONT)
  1418. {
  1419. if (!WATERREVERSE) {
  1420. MulVectorsVect(SubVectors(ev[1].v, ev[0].v), SubVectors(ev[2].v, ev[0].v), nv);
  1421. if (nv.x*ev[0].v.x + nv.y*ev[0].v.y + nv.z*ev[0].v.z<0) return;
  1422. }
  1423. cp[0].ev = ev[0]; cp[1].ev = ev[1]; cp[2].ev = ev[2];
  1424. if (ReverseOn)
  1425. if (SECONT) {
  1426. switch (TDirection) {
  1427. case 0:
  1428. cp[0].tx = TCMIN; cp[0].ty = TCMAX;
  1429. cp[1].tx = TCMAX; cp[1].ty = TCMIN;
  1430. cp[2].tx = TCMAX; cp[2].ty = TCMAX;
  1431. break;
  1432. case 1:
  1433. cp[0].tx = TCMAX; cp[0].ty = TCMAX;
  1434. cp[1].tx = TCMIN; cp[1].ty = TCMIN;
  1435. cp[2].tx = TCMAX; cp[2].ty = TCMIN;
  1436. break;
  1437. case 2:
  1438. cp[0].tx = TCMAX; cp[0].ty = TCMIN;
  1439. cp[1].tx = TCMIN; cp[1].ty = TCMAX;
  1440. cp[2].tx = TCMIN; cp[2].ty = TCMIN;
  1441. break;
  1442. case 3:
  1443. cp[0].tx = TCMIN; cp[0].ty = TCMIN;
  1444. cp[1].tx = TCMAX; cp[1].ty = TCMAX;
  1445. cp[2].tx = TCMIN; cp[2].ty = TCMAX;
  1446. break;
  1447. }
  1448. } else {
  1449. switch (TDirection) {
  1450. case 0:
  1451. cp[0].tx = TCMIN; cp[0].ty = TCMIN;
  1452. cp[1].tx = TCMAX; cp[1].ty = TCMIN;
  1453. cp[2].tx = TCMIN; cp[2].ty = TCMAX;
  1454. break;
  1455. case 1:
  1456. cp[0].tx = TCMIN; cp[0].ty = TCMAX;
  1457. cp[1].tx = TCMIN; cp[1].ty = TCMIN;
  1458. cp[2].tx = TCMAX; cp[2].ty = TCMAX;
  1459. break;
  1460. case 2:
  1461. cp[0].tx = TCMAX; cp[0].ty = TCMAX;
  1462. cp[1].tx = TCMIN; cp[1].ty = TCMAX;
  1463. cp[2].tx = TCMAX; cp[2].ty = TCMIN;
  1464. break;
  1465. case 3:
  1466. cp[0].tx = TCMAX; cp[0].ty = TCMIN;
  1467. cp[1].tx = TCMAX; cp[1].ty = TCMAX;
  1468. cp[2].tx = TCMIN; cp[2].ty = TCMIN;
  1469. break;
  1470. }
  1471. }
  1472. else
  1473. if (SECONT) {
  1474. switch (TDirection) {
  1475. case 0:
  1476. cp[0].tx = TCMIN; cp[0].ty = TCMIN;
  1477. cp[1].tx = TCMAX; cp[1].ty = TCMAX;
  1478. cp[2].tx = TCMIN; cp[2].ty = TCMAX;
  1479. break;
  1480. case 1:
  1481. cp[0].tx = TCMIN; cp[0].ty = TCMAX;
  1482. cp[1].tx = TCMAX; cp[1].ty = TCMIN;
  1483. cp[2].tx = TCMAX; cp[2].ty = TCMAX;
  1484. break;
  1485. case 2:
  1486. cp[0].tx = TCMAX; cp[0].ty = TCMAX;
  1487. cp[1].tx = TCMIN; cp[1].ty = TCMIN;
  1488. cp[2].tx = TCMAX; cp[2].ty = TCMIN;
  1489. break;
  1490. case 3:
  1491. cp[0].tx = TCMAX; cp[0].ty = TCMIN;
  1492. cp[1].tx = TCMIN; cp[1].ty = TCMAX;
  1493. cp[2].tx = TCMIN; cp[2].ty = TCMIN;
  1494. break;
  1495. }
  1496. } else {
  1497. switch (TDirection) {
  1498. case 0:
  1499. cp[0].tx = TCMIN; cp[0].ty = TCMIN;
  1500. cp[1].tx = TCMAX; cp[1].ty = TCMIN;
  1501. cp[2].tx = TCMAX; cp[2].ty = TCMAX;
  1502. break;
  1503. case 1:
  1504. cp[0].tx = TCMIN; cp[0].ty = TCMAX;
  1505. cp[1].tx = TCMIN; cp[1].ty = TCMIN;
  1506. cp[2].tx = TCMAX; cp[2].ty = TCMIN;
  1507. break;
  1508. case 2:
  1509. cp[0].tx = TCMAX; cp[0].ty = TCMAX;
  1510. cp[1].tx = TCMIN; cp[1].ty = TCMAX;
  1511. cp[2].tx = TCMIN; cp[2].ty = TCMIN;
  1512. break;
  1513. case 3:
  1514. cp[0].tx = TCMAX; cp[0].ty = TCMIN;
  1515. cp[1].tx = TCMAX; cp[1].ty = TCMAX;
  1516. cp[2].tx = TCMIN; cp[2].ty = TCMAX;
  1517. break;
  1518. }
  1519. }
  1520. vused = 3;
  1521. for (u=0; u<vused; u++) cp[u].ev.v.z+=16.0f;
  1522. for (u=0; u<vused; u++) ClipVector(ClipZ,u);
  1523. for (u=0; u<vused; u++) cp[u].ev.v.z-=16.0f;
  1524. if (vused<3) return;
  1525. for (u=0; u<vused; u++) ClipVector(ClipA,u); if (vused<3) return;
  1526. for (u=0; u<vused; u++) ClipVector(ClipB,u); if (vused<3) return;
  1527. for (u=0; u<vused; u++) ClipVector(ClipC,u); if (vused<3) return;
  1528. for (u=0; u<vused; u++) ClipVector(ClipD,u); if (vused<3) return;
  1529. for (u=0; u<vused; u++) {
  1530. cp[u].ev.scrx = VideoCXf - cp[u].ev.v.x / cp[u].ev.v.z * CameraW;
  1531. cp[u].ev.scry = VideoCYf + cp[u].ev.v.y / cp[u].ev.v.z * CameraH;
  1532. }
  1533. if (!lpVertexG)
  1534. d3dStartBufferG();
  1535. if (GVCnt>380) {
  1536. if (lpVertexG) d3dEndBufferG(FALSE);
  1537. d3dStartBufferG();
  1538. }
  1539. if (hGTexture!=hTexture) {
  1540. hGTexture=hTexture;
  1541. lpInstructionG->bOpcode = D3DOP_STATERENDER;
  1542. lpInstructionG->bSize = sizeof(D3DSTATE);
  1543. lpInstructionG->wCount = 1;
  1544. lpInstructionG++;
  1545. lpState = (LPD3DSTATE)lpInstructionG;
  1546. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREHANDLE;
  1547. lpState->dwArg[0] = hTexture;
  1548. lpState++;
  1549. lpInstructionG = (LPD3DINSTRUCTION)lpState;
  1550. lpwTriCount = &(lpInstructionG->wCount);
  1551. lpInstructionG->bOpcode = D3DOP_TRIANGLE;
  1552. lpInstructionG->bSize = sizeof(D3DTRIANGLE);
  1553. lpInstructionG->wCount = 0;
  1554. lpInstructionG++;
  1555. }
  1556. lpTriangle = (LPD3DTRIANGLE)lpInstructionG;
  1557. for (u=0; u<vused-2; u++) {
  1558. lpVertexG->sx = (float)cp[0].ev.scrx;
  1559. lpVertexG->sy = (float)cp[0].ev.scry;
  1560. lpVertexG->sz = _ZSCALE / cp[0].ev.v.z;
  1561. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  1562. lpVertexG->color = (int)(cp[0].ev.Light) * 0x00010101 | ((int)cp[0].ev.ALPHA<<24);
  1563. lpVertexG->specular = (255-(int)cp[0].ev.Fog)<<24;//0x7F000000;
  1564. lpVertexG->tu = (float)(cp[0].tx);// / (128.f*65536.f);
  1565. lpVertexG->tv = (float)(cp[0].ty);// / (128.f*65536.f);
  1566. lpVertexG++;
  1567. lpVertexG->sx = (float)cp[u+1].ev.scrx;
  1568. lpVertexG->sy = (float)cp[u+1].ev.scry;
  1569. lpVertexG->sz = _ZSCALE / cp[u+1].ev.v.z;
  1570. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  1571. lpVertexG->color = (int)(cp[u+1].ev.Light) * 0x00010101 | ((int)cp[u+1].ev.ALPHA<<24);
  1572. lpVertexG->specular = (255-(int)cp[u+1].ev.Fog)<<24;//0x7F000000;
  1573. lpVertexG->tu = (float)(cp[u+1].tx);// / (128.f*65536.f);
  1574. lpVertexG->tv = (float)(cp[u+1].ty);// / (128.f*65536.f);
  1575. lpVertexG++;
  1576. lpVertexG->sx = (float)cp[u+2].ev.scrx;
  1577. lpVertexG->sy = (float)cp[u+2].ev.scry;
  1578. lpVertexG->sz = _ZSCALE / cp[u+2].ev.v.z;
  1579. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  1580. lpVertexG->color = (int)(cp[u+2].ev.Light) * 0x00010101 | ((int)cp[u+2].ev.ALPHA<<24);
  1581. lpVertexG->specular = (255-(int)cp[u+2].ev.Fog)<<24;//0x7F000000;
  1582. lpVertexG->tu = (float)(cp[u+2].tx);// / (128.f*65536.f);
  1583. lpVertexG->tv = (float)(cp[u+2].ty);// / (128.f*65536.f);
  1584. lpVertexG++;
  1585. lpTriangle->wV1 = GVCnt;
  1586. lpTriangle->wV2 = GVCnt+1;
  1587. lpTriangle->wV3 = GVCnt+2;
  1588. lpTriangle->wFlags = 0;
  1589. lpTriangle++;
  1590. *lpwTriCount = (*lpwTriCount) + 1;
  1591. GVCnt+=3;
  1592. }
  1593. lpInstructionG = (LPD3DINSTRUCTION)lpTriangle;
  1594. }
  1595. void DrawTPlane(BOOL SECONT)
  1596. {
  1597. int n;
  1598. //if (!WATERREVERSE) {
  1599. MulVectorsVect(SubVectors(ev[1].v, ev[0].v), SubVectors(ev[2].v, ev[0].v), nv);
  1600. if (nv.x*ev[0].v.x + nv.y*ev[0].v.y + nv.z*ev[0].v.z<0) return;
  1601. //}
  1602. Mask1=0x007F;
  1603. for (n=0; n<3; n++) {
  1604. if (ev[n].DFlags & 128) return;
  1605. Mask1=Mask1 & ev[n].DFlags; }
  1606. if (Mask1>0) return;
  1607. if (ReverseOn)
  1608. if (SECONT) {
  1609. switch (TDirection) {
  1610. case 0:
  1611. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  1612. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  1613. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  1614. break;
  1615. case 1:
  1616. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  1617. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  1618. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  1619. break;
  1620. case 2:
  1621. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  1622. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  1623. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  1624. break;
  1625. case 3:
  1626. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  1627. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  1628. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  1629. break;
  1630. }
  1631. } else {
  1632. switch (TDirection) {
  1633. case 0:
  1634. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  1635. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  1636. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  1637. break;
  1638. case 1:
  1639. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  1640. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  1641. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  1642. break;
  1643. case 2:
  1644. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  1645. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  1646. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  1647. break;
  1648. case 3:
  1649. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  1650. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  1651. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  1652. break;
  1653. }
  1654. }
  1655. else
  1656. if (SECONT) {
  1657. switch (TDirection) {
  1658. case 0:
  1659. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  1660. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  1661. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  1662. break;
  1663. case 1:
  1664. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  1665. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  1666. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  1667. break;
  1668. case 2:
  1669. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  1670. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  1671. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  1672. break;
  1673. case 3:
  1674. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  1675. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  1676. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  1677. break;
  1678. }
  1679. } else {
  1680. switch (TDirection) {
  1681. case 0:
  1682. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  1683. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  1684. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  1685. break;
  1686. case 1:
  1687. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  1688. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  1689. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  1690. break;
  1691. case 2:
  1692. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  1693. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  1694. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  1695. break;
  1696. case 3:
  1697. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  1698. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  1699. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  1700. break;
  1701. }
  1702. }
  1703. int alpha1 = 255;
  1704. int alpha2 = 255;
  1705. int alpha3 = 255;
  1706. //if (!WATERREVERSE)
  1707. if (zs > (ctViewR-8)<<8) {
  1708. int zz;
  1709. zz = (int)VectorLength(ev[0].v) - 256 * (ctViewR-4);
  1710. if (zz > 0) alpha1 = max(0, 255 - zz / 3); else alpha1 = 255;
  1711. zz = (int)VectorLength(ev[1].v) - 256 * (ctViewR-4);
  1712. if (zz > 0) alpha2 = max(0, 255 - zz / 3); else alpha2 = 255;
  1713. zz = (int)VectorLength(ev[2].v) - 256 * (ctViewR-4);
  1714. if (zz > 0) alpha3 = max(0, 255 - zz / 3); else alpha3 = 255;
  1715. }
  1716. if (!lpVertexG)
  1717. d3dStartBufferG();
  1718. if (GVCnt>380) {
  1719. if (lpVertexG) d3dEndBufferG(FALSE);
  1720. d3dStartBufferG();
  1721. }
  1722. lpVertexG->sx = (float)ev[0].scrx;
  1723. lpVertexG->sy = (float)ev[0].scry;
  1724. lpVertexG->sz = _ZSCALE / ev[0].v.z;
  1725. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  1726. lpVertexG->color = (int)(ev[0].Light) * 0x00010101 | alpha1<<24;
  1727. lpVertexG->specular = (255-(int)ev[0].Fog)<<24;//0x7F000000;
  1728. lpVertexG->tu = (float)(scrp[0].tx);// / (128.f*65536.f);
  1729. lpVertexG->tv = (float)(scrp[0].ty);// / (128.f*65536.f);
  1730. lpVertexG++;
  1731. lpVertexG->sx = (float)ev[1].scrx;
  1732. lpVertexG->sy = (float)ev[1].scry;
  1733. lpVertexG->sz = _ZSCALE / ev[1].v.z;
  1734. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  1735. lpVertexG->color = (int)(ev[1].Light) * 0x00010101 | alpha2<<24;
  1736. lpVertexG->specular = (255-(int)ev[1].Fog)<<24;//0x7F000000;
  1737. lpVertexG->tu = (float)(scrp[1].tx);// / (128.f*65536.f);
  1738. lpVertexG->tv = (float)(scrp[1].ty);// / (128.f*65536.f);
  1739. lpVertexG++;
  1740. lpVertexG->sx = (float)ev[2].scrx;
  1741. lpVertexG->sy = (float)ev[2].scry;
  1742. lpVertexG->sz = _ZSCALE / ev[2].v.z;
  1743. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  1744. lpVertexG->color = (int)(ev[2].Light) * 0x00010101 | alpha3<<24;
  1745. lpVertexG->specular = (255-(int)ev[2].Fog)<<24;//0x7F000000;
  1746. lpVertexG->tu = (float)(scrp[2].tx);// / (128.f*65536.f);
  1747. lpVertexG->tv = (float)(scrp[2].ty);// / (128.f*65536.f);
  1748. lpVertexG++;
  1749. if (hGTexture!=hTexture) {
  1750. hGTexture=hTexture;
  1751. lpInstructionG->bOpcode = D3DOP_STATERENDER;
  1752. lpInstructionG->bSize = sizeof(D3DSTATE);
  1753. lpInstructionG->wCount = 1;
  1754. lpInstructionG++;
  1755. lpState = (LPD3DSTATE)lpInstructionG;
  1756. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREHANDLE;
  1757. lpState->dwArg[0] = hTexture;
  1758. lpState++;
  1759. lpInstructionG = (LPD3DINSTRUCTION)lpState;
  1760. lpwTriCount = (&lpInstructionG->wCount);
  1761. lpInstructionG->bOpcode = D3DOP_TRIANGLE;
  1762. lpInstructionG->bSize = sizeof(D3DTRIANGLE);
  1763. lpInstructionG->wCount = 0;
  1764. lpInstructionG++;
  1765. }
  1766. lpTriangle = (LPD3DTRIANGLE)lpInstructionG;
  1767. lpTriangle->wV1 = GVCnt;
  1768. lpTriangle->wV2 = GVCnt+1;
  1769. lpTriangle->wV3 = GVCnt+2;
  1770. lpTriangle->wFlags = 0;
  1771. lpTriangle++;
  1772. *lpwTriCount = (*lpwTriCount) + 1;
  1773. lpInstructionG = (LPD3DINSTRUCTION)lpTriangle;
  1774. GVCnt+=3;
  1775. }
  1776. void DrawTPlaneW(BOOL SECONT)
  1777. {
  1778. int n;
  1779. Mask1=0x007F;
  1780. for (n=0; n<3; n++) {
  1781. if (ev[n].DFlags & 128) return;
  1782. Mask1=Mask1 & ev[n].DFlags; }
  1783. if (Mask1>0) return;
  1784. /*
  1785. for (n=0; n<3; n++) {
  1786. scrp[n].x = (float)(VideoCX) - (ev[n].v.x / ev[n].v.z * CameraW);
  1787. scrp[n].y = (float)(VideoCY) + (ev[n].v.y / ev[n].v.z * CameraH);
  1788. scrp[n].Light = ev[n].Light;
  1789. } */
  1790. if (ReverseOn)
  1791. if (SECONT) {
  1792. switch (TDirection) {
  1793. case 0:
  1794. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  1795. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  1796. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  1797. break;
  1798. case 1:
  1799. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  1800. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  1801. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  1802. break;
  1803. case 2:
  1804. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  1805. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  1806. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  1807. break;
  1808. case 3:
  1809. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  1810. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  1811. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  1812. break;
  1813. }
  1814. } else {
  1815. switch (TDirection) {
  1816. case 0:
  1817. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  1818. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  1819. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  1820. break;
  1821. case 1:
  1822. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  1823. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  1824. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  1825. break;
  1826. case 2:
  1827. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  1828. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  1829. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  1830. break;
  1831. case 3:
  1832. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  1833. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  1834. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  1835. break;
  1836. }
  1837. }
  1838. else
  1839. if (SECONT) {
  1840. switch (TDirection) {
  1841. case 0:
  1842. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  1843. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  1844. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  1845. break;
  1846. case 1:
  1847. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  1848. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  1849. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  1850. break;
  1851. case 2:
  1852. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  1853. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  1854. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  1855. break;
  1856. case 3:
  1857. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  1858. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  1859. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  1860. break;
  1861. }
  1862. } else {
  1863. switch (TDirection) {
  1864. case 0:
  1865. scrp[0].tx = TCMIN; scrp[0].ty = TCMIN;
  1866. scrp[1].tx = TCMAX; scrp[1].ty = TCMIN;
  1867. scrp[2].tx = TCMAX; scrp[2].ty = TCMAX;
  1868. break;
  1869. case 1:
  1870. scrp[0].tx = TCMIN; scrp[0].ty = TCMAX;
  1871. scrp[1].tx = TCMIN; scrp[1].ty = TCMIN;
  1872. scrp[2].tx = TCMAX; scrp[2].ty = TCMIN;
  1873. break;
  1874. case 2:
  1875. scrp[0].tx = TCMAX; scrp[0].ty = TCMAX;
  1876. scrp[1].tx = TCMIN; scrp[1].ty = TCMAX;
  1877. scrp[2].tx = TCMIN; scrp[2].ty = TCMIN;
  1878. break;
  1879. case 3:
  1880. scrp[0].tx = TCMAX; scrp[0].ty = TCMIN;
  1881. scrp[1].tx = TCMAX; scrp[1].ty = TCMAX;
  1882. scrp[2].tx = TCMIN; scrp[2].ty = TCMAX;
  1883. break;
  1884. }
  1885. }
  1886. if (!UNDERWATER)
  1887. if (zs > (ctViewR-8)<<8) {
  1888. float zz;
  1889. zz = VectorLength(ev[0].v) - 256 * (ctViewR-4);
  1890. if (zz > 0) ev[0].ALPHA = max(0.f,255.f - zz / 3.f);
  1891. zz = VectorLength(ev[1].v) - 256 * (ctViewR-4);
  1892. if (zz > 0) ev[1].ALPHA = max(0.f, 255.f - zz / 3.f);
  1893. zz = VectorLength(ev[2].v) - 256 * (ctViewR-4);
  1894. if (zz > 0) ev[2].ALPHA = max(0,255.f - zz / 3.f);
  1895. }
  1896. if (!lpVertexG)
  1897. d3dStartBufferG();
  1898. if (GVCnt>380) {
  1899. if (lpVertexG) d3dEndBufferG(FALSE);
  1900. d3dStartBufferG();
  1901. }
  1902. lpVertexG->sx = (float)ev[0].scrx;
  1903. lpVertexG->sy = (float)ev[0].scry;
  1904. lpVertexG->sz = _ZSCALE / ev[0].v.z;
  1905. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  1906. lpVertexG->color = (int)(ev[0].Light) * 0x00010101 | ev[0].ALPHA<<24;
  1907. lpVertexG->specular = (255-(int)ev[0].Fog)<<24;
  1908. lpVertexG->tu = (float)(scrp[0].tx);
  1909. lpVertexG->tv = (float)(scrp[0].ty);
  1910. lpVertexG++;
  1911. lpVertexG->sx = (float)ev[1].scrx;
  1912. lpVertexG->sy = (float)ev[1].scry;
  1913. lpVertexG->sz = _ZSCALE / ev[1].v.z;
  1914. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  1915. lpVertexG->color = (int)(ev[1].Light) * 0x00010101 | ev[1].ALPHA<<24;
  1916. lpVertexG->specular = (255-(int)ev[1].Fog)<<24;
  1917. lpVertexG->tu = (float)(scrp[1].tx);
  1918. lpVertexG->tv = (float)(scrp[1].ty);
  1919. lpVertexG++;
  1920. lpVertexG->sx = (float)ev[2].scrx;
  1921. lpVertexG->sy = (float)ev[2].scry;
  1922. lpVertexG->sz = _ZSCALE / ev[2].v.z;
  1923. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  1924. lpVertexG->color = (int)(ev[2].Light) * 0x00010101 | ev[2].ALPHA<<24;
  1925. lpVertexG->specular = (255-(int)ev[2].Fog)<<24;
  1926. lpVertexG->tu = (float)(scrp[2].tx);
  1927. lpVertexG->tv = (float)(scrp[2].ty);
  1928. lpVertexG++;
  1929. if (hGTexture!=hTexture) {
  1930. hGTexture=hTexture;
  1931. lpInstructionG->bOpcode = D3DOP_STATERENDER;
  1932. lpInstructionG->bSize = sizeof(D3DSTATE);
  1933. lpInstructionG->wCount = 1;
  1934. lpInstructionG++;
  1935. lpState = (LPD3DSTATE)lpInstructionG;
  1936. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREHANDLE;
  1937. lpState->dwArg[0] = hTexture;
  1938. lpState++;
  1939. lpInstructionG = (LPD3DINSTRUCTION)lpState;
  1940. lpwTriCount = (&lpInstructionG->wCount);
  1941. lpInstructionG->bOpcode = D3DOP_TRIANGLE;
  1942. lpInstructionG->bSize = sizeof(D3DTRIANGLE);
  1943. lpInstructionG->wCount = 0;
  1944. lpInstructionG++;
  1945. }
  1946. lpTriangle = (LPD3DTRIANGLE)lpInstructionG;
  1947. lpTriangle->wV1 = GVCnt;
  1948. lpTriangle->wV2 = GVCnt+1;
  1949. lpTriangle->wV3 = GVCnt+2;
  1950. lpTriangle->wFlags = 0;
  1951. lpTriangle++;
  1952. *lpwTriCount = (*lpwTriCount) + 1;
  1953. lpInstructionG = (LPD3DINSTRUCTION)lpTriangle;
  1954. GVCnt+=3;
  1955. }
  1956. void _RenderObject(int x, int y)
  1957. {
  1958. int ob = OMap[y][x];
  1959. if (!MObjects[ob].model) {
  1960. //return;
  1961. wsprintf(logt,"Incorrect model at [%d][%d]!", x, y);
  1962. DoHalt(logt);
  1963. }
  1964. //int mlight = -(RandomMap[y & 31][x & 31] >> 5) + (LMap[y][x]>>1) + 96;
  1965. int FI = (FMap[y][x] >> 2) & 3;
  1966. float fi = CameraAlpha + (float)(FI * 2.f*pi / 4.f);
  1967. int mlight;
  1968. if (MObjects[ob].info.flags & ofDEFLIGHT)
  1969. mlight = MObjects[ob].info.DefLight; else
  1970. if (MObjects[ob].info.flags & ofGRNDLIGHT)
  1971. {
  1972. mlight = 128;
  1973. CalcModelGroundLight(MObjects[ob].model, x*256+128, y*256+128, FI);
  1974. FI = 0;
  1975. }
  1976. else
  1977. mlight = -(RandomMap[y & 31][x & 31] >> 5) + (LMap[y][x]>>1) + 96;
  1978. if (mlight >192) mlight =192;
  1979. if (mlight < 64) mlight = 64;
  1980. v[0].x = x*256+128 - CameraX;
  1981. v[0].z = y*256+128 - CameraZ;
  1982. v[0].y = (float)(HMapO[y][x]) * ctHScale - CameraY;
  1983. float zs = VectorLength(v[0]);
  1984. //if (v[0].y + MObjects[ob].info.YHi < (int)(HMap[y][x]+HMap[y+1][x+1]) / 2 * ctHScale - CameraY) return;
  1985. CalcFogLevel_Gradient(v[0]);
  1986. v[0] = RotateVector(v[0]);
  1987. GlassL = 0;
  1988. if (zs > 256 * (ctViewR-8))
  1989. GlassL=min(255,(int)(zs - 256 * (ctViewR-8)) / 4);
  1990. if (GlassL==255) return;
  1991. if (MObjects[ob].info.flags & ofANIMATED)
  1992. if (MObjects[ob].info.LastAniTime!=RealTime) {
  1993. MObjects[ob].info.LastAniTime=RealTime;
  1994. CreateMorphedObject(MObjects[ob].model,
  1995. MObjects[ob].vtl,
  1996. RealTime % MObjects[ob].vtl.AniTime);
  1997. }
  1998. if (MObjects[ob].info.flags & ofNOBMP) zs = 0;
  1999. /*
  2000. if (!NeedWater && zs>ctViewRM*256 && zs<(ctViewRM+1)*256) {
  2001. GlassL = 255-(int)(zs - ctViewRM*256);
  2002. RenderBMPModel(&MObjects[ob].bmpmodel, v[0].x, v[0].y, v[0].z, mlight-32);
  2003. GlassL=255-GlassL;
  2004. RenderModel(MObjects[ob].model, v[0].x, v[0].y, v[0].z, mlight, FI, fi, CameraBeta);
  2005. } else */
  2006. if (zs>ctViewRM*256)
  2007. RenderBMPModel(&MObjects[ob].bmpmodel, v[0].x, v[0].y, v[0].z, mlight-16);
  2008. else
  2009. if (v[0].z<-256*8)
  2010. RenderModel(MObjects[ob].model, v[0].x, v[0].y, v[0].z, mlight, FI, fi, CameraBeta);
  2011. else
  2012. RenderModelClip(MObjects[ob].model, v[0].x, v[0].y, v[0].z, mlight, FI, fi, CameraBeta);
  2013. }
  2014. void RenderObject(int x, int y)
  2015. {
  2016. if (OMap[y][x]==255) return;
  2017. if (!MODELS) return;
  2018. if (ORLCount>2000) return;
  2019. ORList[ORLCount].x = x;
  2020. ORList[ORLCount].y = y;
  2021. ORLCount++;
  2022. }
  2023. void RenderModelsList()
  2024. {
  2025. d3dEndBufferG(FALSE);
  2026. for (int o=0; o<ORLCount; o++)
  2027. _RenderObject(ORList[o].x, ORList[o].y);
  2028. ORLCount=0;
  2029. d3dEndBufferG(TRUE);
  2030. }
  2031. void ProcessMap(int x, int y, int r)
  2032. {
  2033. //WATERREVERSE = FALSE;
  2034. if (x>=ctMapSize-1 || y>=ctMapSize-1 ||
  2035. x<0 || y<0) return;
  2036. float BackR = BackViewR;
  2037. if (OMap[y][x]!=255) BackR+=MObjects[OMap[y][x]].info.BoundR;
  2038. ev[0] = VMap[y-CCY+128][x-CCX+128];
  2039. if (ev[0].v.z>BackR) return;
  2040. int t1 = TMap1[y][x];
  2041. ReverseOn = (FMap[y][x] & fmReverse);
  2042. TDirection = (FMap[y][x] & 3);
  2043. x = x - CCX + 128;
  2044. y = y - CCY + 128;
  2045. ev[1] = VMap[y][x+1];
  2046. if (ReverseOn) ev[2] = VMap[y+1][x];
  2047. else ev[2] = VMap[y+1][x+1];
  2048. //Mask1=0x007F;
  2049. //for (n=0; n<3; n++) Mask1&=ev[n].DFlags;
  2050. //if (Mask1) return;
  2051. float xx = (ev[0].v.x + VMap[y+1][x+1].v.x) / 2;
  2052. float yy = (ev[0].v.y + VMap[y+1][x+1].v.y) / 2;
  2053. float zz = (ev[0].v.z + VMap[y+1][x+1].v.z) / 2;
  2054. if ( fabs(xx*FOVK) > -zz + BackR) return;
  2055. zs = (int)sqrt( xx*xx + zz*zz + yy*yy);
  2056. /*
  2057. if (MIPMAP && (zs > 256 * 10 && t1 || LOWRESTX)) d3dSetTexture(Textures[t1]->DataB, 64, 64);
  2058. else d3dSetTexture(Textures[t1]->DataA, 128, 128);
  2059. */
  2060. if (!LOWRESTX) {
  2061. if (zs > 256 * 20) d3dSetTexture(Textures[t1]->DataC, 32, 32); else
  2062. if (zs > 256 * 10) d3dSetTexture(Textures[t1]->DataB, 64, 64);
  2063. else d3dSetTexture(Textures[t1]->DataA, 128, 128);
  2064. } else {
  2065. if (zs > 256 * 10) d3dSetTexture(Textures[t1]->DataC, 32, 32);
  2066. else d3dSetTexture(Textures[t1]->DataB, 64, 64);
  2067. }
  2068. if (r>8) DrawTPlane(FALSE);
  2069. else DrawTPlaneClip(FALSE);
  2070. if (ReverseOn) { ev[0] = ev[2]; ev[2] = VMap[y+1][x+1]; }
  2071. else { ev[1] = ev[2]; ev[2] = VMap[y+1][x]; }
  2072. if (r>8) DrawTPlane(TRUE);
  2073. else DrawTPlaneClip(TRUE);
  2074. x = x + CCX - 128;
  2075. y = y + CCY - 128;
  2076. if (OMap[y][x]==255) return;
  2077. if (zz<BackR)
  2078. RenderObject(x, y);
  2079. }
  2080. void ProcessMap2(int x, int y, int r)
  2081. {
  2082. //WATERREVERSE = FALSE;
  2083. if (x>=ctMapSize-1 || y>=ctMapSize-1 ||
  2084. x<0 || y<0) return;
  2085. ev[0] = VMap[y-CCY+128][x-CCX+128];
  2086. if (ev[0].v.z>BackViewR) return;
  2087. int t1 = TMap2[y][x];
  2088. TDirection = ((FMap[y][x]>>8) & 3);
  2089. ReverseOn = FALSE;
  2090. x = x - CCX + 128;
  2091. y = y - CCY + 128;
  2092. ev[1] = VMap[y][x+2];
  2093. if (ReverseOn) ev[2] = VMap[y+2][x];
  2094. else ev[2] = VMap[y+2][x+2];
  2095. float xx = (ev[0].v.x + VMap[y+2][x+2].v.x) / 2;
  2096. float yy = (ev[0].v.y + VMap[y+2][x+2].v.y) / 2;
  2097. float zz = (ev[0].v.z + VMap[y+2][x+2].v.z) / 2;
  2098. if ( fabs(xx*FOVK) > -zz + BackViewR) return;
  2099. zs = (int)sqrt( xx*xx + zz*zz + yy*yy);
  2100. if (zs>ctViewR*256) return;
  2101. d3dSetTexture(Textures[t1]->DataB, 64, 64);
  2102. DrawTPlane(FALSE);
  2103. if (ReverseOn) { ev[0] = ev[2]; ev[2] = VMap[y+2][x+2]; }
  2104. else { ev[1] = ev[2]; ev[2] = VMap[y+2][x]; }
  2105. DrawTPlane(TRUE);
  2106. x = x + CCX - 128;
  2107. y = y + CCY - 128;
  2108. RenderObject(x , y);
  2109. RenderObject(x+1, y);
  2110. RenderObject(x , y+1);
  2111. RenderObject(x+1, y+1);
  2112. }
  2113. void ProcessMapW(int x, int y, int r)
  2114. {
  2115. if (!( (FMap[y ][x ] & fmWaterA) &&
  2116. (FMap[y ][x+1] & fmWaterA) &&
  2117. (FMap[y+1][x ] & fmWaterA) &&
  2118. (FMap[y+1][x+1] & fmWaterA) )) return;
  2119. WATERREVERSE = TRUE;
  2120. int t1 = WaterList[ WMap[y][x] ].tindex;
  2121. ev[0] = VMap2[y-CCY+128][x-CCX+128];
  2122. if (ev[0].v.z>BackViewR) return;
  2123. ReverseOn = FALSE;
  2124. TDirection = 0;
  2125. x = x - CCX + 128;
  2126. y = y - CCY + 128;
  2127. ev[1] = VMap2[y][x+1];
  2128. ev[2] = VMap2[y+1][x+1];
  2129. float xx = (ev[0].v.x + VMap2[y+1][x+1].v.x) / 2;
  2130. float yy = (ev[0].v.y + VMap2[y+1][x+1].v.y) / 2;
  2131. float zz = (ev[0].v.z + VMap2[y+1][x+1].v.z) / 2;
  2132. if ( fabs(xx*FOVK) > -zz + BackViewR) return;
  2133. zs = (int)sqrt( xx*xx + zz*zz + yy*yy);
  2134. if (zs > ctViewR*256) return;
  2135. /*
  2136. if (MIPMAP && (zs > 256 * 10 && t1 || LOWRESTX)) d3dSetTexture(Textures[t1]->DataB, 64, 64);
  2137. else d3dSetTexture(Textures[t1]->DataA, 128, 128);
  2138. */
  2139. if (!LOWRESTX) {
  2140. if (zs > 256 * 20) d3dSetTexture(Textures[t1]->DataC, 32, 32); else
  2141. if (zs > 256 * 10) d3dSetTexture(Textures[t1]->DataB, 64, 64);
  2142. else d3dSetTexture(Textures[t1]->DataA, 128, 128);
  2143. } else {
  2144. if (zs > 256 * 10) d3dSetTexture(Textures[t1]->DataC, 32, 32);
  2145. else d3dSetTexture(Textures[t1]->DataB, 64, 64);
  2146. }
  2147. if (r>8) DrawTPlaneW(FALSE);
  2148. else DrawTPlaneClip(FALSE);
  2149. ev[1] = ev[2]; ev[2] = VMap2[y+1][x];
  2150. if (r>8) DrawTPlaneW(TRUE);
  2151. else DrawTPlaneClip(TRUE);
  2152. WATERREVERSE = FALSE;
  2153. }
  2154. void ProcessMapW2(int x, int y, int r)
  2155. {
  2156. if (!( (FMap[y ][x ] & fmWaterA) &&
  2157. (FMap[y ][x+2] & fmWaterA) &&
  2158. (FMap[y+2][x ] & fmWaterA) &&
  2159. (FMap[y+2][x+2] & fmWaterA) )) return;
  2160. int t1 = WaterList[ WMap[y][x] ].tindex;
  2161. ev[0] = VMap2[y-CCY+128][x-CCX+128];
  2162. if (ev[0].v.z>BackViewR) return;
  2163. //WATERREVERSE = TRUE;
  2164. ReverseOn = FALSE;
  2165. TDirection = 0;
  2166. x = x - CCX + 128;
  2167. y = y - CCY + 128;
  2168. ev[1] = VMap2[y][x+2];
  2169. ev[2] = VMap2[y+2][x+2];
  2170. float xx = (ev[0].v.x + VMap2[y+2][x+2].v.x) / 2;
  2171. float yy = (ev[0].v.y + VMap2[y+2][x+2].v.y) / 2;
  2172. float zz = (ev[0].v.z + VMap2[y+2][x+2].v.z) / 2;
  2173. if ( fabs(xx*FOVK) > -zz + BackViewR) return;
  2174. zs = (int)sqrt( xx*xx + zz*zz + yy*yy);
  2175. if (zs > ctViewR*256) return;
  2176. d3dSetTexture(Textures[t1]->DataB, 64, 64);
  2177. DrawTPlaneW(FALSE);
  2178. ev[1] = ev[2]; ev[2] = VMap2[y+2][x];
  2179. DrawTPlaneW(TRUE);
  2180. }
  2181. void RenderGround()
  2182. {
  2183. for (r=ctViewR; r>=ctViewR1; r-=2) {
  2184. for (int x=r; x>0; x-=2) {
  2185. ProcessMap2(CCX-x, CCY+r, r);
  2186. ProcessMap2(CCX+x, CCY+r, r);
  2187. ProcessMap2(CCX-x, CCY-r, r);
  2188. ProcessMap2(CCX+x, CCY-r, r);
  2189. }
  2190. ProcessMap2(CCX, CCY-r, r);
  2191. ProcessMap2(CCX, CCY+r, r);
  2192. for (int y=r-2; y>0; y-=2) {
  2193. ProcessMap2(CCX+r, CCY-y, r);
  2194. ProcessMap2(CCX+r, CCY+y, r);
  2195. ProcessMap2(CCX-r, CCY+y, r);
  2196. ProcessMap2(CCX-r, CCY-y, r);
  2197. }
  2198. ProcessMap2(CCX-r, CCY, r);
  2199. ProcessMap2(CCX+r, CCY, r);
  2200. }
  2201. r = ctViewR1-1;
  2202. for (int x=r; x>-r; x--) {
  2203. ProcessMap(CCX+r, CCY+x, r);
  2204. ProcessMap(CCX+x, CCY+r, r);
  2205. }
  2206. for (r=ctViewR1-2; r>0; r--) {
  2207. for (int x=r; x>0; x--) {
  2208. ProcessMap(CCX-x, CCY+r, r);
  2209. ProcessMap(CCX+x, CCY+r, r);
  2210. ProcessMap(CCX-x, CCY-r, r);
  2211. ProcessMap(CCX+x, CCY-r, r);
  2212. }
  2213. ProcessMap(CCX, CCY-r, r);
  2214. ProcessMap(CCX, CCY+r, r);
  2215. for (int y=r-1; y>0; y--) {
  2216. ProcessMap(CCX+r, CCY-y, r);
  2217. ProcessMap(CCX+r, CCY+y, r);
  2218. ProcessMap(CCX-r, CCY+y, r);
  2219. ProcessMap(CCX-r, CCY-y, r);
  2220. }
  2221. ProcessMap(CCX-r, CCY, r);
  2222. ProcessMap(CCX+r, CCY, r);
  2223. }
  2224. ProcessMap(CCX, CCY, 0);
  2225. }
  2226. void RenderWCircles()
  2227. {
  2228. TWCircle *wptr;
  2229. Vector3d rpos;
  2230. for (int c=0; c<WCCount; c++) {
  2231. wptr = &WCircles[c];
  2232. rpos.x = wptr->pos.x - CameraX;
  2233. rpos.y = wptr->pos.y - CameraY;
  2234. rpos.z = wptr->pos.z - CameraZ;
  2235. float r = (float)max( fabs(rpos.x), fabs(rpos.z) );
  2236. int ri = -1 + (int)(r / 256.f + 0.4f);
  2237. if (ri < 0) ri = 0;
  2238. if (ri > ctViewR) continue;
  2239. rpos = RotateVector(rpos);
  2240. if ( rpos.z > BackViewR) continue;
  2241. if ( fabs(rpos.x) > -rpos.z + BackViewR ) continue;
  2242. if ( fabs(rpos.y) > -rpos.z + BackViewR ) continue;
  2243. GlassL = 255 - (2000-wptr->FTime) / 38;
  2244. CreateMorphedModel(WCircleModel.mptr, &WCircleModel.Animation[0], (int)(wptr->FTime), wptr->scale);
  2245. if ( fabs(rpos.z) + fabs(rpos.x) < 1000)
  2246. RenderModelClip(WCircleModel.mptr,
  2247. rpos.x, rpos.y, rpos.z, 250, 0, 0, CameraBeta);
  2248. else
  2249. RenderModel(WCircleModel.mptr,
  2250. rpos.x, rpos.y, rpos.z, 250, 0, 0, CameraBeta);
  2251. }
  2252. GlassL = 0;
  2253. }
  2254. void RenderWater()
  2255. {
  2256. SetRenderStates(FALSE, D3DBLEND_INVSRCALPHA);
  2257. for (int r=ctViewR; r>=ctViewR1; r-=2) {
  2258. for (int x=r; x>0; x-=2) {
  2259. ProcessMapW2(CCX-x, CCY+r, r);
  2260. ProcessMapW2(CCX+x, CCY+r, r);
  2261. ProcessMapW2(CCX-x, CCY-r, r);
  2262. ProcessMapW2(CCX+x, CCY-r, r);
  2263. }
  2264. ProcessMapW2(CCX, CCY-r, r);
  2265. ProcessMapW2(CCX, CCY+r, r);
  2266. for (int y=r-2; y>0; y-=2) {
  2267. ProcessMapW2(CCX+r, CCY-y, r);
  2268. ProcessMapW2(CCX+r, CCY+y, r);
  2269. ProcessMapW2(CCX-r, CCY+y, r);
  2270. ProcessMapW2(CCX-r, CCY-y, r);
  2271. }
  2272. ProcessMapW2(CCX-r, CCY, r);
  2273. ProcessMapW2(CCX+r, CCY, r);
  2274. }
  2275. for (int y=-ctViewR1+2; y<ctViewR1; y++)
  2276. for (int x=-ctViewR1+2; x<ctViewR1; x++)
  2277. ProcessMapW(CCX+x, CCY+y, max(abs(x), abs(y)));
  2278. d3dEndBufferG(FALSE);
  2279. FogYBase = 0;
  2280. SetRenderStates(FALSE, D3DBLEND_ONE);
  2281. RenderWCircles();
  2282. SetRenderStates(TRUE, D3DBLEND_INVSRCALPHA);
  2283. }
  2284. void RenderPoint(float cx, float cy, float z, float _R, DWORD RGBA, DWORD RGBA2)
  2285. {
  2286. /*
  2287. RGBA = (RGBA & 0xFF00FF00) + ((RGBA & 0x000000FF)<<16) + ((RGBA & 0x00FF0000)>>16);
  2288. RGBA2= (RGBA2 & 0xFF00FF00) + ((RGBA2 & 0x000000FF)<<16) + ((RGBA2 & 0x00FF0000)>>16);
  2289. */
  2290. float R = (float)((int)( _R*16.f)) / 16.f;
  2291. float R2= (float)((int)(0.6f* _R*16.f)) / 16.f;
  2292. float sz = _ZSCALE / z;
  2293. if (R>1) R=1;
  2294. DWORD A = (int)(((RGBA>>25) + (RGBA2>>25)) * R);
  2295. RGBA = (RGBA & 0x00FFFFFF) | (A<<24);
  2296. lpVertex->sx = cx;
  2297. lpVertex->sy = cy-R2;
  2298. lpVertex->color = RGBA;
  2299. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2300. lpVertex->sx = cx-R2;
  2301. lpVertex->sy = cy+R2;
  2302. lpVertex->color = RGBA;
  2303. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2304. lpVertex->sx = cx+R2;
  2305. lpVertex->sy = cy+R2;
  2306. lpVertex->color = RGBA;
  2307. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2308. //================ 1
  2309. }
  2310. void RenderCircle(float cx, float cy, float z, float _R, DWORD RGBA, DWORD RGBA2)
  2311. {
  2312. /*
  2313. RGBA = (RGBA & 0xFF00FF00) + ((RGBA & 0x000000FF)<<16) + ((RGBA & 0x00FF0000)>>16);
  2314. RGBA2= (RGBA2 & 0xFF00FF00) + ((RGBA2 & 0x000000FF)<<16) + ((RGBA2 & 0x00FF0000)>>16);
  2315. */
  2316. float R = (float)((int)( _R*16.f)) / 16.f;
  2317. float R2 = (float)((int)(0.65f*_R*16.f)) / 16.f;
  2318. float sz = _ZSCALE / z;
  2319. lpVertex->sx = cx;
  2320. lpVertex->sy = cy;
  2321. lpVertex->color = RGBA;
  2322. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2323. lpVertex->sx = cx;
  2324. lpVertex->sy = cy-R;
  2325. lpVertex->color = RGBA2;
  2326. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2327. lpVertex->sx = cx+R2;
  2328. lpVertex->sy = cy-R2;
  2329. lpVertex->color = RGBA2;
  2330. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2331. //================ 1
  2332. lpVertex->sx = cx;
  2333. lpVertex->sy = cy;
  2334. lpVertex->color = RGBA;
  2335. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2336. lpVertex->sx = cx+R2;
  2337. lpVertex->sy = cy-R2;
  2338. lpVertex->color = RGBA2;
  2339. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2340. lpVertex->sx = cx+R;
  2341. lpVertex->sy = cy;
  2342. lpVertex->color = RGBA2;
  2343. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2344. //================ 2
  2345. lpVertex->sx = cx;
  2346. lpVertex->sy = cy;
  2347. lpVertex->color = RGBA;
  2348. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2349. lpVertex->sx = cx+R;
  2350. lpVertex->sy = cy;
  2351. lpVertex->color = RGBA2;
  2352. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2353. lpVertex->sx = cx+R2;
  2354. lpVertex->sy = cy+R2;
  2355. lpVertex->color = RGBA2;
  2356. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2357. //================ 3
  2358. lpVertex->sx = cx;
  2359. lpVertex->sy = cy;
  2360. lpVertex->color = RGBA;
  2361. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2362. lpVertex->sx = cx+R2;
  2363. lpVertex->sy = cy+R2;
  2364. lpVertex->color = RGBA2;
  2365. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2366. lpVertex->sx = cx;
  2367. lpVertex->sy = cy+R;
  2368. lpVertex->color = RGBA2;
  2369. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2370. //================ 4
  2371. lpVertex->sx = cx;
  2372. lpVertex->sy = cy;
  2373. lpVertex->color = RGBA;
  2374. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2375. lpVertex->sx = cx;
  2376. lpVertex->sy = cy+R;
  2377. lpVertex->color = RGBA2;
  2378. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2379. lpVertex->sx = cx-R2;
  2380. lpVertex->sy = cy+R2;
  2381. lpVertex->color = RGBA2;
  2382. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2383. //================ 5
  2384. lpVertex->sx = cx;
  2385. lpVertex->sy = cy;
  2386. lpVertex->color = RGBA;
  2387. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2388. lpVertex->sx = cx-R2;
  2389. lpVertex->sy = cy+R2;
  2390. lpVertex->color = RGBA2;
  2391. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2392. lpVertex->sx = cx-R;
  2393. lpVertex->sy = cy;
  2394. lpVertex->color = RGBA2;
  2395. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2396. //================ 6
  2397. lpVertex->sx = cx;
  2398. lpVertex->sy = cy;
  2399. lpVertex->color = RGBA;
  2400. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2401. lpVertex->sx = cx-R;
  2402. lpVertex->sy = cy;
  2403. lpVertex->color = RGBA2;
  2404. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2405. lpVertex->sx = cx-R2;
  2406. lpVertex->sy = cy-R2;
  2407. lpVertex->color = RGBA2;
  2408. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2409. //================ 7
  2410. lpVertex->sx = cx;
  2411. lpVertex->sy = cy;
  2412. lpVertex->color = RGBA;
  2413. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2414. lpVertex->sx = cx-R2;
  2415. lpVertex->sy = cy-R2;
  2416. lpVertex->color = RGBA2;
  2417. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2418. lpVertex->sx = cx;
  2419. lpVertex->sy = cy-R;
  2420. lpVertex->color = RGBA2;
  2421. lpVertex->specular = 0xFF000000; lpVertex->sz = sz; lpVertex->rhw = 1.f; lpVertex++;
  2422. //================ 8
  2423. }
  2424. void BuildTreeNoSort()
  2425. {
  2426. Vector2di v[3];
  2427. Current = -1;
  2428. int LastFace = -1;
  2429. TFace* fptr;
  2430. int sg;
  2431. for (int f=0; f<mptr->FCount; f++)
  2432. {
  2433. fptr = &mptr->gFace[f];
  2434. v[0] = gScrp[fptr->v1];
  2435. v[1] = gScrp[fptr->v2];
  2436. v[2] = gScrp[fptr->v3];
  2437. if (v[0].x == 0xFFFFFF) continue;
  2438. if (v[1].x == 0xFFFFFF) continue;
  2439. if (v[2].x == 0xFFFFFF) continue;
  2440. if (fptr->Flags & (sfDarkBack+sfNeedVC)) {
  2441. 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);
  2442. if (sg<0) continue;
  2443. }
  2444. fptr->Next=-1;
  2445. if (Current==-1) { Current=f; LastFace = f; } else
  2446. { mptr->gFace[LastFace].Next=f; LastFace=f; }
  2447. }
  2448. }
  2449. void BuildTreeNoSortf()
  2450. {
  2451. Vector2df v[3];
  2452. Current = -1;
  2453. int LastFace = -1;
  2454. TFace* fptr;
  2455. int sg;
  2456. for (int f=0; f<mptr->FCount; f++)
  2457. {
  2458. fptr = &mptr->gFace[f];
  2459. v[0] = gScrpf[fptr->v1];
  2460. v[1] = gScrpf[fptr->v2];
  2461. v[2] = gScrpf[fptr->v3];
  2462. if (v[0].x <-102300) continue;
  2463. if (v[1].x <-102300) continue;
  2464. if (v[2].x <-102300) continue;
  2465. if (fptr->Flags & (sfDarkBack+sfNeedVC)) {
  2466. 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);
  2467. if (sg<0) continue;
  2468. }
  2469. fptr->Next=-1;
  2470. if (Current==-1) { Current=f; LastFace = f; } else
  2471. { mptr->gFace[LastFace].Next=f; LastFace=f; }
  2472. }
  2473. }
  2474. int BuildTreeClipNoSort()
  2475. {
  2476. Current = -1;
  2477. int fc = 0;
  2478. int LastFace = -1;
  2479. TFace* fptr;
  2480. for (int f=0; f<mptr->FCount; f++)
  2481. {
  2482. fptr = &mptr->gFace[f];
  2483. if (fptr->Flags & (sfDarkBack + sfNeedVC) ) {
  2484. MulVectorsVect(SubVectors(rVertex[fptr->v2], rVertex[fptr->v1]), SubVectors(rVertex[fptr->v3], rVertex[fptr->v1]), nv);
  2485. if (nv.x*rVertex[fptr->v1].x + nv.y*rVertex[fptr->v1].y + nv.z*rVertex[fptr->v1].z<0) continue;
  2486. }
  2487. fc++;
  2488. fptr->Next=-1;
  2489. if (Current==-1) { Current=f; LastFace = f; } else
  2490. { mptr->gFace[LastFace].Next=f; LastFace=f; }
  2491. }
  2492. return fc;
  2493. }
  2494. void RenderBMPModel(TBMPModel* mptr, float x0, float y0, float z0, int light)
  2495. {
  2496. if (fabs(y0) > -(z0-256*6)) return;
  2497. int minx = 10241024;
  2498. int maxx =-10241024;
  2499. int miny = 10241024;
  2500. int maxy =-10241024;
  2501. BOOL FOGACTIVE = (FOGON && (FogYBase>0));
  2502. for (int s=0; s<4; s++) {
  2503. if (FOGACTIVE) {
  2504. vFogT[s] = 255-(int) (FogYBase + mptr->gVertex[s].y * FogYGrad);
  2505. if (vFogT[s]<0 ) vFogT[s] = 0;
  2506. if (vFogT[s]>255) vFogT[s]=255;
  2507. vFogT[s]<<=24;
  2508. } else vFogT[s]=255<<24;
  2509. rVertex[s].x = mptr->gVertex[s].x + x0;
  2510. rVertex[s].y = mptr->gVertex[s].y * cb + y0;
  2511. rVertex[s].z = mptr->gVertex[s].y * sb + z0;
  2512. if (rVertex[s].z<-256) {
  2513. gScrp[s].x = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  2514. gScrp[s].y = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH);
  2515. } else return;
  2516. if (gScrp[s].x > maxx) maxx = gScrp[s].x;
  2517. if (gScrp[s].x < minx) minx = gScrp[s].x;
  2518. if (gScrp[s].y > maxy) maxy = gScrp[s].y;
  2519. if (gScrp[s].y < miny) miny = gScrp[s].y;
  2520. }
  2521. if (minx == 10241024) return;
  2522. if (minx>WinW || maxx<0 || miny>WinH || maxy<0) return;
  2523. int argb = light * 0x00010101 + ((255-GlassL)<<24);
  2524. float d = (float) sqrt(x0*x0 + y0*y0 + z0*z0);
  2525. d3dSetTexture(mptr->lpTexture, 128, 128);
  2526. //d3dStartBuffer();
  2527. if (!lpVertexG)
  2528. d3dStartBufferGBMP();
  2529. if (GVCnt>380) {
  2530. if (lpVertexG) d3dEndBufferG(TRUE);
  2531. d3dStartBufferGBMP();
  2532. }
  2533. lpVertexG->sx = (float)gScrp[0].x;
  2534. lpVertexG->sy = (float)gScrp[0].y;
  2535. lpVertexG->sz = _ZSCALE / rVertex[0].z;
  2536. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  2537. lpVertexG->color = argb;
  2538. lpVertexG->specular = vFogT[0];
  2539. lpVertexG->tu = (float)(0.0f);
  2540. lpVertexG->tv = (float)(0.0f);
  2541. lpVertexG++;
  2542. lpVertexG->sx = (float)gScrp[1].x;
  2543. lpVertexG->sy = (float)gScrp[1].y;
  2544. lpVertexG->sz = _ZSCALE / rVertex[1].z;
  2545. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  2546. lpVertexG->color = argb;
  2547. lpVertexG->specular = vFogT[1];
  2548. lpVertexG->tu = (float)(0.995f);
  2549. lpVertexG->tv = (float)(0.0f);
  2550. lpVertexG++;
  2551. lpVertexG->sx = (float)gScrp[2].x;
  2552. lpVertexG->sy = (float)gScrp[2].y;
  2553. lpVertexG->sz = _ZSCALE / rVertex[2].z;
  2554. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  2555. lpVertexG->color = argb;
  2556. lpVertexG->specular = vFogT[2];
  2557. lpVertexG->tu = (float)(0.995f);
  2558. lpVertexG->tv = (float)(0.995f);
  2559. lpVertexG++;
  2560. //=========//
  2561. lpVertexG->sx = (float)gScrp[0].x;
  2562. lpVertexG->sy = (float)gScrp[0].y;
  2563. lpVertexG->sz = _ZSCALE / rVertex[0].z;
  2564. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  2565. lpVertexG->color = argb;
  2566. lpVertexG->specular = vFogT[0];
  2567. lpVertexG->tu = (float)(0.0f);
  2568. lpVertexG->tv = (float)(0.0f);
  2569. lpVertexG++;
  2570. lpVertexG->sx = (float)gScrp[2].x;
  2571. lpVertexG->sy = (float)gScrp[2].y;
  2572. lpVertexG->sz = _ZSCALE / rVertex[2].z;
  2573. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  2574. lpVertexG->color = argb;
  2575. lpVertexG->specular = vFogT[2];
  2576. lpVertexG->tu = (float)(0.995f);
  2577. lpVertexG->tv = (float)(0.995f);
  2578. lpVertexG++;
  2579. lpVertexG->sx = (float)gScrp[3].x;
  2580. lpVertexG->sy = (float)gScrp[3].y;
  2581. lpVertexG->sz = _ZSCALE / rVertex[3].z;
  2582. lpVertexG->rhw = lpVertexG->sz * _AZSCALE;
  2583. lpVertexG->color = argb;
  2584. lpVertexG->specular = vFogT[3];
  2585. lpVertexG->tu = (float)(0.0f);
  2586. lpVertexG->tv = (float)(0.995f);
  2587. lpVertexG++;
  2588. //d3dFlushBuffer(0, 2);
  2589. if (hGTexture!=hTexture) {
  2590. hGTexture=hTexture;
  2591. lpInstructionG->bOpcode = D3DOP_STATERENDER;
  2592. lpInstructionG->bSize = sizeof(D3DSTATE);
  2593. lpInstructionG->wCount = 1;
  2594. lpInstructionG++;
  2595. lpState = (LPD3DSTATE)lpInstructionG;
  2596. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREHANDLE;
  2597. lpState->dwArg[0] = hTexture;
  2598. lpState++;
  2599. lpInstructionG = (LPD3DINSTRUCTION)lpState;
  2600. lpwTriCount = (&lpInstructionG->wCount);
  2601. lpInstructionG->bOpcode = D3DOP_TRIANGLE;
  2602. lpInstructionG->bSize = sizeof(D3DTRIANGLE);
  2603. lpInstructionG->wCount = 0;
  2604. lpInstructionG++;
  2605. }
  2606. lpTriangle = (LPD3DTRIANGLE)lpInstructionG;
  2607. lpTriangle->wV1 = GVCnt;
  2608. lpTriangle->wV2 = GVCnt+1;
  2609. lpTriangle->wV3 = GVCnt+2;
  2610. lpTriangle->wFlags = 0;
  2611. lpTriangle++;
  2612. lpTriangle->wV1 = GVCnt+3;
  2613. lpTriangle->wV2 = GVCnt+4;
  2614. lpTriangle->wV3 = GVCnt+5;
  2615. lpTriangle->wFlags = 0;
  2616. lpTriangle++;
  2617. *lpwTriCount = (*lpwTriCount) + 2;
  2618. lpInstructionG = (LPD3DINSTRUCTION)lpTriangle;
  2619. GVCnt+=6;
  2620. }
  2621. void RenderModel(TModel* _mptr, float x0, float y0, float z0, int light, int VT, float al, float bt)
  2622. {
  2623. int f;
  2624. if (fabs(y0) > -(z0-256*6)) return;
  2625. mptr = _mptr;
  2626. float ca = (float)f_cos(al);
  2627. float sa = (float)f_sin(al);
  2628. float cb = (float)f_cos(bt);
  2629. float sb = (float)f_sin(bt);
  2630. float minx = 10241024;
  2631. float maxx =-10241024;
  2632. float miny = 10241024;
  2633. float maxy =-10241024;
  2634. BOOL FOGACTIVE = (FOGON && (FogYBase>0));
  2635. int alphamask = (255-GlassL)<<24;
  2636. int ml = light;
  2637. TPoint3d p;
  2638. for (int s=0; s<mptr->VCount; s++) {
  2639. p = mptr->gVertex[s];
  2640. if (FOGACTIVE) {
  2641. vFogT[s] = 255-(int)(FogYBase + p.y * FogYGrad);
  2642. if (vFogT[s]<5 ) vFogT[s] = 5;
  2643. if (vFogT[s]>255) vFogT[s]=255;
  2644. vFogT[s]<<=24;
  2645. } else vFogT[s] = 255<<24;
  2646. rVertex[s].x = (p.x * ca + p.z * sa) + x0;
  2647. float vz = p.z * ca - p.x * sa;
  2648. rVertex[s].y = (p.y * cb - vz * sb) + y0;
  2649. rVertex[s].z = (vz * cb + p.y * sb) + z0;
  2650. if (rVertex[s].z<-32) {
  2651. gScrpf[s].x = VideoCXf - (rVertex[s].x / rVertex[s].z * CameraW);
  2652. gScrpf[s].y = VideoCYf + (rVertex[s].y / rVertex[s].z * CameraH);
  2653. } else gScrpf[s].x=-102400;
  2654. if (gScrpf[s].x > maxx) maxx = gScrpf[s].x;
  2655. if (gScrpf[s].x < minx) minx = gScrpf[s].x;
  2656. if (gScrpf[s].y > maxy) maxy = gScrpf[s].y;
  2657. if (gScrpf[s].y < miny) miny = gScrpf[s].y;
  2658. }
  2659. if (minx == 10241024) return;
  2660. if (minx>WinW || maxx<0 || miny>WinH || maxy<0) return;
  2661. BuildTreeNoSortf();
  2662. float d = (float) sqrt(x0*x0 + y0*y0 + z0*z0);
  2663. if (LOWRESTX) d = 14*256;
  2664. if (MIPMAP && (d > 12*256)) d3dSetTexture(mptr->lpTexture2, 128, 128);
  2665. else d3dSetTexture(mptr->lpTexture, 256, 256);
  2666. int PrevOpacity = 0;
  2667. int NewOpacity = 0;
  2668. int PrevTransparent = 0;
  2669. int NewTransparent = 0;
  2670. d3dStartBuffer();
  2671. int fproc1 = 0;
  2672. int fproc2 = 0;
  2673. f = Current;
  2674. BOOL CKEY = FALSE;
  2675. while( f!=-1 ) {
  2676. TFace *fptr = & mptr->gFace[f];
  2677. f = mptr->gFace[f].Next;
  2678. if (minx<0)
  2679. if (gScrpf[fptr->v1].x <0 && gScrpf[fptr->v2].x<0 && gScrpf[fptr->v3].x<0) continue;
  2680. if (maxx>WinW)
  2681. if (gScrpf[fptr->v1].x >WinW && gScrpf[fptr->v2].x>WinW && gScrpf[fptr->v3].x>WinW) continue;
  2682. if (fptr->Flags & (sfOpacity | sfTransparent)) fproc2++; else fproc1++;
  2683. int _ml = ml + mptr->VLight[VT][fptr->v1];
  2684. lpVertex->sx = (float)gScrpf[fptr->v1].x;
  2685. lpVertex->sy = (float)gScrpf[fptr->v1].y;
  2686. lpVertex->sz = _ZSCALE / rVertex[fptr->v1].z;
  2687. lpVertex->rhw = 1.f;
  2688. lpVertex->color = _ml * 0x00010101 | alphamask;
  2689. lpVertex->specular = vFogT[fptr->v1];
  2690. lpVertex->tu = (float)(fptr->tax);
  2691. lpVertex->tv = (float)(fptr->tay);
  2692. lpVertex++;
  2693. _ml = ml + mptr->VLight[VT][fptr->v2];
  2694. lpVertex->sx = (float)gScrpf[fptr->v2].x;
  2695. lpVertex->sy = (float)gScrpf[fptr->v2].y;
  2696. lpVertex->sz = _ZSCALE / rVertex[fptr->v2].z;
  2697. lpVertex->rhw = 1.f;
  2698. lpVertex->color = _ml * 0x00010101 | alphamask;;
  2699. lpVertex->specular = vFogT[fptr->v2];
  2700. lpVertex->tu = (float)(fptr->tbx);
  2701. lpVertex->tv = (float)(fptr->tby);
  2702. lpVertex++;
  2703. _ml = ml + mptr->VLight[VT][fptr->v3];
  2704. lpVertex->sx = (float)gScrpf[fptr->v3].x;
  2705. lpVertex->sy = (float)gScrpf[fptr->v3].y;
  2706. lpVertex->sz = _ZSCALE / rVertex[fptr->v3].z;
  2707. lpVertex->rhw = 1.f;
  2708. lpVertex->color = _ml * 0x00010101 | alphamask;;
  2709. lpVertex->specular = vFogT[fptr->v3];
  2710. lpVertex->tu = (float)(fptr->tcx);
  2711. lpVertex->tv = (float)(fptr->tcy);
  2712. lpVertex++;
  2713. //f = mptr->gFace[f].Next;
  2714. }
  2715. d3dFlushBuffer(fproc1, fproc2);
  2716. }
  2717. void RenderShadowClip(TModel* _mptr,
  2718. float xm0, float ym0, float zm0,
  2719. float x0, float y0, float z0, float cal, float al, float bt)
  2720. {
  2721. int f,CMASK,j;
  2722. mptr = _mptr;
  2723. float cla = (float)f_cos(cal);
  2724. float sla = (float)f_sin(cal);
  2725. float ca = (float)f_cos(al);
  2726. float sa = (float)f_sin(al);
  2727. float cb = (float)f_cos(bt);
  2728. float sb = (float)f_sin(bt);
  2729. BOOL BL = FALSE;
  2730. for (int s=0; s<mptr->VCount; s++) {
  2731. float mrx = mptr->gVertex[s].x * cla + mptr->gVertex[s].z * sla;
  2732. float mrz = mptr->gVertex[s].z * cla - mptr->gVertex[s].x * sla;
  2733. float shx = mrx + mptr->gVertex[s].y * SunShadowK;
  2734. float shz = mrz + mptr->gVertex[s].y * SunShadowK;
  2735. float shy = GetLandH(shx + xm0, shz + zm0) - ym0;
  2736. rVertex[s].x = (shx * ca + shz * sa) + x0;
  2737. float vz = shz * ca - shx * sa;
  2738. rVertex[s].y = (shy * cb - vz * sb) + y0;
  2739. rVertex[s].z = (vz * cb + shy * sb) + z0;
  2740. if (rVertex[s].z<0) BL=TRUE;
  2741. if (rVertex[s].z>-256) { gScrp[s].x = 0xFFFFFF; gScrp[s].y = 0xFF; }
  2742. else {
  2743. int f = 0;
  2744. int sx = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  2745. int sy = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH);
  2746. if (sx>=WinEX) f+=1;
  2747. if (sx<=0 ) f+=2;
  2748. if (sy>=WinEY) f+=4;
  2749. if (sy<=0 ) f+=8;
  2750. gScrp[s].y = f;
  2751. }
  2752. }
  2753. if (!BL) return;
  2754. float d = (float) sqrt(x0*x0 + y0*y0 + z0*z0);
  2755. if (LOWRESTX) d = 14*256;
  2756. if (MIPMAP && (d > 12*256)) d3dSetTexture(mptr->lpTexture2, 128, 128);
  2757. else d3dSetTexture(mptr->lpTexture, 256, 256);
  2758. BuildTreeClipNoSort();
  2759. d3dStartBuffer();
  2760. int fproc1 = 0;
  2761. f = Current;
  2762. while( f!=-1 ) {
  2763. vused = 3;
  2764. TFace *fptr = &mptr->gFace[f];
  2765. CMASK = 0;
  2766. CMASK|=gScrp[fptr->v1].y;
  2767. CMASK|=gScrp[fptr->v2].y;
  2768. CMASK|=gScrp[fptr->v3].y;
  2769. cp[0].ev.v = rVertex[fptr->v1]; cp[0].tx = fptr->tax; cp[0].ty = fptr->tay;
  2770. cp[1].ev.v = rVertex[fptr->v2]; cp[1].tx = fptr->tbx; cp[1].ty = fptr->tby;
  2771. cp[2].ev.v = rVertex[fptr->v3]; cp[2].tx = fptr->tcx; cp[2].ty = fptr->tcy;
  2772. if (CMASK == 0xFF) {
  2773. for (u=0; u<vused; u++) cp[u].ev.v.z+=16.0f;
  2774. for (u=0; u<vused; u++) ClipVector(ClipZ,u);
  2775. for (u=0; u<vused; u++) cp[u].ev.v.z-=16.0f;
  2776. if (vused<3) goto LNEXT;
  2777. }
  2778. if (CMASK & 1) for (u=0; u<vused; u++) ClipVector(ClipA,u); if (vused<3) goto LNEXT;
  2779. if (CMASK & 2) for (u=0; u<vused; u++) ClipVector(ClipC,u); if (vused<3) goto LNEXT;
  2780. if (CMASK & 4) for (u=0; u<vused; u++) ClipVector(ClipB,u); if (vused<3) goto LNEXT;
  2781. if (CMASK & 8) for (u=0; u<vused; u++) ClipVector(ClipD,u); if (vused<3) goto LNEXT;
  2782. for (j=0; j<vused-2; j++) {
  2783. u = 0;
  2784. lpVertex->sx = (float)(VideoCX - (int)(cp[u].ev.v.x / cp[u].ev.v.z * CameraW));
  2785. lpVertex->sy = (float)(VideoCY + (int)(cp[u].ev.v.y / cp[u].ev.v.z * CameraH));
  2786. lpVertex->sz = (_ZSCALE-0.5f) / cp[u].ev.v.z;
  2787. lpVertex->rhw = 1.f;
  2788. lpVertex->color = GlassL;
  2789. lpVertex->specular = 0xFF000000;
  2790. lpVertex->tu = 0.f;
  2791. lpVertex->tv = 0.f;
  2792. lpVertex++;
  2793. u = j+1;
  2794. lpVertex->sx = (float)(VideoCX - (int)(cp[u].ev.v.x / cp[u].ev.v.z * CameraW));
  2795. lpVertex->sy = (float)(VideoCY + (int)(cp[u].ev.v.y / cp[u].ev.v.z * CameraH));
  2796. lpVertex->sz = (_ZSCALE-0.5f) / cp[u].ev.v.z;
  2797. lpVertex->rhw = 1.f;
  2798. lpVertex->color = GlassL;
  2799. lpVertex->specular = 0xFF000000;
  2800. lpVertex->tu = 0.f;
  2801. lpVertex->tv = 0.f;
  2802. lpVertex++;
  2803. u = j+2;
  2804. lpVertex->sx = (float)(VideoCX - (int)(cp[u].ev.v.x / cp[u].ev.v.z * CameraW));
  2805. lpVertex->sy = (float)(VideoCY + (int)(cp[u].ev.v.y / cp[u].ev.v.z * CameraH));
  2806. lpVertex->sz = (_ZSCALE-0.5f) / cp[u].ev.v.z;
  2807. lpVertex->rhw = 1.f;
  2808. lpVertex->color = GlassL;
  2809. lpVertex->specular = 0xFF000000;
  2810. lpVertex->tu = 0.f;
  2811. lpVertex->tv = 0.f;
  2812. lpVertex++;
  2813. fproc1++;
  2814. }
  2815. LNEXT:
  2816. f = mptr->gFace[f].Next;
  2817. }
  2818. d3dFlushBuffer(fproc1, 0);
  2819. }
  2820. void RenderModelClip(TModel* _mptr, float x0, float y0, float z0, int light, int VT, float al, float bt)
  2821. {
  2822. int f,CMASK;
  2823. //if (fabs(y0) > -(z0-256*6)) return;
  2824. mptr = _mptr;
  2825. float ca = (float)f_cos(al);
  2826. float sa = (float)f_sin(al);
  2827. float cb = (float)f_cos(bt);
  2828. float sb = (float)f_sin(bt);
  2829. int flight = (int)light;
  2830. DWORD almask;
  2831. DWORD alphamask = (255-GlassL)<<24;
  2832. BOOL BL = FALSE;
  2833. BOOL FOGACTIVE = (FOGON && (FogYBase>0));
  2834. for (int s=0; s<mptr->VCount; s++) {
  2835. if (FOGACTIVE) {
  2836. vFogT[s] = 255-(int)(FogYBase + mptr->gVertex[s].y * FogYGrad);
  2837. if (vFogT[s]<5 ) vFogT[s] = 5;
  2838. if (vFogT[s]>255) vFogT[s]=255;
  2839. } else vFogT[s] = 255;
  2840. rVertex[s].x = (mptr->gVertex[s].x * ca + mptr->gVertex[s].z * sa) /* * mdlScale */ + x0;
  2841. float vz = mptr->gVertex[s].z * ca - mptr->gVertex[s].x * sa;
  2842. rVertex[s].y = (mptr->gVertex[s].y * cb - vz * sb) /* * mdlScale */ + y0;
  2843. rVertex[s].z = (vz * cb + mptr->gVertex[s].y * sb) /* * mdlScale */ + z0;
  2844. if (rVertex[s].z<0) BL=TRUE;
  2845. if (rVertex[s].z>-256) { gScrp[s].x = 0xFFFFFF; gScrp[s].y = 0xFF; }
  2846. else {
  2847. int f = 0;
  2848. int sx = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  2849. int sy = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH);
  2850. if (sx>=WinEX) f+=1;
  2851. if (sx<=0 ) f+=2;
  2852. if (sy>=WinEY) f+=4;
  2853. if (sy<=0 ) f+=8;
  2854. gScrp[s].y = f;
  2855. }
  2856. }
  2857. if (!BL) return;
  2858. if (LOWRESTX) d3dSetTexture(mptr->lpTexture2, 128, 128);
  2859. else d3dSetTexture(mptr->lpTexture, 256, 256);
  2860. BuildTreeClipNoSort();
  2861. d3dStartBuffer();
  2862. f = Current;
  2863. int fproc1 = 0;
  2864. int fproc2 = 0;
  2865. BOOL CKEY = FALSE;
  2866. while( f!=-1 ) {
  2867. vused = 3;
  2868. TFace *fptr = &mptr->gFace[f];
  2869. CMASK = 0;
  2870. CMASK|=gScrp[fptr->v1].y;
  2871. CMASK|=gScrp[fptr->v2].y;
  2872. CMASK|=gScrp[fptr->v3].y;
  2873. cp[0].ev.v = rVertex[fptr->v1]; cp[0].tx = fptr->tax; cp[0].ty = fptr->tay; cp[0].ev.Fog = vFogT[fptr->v1]; cp[0].ev.Light = mptr->VLight[VT][fptr->v1];
  2874. cp[1].ev.v = rVertex[fptr->v2]; cp[1].tx = fptr->tbx; cp[1].ty = fptr->tby; cp[1].ev.Fog = vFogT[fptr->v2]; cp[1].ev.Light = mptr->VLight[VT][fptr->v2];
  2875. cp[2].ev.v = rVertex[fptr->v3]; cp[2].tx = fptr->tcx; cp[2].ty = fptr->tcy; cp[2].ev.Fog = vFogT[fptr->v3]; cp[2].ev.Light = mptr->VLight[VT][fptr->v3];
  2876. {
  2877. for (u=0; u<vused; u++) cp[u].ev.v.z+= 8.0f;
  2878. for (u=0; u<vused; u++) ClipVector(ClipZ,u);
  2879. for (u=0; u<vused; u++) cp[u].ev.v.z-= 8.0f;
  2880. if (vused<3) goto LNEXT;
  2881. }
  2882. if (CMASK & 1) for (u=0; u<vused; u++) ClipVector(ClipA,u); if (vused<3) goto LNEXT;
  2883. if (CMASK & 2) for (u=0; u<vused; u++) ClipVector(ClipC,u); if (vused<3) goto LNEXT;
  2884. if (CMASK & 4) for (u=0; u<vused; u++) ClipVector(ClipB,u); if (vused<3) goto LNEXT;
  2885. if (CMASK & 8) for (u=0; u<vused; u++) ClipVector(ClipD,u); if (vused<3) goto LNEXT;
  2886. almask = 0xFF000000;
  2887. if (fptr->Flags & sfTransparent) almask = 0x70000000;
  2888. if (almask > alphamask)
  2889. almask = alphamask;
  2890. for (u=0; u<vused-2; u++) {
  2891. int _flight = flight + cp[0].ev.Light;
  2892. lpVertex->sx = VideoCXf - cp[0].ev.v.x / cp[0].ev.v.z * CameraW;
  2893. lpVertex->sy = VideoCYf + cp[0].ev.v.y / cp[0].ev.v.z * CameraH;
  2894. lpVertex->sz = _ZSCALE / cp[0].ev.v.z;
  2895. lpVertex->rhw = lpVertex->sz * _AZSCALE;
  2896. lpVertex->color = _flight * 0x00010101 | almask;
  2897. lpVertex->specular = ((int)cp[0].ev.Fog)<<24;
  2898. lpVertex->tu = (float)(cp[0].tx);
  2899. lpVertex->tv = (float)(cp[0].ty);
  2900. lpVertex++;
  2901. _flight = flight + cp[u+1].ev.Light;
  2902. lpVertex->sx = VideoCXf - cp[u+1].ev.v.x / cp[u+1].ev.v.z * CameraW;
  2903. lpVertex->sy = VideoCYf + cp[u+1].ev.v.y / cp[u+1].ev.v.z * CameraH;
  2904. lpVertex->sz = _ZSCALE / cp[u+1].ev.v.z;
  2905. lpVertex->rhw = lpVertex->sz * _AZSCALE;
  2906. lpVertex->color = _flight * 0x00010101 | almask;
  2907. lpVertex->specular = ((int)cp[u+1].ev.Fog)<<24;
  2908. lpVertex->tu = (float)(cp[u+1].tx);
  2909. lpVertex->tv = (float)(cp[u+1].ty);
  2910. lpVertex++;
  2911. _flight = flight + cp[u+2].ev.Light;
  2912. lpVertex->sx = VideoCXf - cp[u+2].ev.v.x / cp[u+2].ev.v.z * CameraW;
  2913. lpVertex->sy = VideoCYf + cp[u+2].ev.v.y / cp[u+2].ev.v.z * CameraH;
  2914. lpVertex->sz = _ZSCALE / cp[u+2].ev.v.z;
  2915. lpVertex->rhw = lpVertex->sz * _AZSCALE;
  2916. lpVertex->color = _flight * 0x00010101 | almask;
  2917. lpVertex->specular = ((int)cp[u+2].ev.Fog)<<24;
  2918. lpVertex->tu = (float)(cp[u+2].tx);
  2919. lpVertex->tv = (float)(cp[u+2].ty);
  2920. lpVertex++;
  2921. if (fptr->Flags & (sfOpacity | sfTransparent)) fproc2++; else fproc1++;
  2922. }
  2923. LNEXT:
  2924. f = mptr->gFace[f].Next;
  2925. }
  2926. d3dFlushBuffer(fproc1, fproc2);
  2927. }
  2928. void RenderModelClipEnvMap(TModel* _mptr, float x0, float y0, float z0, float al, float bt)
  2929. {
  2930. int f,CMASK;
  2931. mptr = _mptr;
  2932. float ca = (float)f_cos(al);
  2933. float sa = (float)f_sin(al);
  2934. float cb = (float)f_cos(bt);
  2935. float sb = (float)f_sin(bt);
  2936. DWORD PHCOLOR = 0xFFFFFFFF;
  2937. BOOL BL = FALSE;
  2938. for (int s=0; s<mptr->VCount; s++) {
  2939. rVertex[s].x = (mptr->gVertex[s].x * ca + mptr->gVertex[s].z * sa) /* * mdlScale */ + x0;
  2940. float vz = mptr->gVertex[s].z * ca - mptr->gVertex[s].x * sa;
  2941. rVertex[s].y = (mptr->gVertex[s].y * cb - vz * sb) /* * mdlScale */ + y0;
  2942. rVertex[s].z = (vz * cb + mptr->gVertex[s].y * sb) /* * mdlScale */ + z0;
  2943. if (rVertex[s].z<0) BL=TRUE;
  2944. if (rVertex[s].z>-256) { gScrp[s].x = 0xFFFFFF; gScrp[s].y = 0xFF; }
  2945. else {
  2946. int f = 0;
  2947. int sx = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  2948. int sy = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH);
  2949. if (sx>=WinEX) f+=1;
  2950. if (sx<=0 ) f+=2;
  2951. if (sy>=WinEY) f+=4;
  2952. if (sy<=0 ) f+=8;
  2953. gScrp[s].y = f;
  2954. }
  2955. }
  2956. d3dSetTexture(TFX_ENVMAP.lpImage, TFX_ENVMAP.W, TFX_ENVMAP.W);
  2957. SetRenderStates(FALSE, D3DBLEND_ONE);
  2958. BuildTreeClipNoSort();
  2959. d3dStartBuffer();
  2960. f = Current;
  2961. int fproc1 = 0;
  2962. while( f!=-1 ) {
  2963. vused = 3;
  2964. TFace *fptr = &mptr->gFace[f];
  2965. if (!(fptr->Flags & sfEnvMap)) goto LNEXT;
  2966. CMASK = 0;
  2967. CMASK|=gScrp[fptr->v1].y;
  2968. CMASK|=gScrp[fptr->v2].y;
  2969. CMASK|=gScrp[fptr->v3].y;
  2970. cp[0].ev.v = rVertex[fptr->v1]; cp[0].tx = PhongMapping[fptr->v1].x/256.f; cp[0].ty = PhongMapping[fptr->v1].y/256.f;
  2971. cp[1].ev.v = rVertex[fptr->v2]; cp[1].tx = PhongMapping[fptr->v2].x/256.f; cp[1].ty = PhongMapping[fptr->v2].y/256.f;
  2972. cp[2].ev.v = rVertex[fptr->v3]; cp[2].tx = PhongMapping[fptr->v3].x/256.f; cp[2].ty = PhongMapping[fptr->v3].y/256.f;
  2973. {
  2974. for (u=0; u<vused; u++) cp[u].ev.v.z+= 8.0f;
  2975. for (u=0; u<vused; u++) ClipVector(ClipZ,u);
  2976. for (u=0; u<vused; u++) cp[u].ev.v.z-= 8.0f;
  2977. if (vused<3) goto LNEXT;
  2978. }
  2979. if (CMASK & 1) for (u=0; u<vused; u++) ClipVector(ClipA,u); if (vused<3) goto LNEXT;
  2980. if (CMASK & 2) for (u=0; u<vused; u++) ClipVector(ClipC,u); if (vused<3) goto LNEXT;
  2981. if (CMASK & 4) for (u=0; u<vused; u++) ClipVector(ClipB,u); if (vused<3) goto LNEXT;
  2982. if (CMASK & 8) for (u=0; u<vused; u++) ClipVector(ClipD,u); if (vused<3) goto LNEXT;
  2983. for (u=0; u<vused-2; u++) {
  2984. lpVertex->sx = VideoCXf - cp[0].ev.v.x / cp[0].ev.v.z * CameraW;
  2985. lpVertex->sy = VideoCYf + cp[0].ev.v.y / cp[0].ev.v.z * CameraH;
  2986. lpVertex->sz = _ZSCALE / cp[0].ev.v.z;
  2987. lpVertex->rhw = lpVertex->sz * _AZSCALE;
  2988. lpVertex->color = PHCOLOR;
  2989. lpVertex->specular = 0xFF000000;
  2990. lpVertex->tu = (float)(cp[0].tx);
  2991. lpVertex->tv = (float)(cp[0].ty);
  2992. lpVertex++;
  2993. lpVertex->sx = VideoCXf - cp[u+1].ev.v.x / cp[u+1].ev.v.z * CameraW;
  2994. lpVertex->sy = VideoCYf + cp[u+1].ev.v.y / cp[u+1].ev.v.z * CameraH;
  2995. lpVertex->sz = _ZSCALE / cp[u+1].ev.v.z;
  2996. lpVertex->rhw = lpVertex->sz * _AZSCALE;
  2997. lpVertex->color = PHCOLOR;
  2998. lpVertex->specular = 0xFF000000;
  2999. lpVertex->tu = (float)(cp[u+1].tx);
  3000. lpVertex->tv = (float)(cp[u+1].ty);
  3001. lpVertex++;
  3002. lpVertex->sx = VideoCXf - cp[u+2].ev.v.x / cp[u+2].ev.v.z * CameraW;
  3003. lpVertex->sy = VideoCYf + cp[u+2].ev.v.y / cp[u+2].ev.v.z * CameraH;
  3004. lpVertex->sz = _ZSCALE / cp[u+2].ev.v.z;
  3005. lpVertex->rhw = lpVertex->sz * _AZSCALE;
  3006. lpVertex->color = PHCOLOR;
  3007. lpVertex->specular = 0xFF000000;
  3008. lpVertex->tu = (float)(cp[u+2].tx);
  3009. lpVertex->tv = (float)(cp[u+2].ty);
  3010. lpVertex++;
  3011. fproc1++;
  3012. }
  3013. LNEXT:
  3014. f = mptr->gFace[f].Next;
  3015. }
  3016. d3dFlushBuffer(fproc1, 0);
  3017. SetRenderStates(TRUE, D3DBLEND_INVSRCALPHA);
  3018. }
  3019. void RenderModelClipPhongMap(TModel* _mptr, float x0, float y0, float z0, float al, float bt)
  3020. {
  3021. int f,CMASK;
  3022. mptr = _mptr;
  3023. float ca = (float)f_cos(al);
  3024. float sa = (float)f_sin(al);
  3025. float cb = (float)f_cos(bt);
  3026. float sb = (float)f_sin(bt);
  3027. int rv = SkyR +64; if (rv>255) rv = 255;
  3028. int gv = SkyG +64; if (gv>255) gv = 255;
  3029. int bv = SkyB +64; if (bv>255) bv = 255;
  3030. DWORD PHCOLOR = 0xFF000000 + (rv<<16) + (gv<<8) + bv;
  3031. BOOL BL = FALSE;
  3032. for (int s=0; s<mptr->VCount; s++) {
  3033. rVertex[s].x = (mptr->gVertex[s].x * ca + mptr->gVertex[s].z * sa) /* * mdlScale */ + x0;
  3034. float vz = mptr->gVertex[s].z * ca - mptr->gVertex[s].x * sa;
  3035. rVertex[s].y = (mptr->gVertex[s].y * cb - vz * sb) /* * mdlScale */ + y0;
  3036. rVertex[s].z = (vz * cb + mptr->gVertex[s].y * sb) /* * mdlScale */ + z0;
  3037. if (rVertex[s].z<0) BL=TRUE;
  3038. if (rVertex[s].z>-256) { gScrp[s].x = 0xFFFFFF; gScrp[s].y = 0xFF; }
  3039. else {
  3040. int f = 0;
  3041. int sx = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  3042. int sy = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH);
  3043. if (sx>=WinEX) f+=1;
  3044. if (sx<=0 ) f+=2;
  3045. if (sy>=WinEY) f+=4;
  3046. if (sy<=0 ) f+=8;
  3047. gScrp[s].y = f;
  3048. }
  3049. }
  3050. d3dSetTexture(TFX_SPECULAR.lpImage, TFX_SPECULAR.W, TFX_SPECULAR.W);
  3051. SetRenderStates(FALSE, D3DBLEND_ONE);
  3052. BuildTreeClipNoSort();
  3053. d3dStartBuffer();
  3054. f = Current;
  3055. int fproc1 = 0;
  3056. while( f!=-1 ) {
  3057. vused = 3;
  3058. TFace *fptr = &mptr->gFace[f];
  3059. if (!(fptr->Flags & sfPhong)) goto LNEXT;
  3060. CMASK = 0;
  3061. CMASK|=gScrp[fptr->v1].y;
  3062. CMASK|=gScrp[fptr->v2].y;
  3063. CMASK|=gScrp[fptr->v3].y;
  3064. cp[0].ev.v = rVertex[fptr->v1]; cp[0].tx = PhongMapping[fptr->v1].x/256.f; cp[0].ty = PhongMapping[fptr->v1].y/256.f;
  3065. cp[1].ev.v = rVertex[fptr->v2]; cp[1].tx = PhongMapping[fptr->v2].x/256.f; cp[1].ty = PhongMapping[fptr->v2].y/256.f;
  3066. cp[2].ev.v = rVertex[fptr->v3]; cp[2].tx = PhongMapping[fptr->v3].x/256.f; cp[2].ty = PhongMapping[fptr->v3].y/256.f;
  3067. {
  3068. for (u=0; u<vused; u++) cp[u].ev.v.z+= 8.0f;
  3069. for (u=0; u<vused; u++) ClipVector(ClipZ,u);
  3070. for (u=0; u<vused; u++) cp[u].ev.v.z-= 8.0f;
  3071. if (vused<3) goto LNEXT;
  3072. }
  3073. if (CMASK & 1) for (u=0; u<vused; u++) ClipVector(ClipA,u); if (vused<3) goto LNEXT;
  3074. if (CMASK & 2) for (u=0; u<vused; u++) ClipVector(ClipC,u); if (vused<3) goto LNEXT;
  3075. if (CMASK & 4) for (u=0; u<vused; u++) ClipVector(ClipB,u); if (vused<3) goto LNEXT;
  3076. if (CMASK & 8) for (u=0; u<vused; u++) ClipVector(ClipD,u); if (vused<3) goto LNEXT;
  3077. for (u=0; u<vused-2; u++) {
  3078. lpVertex->sx = VideoCXf - cp[0].ev.v.x / cp[0].ev.v.z * CameraW;
  3079. lpVertex->sy = VideoCYf + cp[0].ev.v.y / cp[0].ev.v.z * CameraH;
  3080. lpVertex->sz = _ZSCALE / cp[0].ev.v.z;
  3081. lpVertex->rhw = lpVertex->sz * _AZSCALE;
  3082. lpVertex->color = PHCOLOR;
  3083. lpVertex->specular = 0xFF000000;
  3084. lpVertex->tu = (float)(cp[0].tx);
  3085. lpVertex->tv = (float)(cp[0].ty);
  3086. lpVertex++;
  3087. lpVertex->sx = VideoCXf - cp[u+1].ev.v.x / cp[u+1].ev.v.z * CameraW;
  3088. lpVertex->sy = VideoCYf + cp[u+1].ev.v.y / cp[u+1].ev.v.z * CameraH;
  3089. lpVertex->sz = _ZSCALE / cp[u+1].ev.v.z;
  3090. lpVertex->rhw = lpVertex->sz * _AZSCALE;
  3091. lpVertex->color = PHCOLOR;
  3092. lpVertex->specular = 0xFF000000;
  3093. lpVertex->tu = (float)(cp[u+1].tx);
  3094. lpVertex->tv = (float)(cp[u+1].ty);
  3095. lpVertex++;
  3096. lpVertex->sx = VideoCX - cp[u+2].ev.v.x / cp[u+2].ev.v.z * CameraW;
  3097. lpVertex->sy = VideoCY + cp[u+2].ev.v.y / cp[u+2].ev.v.z * CameraH;
  3098. lpVertex->sz = _ZSCALE / cp[u+2].ev.v.z;
  3099. lpVertex->rhw = lpVertex->sz * _AZSCALE;
  3100. lpVertex->color = PHCOLOR;
  3101. lpVertex->specular = 0xFF000000;
  3102. lpVertex->tu = (float)(cp[u+2].tx);
  3103. lpVertex->tv = (float)(cp[u+2].ty);
  3104. lpVertex++;
  3105. fproc1++;
  3106. }
  3107. LNEXT:
  3108. f = mptr->gFace[f].Next;
  3109. }
  3110. d3dFlushBuffer(fproc1, 0);
  3111. SetRenderStates(TRUE, D3DBLEND_INVSRCALPHA);
  3112. }
  3113. void RenderModelSun(TModel* _mptr, float x0, float y0, float z0, int Alpha)
  3114. {
  3115. int f;
  3116. mptr = _mptr;
  3117. int minx = 10241024;
  3118. int maxx =-10241024;
  3119. int miny = 10241024;
  3120. int maxy =-10241024;
  3121. for (int s=0; s<mptr->VCount; s++) {
  3122. rVertex[s].x = mptr->gVertex[s].x + x0;
  3123. rVertex[s].y = mptr->gVertex[s].y + y0;
  3124. rVertex[s].z = mptr->gVertex[s].z + z0;
  3125. if (rVertex[s].z>-64) gScrp[s].x = 0xFFFFFF; else {
  3126. gScrp[s].x = VideoCX + (int)(rVertex[s].x / (-rVertex[s].z) * CameraW);
  3127. gScrp[s].y = VideoCY - (int)(rVertex[s].y / (-rVertex[s].z) * CameraH); }
  3128. if (gScrp[s].x > maxx) maxx = gScrp[s].x;
  3129. if (gScrp[s].x < minx) minx = gScrp[s].x;
  3130. if (gScrp[s].y > maxy) maxy = gScrp[s].y;
  3131. if (gScrp[s].y < miny) miny = gScrp[s].y;
  3132. }
  3133. if (minx == 10241024) return;
  3134. if (minx>WinW || maxx<0 || miny>WinH || maxy<0) return;
  3135. BuildTreeNoSort();
  3136. d3dSetTexture(mptr->lpTexture2, 128, 128);
  3137. d3dStartBuffer();
  3138. DWORD alpha = Alpha;
  3139. alpha = (alpha<<24) | 0x00FFFFFF;
  3140. int fproc1 = 0;
  3141. f = Current;
  3142. while( f!=-1 ) {
  3143. TFace *fptr = & mptr->gFace[f];
  3144. fproc1++;
  3145. lpVertex->sx = (float)gScrp[fptr->v1].x;
  3146. lpVertex->sy = (float)gScrp[fptr->v1].y;
  3147. lpVertex->sz = 0.0002;//_ZSCALE / rVertex[fptr->v1].z;
  3148. lpVertex->rhw = 1.f;
  3149. lpVertex->color = alpha;
  3150. lpVertex->specular = 0xFF000000;
  3151. lpVertex->tu = (float)(fptr->tax);
  3152. lpVertex->tv = (float)(fptr->tay);
  3153. lpVertex++;
  3154. lpVertex->sx = (float)gScrp[fptr->v2].x;
  3155. lpVertex->sy = (float)gScrp[fptr->v2].y;
  3156. lpVertex->sz = 0.0002;//_ZSCALE / rVertex[fptr->v2].z;
  3157. lpVertex->rhw = 1.f;
  3158. lpVertex->color = alpha;
  3159. lpVertex->specular = 0xFF000000;
  3160. lpVertex->tu = (float)(fptr->tbx);
  3161. lpVertex->tv = (float)(fptr->tby);
  3162. lpVertex++;
  3163. lpVertex->sx = (float)gScrp[fptr->v3].x;
  3164. lpVertex->sy = (float)gScrp[fptr->v3].y;
  3165. lpVertex->sz = 0.0002;//_ZSCALE / rVertex[fptr->v3].z;
  3166. lpVertex->rhw = 1.f;
  3167. lpVertex->color = alpha;
  3168. lpVertex->specular = 0xFF000000;
  3169. lpVertex->tu = (float)(fptr->tcx);
  3170. lpVertex->tv = (float)(fptr->tcy);
  3171. lpVertex++;
  3172. f = mptr->gFace[f].Next;
  3173. }
  3174. lpInstruction = (LPD3DINSTRUCTION) ((LPD3DTLVERTEX)d3dExeBufDesc.lpData + d3debvc*3);
  3175. lpInstruction->bOpcode = D3DOP_STATERENDER;
  3176. lpInstruction->bSize = sizeof(D3DSTATE);
  3177. lpInstruction->wCount = 2;
  3178. lpInstruction++;
  3179. lpState = (LPD3DSTATE)lpInstruction;
  3180. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREHANDLE;
  3181. lpState->dwArg[0] = hTexture;
  3182. lpState++;
  3183. lpState->drstRenderStateType = D3DRENDERSTATE_DESTBLEND;
  3184. lpState->dwArg[0] = D3DBLEND_ONE;
  3185. lpState++;
  3186. lpInstruction = (LPD3DINSTRUCTION)lpState;
  3187. lpInstruction->bOpcode = D3DOP_PROCESSVERTICES;
  3188. lpInstruction->bSize = sizeof(D3DPROCESSVERTICES);
  3189. lpInstruction->wCount = 1U;
  3190. lpInstruction++;
  3191. lpProcessVertices = (LPD3DPROCESSVERTICES)lpInstruction;
  3192. lpProcessVertices->dwFlags = D3DPROCESSVERTICES_COPY;
  3193. lpProcessVertices->wStart = 0U;
  3194. lpProcessVertices->wDest = 0U;
  3195. lpProcessVertices->dwCount = fproc1*3;
  3196. lpProcessVertices->dwReserved = 0UL;
  3197. lpProcessVertices++;
  3198. lpInstruction = (LPD3DINSTRUCTION)lpProcessVertices;
  3199. lpInstruction->bOpcode = D3DOP_TRIANGLE;
  3200. lpInstruction->bSize = sizeof(D3DTRIANGLE);
  3201. lpInstruction->wCount = fproc1;
  3202. lpInstruction++;
  3203. lpTriangle = (LPD3DTRIANGLE)lpInstruction;
  3204. int ii = 0;
  3205. for (int i=0; i<fproc1; i++) {
  3206. lpTriangle->wV1 = ii++;
  3207. lpTriangle->wV2 = ii++;
  3208. lpTriangle->wV3 = ii++;
  3209. lpTriangle->wFlags = 0;
  3210. lpTriangle++;
  3211. }
  3212. lpInstruction = (LPD3DINSTRUCTION)lpTriangle;
  3213. lpInstruction->bOpcode = D3DOP_STATERENDER;
  3214. lpInstruction->bSize = sizeof(D3DSTATE);
  3215. lpInstruction->wCount = 1;
  3216. lpInstruction++;
  3217. lpState = (LPD3DSTATE)lpInstruction;
  3218. lpState->drstRenderStateType = D3DRENDERSTATE_DESTBLEND;
  3219. lpState->dwArg[0] = D3DBLEND_INVSRCALPHA;
  3220. lpState++;
  3221. lpInstruction = (LPD3DINSTRUCTION)lpState;
  3222. lpInstruction->bOpcode = D3DOP_STATERENDER;
  3223. lpInstruction->bSize = sizeof(D3DSTATE);
  3224. lpInstruction->wCount = 3;
  3225. lpInstruction++;
  3226. lpState = (LPD3DSTATE)lpInstruction;
  3227. lpState->drstRenderStateType = D3DRENDERSTATE_COLORKEYENABLE;
  3228. lpState->dwArg[0] = FALSE;
  3229. lpState++;
  3230. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMAG;
  3231. lpState->dwArg[0] = D3DFILTER_LINEAR;
  3232. lpState++;
  3233. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMIN;
  3234. lpState->dwArg[0] = D3DFILTER_LINEAR;
  3235. lpState++;
  3236. lpInstruction = (LPD3DINSTRUCTION)lpState;
  3237. lpInstruction->bOpcode = D3DOP_EXIT;
  3238. lpInstruction->bSize = 0UL;
  3239. lpInstruction->wCount = 0U;
  3240. lpd3dExecuteBuffer->Unlock( );
  3241. hRes = lpd3dDevice->Execute(lpd3dExecuteBuffer, lpd3dViewport, D3DEXECUTE_UNCLIPPED);
  3242. //if (FAILED(hRes)) DoHalt("Error execute buffer");
  3243. dFacesCount+=fproc1;
  3244. }
  3245. void RenderNearModel(TModel* _mptr, float x0, float y0, float z0, int light, float al, float bt)
  3246. {
  3247. BOOL b = LOWRESTX;
  3248. Vector3d v;
  3249. v.x = 0; v.y =-128; v.z = 0;
  3250. CalcFogLevel_Gradient(v);
  3251. FogYGrad = 0;
  3252. LOWRESTX = FALSE;
  3253. RenderModelClip(_mptr, x0, y0, z0, light, 0, al, bt);
  3254. LOWRESTX = b;
  3255. }
  3256. void RenderModelClipWater(TModel* _mptr, float x0, float y0, float z0, int light, int VT, float al, float bt)
  3257. {
  3258. }
  3259. void RenderCharacter(int index)
  3260. {
  3261. }
  3262. void RenderExplosion(int index)
  3263. {
  3264. }
  3265. void RenderShip()
  3266. {
  3267. }
  3268. void RenderElements()
  3269. {
  3270. d3dLastTexture = d3dmemmapsize+1;
  3271. hTexture = NULL;
  3272. int fproc1 = 0;
  3273. for (int eg = 0; eg<ElCount; eg++) {
  3274. for (int e = 0; e<Elements[eg].ECount; e++) {
  3275. Vector3d rpos;
  3276. TElement *el = &Elements[eg].EList[e];
  3277. rpos.x = el->pos.x - CameraX;
  3278. rpos.y = el->pos.y - CameraY;
  3279. rpos.z = el->pos.z - CameraZ;
  3280. float r = el->R;
  3281. rpos = RotateVector(rpos);
  3282. if (rpos.z > -64) continue;
  3283. if ( fabs(rpos.x) > -rpos.z) continue;
  3284. if ( fabs(rpos.y) > -rpos.z) continue;
  3285. if (!fproc1) d3dStartBuffer();
  3286. float sx = VideoCXf - (CameraW * rpos.x / rpos.z);
  3287. float sy = VideoCYf + (CameraH * rpos.y / rpos.z);
  3288. RenderCircle(sx, sy, rpos.z, -r*CameraW*0.64 / rpos.z, Elements[eg].RGBA, Elements[eg].RGBA2);
  3289. fproc1+=8;
  3290. if (fproc1>256) {
  3291. d3dFlushBuffer(fproc1, 0);
  3292. fproc1 = 0;
  3293. }
  3294. }
  3295. }
  3296. if (fproc1) d3dFlushBuffer(fproc1, 0);
  3297. fproc1 = 0;
  3298. for (int b=0; b<BloodTrail.Count; b++) {
  3299. Vector3d rpos = BloodTrail.Trail[b].pos;
  3300. DWORD A1 = (0xE0 * BloodTrail.Trail[b].LTime / 20000); if (A1>0xE0) A1=0xE0;
  3301. DWORD A2 = (0x20 * BloodTrail.Trail[b].LTime / 20000); if (A2>0x20) A2=0x20;
  3302. rpos.x = rpos.x - CameraX;
  3303. rpos.y = rpos.y - CameraY;
  3304. rpos.z = rpos.z - CameraZ;
  3305. rpos = RotateVector(rpos);
  3306. if (rpos.z > -64) continue;
  3307. if ( fabs(rpos.x) > -rpos.z) continue;
  3308. if ( fabs(rpos.y) > -rpos.z) continue;
  3309. if (!fproc1) d3dStartBuffer();
  3310. float sx = VideoCXf - (CameraW * rpos.x / rpos.z);
  3311. float sy = VideoCYf + (CameraH * rpos.y / rpos.z);
  3312. RenderCircle(sx, sy, rpos.z, -12*CameraW*0.64 / rpos.z, (A1<<24)+conv_xGx(0x700000), (A2<<24)+conv_xGx(0x300000));
  3313. fproc1+=8;
  3314. if (fproc1>256) {
  3315. d3dFlushBuffer(fproc1, 0);
  3316. fproc1 = 0;
  3317. }
  3318. }
  3319. if (fproc1) d3dFlushBuffer(fproc1, 0);
  3320. if (SNOW)
  3321. for (int s=0; s<SnCount; s++) {
  3322. Vector3d rpos = Snow[s].pos;
  3323. rpos.x = rpos.x - CameraX;
  3324. rpos.y = rpos.y - CameraY;
  3325. rpos.z = rpos.z - CameraZ;
  3326. rpos = RotateVector(rpos);
  3327. if (rpos.z > -64) continue;
  3328. if ( fabs(rpos.x) > -rpos.z) continue;
  3329. if ( fabs(rpos.y) > -rpos.z) continue;
  3330. float sx = VideoCXf - CameraW * rpos.x / rpos.z;
  3331. float sy = VideoCYf + CameraH * rpos.y / rpos.z;
  3332. DWORD A1 = 0xFF;
  3333. DWORD A2 = 0x30;
  3334. if (Snow[s].ftime) {
  3335. A1 = A1 * (2000-Snow[s].ftime) / 2000;
  3336. A2 = A2 * (2000-Snow[s].ftime) / 2000;
  3337. }
  3338. if (!fproc1) d3dStartBuffer();
  3339. if (rpos.z>-1624) {
  3340. RenderCircle(sx, sy, rpos.z, -8*CameraW*0.64 / rpos.z, (A1<<24)+conv_xGx(0xF0F0F0), (A2<<24)+conv_xGx(0xB0B0B0));
  3341. fproc1+=8;
  3342. } else {
  3343. RenderPoint(sx, sy, rpos.z, -8*CameraW*0.64 / rpos.z, (A1<<24)+conv_xGx(0xF0F0F0), (A2<<24)+conv_xGx(0xB0B0B0));
  3344. fproc1++;
  3345. }
  3346. if (fproc1>d3debvc) {
  3347. d3dFlushBuffer(fproc1, 0);
  3348. fproc1 = 0;
  3349. }
  3350. }
  3351. if (fproc1) d3dFlushBuffer(fproc1, 0);
  3352. GlassL = 0;
  3353. }
  3354. void RenderCharacterPost(TCharacter *cptr)
  3355. {
  3356. //mdlScale = 1.0f;
  3357. CreateChMorphedModel(cptr);
  3358. float zs = (float)sqrt( cptr->rpos.x*cptr->rpos.x + cptr->rpos.y*cptr->rpos.y + cptr->rpos.z*cptr->rpos.z);
  3359. if (zs > ctViewR*256) return;
  3360. GlassL = 0;
  3361. if (zs > 256 * (ctViewR-4))
  3362. GlassL = min(255, (zs/4 - 64*(ctViewR-4)));
  3363. waterclip = FALSE;
  3364. if ( cptr->rpos.z >-256*10)
  3365. RenderModelClip(cptr->pinfo->mptr,
  3366. cptr->rpos.x, cptr->rpos.y, cptr->rpos.z, 210, 0,
  3367. -cptr->alpha + pi / 2 + CameraAlpha,
  3368. CameraBeta );
  3369. else
  3370. RenderModel(cptr->pinfo->mptr,
  3371. cptr->rpos.x, cptr->rpos.y, cptr->rpos.z, 210, 0,
  3372. -cptr->alpha + pi / 2 + CameraAlpha,
  3373. CameraBeta );
  3374. if (!SHADOWS3D) return;
  3375. if (zs > 256 * (ctViewR-8)) return;
  3376. int Al = 0x60;
  3377. if (cptr->Health==0) {
  3378. int at = cptr->pinfo->Animation[cptr->Phase].AniTime;
  3379. if (Tranq) return;
  3380. if (cptr->FTime==at-1) return;
  3381. Al = Al * (at-cptr->FTime) / at; }
  3382. if (cptr->AI==0) Al = 0x50;
  3383. GlassL = (Al<<24) + 0x00222222;
  3384. RenderShadowClip(cptr->pinfo->mptr,
  3385. cptr->pos.x, cptr->pos.y, cptr->pos.z,
  3386. cptr->rpos.x, cptr->rpos.y, cptr->rpos.z,
  3387. pi/2-cptr->alpha,
  3388. CameraAlpha,
  3389. CameraBeta );
  3390. }
  3391. void RenderShipPost()
  3392. {
  3393. if (Ship.State==-1) return;
  3394. GlassL = 0;
  3395. zs = (int)VectorLength(Ship.rpos);
  3396. if (zs > 256 * (ctViewR)) return;
  3397. if (zs > 256 * (ctViewR-4))
  3398. GlassL = min(255,(int)(zs - 256 * (ctViewR-4)) / 4);
  3399. /*grConstantColorValue( (255-GlassL) << 24);*/
  3400. CreateMorphedModel(ShipModel.mptr, &ShipModel.Animation[0], Ship.FTime, 1.0);
  3401. if ( fabs(Ship.rpos.z) < 4000)
  3402. RenderModelClip(ShipModel.mptr,
  3403. Ship.rpos.x, Ship.rpos.y, Ship.rpos.z, 210, 0, -Ship.alpha -pi/2 + CameraAlpha, CameraBeta);
  3404. else
  3405. RenderModel(ShipModel.mptr,
  3406. Ship.rpos.x, Ship.rpos.y, Ship.rpos.z, 210, 0, -Ship.alpha -pi/2+ CameraAlpha, CameraBeta);
  3407. /*grConstantColorValue( 0xFF000000);*/
  3408. }
  3409. void Render3DHardwarePosts()
  3410. {
  3411. TCharacter *cptr;
  3412. for (int c=0; c<ChCount; c++) {
  3413. cptr = &Characters[c];
  3414. cptr->rpos.x = cptr->pos.x - CameraX;
  3415. cptr->rpos.y = cptr->pos.y - CameraY;
  3416. cptr->rpos.z = cptr->pos.z - CameraZ;
  3417. float r = (float)max( fabs(cptr->rpos.x), fabs(cptr->rpos.z) );
  3418. int ri = -1 + (int)(r / 256.f + 0.5f);
  3419. if (ri < 0) ri = 0;
  3420. if (ri > ctViewR) continue;
  3421. if (FOGON)
  3422. CalcFogLevel_Gradient(cptr->rpos);
  3423. cptr->rpos = RotateVector(cptr->rpos);
  3424. float br = BackViewR + DinoInfo[cptr->CType].Radius;
  3425. if (cptr->rpos.z > br) continue;
  3426. if ( fabs(cptr->rpos.x) > -cptr->rpos.z + br ) continue;
  3427. if ( fabs(cptr->rpos.y) > -cptr->rpos.z + br ) continue;
  3428. RenderCharacterPost(cptr);
  3429. }
  3430. Ship.rpos.x = Ship.pos.x - CameraX;
  3431. Ship.rpos.y = Ship.pos.y - CameraY;
  3432. Ship.rpos.z = Ship.pos.z - CameraZ;
  3433. float r = (float)max( fabs(Ship.rpos.x), fabs(Ship.rpos.z) );
  3434. int ri = -1 + (int)(r / 256.f + 0.2f);
  3435. if (ri < 0) ri = 0;
  3436. if (ri < ctViewR) {
  3437. if (FOGON)
  3438. CalcFogLevel_Gradient(Ship.rpos);
  3439. Ship.rpos = RotateVector(Ship.rpos);
  3440. if (Ship.rpos.z > BackViewR) goto NOSHIP;
  3441. if ( fabs(Ship.rpos.x) > -Ship.rpos.z + BackViewR ) goto NOSHIP;
  3442. RenderShipPost();
  3443. }
  3444. NOSHIP: ;
  3445. SunLight *= GetTraceK(SunScrX, SunScrY);
  3446. }
  3447. void ClearVideoBuf()
  3448. {
  3449. }
  3450. int CircleCX, CircleCY;
  3451. WORD CColor;
  3452. void PutPixel(int x, int y)
  3453. { *((WORD*)ddsd.lpSurface + y*lsw + x) = CColor; }
  3454. void Put8pix(int X,int Y)
  3455. {
  3456. PutPixel(CircleCX + X, CircleCY + Y);
  3457. PutPixel(CircleCX + X, CircleCY - Y);
  3458. PutPixel(CircleCX - X, CircleCY + Y);
  3459. PutPixel(CircleCX - X, CircleCY - Y);
  3460. PutPixel(CircleCX + Y, CircleCY + X);
  3461. PutPixel(CircleCX + Y, CircleCY - X);
  3462. PutPixel(CircleCX - Y, CircleCY + X);
  3463. PutPixel(CircleCX - Y, CircleCY - X);
  3464. }
  3465. void DrawCircle(int cx, int cy, int R)
  3466. {
  3467. int d = 3 - (2 * R);
  3468. int x = 0;
  3469. int y = R;
  3470. CircleCX=cx;
  3471. CircleCY=cy;
  3472. do {
  3473. Put8pix(x,y); x++;
  3474. if (d < 0) d = d + (x<<2) + 6; else
  3475. { d = d + (x - y) * 4 + 10; y--; }
  3476. } while (x<y);
  3477. Put8pix(x,y);
  3478. }
  3479. void DrawBox( WORD *lfbPtr, int lsw, int xx, int yy, WORD c)
  3480. {
  3481. yy = yy * lsw + xx;
  3482. *(lfbPtr + yy) = c;
  3483. *(lfbPtr + yy + 1) = c;
  3484. yy+=lsw;
  3485. *(lfbPtr + yy) = c;
  3486. *(lfbPtr + yy + 1) = c;
  3487. }
  3488. void DrawHMap()
  3489. {
  3490. int c;
  3491. DrawPicture(VideoCX-MapPic.W/2, VideoCY - MapPic.H/2-6, MapPic);
  3492. ZeroMemory( &ddsd, sizeof(DDSURFACEDESC) );
  3493. ddsd.dwSize = sizeof(DDSURFACEDESC);
  3494. if( lpddBack->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) != DD_OK ) return;
  3495. lsw = ddsd.lPitch / 2;
  3496. int RShift, GShift;
  3497. if (VMFORMAT565) {
  3498. RShift=11; GShift=6;
  3499. } else {
  3500. RShift=10; GShift=5;
  3501. }
  3502. int xx = VideoCX - 128 + (CCX>>2);
  3503. int yy = VideoCY - 128 + (CCY>>2);
  3504. if (yy<0 || yy>=WinH) goto endmap;
  3505. if (xx<0 || xx>=WinW) goto endmap;
  3506. DrawBox((WORD*)ddsd.lpSurface, lsw, xx+1, yy+1, 8<<RShift);
  3507. DrawBox((WORD*)ddsd.lpSurface, lsw, xx, yy, 30<<RShift);
  3508. /*
  3509. *((WORD*)ddsd.lpSurface + yy*lsw + xx) = 30<<RShift;
  3510. *((WORD*)ddsd.lpSurface + yy*lsw + xx + 1) = 30<<RShift;
  3511. yy++;
  3512. *((WORD*)ddsd.lpSurface + yy*lsw + xx) = 30<<RShift;
  3513. *((WORD*)ddsd.lpSurface + yy*lsw + xx + 1) = 30<<RShift;
  3514. */
  3515. CColor = 4<<GShift; DrawCircle(xx+1, yy+1, (ctViewR/4));
  3516. CColor = 18<<GShift; DrawCircle(xx, yy, (ctViewR/4));
  3517. if (RadarMode)
  3518. for (c=0; c<ChCount; c++) {
  3519. if (Characters[c].AI<10) continue;
  3520. if (! (TargetDino & (1<<Characters[c].AI)) ) continue;
  3521. if (!Characters[c].Health) continue;
  3522. xx = VideoCX - 128 + (int)Characters[c].pos.x / 1024;
  3523. yy = VideoCY - 128 + (int)Characters[c].pos.z / 1024;
  3524. if (yy<=0 || yy>=WinH) goto endmap;
  3525. if (xx<=0 || xx>=WinW) goto endmap;
  3526. DrawBox((WORD*)ddsd.lpSurface, lsw, xx, yy, 31);
  3527. }
  3528. endmap:
  3529. lpddBack->Unlock(ddsd.lpSurface);
  3530. }
  3531. void RenderSun(float x, float y, float z)
  3532. {
  3533. SunScrX = VideoCX + (int)(x / (-z) * CameraW);
  3534. SunScrY = VideoCY - (int)(y / (-z) * CameraH);
  3535. GetSkyK(SunScrX, SunScrY);
  3536. float d = (float)sqrt(x*x + y*y);
  3537. if (d<2048) {
  3538. SunLight = (220.f- d*220.f/2048.f);
  3539. if (SunLight>140) SunLight = 140;
  3540. SunLight*=SkyTraceK;
  3541. }
  3542. if (d>812.f) d = 812.f;
  3543. d = (2048.f + d) / 3048.f;
  3544. d+=(1.f-SkyTraceK)/2.f;
  3545. if (OptDayNight==2) d=1.5;
  3546. RenderModelSun(SunModel, x*d, y*d, z*d, (int)(200.f* SkyTraceK));
  3547. }
  3548. void RotateVVector(Vector3d& v)
  3549. {
  3550. float x = v.x * ca - v.z * sa;
  3551. float y = v.y;
  3552. float z = v.z * ca + v.x * sa;
  3553. float xx = x;
  3554. float xy = y * cb + z * sb;
  3555. float xz = z * cb - y * sb;
  3556. v.x = xx; v.y = xy; v.z = xz;
  3557. }
  3558. void RenderSkyPlane()
  3559. {
  3560. Vector3d v,vbase;
  3561. Vector3d tx,ty,nv;
  3562. float p,q, qx, qy, qz, px, py, pz, rx, ry, rz, ddx, ddy;
  3563. float lastdt = 0.f;
  3564. d3dSetTexture(SkyPic, 256, 256);
  3565. nv.x = 512; nv.y = 4024; nv.z=0;
  3566. cb = (float)f_cos(CameraBeta);
  3567. sb = (float)f_sin(CameraBeta);
  3568. SKYDTime = RealTime & ((1<<17) - 1);
  3569. float sh = - CameraY;
  3570. if (MapMinY==10241024)
  3571. MapMinY=0;
  3572. sh = (float)((int)MapMinY)*ctHScale - CameraY;
  3573. if (sh<-2024) sh=-2024;
  3574. v.x = 0;
  3575. v.z = ctViewR*256.f;
  3576. v.y = sh;
  3577. vbase.x = v.x;
  3578. vbase.y = v.y * cb + v.z * sb;
  3579. vbase.z = v.z * cb - v.y * sb;
  3580. if (vbase.z < 128) vbase.z = 128;
  3581. int scry = VideoCY - (int)(vbase.y / vbase.z * CameraH);
  3582. if (scry<0) return;
  3583. if (scry>WinEY+1) scry = WinEY+1;
  3584. cb = (float)f_cos(CameraBeta-0.15);
  3585. sb = (float)f_sin(CameraBeta-0.15);
  3586. v.x = 0;
  3587. v.z = 2*256.f*256.f;
  3588. v.y = 512;
  3589. vbase.x = v.x;
  3590. vbase.y = v.y * cb + v.z * sb;
  3591. vbase.z = v.z * cb - v.y * sb;
  3592. if (vbase.z < 128) vbase.z = 128;
  3593. int _scry = VideoCY - (int)(vbase.y / vbase.z * CameraH);
  3594. if (scry > _scry) scry = _scry;
  3595. tx.x=0.002f; tx.y=0; tx.z=0;
  3596. ty.x=0.0f; ty.y=0; ty.z=0.002f;
  3597. nv.x=0; nv.y=-1.f; nv.z=0;
  3598. RotateVVector(tx);
  3599. RotateVVector(ty);
  3600. RotateVVector(nv);
  3601. sh = 4*512*16;
  3602. vbase.x = -CameraX;
  3603. vbase.y = sh;
  3604. vbase.z = +CameraZ;
  3605. RotateVVector(vbase);
  3606. //============= calc render params =================//
  3607. p = nv.x * vbase.x + nv.y * vbase.y + nv.z * vbase.z;
  3608. ddx = vbase.x * tx.x + vbase.y * tx.y + vbase.z * tx.z;
  3609. ddy = vbase.x * ty.x + vbase.y * ty.y + vbase.z * ty.z;
  3610. qx = CameraH * nv.x; qy = CameraW * nv.y; qz = CameraW*CameraH * nv.z;
  3611. px = p*CameraH*tx.x; py = p*CameraW*tx.y; pz = p*CameraW*CameraH* tx.z;
  3612. rx = p*CameraH*ty.x; ry = p*CameraW*ty.y; rz = p*CameraW*CameraH* ty.z;
  3613. px=px - ddx*qx; py=py - ddx*qy; pz=pz - ddx*qz;
  3614. rx=rx - ddy*qx; ry=ry - ddy*qy; rz=rz - ddy*qz;
  3615. float za = CameraW * CameraH * p / (qy * VideoCY + qz);
  3616. float zb = CameraW * CameraH * p / (qy * (VideoCY-scry/2.f) + qz);
  3617. float zc = CameraW * CameraH * p / (qy * (VideoCY-scry) + qz);
  3618. float _za = fabs(za) - 100200.f; if (_za<0) _za=0.f;
  3619. float _zb = fabs(zb) - 100200.f; if (_zb<0) _zb=0.f;
  3620. float _zc = fabs(zc) - 100200.f; if (_zc<0) _zc=0.f;
  3621. int alpha = (int)(255*40240 / (40240+_za));
  3622. int alphb = (int)(255*40240 / (40240+_zb));
  3623. int alphc = (int)(255*40240 / (40240+_zc));
  3624. int sx1 = - VideoCX;
  3625. int sx2 = + VideoCX;
  3626. float qx1 = qx * sx1 + qz;
  3627. float qx2 = qx * sx2 + qz;
  3628. float qyy;
  3629. // d3dStartBuffer();
  3630. ZeroMemory(&d3dExeBufDesc, sizeof(d3dExeBufDesc));
  3631. d3dExeBufDesc.dwSize = sizeof(d3dExeBufDesc);
  3632. hRes = lpd3dExecuteBufferG->Lock( &d3dExeBufDesc );
  3633. if (FAILED(hRes)) DoHalt("Error locking execute buffer");
  3634. lpVertex = (LPD3DTLVERTEX)d3dExeBufDesc.lpData;
  3635. float dtt = (float)(SKYDTime) / 512.f;
  3636. float sky=0;
  3637. float sy = VideoCY - sky;
  3638. qyy = qy * sy;
  3639. q = qx1 + qyy;
  3640. float fxa = (px * sx1 + py * sy + pz) / q;
  3641. float fya = (rx * sx1 + ry * sy + rz) / q;
  3642. q = qx2 + qyy;
  3643. float fxb = (px * sx2 + py * sy + pz) / q;
  3644. float fyb = (rx * sx2 + ry * sy + rz) / q;
  3645. lpVertex->sx = 0.f;
  3646. lpVertex->sy = (float)sky;
  3647. lpVertex->sz = 0.0001f;//-8.f / za;
  3648. lpVertex->rhw = 128.f / za;
  3649. /*if (FOGENABLE) {
  3650. lpVertex->color = 0xFFFFFFFF;
  3651. lpVertex->specular = alpha<<24; } else */{
  3652. lpVertex->color = 0x00FFFFFF | alpha<<24;
  3653. lpVertex->specular = 0xFF000000; }
  3654. lpVertex->tu = (fxa + dtt) / 256.f;
  3655. lpVertex->tv = (fya - dtt) / 256.f;
  3656. lpVertex++;
  3657. lpVertex->sx = (float)WinW;
  3658. lpVertex->sy = (float)sky;
  3659. lpVertex->sz = 0.0001f;//-8.f / za;
  3660. lpVertex->rhw = 128.f / za;
  3661. /*if (FOGENABLE) {
  3662. lpVertex->color = 0xFFFFFFFF;
  3663. lpVertex->specular = alpha<<24; } else */{
  3664. lpVertex->color = 0x00FFFFFF | alpha<<24;
  3665. lpVertex->specular = 0xFF000000; }
  3666. lpVertex->tu = (fxb + dtt) / 256.f;
  3667. lpVertex->tv = (fyb - dtt) / 256.f;
  3668. lpVertex++;
  3669. sky=scry/2.f;
  3670. sy = VideoCY - sky;
  3671. qyy = qy * sy;
  3672. q = qx1 + qyy;
  3673. fxa = (px * sx1 + py * sy + pz) / q;
  3674. fya = (rx * sx1 + ry * sy + rz) / q;
  3675. q = qx2 + qyy;
  3676. fxb = (px * sx2 + py * sy + pz) / q;
  3677. fyb = (rx * sx2 + ry * sy + rz) / q;
  3678. lpVertex->sx = 0.f;
  3679. lpVertex->sy = (float)sky;
  3680. lpVertex->sz = 0.0001f;//-8.f / zb;
  3681. lpVertex->rhw = 128.f / zb;
  3682. /*if (FOGENABLE) {
  3683. lpVertex->color = 0xFFFFFFFF;
  3684. lpVertex->specular = alphb<<24; } else */{
  3685. lpVertex->color = 0x00FFFFFF | alphb<<24;
  3686. lpVertex->specular = 0xFF000000; }
  3687. lpVertex->tu = (fxa + dtt) / 256.f;
  3688. lpVertex->tv = (fya - dtt) / 256.f;
  3689. lpVertex++;
  3690. lpVertex->sx = (float)WinW;
  3691. lpVertex->sy = (float)sky;
  3692. lpVertex->sz = 0.0001f;//-8.f / zb;
  3693. lpVertex->rhw = 128.f / zb;
  3694. /*if (FOGENABLE) {
  3695. lpVertex->color = 0xFFFFFFFF;
  3696. lpVertex->specular = alphb<<24; } else */{
  3697. lpVertex->color = 0x00FFFFFF | alphb<<24;
  3698. lpVertex->specular = 0xFF000000; }
  3699. lpVertex->tu = (fxb + dtt) / 256.f;
  3700. lpVertex->tv = (fyb - dtt) / 256.f;
  3701. lpVertex++;
  3702. sky=(float)scry;
  3703. sy = VideoCY - sky;
  3704. qyy = qy * sy;
  3705. q = qx1 + qyy;
  3706. fxa = (px * sx1 + py * sy + pz) / q;
  3707. fya = (rx * sx1 + ry * sy + rz) / q;
  3708. q = qx2 + qyy;
  3709. fxb = (px * sx2 + py * sy + pz) / q;
  3710. fyb = (rx * sx2 + ry * sy + rz) / q;
  3711. lpVertex->sx = 0.f;
  3712. lpVertex->sy = (float)sky;
  3713. lpVertex->sz = 0.0001f;//-8.f / zb;
  3714. lpVertex->rhw = 128.f / zc;
  3715. /*if (FOGENABLE) {
  3716. lpVertex->color = 0xFFFFFFFF;
  3717. lpVertex->specular = alphc<<24; } else */{
  3718. lpVertex->color = 0x00FFFFFF | alphc<<24;
  3719. lpVertex->specular = 0xFF000000; }
  3720. lpVertex->tu = (fxa + dtt) / 256.f;
  3721. lpVertex->tv = (fya - dtt) / 256.f;
  3722. lpVertex++;
  3723. lpVertex->sx = (float)WinW;
  3724. lpVertex->sy = (float)sky;
  3725. lpVertex->sz = 0.0001f;//-8.f / zb;
  3726. lpVertex->rhw = 128.f / zc;
  3727. /*if (FOGENABLE) {
  3728. lpVertex->color = 0xFFFFFFFF;
  3729. lpVertex->specular = alphc<<24; } else */{
  3730. lpVertex->color = 0x00FFFFFF | alphc<<24;
  3731. lpVertex->specular = 0xFF000000; }
  3732. lpVertex->tu = (fxb + dtt) / 256.f;
  3733. lpVertex->tv = (fyb - dtt) / 256.f;
  3734. lpVertex++;
  3735. lpInstruction = (LPD3DINSTRUCTION) ((LPD3DTLVERTEX)d3dExeBufDesc.lpData + d3debvcG*3);
  3736. lpInstruction->bOpcode = D3DOP_STATERENDER;
  3737. lpInstruction->bSize = sizeof(D3DSTATE);
  3738. lpInstruction->wCount = 4;
  3739. lpInstruction++;
  3740. lpState = (LPD3DSTATE)lpInstruction;
  3741. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREHANDLE;
  3742. lpState->dwArg[0] = hTexture;
  3743. lpState++;
  3744. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMAG;
  3745. lpState->dwArg[0] = D3DFILTER_LINEAR;
  3746. lpState++;
  3747. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREMIN;
  3748. lpState->dwArg[0] = D3DFILTER_LINEAR;
  3749. lpState++;
  3750. lpState->drstRenderStateType = D3DRENDERSTATE_FOGCOLOR;
  3751. if (UNDERWATER) lpState->dwArg[0] = CurFogColor;
  3752. else lpState->dwArg[0] = (SkyR<<16) + (SkyG<<8) + SkyB;
  3753. lpState++;
  3754. lpInstruction = (LPD3DINSTRUCTION)lpState;
  3755. lpInstruction->bOpcode = D3DOP_PROCESSVERTICES;
  3756. lpInstruction->bSize = sizeof(D3DPROCESSVERTICES);
  3757. lpInstruction->wCount = 1U;
  3758. lpInstruction++;
  3759. lpProcessVertices = (LPD3DPROCESSVERTICES)lpInstruction;
  3760. lpProcessVertices->dwFlags = D3DPROCESSVERTICES_COPY;
  3761. lpProcessVertices->wStart = 0U;
  3762. lpProcessVertices->wDest = 0U;
  3763. lpProcessVertices->dwCount = 6;
  3764. lpProcessVertices->dwReserved = 0UL;
  3765. lpProcessVertices++;
  3766. lpInstruction = (LPD3DINSTRUCTION)lpProcessVertices;
  3767. lpInstruction->bOpcode = D3DOP_TRIANGLE;
  3768. lpInstruction->bSize = sizeof(D3DTRIANGLE);
  3769. lpInstruction->wCount = 4;
  3770. lpInstruction++;
  3771. lpTriangle = (LPD3DTRIANGLE)lpInstruction;
  3772. lpTriangle->wV1 = 0;
  3773. lpTriangle->wV2 = 1;
  3774. lpTriangle->wV3 = 2;
  3775. lpTriangle->wFlags = 0;
  3776. lpTriangle++;
  3777. lpTriangle->wV1 = 1;
  3778. lpTriangle->wV2 = 2;
  3779. lpTriangle->wV3 = 3;
  3780. lpTriangle->wFlags = 0;
  3781. lpTriangle++;
  3782. lpTriangle->wV1 = 2;
  3783. lpTriangle->wV2 = 3;
  3784. lpTriangle->wV3 = 4;
  3785. lpTriangle->wFlags = 0;
  3786. lpTriangle++;
  3787. lpTriangle->wV1 = 3;
  3788. lpTriangle->wV2 = 4;
  3789. lpTriangle->wV3 = 5;
  3790. lpTriangle->wFlags = 0;
  3791. lpTriangle++;
  3792. lpInstruction = (LPD3DINSTRUCTION)lpTriangle;
  3793. lpInstruction->bOpcode = D3DOP_EXIT;
  3794. lpInstruction->bSize = 0UL;
  3795. lpInstruction->wCount = 0U;
  3796. lpd3dExecuteBufferG->Unlock( );
  3797. hRes = lpd3dDevice->Execute(lpd3dExecuteBufferG, lpd3dViewport, D3DEXECUTE_UNCLIPPED);
  3798. LINEARFILTER = TRUE;
  3799. nv = RotateVector(Sun3dPos);
  3800. SunLight = 0;
  3801. if (nv.z < -2024) RenderSun(nv.x, nv.y, nv.z);
  3802. }
  3803. void RenderFSRect(DWORD Color)
  3804. {
  3805. d3dStartBuffer();
  3806. lpVertex->sx = 0.f;
  3807. lpVertex->sy = 0.f;
  3808. lpVertex->sz = 0.999f;
  3809. lpVertex->rhw = 1.f;
  3810. lpVertex->color = Color;
  3811. lpVertex->specular = 0xFF000000;
  3812. lpVertex->tu = 0;
  3813. lpVertex->tv = 0;
  3814. lpVertex++;
  3815. lpVertex->sx = (float)WinW;
  3816. lpVertex->sy = 0.f;
  3817. lpVertex->sz = 0.999f;
  3818. lpVertex->rhw = 1.f;
  3819. lpVertex->color = Color;
  3820. lpVertex->specular = 0xFF000000;
  3821. lpVertex->tu = 0;
  3822. lpVertex->tv = 0;
  3823. lpVertex++;
  3824. lpVertex->sx = 0.f;
  3825. lpVertex->sy = (float)WinH;
  3826. lpVertex->sz = 0.999f;
  3827. lpVertex->rhw = 1.f;
  3828. lpVertex->color = Color;
  3829. lpVertex->specular = 0xFF000000;
  3830. lpVertex->tu = 0;
  3831. lpVertex->tv = 0;
  3832. lpVertex++;
  3833. lpVertex->sx = (float)WinW;
  3834. lpVertex->sy = (float)WinH;
  3835. lpVertex->sz = (float)0.999f;
  3836. lpVertex->rhw = 1.f;
  3837. lpVertex->color = Color;
  3838. lpVertex->specular = 0xFF000000;
  3839. lpVertex->tu = 0;
  3840. lpVertex->tv = 0;
  3841. lpVertex++;
  3842. lpInstruction = (LPD3DINSTRUCTION) ((LPD3DTLVERTEX)d3dExeBufDesc.lpData + d3debvc*3);
  3843. lpInstruction->bOpcode = D3DOP_STATERENDER;
  3844. lpInstruction->bSize = sizeof(D3DSTATE);
  3845. lpInstruction->wCount = 1;
  3846. lpInstruction++;
  3847. lpState = (LPD3DSTATE)lpInstruction;
  3848. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREHANDLE;
  3849. lpState->dwArg[0] = NULL;
  3850. lpState++;
  3851. lpInstruction = (LPD3DINSTRUCTION)lpState;
  3852. lpInstruction->bOpcode = D3DOP_PROCESSVERTICES;
  3853. lpInstruction->bSize = sizeof(D3DPROCESSVERTICES);
  3854. lpInstruction->wCount = 1U;
  3855. lpInstruction++;
  3856. lpProcessVertices = (LPD3DPROCESSVERTICES)lpInstruction;
  3857. lpProcessVertices->dwFlags = D3DPROCESSVERTICES_COPY;
  3858. lpProcessVertices->wStart = 0U;
  3859. lpProcessVertices->wDest = 0U;
  3860. lpProcessVertices->dwCount = 4;
  3861. lpProcessVertices->dwReserved = 0UL;
  3862. lpProcessVertices++;
  3863. lpInstruction = (LPD3DINSTRUCTION)lpProcessVertices;
  3864. lpInstruction->bOpcode = D3DOP_TRIANGLE;
  3865. lpInstruction->bSize = sizeof(D3DTRIANGLE);
  3866. lpInstruction->wCount = 2;
  3867. lpInstruction++;
  3868. lpTriangle = (LPD3DTRIANGLE)lpInstruction;
  3869. lpTriangle->wV1 = 0;
  3870. lpTriangle->wV2 = 1;
  3871. lpTriangle->wV3 = 2;
  3872. lpTriangle->wFlags = 0;
  3873. lpTriangle++;
  3874. lpTriangle->wV1 = 1;
  3875. lpTriangle->wV2 = 2;
  3876. lpTriangle->wV3 = 3;
  3877. lpTriangle->wFlags = 0;
  3878. lpTriangle++;
  3879. lpInstruction = (LPD3DINSTRUCTION)lpTriangle;
  3880. lpInstruction->bOpcode = D3DOP_EXIT;
  3881. lpInstruction->bSize = 0UL;
  3882. lpInstruction->wCount = 0U;
  3883. lpd3dExecuteBuffer->Unlock( );
  3884. hRes = lpd3dDevice->Execute(lpd3dExecuteBuffer, lpd3dViewport, D3DEXECUTE_UNCLIPPED);
  3885. }
  3886. void RenderHealthBar()
  3887. {
  3888. if (MyHealth >= 100000) return;
  3889. if (MyHealth == 000000) return;
  3890. int L = WinW / 4;
  3891. int x0 = WinW - (WinW / 20) - L;
  3892. int y0 = WinH / 40;
  3893. int G = min( (MyHealth * 240 / 100000), 160);
  3894. int R = min( ( (100000 - MyHealth) * 240 / 100000), 160);
  3895. int L0 = (L * MyHealth) / 100000;
  3896. int H = WinH / 200;
  3897. d3dStartBuffer();
  3898. for (int y=0; y<4; y++) {
  3899. lpVertex->sx = (float)x0-1;
  3900. lpVertex->sy = (float)y0+y;
  3901. lpVertex->sz = 0.9999f;
  3902. lpVertex->rhw = 1.f;
  3903. lpVertex->color = 0xF0000010;
  3904. lpVertex->specular = 0xFF000000;
  3905. lpVertex->tu = 0;
  3906. lpVertex->tv = 0;
  3907. lpVertex++;
  3908. lpVertex->sx = (float)x0+L0+1;
  3909. lpVertex->sy = (float)y0+y;
  3910. lpVertex->sz = 0.9999f;
  3911. lpVertex->rhw = 1.f;
  3912. lpVertex->color = 0xF0000010;
  3913. lpVertex->specular = 0xFF000000;
  3914. lpVertex->tu = 0;
  3915. lpVertex->tv = 0;
  3916. lpVertex++;
  3917. }
  3918. for (y=1; y<3; y++) {
  3919. lpVertex->sx = (float)x0;
  3920. lpVertex->sy = (float)y0+y;
  3921. lpVertex->sz = 0.99999f;
  3922. lpVertex->rhw = 1.f;
  3923. lpVertex->color = 0xF0000000 + (G<<8) + (R<<16);
  3924. lpVertex->specular = 0xFF000000;// + (G<<8) + (R<<16);
  3925. lpVertex->tu = 0;
  3926. lpVertex->tv = 0;
  3927. lpVertex++;
  3928. lpVertex->sx = (float)x0+L0;
  3929. lpVertex->sy = (float)y0+y;
  3930. lpVertex->sz = 0.99999f;
  3931. lpVertex->rhw = 1.f;
  3932. lpVertex->color = 0xF0000000 + (G<<8) + (R<<16);
  3933. lpVertex->specular = 0xFF000000;// + (G<<8) + (R<<16);
  3934. lpVertex->tu = 0;
  3935. lpVertex->tv = 0;
  3936. lpVertex++;
  3937. }
  3938. lpInstruction = (LPD3DINSTRUCTION) ((LPD3DTLVERTEX)d3dExeBufDesc.lpData + d3debvc*3);
  3939. lpInstruction->bOpcode = D3DOP_STATERENDER;
  3940. lpInstruction->bSize = sizeof(D3DSTATE);
  3941. lpInstruction->wCount = 1;
  3942. lpInstruction++;
  3943. lpState = (LPD3DSTATE)lpInstruction;
  3944. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREHANDLE;
  3945. lpState->dwArg[0] = NULL;
  3946. lpState++;
  3947. lpInstruction = (LPD3DINSTRUCTION)lpState;
  3948. lpInstruction->bOpcode = D3DOP_PROCESSVERTICES;
  3949. lpInstruction->bSize = sizeof(D3DPROCESSVERTICES);
  3950. lpInstruction->wCount = 1U;
  3951. lpInstruction++;
  3952. lpProcessVertices = (LPD3DPROCESSVERTICES)lpInstruction;
  3953. lpProcessVertices->dwFlags = D3DPROCESSVERTICES_COPY;
  3954. lpProcessVertices->wStart = 0U;
  3955. lpProcessVertices->wDest = 0U;
  3956. lpProcessVertices->dwCount = 12;
  3957. lpProcessVertices->dwReserved = 0UL;
  3958. lpProcessVertices++;
  3959. lpInstruction = (LPD3DINSTRUCTION)lpProcessVertices;
  3960. lpInstruction->bOpcode = D3DOP_LINE;
  3961. lpInstruction->bSize = sizeof(D3DLINE);
  3962. lpInstruction->wCount = 6;
  3963. lpInstruction++;
  3964. lpLine = (LPD3DLINE)lpInstruction;
  3965. for (y=0; y<6; y++) {
  3966. lpLine->wV1 = y*2;
  3967. lpLine->wV2 = y*2+1;
  3968. lpLine++;
  3969. }
  3970. lpInstruction = (LPD3DINSTRUCTION)lpLine;
  3971. lpInstruction->bOpcode = D3DOP_EXIT;
  3972. lpInstruction->bSize = 0UL;
  3973. lpInstruction->wCount = 0U;
  3974. lpd3dExecuteBuffer->Unlock( );
  3975. hRes = lpd3dDevice->Execute(lpd3dExecuteBuffer, lpd3dViewport, D3DEXECUTE_UNCLIPPED);
  3976. }
  3977. void Render_Cross(int sx, int sy)
  3978. {
  3979. float w = (float) WinW / 12.f;
  3980. d3dStartBuffer();
  3981. lpVertex->sx = (float)sx-w;
  3982. lpVertex->sy = (float)sy;
  3983. lpVertex->sz = 0.99999f;
  3984. lpVertex->rhw = 1.f;
  3985. lpVertex->color = 0x80000010;
  3986. lpVertex->specular = 0xFF000000;
  3987. lpVertex->tu = 0;
  3988. lpVertex->tv = 0;
  3989. lpVertex++;
  3990. lpVertex->sx = (float)sx+w;
  3991. lpVertex->sy = (float)sy;
  3992. lpVertex->sz = 0.99999f;
  3993. lpVertex->rhw = 1.f;
  3994. lpVertex->color = 0x80000010;
  3995. lpVertex->specular = 0xFF000000;
  3996. lpVertex->tu = 0;
  3997. lpVertex->tv = 0;
  3998. lpVertex++;
  3999. lpVertex->sx = (float)sx;
  4000. lpVertex->sy = (float)sy-w;
  4001. lpVertex->sz = 0.99999f;
  4002. lpVertex->rhw = 1.f;
  4003. lpVertex->color = 0x80000010;
  4004. lpVertex->specular = 0xFF000000;
  4005. lpVertex->tu = 0;
  4006. lpVertex->tv = 0;
  4007. lpVertex++;
  4008. lpVertex->sx = (float)sx;
  4009. lpVertex->sy = (float)sy+w;
  4010. lpVertex->sz = 0.99999f;
  4011. lpVertex->rhw = 1.f;
  4012. lpVertex->color = 0x80000010;
  4013. lpVertex->specular = 0xFF000000;
  4014. lpVertex->tu = 0;
  4015. lpVertex->tv = 0;
  4016. lpVertex++;
  4017. lpInstruction = (LPD3DINSTRUCTION) ((LPD3DTLVERTEX)d3dExeBufDesc.lpData + d3debvc*3);
  4018. lpInstruction->bOpcode = D3DOP_STATERENDER;
  4019. lpInstruction->bSize = sizeof(D3DSTATE);
  4020. lpInstruction->wCount = 1;
  4021. lpInstruction++;
  4022. lpState = (LPD3DSTATE)lpInstruction;
  4023. lpState->drstRenderStateType = D3DRENDERSTATE_TEXTUREHANDLE;
  4024. lpState->dwArg[0] = NULL;
  4025. lpState++;
  4026. lpInstruction = (LPD3DINSTRUCTION)lpState;
  4027. lpInstruction->bOpcode = D3DOP_PROCESSVERTICES;
  4028. lpInstruction->bSize = sizeof(D3DPROCESSVERTICES);
  4029. lpInstruction->wCount = 1U;
  4030. lpInstruction++;
  4031. lpProcessVertices = (LPD3DPROCESSVERTICES)lpInstruction;
  4032. lpProcessVertices->dwFlags = D3DPROCESSVERTICES_COPY;
  4033. lpProcessVertices->wStart = 0U;
  4034. lpProcessVertices->wDest = 0U;
  4035. lpProcessVertices->dwCount = 4;
  4036. lpProcessVertices->dwReserved = 0UL;
  4037. lpProcessVertices++;
  4038. lpInstruction = (LPD3DINSTRUCTION)lpProcessVertices;
  4039. lpInstruction->bOpcode = D3DOP_LINE;
  4040. lpInstruction->bSize = sizeof(D3DLINE);
  4041. lpInstruction->wCount = 2;
  4042. lpInstruction++;
  4043. lpLine = (LPD3DLINE)lpInstruction;
  4044. lpLine->wV1 = 0;
  4045. lpLine->wV2 = 1;
  4046. lpLine++;
  4047. lpLine->wV1 = 2;
  4048. lpLine->wV2 = 3;
  4049. lpLine++;
  4050. lpInstruction = (LPD3DINSTRUCTION)lpLine;
  4051. lpInstruction->bOpcode = D3DOP_EXIT;
  4052. lpInstruction->bSize = 0UL;
  4053. lpInstruction->wCount = 0U;
  4054. lpd3dExecuteBuffer->Unlock( );
  4055. hRes = lpd3dDevice->Execute(lpd3dExecuteBuffer, lpd3dViewport, D3DEXECUTE_UNCLIPPED);
  4056. }
  4057. #endif