PointLight.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "gosFXHeaders.hpp"
  5. #include <MLR\MLRPointLight.hpp>
  6. gosFX::LightManager
  7. *gosFX::LightManager::Instance = NULL;
  8. gosFX::Light*
  9. gosFX::LightManager::MakePointLight(const char* light_map)
  10. {
  11. return reinterpret_cast<Light*>(this);
  12. }
  13. void
  14. gosFX::LightManager::ChangeLight(
  15. Light *light,
  16. Info *info
  17. )
  18. {
  19. }
  20. void
  21. gosFX::LightManager::DeleteLight(Light *light)
  22. {
  23. }
  24. //############################################################################
  25. //######################## gosFX::PointLight__Specification #############################
  26. //############################################################################
  27. //------------------------------------------------------------------------------
  28. //
  29. gosFX::PointLight__Specification::PointLight__Specification(
  30. Stuff::MemoryStream *stream,
  31. int gfx_version
  32. ):
  33. Effect__Specification(PointLightClassID, stream, gfx_version)
  34. {
  35. Check_Pointer(this);
  36. Verify(m_class == PointLightClassID);
  37. Verify(gos_GetCurrentHeap() == Heap);
  38. m_red.Load(stream, gfx_version);
  39. m_green.Load(stream, gfx_version);
  40. m_blue.Load(stream, gfx_version);
  41. m_intensity.Load(stream, gfx_version);
  42. if (gfx_version>15)
  43. {
  44. m_innerRadius.Load(stream, gfx_version);
  45. m_outerRadius.Load(stream, gfx_version);
  46. if (gfx_version>16)
  47. *stream >> m_lightMap;
  48. }
  49. else
  50. {
  51. m_innerRadius.SetCurve(0.0f);
  52. m_outerRadius.SetCurve(25.0f);
  53. }
  54. *stream >> m_twoSided;
  55. }
  56. //------------------------------------------------------------------------------
  57. //
  58. gosFX::PointLight__Specification::PointLight__Specification():
  59. Effect__Specification(PointLightClassID)
  60. {
  61. Check_Pointer(this);
  62. Verify(gos_GetCurrentHeap() == Heap);
  63. m_twoSided = false;
  64. }
  65. //------------------------------------------------------------------------------
  66. //
  67. gosFX::PointLight__Specification*
  68. gosFX::PointLight__Specification::Make(
  69. Stuff::MemoryStream *stream,
  70. int gfx_version
  71. )
  72. {
  73. Check_Object(stream);
  74. gos_PushCurrentHeap(Heap);
  75. PointLight__Specification *spec =
  76. new gosFX::PointLight__Specification(stream, gfx_version);
  77. gos_PopCurrentHeap();
  78. return spec;
  79. }
  80. //------------------------------------------------------------------------------
  81. //
  82. void
  83. gosFX::PointLight__Specification::Save(Stuff::MemoryStream *stream)
  84. {
  85. Check_Object(this);
  86. Check_Object(stream);
  87. Effect__Specification::Save(stream);
  88. m_red.Save(stream);
  89. m_green.Save(stream);
  90. m_blue.Save(stream);
  91. m_intensity.Save(stream);
  92. m_innerRadius.Save(stream);
  93. m_outerRadius.Save(stream);
  94. *stream << m_lightMap << m_twoSided;
  95. }
  96. //------------------------------------------------------------------------------
  97. //
  98. void
  99. gosFX::PointLight__Specification::BuildDefaults()
  100. {
  101. Check_Object(this);
  102. Effect__Specification::BuildDefaults();
  103. m_red.SetCurve(1.0f);
  104. m_green.SetCurve(1.0f);
  105. m_blue.SetCurve(1.0f);
  106. m_intensity.SetCurve(1.0f);
  107. m_innerRadius.SetCurve(0.0f);
  108. m_outerRadius.SetCurve(10.0f);
  109. m_twoSided = false;
  110. }
  111. //------------------------------------------------------------------------------
  112. //
  113. bool
  114. gosFX::PointLight__Specification::IsDataValid(bool fix_data)
  115. {
  116. Check_Object(this);
  117. return Effect__Specification::IsDataValid(fix_data);
  118. /*
  119. m_red.SetCurve(1.0f);
  120. m_green.SetCurve(1.0f);
  121. m_blue.SetCurve(1.0f);
  122. m_intensity.SetCurve(1.0f);
  123. m_innerRadius.SetCurve(0.0f);
  124. m_outerRadius.SetCurve(10.0f);
  125. m_twoSided = false;
  126. */
  127. }
  128. //------------------------------------------------------------------------------
  129. //
  130. void
  131. gosFX::PointLight__Specification::Copy(PointLight__Specification *spec)
  132. {
  133. Check_Object(this);
  134. Check_Object(spec);
  135. Effect__Specification::Copy(spec);
  136. gos_PushCurrentHeap(Heap);
  137. m_red = spec->m_red;
  138. m_green = spec->m_green;
  139. m_blue = spec->m_blue;
  140. m_intensity = spec->m_intensity;
  141. m_twoSided = spec->m_twoSided;
  142. m_lightMap = spec->m_lightMap;
  143. gos_PopCurrentHeap();
  144. }
  145. //############################################################################
  146. //############################## gosFX::PointLight ################################
  147. //############################################################################
  148. gosFX::PointLight::ClassData*
  149. gosFX::PointLight::DefaultData = NULL;
  150. //------------------------------------------------------------------------------
  151. //
  152. void
  153. gosFX::PointLight::InitializeClass()
  154. {
  155. Verify(!DefaultData);
  156. Verify(gos_GetCurrentHeap() == Heap);
  157. DefaultData =
  158. new ClassData(
  159. PointLightClassID,
  160. "gosFX::PointLight",
  161. Effect::DefaultData,
  162. (Effect::Factory)&Make,
  163. (Specification::Factory)&Specification::Make
  164. );
  165. Register_Object(DefaultData);
  166. }
  167. //------------------------------------------------------------------------------
  168. //
  169. void
  170. gosFX::PointLight::TerminateClass()
  171. {
  172. if ( DefaultData )
  173. {
  174. Unregister_Object(DefaultData);
  175. delete DefaultData;
  176. DefaultData = NULL;
  177. }
  178. }
  179. //------------------------------------------------------------------------------
  180. //
  181. gosFX::PointLight::PointLight(
  182. Specification *spec,
  183. unsigned flags
  184. ):
  185. Effect(DefaultData, spec, flags)
  186. {
  187. Check_Object(spec);
  188. Verify(gos_GetCurrentHeap() == Heap);
  189. }
  190. //------------------------------------------------------------------------------
  191. //
  192. gosFX::PointLight::~PointLight()
  193. {
  194. if (m_light)
  195. {
  196. Check_Pointer(m_light);
  197. LightManager::Instance->DeleteLight(m_light);
  198. }
  199. }
  200. //------------------------------------------------------------------------------
  201. //
  202. gosFX::PointLight*
  203. gosFX::PointLight::Make(
  204. Specification *spec,
  205. unsigned flags
  206. )
  207. {
  208. Check_Object(spec);
  209. gos_PushCurrentHeap(Heap);
  210. PointLight *obj = new gosFX::PointLight(spec, flags);
  211. gos_PopCurrentHeap();
  212. return obj;
  213. }
  214. //------------------------------------------------------------------------------
  215. //
  216. void
  217. gosFX::PointLight::Start(ExecuteInfo *info)
  218. {
  219. Check_Object(this);
  220. Check_Object(info);
  221. Effect::Start(info);
  222. Specification *spec = GetSpecification();
  223. Check_Object(spec);
  224. Check_Object(LightManager::Instance);
  225. m_light = LightManager::Instance->MakePointLight(spec->m_lightMap);
  226. Check_Pointer(m_light);
  227. }
  228. //------------------------------------------------------------------------------
  229. //
  230. bool
  231. gosFX::PointLight::Execute(ExecuteInfo *info)
  232. {
  233. Check_Object(this);
  234. Check_Object(info);
  235. if (!IsExecuted())
  236. return false;
  237. //
  238. //------------------------
  239. // Do the effect animation
  240. //------------------------
  241. //
  242. if (!Effect::Execute(info))
  243. return false;
  244. //
  245. //-----------------------------------------
  246. // Animate the parent then get our pointers
  247. //-----------------------------------------
  248. //
  249. Specification *spec = GetSpecification();
  250. Check_Object(spec);
  251. //
  252. //--------------------------------
  253. // Animate the color and intensity
  254. //--------------------------------
  255. //
  256. Stuff::LinearMatrix4D local_to_world;
  257. //
  258. //-----------------------------------------------------------
  259. // Construct the info object and send it to the light manager
  260. //-----------------------------------------------------------
  261. //
  262. LightManager::Info light_info;
  263. light_info.m_color.red = spec->m_red.ComputeValue(m_age, m_seed);
  264. light_info.m_color.green = spec->m_green.ComputeValue(m_age, m_seed);
  265. light_info.m_color.blue = spec->m_blue.ComputeValue(m_age, m_seed);
  266. light_info.m_origin.Multiply(m_localToParent, *info->m_parentToWorld);
  267. light_info.m_intensity = spec->m_intensity.ComputeValue(m_age, m_seed);
  268. light_info.m_inner = spec->m_innerRadius.ComputeValue(m_age, m_seed);
  269. light_info.m_outer = spec->m_outerRadius.ComputeValue(m_age, m_seed);
  270. Check_Pointer(m_light);
  271. Check_Object(LightManager::Instance);
  272. LightManager::Instance->ChangeLight(m_light, &light_info);
  273. return true;
  274. }
  275. //------------------------------------------------------------------------------
  276. //
  277. void
  278. gosFX::PointLight::Kill()
  279. {
  280. Check_Object(this);
  281. Effect::Kill();
  282. Check_Pointer(m_light);
  283. LightManager::Instance->DeleteLight(m_light);
  284. m_light = NULL;
  285. }
  286. //------------------------------------------------------------------------------
  287. //
  288. void
  289. gosFX::PointLight::TestInstance() const
  290. {
  291. Verify(IsDerivedFrom(DefaultData));
  292. }