Shape.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "gosFXHeaders.hpp"
  2. //==========================================================================//
  3. // File: gosFX_Shape.cpp //
  4. // Contents: Base gosFX::Shape Component //
  5. //---------------------------------------------------------------------------//
  6. // Copyright (C) Microsoft Corporation. All rights reserved. //
  7. //===========================================================================//
  8. //
  9. //############################################################################
  10. //######################## gosFX::Shape__Specification #############################
  11. //############################################################################
  12. //------------------------------------------------------------------------------
  13. //
  14. gosFX::Shape__Specification::Shape__Specification(
  15. Stuff::MemoryStream *stream,
  16. int gfx_version
  17. ):
  18. Singleton__Specification(gosFX::ShapeClassID, stream, gfx_version)
  19. {
  20. Check_Pointer(this);
  21. Verify(m_class == ShapeClassID);
  22. Verify(gos_GetCurrentHeap() == Heap);
  23. //
  24. //---------------
  25. // Load the shape
  26. //---------------
  27. //
  28. m_shape =
  29. MidLevelRenderer::MLRShape::Make(
  30. stream,
  31. MidLevelRenderer::ReadMLRVersion(stream)
  32. );
  33. Register_Object(m_shape);
  34. *stream >> m_radius;
  35. }
  36. //------------------------------------------------------------------------------
  37. //
  38. gosFX::Shape__Specification::Shape__Specification(
  39. MidLevelRenderer::MLRShape *shape
  40. ):
  41. Singleton__Specification(gosFX::ShapeClassID)
  42. {
  43. Check_Pointer(this);
  44. Verify(gos_GetCurrentHeap() == Heap);
  45. m_shape = NULL;
  46. SetShape(shape);
  47. }
  48. //------------------------------------------------------------------------------
  49. //
  50. gosFX::Shape__Specification::~Shape__Specification()
  51. {
  52. Check_Object(this);
  53. if (m_shape)
  54. {
  55. Check_Object(m_shape);
  56. m_shape->DetachReference();
  57. }
  58. }
  59. //------------------------------------------------------------------------------
  60. //
  61. gosFX::Shape__Specification*
  62. gosFX::Shape__Specification::Make(
  63. Stuff::MemoryStream *stream,
  64. int gfx_version
  65. )
  66. {
  67. Check_Object(stream);
  68. gos_PushCurrentHeap(Heap);
  69. Shape__Specification *spec =
  70. new gosFX::Shape__Specification(stream, gfx_version);
  71. gos_PopCurrentHeap();
  72. return spec;
  73. }
  74. //------------------------------------------------------------------------------
  75. //
  76. void
  77. gosFX::Shape__Specification::Save(Stuff::MemoryStream *stream)
  78. {
  79. Check_Object(this);
  80. Check_Object(stream);
  81. Singleton__Specification::Save(stream);
  82. MidLevelRenderer::WriteMLRVersion(stream);
  83. m_shape->Save(stream);
  84. *stream << m_radius;
  85. }
  86. //------------------------------------------------------------------------------
  87. //
  88. void
  89. gosFX::Shape__Specification::Copy(Shape__Specification *spec)
  90. {
  91. Check_Object(this);
  92. Check_Object(spec);
  93. Singleton__Specification::Copy(spec);
  94. gos_PushCurrentHeap(Heap);
  95. m_radius = spec->m_radius;
  96. m_shape = spec->m_shape;
  97. gos_PopCurrentHeap();
  98. Check_Object(m_shape);
  99. m_shape->AttachReference();
  100. }
  101. //------------------------------------------------------------------------------
  102. //
  103. void
  104. gosFX::Shape__Specification::SetShape(MidLevelRenderer::MLRShape *shape)
  105. {
  106. Check_Object(this);
  107. //
  108. //------------------------------------
  109. // Detach the old shape if it is there
  110. //------------------------------------
  111. //
  112. if (m_shape)
  113. {
  114. Check_Object(m_shape);
  115. m_shape->DetachReference();
  116. }
  117. //
  118. //------------------------------------
  119. // Attach the new shape if it is there
  120. //------------------------------------
  121. //
  122. if (shape)
  123. {
  124. Check_Object(shape);
  125. m_shape = shape;
  126. m_shape->AttachReference();
  127. //
  128. //-----------------------------------------------------------------
  129. // Get the radius of the bounding sphere. This will be the largest
  130. // distance any point is from the origin
  131. //-----------------------------------------------------------------
  132. //
  133. m_radius = 0.0f;
  134. int count = m_shape->GetNum();
  135. for (int i=0; i<count; ++i)
  136. {
  137. MidLevelRenderer::MLRPrimitiveBase *primitive = m_shape->Find(i);
  138. Check_Object(primitive);
  139. Stuff::Point3D *points;
  140. int vertex_count;
  141. primitive->GetCoordData(&points, &vertex_count);
  142. for (int v=0; v<vertex_count; ++v)
  143. {
  144. Stuff::Scalar len = points[v].GetLengthSquared();
  145. if (len > m_radius)
  146. m_radius = len;
  147. }
  148. }
  149. m_radius = Stuff::Sqrt(m_radius);
  150. }
  151. }
  152. //############################################################################
  153. //############################## gosFX::Shape ################################
  154. //############################################################################
  155. gosFX::Shape::ClassData*
  156. gosFX::Shape::DefaultData = NULL;
  157. //------------------------------------------------------------------------------
  158. //
  159. void
  160. gosFX::Shape::InitializeClass()
  161. {
  162. Verify(!DefaultData);
  163. Verify(gos_GetCurrentHeap() == Heap);
  164. DefaultData =
  165. new ClassData(
  166. ShapeClassID,
  167. "gosFX::Shape",
  168. Singleton::DefaultData,
  169. (Effect::Factory)&Make,
  170. (Specification::Factory)&Specification::Make
  171. );
  172. Register_Object(DefaultData);
  173. }
  174. //------------------------------------------------------------------------------
  175. //
  176. void
  177. gosFX::Shape::TerminateClass()
  178. {
  179. Unregister_Object(DefaultData);
  180. delete DefaultData;
  181. DefaultData = NULL;
  182. }
  183. //------------------------------------------------------------------------------
  184. //
  185. gosFX::Shape::Shape(
  186. Specification *spec,
  187. unsigned flags
  188. ):
  189. Singleton(DefaultData, spec, flags)
  190. {
  191. Verify(gos_GetCurrentHeap() == Heap);
  192. m_radius = spec->m_radius;
  193. }
  194. //------------------------------------------------------------------------------
  195. //
  196. gosFX::Shape*
  197. gosFX::Shape::Make(
  198. Specification *spec,
  199. unsigned flags
  200. )
  201. {
  202. Check_Object(spec);
  203. gos_PushCurrentHeap(Heap);
  204. Shape *cloud = new gosFX::Shape(spec, flags);
  205. gos_PopCurrentHeap();
  206. return cloud;
  207. }
  208. //------------------------------------------------------------------------------
  209. //
  210. void gosFX::Shape::Draw(DrawInfo *info)
  211. {
  212. Check_Object(this);
  213. Check_Object(info);
  214. Check_Object(info->m_parentToWorld);
  215. //
  216. //----------------------------
  217. // Set up the common draw info
  218. //----------------------------
  219. //
  220. MidLevelRenderer::DrawScalableShapeInformation dinfo;
  221. MidLevelRenderer::MLRShape *shape = GetSpecification()->m_shape;
  222. dinfo.clippingFlags.SetClippingState(0x3f);
  223. dinfo.worldToShape = NULL;
  224. Specification *spec = GetSpecification();
  225. Check_Object(spec);
  226. dinfo.state.Combine(info->m_state, spec->m_state);
  227. dinfo.activeLights = NULL;
  228. dinfo.nrOfActiveLights = 0;
  229. dinfo.shape = shape;
  230. Stuff::Vector3D scale(m_scale, m_scale, m_scale);
  231. dinfo.scaling = &scale;
  232. dinfo.paintMe = &m_color;
  233. Stuff::LinearMatrix4D local_to_world;
  234. local_to_world.Multiply(m_localToParent, *info->m_parentToWorld);
  235. dinfo.shapeToWorld = &local_to_world;
  236. //
  237. //--------------------------------------------------------------
  238. // Check the orientation mode. The first case is XY orientation
  239. //--------------------------------------------------------------
  240. //
  241. if (spec->m_alignZUsingX)
  242. {
  243. Stuff::Point3D
  244. camera_in_world(info->m_clipper->GetCameraToWorldMatrix());
  245. Stuff::Point3D card_in_world(local_to_world);
  246. Stuff::Vector3D look_at;
  247. look_at.Subtract(camera_in_world, card_in_world);
  248. if (spec->m_alignZUsingY)
  249. local_to_world.AlignLocalAxisToWorldVector(
  250. look_at,
  251. Stuff::Z_Axis,
  252. Stuff::Y_Axis,
  253. Stuff::X_Axis
  254. );
  255. else
  256. local_to_world.AlignLocalAxisToWorldVector(
  257. look_at,
  258. Stuff::Z_Axis,
  259. Stuff::X_Axis,
  260. -1
  261. );
  262. }
  263. //
  264. //-------------------------------------------------------
  265. // Each matrix needs to be aligned to the camera around Y
  266. //-------------------------------------------------------
  267. //
  268. else if (spec->m_alignZUsingY)
  269. {
  270. Stuff::Point3D
  271. camera_in_world(info->m_clipper->GetCameraToWorldMatrix());
  272. Stuff::Point3D card_in_world(local_to_world);
  273. Stuff::Vector3D look_at;
  274. look_at.Subtract(camera_in_world, card_in_world);
  275. local_to_world.AlignLocalAxisToWorldVector(
  276. look_at,
  277. Stuff::Z_Axis,
  278. Stuff::Y_Axis,
  279. -1
  280. );
  281. }
  282. //
  283. //----------------------------
  284. // Let our parent do its thing
  285. //----------------------------
  286. //
  287. info->m_clipper->DrawScalableShape(&dinfo);
  288. Singleton::Draw(info);
  289. }
  290. //------------------------------------------------------------------------------
  291. //
  292. void
  293. gosFX::Shape::TestInstance() const
  294. {
  295. Verify(IsDerivedFrom(DefaultData));
  296. }