tgl.cpp 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152
  1. //-------------------------------------------------------------------------------
  2. //
  3. // Tiny Geometry Layer
  4. //
  5. // For MechCommander 2 -- Copyright (c) 1999 Microsoft
  6. //
  7. //---------------------------------------------------------------------------//
  8. // Copyright (C) Microsoft Corporation. All rights reserved. //
  9. //===========================================================================//
  10. #ifndef TGL_H
  11. #include "tgl.h"
  12. #endif
  13. #ifndef CLIP_H
  14. #include "clip.h"
  15. #endif
  16. #ifndef TIMING_H
  17. #include "timing.h"
  18. #endif
  19. #ifndef TERRAIN_H
  20. #include "terrain.h"
  21. #endif
  22. #ifndef CAMERA_H
  23. #include "camera.h"
  24. #endif
  25. #ifndef TXMMGR_H
  26. #include "txmmgr.h"
  27. #endif
  28. //-------------------------------------------------------------------------------
  29. // Include Files
  30. #include <toolOS.hpp>
  31. #define USE_ASSEMBLY
  32. //-------------------------------------------------------------------------------
  33. // Statics
  34. Stuff::LinearMatrix4D *TG_Shape::cameraOrigin = NULL;
  35. Stuff::Matrix4D *TG_Shape::cameraToClip = NULL;
  36. Stuff::LinearMatrix4D TG_Shape::worldToCamera;
  37. Stuff::Matrix4D TG_Shape::worldToClip;
  38. float TG_Shape::viewMulX = 0.0;
  39. float TG_Shape::viewAddX = 0.0;
  40. float TG_Shape::viewMulY = 0.0;
  41. float TG_Shape::viewAddY = 0.0;
  42. DWORD TG_Shape::fogColor = 0xffffffff;
  43. float TG_Shape::fogStart = 0.0f;
  44. float TG_Shape::fogFull = 0.0f;
  45. TG_LightPtr *TG_Shape::listOfLights = NULL;
  46. DWORD TG_Shape::numLights = 0;
  47. Stuff::LinearMatrix4D TG_Shape::lightToShape[MAX_LIGHTS_IN_WORLD];
  48. Stuff::Vector3D TG_Shape::lightDir[MAX_LIGHTS_IN_WORLD];
  49. Stuff::Vector3D TG_Shape::rootLightDir[MAX_LIGHTS_IN_WORLD];
  50. Stuff::Vector3D TG_Shape::spotDir[MAX_LIGHTS_IN_WORLD];
  51. UserHeapPtr TG_Shape::tglHeap = NULL;
  52. DWORD TG_Shape::lighteningLevel = 0;
  53. TG_VertexPool *colorPool = NULL;
  54. TG_GOSVertexPool *vertexPool = NULL;
  55. TG_DWORDPool *facePool = NULL;
  56. TG_ShadowPool *shadowPool = NULL;
  57. TG_TrianglePool *trianglePool = NULL;
  58. //-------------------------------------------------------------------------------
  59. extern bool useVertexLighting;
  60. extern bool useFaceLighting;
  61. extern bool hasGuardBand;
  62. extern bool useFog;
  63. extern DWORD BaseVertexColor;
  64. bool drawOldWay = false;
  65. extern bool useShadows;
  66. bool useLocalShadows = false;
  67. bool renderTGLShapes = true;
  68. bool silentMode = false; //Used for automated builds to keep errors from popping up.
  69. //-------------------------------------------------------------------------------
  70. // Parse Functions
  71. void GetNumberData (char *rawData, char *result)
  72. {
  73. long startIndex = 0;
  74. long endIndex = 0;
  75. while ( (rawData[startIndex] != '+') &&
  76. (rawData[startIndex] != '-') &&
  77. (rawData[startIndex] != '.') &&
  78. (rawData[startIndex] < '0') ||
  79. (rawData[startIndex] > '9'))
  80. {
  81. startIndex++;
  82. }
  83. endIndex = startIndex;
  84. while ( (rawData[endIndex] == '+') ||
  85. (rawData[endIndex] == '-') ||
  86. (rawData[endIndex] == '.') ||
  87. (rawData[endIndex] >= '0') &&
  88. (rawData[endIndex] <= '9'))
  89. {
  90. endIndex++;
  91. }
  92. strncpy(result,&rawData[startIndex],endIndex - startIndex);
  93. result[endIndex-startIndex] = 0;
  94. }
  95. //-------------------------------------------------------------------------------
  96. void GetWordData (char *rawData, char *result)
  97. {
  98. long startIndex = 0;
  99. long endIndex = 0;
  100. while ( (rawData[startIndex] < 'A') ||
  101. (rawData[startIndex] > 'Z') &&
  102. (rawData[startIndex] < 'a') ||
  103. (rawData[startIndex] > 'z'))
  104. {
  105. startIndex++;
  106. }
  107. endIndex = startIndex;
  108. while ( ((rawData[endIndex] >= 'A') && (rawData[endIndex] <= 'Z')) ||
  109. (rawData[endIndex] == '_') ||
  110. ((rawData[endIndex] >= 'a') && (rawData[endIndex] <= 'z')) )
  111. {
  112. endIndex++;
  113. }
  114. strncpy(result,&rawData[startIndex],endIndex - startIndex);
  115. result[endIndex-startIndex] = 0;
  116. }
  117. //-------------------------------------------------------------------------------
  118. void GetNameData (char *rawData, char *result)
  119. {
  120. long startIndex = 0;
  121. long endIndex = 0;
  122. while ( (rawData[startIndex] != '"') )
  123. {
  124. startIndex++;
  125. }
  126. startIndex++;
  127. endIndex = startIndex;
  128. while ( (rawData[endIndex] != '"') )
  129. {
  130. endIndex++;
  131. }
  132. strncpy(result,&rawData[startIndex],endIndex - startIndex);
  133. result[endIndex-startIndex] = 0;
  134. }
  135. //-------------------------------------------------------------------------------
  136. // Class TG_TypeNode
  137. //-------------------------------------------------------------------------------
  138. void *TG_TypeNode::operator new (size_t mySize)
  139. {
  140. void *result = TG_Shape::tglHeap->Malloc(mySize);
  141. return result;
  142. }
  143. //-------------------------------------------------------------------------------
  144. void TG_TypeNode::operator delete (void *us)
  145. {
  146. TG_Shape::tglHeap->Free(us);
  147. }
  148. //-------------------------------------------------------------------------------
  149. // Frees memory and resets locals to defaults.
  150. void TG_TypeNode::destroy (void)
  151. {
  152. init();
  153. }
  154. //-------------------------------------------------------------------------------
  155. // This function creates an instance of a TG_Shape from a TG_TypeNode. Saves RAM.
  156. TG_ShapePtr TG_TypeNode::CreateFrom (void)
  157. {
  158. TG_ShapePtr newShape = NULL;
  159. newShape = (TG_ShapePtr)TG_Shape::tglHeap->Malloc(sizeof(TG_Shape));
  160. gosASSERT(newShape != NULL);
  161. //listOfVertices
  162. newShape->numVertices = 0;
  163. newShape->listOfColors = NULL;
  164. newShape->listOfVertices = NULL;
  165. newShape->listOfShadowVertices = NULL;
  166. //listOfTriangles
  167. newShape->numTriangles = 0;
  168. newShape->listOfTriangles = NULL;
  169. //listOfVisibleFaces
  170. newShape->numVisibleFaces = 0;
  171. newShape->listOfVisibleFaces = NULL;
  172. //listOfVisibleShadows
  173. newShape->numVisibleShadows = 0;
  174. newShape->listOfVisibleShadows = NULL;
  175. //Other Data
  176. newShape->myType = this;
  177. if (newShape->myType == NULL)
  178. STOP(("ShapeType NULL. BAD Shape in MAX FILE"));
  179. newShape->aRGBHighlight = 0x00000000;
  180. newShape->lightsOut = false;
  181. newShape->recalcShadows = true;
  182. newShape->shapeScalar = 0.0f;
  183. for (long i=0;i<MAX_SHADOWS;i++)
  184. newShape->shadowsVisible[i] = false;
  185. newShape->isSpotlight = (strnicmp(newShape->getNodeName(),"SpotLight_",10) == 0);
  186. newShape->isWindow = (strnicmp(newShape->getNodeName(),"LitWin_",6) == 0);
  187. if (newShape->isSpotlight)
  188. newShape->noShadow = true;
  189. else
  190. newShape->noShadow = false;
  191. return newShape;
  192. }
  193. //-------------------------------------------------------------------------------
  194. //Function return 0 is OK. -1 if file is not ASE Format or missing data.
  195. //This function simply parses the ASE buffers handed to it. This allows
  196. //users to load the ase file themselves and manage their own memory for it.
  197. //It allocates memory for internal Lists. These are straight tglHeap->Mallocs at present.
  198. //
  199. long TG_TypeNode::MakeFromHelper (BYTE *aseBuffer, char *fileName)
  200. {
  201. //------------------------------------------
  202. // Store off the Node Names.
  203. char *nodeName = strstr((char *)aseBuffer,ASE_NODE_NAME);
  204. gosASSERT(nodeName != NULL);
  205. char nodeString[1024];
  206. nodeName += strlen(ASE_NODE_NAME)+1;
  207. GetNameData(nodeName,nodeString);
  208. #ifdef _DEBUG
  209. if (strlen(nodeString) >= TG_NODE_ID)
  210. {
  211. if (!silentMode)
  212. PAUSE(("WARNING: Node ID %s in Shape %s is greater then 24 characters!!",nodeString,fileName));
  213. }
  214. #endif
  215. strncpy(nodeId,nodeString,24);
  216. char* parentName = strstr((char *)nodeName,ASE_NODE_PARENT);
  217. //-------------------------------------------------------------------
  218. // Must also check to make sure we HAVE a parent.
  219. // We will get the next GeomObject's parent if we don't check length!
  220. if ((parentName != NULL) && ((parentName - nodeName) < MAX_SCAN_LENGTH))
  221. {
  222. parentName += strlen(ASE_NODE_PARENT)+1;
  223. GetNameData(parentName,nodeString);
  224. strncpy(parentId,nodeString,24);
  225. }
  226. else
  227. {
  228. strcpy(parentId,"None");
  229. }
  230. //----------------------------------------------------
  231. // Store off NODE ABS position for heirarchy
  232. nodeName = strstr((char *)aseBuffer,ASE_NODE_POS);
  233. gosASSERT(nodeName != NULL);
  234. nodeName += strlen(ASE_NODE_POS)+1;
  235. char numData[512];
  236. GetNumberData(nodeName,numData);
  237. nodeCenter.x = -(float)atof(numData);
  238. nodeName += strlen(numData)+1;
  239. GetNumberData(nodeName,numData);
  240. nodeCenter.z = (float)atof(numData);
  241. nodeName += strlen(numData)+1;
  242. GetNumberData(nodeName,numData);
  243. nodeCenter.y = (float)atof(numData);
  244. return(0);
  245. }
  246. //-------------------------------------------------------------------------------
  247. void TG_TypeNode::LoadBinaryCopy (File &binFile)
  248. {
  249. //Other Data
  250. nodeCenter.x = binFile.readFloat();
  251. nodeCenter.y = binFile.readFloat();
  252. nodeCenter.z = binFile.readFloat();
  253. relativeNodeCenter.x = binFile.readFloat();
  254. relativeNodeCenter.y = binFile.readFloat();
  255. relativeNodeCenter.z = binFile.readFloat();
  256. binFile.read((MemoryPtr)nodeId,TG_NODE_ID);
  257. binFile.read((MemoryPtr)parentId,TG_NODE_ID);
  258. }
  259. //-------------------------------------------------------------------------------
  260. void TG_TypeNode::SaveBinaryCopy (File &binFile)
  261. {
  262. //What kind of thing is this
  263. binFile.writeLong(TYPE_NODE);
  264. //Other Data
  265. binFile.writeFloat(nodeCenter.x);
  266. binFile.writeFloat(nodeCenter.y);
  267. binFile.writeFloat(nodeCenter.z);
  268. binFile.writeFloat(relativeNodeCenter.x);
  269. binFile.writeFloat(relativeNodeCenter.y);
  270. binFile.writeFloat(relativeNodeCenter.z);
  271. binFile.write((MemoryPtr)nodeId,TG_NODE_ID);
  272. binFile.write((MemoryPtr)parentId,TG_NODE_ID);
  273. }
  274. //-------------------------------------------------------------------------------
  275. // Class TG_Shape && TG_TypeShape
  276. //-------------------------------------------------------------------------------
  277. // TG_TypeShape
  278. // This function creates an instance of a TG_Shape from a TG_TypeShape. Saves RAM.
  279. TG_ShapePtr TG_TypeShape::CreateFrom (void)
  280. {
  281. TG_ShapePtr newShape = NULL;
  282. newShape = (TG_ShapePtr)TG_Shape::tglHeap->Malloc(sizeof(TG_Shape));
  283. gosASSERT(newShape != NULL);
  284. //listOfVertices
  285. newShape->numVertices = numTypeVertices;
  286. if (numTypeVertices)
  287. {
  288. //Mark with invalid pointer so we now to get RAM from POOL!
  289. newShape->listOfVertices = NULL;
  290. newShape->listOfColors = NULL;
  291. }
  292. else
  293. {
  294. //Otherwise mark with NULL so we know not to reference!!
  295. newShape->listOfVertices = NULL;
  296. newShape->listOfColors = NULL;
  297. }
  298. //listOfShadowVertices
  299. if (numTypeVertices)
  300. {
  301. //These are UNIQUE to each instance and do not change much per frame.
  302. // Thus, store them between frames to save processing time!
  303. newShape->listOfShadowVertices = (TG_ShadowVertexPtr)TG_Shape::tglHeap->Malloc(sizeof(TG_ShadowVertex) * numTypeVertices * MAX_SHADOWS);
  304. gosASSERT(newShape->listOfShadowVertices != NULL);
  305. memset(newShape->listOfShadowVertices,0xff,sizeof(TG_ShadowVertex) * numTypeVertices * MAX_SHADOWS);
  306. //Mark with invalid pointer so we now to get RAM from POOL!
  307. newShape->listOfShadowTVertices = NULL;
  308. }
  309. else
  310. {
  311. //Otherwise mark with NULL so we know not to reference!!
  312. newShape->listOfShadowVertices = NULL;
  313. newShape->listOfShadowTVertices = NULL;
  314. }
  315. //listOfTriangles
  316. newShape->numTriangles = numTypeTriangles;
  317. if (numTypeTriangles)
  318. {
  319. //Mark with invalid pointer so we now to get RAM from POOL!
  320. newShape->listOfTriangles = NULL;
  321. }
  322. else
  323. {
  324. //Otherwise mark with NULL so we know not to reference!!
  325. newShape->listOfTriangles = NULL;
  326. }
  327. //listOfVisibleFaces
  328. newShape->numVisibleFaces = 0;
  329. if (numTypeTriangles)
  330. {
  331. //Mark with invalid pointer so we now to get RAM from POOL!
  332. newShape->listOfVisibleFaces = NULL;
  333. }
  334. else
  335. {
  336. //Otherwise mark with NULL so we know not to reference!!
  337. newShape->listOfVisibleFaces = NULL;
  338. }
  339. //listOfVisibleShadows
  340. newShape->numVisibleShadows = 0;
  341. if (numTypeTriangles)
  342. {
  343. //Mark with invalid pointer so we now to get RAM from POOL!
  344. newShape->listOfVisibleShadows = NULL;
  345. }
  346. else
  347. {
  348. //Otherwise mark with NULL so we know not to reference!!
  349. newShape->listOfVisibleShadows = NULL;
  350. }
  351. //Other Data
  352. newShape->myType = this;
  353. if (newShape->myType == NULL)
  354. STOP(("ShapeType NULL. BAD Shape in MAX FILE"));
  355. newShape->aRGBHighlight = 0x00000000;
  356. newShape->lightsOut = false;
  357. newShape->recalcShadows = true;
  358. newShape->shapeScalar = 0.0f;
  359. for (long i=0;i<MAX_SHADOWS;i++)
  360. newShape->shadowsVisible[i] = false;
  361. newShape->isSpotlight = (strnicmp(newShape->getNodeName(),"SpotLight_",10) == 0);
  362. newShape->isWindow = (strnicmp(newShape->getNodeName(),"LitWin_",6) == 0);
  363. if (newShape->isSpotlight)
  364. newShape->noShadow = true;
  365. else
  366. newShape->noShadow = false;
  367. return newShape;
  368. }
  369. //-------------------------------------------------------------------------------
  370. void TG_TypeShape::LoadBinaryCopy (File &binFile)
  371. {
  372. //listOfTypeVertices
  373. numTypeVertices = binFile.readLong();
  374. if (numTypeVertices)
  375. {
  376. listOfTypeVertices = (TG_TypeVertexPtr)TG_Shape::tglHeap->Malloc(sizeof(TG_TypeVertex) * numTypeVertices);
  377. gosASSERT(listOfTypeVertices != NULL);
  378. binFile.read((MemoryPtr)listOfTypeVertices,sizeof(TG_TypeVertex) * numTypeVertices);
  379. }
  380. else
  381. {
  382. listOfTypeVertices = NULL;
  383. }
  384. //listOfTypeTriangles
  385. numTypeTriangles = binFile.readLong();
  386. if (numTypeTriangles)
  387. {
  388. listOfTypeTriangles = (TG_TypeTrianglePtr)TG_Shape::tglHeap->Malloc(sizeof(TG_TypeTriangle) * numTypeTriangles);
  389. gosASSERT(listOfTypeTriangles != NULL);
  390. binFile.read((MemoryPtr)listOfTypeTriangles,sizeof(TG_TypeTriangle) * numTypeTriangles);
  391. }
  392. else
  393. {
  394. listOfTypeTriangles = NULL;
  395. }
  396. //listOfTextures
  397. numTextures = binFile.readLong();
  398. if (numTextures)
  399. {
  400. listOfTextures = (TG_TinyTexturePtr)TG_Shape::tglHeap->Malloc(sizeof(TG_TinyTexture) * numTextures);
  401. gosASSERT(listOfTextures != NULL);
  402. binFile.read((MemoryPtr)listOfTextures,sizeof(TG_TinyTexture) * numTextures);
  403. }
  404. else
  405. {
  406. listOfTextures = NULL;
  407. }
  408. //Other Data
  409. nodeCenter.x = binFile.readFloat();
  410. nodeCenter.y = binFile.readFloat();
  411. nodeCenter.z = binFile.readFloat();
  412. relativeNodeCenter.x = binFile.readFloat();
  413. relativeNodeCenter.y = binFile.readFloat();
  414. relativeNodeCenter.z = binFile.readFloat();
  415. alphaTestOn = false;
  416. filterOn = true;
  417. binFile.read((MemoryPtr)nodeId,TG_NODE_ID);
  418. binFile.read((MemoryPtr)parentId,TG_NODE_ID);
  419. }
  420. //-------------------------------------------------------------------------------
  421. void TG_TypeShape::SaveBinaryCopy (File &binFile)
  422. {
  423. //What kind of thing is this
  424. binFile.writeLong(SHAPE_NODE);
  425. //listOfTypeVertices
  426. binFile.writeLong(numTypeVertices);
  427. if (numTypeVertices)
  428. {
  429. binFile.write((MemoryPtr)listOfTypeVertices,sizeof(TG_TypeVertex) * numTypeVertices);
  430. }
  431. //listOfTypeTriangles
  432. binFile.writeLong(numTypeTriangles);
  433. if (numTypeTriangles)
  434. {
  435. binFile.write((MemoryPtr)listOfTypeTriangles,sizeof(TG_TypeTriangle) * numTypeTriangles);
  436. }
  437. //listOfTextures
  438. binFile.writeLong(numTextures);
  439. if (numTextures)
  440. {
  441. binFile.write((MemoryPtr)listOfTextures,sizeof(TG_TinyTexture) * numTextures);
  442. }
  443. //Other Data
  444. binFile.writeFloat(nodeCenter.x);
  445. binFile.writeFloat(nodeCenter.y);
  446. binFile.writeFloat(nodeCenter.z);
  447. binFile.writeFloat(relativeNodeCenter.x);
  448. binFile.writeFloat(relativeNodeCenter.y);
  449. binFile.writeFloat(relativeNodeCenter.z);
  450. binFile.write((MemoryPtr)nodeId,TG_NODE_ID);
  451. binFile.write((MemoryPtr)parentId,TG_NODE_ID);
  452. }
  453. //-------------------------------------------------------------------------------
  454. //Frees memory and resets locals to defaults.
  455. void TG_TypeShape::destroy (void)
  456. {
  457. if (listOfTypeVertices)
  458. TG_Shape::tglHeap->Free(listOfTypeVertices);
  459. listOfTypeVertices = NULL;
  460. if (listOfTypeTriangles)
  461. TG_Shape::tglHeap->Free(listOfTypeTriangles);
  462. listOfTypeTriangles = NULL;
  463. if (listOfTextures)
  464. TG_Shape::tglHeap->Free(listOfTextures);
  465. listOfTextures = NULL;
  466. numTypeVertices = numTypeTriangles = numTextures = 0;
  467. }
  468. //-------------------------------------------------------------------------------
  469. //Frees memory and resets locals to defaults.
  470. void TG_Shape::destroy (void)
  471. {
  472. //THIS IS CORRECT!!!!
  473. // They come from a common pool now, Deleteing them would be BAD!!
  474. listOfVertices = NULL;
  475. listOfColors = NULL;
  476. listOfTriangles = NULL;
  477. listOfVisibleFaces = NULL;
  478. listOfVisibleShadows = NULL;
  479. listOfShadowTVertices = NULL;
  480. if (listOfShadowVertices)
  481. tglHeap->Free(listOfShadowVertices);
  482. listOfShadowVertices = NULL;
  483. numVertices = numTriangles = numVisibleFaces = 0;
  484. }
  485. //-------------------------------------------------------------------------------
  486. //Function return 0 is OK. -1 if file is not ASE Format or missing data.
  487. //This function simply parses the ASE buffers handed to it. This allows
  488. //users to load the ase file themselves and manage their own memory for it.
  489. //It allocates memory for internal Lists. These are straight tglHeap->Mallocs at present.
  490. //
  491. // NOTE: Only takes the first HELPOBJECT from the ASE file. Multi-object
  492. // Files will require user intervention to parse!!
  493. long TG_TypeShape::MakeFromHelper (BYTE *aseBuffer, char *fileName)
  494. {
  495. //------------------------------------------
  496. // Store off the Node Names.
  497. char *nodeName = strstr((char *)aseBuffer,ASE_NODE_NAME);
  498. gosASSERT(nodeName != NULL);
  499. char nodeString[1024];
  500. nodeName += strlen(ASE_NODE_NAME)+1;
  501. GetNameData(nodeName,nodeString);
  502. #ifdef _DEBUG
  503. if (strlen(nodeString) >= TG_NODE_ID)
  504. {
  505. if (!silentMode)
  506. PAUSE(("WARNING: Node ID %s in Shape %s is greater then 24 characters!!",nodeString,fileName));
  507. }
  508. #endif
  509. strncpy(nodeId,nodeString,24);
  510. char* parentName = strstr((char *)nodeName,ASE_NODE_PARENT);
  511. //-------------------------------------------------------------------
  512. // Must also check to make sure we HAVE a parent.
  513. // We will get the next GeomObject's parent if we don't check length!
  514. if ((parentName != NULL) && ((parentName - nodeName) < MAX_SCAN_LENGTH))
  515. {
  516. parentName += strlen(ASE_NODE_PARENT)+1;
  517. GetNameData(parentName,nodeString);
  518. strncpy(parentId,nodeString,24);
  519. }
  520. else
  521. {
  522. strcpy(parentId,"None");
  523. }
  524. //----------------------------------------------------
  525. // Store off NODE ABS position for heirarchy
  526. nodeName = strstr((char *)aseBuffer,ASE_NODE_POS);
  527. gosASSERT(nodeName != NULL);
  528. nodeName += strlen(ASE_NODE_POS)+1;
  529. char numData[512];
  530. GetNumberData(nodeName,numData);
  531. nodeCenter.x = -(float)atof(numData);
  532. nodeName += strlen(numData)+1;
  533. GetNumberData(nodeName,numData);
  534. nodeCenter.z = (float)atof(numData);
  535. nodeName += strlen(numData)+1;
  536. GetNumberData(nodeName,numData);
  537. nodeCenter.y = (float)atof(numData);
  538. //----------------------------------------
  539. // Find the Number of Vertices
  540. numTypeVertices = 0;
  541. //---------------------------------------
  542. // Find the Number of faces
  543. numTypeTriangles = 0;
  544. return(0);
  545. }
  546. //-------------------------------------------------------------------------------
  547. //Function return 0 is OK. -1 if file is not ASE Format or missing data.
  548. //This function simply parses the ASE buffers handed to it. This allows
  549. //users to load the ase file themselves and manage their own memory for it.
  550. //It allocates memory for internal Lists. These are straight tglHeap->Mallocs at present.
  551. //
  552. // NOTE: Only takes the first GEOMOBJECT from the ASE file. Multi-object
  553. // Files will require user intervention to parse!!
  554. long TG_TypeShape::ParseASEFile (BYTE *aseBuffer, char *fileName)
  555. {
  556. //------------------------------------------
  557. // Store off the Node Names.
  558. char *nodeName = strstr((char *)aseBuffer,ASE_NODE_NAME);
  559. gosASSERT(nodeName != NULL);
  560. char nodeString[1024];
  561. nodeName += strlen(ASE_NODE_NAME)+1;
  562. GetNameData(nodeName,nodeString);
  563. #ifdef _DEBUG
  564. if (strlen(nodeString) >= TG_NODE_ID)
  565. {
  566. if (!silentMode)
  567. PAUSE(("WARNING: Node ID %s in Shape %s is greater then 24 characters!!",nodeString,fileName));
  568. }
  569. #endif
  570. strncpy(nodeId,nodeString,24);
  571. char* parentName = strstr((char *)nodeName,ASE_NODE_PARENT);
  572. //-------------------------------------------------------------------
  573. // Must also check to make sure we HAVE a parent.
  574. // We will get the next GeomObject's parent if we don't check length!
  575. if ((parentName != NULL) && ((parentName - nodeName) < MAX_SCAN_LENGTH))
  576. {
  577. parentName += strlen(ASE_NODE_PARENT)+1;
  578. GetNameData(parentName,nodeString);
  579. strncpy(parentId,nodeString,24);
  580. }
  581. else
  582. {
  583. strcpy(parentId,"None");
  584. }
  585. //----------------------------------------------------
  586. // Store off NODE ABS position for heirarchy
  587. nodeName = strstr((char *)aseBuffer,ASE_NODE_POS);
  588. gosASSERT(nodeName != NULL);
  589. nodeName += strlen(ASE_NODE_POS)+1;
  590. char numData[512];
  591. GetNumberData(nodeName,numData);
  592. nodeCenter.x = -(float)atof(numData);
  593. nodeName += strlen(numData)+1;
  594. GetNumberData(nodeName,numData);
  595. nodeCenter.z = (float)atof(numData);
  596. nodeName += strlen(numData)+1;
  597. GetNumberData(nodeName,numData);
  598. nodeCenter.y = (float)atof(numData);
  599. //----------------------------------------
  600. // Find the Number of Vertices
  601. char *vertexCount = strstr((char *)aseBuffer,ASE_NUM_VERTEX);
  602. gosASSERT(vertexCount != NULL);
  603. vertexCount += strlen(ASE_NUM_FACE)+1;
  604. numTypeVertices = atol(vertexCount);
  605. if (numTypeVertices == 0) //NO Data for this node. WARN and then do NOT allocate anything!
  606. {
  607. #ifdef _DEBUG
  608. if (!silentMode)
  609. PAUSE(("WARNING: No Vertices for Shape Node %s in Shape %s!",nodeId,fileName));
  610. #endif
  611. init();
  612. return (0);
  613. }
  614. //---------------------------------------
  615. // Find the Number of faces
  616. char *faceCount = strstr((char *)aseBuffer,ASE_NUM_FACE);
  617. gosASSERT(faceCount != NULL);
  618. faceCount += strlen(ASE_NUM_FACE)+1;
  619. numTypeTriangles = atol(faceCount);
  620. gosASSERT(numTypeTriangles != 0);
  621. //-------------------
  622. // Vertex Data Next.
  623. listOfTypeVertices = (TG_TypeVertexPtr)TG_Shape::tglHeap->Malloc(sizeof(TG_TypeVertex) * numTypeVertices);
  624. gosASSERT(listOfTypeVertices != NULL);
  625. memset(listOfTypeVertices,0xff,sizeof(TG_TypeVertex) * numTypeVertices);
  626. //---------------------------
  627. // Create vertex normal list
  628. long *vertexNormalCount = (long *)TG_Shape::tglHeap->Malloc(sizeof(long) * numTypeVertices);
  629. gosASSERT(vertexNormalCount != NULL);
  630. memset(vertexNormalCount,0,sizeof(long) * numTypeVertices);
  631. for (long i=0;i<numTypeVertices;i++)
  632. {
  633. char numberData[256];
  634. //------------------------------------------------
  635. // First the Vertex Position
  636. char vertexID[256];
  637. sprintf(vertexID,"%s% 5d",ASE_MESH_VERTEX_ID,i);
  638. char *vertexData = strstr((char *)aseBuffer,vertexID);
  639. gosASSERT(vertexData != NULL);
  640. vertexData += strlen(vertexID)+1;
  641. GetNumberData(vertexData,numberData);
  642. vertexData += strlen(numberData)+1;
  643. listOfTypeVertices[i].position.x = -(float)atof(numberData);
  644. GetNumberData(vertexData,numberData);
  645. vertexData += strlen(numberData)+1;
  646. listOfTypeVertices[i].position.z = (float)atof(numberData);
  647. GetNumberData(vertexData,numberData);
  648. listOfTypeVertices[i].position.y = (float)atof(numberData);
  649. //------------------------------------------------
  650. // Next, Vertex Normal
  651. // These are "funny" because MAX writes out a
  652. // separate normal for each instance of the vertex
  653. // wrt a face. So we must catch EVERY instance
  654. // and average together and then normalize.
  655. // As such, we must now extract during the Face part below
  656. // Just init the normal here!
  657. listOfTypeVertices[i].normal.x =
  658. listOfTypeVertices[i].normal.y =
  659. listOfTypeVertices[i].normal.z = 0.0f;
  660. //-----------------------------------------------------
  661. // Next, Read in Vertex Colors
  662. listOfTypeVertices[i].aRGBLight = 0xff000000;
  663. //-----------------------------------------------------
  664. }
  665. //---------------------------------------
  666. // Face Data Next.
  667. listOfTypeTriangles = (TG_TypeTrianglePtr)TG_Shape::tglHeap->Malloc(sizeof(TG_TypeTriangle) * numTypeTriangles);
  668. gosASSERT(listOfTypeTriangles != NULL);
  669. memset(listOfTypeTriangles,0xff,sizeof(TG_TypeTriangle) * numTypeTriangles);
  670. for (i=0;i<numTypeTriangles;i++)
  671. {
  672. char numberData[256];
  673. //-----------------------------------------------
  674. // First the Vertices for the face
  675. char faceId[256];
  676. sprintf(faceId,"%s% 5d:",ASE_MESH_FACE_ID,i);
  677. char *faceData = strstr((char *)aseBuffer,faceId);
  678. gosASSERT(faceData != NULL);
  679. faceData = strstr(faceData,"A:");
  680. gosASSERT(faceData != NULL);
  681. GetNumberData(faceData,numberData);
  682. listOfTypeTriangles[i].Vertices[0] = atol(numberData);
  683. faceData = strstr(faceData,"B:");
  684. gosASSERT(faceData != NULL);
  685. GetNumberData(faceData,numberData);
  686. listOfTypeTriangles[i].Vertices[1] = atol(numberData);
  687. faceData = strstr(faceData,"C:");
  688. gosASSERT(faceData != NULL);
  689. GetNumberData(faceData,numberData);
  690. listOfTypeTriangles[i].Vertices[2] = atol(numberData);
  691. if ((numTextures/2) == 1) //We don't need the MATLID, its always 0. Now divBytwo to load shadow texture as well.
  692. listOfTypeTriangles[i].localTextureHandle = 0;
  693. else
  694. {
  695. //Must parse MATLID to find material ID.
  696. faceData = strstr(faceData,ASE_FACE_MATERIAL_ID);
  697. gosASSERT(faceData != NULL);
  698. faceData += strlen(ASE_FACE_MATERIAL_ID)+1;
  699. GetNumberData(faceData,numberData);
  700. long matLid = atol(numberData);
  701. if (matLid >= (numTextures/2))
  702. matLid = (numTextures/2) - 1;
  703. listOfTypeTriangles[i].localTextureHandle = matLid;
  704. }
  705. listOfTypeTriangles[i].renderStateFlags = 0; //No Flags yet.
  706. //---------------------------------------------------------------------
  707. // UVData for the face.
  708. // This should be nummies!
  709. //
  710. //Load up the TFaces
  711. sprintf(faceId,"%s",ASE_NUM_TVFACES);
  712. faceData = strstr((char *)aseBuffer,faceId);
  713. if (faceData)
  714. {
  715. faceData += strlen(faceId);
  716. GetNumberData(faceData,numberData);
  717. Stuff::Vector3D tVFaces;
  718. sprintf(faceId,"%s %d",ASE_MESH_TFACE_ID,i);
  719. faceData = strstr(faceData,faceId);
  720. gosASSERT(faceData != NULL);
  721. faceData += strlen(faceId);
  722. GetNumberData(faceData,numberData);
  723. tVFaces.x = (float)atol(numberData);
  724. faceData += strlen(numberData)+1;
  725. GetNumberData(faceData,numberData);
  726. tVFaces.y = (float)atol(numberData);
  727. faceData += strlen(numberData)+1;
  728. GetNumberData(faceData,numberData);
  729. tVFaces.z = (float)atol(numberData);
  730. //Load up the TVERT0
  731. sprintf(faceId,"%s",ASE_NUM_TVERTEX);
  732. faceData = strstr((char *)aseBuffer,faceId);
  733. gosASSERT(faceData != NULL);
  734. sprintf(faceId,"%s %d",ASE_MESH_TVERT_ID,(long)tVFaces.x);
  735. faceData = strstr(faceData,faceId);
  736. gosASSERT(faceData != NULL);
  737. faceData += strlen(faceId);
  738. GetNumberData(faceData,numberData);
  739. listOfTypeTriangles[i].uvdata.u0 = (float)atof(numberData);
  740. if ((listOfTypeTriangles[i].uvdata.u0 > 100.0f) || (listOfTypeTriangles[i].uvdata.u0 < -100.0f))
  741. {
  742. #ifdef _DEBUG
  743. if (!silentMode)
  744. PAUSE(("WARNING: U0 is %f which is out of range in Shape %s!",listOfTypeTriangles[i].uvdata.u0,fileName));
  745. #endif
  746. listOfTypeTriangles[i].uvdata.u0 = 0.0f;
  747. }
  748. faceData += strlen(numberData)+1;
  749. GetNumberData(faceData,numberData);
  750. listOfTypeTriangles[i].uvdata.v0 = 1.0f - (float)atof(numberData);
  751. if ((listOfTypeTriangles[i].uvdata.v0 > 100.0f) || (listOfTypeTriangles[i].uvdata.v0 < -100.0f))
  752. {
  753. #ifdef _DEBUG
  754. if (!silentMode)
  755. PAUSE(("WARNING: V0 is %f which is out of range in Shape %s!",listOfTypeTriangles[i].uvdata.v0,fileName));
  756. #endif
  757. listOfTypeTriangles[i].uvdata.v0 = 0.0f;
  758. }
  759. //Load up the TVERT1
  760. sprintf(faceId,"%s",ASE_NUM_TVERTEX);
  761. faceData = strstr((char *)aseBuffer,faceId);
  762. gosASSERT(faceData != NULL);
  763. sprintf(faceId,"%s %d",ASE_MESH_TVERT_ID,(long)tVFaces.y);
  764. faceData = strstr(faceData,faceId);
  765. gosASSERT(faceData != NULL);
  766. faceData += strlen(faceId);
  767. GetNumberData(faceData,numberData);
  768. listOfTypeTriangles[i].uvdata.u1 = (float)atof(numberData);
  769. if ((listOfTypeTriangles[i].uvdata.u1 > 100.0f) || (listOfTypeTriangles[i].uvdata.u1 < -100.0f))
  770. {
  771. #ifdef _DEBUG
  772. if (!silentMode)
  773. PAUSE(("WARNING: U1 is %f which is out of range in Shape %s!",listOfTypeTriangles[i].uvdata.u1,fileName));
  774. #endif
  775. listOfTypeTriangles[i].uvdata.u1 = 0.0f;
  776. }
  777. faceData += strlen(numberData)+1;
  778. GetNumberData(faceData,numberData);
  779. listOfTypeTriangles[i].uvdata.v1 = 1.0f - (float)atof(numberData);
  780. if ((listOfTypeTriangles[i].uvdata.v1 > 100.0f) || (listOfTypeTriangles[i].uvdata.v1 < -100.0f))
  781. {
  782. #ifdef _DEBUG
  783. if (!silentMode)
  784. PAUSE(("WARNING: V1 is %f which is out of range in Shape %s!",listOfTypeTriangles[i].uvdata.v1,fileName));
  785. #endif
  786. listOfTypeTriangles[i].uvdata.v1 = 0.0f;
  787. }
  788. //Load up the TVERT2
  789. sprintf(faceId,"%s",ASE_NUM_TVERTEX);
  790. faceData = strstr((char *)aseBuffer,faceId);
  791. gosASSERT(faceData != NULL);
  792. sprintf(faceId,"%s %d",ASE_MESH_TVERT_ID,(long)tVFaces.z);
  793. faceData = strstr(faceData,faceId);
  794. gosASSERT(faceData != NULL);
  795. faceData += strlen(faceId);
  796. GetNumberData(faceData,numberData);
  797. listOfTypeTriangles[i].uvdata.u2 = (float)atof(numberData);
  798. if ((listOfTypeTriangles[i].uvdata.u2 > 100.0f) || (listOfTypeTriangles[i].uvdata.u2 < -100.0f))
  799. {
  800. #ifdef _DEBUG
  801. if (!silentMode)
  802. PAUSE(("WARNING: U2 is %f which is out of range in Shape %s!",listOfTypeTriangles[i].uvdata.u2,fileName));
  803. #endif
  804. listOfTypeTriangles[i].uvdata.u2 = 0.0f;
  805. }
  806. faceData += strlen(numberData)+1;
  807. GetNumberData(faceData,numberData);
  808. listOfTypeTriangles[i].uvdata.v2 = 1.0f - (float)atof(numberData);
  809. if ((listOfTypeTriangles[i].uvdata.v2 > 100.0f) || (listOfTypeTriangles[i].uvdata.v2 < -100.0f))
  810. {
  811. #ifdef _DEBUG
  812. if (!silentMode)
  813. PAUSE(("WARNING: V2 is %f which is out of range in Shape %s!",listOfTypeTriangles[i].uvdata.v2,fileName));
  814. #endif
  815. listOfTypeTriangles[i].uvdata.v2 = 0.0f;
  816. }
  817. }
  818. else
  819. {
  820. listOfTypeTriangles[i].uvdata.u0 =
  821. listOfTypeTriangles[i].uvdata.v0 =
  822. listOfTypeTriangles[i].uvdata.u1 =
  823. listOfTypeTriangles[i].uvdata.v1 =
  824. listOfTypeTriangles[i].uvdata.u2 =
  825. listOfTypeTriangles[i].uvdata.v2 = 0.0f;
  826. }
  827. //------------------------------------------------------------
  828. //---------------------------------------------------------------------
  829. // Color Data for the face.
  830. //Load up the CFaces
  831. sprintf(faceId,"%s",ASE_NUM_CVFACES);
  832. faceData = strstr((char *)aseBuffer,faceId);
  833. if (faceData)
  834. {
  835. gosASSERT(faceData != NULL);
  836. faceData += strlen(faceId);
  837. GetNumberData(faceData,numberData);
  838. Stuff::Vector3D cVFaces;
  839. sprintf(faceId,"%s %d",ASE_MESH_CFACE_ID,i);
  840. faceData = strstr(faceData,faceId);
  841. gosASSERT(faceData != NULL);
  842. faceData += strlen(faceId);
  843. GetNumberData(faceData,numberData);
  844. cVFaces.x = (float)atol(numberData);
  845. faceData += strlen(numberData)+1;
  846. GetNumberData(faceData,numberData);
  847. cVFaces.y = (float)atol(numberData);
  848. faceData += strlen(numberData)+1;
  849. GetNumberData(faceData,numberData);
  850. cVFaces.z = (float)atol(numberData);
  851. //Load up the VERTCOL0
  852. sprintf(faceId,"%s",ASE_NUM_CVERTEX);
  853. faceData = strstr((char *)aseBuffer,faceId);
  854. gosASSERT(faceData != NULL);
  855. sprintf(faceId,"%s %d",ASE_MESH_VERTCOL_ID,(long)cVFaces.x);
  856. faceData = strstr(faceData,faceId);
  857. gosASSERT(faceData != NULL);
  858. faceData += strlen(faceId);
  859. GetNumberData(faceData,numberData);
  860. float red = (float)atof(numberData);
  861. faceData += strlen(numberData)+1;
  862. GetNumberData(faceData,numberData);
  863. float green = (float)atof(numberData);
  864. faceData += strlen(numberData)+1;
  865. GetNumberData(faceData,numberData);
  866. float blue = (float)atof(numberData);
  867. DWORD redC = red * 0xff;
  868. DWORD greenC = green * 0xff;
  869. DWORD blueC = blue * 0xff;
  870. listOfTypeVertices[listOfTypeTriangles[i].Vertices[0]].aRGBLight = (0xff << 24) + (redC << 16) + (greenC << 8) + blueC;
  871. //Load up the VERTCOL1
  872. sprintf(faceId,"%s",ASE_NUM_CVERTEX);
  873. faceData = strstr((char *)aseBuffer,faceId);
  874. gosASSERT(faceData != NULL);
  875. sprintf(faceId,"%s %d",ASE_MESH_VERTCOL_ID,(long)cVFaces.y);
  876. faceData = strstr(faceData,faceId);
  877. gosASSERT(faceData != NULL);
  878. faceData += strlen(faceId);
  879. GetNumberData(faceData,numberData);
  880. red = (float)atof(numberData);
  881. faceData += strlen(numberData)+1;
  882. GetNumberData(faceData,numberData);
  883. green = (float)atof(numberData);
  884. faceData += strlen(numberData)+1;
  885. GetNumberData(faceData,numberData);
  886. blue = (float)atof(numberData);
  887. redC = red * 0xff;
  888. greenC = green * 0xff;
  889. blueC = blue * 0xff;
  890. listOfTypeVertices[listOfTypeTriangles[i].Vertices[1]].aRGBLight = (0xff << 24) + (redC << 16) + (greenC << 8) + blueC;
  891. //Load up the VERTCOL2
  892. sprintf(faceId,"%s",ASE_NUM_CVERTEX);
  893. faceData = strstr((char *)aseBuffer,faceId);
  894. gosASSERT(faceData != NULL);
  895. sprintf(faceId,"%s %d",ASE_MESH_VERTCOL_ID,(long)cVFaces.z);
  896. faceData = strstr(faceData,faceId);
  897. gosASSERT(faceData != NULL);
  898. faceData += strlen(faceId);
  899. GetNumberData(faceData,numberData);
  900. red = (float)atof(numberData);
  901. faceData += strlen(numberData)+1;
  902. GetNumberData(faceData,numberData);
  903. green = (float)atof(numberData);
  904. faceData += strlen(numberData)+1;
  905. GetNumberData(faceData,numberData);
  906. blue = (float)atof(numberData);
  907. redC = red * 0xff;
  908. greenC = green * 0xff;
  909. blueC = blue * 0xff;
  910. listOfTypeVertices[listOfTypeTriangles[i].Vertices[2]].aRGBLight = (0xff << 24) + (redC << 16) + (greenC << 8) + blueC;
  911. }
  912. else
  913. {
  914. listOfTypeVertices[listOfTypeTriangles[i].Vertices[0]].aRGBLight =
  915. listOfTypeVertices[listOfTypeTriangles[i].Vertices[1]].aRGBLight =
  916. listOfTypeVertices[listOfTypeTriangles[i].Vertices[2]].aRGBLight = 0xff000000;
  917. }
  918. //------------------------------------------------------------
  919. //------------------------------------------------------------
  920. // Dig out the face Normal.
  921. sprintf(faceId,"%s %d",ASE_FACE_NORMAL_ID,i);
  922. faceData = strstr((char *)aseBuffer,faceId);
  923. gosASSERT(faceData != NULL);
  924. faceData += strlen(faceId);
  925. GetNumberData(faceData,numberData);
  926. listOfTypeTriangles[i].faceNormal.x = -(float)atof(numberData);
  927. faceData += strlen(numberData)+1;
  928. GetNumberData(faceData,numberData);
  929. listOfTypeTriangles[i].faceNormal.z = (float)atof(numberData);
  930. faceData += strlen(numberData)+1;
  931. GetNumberData(faceData,numberData);
  932. listOfTypeTriangles[i].faceNormal.y = (float)atof(numberData);
  933. //------------------------------------------------------------
  934. char vertexID[256];
  935. sprintf(vertexID,"%s",ASE_VERTEX_NORMAL_ID);
  936. char *vertexData = strstr((char *)faceData,vertexID);
  937. GetNumberData(vertexData,numberData);
  938. long vertexNum = atol(numberData);
  939. vertexNormalCount[vertexNum]++;
  940. vertexData += strlen(vertexID)+strlen(numberData)+1;
  941. GetNumberData(vertexData,numberData);
  942. vertexData += strlen(numberData)+1;
  943. listOfTypeVertices[vertexNum].normal.x += -(float)atof(numberData);
  944. GetNumberData(vertexData,numberData);
  945. vertexData += strlen(numberData)+1;
  946. listOfTypeVertices[vertexNum].normal.z += (float)atof(numberData);
  947. GetNumberData(vertexData,numberData);
  948. listOfTypeVertices[vertexNum].normal.y += (float)atof(numberData);
  949. vertexData = strstr((char *)vertexData,vertexID);
  950. GetNumberData(vertexData,numberData);
  951. vertexNum = atol(numberData);
  952. vertexNormalCount[vertexNum]++;
  953. vertexData += strlen(vertexID)+strlen(numberData)+1;
  954. GetNumberData(vertexData,numberData);
  955. vertexData += strlen(numberData)+1;
  956. listOfTypeVertices[vertexNum].normal.x += -(float)atof(numberData);
  957. GetNumberData(vertexData,numberData);
  958. vertexData += strlen(numberData)+1;
  959. listOfTypeVertices[vertexNum].normal.z += (float)atof(numberData);
  960. GetNumberData(vertexData,numberData);
  961. listOfTypeVertices[vertexNum].normal.y += (float)atof(numberData);
  962. vertexData = strstr((char *)vertexData,vertexID);
  963. GetNumberData(vertexData,numberData);
  964. vertexNum = atol(numberData);
  965. vertexNormalCount[vertexNum]++;
  966. vertexData += strlen(vertexID)+strlen(numberData)+1;
  967. GetNumberData(vertexData,numberData);
  968. vertexData += strlen(numberData)+1;
  969. listOfTypeVertices[vertexNum].normal.x += -(float)atof(numberData);
  970. GetNumberData(vertexData,numberData);
  971. vertexData += strlen(numberData)+1;
  972. listOfTypeVertices[vertexNum].normal.z += (float)atof(numberData);
  973. GetNumberData(vertexData,numberData);
  974. listOfTypeVertices[vertexNum].normal.y += (float)atof(numberData);
  975. //------------------------------------------------------------
  976. }
  977. //-----------------------------------------------
  978. // Must average together the vertexNormals
  979. for (i=0;i<numTypeVertices;i++)
  980. {
  981. if (vertexNormalCount[i] == 0)
  982. {
  983. listOfTypeVertices[i].normal.x = 0.0f;
  984. listOfTypeVertices[i].normal.y = 0.0f;
  985. listOfTypeVertices[i].normal.z = 0.0f;
  986. }
  987. else
  988. {
  989. listOfTypeVertices[i].normal.x /= float(vertexNormalCount[i]);
  990. listOfTypeVertices[i].normal.y /= float(vertexNormalCount[i]);
  991. listOfTypeVertices[i].normal.z /= float(vertexNormalCount[i]);
  992. if (listOfTypeVertices[i].normal.GetLength() > Stuff::SMALL)
  993. listOfTypeVertices[i].normal.Normalize(listOfTypeVertices[i].normal);
  994. }
  995. }
  996. TG_Shape::tglHeap->Free(vertexNormalCount);
  997. return(0);
  998. }
  999. //-------------------------------------------------------------------------------
  1000. //Function returns 0 if OK. -1 if file not found or file not ASE Format.
  1001. //This function loads the ASE file into the TG_Triangle and TG_Vertex lists.
  1002. //It allocates memory. These are straight tglHeap->Mallocs at present.
  1003. //
  1004. // NOTE: Only takes the first GEOMOBJECT from the ASE file. Multi-object
  1005. // Files will require user intervention to parse!!
  1006. long TG_TypeShape::LoadTGShapeFromASE (char *fileName)
  1007. {
  1008. //-----------------------------------------------------
  1009. // Fully loads and parses an ASE File. These files are
  1010. // output by 3D Studio MAX's ASCII Scene Exporter.
  1011. //------------------------------------------
  1012. // Check if exists by getting size
  1013. long aseFileSize = gos_FileSize(fileName);
  1014. gosASSERT(aseFileSize > 0);
  1015. //---------------------------------------
  1016. // Create Buffer to read entire file into
  1017. BYTE *aseContents = (BYTE *)TG_Shape::tglHeap->Malloc(aseFileSize);
  1018. gosASSERT(aseContents != NULL);
  1019. //---------------------------------------
  1020. // Open File and read it and close it
  1021. HGOSFILE aseFile;
  1022. gos_OpenFile(&aseFile,fileName,READONLY);
  1023. #ifdef _DEBUG
  1024. long dataRead =
  1025. #endif
  1026. gos_ReadFile(aseFile,aseContents,aseFileSize);
  1027. gosASSERT(dataRead == aseFileSize);
  1028. gos_CloseFile(aseFile);
  1029. //----------------------------------------
  1030. // Check for valid ASE Header data.
  1031. gosASSERT(strnicmp(ASE_HEADER,(char *)aseContents,strlen(ASE_HEADER)) == 0);
  1032. //---------------------------------------
  1033. // Find the number of Textures
  1034. char textureId[256];
  1035. char *textureData;
  1036. char numberData[256];
  1037. sprintf(textureId,ASE_MATERIAL_COUNT);
  1038. textureData = strstr((char *)aseContents,textureId);
  1039. if (!textureData)
  1040. return(-1);
  1041. textureData += strlen(ASE_MATERIAL_COUNT)+1;
  1042. GetNumberData(textureData,numberData);
  1043. long numMaterials = atol(numberData);
  1044. numTextures = 0;
  1045. unsigned char *aseBuffer = aseContents;
  1046. for (long nmt=0;nmt<numMaterials;nmt++)
  1047. {
  1048. sprintf(textureId,ASE_MATERIAL_CLASS);
  1049. textureData = strstr((char *)aseBuffer,textureId);
  1050. if (!textureData)
  1051. return(-1);
  1052. textureData += strlen(ASE_MATERIAL_CLASS)+1;
  1053. GetWordData(textureData,numberData);
  1054. if (strstr(numberData,"Standard"))
  1055. {
  1056. numTextures++;
  1057. }
  1058. else if (strstr(numberData,"Multi"))
  1059. {
  1060. //----------------------------------------------------
  1061. //NOT SUPPORTED YET! Multiple Textures per building!
  1062. // Support NOW! Must have trees!
  1063. textureData = strstr(textureData,ASE_SUBMATERIAL_COUNT);
  1064. gosASSERT(textureData != NULL);
  1065. textureData += strlen(ASE_SUBMATERIAL_COUNT);
  1066. GetNumberData(textureData,numberData);
  1067. numTextures += atol(numberData);
  1068. }
  1069. aseBuffer = (unsigned char *)textureData;
  1070. }
  1071. listOfTextures = (TG_TinyTexturePtr)TG_Shape::tglHeap->Malloc(sizeof(TG_TinyTexture) * numTextures);
  1072. gosASSERT(listOfTextures != NULL);
  1073. memset(listOfTextures,0x0,sizeof(TG_TinyTexture) * numTextures);
  1074. for (long i=0;i<numTextures;i++)
  1075. {
  1076. //-------------------------------------------------------------------------------
  1077. // Get and store texture Name. Will need multiple for Multi-Sub if implemented
  1078. // NEVER GET ACTUAL TEXTURES HERE. ALWAYS HAPPENS IN MULTI!!!!!!!!!!
  1079. // Just set 'em!
  1080. listOfTextures[i].mcTextureNodeIndex = 0xffffffff;
  1081. listOfTextures[i].gosTextureHandle = 0xffffffff;
  1082. listOfTextures[i].textureAlpha = false;
  1083. }
  1084. //---------------------------------------------------
  1085. // Calling from top will load just first GEOMOBJECT!
  1086. long parseResult = ParseASEFile(aseContents,fileName);
  1087. TG_Shape::tglHeap->Free(aseContents);
  1088. aseContents = NULL;
  1089. return(parseResult);
  1090. }
  1091. //-------------------------------------------------------------------------------
  1092. //Need this so that Multi-Shapes can let each shape know texture info.
  1093. void TG_TypeShape::CreateListOfTextures (TG_TexturePtr list, DWORD numTxms)
  1094. {
  1095. numTextures = numTxms;
  1096. if (numTextures)
  1097. {
  1098. listOfTextures = (TG_TinyTexturePtr)TG_Shape::tglHeap->Malloc(sizeof(TG_TinyTexture) * numTextures);
  1099. gosASSERT(listOfTextures != NULL);
  1100. for (long i=0;i<numTextures;i++)
  1101. {
  1102. //-------------------------------------------------------------------------------
  1103. // Get and store texture Name. Will need multiple for Multi-Sub if implemented
  1104. // NEVER GET ACTUAL TEXTURES HERE. ALWAYS HAPPENS IN MULTI!!!!!!!!!!
  1105. // Just set 'em!
  1106. listOfTextures[i].mcTextureNodeIndex = list[i].mcTextureNodeIndex;
  1107. listOfTextures[i].gosTextureHandle = list[i].gosTextureHandle;
  1108. listOfTextures[i].textureAlpha = list[i].textureAlpha;
  1109. }
  1110. }
  1111. else
  1112. listOfTextures = NULL;
  1113. }
  1114. //-------------------------------------------------------------------------------
  1115. //Function returns 0 if OK. -1 if textureNum is out of range of numTextures.
  1116. //This function takes the gosTextureHandle passed in and assigns it to the
  1117. //textureNum entry of the listOfTextures;
  1118. long TG_TypeShape::SetTextureHandle (DWORD textureNum, DWORD gosTextureHandle)
  1119. {
  1120. if (textureNum >= numTextures)
  1121. return(-1);
  1122. listOfTextures[textureNum].mcTextureNodeIndex = gosTextureHandle;
  1123. listOfTextures[textureNum].gosTextureHandle = mcTextureManager->get_gosTextureHandle(gosTextureHandle);
  1124. return(0);
  1125. }
  1126. //-------------------------------------------------------------------------------
  1127. //Function returns 0 if OK. -1 if textureNum is out of range of numTextures.
  1128. //This function takes the gosTextureHandle passed in and assigns it to the
  1129. //textureNum entry of the listOfTextures;
  1130. long TG_TypeShape::SetTextureAlpha (DWORD textureNum, bool alphaFlag)
  1131. {
  1132. if (textureNum >= numTextures)
  1133. return(-1);
  1134. listOfTextures[textureNum].textureAlpha = alphaFlag;
  1135. return(0);
  1136. }
  1137. //-------------------------------------------------------------------------------
  1138. // TG_Shape
  1139. void *TG_Shape::operator new (size_t mySize)
  1140. {
  1141. void *result = TG_Shape::tglHeap->Malloc(mySize);
  1142. return result;
  1143. }
  1144. //-------------------------------------------------------------------------------
  1145. void TG_Shape::operator delete (void *us)
  1146. {
  1147. TG_Shape::tglHeap->Free(us);
  1148. }
  1149. //-------------------------------------------------------------------------------
  1150. //This function sets up the camera Matrices for this TG_Shape to transform
  1151. //itself with. These matrices are static and only need to be set once per
  1152. //render pass if the camera does not change for that pass.
  1153. void TG_Shape::SetCameraMatrices (Stuff::LinearMatrix4D *camOrigin, Stuff::Matrix4D *camToClip)
  1154. {
  1155. cameraOrigin = camOrigin;
  1156. cameraToClip = camToClip;
  1157. worldToCamera.Invert(*cameraOrigin);
  1158. worldToClip.Multiply(worldToCamera, *cameraToClip);
  1159. }
  1160. //-------------------------------------------------------------------------------
  1161. void TG_Shape::SetFog (DWORD fRGB, float fStart, float fFull)
  1162. {
  1163. fogColor = fRGB;
  1164. fogStart = fStart;
  1165. fogFull = fFull;
  1166. if (fogStart == fogFull)
  1167. {
  1168. fogStart = -100000.0f;
  1169. fogFull = -99999.0f;
  1170. }
  1171. }
  1172. //-------------------------------------------------------------------------------
  1173. void TG_Shape::SetViewport (float mulX, float mulY, float addX, float addY)
  1174. {
  1175. viewMulX = mulX;
  1176. viewMulY = mulY;
  1177. viewAddX = addX;
  1178. viewAddY = addY;
  1179. }
  1180. //-------------------------------------------------------------------------------
  1181. //This function sets the list of lights used by the TransformShape function
  1182. //to light the shape.
  1183. //Function returns 0 if lightList entries are all OK. -1 otherwise.
  1184. //
  1185. long TG_Shape::SetLightList (TG_LightPtr *lightList, DWORD nLights)
  1186. {
  1187. if (lightList)
  1188. {
  1189. listOfLights = lightList;
  1190. numLights = nLights;
  1191. }
  1192. else
  1193. {
  1194. listOfLights = NULL;
  1195. numLights = 0;
  1196. }
  1197. return 0;
  1198. }
  1199. //-------------------------------------------------------------------------------
  1200. //This function sets the fog values for the shape. Straight fog right now.
  1201. void TG_Shape::SetFogRGB (DWORD fRGB)
  1202. {
  1203. fogRGB = fRGB;
  1204. }
  1205. //-------------------------------------------------------------------------------
  1206. void TG_TypeShape::movePosRelativeCenterNode (void)
  1207. {
  1208. for (long i=0;i<numTypeVertices;i++)
  1209. {
  1210. listOfTypeVertices[i].position -= nodeCenter;
  1211. }
  1212. }
  1213. extern float yawRotation;
  1214. //-------------------------------------------------------------------------------
  1215. //This function does exactly what TranformShape does EXCEPT that the shapeToClip,
  1216. //Lighting and backface matrices have been calculated in the step above this one.
  1217. //This saves enormous processor cycles when matrices are the same and transforms
  1218. //Can just be run from the same matrices without recalcing them!
  1219. //Function returns -1 if all vertex screen positions are off screen.
  1220. //Function returns 0 if any one vertex screen position is off screen.
  1221. //Function returns 1 is all vertex screen positions are on screen.
  1222. // NOTE: THIS IS NOT A RIGOROUS CLIP!!!!!!!!!
  1223. #define MAX_FOG_ELEVATION 100.0f
  1224. #define MAX_FOG_ELEVATION_INV .01f
  1225. #define TGL_WINDOW_THRESHOLD 0.75f
  1226. long TG_Shape::MultiTransformShape (Stuff::Matrix4D *shapeToClip, Stuff::Point3D *backFacePoint, TG_ShapeRecPtr parentNode, bool isHudElement, BYTE alphaValue, bool isClamped)
  1227. {
  1228. if (!numVertices) //WE are the root Shape which may have no shape or a helper shape which defintely has no shape!
  1229. return(1);
  1230. //-------------------------------------------------
  1231. // Transform entire list of vertices.
  1232. // shapeOrigin is ShapeToWorld.
  1233. // invShapeOrigin is WorldToShape.
  1234. bool oneOff = false;
  1235. bool oneOn = false;
  1236. bool isNight = eye->getIsNight();
  1237. float nightFactor = eye->getNightFactor();
  1238. if (lightsOut)
  1239. {
  1240. isNight = false;
  1241. nightFactor = 0.0f;
  1242. }
  1243. if (isSpotlight && !isNight)
  1244. {
  1245. listOfVertices = NULL; //Mark this as untransformed!!
  1246. return(1);
  1247. }
  1248. TG_TypeShapePtr theShape = (TG_TypeShapePtr)myType;
  1249. //At this point, we know we are going to process this shape,
  1250. // Get memory for its components from the pools!
  1251. listOfVertices = vertexPool->getVerticesFromPool(numVertices);
  1252. listOfColors = colorPool->getColorsFromPool(numVertices);
  1253. listOfShadowTVertices = shadowPool->getShadowsFromPool(numVertices);
  1254. listOfTriangles = trianglePool->getTrianglesFromPool(numTriangles);
  1255. listOfVisibleFaces = facePool->getFacesFromPool(numTriangles);
  1256. listOfVisibleShadows = facePool->getFacesFromPool(numTriangles);
  1257. if (!listOfVertices ||
  1258. !listOfColors ||
  1259. !listOfShadowTVertices ||
  1260. !listOfTriangles ||
  1261. !listOfVisibleFaces ||
  1262. !listOfVisibleShadows)
  1263. return(1);
  1264. lastTurnTransformed = turn;
  1265. for (long j=0;j<numVertices;j++)
  1266. {
  1267. Stuff::Point3D pos = theShape->listOfTypeVertices[j].position;
  1268. if (shapeScalar > 0.0f)
  1269. pos *= shapeScalar;
  1270. Stuff::Vector4D xformCoords;
  1271. Stuff::Vector4D screen;
  1272. xformCoords.Multiply(pos,*shapeToClip);
  1273. if (eye->usePerspective)
  1274. {
  1275. //---------------------------------------
  1276. // Perspective Transform
  1277. float rhw = 1.0f;
  1278. if (xformCoords.w != 0.0f)
  1279. rhw = 1.0f / xformCoords.w;
  1280. screen.x = (xformCoords.x * rhw) * viewMulX + viewAddX;
  1281. screen.y = (xformCoords.y * rhw) * viewMulY + viewAddY;
  1282. screen.z = (xformCoords.z * rhw);
  1283. screen.w = fabs(rhw);
  1284. }
  1285. else
  1286. {
  1287. //---------------------------------------
  1288. // Parallel Transform
  1289. screen.x = (1.0f - xformCoords.x) * viewMulX + viewAddX;
  1290. screen.y = (1.0f - xformCoords.y) * viewMulY + viewAddY;
  1291. screen.z = xformCoords.z;
  1292. screen.w = 0.000001f;
  1293. }
  1294. if ((screen.x < 0) || (screen.y < 0) || (screen.x >= viewMulX) || (screen.y >= viewMulY))
  1295. oneOff = TRUE;
  1296. if ((screen.x >= 0) && (screen.y >= 0) && (screen.x < viewMulX) && (screen.y <= viewMulY))
  1297. oneOn = TRUE;
  1298. listOfVertices[j].x = screen.x;
  1299. listOfVertices[j].y = screen.y;
  1300. listOfVertices[j].z = screen.z;
  1301. listOfVertices[j].rhw = screen.w;
  1302. listOfVertices[j].frgb = fogRGB;
  1303. memset(&listOfColors[j],0,sizeof(listOfColors[j]));
  1304. //----------------------------------------------------
  1305. // Lighting goes here.
  1306. DWORD redFinal=0, greenFinal=0, blueFinal=0;
  1307. DWORD redAmb = 0, greenAmb = 0, blueAmb = 0;
  1308. DWORD redSpec=0, greenSpec=0, blueSpec=0;
  1309. if (lighteningLevel > 0)
  1310. {
  1311. redSpec = blueSpec = greenSpec = lighteningLevel;
  1312. }
  1313. DWORD startVLight = theShape->listOfTypeVertices[j].aRGBLight;
  1314. if (startVLight == 0xffff00ff) //Hot Pink -- Lit Windows -- ONLY at NIGHT
  1315. {
  1316. if (isNight)
  1317. {
  1318. redFinal = (theShape->hotPinkRGB>>16) & 0x000000ff;
  1319. greenFinal = (theShape->hotPinkRGB>>8) & 0x000000ff;
  1320. blueFinal = (theShape->hotPinkRGB) & 0x000000ff;
  1321. }
  1322. else if (nightFactor > Stuff::SMALL)
  1323. {
  1324. redFinal = (theShape->hotPinkRGB>>16) & 0x000000ff;
  1325. greenFinal = (theShape->hotPinkRGB>>8) & 0x000000ff;
  1326. blueFinal = (theShape->hotPinkRGB) & 0x000000ff;
  1327. redFinal *= nightFactor;
  1328. blueFinal *= nightFactor;
  1329. greenFinal *= nightFactor;
  1330. }
  1331. else //Its not night, paint windows dark grey
  1332. {
  1333. redFinal = 0x2f;
  1334. greenFinal = 0x2f;
  1335. blueFinal = 0x2f;
  1336. }
  1337. }
  1338. else if (startVLight == 0xffffff00) //Hot Yellow -- Outside Building Lights -- ONLY at NIGHT
  1339. {
  1340. if (nightFactor >= 0.75f)
  1341. {
  1342. if (isNight)
  1343. {
  1344. redFinal = (theShape->hotYellowRGB>>16) & 0x000000ff;
  1345. greenFinal = (theShape->hotYellowRGB>>8) & 0x000000ff;
  1346. blueFinal = (theShape->hotYellowRGB) & 0x000000ff;
  1347. }
  1348. else
  1349. {
  1350. redFinal = (theShape->hotYellowRGB>>16) & 0x000000ff;
  1351. greenFinal = (theShape->hotYellowRGB>>8) & 0x000000ff;
  1352. blueFinal = (theShape->hotYellowRGB) & 0x000000ff;
  1353. redFinal *= nightFactor;
  1354. blueFinal *= nightFactor;
  1355. greenFinal *= nightFactor;
  1356. }
  1357. }
  1358. }
  1359. else if (startVLight == 0xff00ff00) //Hot Green -- Building Base Lights -- ONLY at NIGHT
  1360. {
  1361. if (isNight)
  1362. {
  1363. redFinal = (theShape->hotGreenRGB>>16) & 0x000000ff;
  1364. greenFinal = (theShape->hotGreenRGB>>8) & 0x000000ff;
  1365. blueFinal = (theShape->hotGreenRGB) & 0x000000ff;
  1366. }
  1367. else if (nightFactor > Stuff::SMALL)
  1368. {
  1369. redFinal = (theShape->hotGreenRGB>>16) & 0x000000ff;
  1370. greenFinal = (theShape->hotGreenRGB>>8) & 0x000000ff;
  1371. blueFinal = (theShape->hotGreenRGB) & 0x000000ff;
  1372. redFinal *= nightFactor;
  1373. blueFinal *= nightFactor;
  1374. greenFinal *= nightFactor;
  1375. }
  1376. }
  1377. else if (startVLight == 0xffff0000) //Hot Red -- Blink this light.
  1378. {
  1379. }
  1380. else if (startVLight == 0xff0000ff) //Hot Blue -- Blink this light.
  1381. {
  1382. }
  1383. else if (startVLight & 0x00ffffff) //Some other kind of light, just add it in.
  1384. {
  1385. if (!lightsOut)
  1386. {
  1387. redFinal = (startVLight>>16) & 0x000000ff;
  1388. greenFinal = (startVLight>>8) & 0x000000ff;
  1389. blueFinal = (startVLight) & 0x000000ff;
  1390. }
  1391. }
  1392. else if (isHudElement)
  1393. {
  1394. redFinal = blueFinal = greenFinal = 0xff; //Just max out its light.
  1395. }
  1396. if (BaseVertexColor)
  1397. {
  1398. redFinal += ((BaseVertexColor>>16) & 0x000000ff);
  1399. if (redFinal > 0xff)
  1400. redFinal = 0xff;
  1401. greenFinal += ((BaseVertexColor>>8) & 0x000000ff);
  1402. if (greenFinal > 0xff)
  1403. greenFinal = 0xff;
  1404. blueFinal += (BaseVertexColor & 0x000000ff);
  1405. if (blueFinal > 0xff)
  1406. blueFinal = 0xff;
  1407. }
  1408. if (useVertexLighting && (Environment.Renderer != 3))
  1409. {
  1410. if (!isSpotlight && !isWindow)
  1411. {
  1412. for (long i=0;i<numLights;i++)
  1413. {
  1414. if ((listOfLights[i] != NULL) && (listOfLights[i]->active))
  1415. {
  1416. DWORD startLight = listOfLights[i]->GetaRGB();
  1417. switch (listOfLights[i]->lightType)
  1418. {
  1419. case TG_LIGHT_AMBIENT:
  1420. {
  1421. redAmb = ((startLight>>16) & 0x000000ff);
  1422. greenAmb = ((startLight>>8) & 0x000000ff);
  1423. blueAmb = ((startLight) & 0x000000ff);
  1424. }
  1425. break;
  1426. case TG_LIGHT_INFINITE:
  1427. {
  1428. float cosine = lightDir[i].x * theShape->listOfTypeVertices[j].normal.x;
  1429. cosine += lightDir[i].y * theShape->listOfTypeVertices[j].normal.y;
  1430. cosine += lightDir[i].z * theShape->listOfTypeVertices[j].normal.z;
  1431. if (cosine < 0.0f)
  1432. {
  1433. float cos = fabs(cosine);
  1434. float red = float((startLight>>16) & 0x000000ff) * cos;
  1435. float green = float((startLight>>8) & 0x000000ff) * cos;
  1436. float blue = float((startLight) & 0x000000ff) * cos;
  1437. redFinal += float2long(red);
  1438. greenFinal += float2long(green);
  1439. blueFinal += float2long(blue);
  1440. /*
  1441. //-----------------------
  1442. // Run Specular next.
  1443. // Mirror the Light vector.
  1444. // Then DOT with Camera vector.
  1445. // Mul Result by itself 4 times.
  1446. // Mul that result by shinyness.
  1447. // Multiply THAT through color and apply!
  1448. float mirrorScalar = cosine * 2.0f;
  1449. Stuff::Vector3D MirrorVector(listOfVertices[i].normal);
  1450. MirrorVector *= mirrorScalar;
  1451. MirrorVector.Subtract(MirrorVector,lightDir[i]);
  1452. MirrorVector.Normalize(MirrorVector);
  1453. Stuff::Vector3D SpecPoint;
  1454. if (turn > 3)
  1455. SpecPoint.Normalize(*backFacePoint);
  1456. float specular = SpecPoint.x * MirrorVector.x;
  1457. specular += SpecPoint.y * MirrorVector.y;
  1458. specular += SpecPoint.z * MirrorVector.z;
  1459. if (specular < 0.0f)
  1460. {
  1461. //specular = fabs(specular);
  1462. specular *= specular;
  1463. specular *= specular;
  1464. specular *= specular;
  1465. specular *= specular;
  1466. specular *= 1.0f;
  1467. // For now, a shinyness of 1.0f. Perfect reflection!
  1468. // Multiplies will make specular positive!
  1469. float red = float((startLight>>16) & 0x000000ff) * specular;
  1470. float green = float((startLight>>8) & 0x000000ff) * specular;
  1471. float blue = float((startLight) & 0x000000ff) * specular;
  1472. redSpec += (DWORD)red;
  1473. greenSpec += (DWORD)green;
  1474. blueSpec += (DWORD)blue;
  1475. }
  1476. */
  1477. }
  1478. }
  1479. break;
  1480. case TG_LIGHT_INFINITEWITHFALLOFF:
  1481. {
  1482. Stuff::Point3D vertexToLight;
  1483. vertexToLight = lightToShape[i];
  1484. vertexToLight -= theShape->listOfTypeVertices[j].position;
  1485. float length = vertexToLight.GetApproximateLength();
  1486. float falloff = 1.0f;
  1487. float red,green,blue;
  1488. if (listOfLights[i]->GetFalloff(length, falloff))
  1489. {
  1490. float cosine = -(lightDir[i] * (theShape->listOfTypeVertices[j].normal));
  1491. red = float((startLight>>16) & 0x000000ff) * falloff;
  1492. green = float((startLight>>8) & 0x000000ff) * falloff;
  1493. blue = float((startLight) & 0x000000ff) * falloff;
  1494. red *= cosine;
  1495. green *= cosine;
  1496. blue *= cosine;
  1497. redFinal += (DWORD)red;
  1498. greenFinal += (DWORD)green;
  1499. blueFinal += (DWORD)blue;
  1500. }
  1501. }
  1502. break;
  1503. case TG_LIGHT_POINT:
  1504. {
  1505. Stuff::Point3D vertexToLight;
  1506. vertexToLight = lightDir[i];
  1507. float length = vertexToLight.GetApproximateLength();
  1508. if (length > Stuff::SMALL)
  1509. {
  1510. vertexToLight.Normalize(vertexToLight);
  1511. float falloff = 1.0f;
  1512. if (listOfLights[i]->GetFalloff(length, falloff))
  1513. {
  1514. float cosine = vertexToLight * (theShape->listOfTypeVertices[j].normal);
  1515. float red,green,blue;
  1516. if (cosine < 0.0f)
  1517. {
  1518. cosine = fabs(cosine);
  1519. red = float((startLight>>16) & 0x000000ff) * falloff;
  1520. green = float((startLight>>8) & 0x000000ff) * falloff;
  1521. blue = float((startLight) & 0x000000ff) * falloff;
  1522. red *= cosine;
  1523. green *= cosine;
  1524. blue *= cosine;
  1525. redSpec += (DWORD)red;
  1526. greenSpec += (DWORD)green;
  1527. blueSpec += (DWORD)blue;
  1528. }
  1529. }
  1530. }
  1531. else
  1532. {
  1533. //Object is in center of light. BLOW IT OUT!
  1534. float cosine = 1.0f;
  1535. float red,green,blue;
  1536. if (cosine < 0.0f)
  1537. {
  1538. cosine = fabs(cosine);
  1539. red = float((startLight>>16) & 0x000000ff);
  1540. green = float((startLight>>8) & 0x000000ff);
  1541. blue = float((startLight) & 0x000000ff);
  1542. red *= cosine;
  1543. green *= cosine;
  1544. blue *= cosine;
  1545. redSpec += (DWORD)red;
  1546. greenSpec += (DWORD)green;
  1547. blueSpec += (DWORD)blue;
  1548. }
  1549. }
  1550. }
  1551. break;
  1552. case TG_LIGHT_TERRAIN:
  1553. {
  1554. if (useShadows)
  1555. {
  1556. Stuff::Point3D vertexToLight;
  1557. Stuff::Vector3D pos = theShape->listOfTypeVertices[j].position;
  1558. RotateLight(pos,yawRotation);
  1559. vertexToLight.Add(lightDir[i],pos);
  1560. float length = vertexToLight.GetApproximateLength();
  1561. if (length > Stuff::SMALL)
  1562. {
  1563. float falloff = 1.0f;
  1564. if (listOfLights[i]->GetFalloff(length, falloff))
  1565. {
  1566. float red,green,blue;
  1567. red = float((startLight>>16) & 0x000000ff) * falloff;
  1568. green = float((startLight>>8) & 0x000000ff) * falloff;
  1569. blue = float((startLight) & 0x000000ff) * falloff;
  1570. listOfColors[j].redSpec = (DWORD)red;
  1571. listOfColors[j].greenSpec = (DWORD)green;
  1572. listOfColors[j].blueSpec = (DWORD)blue;
  1573. }
  1574. }
  1575. else
  1576. {
  1577. //Object is in center of light. NOTHING HAPPENS WITH THIS KIND!!!
  1578. //Light is already burned in!
  1579. }
  1580. }
  1581. }
  1582. break;
  1583. case TG_LIGHT_SPOT:
  1584. {
  1585. Stuff::Point3D vertexToLight;
  1586. vertexToLight = lightDir[i];
  1587. //-------------------------------------------------
  1588. // Defines the actual spot of light on the ground
  1589. float length = vertexToLight.GetApproximateLength();
  1590. //-------------------------------------------------
  1591. // Defines the REAL direction of the spot light.
  1592. vertexToLight = spotDir[i];
  1593. if (vertexToLight.GetApproximateLength() > Stuff::SMALL)
  1594. vertexToLight.Normalize(vertexToLight);
  1595. else
  1596. length = 99999999999999.0f;
  1597. float falloff = 1.0f;
  1598. if (listOfLights[i]->GetFalloff(length, falloff))
  1599. {
  1600. float cosine = vertexToLight * (theShape->listOfTypeVertices[j].normal);
  1601. float red,green,blue;
  1602. if (cosine < 0.0f)
  1603. {
  1604. cosine = fabs(cosine);
  1605. red = float((startLight>>16) & 0x000000ff) * falloff;
  1606. green = float((startLight>>8) & 0x000000ff) * falloff;
  1607. blue = float((startLight) & 0x000000ff) * falloff;
  1608. red *= cosine;
  1609. green *= cosine;
  1610. blue *= cosine;
  1611. redSpec += (DWORD)red;
  1612. greenSpec += (DWORD)green;
  1613. blueSpec += (DWORD)blue;
  1614. }
  1615. }
  1616. }
  1617. break;
  1618. }
  1619. }
  1620. }
  1621. redFinal += redAmb;
  1622. blueFinal += blueAmb;
  1623. greenFinal += greenAmb;
  1624. }
  1625. if (redFinal > 255)
  1626. redFinal = 255;
  1627. if (greenFinal > 255)
  1628. greenFinal = 255;
  1629. if (blueFinal > 255)
  1630. blueFinal = 255;
  1631. listOfVertices[j].argb = (0xff << 24) + (redFinal << 16) + (greenFinal << 8) + (blueFinal);
  1632. // NEW
  1633. // Add in the pre-calced or calced this frame terrain lights.
  1634. // Terrain lights will only be made ACTIVE when day/night changes.
  1635. // One frame of active is enough to store new values!
  1636. redSpec += listOfColors[j].redSpec;
  1637. greenSpec += listOfColors[j].greenSpec;
  1638. blueSpec += listOfColors[j].blueSpec;
  1639. if (redSpec > 255)
  1640. redSpec = 255;
  1641. if (greenSpec > 255)
  1642. greenSpec = 255;
  1643. if (blueSpec > 255)
  1644. blueSpec = 255;
  1645. }
  1646. else
  1647. {
  1648. listOfVertices[j].argb = (0xff << 24) + (redAmb << 16) + (greenAmb << 8) + (blueAmb);
  1649. }
  1650. DWORD fogValue = (listOfVertices[j].frgb >> 24) & 0x000000ff;
  1651. if (Environment.Renderer != 3)
  1652. {
  1653. if (useFog)
  1654. {
  1655. float relElevation = theShape->listOfTypeVertices[j].position.y;
  1656. relElevation += theShape->relativeNodeCenter.y;
  1657. TG_ShapeRecPtr curNode = parentNode;
  1658. while (curNode && curNode->node)
  1659. {
  1660. relElevation += curNode->node->myType->GetRelativeNodeCenter().y;
  1661. curNode = curNode->parentNode;
  1662. }
  1663. if (relElevation < MAX_FOG_ELEVATION)
  1664. {
  1665. float fogFactor = MAX_FOG_ELEVATION - relElevation;
  1666. if (fogFactor >= 0.0)
  1667. {
  1668. fogFactor /= (MAX_FOG_ELEVATION);
  1669. if (fogFactor <= 1.0)
  1670. {
  1671. fogFactor *= fogFactor;
  1672. fogFactor = 1.0 - fogFactor;
  1673. }
  1674. else
  1675. {
  1676. fogFactor = 0.0;
  1677. }
  1678. fogValue += float2long((0x000000ff - fogValue) * fogFactor);
  1679. }
  1680. }
  1681. else
  1682. {
  1683. fogValue = 0x000000ff;
  1684. }
  1685. listOfVertices[j].frgb = (fogValue << 24) + (redSpec << 16) + (greenSpec << 8) + (blueSpec);
  1686. }
  1687. else
  1688. {
  1689. listOfVertices[j].frgb = (0xff << 24) + (redSpec << 16) + (greenSpec << 8) + (blueSpec);
  1690. }
  1691. }
  1692. else
  1693. {
  1694. listOfVertices[j].frgb = (0xff << 24) + (redSpec << 16) + (greenSpec << 8) + (blueSpec);
  1695. }
  1696. //-------------------
  1697. // Distance FOG now.
  1698. if (useFog && Camera::HazeFactor != 0.0f)
  1699. {
  1700. float fogFactor = 1.0 - Camera::HazeFactor;
  1701. DWORD distFog = float2long(fogFactor * 255.0f);
  1702. if (distFog < fogValue)
  1703. fogValue = distFog;
  1704. listOfVertices[j].frgb = (fogValue << 24) + (redSpec << 16) + (greenSpec << 8) + (blueSpec);
  1705. }
  1706. if (aRGBHighlight)
  1707. {
  1708. DWORD frgb = listOfVertices[j].frgb;
  1709. DWORD fFinal = (frgb>>24) & 0x000000ff;
  1710. DWORD rFinal = (frgb>>16) & 0x000000ff;
  1711. DWORD gFinal = (frgb>>8) & 0x000000ff;
  1712. DWORD bFinal = (frgb) & 0x000000ff;
  1713. rFinal += (aRGBHighlight>>16) & 0x000000ff;
  1714. if (rFinal > 255)
  1715. rFinal = 255;
  1716. gFinal += (aRGBHighlight>>8) & 0x000000ff;
  1717. if (gFinal > 255)
  1718. gFinal = 255;
  1719. bFinal += (aRGBHighlight) & 0x000000ff;;
  1720. if (bFinal > 255)
  1721. bFinal = 255;
  1722. frgb = (fFinal << 24) + (rFinal << 16) + (gFinal << 8) + (bFinal);
  1723. listOfVertices[j].frgb = frgb;
  1724. }
  1725. }
  1726. numVisibleFaces = 0; //Reset Visible Faces
  1727. TG_TypeTrianglePtr tri = &(theShape->listOfTypeTriangles[0]);
  1728. for (j=0;j<numTriangles;j++,tri++)
  1729. {
  1730. //---------------------------------------
  1731. // Mark backfacing those that are.
  1732. float cosine = 0.0;
  1733. cosine = (*backFacePoint * (tri->faceNormal));
  1734. if ((cosine > 0.0f) || isSpotlight)
  1735. {
  1736. listOfVisibleFaces[numVisibleFaces] = j;
  1737. numVisibleFaces++;
  1738. DWORD redFinal = 0, greenFinal = 0, blueFinal = 0;
  1739. DWORD redSpec=0, greenSpec=0, blueSpec=0;
  1740. DWORD redAmb=0, greenAmb=0, blueAmb=0;
  1741. if (useFaceLighting)
  1742. {
  1743. //--------------------------------------------
  1744. // Flat Shade any face which are flat shaded.
  1745. for (long i=0;i<numLights;i++)
  1746. {
  1747. if (listOfLights[i] != NULL)
  1748. {
  1749. DWORD startVLight = listOfLights[i]->GetaRGB();
  1750. switch (listOfLights[i]->lightType)
  1751. {
  1752. case TG_LIGHT_INFINITE:
  1753. {
  1754. if (lightDir[i].GetLength() > Stuff::SMALL)
  1755. lightDir[i].Normalize(lightDir[i]);
  1756. float cosine = -(lightDir[i] * (theShape->listOfTypeTriangles[j].faceNormal));
  1757. if (cosine > 0.0f)
  1758. {
  1759. float red = float((startVLight>>16) & 0x000000ff) * cosine;
  1760. float green = float((startVLight>>8) & 0x000000ff) * cosine;
  1761. float blue = float((startVLight) & 0x000000ff) * cosine;
  1762. redFinal += float2long(red);
  1763. greenFinal += float2long(green);
  1764. blueFinal += float2long(blue);
  1765. }
  1766. }
  1767. break;
  1768. case TG_LIGHT_INFINITEWITHFALLOFF:
  1769. {
  1770. if (lightDir[i].GetLength() > Stuff::SMALL)
  1771. lightDir[i].Normalize(lightDir[i]);
  1772. float cosine = -(lightDir[i] * (theShape->listOfTypeTriangles[j].faceNormal));
  1773. Stuff::Point3D vertexToLight;
  1774. vertexToLight = lightToShape[i];
  1775. vertexToLight -= theShape->listOfTypeVertices[theShape->listOfTypeTriangles[j].Vertices[0]].position;
  1776. float length = vertexToLight.GetApproximateLength();
  1777. float falloff = 1.0f;
  1778. float red,green,blue;
  1779. if ((cosine > 0.0f) && listOfLights[i]->GetFalloff(length, falloff))
  1780. {
  1781. red = float((startVLight>>16) & 0x000000ff) * falloff;
  1782. green = float((startVLight>>8) & 0x000000ff) * falloff;
  1783. blue = float((startVLight) & 0x000000ff) * falloff;
  1784. red *= cosine;
  1785. green *= cosine;
  1786. blue *= cosine;
  1787. redFinal += (DWORD)red;
  1788. greenFinal += (DWORD)green;
  1789. blueFinal += (DWORD)blue;
  1790. }
  1791. }
  1792. break;
  1793. case TG_LIGHT_POINT:
  1794. {
  1795. Stuff::Point3D vertexToLight;
  1796. vertexToLight = lightToShape[i];
  1797. vertexToLight -= theShape->listOfTypeVertices[theShape->listOfTypeTriangles[j].Vertices[0]].position;
  1798. float length = vertexToLight.GetApproximateLength();
  1799. vertexToLight.Normalize(vertexToLight);
  1800. float falloff = 1.0f;
  1801. if (listOfLights[i]->GetFalloff(length, falloff))
  1802. {
  1803. float cosine = -(vertexToLight * (theShape->listOfTypeTriangles[j].faceNormal));
  1804. float red,green,blue;
  1805. if (cosine > 0.0f)
  1806. {
  1807. red = float((startVLight>>16) & 0x000000ff) * falloff;
  1808. green = float((startVLight>>8) & 0x000000ff) * falloff;
  1809. blue = float((startVLight) & 0x000000ff) * falloff;
  1810. red *= cosine;
  1811. green *= cosine;
  1812. blue *= cosine;
  1813. redSpec += (DWORD)red;
  1814. greenSpec += (DWORD)green;
  1815. blueSpec += (DWORD)blue;
  1816. }
  1817. }
  1818. }
  1819. break;
  1820. case TG_LIGHT_AMBIENT:
  1821. {
  1822. redAmb = ((startVLight>>16) & 0x000000ff);
  1823. greenAmb = ((startVLight>>8) & 0x000000ff);
  1824. blueAmb = ((startVLight) & 0x000000ff);
  1825. }
  1826. break;
  1827. }
  1828. }
  1829. }
  1830. redFinal += redAmb;
  1831. blueFinal += blueAmb;
  1832. greenFinal += greenAmb;
  1833. if (redFinal > 255)
  1834. redFinal = 255;
  1835. if (greenFinal > 255)
  1836. greenFinal = 255;
  1837. if (blueFinal > 255)
  1838. blueFinal = 255;
  1839. if (redSpec > 255)
  1840. redSpec = 255;
  1841. if (greenSpec > 255)
  1842. greenSpec = 255;
  1843. if (blueSpec > 255)
  1844. blueSpec = 255;
  1845. }
  1846. for (long i=0;i<3;i++ )
  1847. {
  1848. DWORD argb = listOfVertices[theShape->listOfTypeTriangles[j].Vertices[i]].argb;
  1849. DWORD rFinal = (argb>>16) & 0x000000ff;
  1850. DWORD gFinal = (argb>>8) & 0x000000ff;
  1851. DWORD bFinal = (argb) & 0x000000ff;
  1852. rFinal += redFinal;
  1853. if (rFinal > 255)
  1854. rFinal = 255;
  1855. gFinal += greenFinal;
  1856. if (gFinal > 255)
  1857. gFinal = 255;
  1858. bFinal += blueFinal;
  1859. if (bFinal > 255)
  1860. bFinal = 255;
  1861. listOfTriangles[j].aRGBLight[i] = (alphaValue << 24) + (rFinal << 16) + (gFinal << 8) + (bFinal);
  1862. DWORD frgb = listOfVertices[theShape->listOfTypeTriangles[j].Vertices[i]].frgb;
  1863. DWORD fFinal = (frgb>>24) & 0x000000ff;
  1864. rFinal = (frgb>>16) & 0x000000ff;
  1865. gFinal = (frgb>>8) & 0x000000ff;
  1866. bFinal = (frgb) & 0x000000ff;
  1867. rFinal += (redSpec>>16) & 0x000000ff;
  1868. if (rFinal > 255)
  1869. rFinal = 255;
  1870. gFinal += (greenSpec>>8) & 0x000000ff;
  1871. if (gFinal > 255)
  1872. gFinal = 255;
  1873. bFinal += (blueSpec) & 0x000000ff;
  1874. if (bFinal > 255)
  1875. bFinal = 255;
  1876. listOfTriangles[j].fRGBLight[i] = (fFinal << 24) + (rFinal << 16) + (gFinal << 8) + (bFinal);
  1877. }
  1878. DWORD addFlags = 0;
  1879. if (isHudElement)
  1880. {
  1881. addFlags = MC2_ISCOMPASS;
  1882. }
  1883. if (isClamped)
  1884. {
  1885. addFlags = MC2_ISTERRAIN;
  1886. }
  1887. if (isSpotlight)
  1888. {
  1889. mcTextureManager->addTriangle(0xffffffff,MC2_ISSPOTLGT);
  1890. }
  1891. else if (isWindow)
  1892. {
  1893. mcTextureManager->addTriangle(0xffffffff,MC2_DRAWALPHA);
  1894. }
  1895. else
  1896. {
  1897. //--------------------------------------------------
  1898. // Any sorting we need to do occurs in here as well!
  1899. // So, find out what texture this face is using and mark that texture as needing 3 more vertices.
  1900. if (theShape->listOfTextures[theShape->listOfTypeTriangles[j].localTextureHandle].textureAlpha || (alphaValue != 0xff))
  1901. {
  1902. mcTextureManager->addTriangle(theShape->listOfTextures[theShape->listOfTypeTriangles[j].localTextureHandle].mcTextureNodeIndex,MC2_DRAWALPHA | addFlags);
  1903. }
  1904. else
  1905. {
  1906. mcTextureManager->addTriangle(theShape->listOfTextures[theShape->listOfTypeTriangles[j].localTextureHandle].mcTextureNodeIndex,MC2_DRAWSOLID | addFlags);
  1907. }
  1908. }
  1909. }
  1910. }
  1911. gosASSERT(oneOff || oneOn);
  1912. if (oneOff && !oneOn)
  1913. {
  1914. return(-1);
  1915. }
  1916. else if (oneOff && oneOn)
  1917. {
  1918. return(0);
  1919. }
  1920. else if (!oneOff && oneOn)
  1921. {
  1922. return(1);
  1923. }
  1924. return(-1);
  1925. }
  1926. #define TERRAIN_DEPTH_FUDGE (0.000f)
  1927. //-------------------------------------------------------------------------------
  1928. //This function takes the current listOfVisibleFaces and draws them using
  1929. //gos_DrawTriangle.
  1930. void TG_Shape::Render (float forceZ, bool isHudElement, BYTE alphaValue, bool isClamped)
  1931. {
  1932. if (!renderTGLShapes)
  1933. return;
  1934. if (!numVertices) //WE are the root Shape which may have no shape or a helper shape which defintely has no shape!
  1935. return;
  1936. if (!listOfVertices ||
  1937. !listOfColors ||
  1938. !listOfShadowTVertices ||
  1939. !listOfTriangles ||
  1940. !listOfVisibleFaces ||
  1941. !listOfVisibleShadows ||
  1942. (/*(lastTurnTransformed != (turn-1)) &&*/ (lastTurnTransformed != turn)))
  1943. return;
  1944. if (fogColor != 0xffffffff)
  1945. {
  1946. gos_SetRenderState( gos_State_Fog, (int)&(fogColor));
  1947. }
  1948. else
  1949. {
  1950. gos_SetRenderState( gos_State_Fog, 0);
  1951. }
  1952. TG_TypeShapePtr theShape = (TG_TypeShapePtr)myType;
  1953. gos_SetRenderState( gos_State_AlphaTest, theShape->alphaTestOn);
  1954. gos_SetRenderState( gos_State_Filter, theShape->filterOn ? gos_FilterBiLinear : gos_FilterNone);
  1955. DWORD lastTextureUsed = 0xffffffff;
  1956. bool isNight = eye->getIsNight();
  1957. float nightFactor = eye->getNightFactor();
  1958. if (lightsOut)
  1959. {
  1960. isNight = false;
  1961. nightFactor = 0.0f;
  1962. }
  1963. if (isSpotlight && !isNight)
  1964. return;
  1965. for (long j=0;j<numVisibleFaces;j++)
  1966. {
  1967. if (listOfVisibleFaces[j] < numTriangles)
  1968. {
  1969. TG_TypeTriangle triType = theShape->listOfTypeTriangles[listOfVisibleFaces[j]];
  1970. TG_Triangle tri = listOfTriangles[listOfVisibleFaces[j]];
  1971. gos_VERTEX gVertex[3];
  1972. gVertex[0] = listOfVertices[triType.Vertices[0]];
  1973. gVertex[1] = listOfVertices[triType.Vertices[1]];
  1974. gVertex[2] = listOfVertices[triType.Vertices[2]];
  1975. gVertex[0].u = triType.uvdata.u0;
  1976. gVertex[0].v = triType.uvdata.v0;
  1977. gVertex[0].argb = tri.aRGBLight[0];
  1978. gVertex[1].u = triType.uvdata.u1;
  1979. gVertex[1].v = triType.uvdata.v1;
  1980. gVertex[1].argb = tri.aRGBLight[1];
  1981. gVertex[2].u = triType.uvdata.u2;
  1982. gVertex[2].v = triType.uvdata.v2;
  1983. gVertex[2].argb = tri.aRGBLight[2];
  1984. DWORD addFlags = 0;
  1985. if (isHudElement) //We are a HUD Element like the compass. Mark us as such.
  1986. {
  1987. addFlags = MC2_ISCOMPASS;
  1988. }
  1989. if (isClamped)
  1990. {
  1991. addFlags = MC2_ISTERRAIN;
  1992. }
  1993. if (drawOldWay)
  1994. {
  1995. if (theShape->listOfTextures &&
  1996. (theShape->listOfTextures[triType.localTextureHandle].gosTextureHandle != 0xffffffff))
  1997. {
  1998. if (theShape->listOfTextures[triType.localTextureHandle].gosTextureHandle != lastTextureUsed)
  1999. {
  2000. gos_SetRenderState( gos_State_Texture, theShape->listOfTextures[triType.localTextureHandle].gosTextureHandle );
  2001. if (theShape->listOfTextures[triType.localTextureHandle].textureAlpha)
  2002. {
  2003. gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha);
  2004. gos_SetRenderState( gos_State_AlphaTest, true);
  2005. }
  2006. lastTextureUsed = theShape->listOfTextures[triType.localTextureHandle].gosTextureHandle;
  2007. }
  2008. }
  2009. else
  2010. gos_SetRenderState( gos_State_Texture, 0 );
  2011. if ((gVertex[0].z >= 0.0f) &&
  2012. (gVertex[0].z < 1.0f) &&
  2013. (gVertex[1].z >= 0.0f) &&
  2014. (gVertex[1].z < 1.0f) &&
  2015. (gVertex[2].z >= 0.0f) &&
  2016. (gVertex[2].z < 1.0f))
  2017. {
  2018. //-----------------------------------------------------------------------------
  2019. // Reject Any triangle which has vertices off screeen in software for now.
  2020. // Do real cliping in geometry layer for software and hardware that needs it!
  2021. gos_DrawTriangles(gVertex, 3);
  2022. }
  2023. }
  2024. else
  2025. {
  2026. if ((gVertex[0].z >= 0.0f) &&
  2027. (gVertex[0].z < 1.0f) &&
  2028. (gVertex[1].z >= 0.0f) &&
  2029. (gVertex[1].z < 1.0f) &&
  2030. (gVertex[2].z >= 0.0f) &&
  2031. (gVertex[2].z < 1.0f))
  2032. {
  2033. if ((forceZ >= 0.0f) && (forceZ < 1.0f))
  2034. {
  2035. gVertex[0].z = forceZ;
  2036. gVertex[1].z = forceZ;
  2037. gVertex[2].z = forceZ;
  2038. }
  2039. //Can make this a flag to optimize!
  2040. if (isSpotlight)
  2041. {
  2042. mcTextureManager->addVertices(0xffffffff,gVertex,MC2_ISSPOTLGT);
  2043. }
  2044. else if (isWindow)
  2045. {
  2046. mcTextureManager->addVertices(0xffffffff,gVertex,MC2_DRAWALPHA);
  2047. }
  2048. else
  2049. {
  2050. //--------------------------------------------------
  2051. // Any sorting we need to do occurs in here as well!
  2052. // So, find out what texture this face is using and mark that texture as needing 3 more vertices.
  2053. if (theShape->listOfTextures[triType.localTextureHandle].textureAlpha || (alphaValue != 0xff))
  2054. {
  2055. mcTextureManager->addVertices(theShape->listOfTextures[triType.localTextureHandle].mcTextureNodeIndex,gVertex,MC2_DRAWALPHA | addFlags);
  2056. }
  2057. else
  2058. {
  2059. mcTextureManager->addVertices(theShape->listOfTextures[triType.localTextureHandle].mcTextureNodeIndex,gVertex,MC2_DRAWSOLID | addFlags);
  2060. }
  2061. }
  2062. }
  2063. }
  2064. }
  2065. }
  2066. gos_SetRenderState( gos_State_Fog, 0);
  2067. }
  2068. extern bool InEditor;
  2069. //------------------------------------------------------------------------------------------------------------
  2070. // This function checks each visible triangle against the mouse position and returns if we are over ANY poly!
  2071. bool TG_Shape::PerPolySelect (float mouseX, float mouseY)
  2072. {
  2073. if (!InEditor)
  2074. {
  2075. if (!listOfVertices ||
  2076. !listOfColors ||
  2077. !listOfShadowTVertices ||
  2078. !listOfTriangles ||
  2079. !listOfVisibleFaces ||
  2080. !listOfVisibleShadows ||
  2081. ((lastTurnTransformed != (turn-1)) /*&& (lastTurnTransformed != turn)*/))
  2082. return false;
  2083. }
  2084. else
  2085. {
  2086. if (!listOfVertices ||
  2087. !listOfColors ||
  2088. !listOfShadowTVertices ||
  2089. !listOfTriangles ||
  2090. !listOfVisibleFaces ||
  2091. !listOfVisibleShadows ||
  2092. ((lastTurnTransformed != (turn-1)) && (lastTurnTransformed != turn)))
  2093. return false;
  2094. }
  2095. TG_TypeShapePtr theShape = (TG_TypeShapePtr)myType;
  2096. for (long j=0;j<numVisibleFaces;j++)
  2097. {
  2098. if (listOfVisibleFaces[j] < numTriangles)
  2099. {
  2100. TG_TypeTriangle triType = theShape->listOfTypeTriangles[listOfVisibleFaces[j]];
  2101. Stuff::Point3D v0,v1,v2;
  2102. v0.x = listOfVertices[triType.Vertices[0]].x;
  2103. v0.y = listOfVertices[triType.Vertices[0]].y;
  2104. v1.x = listOfVertices[triType.Vertices[1]].x;
  2105. v1.y = listOfVertices[triType.Vertices[1]].y;
  2106. v2.x = listOfVertices[triType.Vertices[2]].x;
  2107. v2.y = listOfVertices[triType.Vertices[2]].y;
  2108. v0.z = v1.z = v2.z = 0.0f;
  2109. //Using the above vertex Data, determine if the mouse is over this poly!
  2110. //
  2111. //Theorem:
  2112. // Given the sides of the triangle defined by the lines v0v1, v1v2 and v2v0
  2113. // in the form Ax + By + C = 0
  2114. //
  2115. // the point mousex, mousey lies inside the triangle if and only if
  2116. //
  2117. // A0 * mouseX + B0 * mouseY + C0 = D0
  2118. // A1 * mouseX * B1 * mouseY + c1 = D1
  2119. // A2 * mouseX + B2 * mouseY + c2 = D2
  2120. //
  2121. // Dx is the same sign for each line as the correct sign for clockwise or counterclockwise vertices!
  2122. //
  2123. Stuff::Vector3D line1;
  2124. Stuff::Vector3D line2;
  2125. line1.Subtract(v0,v1);
  2126. line2.Subtract(v1,v2);
  2127. float order = line2.x * line1.y - line1.x * line2.y;
  2128. order = sign(order);
  2129. float A0 = -(v0.y - v1.y);
  2130. float B0 = (v0.x - v1.x);
  2131. float C0 = -B0*(v0.y) - A0*(v0.x);
  2132. float D0 = A0 * mouseX + B0 * mouseY + C0;
  2133. float A1 = -(v1.y - v2.y);
  2134. float B1 = (v1.x - v2.x);
  2135. float C1 = -B1*(v1.y) - A1*(v1.x);
  2136. float D1 = A1 * mouseX + B1 * mouseY + C1;
  2137. float A2 = -(v2.y - v0.y);
  2138. float B2 = (v2.x - v0.x);
  2139. float C2 = -B2*(v2.y) - A2*(v2.x);
  2140. float D2 = A2 * mouseX + B2 * mouseY + C2;
  2141. if ((sign(D0) == order) && (sign(D0) == sign(D1)) && (sign(D0) == sign(D2)))
  2142. return true;
  2143. }
  2144. }
  2145. return false;
  2146. }
  2147. #define SHADOW_HARDWARE_BLACK 0xff000000
  2148. #define SHADOW_SOFTWARE_BLACK 0xff000000
  2149. #define FLOAT_SHADOW_HARDWARE_BLACK 128.0f
  2150. //-------------------------------------------------------------------------------
  2151. //This function creates the list of shadows and transforms them in preparation to drawing.
  2152. //
  2153. void TG_Shape::MultiTransformShadows (Stuff::Point3D *pos, Stuff::LinearMatrix4D *s2w, float rotation)
  2154. {
  2155. if (!numVertices) //WE are the root Shape which may have no shape or a helper shape which defintely has no shape!
  2156. return;
  2157. //Can make this a flag to optimize!
  2158. if (noShadow)
  2159. return;
  2160. if (!listOfVertices ||
  2161. !listOfColors ||
  2162. !listOfShadowTVertices ||
  2163. !listOfTriangles ||
  2164. !listOfVisibleFaces ||
  2165. !listOfVisibleShadows)
  2166. return;
  2167. Stuff::Matrix4D identityMatrix;
  2168. identityMatrix.BuildIdentity();
  2169. TG_TypeShapePtr theShape = (TG_TypeShapePtr)myType;
  2170. //-----------------------------------------------------------------
  2171. // Run Through listOfVertices and create the listOfShadowVertices
  2172. // for each light source. Only need to calc infinite rarely if at all.
  2173. // May need some way to delineate this. Above here, probably!
  2174. //
  2175. // As we calc shadow vertices, transform and store results.
  2176. //
  2177. // listOfTriangles will simply use these vertices to draw shadows.
  2178. //
  2179. // Again, note that after first iteration of infinite light we only need to transform infinite shadows!
  2180. Stuff::Matrix4D shapeToLocal;
  2181. Stuff::Matrix4D shapeToClip;
  2182. long shadowNum = 0;
  2183. memset(shadowsVisible,0,sizeof(bool) * MAX_SHADOWS);
  2184. //-------------------------------------------------------
  2185. // Now, for each light IN RANGE
  2186. // Use formula form Blinn-Trip Down the Graphics Pipeline-Chapter 6
  2187. for (long i=0;i<numLights;i++)
  2188. {
  2189. if ((listOfLights[i] != NULL) && (listOfLights[i]->active) && (shadowNum < MAX_SHADOWS))
  2190. {
  2191. switch (listOfLights[i]->lightType)
  2192. {
  2193. case TG_LIGHT_AMBIENT:
  2194. {
  2195. //Ambient light casts no shadows!
  2196. }
  2197. break;
  2198. case TG_LIGHT_INFINITE:
  2199. {
  2200. //----------------------------------------------
  2201. // The Sun casts no shadows at night!!!
  2202. if (!eye->getIsNight())
  2203. {
  2204. Stuff::Vector3D lightDir = rootLightDir[i];
  2205. RotateLight(lightDir,rotation);
  2206. for (long j=0;j<numVertices;j++)
  2207. {
  2208. long index = j + (shadowNum * numVertices);
  2209. //if (!recalcShadows)
  2210. {
  2211. if (listOfShadowVertices[index].bDataIsNotValid)
  2212. {
  2213. recalcShadows = true;
  2214. listOfShadowVertices[index].bDataIsNotValid = false;
  2215. }
  2216. }
  2217. if (recalcShadows) //Otherwise, just project last known DATA!!
  2218. //This should almost never be true for any non-moving object.
  2219. //Movers will recalc every frame. Non-Movers will recalc
  2220. //when the sun moves enough!
  2221. {
  2222. Stuff::Point3D post = theShape->listOfTypeVertices[j].position;
  2223. //--------------------------------------------------
  2224. Stuff::Vector4D up;
  2225. Stuff::Point3D s_position;
  2226. shapeToLocal.Multiply(*s2w,identityMatrix);
  2227. up.Multiply(post,shapeToLocal);
  2228. //Everything is in WORLD coords now.
  2229. //But this assumes terrain is FLAT and at zero elevation.
  2230. // Thus, we must translate up to tangent plane at terrain position
  2231. up.y -= pos->y;
  2232. //--------------------------------------------------------
  2233. // Check simple case first. Vertex is on or below ground.
  2234. if (up.y <= 0.0f)
  2235. {
  2236. s_position.x = up.x;
  2237. s_position.z = up.z;
  2238. }
  2239. else
  2240. {
  2241. float zFactor = up.y / lightDir.y;
  2242. s_position.x = up.x - (zFactor * lightDir.x);
  2243. s_position.z = up.z - (zFactor * lightDir.z);
  2244. }
  2245. s_position.y = 0.0f;
  2246. //-------------------------------------------------------------------
  2247. // Do transform math for the vertex.
  2248. //RotateLight(s_position,rotation);
  2249. //post.Add(s_position,*pos);
  2250. Stuff::Vector3D rPos;
  2251. rPos.x = -s_position.x;
  2252. rPos.y = s_position.z;
  2253. rPos.z = land->getTerrainElevation(rPos);
  2254. listOfShadowVertices[index].position = rPos;
  2255. //--------------------------------------------------------------------------------
  2256. }
  2257. //The transformed position and fog data DO NOT NEED TO EXIST all of time.
  2258. // ONLY the calced position data is invariant. The other two could come
  2259. // from a common pool and save us SCADS of RAM!!
  2260. // Do this!!!! I DID!
  2261. // -fs
  2262. eye->projectZ(listOfShadowVertices[index].position,listOfShadowTVertices[index].transformedPosition);
  2263. shadowsVisible[shadowNum] = true;
  2264. //eye->projectZ(listOfShadowVertices[index].position,listOfShadowTVertices[index].transformedPosition);
  2265. DWORD fogRGB = 0xff000000;
  2266. if (useFog)
  2267. {
  2268. if (pos->y < fogStart)
  2269. {
  2270. float fogFactor = fogStart - pos->y;
  2271. if (fogFactor < 0.0)
  2272. fogRGB = (0xff<<24);
  2273. else
  2274. {
  2275. fogFactor /= (fogStart - fogFull);
  2276. if (fogFactor <= 1.0)
  2277. {
  2278. fogFactor *= fogFactor;
  2279. fogFactor = 1.0 - fogFactor;
  2280. fogFactor *= 256.0;
  2281. }
  2282. else
  2283. {
  2284. fogFactor = 256.0;
  2285. }
  2286. unsigned char fogResult = float2long(fogFactor);
  2287. fogRGB = (fogResult << 24);
  2288. }
  2289. }
  2290. else
  2291. {
  2292. fogRGB = (0xff<<24);
  2293. }
  2294. }
  2295. //-------------------
  2296. // Distance FOG now.
  2297. if (useFog && Camera::HazeFactor != 0.0f)
  2298. {
  2299. float fogFactor = 1.0 - Camera::HazeFactor;
  2300. DWORD distFog = float2long(fogFactor * 255.0f);
  2301. distFog <<= 24;
  2302. if (distFog < fogRGB)
  2303. fogRGB = distFog;
  2304. }
  2305. listOfShadowTVertices[index].fRGBFog = fogRGB;
  2306. }
  2307. shadowNum++;
  2308. }
  2309. }
  2310. break;
  2311. case TG_LIGHT_INFINITEWITHFALLOFF:
  2312. {
  2313. //Not planning to use this type of light anymore. No shadows for now.
  2314. }
  2315. break;
  2316. case TG_LIGHT_POINT:
  2317. case TG_LIGHT_SPOT:
  2318. {
  2319. if (useLocalShadows)
  2320. {
  2321. //------------------------------------------------------------------------------------------
  2322. // Point light does cast a shadow. LightDir Will change every frame relative to the point.
  2323. // I.e. we DO need to recalc every frame.
  2324. //-----------------------------------------------------
  2325. // Check if light source is IN_RANGE!
  2326. Stuff::Point3D vertexToLight;
  2327. vertexToLight = lightDir[i];
  2328. float length = vertexToLight.GetApproximateLength();
  2329. vertexToLight = rootLightDir[i];
  2330. float spotLength = listOfLights[i]->maxSpotLength - vertexToLight.GetApproximateLength();
  2331. if (spotLength < 50.0f)
  2332. spotLength = 50.0f;
  2333. float falloff = 1.0f;
  2334. //Lights do not cast a shadow unless they are intense enough!!
  2335. if (listOfLights[i]->GetFalloff(length, falloff) && (falloff > 0.5f))
  2336. {
  2337. for (long j=0;j<numVertices;j++)
  2338. {
  2339. long index = j + (shadowNum * numVertices);
  2340. Stuff::Point3D post = theShape->listOfTypeVertices[j].position;
  2341. //--------------------------------------------------
  2342. Stuff::Vector4D up;
  2343. Stuff::Point3D s_position;
  2344. shapeToLocal.Multiply(*s2w,identityMatrix);
  2345. up.Multiply(post,shapeToLocal);
  2346. //Everything is in WORLD coords now.
  2347. //But this assumes terrain is FLAT and at zero elevation.
  2348. // Thus, we must translate up to tangent plane at terrain position
  2349. up.y -= pos->y;
  2350. shadowsVisible[shadowNum] = true;
  2351. //--------------------------------------------------------
  2352. // Check simple case first. Vertex is on or below ground.
  2353. if (up.y <= 0.0f)
  2354. {
  2355. s_position.x = up.x;
  2356. s_position.z = up.z;
  2357. }
  2358. else
  2359. {
  2360. float zFactor = up.y / spotLength;
  2361. s_position.x = up.x - (zFactor * -rootLightDir[i].x);
  2362. s_position.z = up.z - (zFactor * -rootLightDir[i].z);
  2363. }
  2364. s_position.y = 0.0f;
  2365. //-------------------------------------------------------------------
  2366. // Do transform math for the vertex.
  2367. //RotateLight(s_position,rotation);
  2368. //post.Add(s_position,*pos);
  2369. Stuff::Vector3D rPos;
  2370. rPos.x = -s_position.x;
  2371. rPos.y = s_position.z;
  2372. rPos.z = land->getTerrainElevation(rPos);
  2373. listOfShadowVertices[index].position = rPos;
  2374. //--------------------------------------------------------------------------------
  2375. //The transformed position and fog data DO NOT NEED TO EXIST all of time.
  2376. // ONLY the calced position data is invariant. The other two could come
  2377. // from a common pool and save us SCADS of RAM!!
  2378. // Do this!!!! I DID!
  2379. // -fs
  2380. eye->projectZ(listOfShadowVertices[index].position,listOfShadowTVertices[index].transformedPosition);
  2381. shadowsVisible[shadowNum] = true;
  2382. DWORD fogRGB = 0xff000000;
  2383. if (useFog)
  2384. {
  2385. if (pos->y < fogStart)
  2386. {
  2387. float fogFactor = fogStart - pos->y;
  2388. if (fogFactor < 0.0)
  2389. fogRGB = (0xff<<24);
  2390. else
  2391. {
  2392. fogFactor /= (fogStart - fogFull);
  2393. if (fogFactor <= 1.0)
  2394. {
  2395. fogFactor *= fogFactor;
  2396. fogFactor = 1.0 - fogFactor;
  2397. fogFactor *= 256.0;
  2398. }
  2399. else
  2400. {
  2401. fogFactor = 256.0;
  2402. }
  2403. unsigned char fogResult = float2long(fogFactor);
  2404. fogRGB = (fogResult << 24);
  2405. }
  2406. }
  2407. else
  2408. {
  2409. fogRGB = (0xff<<24);
  2410. }
  2411. }
  2412. //-------------------
  2413. // Distance FOG now.
  2414. if (useFog && Camera::HazeFactor != 0.0f)
  2415. {
  2416. float fogFactor = 1.0 - Camera::HazeFactor;
  2417. DWORD distFog = float2long(fogFactor * 255.0f);
  2418. distFog <<= 24;
  2419. if (distFog < fogRGB)
  2420. fogRGB = distFog;
  2421. }
  2422. listOfShadowTVertices[index].fRGBFog = fogRGB;
  2423. }
  2424. shadowNum++;
  2425. }
  2426. else
  2427. {
  2428. shadowsVisible[shadowNum] = false;
  2429. }
  2430. }
  2431. }
  2432. break;
  2433. }
  2434. }
  2435. }
  2436. numVisibleShadows = 0; //Reset Visible Shadows
  2437. long totalShadows = 0;
  2438. long j=0;
  2439. for (j=0;j<MAX_SHADOWS;j++)
  2440. if (shadowsVisible[j])
  2441. totalShadows++;
  2442. if (totalShadows)
  2443. {
  2444. for (j=0;j<numTriangles;j++)
  2445. {
  2446. listOfVisibleShadows[numVisibleShadows] = j;
  2447. numVisibleShadows++;
  2448. TG_TypeTriangle triType = theShape->listOfTypeTriangles[j];
  2449. //--------------------------------------------------
  2450. // Any sorting we need to do occurs in here as well!
  2451. // So, find out what texture this face is using and mark that texture as needing 3 more vertices.
  2452. if (theShape->listOfTextures[triType.localTextureHandle + (theShape->numTextures>>1)].gosTextureHandle != 0xffffffff)
  2453. {
  2454. for (long k=0;k<totalShadows;k++)
  2455. mcTextureManager->addTriangle(theShape->listOfTextures[triType.localTextureHandle + (theShape->numTextures>>1)].mcTextureNodeIndex,MC2_DRAWALPHA | MC2_ISSHADOWS);
  2456. }
  2457. else
  2458. {
  2459. for (long k=0;k<totalShadows;k++)
  2460. mcTextureManager->addTriangle(theShape->listOfTextures[triType.localTextureHandle].mcTextureNodeIndex,MC2_DRAWALPHA | MC2_ISSHADOWS);
  2461. }
  2462. }
  2463. }
  2464. }
  2465. //------------------------------------------------------------------------------------------------------------------------------------
  2466. // This functions draws the shadows. May want zCompare and zWrite off at this point, hence separate render to facilitate draw order.
  2467. // So shadows fall across all 3d objects on terrain.
  2468. long TG_Shape::RenderShadows (long startFace)
  2469. {
  2470. if (!renderTGLShapes)
  2471. return(startFace);
  2472. if (!numVertices) //WE are the root Shape which may have no shape or a helper shape which defintely has no shape!
  2473. return(startFace);
  2474. if (!numVisibleShadows)
  2475. return startFace;
  2476. //Can make this a flag to optimize!
  2477. if (strnicmp(getNodeName(),"SpotLight_",10) == 0)
  2478. return startFace;
  2479. if (!listOfVertices ||
  2480. !listOfColors ||
  2481. !listOfShadowTVertices ||
  2482. !listOfTriangles ||
  2483. !listOfVisibleFaces ||
  2484. !listOfVisibleShadows ||
  2485. (/*(lastTurnTransformed != (turn-1)) &&*/ (lastTurnTransformed != turn)))
  2486. return startFace;
  2487. if (fogColor != 0xffffffff)
  2488. {
  2489. gos_SetRenderState( gos_State_Fog, (int)&(fogColor));
  2490. }
  2491. else
  2492. {
  2493. gos_SetRenderState( gos_State_Fog, 0);
  2494. }
  2495. TG_TypeShapePtr theShape = (TG_TypeShapePtr)myType;
  2496. for (long i=0;i<MAX_SHADOWS;i++)
  2497. {
  2498. if (shadowsVisible[i])
  2499. {
  2500. for (long j=0;j<numVisibleShadows && listOfShadowTVertices;j++)
  2501. {
  2502. DWORD realStart = (j + startFace) % 64;
  2503. if (listOfVisibleShadows[j] < numTriangles)
  2504. {
  2505. TG_TypeTriangle triType = theShape->listOfTypeTriangles[listOfVisibleShadows[j]];
  2506. TG_ShadowVertexTemp vertex0 = listOfShadowTVertices[triType.Vertices[0] + (i * numVertices)];
  2507. TG_ShadowVertexTemp vertex1 = listOfShadowTVertices[triType.Vertices[1] + (i * numVertices)];
  2508. TG_ShadowVertexTemp vertex2 = listOfShadowTVertices[triType.Vertices[2] + (i * numVertices)];
  2509. gos_VERTEX gVertex[3];
  2510. gVertex[0].x = vertex0.transformedPosition.x;
  2511. gVertex[0].y = vertex0.transformedPosition.y;
  2512. gVertex[0].z = vertex0.transformedPosition.z + (1.0f/60000.0f)*(realStart);
  2513. gVertex[0].rhw = vertex0.transformedPosition.w;
  2514. gVertex[0].u = triType.uvdata.u0;
  2515. gVertex[0].v = triType.uvdata.v0;
  2516. gVertex[0].argb = 0x3f000000;
  2517. gVertex[0].frgb = vertex0.fRGBFog;
  2518. gVertex[1].x = vertex1.transformedPosition.x;
  2519. gVertex[1].y = vertex1.transformedPosition.y;
  2520. gVertex[1].z = vertex1.transformedPosition.z + (1.0f/60000.0f)*(realStart);
  2521. gVertex[1].rhw = vertex1.transformedPosition.w;
  2522. gVertex[1].u = triType.uvdata.u1;
  2523. gVertex[1].v = triType.uvdata.v1;
  2524. gVertex[1].argb = 0x3f000000;
  2525. gVertex[1].frgb = vertex1.fRGBFog;
  2526. gVertex[2].x = vertex2.transformedPosition.x;
  2527. gVertex[2].y = vertex2.transformedPosition.y;
  2528. gVertex[2].z = vertex2.transformedPosition.z + (1.0f/60000.0f)*(realStart);
  2529. gVertex[2].rhw = vertex2.transformedPosition.w;
  2530. gVertex[2].u = triType.uvdata.u2;
  2531. gVertex[2].v = triType.uvdata.v2;
  2532. gVertex[2].argb = 0x3f000000;
  2533. gVertex[2].frgb = vertex2.fRGBFog;
  2534. if (drawOldWay)
  2535. {
  2536. if (theShape->listOfTextures[triType.localTextureHandle + (theShape->numTextures>>1)].gosTextureHandle != 0xffffffff)
  2537. gos_SetRenderState( gos_State_Texture, theShape->listOfTextures[triType.localTextureHandle + (theShape->numTextures>>1)].gosTextureHandle );
  2538. else if (theShape->listOfTextures[triType.localTextureHandle].gosTextureHandle != 0xffffffff)
  2539. gos_SetRenderState( gos_State_Texture, theShape->listOfTextures[triType.localTextureHandle].gosTextureHandle );
  2540. else
  2541. gos_SetRenderState( gos_State_Texture, 0 );
  2542. if ((gVertex[0].z >= 0.0f) &&
  2543. (gVertex[0].z < 1.0f) &&
  2544. (gVertex[1].z >= 0.0f) &&
  2545. (gVertex[1].z < 1.0f) &&
  2546. (gVertex[2].z >= 0.0f) &&
  2547. (gVertex[2].z < 1.0f))
  2548. {
  2549. gos_DrawTriangles(gVertex, 3);
  2550. }
  2551. }
  2552. else
  2553. {
  2554. if ((gVertex[0].z >= 0.0f) &&
  2555. (gVertex[0].z < 1.0f) &&
  2556. (gVertex[1].z >= 0.0f) &&
  2557. (gVertex[1].z < 1.0f) &&
  2558. (gVertex[2].z >= 0.0f) &&
  2559. (gVertex[2].z < 1.0f))
  2560. {
  2561. //--------------------------------------------------
  2562. // Any sorting we need to do occurs in here as well!
  2563. // So, find out what texture this face is using and mark that texture as needing 3 more vertices.
  2564. if (theShape->listOfTextures[triType.localTextureHandle + (theShape->numTextures>>1)].gosTextureHandle != 0xffffffff)
  2565. {
  2566. mcTextureManager->addVertices(theShape->listOfTextures[triType.localTextureHandle + (theShape->numTextures>>1)].mcTextureNodeIndex,gVertex,MC2_ISSHADOWS | MC2_DRAWALPHA);
  2567. }
  2568. else
  2569. {
  2570. mcTextureManager->addVertices(theShape->listOfTextures[triType.localTextureHandle].mcTextureNodeIndex,gVertex,MC2_ISSHADOWS | MC2_DRAWALPHA);
  2571. }
  2572. }
  2573. }
  2574. }
  2575. }
  2576. }
  2577. }
  2578. gos_SetRenderState( gos_State_Fog, 0);
  2579. return(startFace + numVisibleShadows);
  2580. }
  2581. //-------------------------------------------------------------------------------