Advertisement
Guest User

Untitled

a guest
Jan 25th, 2012
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.44 KB | None | 0 0
  1. #include "DXHandler.h"
  2. #include "stdafx.h"
  3.  
  4. DXHandler::DXHandler(HWND* handles, int* widths, int* heights)
  5. {
  6. windowsResizedRecently = false;
  7.  
  8. d3d = Direct3DCreate9(D3D_SDK_VERSION);
  9. hWnd[0] = handles[0];
  10. hWnd[1] = handles[1];
  11. hWnd[2] = handles[2];
  12. hWnd[3] = handles[3];
  13. screenSize[0].x = widths[0];
  14. screenSize[0].y = heights[0];
  15. screenSize[1].x = widths[1];
  16. screenSize[1].y = heights[1];
  17. screenSize[2].x = widths[2];
  18. screenSize[2].y = heights[2];
  19. screenSize[3].x = widths[3];
  20. screenSize[3].y = heights[3];
  21.  
  22.  
  23. D3DDISPLAYMODE d3ddm;
  24. HRESULT hr;
  25. d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
  26.  
  27. ZeroMemory(&d3dpp, sizeof(d3dpp));
  28.  
  29. d3dpp.Windowed = TRUE;
  30. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  31. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  32. d3dpp.EnableAutoDepthStencil = TRUE;
  33. d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  34. d3dpp.hDeviceWindow = hWnd[0];
  35.  
  36. d3dpp.BackBufferHeight = screenSize[0].x;
  37. d3dpp.BackBufferWidth = screenSize[0].y;
  38.  
  39.  
  40. d3d->CreateDevice(D3DADAPTER_DEFAULT,
  41. D3DDEVTYPE_HAL,
  42. hWnd[0],
  43. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  44. &d3dpp,
  45. &d3ddev);
  46.  
  47.  
  48. // Initialise Variables
  49. setVariable("TeapotRotDir", (void*)1);
  50.  
  51. meshTeapot = NULL;
  52.  
  53. float fVal = 0.3f;
  54. DWORD dwVal = *(DWORD*)&fVal;
  55. setVariable("TeapotSpeed", (void*)dwVal);
  56.  
  57. restoreDeviceObjects();
  58. }
  59.  
  60. DXHandler::~DXHandler()
  61. {
  62. clean();
  63. }
  64.  
  65. void DXHandler::render()
  66. {
  67. if(windowsResizedRecently)
  68. {
  69. invalidateDeviceObjects();
  70.  
  71. d3dpp.BackBufferHeight = screenSize[0].x;
  72. d3dpp.BackBufferWidth = screenSize[0].y;
  73. d3dpp.hDeviceWindow = hWnd[0];
  74.  
  75. HRESULT hr = d3ddev->Reset(&d3dpp);
  76.  
  77. if(hr != D3D_OK)
  78. {
  79. MessageBox(NULL, "Call to Reset() failed!", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  80. }
  81.  
  82. restoreDeviceObjects();
  83.  
  84. windowsResizedRecently = false;
  85. }
  86.  
  87. render0();
  88. render1();
  89. render2();
  90. render3();
  91. }
  92.  
  93. void DXHandler::clean()
  94. {
  95. invalidateDeviceObjects();
  96.  
  97. d3ddev->Release();
  98. d3d->Release();
  99. }
  100.  
  101. void DXHandler::init_light()
  102. {
  103. D3DLIGHT9 light;
  104. D3DMATERIAL9 material;
  105.  
  106. ZeroMemory(&light, sizeof(light));
  107. light.Type = D3DLIGHT_DIRECTIONAL;
  108. light.Diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
  109. light.Direction = D3DXVECTOR3(-1.0f, -0.3f, -1.0f);
  110.  
  111. d3ddev->SetLight(0, &light);
  112. d3ddev->LightEnable(0, TRUE);
  113.  
  114. ZeroMemory(&material, sizeof(D3DMATERIAL9));
  115. material.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  116. material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  117.  
  118. d3ddev->SetMaterial(&material);
  119. }
  120.  
  121. void DXHandler::setVariable(string name, void* value)
  122. {
  123. map<string, void*>::iterator iter = variables.begin();
  124. iter = variables.find(name);
  125.  
  126. if(iter != variables.end())
  127. {
  128. iter->second = value;
  129. }
  130. else
  131. {
  132. variables.insert(pair<string, void*>(name, value));
  133. }
  134. }
  135.  
  136. void* DXHandler::getVariable(string name)
  137. {
  138. map<string, void*>::iterator iter = variables.begin();
  139. iter = variables.find(name);
  140.  
  141. if(iter != variables.end())
  142. {
  143. return iter->second;;
  144. }
  145. else
  146. {
  147. return NULL;
  148. }
  149. }
  150.  
  151. void DXHandler::updateBackBuffer(int width, int height, short which)
  152. {
  153. screenSize[which].x = width;
  154. screenSize[which].y = height;
  155. d3dpp.BackBufferHeight = screenSize[which].x;
  156. d3dpp.BackBufferWidth = screenSize[which].y;
  157. d3dpp.hDeviceWindow = hWnd[which];
  158.  
  159. windowsResizedRecently = true;
  160.  
  161. /* invalidateDeviceObjects();
  162.  
  163. HRESULT hr = d3ddev->Reset(&d3dpp);
  164.  
  165. if(hr != D3D_OK)
  166. {
  167. MessageBox(NULL, "Call to Reset() failed!", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  168. }
  169.  
  170. restoreDeviceObjects();*/
  171. }
  172.  
  173. void DXHandler::invalidateDeviceObjects()
  174. {
  175. if(meshTeapot != NULL)
  176. {
  177. int nNewRefCount = meshTeapot->Release();
  178.  
  179. if(nNewRefCount > 0)
  180. {
  181. static char strError[255];
  182. sprintf( strError, "The teapot object failed to cleanup properly.\n"
  183. "Release() returned a reference count of %d", nNewRefCount );
  184. MessageBox( NULL, strError, "ERROR", MB_OK | MB_ICONEXCLAMATION );
  185. }
  186.  
  187. meshTeapot = NULL;
  188. }
  189.  
  190. for(int i = 0; i < sizeof(swapchains) / sizeof(LPDIRECT3DSWAPCHAIN9); i++)
  191. {
  192. if(swapchains[i] != NULL)
  193. {
  194. int nNewRefCount = swapchains[i]->Release();
  195.  
  196. if(nNewRefCount > 0)
  197. {
  198. static char strError[255];
  199. sprintf( strError, "The swapchain object failed to cleanup properly.\n"
  200. "Release() returned a reference count of %d", nNewRefCount );
  201. MessageBox( NULL, strError, "ERROR", MB_OK | MB_ICONEXCLAMATION );
  202. }
  203.  
  204. swapchains[i] = NULL;
  205. }
  206. }
  207.  
  208. for(int i = 0; i < sizeof(depthStencils) / sizeof(IDirect3DSurface9); i++)
  209. {
  210. if(depthStencils[i] != NULL)
  211. {
  212. int nNewRefCount = depthStencils[i]->Release();
  213.  
  214. if(nNewRefCount > 0)
  215. {
  216. static char strError[255];
  217. sprintf(strError, "The depthStencils object failed to cleanup properly.\n"
  218. "Release() returned a reference count of %d", nNewRefCount);
  219. MessageBox(NULL, strError, "ERROR", MB_OK | MB_ICONEXCLAMATION);
  220. }
  221.  
  222. depthStencils[i] = NULL;
  223. }
  224. }
  225. }
  226.  
  227. void DXHandler::restoreDeviceObjects()
  228. {
  229. d3ddev->GetSwapChain(0, &swapchains[0]);
  230. d3ddev->GetDepthStencilSurface(&depthStencils[0]);
  231.  
  232. for(int i = 1; i < 4; i++)
  233. {
  234. d3dpp.BackBufferHeight = screenSize[i].x;
  235. d3dpp.BackBufferWidth = screenSize[i].y;
  236. d3dpp.hDeviceWindow = hWnd[i];
  237.  
  238. d3ddev->CreateDepthStencilSurface(d3dpp.BackBufferHeight, d3dpp.BackBufferWidth, d3dpp.BackBufferFormat, d3dpp.MultiSampleType, d3dpp.MultiSampleQuality, true, &depthStencils[i], NULL);
  239.  
  240. d3ddev->CreateAdditionalSwapChain(&d3dpp, &swapchains[i]);
  241. }
  242.  
  243. init_light();
  244.  
  245. d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
  246. d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);
  247. d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));
  248.  
  249. D3DXCreateTeapot(d3ddev, &meshTeapot, NULL);
  250. }
  251.  
  252. void DXHandler::render0()
  253. {
  254. LPDIRECT3DSURFACE9 pBackBuffer = NULL;
  255. swapchains[0]->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
  256. d3ddev->SetRenderTarget(0, pBackBuffer);
  257. d3ddev->SetDepthStencilSurface(depthStencils[0]);
  258.  
  259. d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  260. d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  261.  
  262. d3ddev->BeginScene();
  263.  
  264. // set the view transform
  265. D3DXMATRIX matView; // the view transform matrix
  266. D3DXMatrixLookAtLH(&matView,
  267. &D3DXVECTOR3 (0.0f, 1.0f, -6.0f), // the camera position
  268. &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
  269. &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
  270. d3ddev->SetTransform(D3DTS_VIEW, &matView);
  271.  
  272. // set the projection transform
  273. D3DXMATRIX matProjection; // the projection transform matrix
  274. D3DXMatrixPerspectiveFovLH(&matProjection,
  275. D3DXToRadian(45), // the horizontal field of view
  276. (FLOAT)screenSize[0].x / (FLOAT)screenSize[0].y, // aspect ratio
  277. 1.0f, // the near view-plane
  278. 100.0f); // the far view-plane
  279. d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);
  280.  
  281. static float index = 0.0f;
  282. if((bool)getVariable("TeapotRotDir") == 1)
  283. index += 0.3f / 10;
  284. else
  285. index -= 0.3f / 10;
  286.  
  287. D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
  288. D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
  289. d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);
  290.  
  291. meshTeapot->DrawSubset(0);
  292.  
  293. d3ddev->EndScene();
  294.  
  295. swapchains[0]->Present(NULL, NULL, hWnd[0], NULL, 0);
  296.  
  297. pBackBuffer->Release();
  298. }
  299.  
  300. void DXHandler::render1()
  301. {
  302. LPDIRECT3DSURFACE9 pBackBuffer = NULL;
  303. swapchains[1]->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
  304. d3ddev->SetRenderTarget(0, pBackBuffer);
  305. d3ddev->SetDepthStencilSurface(depthStencils[1]);
  306.  
  307. d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  308. d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  309.  
  310. d3ddev->BeginScene();
  311.  
  312. // set the view transform
  313. D3DXMATRIX matView; // the view transform matrix
  314. D3DXMatrixLookAtLH(&matView,
  315. &D3DXVECTOR3 (0.0f, 1.0f, -6.0f), // the camera position
  316. &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
  317. &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
  318. d3ddev->SetTransform(D3DTS_VIEW, &matView);
  319.  
  320. // set the projection transform
  321. D3DXMATRIX matProjection; // the projection transform matrix
  322. D3DXMatrixPerspectiveFovLH(&matProjection,
  323. D3DXToRadian(45), // the horizontal field of view
  324. (FLOAT)screenSize[1].x / (FLOAT)screenSize[1].y, // aspect ratio
  325. 1.0f, // the near view-plane
  326. 100.0f); // the far view-plane
  327. d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);
  328.  
  329. static float index = 0.0f;
  330. if((bool)getVariable("TeapotRotDir") == 1)
  331. index += 0.3f / 10;
  332. else
  333. index -= 0.3f / 10;
  334.  
  335. D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
  336. D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
  337. d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);
  338.  
  339. meshTeapot->DrawSubset(0);
  340.  
  341. d3ddev->EndScene();
  342.  
  343. swapchains[1]->Present( NULL, NULL, hWnd[1], NULL, 0 );
  344.  
  345. pBackBuffer->Release();
  346. }
  347.  
  348. void DXHandler::render2()
  349. {
  350. LPDIRECT3DSURFACE9 pBackBuffer = NULL;
  351. swapchains[2]->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
  352. d3ddev->SetRenderTarget(0, pBackBuffer);
  353. d3ddev->SetDepthStencilSurface(depthStencils[2]);
  354.  
  355. d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  356. d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  357.  
  358. d3ddev->BeginScene();
  359.  
  360. // set the view transform
  361. D3DXMATRIX matView; // the view transform matrix
  362. D3DXMatrixLookAtLH(&matView,
  363. &D3DXVECTOR3 (0.0f, 1.0f, -6.0f), // the camera position
  364. &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
  365. &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
  366. d3ddev->SetTransform(D3DTS_VIEW, &matView);
  367.  
  368. // set the projection transform
  369. D3DXMATRIX matProjection; // the projection transform matrix
  370. D3DXMatrixPerspectiveFovLH(&matProjection,
  371. D3DXToRadian(45), // the horizontal field of view
  372. (FLOAT)screenSize[2].x / (FLOAT)screenSize[2].y, // aspect ratio
  373. 1.0f, // the near view-plane
  374. 100.0f); // the far view-plane
  375. d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);
  376.  
  377. static float index = 0.0f;
  378. if((bool)getVariable("TeapotRotDir") == 1)
  379. index += 0.3f / 10;
  380. else
  381. index -= 0.3f / 10;
  382.  
  383. D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
  384. D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
  385. d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);
  386.  
  387. meshTeapot->DrawSubset(0);
  388.  
  389. d3ddev->EndScene();
  390.  
  391. swapchains[2]->Present( NULL, NULL, hWnd[2], NULL, 0 );
  392.  
  393. pBackBuffer->Release();
  394. }
  395.  
  396. void DXHandler::render3()
  397. {
  398. LPDIRECT3DSURFACE9 pBackBuffer = NULL;
  399. swapchains[3]->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
  400. d3ddev->SetRenderTarget(0, pBackBuffer);
  401. d3ddev->SetDepthStencilSurface(depthStencils[3]);
  402.  
  403. d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  404. d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  405.  
  406. d3ddev->BeginScene();
  407.  
  408. // set the view transform
  409. D3DXMATRIX matView; // the view transform matrix
  410. D3DXMatrixLookAtLH(&matView,
  411. &D3DXVECTOR3 (0.0f, 1.0f, -6.0f), // the camera position
  412. &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
  413. &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
  414. d3ddev->SetTransform(D3DTS_VIEW, &matView);
  415.  
  416. // set the projection transform
  417. D3DXMATRIX matProjection; // the projection transform matrix
  418. D3DXMatrixPerspectiveFovLH(&matProjection,
  419. D3DXToRadian(45), // the horizontal field of view
  420. (FLOAT)screenSize[3].x / (FLOAT)screenSize[3].y, // aspect ratio
  421. 1.0f, // the near view-plane
  422. 100.0f); // the far view-plane
  423. d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);
  424.  
  425. static float index = 0.0f;
  426. if((bool)getVariable("TeapotRotDir") == 1)
  427. index += 0.3f / 10;
  428. else
  429. index -= 0.3f / 10;
  430.  
  431. D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
  432. D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
  433. d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);
  434.  
  435. meshTeapot->DrawSubset(0);
  436.  
  437. d3ddev->EndScene();
  438.  
  439. swapchains[3]->Present( NULL, NULL, hWnd[3], NULL, 0 );
  440.  
  441. pBackBuffer->Release();
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement