MLRIndexedPrimitive.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "MLRHeaders.hpp"
  5. //#############################################################################
  6. //##################### MLRIndexedPrimitive #############################
  7. //#############################################################################
  8. MLRIndexedPrimitive::ClassData*
  9. MLRIndexedPrimitive::DefaultData = NULL;
  10. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. //
  12. void
  13. MLRIndexedPrimitive::InitializeClass()
  14. {
  15. Verify(!DefaultData);
  16. DefaultData =
  17. new ClassData(
  18. MLRIndexedPrimitiveClassID,
  19. "MidLevelRenderer::MLRIndexedPrimitive",
  20. MLRPrimitive::DefaultData,
  21. NULL
  22. );
  23. Register_Object(DefaultData);
  24. }
  25. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. //
  27. void
  28. MLRIndexedPrimitive::TerminateClass()
  29. {
  30. Unregister_Object(DefaultData);
  31. delete DefaultData;
  32. DefaultData = NULL;
  33. }
  34. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. //
  36. MLRIndexedPrimitive::MLRIndexedPrimitive(
  37. ClassData *class_data,
  38. MemoryStream *stream,
  39. int version
  40. ):
  41. MLRPrimitive(class_data, stream, version)
  42. {
  43. Check_Pointer(this);
  44. Check_Object(stream);
  45. MemoryStreamIO_Read(stream, &index);
  46. visibleIndexedVerticesKey = false;
  47. visibleIndexedVertices.SetLength(numVertices);
  48. }
  49. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  50. //
  51. void
  52. MLRIndexedPrimitive::Save(MemoryStream *stream)
  53. {
  54. Check_Object(this);
  55. Check_Object(stream);
  56. MLRPrimitive::Save(stream);
  57. MemoryStreamIO_Write(stream, &index);
  58. }
  59. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. //
  61. MLRIndexedPrimitive::MLRIndexedPrimitive(ClassData *class_data):
  62. MLRPrimitive(class_data), index(0)
  63. {
  64. }
  65. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  66. //
  67. MLRIndexedPrimitive::~MLRIndexedPrimitive()
  68. {
  69. }
  70. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71. //
  72. void
  73. MLRIndexedPrimitive::TestInstance() const
  74. {
  75. Verify(IsDerivedFrom(DefaultData));
  76. }
  77. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  78. //
  79. void
  80. MLRIndexedPrimitive::InitializeDrawPrimitive(int vis, int parameter)
  81. {
  82. MLRPrimitive::InitializeDrawPrimitive(vis, parameter);
  83. gos_indices = NULL;
  84. numGOSIndices = -1;
  85. visibleIndexedVerticesKey = false;
  86. int i, len = visibleIndexedVertices.GetLength();
  87. for(i=0;i<len;i++)
  88. {
  89. visibleIndexedVertices[i] = 0;
  90. }
  91. }
  92. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. //
  94. void
  95. MLRIndexedPrimitive::SetCoordData(
  96. const Point3D *data,
  97. int dataSize
  98. )
  99. {
  100. Check_Object(this);
  101. Check_Pointer(data);
  102. Verify(colors.GetLength() == 0 || dataSize == colors.GetLength());
  103. Verify(normals.GetLength() == 0 || dataSize == normals.GetLength());
  104. Verify(texCoords.GetLength() == 0 || dataSize == texCoords.GetLength());
  105. #if defined (MAX_NUMBER_VERTICES)
  106. Verify(dataSize <= MAX_NUMBER_VERTICES);
  107. #endif
  108. coords.AssignData(data, dataSize);
  109. transformedCoords.SetLength(dataSize);
  110. numVertices = dataSize;
  111. if(index.GetLength() > 0 && visibleIndexedVertices.GetLength() != dataSize)
  112. {
  113. visibleIndexedVertices.SetLength(dataSize);
  114. }
  115. }
  116. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  117. //
  118. void
  119. MLRIndexedPrimitive::SetIndexData(
  120. unsigned short *index_array,
  121. int index_count
  122. )
  123. {
  124. Check_Object(this);
  125. Check_Pointer(index_array);
  126. if(coords.GetLength() > 0)
  127. {
  128. visibleIndexedVertices.SetLength(coords.GetLength());
  129. }
  130. index.AssignData(index_array, index_count);
  131. }
  132. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. //
  134. void
  135. MLRIndexedPrimitive::GetIndexData(
  136. unsigned short **index_array,
  137. int *index_count
  138. )
  139. {
  140. Check_Object(this);
  141. *index_array = index.GetData();
  142. *index_count = index.GetLength();
  143. }
  144. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  145. //
  146. void
  147. MLRIndexedPrimitive::Transform(Matrix4D *mat)
  148. {
  149. Check_Object(this);
  150. int i, len = coords.GetLength();
  151. for(i=0;i<len;i++)
  152. {
  153. transformedCoords[i].Multiply(coords[i], *mat);
  154. }
  155. #ifdef LAB_ONLY
  156. Statistics::MLR_TransformedVertices += len;
  157. #endif
  158. }
  159. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  160. //
  161. void
  162. MLRIndexedPrimitive::Lighting (
  163. MLRLight **lights,
  164. int nrLights
  165. )
  166. {
  167. Check_Object(this);
  168. // set the to use colors to the original colors ...
  169. // only lighting could overwrite this;
  170. actualColors = &colors;
  171. if(nrLights == 0)
  172. {
  173. return;
  174. }
  175. if(normals.GetLength() == 0)
  176. {
  177. return;
  178. }
  179. if(lights == NULL)
  180. {
  181. return;
  182. }
  183. switch (GetCurrentState().GetLightingMode())
  184. {
  185. case MLRState::LightingOffMode:
  186. return;
  187. case MLRState::LightingClassicOnlyMode:
  188. {
  189. Verify(colors.GetLength() == litColors.GetLength());
  190. Verify(normals.GetLength() == colors.GetLength());
  191. Verify(coords.GetLength() == colors.GetLength());
  192. int i, k, len = colors.GetLength();
  193. MLRVertexData vertexData;
  194. #if COLOR_AS_DWORD
  195. TO_DO;
  196. #else
  197. RGBAColor *color = &colors[0];
  198. RGBAColor *litColor = &litColors[0];
  199. #if USE_ASSEMBLER_CODE
  200. _asm {
  201. push esi
  202. push edi
  203. mov esi, color
  204. mov edi, litColor
  205. mov ecx, len
  206. _loop1:
  207. mov eax, dword ptr [esi]
  208. mov ebx, dword ptr [esi+4]
  209. mov dword ptr [edi], eax
  210. mov dword ptr [edi+ 4], ebx
  211. mov eax, dword ptr [esi + 8]
  212. mov ebx, dword ptr [esi + 12]
  213. mov dword ptr [edi + 8], eax
  214. mov dword ptr [edi + 12], ebx
  215. add esi,16
  216. add edi,16
  217. dec ecx
  218. jnz _loop1
  219. pop edi
  220. pop esi
  221. }
  222. #else // it doesnt know that ...
  223. memcpy(litColor, color, (len<<2)*sizeof(Scalar));
  224. #endif
  225. #endif
  226. //
  227. //-----------------------------------
  228. // Test each light against the vertex
  229. //-----------------------------------
  230. //
  231. for (i=0;i<nrLights;i++)
  232. {
  233. MLRLight *light = lights[i];
  234. Check_Object(light);
  235. vertexData.point = &coords[0];
  236. vertexData.color = &litColors[0];
  237. vertexData.normal = &normals[0];
  238. for(k=0;k<len;k++)
  239. {
  240. light->LightVertex(vertexData);
  241. vertexData.point++;
  242. vertexData.color++;
  243. vertexData.normal++;
  244. }
  245. }
  246. #ifdef LAB_ONLY
  247. Statistics::MLR_LitVertices += len*nrLights;
  248. #endif
  249. // set the to use colors to the original colors ...
  250. // only lighting could overwrite this;
  251. actualColors = &litColors;
  252. }
  253. break;
  254. case MLRState::LightingLightMapOnlyMode:
  255. {
  256. Verify(state.GetAlphaMode() == MLRState::OneZeroMode);
  257. STOP(("Lightmaps not implemented yet."));
  258. }
  259. break;
  260. case MLRState::LightingClassicAndLightMapMode:
  261. {
  262. Verify(state.GetAlphaMode() == MLRState::OneZeroMode);
  263. Verify(colors.GetLength() == litColors.GetLength());
  264. Verify(normals.GetLength() == colors.GetLength());
  265. Verify(coords.GetLength() == colors.GetLength());
  266. int i, k, len = colors.GetLength();
  267. MLRVertexData vertexData;
  268. #if COLOR_AS_DWORD
  269. TO_DO;
  270. #else
  271. RGBAColor *color = &colors[0];
  272. RGBAColor *litColor = &litColors[0];
  273. #if USE_ASSEMBLER_CODE
  274. _asm {
  275. push esi
  276. push edi
  277. mov esi, color
  278. mov edi, litColor
  279. mov ecx, len
  280. _loop2:
  281. mov eax, dword ptr [esi]
  282. mov ebx, dword ptr [esi+4]
  283. mov dword ptr [edi], eax
  284. mov dword ptr [edi+ 4], ebx
  285. mov eax, dword ptr [esi + 8]
  286. mov ebx, dword ptr [esi + 12]
  287. mov dword ptr [edi + 8], eax
  288. mov dword ptr [edi + 12], ebx
  289. add esi,16
  290. add edi,16
  291. dec ecx
  292. jnz _loop2
  293. pop edi
  294. pop esi
  295. }
  296. #else // it doesnt know that ...
  297. memcpy(litColor, color, (len<<2)*sizeof(Scalar));
  298. #endif
  299. #endif
  300. //
  301. //-----------------------------------
  302. // Test each light against the vertex
  303. //-----------------------------------
  304. //
  305. for (i=0;i<nrLights;i++)
  306. {
  307. MLRLight *light = lights[i];
  308. Check_Object(light);
  309. vertexData.point = &coords[0];
  310. vertexData.color = &litColors[0];
  311. vertexData.normal = &normals[0];
  312. for(k=0;k<len;k++)
  313. {
  314. light->LightVertex(vertexData);
  315. vertexData.point++;
  316. vertexData.color++;
  317. vertexData.normal++;
  318. }
  319. }
  320. #ifdef LAB_ONLY
  321. Statistics::MLR_LitVertices += len*nrLights;
  322. #endif
  323. // set the to use colors to the original colors ...
  324. // only lighting could overwrite this;
  325. actualColors = &litColors;
  326. STOP(("Lightmaps not implemented yet."));
  327. }
  328. break;
  329. }
  330. }