dcdevice3d.cpp 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  1. #include "pch.h"
  2. // compile this file for speed
  3. #pragma optimize("t", on)
  4. // #pragma optimize("s", on)
  5. //////////////////////////////////////////////////////////////////////////////
  6. //
  7. // Clipping Planes
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. const int g_maxCountPlanes = 8;
  11. typedef BYTE Code;
  12. const Code ClipFront = 0x01;
  13. const Code ClipBack = 0x02;
  14. const Code ClipLeft = 0x04;
  15. const Code ClipRight = 0x08;
  16. const Code ClipBottom = 0x10;
  17. const Code ClipTop = 0x20;
  18. const Code ClipPlane = 0x40;
  19. //////////////////////////////////////////////////////////////////////////////
  20. //
  21. // Transformed and Lit Vertex
  22. //
  23. //////////////////////////////////////////////////////////////////////////////
  24. class VertexTL {
  25. public:
  26. float x, y, z, w;
  27. float r, g, b, a;
  28. float u, v;
  29. WORD m_index;
  30. Code m_code;
  31. VertexTL()
  32. {
  33. }
  34. VertexTL(const VertexTL& v) :
  35. x(v.x),
  36. y(v.y),
  37. z(v.z),
  38. w(v.w),
  39. r(v.r),
  40. g(v.g),
  41. b(v.b),
  42. a(v.a),
  43. u(v.u),
  44. v(v.v),
  45. m_index(v.m_index),
  46. m_code(v.m_code)
  47. {
  48. }
  49. void Interpolate(const VertexTL& vertex0, const VertexTL& vertex1, float alpha)
  50. {
  51. float beta = 1 - alpha;
  52. x = alpha * vertex1.x + beta * vertex0.x;
  53. y = alpha * vertex1.y + beta * vertex0.y;
  54. z = alpha * vertex1.z + beta * vertex0.z;
  55. w = alpha * vertex1.w + beta * vertex0.w;
  56. u = alpha * vertex1.u + beta * vertex0.u;
  57. v = alpha * vertex1.v + beta * vertex0.v;
  58. r = alpha * vertex1.r + beta * vertex0.r;
  59. g = alpha * vertex1.g + beta * vertex0.g;
  60. b = alpha * vertex1.b + beta * vertex0.b;
  61. a = alpha * vertex1.a + beta * vertex0.a;
  62. }
  63. void UpdateClipCode()
  64. {
  65. m_code = 0;
  66. if (x < -w) m_code |= ClipLeft;
  67. if (x > w) m_code |= ClipRight;
  68. if (y < -w) m_code |= ClipBottom;
  69. if (y > w) m_code |= ClipTop;
  70. if (z < 0) m_code |= ClipFront;
  71. if (z > w) m_code |= ClipBack;
  72. }
  73. void UpdateClipCode(const Plane& plane, Code code)
  74. {
  75. if (plane.Distance(HVector(x, y, z, w)) <= 0) {
  76. m_code |= code;
  77. }
  78. }
  79. };
  80. //////////////////////////////////////////////////////////////////////////////
  81. //
  82. // Device3D
  83. //
  84. //////////////////////////////////////////////////////////////////////////////
  85. class Device3D : public IDevice3D {
  86. //
  87. // Types
  88. //
  89. typedef void (Device3D::*PFNClip)(VertexTL* pvertex, int vcount, const WORD* pindex, int icount);
  90. typedef void (Device3D::*PFNDraw)(const VertexScreen* pvertex, int vcount, const WORD* pindex, int icount);
  91. typedef void (Device3D::*PFNVertex)(const Vertex* pvertex, int vcount, const WORD* pindex, int icount, PFNClip pfnClip, PFNDraw pfnDraw);
  92. typedef void (Device3D::*PFNVertexL)(const VertexL* pvertex, int vcount, const WORD* pindex, int icount, PFNClip pfnClip, PFNDraw pfnDraw);
  93. typedef void (Device3D::*PFNDrawD3D)(const D3DLVertex* pvertex, int vcount, const WORD* pindex, int icount);
  94. typedef void (Device3D::*PFNVertexDrawD3D)(const Vertex* pvertex, int vcount, const WORD* pindex, int icount, PFNDrawD3D pfnDrawD3D);
  95. //
  96. // Interfaces
  97. //
  98. TRef<ID3DRasterizer> m_prasterizer;
  99. //
  100. // Current state
  101. //
  102. bool m_bDrawD3D;
  103. Rect m_rectView;
  104. bool m_bClip;
  105. ShadeMode m_shadeMode;
  106. PFNVertexL m_pfnTransform;
  107. PFNVertex m_pfnLightVertex;
  108. PFNVertexL m_pfnLightVertexL;
  109. PFNVertexDrawD3D m_pfnLightD3D;
  110. Matrix m_mat;
  111. Matrix m_matPerspective;
  112. Matrix m_matFull;
  113. bool m_bUpdateMatFull;
  114. bool m_bUpdateLighting;
  115. bool m_bUpdatePointers;
  116. float m_brightAmbient;
  117. Vector m_vecLight;
  118. Vector m_vecLightWorld;
  119. TRef<IDirect3DLight> m_plight;
  120. TRef<IDirect3DMaterial2> m_pmat;
  121. TRef<Material> m_pmaterial;
  122. //
  123. // Clipping
  124. //
  125. float m_scalex;
  126. float m_scaley;
  127. float m_xoffset;
  128. float m_yoffset;
  129. int m_countPlanes;
  130. Plane m_pplane[g_maxCountPlanes];
  131. //
  132. // Temporary vertex and index buffers
  133. //
  134. D3DLVertex* m_pvertexLBufferD3D;
  135. int m_countVertexLBufferD3D;
  136. VertexL* m_pvertexLBuffer;
  137. int m_countVertexLBuffer;
  138. VertexTL* m_pvertexTLBuffer;
  139. int m_countVertexTLBuffer;
  140. VertexScreen* m_pvertexScreenBuffer;
  141. int m_countVertexScreenBuffer;
  142. int m_indexVertexScreenBuffer;
  143. WORD* m_pindexBuffer;
  144. int m_countIndexBuffer;
  145. int m_indexIndexBuffer;
  146. //
  147. // Performance counters
  148. //
  149. #ifdef EnablePerformanceCounters
  150. int m_countPoints;
  151. int m_countLines;
  152. int m_countTriangles;
  153. int m_countMeshes;
  154. int m_countObjects;
  155. int m_countObjectsRendered;
  156. int m_countMatrixLoads;
  157. #endif
  158. public:
  159. //////////////////////////////////////////////////////////////////////////////
  160. //
  161. // Constructor
  162. //
  163. //////////////////////////////////////////////////////////////////////////////
  164. Device3D(ID3DRasterizer* prasterizer) :
  165. m_prasterizer(prasterizer),
  166. m_pvertexLBufferD3D(NULL),
  167. m_countVertexLBufferD3D(0),
  168. m_pvertexLBuffer(NULL),
  169. m_countVertexLBuffer(0),
  170. m_pvertexTLBuffer(NULL),
  171. m_countVertexTLBuffer(0),
  172. m_pvertexScreenBuffer(NULL),
  173. m_countVertexScreenBuffer(0),
  174. m_indexVertexScreenBuffer(0),
  175. m_pindexBuffer(NULL),
  176. m_countIndexBuffer(0),
  177. m_indexIndexBuffer(0),
  178. m_bDrawD3D(true),
  179. m_bClip(true),
  180. m_shadeMode(ShadeModeGouraud),
  181. m_bUpdateMatFull(true),
  182. m_bUpdateLighting(true),
  183. m_bUpdatePointers(true),
  184. m_brightAmbient(0.5f),
  185. m_vecLightWorld(1, 0, 0)
  186. {
  187. m_mat.SetIdentity();
  188. m_matPerspective.SetIdentity();
  189. //
  190. // Setup the clipping planes
  191. //
  192. m_countPlanes = 6;
  193. m_pplane[0] = Plane(HVector( 0, 0, 1, 0));
  194. m_pplane[1] = Plane(HVector( 0, 0, 1, -1));
  195. m_pplane[2] = Plane(HVector( 1, 0, 0, 1));
  196. m_pplane[3] = Plane(HVector(-1, 0, 0, 1));
  197. m_pplane[4] = Plane(HVector( 0, 1, 0, 1));
  198. m_pplane[5] = Plane(HVector( 0,-1, 0, 1));
  199. //
  200. // Initialize Performance counters
  201. //
  202. #ifdef EnablePerformanceCounters
  203. ResetPerformanceCounters();
  204. #endif
  205. }
  206. bool IsValid()
  207. {
  208. return m_prasterizer->IsValid();
  209. }
  210. //////////////////////////////////////////////////////////////////////////////
  211. //
  212. // Performance counters
  213. //
  214. //////////////////////////////////////////////////////////////////////////////
  215. #ifdef EnablePerformanceCounters
  216. void ResetPerformanceCounters()
  217. {
  218. m_countPoints = 0;
  219. m_countLines = 0;
  220. m_countTriangles = 0;
  221. m_countMeshes = 0;
  222. m_countObjects = 0;
  223. m_countObjectsRendered = 0;
  224. m_countMatrixLoads = 0;
  225. }
  226. int GetPerformanceCounter(Counter counter)
  227. {
  228. switch (counter) {
  229. case CounterPoints : return m_countPoints ;
  230. case CounterLines : return m_countLines ;
  231. case CounterTriangles : return m_countTriangles ;
  232. case CounterMeshes : return m_countMeshes ;
  233. case CounterObjects : return m_countObjects ;
  234. case CounterObjectsRendered : return m_countObjectsRendered;
  235. case CounterMatrixLoads : return m_countMatrixLoads ;
  236. }
  237. ZError("Unknown counter");
  238. return 0;
  239. }
  240. #endif
  241. //////////////////////////////////////////////////////////////////////////////
  242. //
  243. // Destructor
  244. //
  245. //////////////////////////////////////////////////////////////////////////////
  246. ~Device3D()
  247. {
  248. if (m_pvertexLBufferD3D) {
  249. free(m_pvertexLBufferD3D);
  250. }
  251. if (m_pvertexLBuffer) {
  252. free(m_pvertexLBuffer);
  253. }
  254. if (m_pvertexTLBuffer) {
  255. free(m_pvertexTLBuffer);
  256. }
  257. if (m_pvertexScreenBuffer) {
  258. free(m_pvertexScreenBuffer);
  259. }
  260. if (m_pindexBuffer) {
  261. free(m_pindexBuffer);
  262. }
  263. }
  264. //////////////////////////////////////////////////////////////////////////////
  265. //
  266. // Device
  267. //
  268. //////////////////////////////////////////////////////////////////////////////
  269. void BeginScene()
  270. {
  271. m_prasterizer->BeginScene();
  272. }
  273. void EndScene()
  274. {
  275. m_prasterizer->EndScene();
  276. }
  277. void ClearZBuffer()
  278. {
  279. m_prasterizer->ClearZBuffer();
  280. }
  281. //////////////////////////////////////////////////////////////////////////////
  282. //
  283. // View Rect
  284. //
  285. //////////////////////////////////////////////////////////////////////////////
  286. void SetViewRect(const Rect& rectView)
  287. {
  288. m_rectView = rectView;
  289. m_bUpdateMatFull = true;
  290. m_prasterizer->SetViewRect(rectView);
  291. }
  292. //////////////////////////////////////////////////////////////////////////////
  293. //
  294. // Lighting
  295. //
  296. //////////////////////////////////////////////////////////////////////////////
  297. void DirectionalLight(const Color& color, const Vector& vec)
  298. {
  299. m_vecLightWorld = m_mat.TransformDirection(vec);
  300. if (!m_plight)
  301. {
  302. DDCall(m_prasterizer->GetD3D()->CreateLight(&m_plight, NULL));
  303. D3DLIGHT light;
  304. ZeroMemory(&light, sizeof(D3DLIGHT));
  305. light.dwSize = sizeof(D3DLIGHT);
  306. light.dltType = D3DLIGHT_DIRECTIONAL;
  307. light.dcvColor = D3DColorValue(color);
  308. light.dvDirection = D3DVector(vec);
  309. light.dvFalloff = 1.0f;
  310. light.dvAttenuation0 = 1.0f;
  311. light.dvAttenuation1 = 0.0f;
  312. light.dvAttenuation2 = 0.0f;
  313. light.dvRange = D3DLIGHT_RANGE_MAX;
  314. //light.dwFlags = D3DLIGHT_ACTIVE;
  315. DDCall(m_plight->SetLight(&light));
  316. DDCall(m_prasterizer->GetViewport()->AddLight(m_plight));
  317. D3DMATERIAL material;
  318. D3DMATERIALHANDLE hmat;
  319. material.dwSize = sizeof(material);
  320. material.diffuse.r = material.diffuse.a = 1.0;
  321. material.diffuse.g = material.diffuse.b = 1.0;
  322. material.ambient.a = 1.0;
  323. material.ambient.r = material.ambient.g = material.ambient.b = 1.0;
  324. material.emissive.a = 1.0;
  325. material.emissive.r = material.emissive.g = material.emissive.b = 1.0;
  326. material.specular.a = 1.0;
  327. material.specular.r = material.specular.g = material.specular.b = 1.0;
  328. material.power = 1;
  329. material.hTexture = NULL;
  330. material.dwRampSize = 0;
  331. DDCall(m_prasterizer->GetD3D()->CreateMaterial(&m_pmat, NULL));
  332. DDCall(m_pmat->SetMaterial(&material));
  333. DDCall(m_pmat->GetHandle(m_prasterizer->GetD3DDeviceX(), &hmat));
  334. DDCall(m_prasterizer->GetD3DDeviceX()->SetLightState(D3DLIGHTSTATE_MATERIAL, hmat));
  335. }
  336. else
  337. {
  338. D3DLIGHT light;
  339. ZeroMemory(&light, sizeof(D3DLIGHT));
  340. light.dwSize = sizeof(D3DLIGHT);
  341. DDCall(m_plight->GetLight(&light));
  342. light.dcvColor = D3DColorValue(color);
  343. light.dvDirection = D3DVector(vec);
  344. DDCall(m_plight->SetLight(&light));
  345. }
  346. }
  347. //////////////////////////////////////////////////////////////////////////////
  348. //
  349. //
  350. //
  351. //////////////////////////////////////////////////////////////////////////////
  352. void SetMatrix(D3DMATRIX* pdmat, const Matrix& mat)
  353. {
  354. pdmat->_11 = mat[0][0];
  355. pdmat->_12 = mat[1][0];
  356. pdmat->_13 = mat[2][0];
  357. pdmat->_14 = mat[3][0];
  358. pdmat->_21 = mat[0][1];
  359. pdmat->_22 = mat[1][1];
  360. pdmat->_23 = mat[2][1];
  361. pdmat->_24 = mat[3][1];
  362. pdmat->_31 = mat[0][2];
  363. pdmat->_32 = mat[1][2];
  364. pdmat->_33 = mat[2][2];
  365. pdmat->_34 = mat[3][2];
  366. pdmat->_41 = mat[0][3];
  367. pdmat->_42 = mat[1][3];
  368. pdmat->_43 = mat[2][3];
  369. pdmat->_44 = mat[3][3];
  370. }
  371. const Matrix& GetFullMatrix()
  372. {
  373. if (m_bUpdateMatFull) {
  374. m_bUpdateMatFull = false;
  375. m_matFull = m_mat;
  376. m_matFull.Multiply(m_matPerspective);
  377. Point pointCenter = m_rectView.Center();
  378. m_scalex = 0.5f * m_rectView.XSize();
  379. // !!! In D3D pixels centers are on integer boundaries
  380. // The device clips to integer boundaries
  381. // subtract 0.25 to move the first row of pixels centers into the clip rect
  382. m_xoffset = pointCenter.X() - 0.25f;
  383. m_yoffset = pointCenter.Y() - 0.25f;
  384. if (m_bClip) {
  385. m_scaley = 0.5f * m_rectView.YSize();
  386. m_matFull.Scale(
  387. Vector(
  388. 1,
  389. m_scalex / m_scaley,
  390. 1.0f
  391. )
  392. );
  393. } else {
  394. m_matFull.Scale(
  395. Vector(
  396. m_scalex,
  397. -m_scalex,
  398. 1.0f
  399. )
  400. );
  401. m_matFull.Translate(
  402. Vector(
  403. m_xoffset,
  404. m_yoffset,
  405. 0
  406. )
  407. );
  408. }
  409. }
  410. return m_matFull;
  411. }
  412. void UpdateLighting()
  413. {
  414. if (m_bUpdateLighting) {
  415. m_bUpdateLighting = false;
  416. Matrix matInverse;
  417. matInverse.SetInverse(m_mat);
  418. m_vecLight = matInverse.TransformDirection(m_vecLightWorld);
  419. m_vecLight.SetNormalize();
  420. }
  421. }
  422. //////////////////////////////////////////////////////////////////////////////
  423. //
  424. // Update Pointers
  425. //
  426. //////////////////////////////////////////////////////////////////////////////
  427. void UpdatePointers()
  428. {
  429. if (m_bUpdatePointers) {
  430. m_bUpdatePointers = false;
  431. if (!m_bDrawD3D) {
  432. bool bClip = m_bClip || m_countPlanes > 6;
  433. if (bClip) {
  434. m_pfnTransform = TransformClip;
  435. } else {
  436. m_pfnTransform = TransformNoClip;
  437. }
  438. m_pfnLightVertexL = m_pfnTransform;
  439. switch (m_shadeMode) {
  440. case ShadeModeCopy:
  441. m_pfnLightVertex = LightVertexCopy;
  442. break;
  443. case ShadeModeFlat:
  444. case ShadeModeGouraud:
  445. if (m_pmaterial) {
  446. m_pfnLightVertex = MaterialLightVertex;
  447. } else {
  448. if (bClip) {
  449. m_pfnLightVertex = LightVertex;
  450. } else {
  451. m_pfnLightVertex = TransformAndLightNoClip;
  452. }
  453. }
  454. break;
  455. default:
  456. ZError("Invalid ShadeMode");
  457. }
  458. } else {
  459. switch (m_shadeMode) {
  460. case ShadeModeCopy:
  461. m_pfnLightD3D = CopyLightVertexD3D;
  462. break;
  463. case ShadeModeFlat:
  464. case ShadeModeGouraud:
  465. if (m_pmaterial) {
  466. m_pfnLightD3D = MaterialLightVertexD3D;
  467. } else {
  468. m_pfnLightD3D = LightVertexD3D;
  469. }
  470. break;
  471. default:
  472. ZError("Invalid ShadeMode");
  473. }
  474. }
  475. }
  476. }
  477. //////////////////////////////////////////////////////////////////////////////
  478. //
  479. // Get State
  480. //
  481. //////////////////////////////////////////////////////////////////////////////
  482. bool GetClipping()
  483. {
  484. return m_bClip;
  485. }
  486. const Matrix& GetMatrix()
  487. {
  488. return m_mat;
  489. }
  490. //////////////////////////////////////////////////////////////////////////////
  491. //
  492. // Set State
  493. //
  494. //////////////////////////////////////////////////////////////////////////////
  495. void SetMatrix(const Matrix& mat)
  496. {
  497. m_mat = mat;
  498. m_bUpdateMatFull = true;
  499. m_bUpdateLighting = true;
  500. if (m_bDrawD3D)
  501. {
  502. D3DMATRIX dmat;
  503. SetMatrix(&dmat, m_mat);
  504. m_prasterizer->GetD3DDeviceX()->SetTransform(
  505. D3DTRANSFORMSTATE_WORLD,
  506. &dmat);
  507. }
  508. }
  509. void SetPerspectiveMatrix(const Matrix& mat)
  510. {
  511. m_matPerspective = mat;
  512. m_bUpdateMatFull = true;
  513. if (m_bDrawD3D)
  514. {
  515. D3DMATRIX dmat;
  516. SetMatrix(&dmat, m_matPerspective);
  517. m_prasterizer->GetD3DDeviceX()->SetTransform(
  518. D3DTRANSFORMSTATE_PROJECTION,
  519. &dmat);
  520. }
  521. }
  522. void SetClipping(bool bClip)
  523. {
  524. m_bClip = bClip;
  525. m_bUpdateMatFull = true;
  526. m_bUpdatePointers = true;
  527. }
  528. void RemoveClipPlane(int indexRemove)
  529. {
  530. ZAssert(indexRemove >= 0 && indexRemove + 6 < m_countPlanes);
  531. for(int index = m_countPlanes - indexRemove - 1; index < m_countPlanes - 1; index++) {
  532. m_pplane[index] = m_pplane[index + 1];
  533. }
  534. m_countPlanes--;
  535. }
  536. void AddClipPlane(const Plane& plane)
  537. {
  538. ZAssert(m_countPlanes < g_maxCountPlanes);
  539. //
  540. // transform the plane into eye coordinates
  541. //
  542. const Matrix& mat = GetFullMatrix();
  543. Matrix mati;
  544. mati.SetInverse(mat);
  545. mati.Transpose();
  546. HVector p = plane.GetHVector();
  547. HVector pp = mati.Transform(p);
  548. m_pplane[m_countPlanes] = Plane(pp);
  549. m_countPlanes++;
  550. }
  551. void SetShadeMode(ShadeMode shadeMode)
  552. {
  553. m_shadeMode = shadeMode;
  554. m_bUpdatePointers = true;
  555. m_prasterizer->SetShadeMode(shadeMode);
  556. }
  557. void SetMaterial(Material* pmaterial)
  558. {
  559. m_pmaterial = pmaterial;
  560. m_bUpdatePointers = true;
  561. }
  562. void SetBlendMode(BlendMode blendMode)
  563. {
  564. m_prasterizer->SetBlendMode(blendMode);
  565. }
  566. void SetTexture(Surface* psurfaceTexture)
  567. {
  568. m_prasterizer->SetTexture(psurfaceTexture);
  569. }
  570. void SetWrapMode(WrapMode wrapMode)
  571. {
  572. m_prasterizer->SetWrapMode(wrapMode);
  573. }
  574. void SetCullMode(CullMode cullMode)
  575. {
  576. m_prasterizer->SetCullMode(cullMode);
  577. }
  578. void SetZTest(bool bZTest)
  579. {
  580. m_prasterizer->SetZTest(bZTest);
  581. }
  582. void SetZWrite(bool bZWrite)
  583. {
  584. m_prasterizer->SetZWrite(bZWrite);
  585. }
  586. void SetLinearFilter(bool bLinearFilter)
  587. {
  588. m_prasterizer->SetLinearFilter(bLinearFilter);
  589. }
  590. void SetPerspectiveCorrection(bool bPerspectiveCorrection)
  591. {
  592. m_prasterizer->SetPerspectiveCorrection(bPerspectiveCorrection);
  593. }
  594. void SetDither(bool bDither)
  595. {
  596. m_prasterizer->SetDither(bDither);
  597. }
  598. //////////////////////////////////////////////////////////////////////////////
  599. //
  600. // Buffers
  601. //
  602. //////////////////////////////////////////////////////////////////////////////
  603. //
  604. // , may want to grow to more than count
  605. //
  606. void SizeVertexLBufferD3D(int count)
  607. {
  608. if (m_countVertexLBufferD3D < count) {
  609. m_countVertexLBufferD3D = count;
  610. m_pvertexLBufferD3D = (D3DLVertex*)realloc(m_pvertexLBufferD3D, sizeof(D3DLVertex) * count);
  611. }
  612. }
  613. void SizeVertexLBuffer(int count)
  614. {
  615. if (m_countVertexLBuffer < count) {
  616. m_countVertexLBuffer = count;
  617. m_pvertexLBuffer = (VertexL*)realloc(m_pvertexLBuffer, sizeof(VertexL) * count);
  618. }
  619. }
  620. void SizeVertexTLBuffer(int count)
  621. {
  622. if (m_countVertexTLBuffer < count) {
  623. m_countVertexTLBuffer = count;
  624. m_pvertexTLBuffer = (VertexTL*)realloc(m_pvertexTLBuffer, sizeof(VertexTL) * count);
  625. }
  626. }
  627. void SizeVertexScreenBuffer(int count)
  628. {
  629. if (m_countVertexScreenBuffer < count) {
  630. m_pvertexScreenBuffer = (VertexScreen*)realloc(m_pvertexScreenBuffer, sizeof(VertexScreen) * count);
  631. for (int index = m_countVertexScreenBuffer; index < count; index++) {
  632. m_pvertexScreenBuffer[index].specular = 0;
  633. }
  634. m_countVertexScreenBuffer = count;
  635. }
  636. }
  637. void SizeIndexBuffer(int count)
  638. {
  639. if (m_countIndexBuffer < count) {
  640. m_countIndexBuffer = count;
  641. m_pindexBuffer = (WORD*)realloc(m_pindexBuffer, sizeof(WORD) * count);
  642. }
  643. }
  644. //////////////////////////////////////////////////////////////////////////////
  645. //
  646. // Vertex Buffers
  647. //
  648. //////////////////////////////////////////////////////////////////////////////
  649. D3DLVertex* GetVertexLBufferD3D(int count)
  650. {
  651. SizeVertexLBufferD3D(count);
  652. return m_pvertexLBufferD3D;
  653. }
  654. VertexL* GetVertexLBuffer(int count)
  655. {
  656. SizeVertexLBuffer(count);
  657. return m_pvertexLBuffer;
  658. }
  659. VertexScreen* GetVertexScreenBuffer(int count)
  660. {
  661. SizeVertexScreenBuffer(count);
  662. return m_pvertexScreenBuffer;
  663. }
  664. //////////////////////////////////////////////////////////////////////////////
  665. //
  666. //
  667. //
  668. //////////////////////////////////////////////////////////////////////////////
  669. void StoreTriangle(WORD index1, WORD index2, WORD index3)
  670. {
  671. SizeIndexBuffer(m_indexIndexBuffer + 3);
  672. m_pindexBuffer[m_indexIndexBuffer ] = index1;
  673. m_pindexBuffer[m_indexIndexBuffer + 1] = index2;
  674. m_pindexBuffer[m_indexIndexBuffer + 2] = index3;
  675. m_indexIndexBuffer += 3;
  676. }
  677. void StoreLine(WORD index1, WORD index2)
  678. {
  679. SizeIndexBuffer(m_indexIndexBuffer + 2);
  680. m_pindexBuffer[m_indexIndexBuffer ] = index1;
  681. m_pindexBuffer[m_indexIndexBuffer + 1] = index2;
  682. m_indexIndexBuffer += 2;
  683. }
  684. //////////////////////////////////////////////////////////////////////////////
  685. //
  686. //
  687. //
  688. //////////////////////////////////////////////////////////////////////////////
  689. void ClampVertex(VertexScreen& vertex)
  690. {
  691. if (vertex.x < m_rectView.XMin()) {
  692. vertex.x = m_rectView.XMin();
  693. } else if (vertex.x > m_rectView.XMax()) {
  694. vertex.x = m_rectView.XMax();
  695. }
  696. if (vertex.y < m_rectView.YMin()) {
  697. vertex.y = m_rectView.YMin();
  698. } else if (vertex.y > m_rectView.YMax()) {
  699. vertex.y = m_rectView.YMax();
  700. }
  701. if (vertex.z < 0) {
  702. vertex.z = 0;
  703. } else if (vertex.z > 1) {
  704. vertex.z = 1;
  705. }
  706. }
  707. //////////////////////////////////////////////////////////////////////////////
  708. //
  709. //
  710. //
  711. //////////////////////////////////////////////////////////////////////////////
  712. WORD ConvertVertex(VertexTL& vertex)
  713. {
  714. SizeVertexScreenBuffer(m_indexVertexScreenBuffer + 1);
  715. vertex.m_index = m_indexVertexScreenBuffer;
  716. VertexScreen* pvertex = m_pvertexScreenBuffer + m_indexVertexScreenBuffer;
  717. float rhw = 1.0f / vertex.w;
  718. pvertex->x = m_scalex * rhw * vertex.x + m_xoffset;
  719. pvertex->y = m_yoffset - m_scaley * rhw * vertex.y;
  720. pvertex->z = rhw * vertex.z;
  721. pvertex->qw = rhw;
  722. pvertex->u = vertex.u;
  723. pvertex->v = vertex.v;
  724. pvertex->color = MakeD3DCOLOR(Color(vertex.r, vertex.g, vertex.b, vertex.a));
  725. pvertex->specular = 0;
  726. ClampVertex(*pvertex);
  727. m_indexVertexScreenBuffer++;
  728. return vertex.m_index;
  729. }
  730. //////////////////////////////////////////////////////////////////////////////
  731. //
  732. //
  733. //
  734. //////////////////////////////////////////////////////////////////////////////
  735. WORD TranslateIndex(VertexTL& vertex)
  736. {
  737. if (vertex.m_index == 0xffff) {
  738. return ConvertVertex(vertex);
  739. }
  740. return vertex.m_index;
  741. }
  742. //////////////////////////////////////////////////////////////////////////////
  743. //
  744. //
  745. //
  746. //////////////////////////////////////////////////////////////////////////////
  747. int TranslateIntersection(
  748. const Plane& plane,
  749. const VertexTL& vertex0,
  750. const VertexTL& vertex1
  751. ) {
  752. VertexTL vertex;
  753. CalculateIntersection(plane, vertex0, vertex1, vertex);
  754. return ConvertVertex(vertex);
  755. }
  756. //////////////////////////////////////////////////////////////////////////////
  757. //
  758. // Plane
  759. //
  760. //////////////////////////////////////////////////////////////////////////////
  761. void CalculateIntersection(
  762. const Plane& plane,
  763. const VertexTL& vertex0,
  764. const VertexTL& vertex1,
  765. VertexTL& vertexOut
  766. ) {
  767. HVector v0(vertex0.x, vertex0.y, vertex0.z, vertex0.w);
  768. HVector v1(vertex1.x, vertex1.y, vertex1.z, vertex1.w);
  769. float alpha = plane.Intersect(v0, v1);
  770. vertexOut.Interpolate(vertex0, vertex1, alpha);
  771. vertexOut.m_index = 0xffff;
  772. }
  773. //////////////////////////////////////////////////////////////////////////////
  774. //
  775. // Clip a polygon to a plane
  776. //
  777. //////////////////////////////////////////////////////////////////////////////
  778. int ClipPolygonToPlane(
  779. const Plane& plane,
  780. Code bit,
  781. VertexTL* pvertexIn,
  782. int vcountIn,
  783. VertexTL* pvertexOut
  784. ) {
  785. int vcount = 0;
  786. int vindexPrevious = vcountIn - 1;
  787. Code codePrevious = pvertexIn[vindexPrevious].m_code & bit;
  788. for (int vindex = 0; vindex < vcountIn; vindex++) {
  789. Code code = pvertexIn[vindex].m_code & bit;
  790. if ((code ^ codePrevious) != 0) {
  791. CalculateIntersection(
  792. plane,
  793. pvertexIn[vindexPrevious],
  794. pvertexIn[vindex],
  795. pvertexOut[vcount]
  796. );
  797. pvertexOut[vcount].UpdateClipCode();
  798. if (m_countPlanes > 6) {
  799. Code code = ClipPlane;
  800. for (int index = 6; index < m_countPlanes; index++, code <<= 1) {
  801. pvertexOut[vcount].UpdateClipCode(m_pplane[index], code);
  802. }
  803. }
  804. vcount++;
  805. }
  806. if (code == 0) {
  807. pvertexOut[vcount] = pvertexIn[vindex];
  808. vcount++;
  809. }
  810. ZAssert(vcount <= 8);
  811. codePrevious = code;
  812. vindexPrevious = vindex;
  813. }
  814. return vcount;
  815. }
  816. //////////////////////////////////////////////////////////////////////////////
  817. //
  818. // Clip a polygon to a set of planess
  819. //
  820. //////////////////////////////////////////////////////////////////////////////
  821. void ClipPolygonToPlanes(
  822. const Plane* pplane,
  823. int pcount,
  824. Code codeOr,
  825. const VertexTL& vertex0,
  826. const VertexTL& vertex1,
  827. const VertexTL& vertex2
  828. ) {
  829. VertexTL pverticesIn[3] =
  830. {
  831. vertex0,
  832. vertex1,
  833. vertex2
  834. };
  835. VertexTL pverticesOut0[8];
  836. VertexTL pverticesOut1[8];
  837. VertexTL* pvertexIn = pverticesIn;
  838. VertexTL* pvertexOut = pverticesOut0;
  839. VertexTL* pvertexOther = pverticesOut1;
  840. int vcount = 3;
  841. Code bit = 1;
  842. int pindex = 0;
  843. while (pindex < pcount) {
  844. if ((codeOr & bit) != 0) {
  845. vcount = ClipPolygonToPlane(pplane[pindex], bit, pvertexIn, vcount, pvertexOut);
  846. pvertexIn = pvertexOut;
  847. pvertexOut = pvertexOther;
  848. pvertexOther = pvertexIn;
  849. }
  850. bit *= 2;
  851. pindex++;
  852. }
  853. //
  854. // Translate the vertices to screen coordinates
  855. //
  856. WORD aindex[7];
  857. int index;
  858. for(index = 0; index < vcount; index++) {
  859. aindex[index] = TranslateIndex(pvertexIn[index]);
  860. }
  861. //
  862. // Form all of the triangle fans
  863. //
  864. for(index = 1; index < vcount - 1; index++) {
  865. StoreTriangle(
  866. aindex[0],
  867. aindex[index],
  868. aindex[index + 1]
  869. );
  870. }
  871. }
  872. //////////////////////////////////////////////////////////////////////////////
  873. //
  874. //
  875. //
  876. //////////////////////////////////////////////////////////////////////////////
  877. void ClipPolygonToPlane(
  878. const Plane& plane,
  879. Code code0,
  880. Code code1,
  881. Code code2,
  882. VertexTL& vertex0,
  883. VertexTL& vertex1,
  884. VertexTL& vertex2
  885. ) {
  886. if (code0 != 0) {
  887. if (code1 != 0) {
  888. // p0 and p1 outside
  889. StoreTriangle(
  890. TranslateIntersection(plane, vertex2, vertex0),
  891. TranslateIntersection(plane, vertex1, vertex2),
  892. TranslateIndex(vertex2)
  893. );
  894. } else if (code2 != 0) {
  895. // p0 and p2 outside
  896. StoreTriangle(
  897. TranslateIntersection(plane, vertex0, vertex1),
  898. TranslateIndex(vertex1),
  899. TranslateIntersection(plane, vertex2, vertex1)
  900. );
  901. } else {
  902. // p0 outside
  903. int index0 = TranslateIntersection(plane, vertex0, vertex1);
  904. int index2 = TranslateIndex(vertex2);
  905. StoreTriangle(
  906. index0,
  907. TranslateIndex(vertex1),
  908. index2
  909. );
  910. StoreTriangle(
  911. index0,
  912. index2,
  913. TranslateIntersection(plane, vertex0, vertex2)
  914. );
  915. }
  916. } else {
  917. if (code1 != 0) {
  918. if (code2 != 0) {
  919. // p1 and p2 outside
  920. StoreTriangle(
  921. TranslateIndex(vertex0),
  922. TranslateIntersection(plane, vertex0, vertex1),
  923. TranslateIntersection(plane, vertex2, vertex0)
  924. );
  925. } else {
  926. // p1 outside
  927. int index0 = TranslateIndex(vertex0);
  928. int index2 = TranslateIntersection(plane, vertex1, vertex2);
  929. StoreTriangle(
  930. index0,
  931. TranslateIntersection(plane, vertex0, vertex1),
  932. index2
  933. );
  934. StoreTriangle(
  935. index0,
  936. index2,
  937. TranslateIndex(vertex2)
  938. );
  939. }
  940. } else {
  941. // p2 outside
  942. int index0 = TranslateIndex(vertex0);
  943. int index2 = TranslateIntersection(plane, vertex1, vertex2);
  944. StoreTriangle(
  945. index0,
  946. TranslateIndex(vertex1),
  947. index2
  948. );
  949. StoreTriangle(
  950. index0,
  951. index2,
  952. TranslateIntersection(plane, vertex2, vertex0)
  953. );
  954. }
  955. }
  956. }
  957. //////////////////////////////////////////////////////////////////////////////
  958. //
  959. // Clip Triangles
  960. //
  961. //////////////////////////////////////////////////////////////////////////////
  962. void ClipTriangles(VertexTL* pvertex, int vcountIn, const WORD* pindex, int icountIn)
  963. {
  964. //
  965. // Clip each triangle
  966. //
  967. for(int index = 0; index < icountIn; index += 3) {
  968. int index0 = pindex[index + 0];
  969. int index1 = pindex[index + 1];
  970. int index2 = pindex[index + 2];
  971. Code code0 = pvertex[index0].m_code;
  972. Code code1 = pvertex[index1].m_code;
  973. Code code2 = pvertex[index2].m_code;
  974. Code codeAnd = code0 & code1 & code2;
  975. if (codeAnd == 0) {
  976. Code codeOr = code0 | code1 | code2;
  977. if (codeOr == 0) {
  978. //
  979. // Not clipped
  980. //
  981. StoreTriangle(
  982. TranslateIndex(pvertex[index0]),
  983. TranslateIndex(pvertex[index1]),
  984. TranslateIndex(pvertex[index2])
  985. );
  986. } else {
  987. //
  988. // Partially clipped
  989. //
  990. Code bit = 1;
  991. int pindex = 0;
  992. while (
  993. (pindex < m_countPlanes)
  994. && (codeOr != bit)
  995. ) {
  996. pindex++;
  997. bit *= 2;
  998. }
  999. if (pindex < m_countPlanes) {
  1000. //
  1001. // Clipped by a single plane
  1002. //
  1003. ClipPolygonToPlane(
  1004. m_pplane[pindex],
  1005. code0,
  1006. code1,
  1007. code2,
  1008. pvertex[index0],
  1009. pvertex[index1],
  1010. pvertex[index2]
  1011. );
  1012. } else {
  1013. //
  1014. // Clipped by multiple planes
  1015. //
  1016. ClipPolygonToPlanes(
  1017. m_pplane,
  1018. m_countPlanes,
  1019. codeOr,
  1020. pvertex[index0],
  1021. pvertex[index1],
  1022. pvertex[index2]
  1023. );
  1024. }
  1025. }
  1026. } else {
  1027. //
  1028. // Completely clipped
  1029. //
  1030. int i = 0;
  1031. }
  1032. }
  1033. }
  1034. //////////////////////////////////////////////////////////////////////////////
  1035. //
  1036. // Clip a line to a set of planess
  1037. //
  1038. //////////////////////////////////////////////////////////////////////////////
  1039. void ClipLineToPlanes(
  1040. const Plane* pplane,
  1041. int pcount,
  1042. Code codeOr,
  1043. const VertexTL& vertex0,
  1044. const VertexTL& vertex1
  1045. ) {
  1046. HVector v0(vertex0.x, vertex0.y, vertex0.z, vertex0.w);
  1047. HVector v1(vertex1.x, vertex1.y, vertex1.z, vertex1.w);
  1048. float t0 = 0;
  1049. float t1 = 1;
  1050. int vcount = 3;
  1051. Code bit = 1;
  1052. int pindex = 0;
  1053. while (pindex < pcount) {
  1054. if ((codeOr & bit) != 0) {
  1055. float t = pplane[pindex].Intersect(v0, v1);
  1056. if ((vertex0.m_code & bit) != 0) {
  1057. if (t > t0) {
  1058. t0 = t;
  1059. }
  1060. } else {
  1061. if (t < t1) {
  1062. t1 = t;
  1063. }
  1064. }
  1065. }
  1066. bit *= 2;
  1067. pindex++;
  1068. }
  1069. if (t0 < t1) {
  1070. VertexTL vertexT0; vertexT0.Interpolate(vertex0, vertex1, t0);
  1071. VertexTL vertexT1; vertexT1.Interpolate(vertex0, vertex1, t1);
  1072. StoreLine(
  1073. ConvertVertex(vertexT0),
  1074. ConvertVertex(vertexT1)
  1075. );
  1076. }
  1077. }
  1078. //////////////////////////////////////////////////////////////////////////////
  1079. //
  1080. // Clip Lines
  1081. //
  1082. //////////////////////////////////////////////////////////////////////////////
  1083. void ClipLines(VertexTL* pvertex, int vcount, const WORD* pindex, int icount)
  1084. {
  1085. for (int index = 0; index < icount; index += 2) {
  1086. int index0 = pindex[index + 0];
  1087. int index1 = pindex[index + 1];
  1088. Code code0 = pvertex[index0].m_code;
  1089. Code code1 = pvertex[index1].m_code;
  1090. Code codeAnd = code0 & code1;
  1091. if (codeAnd == 0) {
  1092. Code codeOr = code0 | code1;
  1093. if (codeOr == 0) {
  1094. //
  1095. // Not clipped
  1096. //
  1097. StoreLine(
  1098. TranslateIndex(pvertex[index0]),
  1099. TranslateIndex(pvertex[index1])
  1100. );
  1101. } else {
  1102. //
  1103. // Clipped
  1104. //
  1105. ClipLineToPlanes(
  1106. m_pplane,
  1107. m_countPlanes,
  1108. codeOr,
  1109. pvertex[index0],
  1110. pvertex[index1]
  1111. );
  1112. }
  1113. } else {
  1114. //
  1115. // Completely clipped
  1116. //
  1117. }
  1118. }
  1119. }
  1120. //////////////////////////////////////////////////////////////////////////////
  1121. //
  1122. // Clip Points
  1123. //
  1124. //////////////////////////////////////////////////////////////////////////////
  1125. void ClipPoints(VertexTL* pvertex, int vcount, const WORD* pindex, int icount)
  1126. {
  1127. for(int index = 0; index < vcount; index++) {
  1128. if (pvertex[index].m_code == 0) {
  1129. ConvertVertex(pvertex[index]);
  1130. }
  1131. }
  1132. }
  1133. //////////////////////////////////////////////////////////////////////////////
  1134. //
  1135. // Transform with clipping
  1136. //
  1137. //////////////////////////////////////////////////////////////////////////////
  1138. void TransformClip(const VertexL* pvertex, int vcount, const WORD* pindex, int icount, PFNClip pfnClip, PFNDraw pfnDraw)
  1139. {
  1140. SizeVertexTLBuffer(vcount);
  1141. const Matrix& mat = GetFullMatrix();
  1142. for (int index = 0; index < vcount; index++) {
  1143. const float x = pvertex[index].x;
  1144. const float y = pvertex[index].y;
  1145. const float z = pvertex[index].z;
  1146. m_pvertexTLBuffer[index].x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
  1147. m_pvertexTLBuffer[index].y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
  1148. m_pvertexTLBuffer[index].z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
  1149. m_pvertexTLBuffer[index].w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3];
  1150. m_pvertexTLBuffer[index].u = pvertex[index].u;
  1151. m_pvertexTLBuffer[index].v = pvertex[index].v;
  1152. m_pvertexTLBuffer[index].r = pvertex[index].r;
  1153. m_pvertexTLBuffer[index].g = pvertex[index].g;
  1154. m_pvertexTLBuffer[index].b = pvertex[index].b;
  1155. m_pvertexTLBuffer[index].a = pvertex[index].a;
  1156. m_pvertexTLBuffer[index].m_index = 0xffff;
  1157. m_pvertexTLBuffer[index].UpdateClipCode();
  1158. }
  1159. if (m_countPlanes > 6) {
  1160. for (int index = 0; index < vcount; index++) {
  1161. Code code = ClipPlane;
  1162. for (int iplane = 6; iplane < m_countPlanes; iplane++, code <<= 1) {
  1163. m_pvertexTLBuffer[index].UpdateClipCode(m_pplane[iplane], code);
  1164. }
  1165. }
  1166. }
  1167. m_indexVertexScreenBuffer = 0;
  1168. m_indexIndexBuffer = 0;
  1169. (this->*pfnClip)(m_pvertexTLBuffer, vcount, pindex, icount);
  1170. if (m_indexVertexScreenBuffer != 0) {
  1171. (this->*pfnDraw)(
  1172. m_pvertexScreenBuffer,
  1173. m_indexVertexScreenBuffer,
  1174. m_pindexBuffer,
  1175. m_indexIndexBuffer
  1176. );
  1177. }
  1178. }
  1179. //////////////////////////////////////////////////////////////////////////////
  1180. //
  1181. // Special case Transform and light without clipping
  1182. //
  1183. //////////////////////////////////////////////////////////////////////////////
  1184. #ifndef DREAMCAST
  1185. #define USEASM
  1186. #endif
  1187. #ifndef USEASM
  1188. void TransformAndLightNoClip(
  1189. const Vertex* pvertex,
  1190. int vcount,
  1191. const WORD* pindex,
  1192. int icount,
  1193. PFNClip pfnClip,
  1194. PFNDraw pfnDraw
  1195. ) {
  1196. SizeVertexScreenBuffer(vcount);
  1197. UpdateLighting();
  1198. const Matrix& mat = GetFullMatrix();
  1199. for (int index = 0; index < vcount; index++) {
  1200. const float x = pvertex[index].x;
  1201. const float y = pvertex[index].y;
  1202. const float z = pvertex[index].z;
  1203. const float rhw = 1.0f / (mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3]);
  1204. m_pvertexScreenBuffer[index].x = rhw * (mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3]);
  1205. m_pvertexScreenBuffer[index].y = rhw * (mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3]);
  1206. m_pvertexScreenBuffer[index].z = rhw * (mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3]);
  1207. m_pvertexScreenBuffer[index].qw = rhw;
  1208. m_pvertexScreenBuffer[index].u = pvertex[index].u;
  1209. m_pvertexScreenBuffer[index].v = pvertex[index].v;
  1210. {
  1211. float fbright =
  1212. 255.0f * (
  1213. pvertex[index].nx * m_vecLight.X()
  1214. + pvertex[index].ny * m_vecLight.Y()
  1215. + pvertex[index].nz * m_vecLight.Z()
  1216. + m_brightAmbient
  1217. );
  1218. #ifndef DREAMCAST
  1219. int bright = 0;
  1220. __asm {
  1221. fld fbright
  1222. fistp bright
  1223. }
  1224. #else
  1225. int bright = (int)fbright;
  1226. #endif
  1227. if (bright > 255) {
  1228. bright = 255;
  1229. } else if (bright < 0) {
  1230. bright = 0;
  1231. }
  1232. m_pvertexScreenBuffer[index].color = 0xff000000 | bright | (bright << 8) | (bright << 16);
  1233. }
  1234. }
  1235. (this->*pfnDraw)(m_pvertexScreenBuffer, vcount, pindex, icount);
  1236. }
  1237. #else
  1238. void TransformAndLightNoClip(
  1239. const Vertex* pvertex,
  1240. int vcount,
  1241. const WORD* pindex,
  1242. int icount,
  1243. PFNClip pfnClip,
  1244. PFNDraw pfnDraw
  1245. ) {
  1246. UpdateLighting();
  1247. SizeVertexScreenBuffer(vcount);
  1248. const Matrix mat = GetFullMatrix();
  1249. const Vector vecLight = m_vecLight;
  1250. const float brightAmbient = m_brightAmbient;
  1251. const float mult = 255.0f;
  1252. const float one = 1.0f;
  1253. int bright;
  1254. _asm {
  1255. mov ebx, 255
  1256. mov ecx, this
  1257. mov esi, pvertex
  1258. mov edi, [ecx].m_pvertexScreenBuffer
  1259. mov ecx, vcount
  1260. loopTop:
  1261. // int bright = (int)(255.0f * (pvertex[index].GetNormal() * m_vecLight + m_brightAmbient));
  1262. // const float rhw = 1.0f / (mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3]);
  1263. // m_pvertexScreenBuffer[index].SetX(rhw * (mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3]);
  1264. // m_pvertexScreenBuffer[index].SetY(rhw * (mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3]);
  1265. // m_pvertexScreenBuffer[index].SetZ(rhw * (mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3]));
  1266. // m_pvertexScreenBuffer[index].SetTextureCoordinate(pvertex[index].GetTextureCoordinate());
  1267. fld [vecLight].x //
  1268. fmul [esi]Vertex.nx // lx
  1269. fld [vecLight].y //
  1270. fmul [esi]Vertex.ny // ly, lx
  1271. fld [vecLight].z //
  1272. fmul [esi]Vertex.nz // lz, ly, lx
  1273. fxch st(2) // lx, ly, lz
  1274. faddp st(1), st(0) // lx + ly, lz
  1275. fld one // 1, lx + ly, lz
  1276. fld [mat + 48 + 0] //
  1277. fmul [esi]Vertex.x // wx, 1, lx + ly, lz
  1278. fxch st(3) // lz, 1, lx + ly, wx
  1279. fadd [brightAmbient] // lz + dl, 1, lx + ly, wx
  1280. fld [mat + 48 + 4] //
  1281. fmul [esi]Vertex.y // wy, lz + dl, 1, lx + ly, wx
  1282. fxch st(1) // lz + dl, wy, 1, lx + ly, wx
  1283. faddp st(3), st(0) // wy, 1, l, wx
  1284. fld [mat + 48 + 8] //
  1285. fmul [esi]Vertex.z // wz, wy, 1, l, wx
  1286. fxch st(1) // wy, wz, 1, l, wx
  1287. faddp st(4), st(0) // wz, 1, l, wx + wy
  1288. fxch st(2) // l, 1, wz, wx + wy
  1289. fmul mult // 256 * l, 1, wz, wx + wy
  1290. fld [mat + 0 + 0] //
  1291. fmul [esi]Vertex.x // xx, 255 * l, 1, wz, wx + wy
  1292. fxch st(3) // wz, 255 * l, 1, xx, wx + wy
  1293. fadd DWORD PTR [mat + 48 + 12] // wz + dw, 255 * l, 1, xx, wx + wy
  1294. fxch st(1) // 255 * l, wz + dw, 1, xx, wx + wy
  1295. fistp bright // wz + dw, 1, xx, wx + wy
  1296. fld [mat + 0 + 4] //
  1297. fmul [esi]Vertex.y // xy, wz + dw, 1, xx, wx + wy
  1298. fxch st(1) // wz + dw, xy, 1, xx, wx + wy
  1299. faddp st(4), st(0) // xy, 1, xx, w
  1300. fld [mat + 0 + 8] //
  1301. fmul [esi]Vertex.z // xz, xy, 1, xx, w
  1302. fxch st(1) // xy, xz, 1, xx, w
  1303. faddp st(3), st(0) // xz, 1, xx + xy, w
  1304. fxch st(3) // w, 1, xx + xy, xz
  1305. fdivp st(1), st(0) // rhw, xx + xy, xz
  1306. fxch st(2) // xz, xx + xy, rhw
  1307. // if (bright > 255) {
  1308. // bright = 255;
  1309. // } else if (bright < 0) {
  1310. // bright = 0;
  1311. // }
  1312. mov eax,bright
  1313. cmp eax,ebx
  1314. jle label1
  1315. mov eax,ebx
  1316. jmp label2
  1317. label1:
  1318. test eax,eax
  1319. jge label2
  1320. xor eax,eax
  1321. // m_pvertexScreenBuffer[index].color = 0xff000000 | bright | (bright << 8) | (bright << 16);
  1322. label2:
  1323. mov edx,eax
  1324. shl edx,8
  1325. or edx,eax
  1326. shl edx,8
  1327. or edx,eax
  1328. or edx, 0xff000000
  1329. mov [edi]VertexScreen.color, edx
  1330. mov eax, [esi]Vertex.u
  1331. mov edx, [esi]Vertex.v
  1332. mov [edi]VertexScreen.tu, eax
  1333. mov [edi]VertexScreen.tv, edx
  1334. // continue with floating point stuff
  1335. // xz, xx + xy, rhw
  1336. fadd DWORD PTR [mat + 0 + 12] // xz + dx, xx + xy, rhw
  1337. fld [mat + 16 + 0] //
  1338. fmul [esi]Vertex.x // yx, xz + dx, xx + xy, rhw
  1339. fld [mat + 16 + 4] //
  1340. fmul [esi]Vertex.y // yy, yx, xz + dx, xx + xy, rhw
  1341. fld [mat + 16 + 8] //
  1342. fmul [esi]Vertex.z // yz, yy, yx, xz + dx, xx + xy, rhw
  1343. fxch st(1) // yy, yz, yx, xz + dx, xx + xy, rhw
  1344. faddp st(2), st(0) // yz, yx + yy, xz + dx, xx + xy, rhw
  1345. fld [mat + 32 + 0] //
  1346. fmul [esi]Vertex.x // zx, yz, yx + yy, xz + dx, xx + xy, rhw
  1347. fld [mat + 32 + 4] //
  1348. fmul [esi]Vertex.y // zy, zx, yz, yx + yy, xz + dx, xx + xy, rhw
  1349. fld [mat + 32 + 8] //
  1350. fmul [esi]Vertex.z // zz, zy, zx, yz, yx + yy, xz + dx, xx + xy, rhw
  1351. fxch st(3) // yz, zy, zx, zz, yx + yy, xz + dx, xx + xy, rhw
  1352. fadd DWORD PTR [mat + 16 + 12] // yz + dy, zy, zx, zz, yx + yy, xz + dx, xx + xy, rhw
  1353. fxch st(1) // zy, yz + dy, zx, zz, yx + yy, xz + dx, xx + xy, rhw
  1354. faddp st(2), st(0) // yz + dy, zx + zy, zz, yx + yy, xz + dx, xx + xy, rhw
  1355. fxch st(2) // zz, zx + zy, yz + dy, yx + yy, xz + dx, xx + xy, rhw
  1356. fadd DWORD PTR [mat + 32 + 12] // zz + dz, zx + zy, yz + dy, yx + yy, xz + dx, xx + xy, rhw
  1357. fxch st(4) // xz + dx, zx + zy, yz + dy, yx + yy, zz + dz, xx + xy, rhw
  1358. faddp st(5), st(0) // zx + zy, yz + dy, yx + yy, zz + dz, x, rhw
  1359. fxch st(1) // yz + dy, zx + zy, yx + yy, zz + dz, x, rhw
  1360. faddp st(2), st(0) // zx + zy, y, zz + dz, x, rhw
  1361. faddp st(2), st(0) // y, z, x, rhw
  1362. fxch st(2) // x, z, y, rhw
  1363. fmul st(0), st(3) // rhw*x, z, y, rhw
  1364. fxch st(2) // y, z, rhw*x, rhw
  1365. fmul st(0), st(3) // rhw*y, z, rhw*x, rhw
  1366. fxch st(1) // z, rhw*y, rhw*x, rhw
  1367. fmul st(0), st(3) // rhw*z, rhw*y, rhw*x, rhw
  1368. fxch st(3) // rhw, rhw*y, rhw*x, rhw*z
  1369. fstp [edi]VertexScreen.rhw // rhw*y, rhw*x, rhw*z
  1370. fxch st(1) // rhw*x, rhw*y, rhw*z
  1371. fstp [edi]VertexScreen.x // rhw*y, rhw*z
  1372. fstp [edi]VertexScreen.y // rhw*z
  1373. fstp [edi]VertexScreen.z //
  1374. // loop
  1375. add edi, SIZE VertexScreen
  1376. add esi, SIZE Vertex
  1377. dec ecx
  1378. jne loopTop
  1379. }
  1380. (this->*pfnDraw)(m_pvertexScreenBuffer, vcount, pindex, icount);
  1381. }
  1382. #endif
  1383. //////////////////////////////////////////////////////////////////////////////
  1384. //
  1385. // Tranform without clipping
  1386. //
  1387. //////////////////////////////////////////////////////////////////////////////
  1388. void TransformNoClip(const VertexL* pvertex, int vcount, const WORD* pindex, int icount, PFNClip pfnClip, PFNDraw pfnDraw)
  1389. {
  1390. SizeVertexScreenBuffer(vcount);
  1391. const Matrix& mat = GetFullMatrix();
  1392. for (int index = 0; index < vcount; index++) {
  1393. const float x = pvertex[index].x;
  1394. const float y = pvertex[index].y;
  1395. const float z = pvertex[index].z;
  1396. const float rhw = 1.0f / (mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3]);
  1397. m_pvertexScreenBuffer[index].x = rhw * (mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3]);
  1398. m_pvertexScreenBuffer[index].y = rhw * (mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3]);
  1399. m_pvertexScreenBuffer[index].z = rhw * (mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3]);
  1400. m_pvertexScreenBuffer[index].qw = rhw;
  1401. m_pvertexScreenBuffer[index].u = pvertex[index].u;
  1402. m_pvertexScreenBuffer[index].v = pvertex[index].v;
  1403. // !!! write asm MakeD3DColor
  1404. m_pvertexScreenBuffer[index].color =
  1405. MakeD3DCOLOR(
  1406. Color(
  1407. pvertex[index].r,
  1408. pvertex[index].g,
  1409. pvertex[index].b,
  1410. pvertex[index].a
  1411. )
  1412. );
  1413. }
  1414. (this->*pfnDraw)(m_pvertexScreenBuffer, vcount, pindex, icount);
  1415. }
  1416. //////////////////////////////////////////////////////////////////////////////
  1417. //
  1418. // Copy Lighting
  1419. //
  1420. //////////////////////////////////////////////////////////////////////////////
  1421. void LightVertexCopy(const Vertex* pvertex, int vcount, const WORD* pindex, int icount, PFNClip pfnClip, PFNDraw pfnDraw)
  1422. {
  1423. SizeVertexLBuffer(vcount);
  1424. for (int index = 0; index < vcount; index++) {
  1425. m_pvertexLBuffer[index].x = pvertex[index].x;
  1426. m_pvertexLBuffer[index].y = pvertex[index].y;
  1427. m_pvertexLBuffer[index].z = pvertex[index].z;
  1428. m_pvertexLBuffer[index].u = pvertex[index].u;
  1429. m_pvertexLBuffer[index].v = pvertex[index].v;
  1430. m_pvertexLBuffer[index].r = 1;
  1431. m_pvertexLBuffer[index].g = 1;
  1432. m_pvertexLBuffer[index].b = 1;
  1433. m_pvertexLBuffer[index].a = 1;
  1434. }
  1435. (this->*m_pfnTransform)(m_pvertexLBuffer, vcount, pindex, icount, pfnClip, pfnDraw);
  1436. }
  1437. //////////////////////////////////////////////////////////////////////////////
  1438. //
  1439. // Directional Lighting
  1440. //
  1441. //////////////////////////////////////////////////////////////////////////////
  1442. void LightVertex(const Vertex* pvertex, int vcount, const WORD* pindex, int icount, PFNClip pfnClip, PFNDraw pfnDraw)
  1443. {
  1444. UpdateLighting();
  1445. SizeVertexLBuffer(vcount);
  1446. for (int index = 0; index < vcount; index++) {
  1447. m_pvertexLBuffer[index].x = pvertex[index].x;
  1448. m_pvertexLBuffer[index].y = pvertex[index].y;
  1449. m_pvertexLBuffer[index].z = pvertex[index].z;
  1450. m_pvertexLBuffer[index].u = pvertex[index].u;
  1451. m_pvertexLBuffer[index].v = pvertex[index].v;
  1452. float bright =
  1453. pvertex[index].nx * m_vecLight.X()
  1454. + pvertex[index].ny * m_vecLight.Y()
  1455. + pvertex[index].nz * m_vecLight.Z()
  1456. + m_brightAmbient;
  1457. if (bright > 1) {
  1458. bright = 1;
  1459. } else if (bright < 0) {
  1460. bright = 0;
  1461. }
  1462. m_pvertexLBuffer[index].r = bright;
  1463. m_pvertexLBuffer[index].g = bright;
  1464. m_pvertexLBuffer[index].b = bright;
  1465. m_pvertexLBuffer[index].a = 1;
  1466. }
  1467. (this->*m_pfnTransform)(m_pvertexLBuffer, vcount, pindex, icount, pfnClip, pfnDraw);
  1468. }
  1469. //////////////////////////////////////////////////////////////////////////////
  1470. //
  1471. // Directional Lighting
  1472. //
  1473. //////////////////////////////////////////////////////////////////////////////
  1474. void MaterialLightVertex(const Vertex* pvertex, int vcount, const WORD* pindex, int icount, PFNClip pfnClip, PFNDraw pfnDraw)
  1475. {
  1476. UpdateLighting();
  1477. SizeVertexLBuffer(vcount);
  1478. float r = m_pmaterial->GetDiffuse().GetRed();
  1479. float g = m_pmaterial->GetDiffuse().GetGreen();
  1480. float b = m_pmaterial->GetDiffuse().GetBlue();
  1481. float a = m_pmaterial->GetDiffuse().GetAlpha();
  1482. for (int index = 0; index < vcount; index++) {
  1483. m_pvertexLBuffer[index].x = pvertex[index].x;
  1484. m_pvertexLBuffer[index].y = pvertex[index].y;
  1485. m_pvertexLBuffer[index].z = pvertex[index].z;
  1486. m_pvertexLBuffer[index].u = pvertex[index].u;
  1487. m_pvertexLBuffer[index].v = pvertex[index].v;
  1488. float bright =
  1489. pvertex[index].nx * m_vecLight.X()
  1490. + pvertex[index].ny * m_vecLight.Y()
  1491. + pvertex[index].nz * m_vecLight.Z()
  1492. + m_brightAmbient;
  1493. if (bright > 1) {
  1494. bright = 1;
  1495. } else if (bright < 0) {
  1496. bright = 0;
  1497. }
  1498. m_pvertexLBuffer[index].r = r * bright;
  1499. m_pvertexLBuffer[index].g = g * bright;
  1500. m_pvertexLBuffer[index].b = b * bright;
  1501. m_pvertexLBuffer[index].a = a;
  1502. }
  1503. (this->*m_pfnTransform)(m_pvertexLBuffer, vcount, pindex, icount, pfnClip, pfnDraw);
  1504. }
  1505. //////////////////////////////////////////////////////////////////////////////
  1506. //
  1507. // Lighting for D3D Transformations
  1508. //
  1509. //////////////////////////////////////////////////////////////////////////////
  1510. void LightVertexD3D(
  1511. const Vertex* pvertex,
  1512. int vcount,
  1513. const WORD* pindex,
  1514. int icount,
  1515. PFNDrawD3D pfnDraw
  1516. ) {
  1517. UpdateLighting();
  1518. SizeVertexLBufferD3D(vcount);
  1519. for (int index = 0; index < vcount; index++) {
  1520. m_pvertexLBufferD3D[index].x = pvertex[index].x;
  1521. m_pvertexLBufferD3D[index].y = pvertex[index].y;
  1522. m_pvertexLBufferD3D[index].z = pvertex[index].z;
  1523. m_pvertexLBufferD3D[index].tu = pvertex[index].u;
  1524. m_pvertexLBufferD3D[index].tv = pvertex[index].v;
  1525. float bright =
  1526. pvertex[index].nx * m_vecLight.X()
  1527. + pvertex[index].ny * m_vecLight.Y()
  1528. + pvertex[index].nz * m_vecLight.Z()
  1529. + m_brightAmbient;
  1530. if (bright > 1) {
  1531. bright = 1;
  1532. } else if (bright < 0) {
  1533. bright = 0;
  1534. }
  1535. m_pvertexLBufferD3D[index].color = MakeD3DCOLOR(Color(bright, bright, bright, 1));
  1536. m_pvertexLBufferD3D[index].specular = 0;
  1537. }
  1538. (this->*pfnDraw)(m_pvertexLBufferD3D, vcount, pindex, icount);
  1539. }
  1540. void CopyLightVertexD3D(const Vertex* pvertex, int vcount, const WORD* pindex, int icount, PFNDrawD3D pfnDraw)
  1541. {
  1542. SizeVertexLBufferD3D(vcount);
  1543. for (int index = 0; index < vcount; index++) {
  1544. m_pvertexLBufferD3D[index].x = pvertex[index].x;
  1545. m_pvertexLBufferD3D[index].y = pvertex[index].y;
  1546. m_pvertexLBufferD3D[index].z = pvertex[index].z;
  1547. m_pvertexLBufferD3D[index].tu = pvertex[index].u;
  1548. m_pvertexLBufferD3D[index].tv = pvertex[index].v;
  1549. m_pvertexLBufferD3D[index].color = MakeD3DCOLOR(Color(1, 1, 1, 1));
  1550. m_pvertexLBufferD3D[index].specular = 0;
  1551. }
  1552. (this->*pfnDraw)(m_pvertexLBufferD3D, vcount, pindex, icount);
  1553. }
  1554. void MaterialLightVertexD3D(const Vertex* pvertex, int vcount, const WORD* pindex, int icount, PFNDrawD3D pfnDraw)
  1555. {
  1556. UpdateLighting();
  1557. SizeVertexLBufferD3D(vcount);
  1558. float r = m_pmaterial->GetDiffuse().GetRed();
  1559. float g = m_pmaterial->GetDiffuse().GetGreen();
  1560. float b = m_pmaterial->GetDiffuse().GetBlue();
  1561. float a = m_pmaterial->GetDiffuse().GetAlpha();
  1562. for (int index = 0; index < vcount; index++) {
  1563. m_pvertexLBufferD3D[index].x = pvertex[index].x;
  1564. m_pvertexLBufferD3D[index].y = pvertex[index].y;
  1565. m_pvertexLBufferD3D[index].z = pvertex[index].z;
  1566. m_pvertexLBufferD3D[index].tu = pvertex[index].u;
  1567. m_pvertexLBufferD3D[index].tv = pvertex[index].v;
  1568. float bright =
  1569. pvertex[index].nx * m_vecLight.X()
  1570. + pvertex[index].ny * m_vecLight.Y()
  1571. + pvertex[index].nz * m_vecLight.Z()
  1572. + m_brightAmbient;
  1573. if (bright > 1) {
  1574. bright = 1;
  1575. } else if (bright < 0) {
  1576. bright = 0;
  1577. }
  1578. m_pvertexLBufferD3D[index].color =
  1579. MakeD3DCOLOR(
  1580. Color(r * bright, g * bright, b * bright, a));
  1581. m_pvertexLBufferD3D[index].specular = 0;
  1582. }
  1583. (this->*pfnDraw)(m_pvertexLBufferD3D, vcount, pindex, icount);
  1584. }
  1585. void CopyDrawD3D(const VertexL* pvertex, int vcount, const WORD* pindex, int icount, PFNDrawD3D pfnDraw)
  1586. {
  1587. SizeVertexLBufferD3D(vcount);
  1588. for (int index = 0; index < vcount; index++) {
  1589. m_pvertexLBufferD3D[index].x = pvertex[index].x;
  1590. m_pvertexLBufferD3D[index].y = pvertex[index].y;
  1591. m_pvertexLBufferD3D[index].z = pvertex[index].z;
  1592. m_pvertexLBufferD3D[index].tu = pvertex[index].u;
  1593. m_pvertexLBufferD3D[index].tv = pvertex[index].v;
  1594. m_pvertexLBufferD3D[index].color =
  1595. MakeD3DCOLOR(
  1596. Color(
  1597. pvertex[index].r,
  1598. pvertex[index].g,
  1599. pvertex[index].b,
  1600. pvertex[index].a));
  1601. m_pvertexLBufferD3D[index].specular = 0;
  1602. }
  1603. (this->*pfnDraw)(m_pvertexLBufferD3D, vcount, pindex, icount);
  1604. }
  1605. //////////////////////////////////////////////////////////////////////////////
  1606. //
  1607. // Vertices need lighting
  1608. //
  1609. //////////////////////////////////////////////////////////////////////////////
  1610. void DrawTriangles(const VertexScreen* pvertex, int vcount, const WORD* pindex, int icount)
  1611. {
  1612. m_prasterizer->DrawTriangles(pvertex, vcount, pindex, icount);
  1613. }
  1614. void DrawLines(const VertexScreen* pvertex, int vcount, const WORD* pindex, int icount)
  1615. {
  1616. m_prasterizer->DrawLines(pvertex, vcount, pindex, icount);
  1617. }
  1618. void DrawPoints(const VertexScreen* pvertex, int vcount, const WORD* pindex, int icount)
  1619. {
  1620. m_prasterizer->DrawPoints(pvertex, vcount);
  1621. }
  1622. void DrawPoints(const VertexScreen* pvertex, int vcount)
  1623. {
  1624. m_prasterizer->DrawPoints(pvertex, vcount);
  1625. }
  1626. void DrawTriangles(const D3DVertex* pvertex, int vcount, const WORD* pindex, int icount)
  1627. {
  1628. #ifdef EnablePerformanceCounters
  1629. m_countTriangles += icount / 3;
  1630. #endif
  1631. m_prasterizer->DrawTrianglesD3D(pvertex, vcount, pindex, icount);
  1632. }
  1633. void DrawLines(const D3DVertex* pvertex, int vcount, const WORD* pindex, int icount)
  1634. {
  1635. #ifdef EnablePerformanceCounters
  1636. m_countLines += icount / 3;
  1637. #endif
  1638. m_prasterizer->DrawLinesD3D(pvertex, vcount, pindex, icount);
  1639. }
  1640. void DrawPoints(const D3DVertex* pvertex, int vcount, const WORD* pindex, int icount)
  1641. {
  1642. #ifdef EnablePerformanceCounters
  1643. m_countPoints += icount - 1;
  1644. #endif
  1645. m_prasterizer->DrawPointsD3D(pvertex, vcount);
  1646. }
  1647. void DrawPoints(const D3DVertex* pvertex, int vcount)
  1648. {
  1649. #ifdef EnablePerformanceCounters
  1650. m_countPoints += vcount;
  1651. #endif
  1652. m_prasterizer->DrawPointsD3D(pvertex, vcount);
  1653. }
  1654. void DrawTriangles(const D3DLVertex* pvertex, int vcount, const WORD* pindex, int icount)
  1655. {
  1656. m_prasterizer->DrawTrianglesD3D(pvertex, vcount, pindex, icount);
  1657. }
  1658. void DrawLines(const D3DLVertex* pvertex, int vcount, const WORD* pindex, int icount)
  1659. {
  1660. m_prasterizer->DrawLinesD3D(pvertex, vcount, pindex, icount);
  1661. }
  1662. void DrawPoints(const D3DLVertex* pvertex, int vcount, const WORD* pindex, int icount)
  1663. {
  1664. m_prasterizer->DrawPointsD3D(pvertex, vcount);
  1665. }
  1666. void DrawPoints(const D3DLVertex* pvertex, int vcount)
  1667. {
  1668. m_prasterizer->DrawPointsD3D(pvertex, vcount);
  1669. }
  1670. //////////////////////////////////////////////////////////////////////////////
  1671. //
  1672. //
  1673. //
  1674. //////////////////////////////////////////////////////////////////////////////
  1675. void DrawTriangles(const Vertex* pvertex, int vcount, const WORD* pindex, int icount)
  1676. {
  1677. #ifdef EnablePerformanceCounters
  1678. m_countTriangles += icount / 3;
  1679. #endif
  1680. UpdatePointers();
  1681. if (m_bDrawD3D)
  1682. (this->*m_pfnLightD3D)(pvertex, vcount, pindex, icount, DrawTriangles);
  1683. else
  1684. (this->*m_pfnLightVertex)(pvertex, vcount, pindex, icount, ClipTriangles, DrawTriangles);
  1685. }
  1686. //////////////////////////////////////////////////////////////////////////////
  1687. //
  1688. //
  1689. //
  1690. //////////////////////////////////////////////////////////////////////////////
  1691. void DrawLines(const Vertex* pvertex, int vcount, const WORD* pindex, int icount)
  1692. {
  1693. #ifdef EnablePerformanceCounters
  1694. m_countLines += icount - 1;
  1695. #endif
  1696. UpdatePointers();
  1697. if (m_bDrawD3D)
  1698. (this->*m_pfnLightD3D)(pvertex, vcount, pindex, icount, DrawLines);
  1699. else
  1700. (this->*m_pfnLightVertex)(pvertex, vcount, pindex, icount, ClipLines, DrawLines);
  1701. }
  1702. //////////////////////////////////////////////////////////////////////////////
  1703. //
  1704. //
  1705. //
  1706. //////////////////////////////////////////////////////////////////////////////
  1707. void DrawPoints(const Vertex* pvertex, int vcount)
  1708. {
  1709. #ifdef EnablePerformanceCounters
  1710. m_countPoints += vcount;
  1711. #endif
  1712. UpdatePointers();
  1713. if (m_bDrawD3D)
  1714. (this->*m_pfnLightD3D)(pvertex, vcount, NULL, 0, DrawPoints);
  1715. else
  1716. (this->*m_pfnLightVertex)(pvertex, vcount, NULL, 0, ClipPoints, DrawPoints);
  1717. }
  1718. //////////////////////////////////////////////////////////////////////////////
  1719. //
  1720. // Draw Colored Triangles
  1721. //
  1722. //////////////////////////////////////////////////////////////////////////////
  1723. void DrawTriangles(const VertexL* pvertex, int vcount, const WORD* pindex, int icount)
  1724. {
  1725. #ifdef EnablePerformanceCounters
  1726. m_countTriangles += icount / 3;
  1727. #endif
  1728. UpdatePointers();
  1729. if (m_bDrawD3D)
  1730. CopyDrawD3D(pvertex, vcount, pindex, icount, DrawTriangles);
  1731. else
  1732. (this->*m_pfnLightVertexL)(pvertex, vcount, pindex, icount, ClipTriangles, DrawTriangles);
  1733. }
  1734. //////////////////////////////////////////////////////////////////////////////
  1735. //
  1736. // Draw Colored Lines
  1737. //
  1738. //////////////////////////////////////////////////////////////////////////////
  1739. void DrawLines(const VertexL* pvertex, int vcount, const WORD* pindex, int icount)
  1740. {
  1741. #ifdef EnablePerformanceCounters
  1742. m_countLines += icount - 1;
  1743. #endif
  1744. UpdatePointers();
  1745. if (m_bDrawD3D)
  1746. CopyDrawD3D(pvertex, vcount, pindex, icount, DrawLines);
  1747. else
  1748. (this->*m_pfnLightVertexL)(pvertex, vcount, pindex, icount, ClipLines, DrawLines);
  1749. }
  1750. //////////////////////////////////////////////////////////////////////////////
  1751. //
  1752. // Draw Colored Points
  1753. //
  1754. //////////////////////////////////////////////////////////////////////////////
  1755. void DrawPoints(const VertexL* pvertex, int vcount)
  1756. {
  1757. #ifdef EnablePerformanceCounters
  1758. m_countPoints += vcount;
  1759. #endif
  1760. UpdatePointers();
  1761. if (m_bDrawD3D)
  1762. CopyDrawD3D(pvertex, vcount, NULL, 0, DrawPoints);
  1763. else
  1764. (this->*m_pfnLightVertexL)(pvertex, vcount, NULL, 0, ClipPoints, DrawPoints);
  1765. }
  1766. };
  1767. TRef<IDevice3D> CreateDevice3D(IRasterizer* prasterizerArg)
  1768. {
  1769. ID3DRasterizer* prasterizer; CastTo(prasterizer, prasterizerArg);
  1770. return new Device3D(prasterizer);
  1771. }