dazzle.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WW3D *
  23. * *
  24. * $Archive:: /VSS_Sync/ww3d2/dazzle.cpp $*
  25. * *
  26. * Original Author:: Jani Penttinen *
  27. * *
  28. * $Author:: Vss_sync $*
  29. * *
  30. * $Modtime:: 8/29/01 9:47p $*
  31. * *
  32. * $Revision:: 19 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "dazzle.h"
  38. #include "simplevec.h"
  39. #include "vector2.h"
  40. #include "camera.h"
  41. #include "ww3d.h"
  42. #include "wwstring.h"
  43. #include "wwdebug.h"
  44. #include "assetmgr.h"
  45. #include "vector3i.h"
  46. #include "quat.h"
  47. #include "ini.h"
  48. #include "point.h"
  49. #include "rinfo.h"
  50. #include "vertmaterial.h"
  51. #include "chunkio.h"
  52. #include "wwfile.h"
  53. #include "inisup.h"
  54. #include "persistfactory.h"
  55. #include "ww3dids.h"
  56. #include "dx8wrapper.h"
  57. #include "dx8vertexbuffer.h"
  58. #include "dx8indexbuffer.h"
  59. #include "sortingrenderer.h"
  60. #include "texture.h"
  61. #include "scene.h"
  62. #include "wwprofile.h"
  63. #include <cstdio>
  64. #include <limits.h>
  65. // All dazzle types appear under Dazzles_List in the dazzle.ini file.
  66. const char* DAZZLE_LIST_STRING="Dazzles_List";
  67. // After the dot product between the camera direction and the camera space location of the light source,
  68. // we do a power to Halopow for the halo size and power to DazzlePow for the dazzle size. Halo/DazzleArea
  69. // defines the angle where the values are valid, so any angle beyond HaloArea/DazzleArea results the halo or
  70. // dazzle being scaled to zero. The angles are re-scaled from normalized angle scale of (-1.0...1.0)
  71. // only HaloArea/DazzleArea defined part is used.
  72. const char* HALO_INTENSITY_POW_STRING = "HaloIntensityPow"; // 1.0 would be linear fadeout, smaller than that will steepen the curve (smaller hotspot)
  73. const char* HALO_SIZE_POW_STRING = "HaloSizePow"; // 1.0 would be linear fadeout, smaller than that will steepen the curve (smaller hotspot)
  74. const char* DAZZLE_INTENSITY_POW_STRING = "DazzleIntensityPow";
  75. const char* DAZZLE_SIZE_POW_STRING = "DazzleSizePow";
  76. const char* HALO_AREA_STRING = "HaloArea";
  77. const char* DAZZLE_AREA_STRING = "DazzleArea"; // Something like 0.05 is a good starting point for a dazzle...
  78. // Halo and dazzle are scaled by these values
  79. const char* HALO_SCALE_X_STRING = "HaloScaleX";
  80. const char* HALO_SCALE_Y_STRING = "HaloScaleY";
  81. const char* DAZZLE_SCALE_X_STRING = "DazzleScaleX";
  82. const char* DAZZLE_SCALE_Y_STRING = "DazzleScaleY";
  83. // Dazzle and halo intensity control the base intensities.
  84. const char* HALO_INTENSITY_STRING = "HaloIntensity";
  85. const char* DAZZLE_INTENSITY_STRING = "DazzleIntensity";
  86. // Direction area defines the maximum difference between the light direction and the eyespace location,
  87. // so the dazzle can only be seen if the camera is inside the light cone. Value 0.0 means dazzle is not
  88. // directional, it can be seen from any direction. Halo is not affected. Dazzle direction defines the light
  89. // direction vector.
  90. const char* DAZZLE_DIRECTION_AREA_STRING = "DazzleDirectionArea"; // Something like 0.5 might be a good test value
  91. const char* DAZZLE_DIRECTION_STRING = "DazzleDirection";
  92. // Dazzle and halo start to afde out after FadeoutStart meters and are completely gone by FadeoutEnd meters.
  93. const char* FADEOUT_START_STRING = "FadeoutStart";
  94. const char* FADEOUT_END_STRING = "FadeoutEnd";
  95. // To be done: This parameter needs to be redefined.
  96. const char* SIZE_OPTIMIZATION_LIMIT_STRING = "SizeOptimizationLimit";
  97. // The weight of history for the intensities. The history weight is per millisecond, so if you want to have
  98. // any real history, values higher than 0.95 are recommended (don't use value of 1.0 or anything higher!)
  99. const char* HISTORY_WEIGHT_STRING = "HistoryWeight";
  100. // Textures to be used.
  101. const char* DAZZLE_TEXTURE_STRING = "DazzleTextureName";
  102. const char* HALO_TEXTURE_STRING = "HaloTextureName";
  103. // If false, camera matrix's translation term isn't used. If translation isn't used the dazzle is treated as
  104. // always visible, scene graph visibility is not used.
  105. const char* USE_CAMERA_TRANSLATION = "UseCameraTranslation";
  106. // Halo and dazzle colors, RGB, (1.0, 1.0, 1.0) is white and (0.0, 0.0, 0.0) is black
  107. const char* HALO_COLOR_STRING = "HaloColor";
  108. const char* DAZZLE_COLOR_STRING = "DazzleColor";
  109. // Dazzle test color could be white in many cases but if the level has a lot of white then another color can be defined.
  110. const char* DAZZLE_TEST_COLOR_STRING = "DazzleTestColor";
  111. // Dazzles can blink on and off. The blink period defines the time for one cycle, the blink "on time" defines
  112. // the amount of time within that period that the dazzle is "on"
  113. const char* BLINK_PERIOD_STRING = "BlinkPeriod";
  114. const char* BLINK_ON_TIME_STRING = "BlinkOnTime";
  115. // Dazzle can use a lensflare by defining a name of lensflare in the ini
  116. const char* DAZZLE_LENSFLARE_STRING = "LensflareName";
  117. // Lensflare definitions
  118. const char* LENSFLARE_LIST_STRING = "Lensflares_List";
  119. // Texture to be used by the lensflare
  120. const char* LENSFLARE_TEXTURE_STRING = "TextureName";
  121. // The number of flare sprites in lensflare. The FlareLocation, FlareSize and FlareColor paremeters are procedural, the
  122. // names are produced based on this parameter. If FlareCount is 3, there exists FlareLocation1, FlareLocation2 and
  123. // FlareLocation3... And so on.
  124. const char* FLARE_COUNT_STRING = "FlareCount";
  125. // 1D-locations of the flares. 0.0 means the location of the center of the screen and 1.0 means the location of the
  126. // lightsource. The values can be smaller than zero and larger than 1.0.
  127. const char* FLARE_LOCATION_STRING = "FlareLocation";
  128. // Normalized flare sizes
  129. const char* FLARE_SIZE_STRING = "FlareSize";
  130. // Colors for each flare sprite
  131. const char* FLARE_COLOR_STRING = "FlareColor";
  132. // Uv coordinates (as there can be only one texture used, but one may want multiple images). The values is a 4-float
  133. // vector: start_u, start_v, end_u, end_v.
  134. const char* FLARE_UV_STRING = "FlareUV";
  135. // Radius of dazzle used for raycast tests
  136. const char * RADIUS_STRING = "Radius";
  137. /*
  138. ; DAZZLE.INI example
  139. [Lensflares_List]
  140. 0=DEFAULT_LENSFLARE
  141. [Dazzles_List]
  142. 0=DEFAULT
  143. ;========================================== LENSFLARE DEFINITIONS =================
  144. [DEFAULT_LENSFLARE]
  145. TextureName=dazzle_secondary.jpg
  146. FlareCount=5
  147. FlareLocation1=0.1
  148. FlareLocation2=0.5
  149. FlareLocation3=1.1
  150. FlareLocation4=1.5
  151. FlareLocation5=-0.4
  152. FlareSize1=0.1
  153. FlareSize2=0.05
  154. FlareSize3=0.08
  155. FlareSize4=0.15
  156. FlareSize5=0.10
  157. FlareColor1=1.0,0.5,0.5
  158. FlareColor2=0.5,1.0,0.5
  159. FlareColor3=0.5,0.5,1.0
  160. FlareColor4=0.5,0.5,1.0
  161. FlareColor5=0.5,0.5,1.0
  162. ;========================================== DAZZLE DEFINITIONS ====================
  163. [DEFAULT]
  164. HaloIntensity=1.0
  165. HaloIntensityPow=0.95
  166. HaloSizePow=0.95
  167. HaloArea=1.0
  168. HaloScaleX=0.2
  169. HaloScaleY=0.2
  170. DazzleArea=0.05
  171. DazzleDirectionArea=0
  172. DazzleDirection=0,1,1
  173. DazzleSizePow=0.9
  174. DazzleIntensityPow=0.9
  175. DazzleIntensity=50
  176. DazzleScaleX=1.0
  177. DazzleScaleY=1.0
  178. FadeoutStart=30.0
  179. FadeoutEnd=40.0
  180. SizeOptimizationLimit=0.05
  181. HistoryWeight=0.975
  182. UseCameraTranslation=1
  183. DazzleTextureName=dazzle_primary.jpg
  184. HaloTextureName=dazzle_secondary.jpg
  185. DazzleColor=1,1,1
  186. HaloColor=0,0,1
  187. DazzleTestColor=1,1,1
  188. LensflareName=DEFAULT_LENSFLARE
  189. */
  190. // Global instance of a dazzle loader
  191. DazzleLoaderClass _DazzleLoader;
  192. static SimpleVecClass<DazzleRenderObjClass*> temp_ptrs;
  193. static DazzleTypeClass** types;
  194. static unsigned type_count;
  195. // Current dazzle layer - must be set before rendering
  196. static DazzleLayerClass * current_dazzle_layer = NULL;
  197. static LensflareTypeClass** lensflares;
  198. static unsigned lensflare_count;
  199. static ShaderClass default_dazzle_shader;
  200. static ShaderClass default_halo_shader;
  201. static ShaderClass vis_shader;
  202. static ShaderClass debug_shader;
  203. // static instance of a default dazzle visibility handler
  204. static DazzleVisibilityClass _DefaultVisibilityHandler;
  205. static const DazzleVisibilityClass * _VisibilityHandler = &_DefaultVisibilityHandler;
  206. static void Init_Shaders()
  207. {
  208. default_dazzle_shader.Set_Cull_Mode( ShaderClass::CULL_MODE_DISABLE );
  209. default_dazzle_shader.Set_Depth_Mask( ShaderClass::DEPTH_WRITE_DISABLE );
  210. default_dazzle_shader.Set_Depth_Compare( ShaderClass::PASS_ALWAYS );
  211. default_dazzle_shader.Set_Dst_Blend_Func( ShaderClass::DSTBLEND_ONE );
  212. default_dazzle_shader.Set_Src_Blend_Func( ShaderClass::SRCBLEND_ONE );
  213. default_dazzle_shader.Set_Fog_Func( ShaderClass::FOG_DISABLE );
  214. default_dazzle_shader.Set_Primary_Gradient( ShaderClass::GRADIENT_MODULATE );
  215. default_dazzle_shader.Set_Texturing( ShaderClass::TEXTURING_ENABLE );
  216. default_halo_shader.Set_Cull_Mode( ShaderClass::CULL_MODE_DISABLE );
  217. default_halo_shader.Set_Depth_Mask( ShaderClass::DEPTH_WRITE_DISABLE );
  218. default_halo_shader.Set_Depth_Compare( ShaderClass::PASS_LEQUAL );
  219. default_halo_shader.Set_Dst_Blend_Func( ShaderClass::DSTBLEND_ONE );
  220. default_halo_shader.Set_Src_Blend_Func( ShaderClass::SRCBLEND_ONE );
  221. default_halo_shader.Set_Fog_Func( ShaderClass::FOG_DISABLE );
  222. default_halo_shader.Set_Primary_Gradient( ShaderClass::GRADIENT_MODULATE );
  223. default_halo_shader.Set_Texturing( ShaderClass::TEXTURING_ENABLE );
  224. vis_shader.Set_Cull_Mode( ShaderClass::CULL_MODE_DISABLE );
  225. vis_shader.Set_Depth_Mask( ShaderClass::DEPTH_WRITE_DISABLE );
  226. vis_shader.Set_Dst_Blend_Func( ShaderClass::DSTBLEND_ZERO );
  227. vis_shader.Set_Src_Blend_Func( ShaderClass::SRCBLEND_ONE );
  228. vis_shader.Set_Fog_Func( ShaderClass::FOG_DISABLE );
  229. vis_shader.Set_Primary_Gradient( ShaderClass::GRADIENT_MODULATE );
  230. vis_shader.Set_Texturing( ShaderClass::TEXTURING_DISABLE );
  231. debug_shader.Set_Cull_Mode( ShaderClass::CULL_MODE_DISABLE );
  232. debug_shader.Set_Depth_Mask( ShaderClass::DEPTH_WRITE_DISABLE );
  233. debug_shader.Set_Dst_Blend_Func( ShaderClass::DSTBLEND_SRC_ALPHA );
  234. debug_shader.Set_Src_Blend_Func( ShaderClass::SRCBLEND_ONE );
  235. debug_shader.Set_Fog_Func( ShaderClass::FOG_DISABLE );
  236. debug_shader.Set_Primary_Gradient( ShaderClass::GRADIENT_MODULATE );
  237. debug_shader.Set_Texturing( ShaderClass::TEXTURING_DISABLE );
  238. }
  239. /*
  240. ** Derived INI to support Vector4
  241. */
  242. class DazzleINIClass : public INIClass {
  243. public:
  244. DazzleINIClass(FileClass & file) : INIClass(file) {}
  245. const Vector2 Get_Vector2(char const *section, char const *entry, const Vector2 & defvalue = Vector2(0,0) );
  246. const Vector3 Get_Vector3(char const *section, char const *entry, const Vector3 & defvalue = Vector3(0,0,0) );
  247. const Vector4 Get_Vector4(char const *section, char const *entry, const Vector4 & defvalue = Vector4(0,0,0,0) ) const;
  248. };
  249. const Vector2 DazzleINIClass::Get_Vector2(char const *section, char const *entry, const Vector2 & defvalue)
  250. {
  251. if (section != NULL && entry != NULL) {
  252. INIEntry * entryptr = Find_Entry(section, entry);
  253. if (entryptr && entryptr->Value != NULL) {
  254. Vector2 ret;
  255. if ( sscanf( entryptr->Value, "%f,%f", &ret[0], &ret[1], &ret[2] ) == 2 ) {
  256. return ret;
  257. }
  258. }
  259. }
  260. return defvalue;
  261. }
  262. const Vector3 DazzleINIClass::Get_Vector3(char const *section, char const * entry, const Vector3 & defvalue )
  263. {
  264. if (section != NULL && entry != NULL) {
  265. INIEntry * entryptr = Find_Entry(section, entry);
  266. if (entryptr && entryptr->Value != NULL) {
  267. Vector3 ret;
  268. if ( sscanf( entryptr->Value, "%f,%f,%f", &ret[0], &ret[1], &ret[2] ) == 3 ) {
  269. return ret;
  270. }
  271. }
  272. }
  273. return defvalue;
  274. }
  275. const Vector4 DazzleINIClass::Get_Vector4(char const *section, char const *entry, const Vector4 & defvalue) const
  276. {
  277. if (section != NULL && entry != NULL) {
  278. INIEntry * entryptr = Find_Entry(section, entry);
  279. if (entryptr && entryptr->Value != NULL) {
  280. Vector4 ret;
  281. if ( sscanf( entryptr->Value, "%f,%f,%f,%f", &ret[0], &ret[1], &ret[2], &ret[3] ) == 4 ) {
  282. return ret;
  283. }
  284. }
  285. }
  286. return defvalue;
  287. }
  288. // ----------------------------------------------------------------------------
  289. LensflareTypeClass::LensflareTypeClass(const LensflareInitClass& is)
  290. :
  291. lic(is),
  292. texture(NULL)
  293. {
  294. }
  295. LensflareTypeClass::~LensflareTypeClass()
  296. {
  297. REF_PTR_RELEASE(texture);
  298. }
  299. TextureClass* LensflareTypeClass::Get_Texture()
  300. {
  301. if (!texture) {
  302. texture = WW3DAssetManager::Get_Instance()->Get_Texture(lic.texture_name);
  303. SET_REF_OWNER(texture);
  304. }
  305. return texture;
  306. }
  307. void LensflareTypeClass::Generate_Vertex_Buffers(
  308. VertexFormatXYZNDUV2* vertex,
  309. int& vertex_count,
  310. float screen_x_scale,
  311. float screen_y_scale,
  312. float dazzle_intensity,
  313. const Vector4& transformed_location)
  314. {
  315. // Lensflares are placed on a line 2D that goes through the lightsource and the center of the screen
  316. // float scale=sqrt(transformed_location[0]*transformed_location[0]+transformed_location[1]*transformed_location[1]);
  317. float z=transformed_location[2];
  318. float distance_multiplier=sqrt(transformed_location[0]*transformed_location[0]+transformed_location[1]*transformed_location[1])+1.0f;
  319. for (int a=0;a<lic.flare_count;++a) {
  320. float x=lic.flare_locations[a]*transformed_location[0];
  321. float y=lic.flare_locations[a]*transformed_location[1];
  322. float size=lic.flare_sizes[a]*distance_multiplier;
  323. float ix=size*screen_x_scale;
  324. float iy=size*screen_y_scale;
  325. Vector3 col=lic.flare_colors[a]*dazzle_intensity;
  326. if (col[0]>1.0f) col[0]=1.0f;
  327. if (col[1]>1.0f) col[1]=1.0f;
  328. if (col[2]>1.0f) col[2]=1.0f;
  329. unsigned color=DX8Wrapper::Convert_Color(col,1.0f);
  330. vertex->x=x+ix;
  331. vertex->y=y-iy;
  332. vertex->z=z;
  333. vertex->u1=lic.flare_uv[a][0];
  334. vertex->v1=lic.flare_uv[a][1];
  335. vertex->diffuse=color;
  336. vertex++;
  337. vertex->x=x+ix;
  338. vertex->y=y+iy;
  339. vertex->z=z;
  340. vertex->u1=lic.flare_uv[a][2];
  341. vertex->v1=lic.flare_uv[a][1];
  342. vertex->diffuse=color;
  343. vertex++;
  344. vertex->x=x-ix;
  345. vertex->y=y+iy;
  346. vertex->z=z;
  347. vertex->u1=lic.flare_uv[a][2];
  348. vertex->v1=lic.flare_uv[a][3];
  349. vertex->diffuse=color;
  350. vertex++;
  351. vertex->x=x-ix;
  352. vertex->y=y-iy;
  353. vertex->z=z;
  354. vertex->u1=lic.flare_uv[a][0];
  355. vertex->v1=lic.flare_uv[a][3];
  356. vertex->diffuse=color;
  357. vertex++;
  358. vertex_count+=4;
  359. }
  360. }
  361. // ----------------------------------------------------------------------------
  362. DazzleTypeClass::DazzleTypeClass(const DazzleInitClass& is)
  363. :
  364. ic(is),
  365. dazzle_shader(default_dazzle_shader),
  366. halo_shader(default_halo_shader),
  367. primary_texture(NULL),
  368. secondary_texture(NULL),
  369. lensflare_id(DazzleRenderObjClass::Get_Lensflare_ID(is.lensflare_name)),
  370. radius(is.radius)
  371. {
  372. fadeout_end_sqr=ic.fadeout_end*ic.fadeout_end;
  373. fadeout_start_sqr=ic.fadeout_start*ic.fadeout_start;
  374. dazzle_test_color_integer=
  375. 0xff000000|
  376. (unsigned(255.0f*ic.dazzle_test_color[2])<<16)|
  377. (unsigned(255.0f*ic.dazzle_test_color[1])<<8)|
  378. (unsigned(255.0f*ic.dazzle_test_color[0]));
  379. dazzle_test_mask_integer=dazzle_test_color_integer&0xf8f8f8f8;
  380. }
  381. DazzleTypeClass::~DazzleTypeClass()
  382. {
  383. REF_PTR_RELEASE(primary_texture);
  384. REF_PTR_RELEASE(secondary_texture);
  385. }
  386. // ----------------------------------------------------------------------------
  387. void DazzleTypeClass::Set_Dazzle_Shader(const ShaderClass& s) // Set shader for the dazzle type
  388. {
  389. dazzle_shader=s;
  390. }
  391. // ----------------------------------------------------------------------------
  392. void DazzleTypeClass::Set_Halo_Shader(const ShaderClass& s) // Set shader for the dazzle type
  393. {
  394. halo_shader=s;
  395. }
  396. // ----------------------------------------------------------------------------
  397. TextureClass* DazzleTypeClass::Get_Dazzle_Texture()
  398. {
  399. if (!primary_texture) {
  400. primary_texture = WW3DAssetManager::Get_Instance()->Get_Texture(ic.primary_texture_name);
  401. SET_REF_OWNER(primary_texture);
  402. }
  403. return primary_texture;
  404. }
  405. // ----------------------------------------------------------------------------
  406. TextureClass* DazzleTypeClass::Get_Halo_Texture()
  407. {
  408. if (!secondary_texture) {
  409. secondary_texture = WW3DAssetManager::Get_Instance()->Get_Texture(ic.secondary_texture_name);
  410. SET_REF_OWNER(secondary_texture);
  411. }
  412. return secondary_texture;
  413. }
  414. // ----------------------------------------------------------------------------
  415. void DazzleTypeClass::Calculate_Intensities(
  416. float& dazzle_intensity,
  417. float& dazzle_size,
  418. float& halo_intensity,
  419. const Vector3& camera_dir,
  420. const Vector3& dazzle_dir,
  421. float distance) const
  422. {
  423. if (ic.use_camera_translation && distance>(fadeout_end_sqr)) {
  424. dazzle_intensity=0.0f;
  425. return;
  426. }
  427. dazzle_intensity-=1.0f-ic.dazzle_area;
  428. dazzle_intensity/=ic.dazzle_area;
  429. WWMath::Clamp(dazzle_intensity);
  430. if (ic.dazzle_direction_area>0.0f) {
  431. float angle=-Vector3::Dot_Product(camera_dir,dazzle_dir);
  432. angle-=1.0f-ic.dazzle_direction_area;
  433. angle/=ic.dazzle_direction_area;
  434. WWMath::Clamp(angle);
  435. dazzle_intensity*=angle;
  436. }
  437. if (dazzle_intensity>0.0f) {
  438. dazzle_size=pow(dazzle_intensity,ic.dazzle_size_pow);
  439. dazzle_intensity=pow(dazzle_intensity,ic.dazzle_intensity_pow);
  440. }
  441. else {
  442. dazzle_intensity=0.0f;
  443. }
  444. dazzle_intensity*=ic.dazzle_intensity;
  445. halo_intensity*=ic.halo_intensity;
  446. // Flare fadeout distance - fade it out...
  447. if (ic.use_camera_translation) {
  448. if (distance>(fadeout_start_sqr)) {
  449. distance=sqrt(distance);
  450. distance-=ic.fadeout_start;
  451. distance/=ic.fadeout_end-ic.fadeout_start;
  452. dazzle_intensity*=1.0f-distance; // Scale down intensity
  453. halo_intensity*=1.0f-distance; // Scale down intensity
  454. }
  455. }
  456. return;
  457. }
  458. // ----------------------------------------------------------------------------
  459. void DazzleRenderObjClass::Init_From_INI(const INIClass* ini)
  460. {
  461. if (!ini) return;
  462. if (ini->Section_Count()==0) return;
  463. Init_Shaders();
  464. unsigned count=ini->Entry_Count( LENSFLARE_LIST_STRING );
  465. unsigned entry;
  466. for ( entry = 0; entry < count; entry++ ) {
  467. char section_name[80];
  468. ini->Get_String(
  469. LENSFLARE_LIST_STRING,
  470. ini->Get_Entry( LENSFLARE_LIST_STRING, entry),
  471. "",
  472. section_name,
  473. sizeof( section_name ));
  474. LensflareInitClass lic;
  475. lic.texture_name=ini->Get_String(section_name,LENSFLARE_TEXTURE_STRING);
  476. lic.flare_count=ini->Get_Int(section_name,FLARE_COUNT_STRING,0);
  477. if (lic.flare_count) {
  478. lic.flare_locations=W3DNEWARRAY float[lic.flare_count]; // Flare location is 1D
  479. lic.flare_sizes=W3DNEWARRAY float[lic.flare_count];
  480. lic.flare_colors=W3DNEWARRAY Vector3[lic.flare_count];
  481. lic.flare_uv=W3DNEWARRAY Vector4[lic.flare_count];
  482. }
  483. else {
  484. lic.flare_locations=NULL;
  485. lic.flare_sizes=NULL;
  486. lic.flare_colors=NULL;
  487. }
  488. for (int flare=0;flare<lic.flare_count;++flare) {
  489. // Flare location
  490. StringClass tmp_name(true);
  491. tmp_name.Format("%s%d",FLARE_LOCATION_STRING,flare+1);
  492. lic.flare_locations[flare]=ini->Get_Float(section_name,tmp_name,0.0f);
  493. // Flare size
  494. tmp_name.Format("%s%d",FLARE_SIZE_STRING,flare+1);
  495. lic.flare_sizes[flare]=ini->Get_Float(section_name,tmp_name,1.0f);
  496. // Flare color
  497. tmp_name.Format("%s%d",FLARE_COLOR_STRING,flare+1);
  498. TPoint3D<float> tp=ini->Get_Point(section_name,tmp_name,TPoint3D<float>(1.0f,1.0f,1.0f));
  499. lic.flare_colors[flare]=reinterpret_cast<const Vector3&>(tp);
  500. // Flare uv
  501. const DazzleINIClass* dini=reinterpret_cast<const DazzleINIClass*>(ini);
  502. tmp_name.Format("%s%d",FLARE_UV_STRING,flare+1);
  503. lic.flare_uv[flare]=dini->Get_Vector4(section_name,tmp_name,Vector4(0.0f,0.0f,1.0f,1.0f));
  504. }
  505. lic.type=entry;
  506. Init_Lensflare(lic);
  507. lensflares[entry]->name=section_name;
  508. }
  509. count=ini->Entry_Count( DAZZLE_LIST_STRING );
  510. for ( entry = 0; entry < count; entry++ ) {
  511. char section_name[80];
  512. ini->Get_String(
  513. DAZZLE_LIST_STRING,
  514. ini->Get_Entry( DAZZLE_LIST_STRING, entry),
  515. "",
  516. section_name,
  517. sizeof( section_name ));
  518. DazzleInitClass dic;
  519. dic.primary_texture_name=ini->Get_String(section_name,DAZZLE_TEXTURE_STRING);
  520. dic.secondary_texture_name=ini->Get_String(section_name,HALO_TEXTURE_STRING);
  521. dic.halo_intensity=ini->Get_Float(section_name,HALO_INTENSITY_STRING,0.95f);
  522. dic.halo_intensity_pow=ini->Get_Float(section_name,HALO_INTENSITY_POW_STRING,0.95f);
  523. dic.halo_size_pow=ini->Get_Float(section_name,HALO_SIZE_POW_STRING,0.95f);
  524. dic.halo_area=ini->Get_Float(section_name,HALO_AREA_STRING,0.2f);
  525. dic.halo_scale_x=ini->Get_Float(section_name,HALO_SCALE_X_STRING,2.0f);
  526. dic.halo_scale_y=ini->Get_Float(section_name,HALO_SCALE_Y_STRING,2.0f);
  527. dic.dazzle_area=ini->Get_Float(section_name,DAZZLE_AREA_STRING,0.05f);
  528. dic.dazzle_direction_area=ini->Get_Float(section_name,DAZZLE_DIRECTION_AREA_STRING,0.50f);
  529. dic.dazzle_intensity=ini->Get_Float(section_name,DAZZLE_INTENSITY_STRING,0.90f);
  530. dic.dazzle_intensity_pow=ini->Get_Float(section_name,DAZZLE_INTENSITY_POW_STRING,0.90f);
  531. dic.dazzle_size_pow=ini->Get_Float(section_name,DAZZLE_SIZE_POW_STRING,0.90f);
  532. dic.dazzle_scale_x=ini->Get_Float(section_name,DAZZLE_SCALE_X_STRING,100.0f);
  533. dic.dazzle_scale_y=ini->Get_Float(section_name,DAZZLE_SCALE_Y_STRING,25.0f);
  534. dic.fadeout_start=ini->Get_Float(section_name,FADEOUT_START_STRING,25.0f);
  535. dic.fadeout_end=ini->Get_Float(section_name,FADEOUT_END_STRING,50.0f);
  536. dic.size_optimization_limit=ini->Get_Float(section_name,SIZE_OPTIMIZATION_LIMIT_STRING,0.05f);
  537. dic.history_weight=ini->Get_Float(section_name,HISTORY_WEIGHT_STRING,0.5f);
  538. dic.use_camera_translation=!!ini->Get_Int(section_name,USE_CAMERA_TRANSLATION,1);
  539. dic.lensflare_name=ini->Get_String(section_name,DAZZLE_LENSFLARE_STRING);
  540. dic.type=entry;
  541. TPoint3D<float> tp=ini->Get_Point(section_name,DAZZLE_DIRECTION_STRING,TPoint3D<float>(0.0f,0.0f,0.0f));
  542. tp.Normalize();
  543. dic.dazzle_direction=reinterpret_cast<const Vector3&>(tp);
  544. tp=ini->Get_Point(section_name,DAZZLE_TEST_COLOR_STRING,TPoint3D<float>(1.0f,1.0f,1.0f));
  545. dic.dazzle_test_color=reinterpret_cast<const Vector3&>(tp);
  546. tp=ini->Get_Point(section_name,DAZZLE_COLOR_STRING,TPoint3D<float>(1.0f,1.0f,1.0f));
  547. dic.dazzle_color=reinterpret_cast<const Vector3&>(tp);
  548. tp=ini->Get_Point(section_name,HALO_COLOR_STRING,TPoint3D<float>(1.0f,1.0f,1.0f));
  549. dic.halo_color=reinterpret_cast<const Vector3&>(tp);
  550. dic.radius=ini->Get_Float(section_name,RADIUS_STRING,1.0f);
  551. dic.blink_period=ini->Get_Float(section_name,BLINK_PERIOD_STRING,0.0f);
  552. dic.blink_on_time=ini->Get_Float(section_name,BLINK_ON_TIME_STRING,0.0f);
  553. Init_Type(dic);
  554. types[entry]->name=section_name;
  555. }
  556. }
  557. // ----------------------------------------------------------------------------
  558. void DazzleRenderObjClass::Init_Type(const DazzleInitClass& i)
  559. {
  560. Init_Shaders();
  561. if (i.type>=type_count) {
  562. unsigned new_count=i.type+1;
  563. DazzleTypeClass** new_types=W3DNEWARRAY DazzleTypeClass*[new_count];
  564. for (unsigned a=0;a<type_count;++a) {
  565. new_types[a]=types[a];
  566. }
  567. for (;a<new_count;++a) {
  568. new_types[a]=0;
  569. }
  570. delete[] types;
  571. types=new_types;
  572. type_count=new_count;
  573. }
  574. delete types[i.type];
  575. types[i.type]=W3DNEW DazzleTypeClass(i);
  576. }
  577. // ----------------------------------------------------------------------------
  578. void DazzleRenderObjClass::Init_Lensflare(const LensflareInitClass& i)
  579. {
  580. Init_Shaders();
  581. if (i.type>=lensflare_count) {
  582. unsigned new_count=i.type+1;
  583. LensflareTypeClass** new_lensflares=W3DNEWARRAY LensflareTypeClass*[new_count];
  584. for (unsigned a=0;a<lensflare_count;++a) {
  585. new_lensflares[a]=lensflares[a];
  586. }
  587. for (;a<new_count;++a) {
  588. new_lensflares[a]=0;
  589. }
  590. delete[] lensflares;
  591. lensflares=new_lensflares;
  592. lensflare_count=new_count;
  593. }
  594. delete lensflares[i.type];
  595. lensflares[i.type]=W3DNEW LensflareTypeClass(i);
  596. }
  597. // ----------------------------------------------------------------------------
  598. void DazzleRenderObjClass::Deinit()
  599. {
  600. // Deinit dazzle types
  601. if (types) {
  602. for (unsigned a=0;a<type_count;++a) {
  603. delete types[a];
  604. }
  605. delete[] types;
  606. }
  607. types=NULL;
  608. type_count=0;
  609. // Deinit lensflare types
  610. if (lensflares) {
  611. for (unsigned a=0;a<lensflare_count;++a) {
  612. delete lensflares[a];
  613. }
  614. delete[] lensflares;
  615. }
  616. lensflares=NULL;
  617. lensflare_count=0;
  618. }
  619. void DazzleRenderObjClass::Install_Dazzle_Visibility_Handler(const DazzleVisibilityClass * visibility_handler)
  620. {
  621. if (visibility_handler == NULL) {
  622. _VisibilityHandler = &_DefaultVisibilityHandler;
  623. } else {
  624. _VisibilityHandler = visibility_handler;
  625. }
  626. }
  627. // ----------------------------------------------------------------------------
  628. //
  629. // The parameter t defines the dazzle type. t has to be a valid type id in order
  630. // for the dazzle to work.
  631. //
  632. // ----------------------------------------------------------------------------
  633. DazzleRenderObjClass::DazzleRenderObjClass(unsigned t)
  634. :
  635. succ(NULL),
  636. type(t),
  637. current_dazzle_intensity(0.0f),
  638. current_dazzle_size(0.0f),
  639. dazzle_color(1.0f,1.0f,1.0f),
  640. halo_color(1.0f,1.0f,1.0f),
  641. lensflare_intensity(1.0f),
  642. on_list(false),
  643. visibility(0.0f),
  644. radius(types[t]->radius)
  645. {
  646. creation_time = WW3D::Get_Sync_Time();
  647. }
  648. // ----------------------------------------------------------------------------
  649. //
  650. // Creating a dazzle with its type name is a bit slower (due to scanning) but
  651. // works equally to the creation with id.
  652. //
  653. // ----------------------------------------------------------------------------
  654. DazzleRenderObjClass::DazzleRenderObjClass(const char * type_name)
  655. :
  656. succ(NULL),
  657. type(Get_Type_ID(type_name)),
  658. current_dazzle_intensity(0.0f),
  659. current_dazzle_size(0.0f),
  660. dazzle_color(1.0f,1.0f,1.0f),
  661. halo_color(1.0f,1.0f,1.0f),
  662. lensflare_intensity(1.0f),
  663. on_list(false),
  664. visibility(0.0f),
  665. radius(types[Get_Type_ID(type_name)]->radius)
  666. {
  667. creation_time = WW3D::Get_Sync_Time();
  668. }
  669. // ----------------------------------------------------------------------------
  670. //
  671. // Copy constructor creates an exact copy of the object, except for the visibility
  672. // information, which is initialised with default values.
  673. //
  674. // ----------------------------------------------------------------------------
  675. DazzleRenderObjClass::DazzleRenderObjClass(const DazzleRenderObjClass & src)
  676. :
  677. succ(NULL),
  678. type(src.type),
  679. current_dazzle_intensity(src.current_dazzle_intensity),
  680. current_dazzle_size(src.current_dazzle_size),
  681. current_dir(src.current_dir),
  682. dazzle_color(src.dazzle_color),
  683. halo_color(src.halo_color),
  684. lensflare_intensity(src.lensflare_intensity),
  685. on_list(false),
  686. visibility(src.visibility),
  687. radius(src.radius)
  688. {
  689. creation_time = WW3D::Get_Sync_Time();
  690. }
  691. DazzleRenderObjClass& DazzleRenderObjClass::operator = (const DazzleRenderObjClass & src)
  692. {
  693. type=src.type;
  694. current_dir=src.current_dir;
  695. current_dazzle_intensity=src.current_dazzle_intensity;
  696. current_dazzle_size=src.current_dazzle_size;
  697. dazzle_color=src.dazzle_color;
  698. halo_color=src.halo_color;
  699. lensflare_intensity=src.lensflare_intensity;
  700. visibility=src.visibility;
  701. radius=src.radius;
  702. creation_time = WW3D::Get_Sync_Time();
  703. return *this;
  704. }
  705. void DazzleRenderObjClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
  706. {
  707. sphere.Center.Set(0,0,0);
  708. sphere.Radius = radius;
  709. }
  710. void DazzleRenderObjClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
  711. {
  712. box.Center.Set(0,0,0);
  713. box.Extent.Set(radius,radius,radius);
  714. }
  715. void DazzleRenderObjClass::Set_Layer(DazzleLayerClass *layer)
  716. {
  717. // Never insert a dazzle into a list if it already is on one - this caould create infinite
  718. // loops in the dazzle list.
  719. if (on_list) return;
  720. // This function sets the dazzle to be visible in the given layer.
  721. if (!layer) {
  722. WWASSERT(0);
  723. return;
  724. }
  725. if (type>=type_count) return; // If the type doesn't exist the visibility can't be changed
  726. // Insert at the head of the layer's visible list
  727. succ = layer->visible_lists[type];
  728. layer->visible_lists[type] = this;
  729. on_list = true;
  730. }
  731. void DazzleRenderObjClass::Set_Current_Dazzle_Layer(DazzleLayerClass *layer)
  732. {
  733. current_dazzle_layer = layer;
  734. }
  735. /////////////////////////////////////////////////////////////////////////////
  736. // Render Object Interface
  737. /////////////////////////////////////////////////////////////////////////////
  738. RenderObjClass* DazzleRenderObjClass::Clone(void) const
  739. {
  740. return NEW_REF(DazzleRenderObjClass, (*this));
  741. }
  742. // ----------------------------------------------------------------------------
  743. //
  744. // DazzleRenderObjClass's Render() function doesn't actually render the dazzle
  745. // immediatelly but just sets the dazzle visible. This is due to the way the
  746. // dazzle system works (the dazzles need to be rendered after everything else).
  747. // Having the Render() function flag the visibility offers us the visibility
  748. // functionality of the scene graph.
  749. //
  750. // ----------------------------------------------------------------------------
  751. void DazzleRenderObjClass::Render(RenderInfoClass & rinfo)
  752. {
  753. WWPROFILE("Dazzle::Render");
  754. if (Is_Not_Hidden_At_All()) {
  755. // First check if the dazzle is blinking and is "off"
  756. bool is_on = true;
  757. DazzleInitClass & ic = types[type]->ic;
  758. if (ic.blink_period > 0.0f) {
  759. float elapsed_time = ((float)(WW3D::Get_Sync_Time() - creation_time)) / 1000.0f;
  760. float wrapped_time = fmodf(elapsed_time,ic.blink_period);
  761. if (wrapped_time > ic.blink_on_time) {
  762. is_on = false;
  763. }
  764. }
  765. // Next, check visibility
  766. visibility = 1.0f;
  767. if (is_on == false) {
  768. visibility = 0.0f;
  769. } else {
  770. // Vector3 position;
  771. // Get_Transform().Get_Translation(&position);
  772. // visibility = _VisibilityHandler->Compute_Dazzle_Visibility(rinfo,this,position);
  773. Matrix4 view_transform,projection_transform;
  774. DX8Wrapper::Get_Transform(D3DTS_VIEW,view_transform);
  775. DX8Wrapper::Get_Transform(D3DTS_PROJECTION,projection_transform);
  776. Vector3 camera_loc(rinfo.Camera.Get_Position());
  777. Vector3 camera_dir(-view_transform[2][0],-view_transform[2][1],-view_transform[2][2]);
  778. Vector3 loc=Get_Position();
  779. transformed_loc=view_transform*loc;
  780. transformed_loc=projection_transform*transformed_loc;
  781. transformed_loc[0]/=transformed_loc[3];
  782. transformed_loc[1]/=transformed_loc[3];
  783. transformed_loc[2]/=transformed_loc[3];
  784. transformed_loc[3]=1.0f;
  785. current_vloc=Vector3(transformed_loc[0],transformed_loc[1],transformed_loc[2]);
  786. float dazzle_intensity;
  787. Vector3 dir;
  788. dir=camera_loc-loc;
  789. current_distance=dir.Length2();
  790. dir.Normalize();
  791. dazzle_intensity=-Vector3::Dot_Product(dir,camera_dir);
  792. // dazzle_intensity*=visibility;
  793. float dazzle_size;
  794. current_halo_intensity=1.0f;
  795. const DazzleTypeClass* params=types[type];
  796. params->Calculate_Intensities(dazzle_intensity,dazzle_size,current_halo_intensity,camera_dir,current_dir,current_distance);
  797. unsigned time_ms=WW3D::Get_Frame_Time();
  798. if (time_ms==0) time_ms=1;
  799. float weight=pow(params->ic.history_weight,time_ms);
  800. if (dazzle_intensity>0.0f) {
  801. visibility = _VisibilityHandler->Compute_Dazzle_Visibility(rinfo,this,loc);
  802. dazzle_intensity*=visibility;
  803. }
  804. else {
  805. visibility=0.0f;
  806. }
  807. if (visibility == 0.0f) {
  808. float i=dazzle_intensity*(1.0f-weight)+current_dazzle_intensity*weight;
  809. current_dazzle_intensity=i;
  810. if (current_dazzle_intensity<0.05f) current_dazzle_intensity=0.0f;
  811. dazzle_intensity=i;
  812. float s=dazzle_size*(1.0f-weight)+current_dazzle_size*weight;
  813. current_dazzle_size=s;
  814. dazzle_size=s;
  815. } else {
  816. current_dazzle_intensity = dazzle_intensity;
  817. current_dazzle_size = dazzle_size;
  818. }
  819. }
  820. // If this dazzle is visible or it is currently fading, submit it for rendering
  821. if (visibility > 0.0f || current_dazzle_intensity>0.0f) {
  822. WWASSERT(types[type]);
  823. Set_Layer(current_dazzle_layer);
  824. }
  825. }
  826. else {
  827. visibility=0.0f;
  828. }
  829. }
  830. void DazzleRenderObjClass::Render_Dazzle(CameraClass* camera)
  831. {
  832. Matrix4 old_view_transform;
  833. Matrix4 old_world_transform;
  834. Matrix4 old_projection_transform;
  835. Matrix4 view_transform;
  836. Matrix4 world_transform;
  837. Matrix4 projection_transform;
  838. DX8Wrapper::Get_Transform(D3DTS_VIEW,view_transform);
  839. DX8Wrapper::Get_Transform(D3DTS_WORLD,world_transform);
  840. DX8Wrapper::Get_Transform(D3DTS_PROJECTION,projection_transform);
  841. old_view_transform=view_transform;
  842. old_world_transform=world_transform;
  843. old_projection_transform=projection_transform;
  844. Vector3 camera_loc(camera->Get_Position());
  845. Vector3 camera_dir(-view_transform[2][0],-view_transform[2][1],-view_transform[2][2]);
  846. int display_width,display_height,display_bits;
  847. bool windowed;
  848. WW3D::Get_Device_Resolution(display_width,display_height,display_bits,windowed);
  849. float w=float(display_width);
  850. float h=float(display_height);
  851. float screen_x_scale=1.0f;
  852. float screen_y_scale=1.0f;
  853. if (w>h) {
  854. screen_y_scale=w/h;
  855. }
  856. else {
  857. screen_x_scale=h/w;
  858. }
  859. // unsigned time_ms=WW3D::Get_Frame_Time();
  860. // if (time_ms==0) time_ms=1;
  861. float halo_scale_x=types[type]->ic.halo_scale_x;
  862. float halo_scale_y=types[type]->ic.halo_scale_y;
  863. float dazzle_scale_x=types[type]->ic.dazzle_scale_x;
  864. float dazzle_scale_y=types[type]->ic.dazzle_scale_y;
  865. // Allocate some arrays for the dazzle rendering
  866. int vertex_count=4;
  867. const DazzleTypeClass* params=types[type];
  868. int halo_vertex_count=0;
  869. int dazzle_vertex_count=0;
  870. int lensflare_vertex_count=0;
  871. Vector3 dl;
  872. int lens_max_verts=0;
  873. LensflareTypeClass* lensflare = DazzleRenderObjClass::Get_Lensflare_Class(types[type]->lensflare_id);
  874. if (lensflare) {
  875. lens_max_verts=4*lensflare->lic.flare_count;
  876. }
  877. DynamicVBAccessClass vb_access(BUFFER_TYPE_DYNAMIC_DX8,dynamic_fvf_type,vertex_count*2+lens_max_verts);
  878. {
  879. DynamicVBAccessClass::WriteLockClass lock(&vb_access);
  880. VertexFormatXYZNDUV2* verts=lock.Get_Formatted_Vertex_Array();
  881. float halo_size=1.0f;
  882. Vector3 dazzle_dxt(screen_x_scale * dazzle_scale_x,0.0f,0.0f);
  883. Vector3 halo_dxt(camera->Compute_Projected_Sphere_Radius(current_distance,halo_scale_x),0.0f,0.0f);
  884. Vector3 dazzle_dyt(0.0f,screen_y_scale * dazzle_scale_y,0.0f);
  885. Vector3 halo_dyt(0.0f,camera->Compute_Projected_Sphere_Radius(current_distance,halo_scale_y),0.0f);
  886. if (current_dazzle_intensity>0.0f) {
  887. VertexFormatXYZNDUV2* vertex=verts;
  888. dazzle_vertex_count+=4;
  889. Vector3 col(
  890. dazzle_color[0]*params->ic.dazzle_color[0],
  891. dazzle_color[1]*params->ic.dazzle_color[1],
  892. dazzle_color[2]*params->ic.dazzle_color[2]);
  893. col*=current_dazzle_intensity;
  894. if (col[0]>1.0f) col[0]=1.0f;
  895. if (col[1]>1.0f) col[1]=1.0f;
  896. if (col[2]>1.0f) col[2]=1.0f;
  897. unsigned color=DX8Wrapper::Convert_Color(col,1.0f);
  898. dl=current_vloc+(dazzle_dxt-dazzle_dyt)*current_dazzle_size;
  899. reinterpret_cast<Vector3&>(vertex->x)=dl;
  900. vertex->u1=0.0f;
  901. vertex->v1=0.0f;
  902. vertex->diffuse=color;
  903. vertex++;
  904. dl=current_vloc+(dazzle_dxt+dazzle_dyt)*current_dazzle_size;
  905. reinterpret_cast<Vector3&>(vertex->x)=dl;
  906. vertex->u1=1.0f;
  907. vertex->v1=0.0f;
  908. vertex->diffuse=color;
  909. vertex++;
  910. dl=current_vloc-(dazzle_dxt-dazzle_dyt)*current_dazzle_size;
  911. reinterpret_cast<Vector3&>(vertex->x)=dl;
  912. vertex->u1=1.0f;
  913. vertex->v1=1.0f;
  914. vertex->diffuse=color;
  915. vertex++;
  916. dl=current_vloc-(dazzle_dxt+dazzle_dyt)*current_dazzle_size;
  917. reinterpret_cast<Vector3&>(vertex->x)=dl;
  918. vertex->u1=0.0f;
  919. vertex->v1=1.0f;
  920. vertex->diffuse=color;
  921. }
  922. if (current_halo_intensity) {
  923. VertexFormatXYZNDUV2* vertex=verts+dazzle_vertex_count;
  924. halo_vertex_count+=4;
  925. Vector3 col(
  926. halo_color[0]*params->ic.halo_color[0],
  927. halo_color[1]*params->ic.halo_color[1],
  928. halo_color[2]*params->ic.halo_color[2]);
  929. col*=current_halo_intensity;
  930. if (col[0]>1.0f) col[0]=1.0f;
  931. if (col[1]>1.0f) col[1]=1.0f;
  932. if (col[2]>1.0f) col[2]=1.0f;
  933. unsigned color=DX8Wrapper::Convert_Color(col,1.0f);
  934. dl=current_vloc+(halo_dxt-halo_dyt)*halo_size;
  935. reinterpret_cast<Vector3&>(vertex->x)=dl;
  936. vertex->u1=0.0f;
  937. vertex->v1=0.0f;
  938. vertex->diffuse=color;
  939. vertex++;
  940. dl=current_vloc+(halo_dxt+halo_dyt)*halo_size;
  941. reinterpret_cast<Vector3&>(vertex->x)=dl;
  942. vertex->u1=1.0f;
  943. vertex->v1=0.0f;
  944. vertex->diffuse=color;
  945. vertex++;
  946. dl=current_vloc-(halo_dxt-halo_dyt)*halo_size;
  947. reinterpret_cast<Vector3&>(vertex->x)=dl;
  948. vertex->u1=1.0f;
  949. vertex->v1=1.0f;
  950. vertex->diffuse=color;
  951. vertex++;
  952. dl=current_vloc-(halo_dxt+halo_dyt)*halo_size;
  953. reinterpret_cast<Vector3&>(vertex->x)=dl;
  954. vertex->u1=0.0f;
  955. vertex->v1=1.0f;
  956. vertex->diffuse=color;
  957. }
  958. if (lensflare && current_dazzle_intensity>0.0f) {
  959. VertexFormatXYZNDUV2* vertex=verts+halo_vertex_count+dazzle_vertex_count;
  960. lensflare->Generate_Vertex_Buffers(
  961. vertex,
  962. lensflare_vertex_count,
  963. screen_x_scale,
  964. screen_y_scale,
  965. current_dazzle_intensity * lensflare_intensity,
  966. transformed_loc);
  967. vertex_count+=lensflare_vertex_count;
  968. }
  969. }
  970. int dazzle_poly_count=dazzle_vertex_count>>1;
  971. int halo_poly_count=halo_vertex_count>>1;
  972. int lensflare_poly_count=lensflare_vertex_count>>1;
  973. int poly_count=halo_poly_count>dazzle_poly_count ? halo_poly_count : dazzle_poly_count;
  974. if (lensflare_poly_count>poly_count) poly_count=lensflare_poly_count;
  975. if (!poly_count) {
  976. return;
  977. }
  978. DX8Wrapper::Set_Vertex_Buffer(vb_access);
  979. DynamicIBAccessClass ib_access(BUFFER_TYPE_DYNAMIC_DX8,poly_count*3);
  980. {
  981. DynamicIBAccessClass::WriteLockClass lock(&ib_access);
  982. unsigned short* inds=lock.Get_Index_Array();
  983. // Proceed two polygons at a time
  984. for (int a=0;a<poly_count/2;a++) {
  985. *inds++=short(4*a);
  986. *inds++=short(4*a+1);
  987. *inds++=short(4*a+2);
  988. *inds++=short(4*a);
  989. *inds++=short(4*a+2);
  990. *inds++=short(4*a+3);
  991. }
  992. }
  993. DX8Wrapper::Set_World_Identity();
  994. DX8Wrapper::Set_View_Identity();
  995. DX8Wrapper::Set_Transform(D3DTS_PROJECTION,Matrix4(true));
  996. if (halo_poly_count) {
  997. DX8Wrapper::Set_Index_Buffer(ib_access,dazzle_vertex_count);
  998. DX8Wrapper::Set_Shader(default_halo_shader);
  999. DX8Wrapper::Set_Texture(0,types[type]->Get_Halo_Texture());
  1000. SphereClass sphere(Get_Position(),0.1f);
  1001. DX8Wrapper::Draw_Triangles(0,halo_poly_count,0,vertex_count);
  1002. }
  1003. if (dazzle_poly_count) {
  1004. DX8Wrapper::Set_Index_Buffer(ib_access,0);
  1005. DX8Wrapper::Set_Shader(default_dazzle_shader);
  1006. DX8Wrapper::Set_Texture(0,types[type]->Get_Dazzle_Texture());
  1007. SphereClass sphere(Vector3(0.0f,0.0f,0.0f),0.0f);
  1008. DX8Wrapper::Draw_Triangles(0,dazzle_poly_count,0,vertex_count);
  1009. }
  1010. if (lensflare_poly_count) {
  1011. DX8Wrapper::Set_Index_Buffer(ib_access,dazzle_vertex_count+halo_vertex_count);
  1012. DX8Wrapper::Set_Shader(default_dazzle_shader);
  1013. DX8Wrapper::Set_Texture(0,lensflare->Get_Texture());
  1014. SphereClass sphere(Vector3(0.0f,0.0f,0.0f),0.0f);
  1015. DX8Wrapper::Draw_Triangles(0,lensflare_poly_count,0,vertex_count);
  1016. }
  1017. DX8Wrapper::Set_Transform(D3DTS_PROJECTION,old_projection_transform);
  1018. DX8Wrapper::Set_Transform(D3DTS_VIEW,old_view_transform);
  1019. DX8Wrapper::Set_Transform(D3DTS_WORLD,old_world_transform);
  1020. }
  1021. // ----------------------------------------------------------------------------
  1022. void DazzleRenderObjClass::Set_Transform(const Matrix3D &m)
  1023. {
  1024. RenderObjClass::Set_Transform(m);
  1025. if (type<type_count) {
  1026. Matrix3D::Rotate_Vector(m,types[type]->ic.dazzle_direction,&current_dir);
  1027. }
  1028. }
  1029. // ----------------------------------------------------------------------------
  1030. //
  1031. // Return the type id of a dazzle class with given name. If such type is not
  1032. // found, return UINT_MAX.
  1033. //
  1034. // ----------------------------------------------------------------------------
  1035. unsigned DazzleRenderObjClass::Get_Type_ID(const char* name)
  1036. {
  1037. for (unsigned a=0;a<type_count;++a) {
  1038. if (types[a] && types[a]->name==name) return a;
  1039. }
  1040. return UINT_MAX;
  1041. }
  1042. // ----------------------------------------------------------------------------
  1043. //
  1044. // Return the type name of a dazzle class with given id. If the id is out
  1045. // of range, return "DEFAULT" which by convention is always defined and is
  1046. // id 0.
  1047. //
  1048. // ----------------------------------------------------------------------------
  1049. const char * DazzleRenderObjClass::Get_Type_Name(unsigned id)
  1050. {
  1051. if ((id < type_count) && (id >= 0)) {
  1052. return types[id]->name;
  1053. } else {
  1054. return "DEFAULT";
  1055. }
  1056. }
  1057. // ----------------------------------------------------------------------------
  1058. //
  1059. // Return pointer to DazzleTypeClass object with given id. If the id is out
  1060. // of range (usually UINT_MAX, in can the id was obtained with invalid name
  1061. // string) return NULL.
  1062. //
  1063. // ----------------------------------------------------------------------------
  1064. DazzleTypeClass* DazzleRenderObjClass::Get_Type_Class(unsigned id) // Return dazzle type class pointer, or NULL if not found
  1065. {
  1066. if (id>=type_count) return NULL;
  1067. return types[id];
  1068. }
  1069. // ----------------------------------------------------------------------------
  1070. //
  1071. // Return the type id of a lensflare type class with given name. If such type is
  1072. // not found, return UINT_MAX.
  1073. //
  1074. // ----------------------------------------------------------------------------
  1075. unsigned DazzleRenderObjClass::Get_Lensflare_ID(const char* name)
  1076. {
  1077. for (unsigned a=0;a<lensflare_count;++a) {
  1078. if (lensflares[a] && lensflares[a]->name==name) return a;
  1079. }
  1080. return UINT_MAX;
  1081. }
  1082. // ----------------------------------------------------------------------------
  1083. //
  1084. // Return pointer to LensflareTypeClass object with given id. If the id is out
  1085. // of range (usually UINT_MAX, in can the id was obtained with invalid name
  1086. // string) return NULL.
  1087. //
  1088. // ----------------------------------------------------------------------------
  1089. LensflareTypeClass* DazzleRenderObjClass::Get_Lensflare_Class(unsigned id) // Return lensflare type class pointer, or NULL if not found
  1090. {
  1091. if (id>=lensflare_count) return NULL;
  1092. return lensflares[id];
  1093. }
  1094. // ----------------------------------------------------------------------------
  1095. //
  1096. // Should static dazzles require vis information, here's a function that renders
  1097. // a quad at the location of the dazzle
  1098. //
  1099. // ----------------------------------------------------------------------------
  1100. void DazzleRenderObjClass::vis_render_dazzle(SpecialRenderInfoClass & rinfo)
  1101. {
  1102. }
  1103. void DazzleRenderObjClass::Special_Render(SpecialRenderInfoClass & rinfo)
  1104. {
  1105. if (rinfo.RenderType == SpecialRenderInfoClass::RENDER_VIS) {
  1106. vis_render_dazzle(rinfo);
  1107. }
  1108. }
  1109. /****************************************************************************************
  1110. DazzleRenderObjClass - Persistant object support.
  1111. Dazzles are going to save their type and their transform and simply
  1112. re-create another dazzle of the same type when loaded.
  1113. ****************************************************************************************/
  1114. class DazzlePersistFactoryClass : public PersistFactoryClass
  1115. {
  1116. virtual uint32 Chunk_ID(void) const;
  1117. virtual PersistClass * Load(ChunkLoadClass & cload) const;
  1118. virtual void Save(ChunkSaveClass & csave,PersistClass * obj) const;
  1119. enum
  1120. {
  1121. DAZZLEFACTORY_CHUNKID_VARIABLES = 1212000336,
  1122. DAZZLEFACTORY_VARIABLE_OBJPOINTER = 0x00,
  1123. OBSOLETE_DAZZLEFACTORY_VARIABLE_TYPE,
  1124. DAZZLEFACTORY_VARIABLE_TRANSFORM,
  1125. DAZZLEFACTORY_VARIABLE_TYPENAME,
  1126. };
  1127. };
  1128. static DazzlePersistFactoryClass _DazzleFactory;
  1129. uint32 DazzlePersistFactoryClass::Chunk_ID(void) const
  1130. {
  1131. return WW3D_PERSIST_CHUNKID_DAZZLE;
  1132. }
  1133. PersistClass * DazzlePersistFactoryClass::Load(ChunkLoadClass & cload) const
  1134. {
  1135. DazzleRenderObjClass * old_obj = NULL;
  1136. Matrix3D tm(1);
  1137. char dazzle_type[256];
  1138. dazzle_type[0] = 0;
  1139. /*
  1140. ** Load the dazzle parameters
  1141. */
  1142. while (cload.Open_Chunk()) {
  1143. switch (cload.Cur_Chunk_ID()) {
  1144. case DAZZLEFACTORY_CHUNKID_VARIABLES:
  1145. while (cload.Open_Micro_Chunk()) {
  1146. switch(cload.Cur_Micro_Chunk_ID()) {
  1147. READ_MICRO_CHUNK(cload,DAZZLEFACTORY_VARIABLE_OBJPOINTER,old_obj);
  1148. READ_MICRO_CHUNK(cload,DAZZLEFACTORY_VARIABLE_TRANSFORM,tm);
  1149. READ_MICRO_CHUNK_STRING(cload,DAZZLEFACTORY_VARIABLE_TYPENAME,dazzle_type,sizeof(dazzle_type));
  1150. }
  1151. cload.Close_Micro_Chunk();
  1152. }
  1153. break;
  1154. default:
  1155. WWDEBUG_SAY(("Unhandled Chunk: 0x%X File: %s Line: %d\r\n",__FILE__,__LINE__));
  1156. break;
  1157. };
  1158. cload.Close_Chunk();
  1159. }
  1160. /*
  1161. ** Create a new dazzle object
  1162. */
  1163. int type_index = 0;
  1164. if (strlen(dazzle_type) > 0) {
  1165. type_index = DazzleRenderObjClass::Get_Type_ID(dazzle_type);
  1166. }
  1167. RenderObjClass * new_obj = NEW_REF(DazzleRenderObjClass,(dazzle_type));
  1168. /*
  1169. ** If we failed to create it, replace it with a NULL
  1170. */
  1171. if (new_obj == NULL) {
  1172. static int count = 0;
  1173. if ( ++count < 10 ) {
  1174. WWDEBUG_SAY(("DazzlePersistFactory failed to create dazzle of type: %s!!\r\n",dazzle_type));
  1175. WWDEBUG_SAY(("Replacing it with a NULL render object!\r\n"));
  1176. }
  1177. new_obj = WW3DAssetManager::Get_Instance()->Create_Render_Obj("NULL");
  1178. }
  1179. WWASSERT(new_obj != NULL);
  1180. if (new_obj) {
  1181. new_obj->Set_Transform(tm);
  1182. }
  1183. /*
  1184. ** Register the old pointer for re-mapping to the new pointer
  1185. */
  1186. SaveLoadSystemClass::Register_Pointer(old_obj,new_obj);
  1187. return new_obj;
  1188. }
  1189. void DazzlePersistFactoryClass::Save(ChunkSaveClass & csave,PersistClass * obj) const
  1190. {
  1191. DazzleRenderObjClass * robj = (DazzleRenderObjClass *)obj;
  1192. unsigned int dazzle_type = robj->Get_Dazzle_Type();
  1193. const char * dazzle_type_name = DazzleRenderObjClass::Get_Type_Name(dazzle_type);
  1194. Matrix3D tm = robj->Get_Transform();
  1195. csave.Begin_Chunk(DAZZLEFACTORY_CHUNKID_VARIABLES);
  1196. WRITE_MICRO_CHUNK(csave,DAZZLEFACTORY_VARIABLE_OBJPOINTER,robj);
  1197. WRITE_MICRO_CHUNK(csave,DAZZLEFACTORY_VARIABLE_TRANSFORM,tm);
  1198. WRITE_MICRO_CHUNK_STRING(csave,DAZZLEFACTORY_VARIABLE_TYPENAME,dazzle_type_name);
  1199. csave.End_Chunk();
  1200. }
  1201. /*
  1202. ** DazzleRenderObj save-load.
  1203. */
  1204. const PersistFactoryClass & DazzleRenderObjClass::Get_Factory (void) const
  1205. {
  1206. return _DazzleFactory;
  1207. }
  1208. /**********************************************************************************************
  1209. **
  1210. ** DazzleLayerClass Implementation
  1211. **
  1212. **********************************************************************************************/
  1213. DazzleLayerClass::DazzleLayerClass(void) :
  1214. visible_lists(NULL)
  1215. {
  1216. // Generate an array with one visible list for each type.
  1217. // NOTE - this means that this constructor must be called AFTER all types
  1218. // are initialized
  1219. WWASSERT(type_count);
  1220. visible_lists = W3DNEWARRAY DazzleRenderObjClass *[type_count];
  1221. for (unsigned int i = 0; i < type_count; i++) {
  1222. visible_lists[i] = NULL;
  1223. }
  1224. }
  1225. DazzleLayerClass::~DazzleLayerClass(void)
  1226. {
  1227. // NOTE - this destructor must be called BEFORE DeInit().
  1228. WWASSERT(type_count);
  1229. for (unsigned int i = 0; i < type_count; i++) {
  1230. Clear_Visible_List(i);
  1231. }
  1232. delete [] visible_lists;
  1233. }
  1234. void DazzleLayerClass::Render(CameraClass* camera)
  1235. {
  1236. if (!camera) return;
  1237. camera->Apply();
  1238. unsigned time_ms=WW3D::Get_Frame_Time();
  1239. if (time_ms==0) time_ms=1;
  1240. DX8Wrapper::Set_Material(NULL);
  1241. for (unsigned type=0;type<type_count;++type) {
  1242. if (!types[type]) continue;
  1243. int count = Get_Visible_Item_Count(type);
  1244. if (!count) continue;
  1245. DazzleRenderObjClass* n = visible_lists[type];
  1246. while (n) {
  1247. n->Render_Dazzle(camera);
  1248. n=n->Succ();
  1249. }
  1250. // Must clear the visible list at the end of each render.
  1251. Clear_Visible_List(type);
  1252. }
  1253. }
  1254. // ----------------------------------------------------------------------------
  1255. int DazzleLayerClass::Get_Visible_Item_Count(unsigned int type) const
  1256. {
  1257. if (type >= type_count) {
  1258. WWASSERT(0);
  1259. return 0;
  1260. }
  1261. int count=0;
  1262. DazzleRenderObjClass* n = visible_lists[type];
  1263. while (n) {
  1264. count++;
  1265. n=n->Succ();
  1266. }
  1267. return count;
  1268. }
  1269. // ----------------------------------------------------------------------------
  1270. void DazzleLayerClass::Clear_Visible_List(unsigned int type)
  1271. {
  1272. if (type >= type_count) {
  1273. WWASSERT(0);
  1274. return;
  1275. }
  1276. DazzleRenderObjClass* n = visible_lists[type];
  1277. while (n) {
  1278. n->on_list = false;
  1279. n=n->Succ();
  1280. }
  1281. visible_lists[type] = NULL;
  1282. }
  1283. /**********************************************************************************************
  1284. **
  1285. ** DazzleVisibilityClass Implementation
  1286. ** The default dazzle visibility handler asks the scene to determine whether the dazzle
  1287. ** is occluded.
  1288. **
  1289. **********************************************************************************************/
  1290. float DazzleVisibilityClass::Compute_Dazzle_Visibility
  1291. (
  1292. RenderInfoClass & rinfo,
  1293. DazzleRenderObjClass * dazzle,
  1294. const Vector3 & point
  1295. ) const
  1296. {
  1297. /*
  1298. ** Look up the scene this dazzle is in
  1299. */
  1300. SceneClass * scene = dazzle->Get_Scene();
  1301. RenderObjClass * container = dazzle->Get_Container();
  1302. while ((scene == NULL) && (container != NULL)) {
  1303. scene = container->Get_Scene();
  1304. container = container->Get_Container();
  1305. }
  1306. /*
  1307. ** If we found the scene (we SHOULD!) then ask it to compute the visibility
  1308. */
  1309. if (scene != NULL) {
  1310. float value = scene->Compute_Point_Visibility(rinfo,point);
  1311. scene->Release_Ref();
  1312. return value;
  1313. } else {
  1314. return 1.0f;
  1315. }
  1316. }
  1317. /**********************************************************************************************
  1318. **
  1319. ** DazzlePrototypeClass Implementation
  1320. **
  1321. **********************************************************************************************/
  1322. RenderObjClass * DazzlePrototypeClass::Create(void)
  1323. {
  1324. return NEW_REF(DazzleRenderObjClass,(DazzleType));
  1325. }
  1326. WW3DErrorType DazzlePrototypeClass::Load_W3D(ChunkLoadClass & cload)
  1327. {
  1328. StringClass dazzle_type;
  1329. while (cload.Open_Chunk()) {
  1330. switch (cload.Cur_Chunk_ID())
  1331. {
  1332. READ_WWSTRING_CHUNK(cload,W3D_CHUNK_DAZZLE_NAME,Name);
  1333. READ_WWSTRING_CHUNK(cload,W3D_CHUNK_DAZZLE_TYPENAME,dazzle_type);
  1334. default:
  1335. break;
  1336. }
  1337. cload.Close_Chunk();
  1338. }
  1339. DazzleType = DazzleRenderObjClass::Get_Type_ID(dazzle_type);
  1340. if (DazzleType == UINT_MAX) {
  1341. DazzleType = 0;
  1342. }
  1343. return WW3D_ERROR_OK;
  1344. }
  1345. /**********************************************************************************************
  1346. **
  1347. ** DazzleLoaderClass Implementation
  1348. **
  1349. **********************************************************************************************/
  1350. PrototypeClass * DazzleLoaderClass::Load_W3D(ChunkLoadClass & cload)
  1351. {
  1352. DazzlePrototypeClass * new_proto = W3DNEW DazzlePrototypeClass;
  1353. new_proto->Load_W3D(cload);
  1354. return new_proto;
  1355. }