Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DXHandler.h"
- #include "stdafx.h"
- DXHandler::DXHandler(HWND* handles, int* widths, int* heights)
- {
- windowsResizedRecently = false;
- d3d = Direct3DCreate9(D3D_SDK_VERSION);
- hWnd[0] = handles[0];
- hWnd[1] = handles[1];
- hWnd[2] = handles[2];
- hWnd[3] = handles[3];
- screenSize[0].x = widths[0];
- screenSize[0].y = heights[0];
- screenSize[1].x = widths[1];
- screenSize[1].y = heights[1];
- screenSize[2].x = widths[2];
- screenSize[2].y = heights[2];
- screenSize[3].x = widths[3];
- screenSize[3].y = heights[3];
- D3DDISPLAYMODE d3ddm;
- HRESULT hr;
- d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
- ZeroMemory(&d3dpp, sizeof(d3dpp));
- d3dpp.Windowed = TRUE;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
- d3dpp.EnableAutoDepthStencil = TRUE;
- d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
- d3dpp.hDeviceWindow = hWnd[0];
- d3dpp.BackBufferHeight = screenSize[0].x;
- d3dpp.BackBufferWidth = screenSize[0].y;
- d3d->CreateDevice(D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL,
- hWnd[0],
- D3DCREATE_SOFTWARE_VERTEXPROCESSING,
- &d3dpp,
- &d3ddev);
- // Initialise Variables
- setVariable("TeapotRotDir", (void*)1);
- meshTeapot = NULL;
- float fVal = 0.3f;
- DWORD dwVal = *(DWORD*)&fVal;
- setVariable("TeapotSpeed", (void*)dwVal);
- restoreDeviceObjects();
- }
- DXHandler::~DXHandler()
- {
- clean();
- }
- void DXHandler::render()
- {
- if(windowsResizedRecently)
- {
- invalidateDeviceObjects();
- d3dpp.BackBufferHeight = screenSize[0].x;
- d3dpp.BackBufferWidth = screenSize[0].y;
- d3dpp.hDeviceWindow = hWnd[0];
- HRESULT hr = d3ddev->Reset(&d3dpp);
- if(hr != D3D_OK)
- {
- MessageBox(NULL, "Call to Reset() failed!", "ERROR", MB_OK | MB_ICONEXCLAMATION);
- }
- restoreDeviceObjects();
- windowsResizedRecently = false;
- }
- render0();
- render1();
- render2();
- render3();
- }
- void DXHandler::clean()
- {
- invalidateDeviceObjects();
- d3ddev->Release();
- d3d->Release();
- }
- void DXHandler::init_light()
- {
- D3DLIGHT9 light;
- D3DMATERIAL9 material;
- ZeroMemory(&light, sizeof(light));
- light.Type = D3DLIGHT_DIRECTIONAL;
- light.Diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
- light.Direction = D3DXVECTOR3(-1.0f, -0.3f, -1.0f);
- d3ddev->SetLight(0, &light);
- d3ddev->LightEnable(0, TRUE);
- ZeroMemory(&material, sizeof(D3DMATERIAL9));
- material.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- material.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- d3ddev->SetMaterial(&material);
- }
- void DXHandler::setVariable(string name, void* value)
- {
- map<string, void*>::iterator iter = variables.begin();
- iter = variables.find(name);
- if(iter != variables.end())
- {
- iter->second = value;
- }
- else
- {
- variables.insert(pair<string, void*>(name, value));
- }
- }
- void* DXHandler::getVariable(string name)
- {
- map<string, void*>::iterator iter = variables.begin();
- iter = variables.find(name);
- if(iter != variables.end())
- {
- return iter->second;;
- }
- else
- {
- return NULL;
- }
- }
- void DXHandler::updateBackBuffer(int width, int height, short which)
- {
- screenSize[which].x = width;
- screenSize[which].y = height;
- d3dpp.BackBufferHeight = screenSize[which].x;
- d3dpp.BackBufferWidth = screenSize[which].y;
- d3dpp.hDeviceWindow = hWnd[which];
- windowsResizedRecently = true;
- /* invalidateDeviceObjects();
- HRESULT hr = d3ddev->Reset(&d3dpp);
- if(hr != D3D_OK)
- {
- MessageBox(NULL, "Call to Reset() failed!", "ERROR", MB_OK | MB_ICONEXCLAMATION);
- }
- restoreDeviceObjects();*/
- }
- void DXHandler::invalidateDeviceObjects()
- {
- if(meshTeapot != NULL)
- {
- int nNewRefCount = meshTeapot->Release();
- if(nNewRefCount > 0)
- {
- static char strError[255];
- sprintf( strError, "The teapot object failed to cleanup properly.\n"
- "Release() returned a reference count of %d", nNewRefCount );
- MessageBox( NULL, strError, "ERROR", MB_OK | MB_ICONEXCLAMATION );
- }
- meshTeapot = NULL;
- }
- for(int i = 0; i < sizeof(swapchains) / sizeof(LPDIRECT3DSWAPCHAIN9); i++)
- {
- if(swapchains[i] != NULL)
- {
- int nNewRefCount = swapchains[i]->Release();
- if(nNewRefCount > 0)
- {
- static char strError[255];
- sprintf( strError, "The swapchain object failed to cleanup properly.\n"
- "Release() returned a reference count of %d", nNewRefCount );
- MessageBox( NULL, strError, "ERROR", MB_OK | MB_ICONEXCLAMATION );
- }
- swapchains[i] = NULL;
- }
- }
- for(int i = 0; i < sizeof(depthStencils) / sizeof(IDirect3DSurface9); i++)
- {
- if(depthStencils[i] != NULL)
- {
- int nNewRefCount = depthStencils[i]->Release();
- if(nNewRefCount > 0)
- {
- static char strError[255];
- sprintf(strError, "The depthStencils object failed to cleanup properly.\n"
- "Release() returned a reference count of %d", nNewRefCount);
- MessageBox(NULL, strError, "ERROR", MB_OK | MB_ICONEXCLAMATION);
- }
- depthStencils[i] = NULL;
- }
- }
- }
- void DXHandler::restoreDeviceObjects()
- {
- d3ddev->GetSwapChain(0, &swapchains[0]);
- d3ddev->GetDepthStencilSurface(&depthStencils[0]);
- for(int i = 1; i < 4; i++)
- {
- d3dpp.BackBufferHeight = screenSize[i].x;
- d3dpp.BackBufferWidth = screenSize[i].y;
- d3dpp.hDeviceWindow = hWnd[i];
- d3ddev->CreateDepthStencilSurface(d3dpp.BackBufferHeight, d3dpp.BackBufferWidth, d3dpp.BackBufferFormat, d3dpp.MultiSampleType, d3dpp.MultiSampleQuality, true, &depthStencils[i], NULL);
- d3ddev->CreateAdditionalSwapChain(&d3dpp, &swapchains[i]);
- }
- init_light();
- d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
- d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);
- d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));
- D3DXCreateTeapot(d3ddev, &meshTeapot, NULL);
- }
- void DXHandler::render0()
- {
- LPDIRECT3DSURFACE9 pBackBuffer = NULL;
- swapchains[0]->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
- d3ddev->SetRenderTarget(0, pBackBuffer);
- d3ddev->SetDepthStencilSurface(depthStencils[0]);
- d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
- d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
- d3ddev->BeginScene();
- // set the view transform
- D3DXMATRIX matView; // the view transform matrix
- D3DXMatrixLookAtLH(&matView,
- &D3DXVECTOR3 (0.0f, 1.0f, -6.0f), // the camera position
- &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
- &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
- d3ddev->SetTransform(D3DTS_VIEW, &matView);
- // set the projection transform
- D3DXMATRIX matProjection; // the projection transform matrix
- D3DXMatrixPerspectiveFovLH(&matProjection,
- D3DXToRadian(45), // the horizontal field of view
- (FLOAT)screenSize[0].x / (FLOAT)screenSize[0].y, // aspect ratio
- 1.0f, // the near view-plane
- 100.0f); // the far view-plane
- d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);
- static float index = 0.0f;
- if((bool)getVariable("TeapotRotDir") == 1)
- index += 0.3f / 10;
- else
- index -= 0.3f / 10;
- D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
- D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
- d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);
- meshTeapot->DrawSubset(0);
- d3ddev->EndScene();
- swapchains[0]->Present(NULL, NULL, hWnd[0], NULL, 0);
- pBackBuffer->Release();
- }
- void DXHandler::render1()
- {
- LPDIRECT3DSURFACE9 pBackBuffer = NULL;
- swapchains[1]->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
- d3ddev->SetRenderTarget(0, pBackBuffer);
- d3ddev->SetDepthStencilSurface(depthStencils[1]);
- d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
- d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
- d3ddev->BeginScene();
- // set the view transform
- D3DXMATRIX matView; // the view transform matrix
- D3DXMatrixLookAtLH(&matView,
- &D3DXVECTOR3 (0.0f, 1.0f, -6.0f), // the camera position
- &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
- &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
- d3ddev->SetTransform(D3DTS_VIEW, &matView);
- // set the projection transform
- D3DXMATRIX matProjection; // the projection transform matrix
- D3DXMatrixPerspectiveFovLH(&matProjection,
- D3DXToRadian(45), // the horizontal field of view
- (FLOAT)screenSize[1].x / (FLOAT)screenSize[1].y, // aspect ratio
- 1.0f, // the near view-plane
- 100.0f); // the far view-plane
- d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);
- static float index = 0.0f;
- if((bool)getVariable("TeapotRotDir") == 1)
- index += 0.3f / 10;
- else
- index -= 0.3f / 10;
- D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
- D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
- d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);
- meshTeapot->DrawSubset(0);
- d3ddev->EndScene();
- swapchains[1]->Present( NULL, NULL, hWnd[1], NULL, 0 );
- pBackBuffer->Release();
- }
- void DXHandler::render2()
- {
- LPDIRECT3DSURFACE9 pBackBuffer = NULL;
- swapchains[2]->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
- d3ddev->SetRenderTarget(0, pBackBuffer);
- d3ddev->SetDepthStencilSurface(depthStencils[2]);
- d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
- d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
- d3ddev->BeginScene();
- // set the view transform
- D3DXMATRIX matView; // the view transform matrix
- D3DXMatrixLookAtLH(&matView,
- &D3DXVECTOR3 (0.0f, 1.0f, -6.0f), // the camera position
- &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
- &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
- d3ddev->SetTransform(D3DTS_VIEW, &matView);
- // set the projection transform
- D3DXMATRIX matProjection; // the projection transform matrix
- D3DXMatrixPerspectiveFovLH(&matProjection,
- D3DXToRadian(45), // the horizontal field of view
- (FLOAT)screenSize[2].x / (FLOAT)screenSize[2].y, // aspect ratio
- 1.0f, // the near view-plane
- 100.0f); // the far view-plane
- d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);
- static float index = 0.0f;
- if((bool)getVariable("TeapotRotDir") == 1)
- index += 0.3f / 10;
- else
- index -= 0.3f / 10;
- D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
- D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
- d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);
- meshTeapot->DrawSubset(0);
- d3ddev->EndScene();
- swapchains[2]->Present( NULL, NULL, hWnd[2], NULL, 0 );
- pBackBuffer->Release();
- }
- void DXHandler::render3()
- {
- LPDIRECT3DSURFACE9 pBackBuffer = NULL;
- swapchains[3]->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
- d3ddev->SetRenderTarget(0, pBackBuffer);
- d3ddev->SetDepthStencilSurface(depthStencils[3]);
- d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
- d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
- d3ddev->BeginScene();
- // set the view transform
- D3DXMATRIX matView; // the view transform matrix
- D3DXMatrixLookAtLH(&matView,
- &D3DXVECTOR3 (0.0f, 1.0f, -6.0f), // the camera position
- &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
- &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
- d3ddev->SetTransform(D3DTS_VIEW, &matView);
- // set the projection transform
- D3DXMATRIX matProjection; // the projection transform matrix
- D3DXMatrixPerspectiveFovLH(&matProjection,
- D3DXToRadian(45), // the horizontal field of view
- (FLOAT)screenSize[3].x / (FLOAT)screenSize[3].y, // aspect ratio
- 1.0f, // the near view-plane
- 100.0f); // the far view-plane
- d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);
- static float index = 0.0f;
- if((bool)getVariable("TeapotRotDir") == 1)
- index += 0.3f / 10;
- else
- index -= 0.3f / 10;
- D3DXMATRIX matRotateY; // a matrix to store the rotation for each triangle
- D3DXMatrixRotationY(&matRotateY, index); // the rotation matrix
- d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);
- meshTeapot->DrawSubset(0);
- d3ddev->EndScene();
- swapchains[3]->Present( NULL, NULL, hWnd[3], NULL, 0 );
- pBackBuffer->Release();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement