Environment.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. #define CC4_ENV CC4('E','N','V',0)
  6. /******************************************************************************/
  7. DEFINE_CACHE(Environment, Environments, EnvironmentPtr, "Environment");
  8. /******************************************************************************/
  9. // AMBIENT
  10. /******************************************************************************/
  11. void Environment::Ambient::set ()C {D.ambientColor(on ? color : VecZero); D.nightShadeColor(on ? night_shade_color : VecZero);}
  12. void Environment::Ambient::get () {color=D.ambientColor(); night_shade_color=D.nightShadeColor(); on=(color.any() || night_shade_color.any());}
  13. void Environment::Ambient::reset() {on=true; color=0.4f; night_shade_color.zero();}
  14. Bool Environment::Ambient::save(File &f, CChar *path)C
  15. {
  16. f.cmpUIntV(1); // version
  17. f<<on<<color<<night_shade_color;
  18. return f.ok();
  19. }
  20. Bool Environment::Ambient::load(File &f, CChar *path)
  21. {
  22. switch(f.decUIntV())
  23. {
  24. case 1:
  25. {
  26. f>>on>>color>>night_shade_color;
  27. if(f.ok())return true;
  28. }break;
  29. case 0:
  30. {
  31. f>>on>>color; night_shade_color.zero();
  32. if(f.ok())return true;
  33. }break;
  34. }
  35. reset(); return false;
  36. }
  37. /******************************************************************************/
  38. // BLOOM
  39. /******************************************************************************/
  40. void Environment::Bloom::set()C
  41. {
  42. D.bloomOriginal(on ? original : 1).bloomScale(on ? scale : 0).bloomCut(cut).bloomSaturate(saturate).bloomMaximum(maximum).bloomHalf(half).bloomBlurs(blurs);
  43. }
  44. void Environment::Bloom::get()
  45. {
  46. original=D.bloomOriginal(); scale=D.bloomScale(); cut=D.bloomCut(); saturate=D.bloomSaturate(); maximum=D.bloomMaximum(); half=D.bloomHalf(); blurs=D.bloomBlurs();
  47. on=!(Equal(original, 1) && Equal(scale, 0));
  48. }
  49. void Environment::Bloom::reset()
  50. {
  51. on=true; half=true; saturate=true; maximum=false; blurs=1; original=1.0f; scale=0.5f; cut=0.3f;
  52. }
  53. Bool Environment::Bloom::save(File &f, CChar *path)C
  54. {
  55. f.cmpUIntV(2); // version
  56. f<<on<<half<<saturate<<maximum<<blurs<<original<<scale<<cut;
  57. return f.ok();
  58. }
  59. Bool Environment::Bloom::load(File &f, CChar *path)
  60. {
  61. switch(f.decUIntV())
  62. {
  63. case 2:
  64. {
  65. f>>on>>half>>saturate>>maximum>>blurs>>original>>scale>>cut;
  66. if(f.ok())return true;
  67. }break;
  68. case 1:
  69. {
  70. Flt contrast; f>>on>>half>>maximum>>blurs>>original>>scale>>cut>>contrast; saturate=true;
  71. if(f.ok())return true;
  72. }break;
  73. case 0:
  74. {
  75. Flt contrast; f>>on>>half>>maximum>>blurs>>original>>scale>>cut>>contrast; saturate=true; scale*=2;
  76. if(f.ok())return true;
  77. }break;
  78. }
  79. reset(); return false;
  80. }
  81. /******************************************************************************/
  82. // CLOUDS
  83. /******************************************************************************/
  84. void Environment::Clouds::set()C
  85. {
  86. Int active=0; FREPA(layers)if(layers[i].image)
  87. {
  88. C Layer &src = layers[i ];
  89. LayeredClouds::Layer &dest=::Clouds.layered.layer [active++];
  90. dest.scale=src.scale; dest.velocity=src.velocity; dest.color=src.color; dest.image=src.image;
  91. }
  92. ::Clouds.layered.set(on ? active : false);
  93. ::Clouds.layered.scaleY(vertical_scale);
  94. ::Clouds.layered.rayMaskContrast(ray_mask_contrast);
  95. }
  96. void Environment::Clouds::get()
  97. {
  98. REPA(layers)
  99. {
  100. C LayeredClouds::Layer &src =::Clouds.layered.layer [i];
  101. Layer &dest= layers[i];
  102. dest.scale=src.scale; dest.velocity=src.velocity; dest.color=src.color.asVec4(); dest.image=src.image;
  103. }
  104. on=(::Clouds.layered.layers()>0);
  105. vertical_scale =::Clouds.layered.scaleY();
  106. ray_mask_contrast=::Clouds.layered.rayMaskContrast();
  107. }
  108. void Environment::Clouds::reset()
  109. {
  110. on=true; vertical_scale=1.05f; ray_mask_contrast=4;
  111. REPA(layers){Layer &layer=layers[i]; layer.color=1; layer.image=null;}
  112. layers[0].scale=1.0f/2.8f; layers[0].velocity=0.010f;
  113. layers[1].scale=1.0f/2.4f; layers[1].velocity=0.008f;
  114. layers[2].scale=1.0f/2.0f; layers[2].velocity=0.006f;
  115. layers[3].scale=1.0f/1.6f; layers[3].velocity=0.004f;
  116. }
  117. Bool Environment::Clouds::save(File &f, CChar *path)C
  118. {
  119. f.cmpUIntV(0); // version
  120. f<<on<<vertical_scale<<ray_mask_contrast;
  121. FREPA(layers)
  122. {
  123. C Layer &layer=layers[i]; f<<layer.scale<<layer.velocity<<layer.color; f._putStr(layer.image.name(path));
  124. }
  125. return f.ok();
  126. }
  127. Bool Environment::Clouds::load(File &f, CChar *path)
  128. {
  129. switch(f.decUIntV())
  130. {
  131. case 0:
  132. {
  133. f>>on>>vertical_scale>>ray_mask_contrast;
  134. FREPA(layers)
  135. {
  136. Layer &layer=layers[i]; f>>layer.scale>>layer.velocity>>layer.color; layer.image.require(f._getStr(), path);
  137. }
  138. if(f.ok())return true;
  139. }break;
  140. }
  141. reset(); return false;
  142. }
  143. /******************************************************************************/
  144. // FOG
  145. /******************************************************************************/
  146. void Environment::Fog::set ()C {::Fog.draw=on; ::Fog.affect_sky=affect_sky; ::Fog.density=density; ::Fog.color=color;}
  147. void Environment::Fog::get () {on=::Fog.draw; affect_sky=::Fog.affect_sky; density=::Fog.density; color=::Fog.color;}
  148. void Environment::Fog::reset() {on=false; affect_sky=false; density=0.02f; color=0.5f;}
  149. Bool Environment::Fog::save(File &f, CChar *path)C
  150. {
  151. f.cmpUIntV(0); // version
  152. f<<on<<affect_sky<<density<<color;
  153. return f.ok();
  154. }
  155. Bool Environment::Fog::load(File &f, CChar *path)
  156. {
  157. switch(f.decUIntV())
  158. {
  159. case 0:
  160. {
  161. f>>on>>affect_sky>>density>>color;
  162. if(f.ok())return true;
  163. }break;
  164. }
  165. reset(); return false;
  166. }
  167. /******************************************************************************/
  168. // SKY
  169. /******************************************************************************/
  170. void Environment::Sky::set()C
  171. {
  172. ::Sky.frac(frac)
  173. .atmosphericDensityExponent(atmospheric_density_exponent).atmosphericHorizonExponent (atmospheric_horizon_exponent )
  174. .atmosphericHorizonColor (atmospheric_horizon_color ).atmosphericSkyColor (atmospheric_sky_color )
  175. .atmosphericStars (atmospheric_stars ).atmosphericStarsOrientation(atmospheric_stars_orientation);
  176. if(!on )::Sky.clear();else
  177. if( skybox)::Sky.skybox(skybox);else
  178. ::Sky.atmospheric();
  179. }
  180. void Environment::Sky::get()
  181. {
  182. on=::Sky._is;
  183. frac=::Sky.frac();
  184. skybox=::Sky.skybox();
  185. atmospheric_density_exponent=::Sky.atmosphericDensityExponent(); atmospheric_horizon_exponent =::Sky.atmosphericHorizonExponent ();
  186. atmospheric_horizon_color =::Sky.atmosphericHorizonColor (); atmospheric_sky_color =::Sky.atmosphericSkyColor ();
  187. atmospheric_stars =::Sky.atmosphericStars (); atmospheric_stars_orientation=::Sky.atmosphericStarsOrientation();
  188. }
  189. void Environment::Sky::reset()
  190. {
  191. on=true;
  192. frac=0.8f;
  193. skybox=null;
  194. atmospheric_density_exponent=1;
  195. atmospheric_horizon_exponent=3.5f;
  196. atmospheric_horizon_color.set(0.32f, 0.46f, 0.58f, 1.0f);
  197. atmospheric_sky_color .set(0.16f, 0.36f, 0.54f, 1.0f);
  198. atmospheric_stars =null;
  199. atmospheric_stars_orientation.identity();
  200. }
  201. Bool Environment::Sky::save(File &f, CChar *path)C
  202. {
  203. f.cmpUIntV(0); // version
  204. f<<on<<frac<<atmospheric_density_exponent<<atmospheric_horizon_exponent<<atmospheric_horizon_color<<atmospheric_sky_color<<atmospheric_stars_orientation;
  205. f._putStr(atmospheric_stars.name(path))._putStr(skybox.name(path));
  206. return f.ok();
  207. }
  208. Bool Environment::Sky::load(File &f, CChar *path)
  209. {
  210. switch(f.decUIntV())
  211. {
  212. case 0:
  213. {
  214. f>>on>>frac>>atmospheric_density_exponent>>atmospheric_horizon_exponent>>atmospheric_horizon_color>>atmospheric_sky_color>>atmospheric_stars_orientation;
  215. atmospheric_stars.require(f._getStr(), path);
  216. skybox .require(f._getStr(), path);
  217. if(f.ok())return true;
  218. }break;
  219. }
  220. reset(); return false;
  221. }
  222. /******************************************************************************/
  223. // SUN
  224. /******************************************************************************/
  225. void Environment::Sun::set()C
  226. {
  227. ::Sun.draw =on;
  228. ::Sun.blend =blend;
  229. ::Sun.glow =glow;
  230. ::Sun.size =size;
  231. ::Sun.highlight_front=highlight_front;
  232. ::Sun.highlight_back =highlight_back;
  233. ::Sun.pos =pos;
  234. ::Sun.light_color =light_color;
  235. ::Sun. rays_color = rays_color;
  236. ::Sun.image_color =image_color;
  237. ::Sun.image =image;
  238. }
  239. void Environment::Sun::get()
  240. {
  241. on =::Sun.draw;
  242. blend =::Sun.blend;
  243. glow =::Sun.glow;
  244. size =::Sun.size;
  245. highlight_front=::Sun.highlight_front;
  246. highlight_back =::Sun.highlight_back;
  247. pos =::Sun.pos;
  248. light_color =::Sun.light_color;
  249. rays_color =::Sun. rays_color;
  250. image_color =::Sun.image_color.asVec4();
  251. image =::Sun.image;
  252. }
  253. void Environment::Sun::reset()
  254. {
  255. on=true; blend=true; glow=128; size=0.15; highlight_front=0.10f; highlight_back=0.07f; pos.set(-SQRT3_3, SQRT3_3, -SQRT3_3); light_color=0.7f; rays_color=0.05f; image_color=1; image=null;
  256. }
  257. Bool Environment::Sun::save(File &f, CChar *path)C
  258. {
  259. f.cmpUIntV(0); // version
  260. f<<on<<blend<<glow<<size<<highlight_front<<highlight_back<<pos<<light_color<<rays_color<<image_color; f._putStr(image.name(path));
  261. return f.ok();
  262. }
  263. Bool Environment::Sun::load(File &f, CChar *path)
  264. {
  265. switch(f.decUIntV())
  266. {
  267. case 0:
  268. {
  269. f>>on>>blend>>glow>>size>>highlight_front>>highlight_back>>pos>>light_color>>rays_color>>image_color; image.require(f._getStr(), path);
  270. if(f.ok())return true;
  271. }break;
  272. }
  273. reset(); return false;
  274. }
  275. /******************************************************************************/
  276. // ENVIRONMENT
  277. /******************************************************************************/
  278. void Environment::set ()C {ambient.set (); bloom.set (); clouds.set (); fog.set (); sky.set (); sun.set ();}
  279. void Environment::get () {ambient.get (); bloom.get (); clouds.get (); fog.get (); sky.get (); sun.get ();}
  280. void Environment::reset() {ambient.reset(); bloom.reset(); clouds.reset(); fog.reset(); sky.reset(); sun.reset();}
  281. Bool Environment::save(File &f, CChar *path)C
  282. {
  283. f.putUInt(CC4_ENV).cmpUIntV(0); // version
  284. if(ambient.save(f, path))
  285. if(bloom .save(f, path))
  286. if(clouds .save(f, path))
  287. if(fog .save(f, path))
  288. if(sky .save(f, path))
  289. if(sun .save(f, path))
  290. return f.ok();
  291. return false;
  292. }
  293. Bool Environment::load(File &f, CChar *path)
  294. {
  295. if(f.getUInt()==CC4_ENV)switch(f.decUIntV())
  296. {
  297. case 0:
  298. {
  299. if(ambient.load(f, path))
  300. if(bloom .load(f, path))
  301. if(clouds .load(f, path))
  302. if(fog .load(f, path))
  303. if(sky .load(f, path))
  304. if(sun .load(f, path))
  305. if(f.ok())return true;
  306. }break;
  307. }
  308. reset(); return false;
  309. }
  310. Bool Environment::save(C Str &name)C
  311. {
  312. File f; if(f.writeTry(name)){if(save(f, _GetPath(name)) && f.flush())return true; f.del(); FDelFile(name);}
  313. return false;
  314. }
  315. Bool Environment::load(C Str &name)
  316. {
  317. File f; if(f.readTry(name))return load(f, _GetPath(name));
  318. reset(); return false;
  319. }
  320. /******************************************************************************/
  321. }
  322. /******************************************************************************/