MLRLight.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "MLRHeaders.hpp"
  5. //#############################################################################
  6. //########################### MLRLight ##################################
  7. //#############################################################################
  8. MLRLight::ClassData*
  9. MLRLight::DefaultData = NULL;
  10. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. //
  12. void
  13. MLRLight::InitializeClass()
  14. {
  15. Verify(!DefaultData);
  16. Verify(gos_GetCurrentHeap() == StaticHeap);
  17. DefaultData =
  18. new ClassData(
  19. MLRLightClassID,
  20. "MidLevelRenderer::MLRLight",
  21. RegisteredClass::DefaultData
  22. );
  23. Register_Object(DefaultData);
  24. }
  25. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. //
  27. void
  28. MLRLight::TerminateClass()
  29. {
  30. Unregister_Object(DefaultData);
  31. delete DefaultData;
  32. DefaultData = NULL;
  33. }
  34. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. //
  36. MLRLight::MLRLight(ClassData *class_data) :
  37. RegisteredClass(class_data)
  38. {
  39. Verify(gos_GetCurrentHeap() == Heap);
  40. intensity = 1.0f;
  41. lightToWorld = LinearMatrix4D::Identity;
  42. lightToShape = LinearMatrix4D::Identity;
  43. color = RGBColor(0.0f, 0.0f, 0.0f);
  44. }
  45. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  46. //
  47. MLRLight::MLRLight(
  48. ClassData *class_data,
  49. Stuff::MemoryStream *stream,
  50. int version
  51. ) :
  52. RegisteredClass(class_data)
  53. {
  54. Check_Object(stream);
  55. Verify(gos_GetCurrentHeap() == Heap);
  56. LinearMatrix4D matrix;
  57. *stream >> intensity >> color >> matrix;
  58. if(version>=9)
  59. {
  60. *stream >> lightName;
  61. }
  62. else
  63. {
  64. lightName = "Light";
  65. }
  66. SetLightToWorldMatrix(matrix);
  67. }
  68. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  69. //
  70. MLRLight::MLRLight(
  71. ClassData *class_data,
  72. Stuff::Page *page
  73. ) :
  74. RegisteredClass(class_data)
  75. {
  76. Check_Object(page);
  77. Verify(gos_GetCurrentHeap() == Heap);
  78. lightName = page->GetName();
  79. intensity = 1.0f;
  80. page->GetEntry("Intensity", &intensity);
  81. color = RGBColor(0.0f, 0.0f, 0.0f);
  82. page->GetEntry("Color", &color);
  83. LinearMatrix4D matrix(true);
  84. Point3D position = Point3D::Identity;
  85. page->GetEntry("Position", &position);
  86. matrix.BuildTranslation(position);
  87. Vector3D direction(0.0f, 0.0f, 1.0f);
  88. page->GetEntry("Direction", &direction);
  89. matrix.AlignLocalAxisToWorldVector(direction, Z_Axis, Y_Axis, X_Axis);
  90. SetLightToWorldMatrix(matrix);
  91. }
  92. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. //
  94. MLRLight::~MLRLight()
  95. {
  96. }
  97. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  98. //
  99. MLRLight*
  100. MLRLight::Make(
  101. Stuff::MemoryStream *stream,
  102. int version
  103. )
  104. {
  105. gos_PushCurrentHeap(Heap);
  106. int type;
  107. MLRLight *light = NULL;
  108. *stream >> type;
  109. switch (type)
  110. {
  111. case AmbientLight:
  112. light = new MLRAmbientLight(stream, version);
  113. break;
  114. case InfiniteLight:
  115. light = new MLRInfiniteLight(MLRInfiniteLight::DefaultData, stream, version);
  116. break;
  117. case PointLight:
  118. light = new MLRPointLight(stream, version);
  119. break;
  120. case SpotLight:
  121. light = new MLRSpotLight(stream, version);
  122. break;
  123. case LookUpLight:
  124. light = new MLRLookUpLight(stream, version);
  125. break;
  126. default:
  127. STOP(("Bad light type"));
  128. }
  129. gos_PopCurrentHeap();
  130. return light;
  131. }
  132. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. //
  134. MLRLight*
  135. MLRLight::Make(Stuff::Page *page)
  136. {
  137. gos_PushCurrentHeap(Heap);
  138. const char* type;
  139. page->GetEntry("LightType", &type, true);
  140. MLRLight *light = NULL;
  141. if (!_stricmp(type, "Ambient"))
  142. light = new MLRAmbientLight(page);
  143. else if (!_stricmp(type, "Infinite"))
  144. light = new MLRInfiniteLight(MLRInfiniteLight::DefaultData, page);
  145. else if (!_stricmp(type, "Point"))
  146. light = new MLRPointLight(page);
  147. else if (!_stricmp(type, "Spot"))
  148. light = new MLRSpotLight(page);
  149. else if (!_stricmp(type, "LookUp"))
  150. light = new MLRLookUpLight(page);
  151. else
  152. STOP(("Bad light type"));
  153. gos_PopCurrentHeap();
  154. return light;
  155. }
  156. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  157. //
  158. void
  159. MLRLight::Save(Stuff::MemoryStream *stream)
  160. {
  161. Check_Object(this);
  162. Check_Object(stream);
  163. *stream << static_cast<int>(GetLightType());
  164. *stream << intensity << color << lightToWorld;
  165. *stream << lightName;
  166. }
  167. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  168. //
  169. void
  170. MLRLight::Write(Stuff::Page *page)
  171. {
  172. Check_Object(this);
  173. Check_Object(page);
  174. switch (GetLightType())
  175. {
  176. case AmbientLight:
  177. page->SetEntry("LightType", "Ambient");
  178. break;
  179. case InfiniteLight:
  180. page->SetEntry("LightType", "Infinite");
  181. break;
  182. case PointLight:
  183. page->SetEntry("LightType", "Point");
  184. break;
  185. case SpotLight:
  186. page->SetEntry("LightType", "Spot");
  187. break;
  188. case LookUpLight:
  189. page->SetEntry("LightType", "LookUp");
  190. break;
  191. }
  192. page->SetEntry("Intensity", intensity);
  193. page->SetEntry("Color", color);
  194. Point3D position(lightToWorld);
  195. page->SetEntry("Position", position);
  196. UnitVector3D direction;
  197. lightToWorld.GetLocalForwardInWorld(&direction);
  198. page->SetEntry("Direction", direction);
  199. }
  200. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  201. //
  202. void
  203. MLRLight::TestInstance()
  204. {
  205. Verify(IsDerivedFrom(DefaultData));
  206. }
  207. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  208. //
  209. void
  210. MLRLight::SetLightToShapeMatrix(const LinearMatrix4D& worldToShape)
  211. {
  212. Check_Object(this);
  213. lightToShape.Multiply(lightToWorld, worldToShape);
  214. }
  215. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  216. //
  217. void
  218. MLRLight::SetLightToWorldMatrix(const LinearMatrix4D& matrix)
  219. {
  220. Check_Object(this);
  221. lightToWorld = matrix;
  222. }
  223. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  224. //
  225. void
  226. MLRLight::SetColor (Scalar r, Scalar g, Scalar b)
  227. {
  228. SetColor(RGBColor(r, b, b));
  229. }
  230. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  231. //
  232. void
  233. MLRLight::GetColor (Scalar& r, Scalar& g, Scalar& b)
  234. {
  235. Check_Object(this);
  236. r = color.red;
  237. g = color.green;
  238. b = color.blue;
  239. }