HRESULT RacorX8::RestoreDeviceObjects() { HRESULT hr; IDirect3DDevice8* device; hr = m_spD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, m_iVP, &m_dpps, &device); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateDevice failed!", L"Error", 0); return E_FAIL; } m_spDevice.reset(device, [](IDirect3DDevice8* device) { device->Release(); }); //CreateSphere(); LoadXFile("sphere.x"); IDirect3DVertexBuffer8* vbNormal; hr = m_spDevice->CreateVertexBuffer(m_iNumVertices * 2 * sizeof SimpleVertex, D3DUSAGE_WRITEONLY, D3DFVF_XYZ, D3DPOOL_MANAGED, &vbNormal); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateVertexBuffer failed!", L"Error", 0); return E_FAIL; } hr = CreateNormal<ShaderVertex, SimpleVertex>(m_spVB.get(), vbNormal); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateNormal failed!", L"Error", 0); return E_FAIL; } m_spVBNormal.reset(vbNormal, [](IDirect3DVertexBuffer8* vbNormal){vbNormal->Release(); }); IDirect3DVertexBuffer8* vbTangent; hr = m_spDevice->CreateVertexBuffer(m_iNumVertices * 2 * sizeof SimpleVertex, D3DUSAGE_WRITEONLY, D3DFVF_XYZ, D3DPOOL_MANAGED, &vbTangent); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateVertexBuffer failed!", L"Error", 0); return E_FAIL; } hr = CreateTangent<ShaderVertex, SimpleVertex>(m_spVB.get(), vbTangent); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateTangent failed!", L"Error", 0); return E_FAIL; } m_spVBTangent.reset(vbTangent, [](IDirect3DVertexBuffer8* vbTangent){vbTangent->Release(); }); //DWORD dwDecl[MAX_FVF_DECL_SIZE]; //ZeroMemory(dwDecl, sizeof dwDecl); //pSphere->GetDeclaration(dwDecl); DWORD decl[] = { D3DVSD_STREAM(0), D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3), D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_FLOAT3), D3DVSD_REG(D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT2), D3DVSD_REG(D3DVSDE_TEXCOORD1, D3DVSDT_FLOAT3), D3DVSD_END() }; hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"specular.vso", &m_dwVSH); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateVSFromBinFile failed!", L"Error", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"specular.pso", &m_dwPSH); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"specularBump.pso", &m_dwPSHBump); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0); return E_FAIL; } if (m_bPS14Avaliable) { hr = CreatePSFromBinFile(m_spDevice.get(), L"specularBump14.pso", &m_dwPSHBump14); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0); return E_FAIL; } } DWORD declLine[] = { D3DVSD_STREAM(0), D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3), D3DVSD_END() }; hr = CreateVSFromBinFile(m_spDevice.get(), declLine, L"line.vso", &m_dwVSHLine); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateVSFromBinFile failed!", L"Error", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"line.pso", &m_dwVPHLine); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0); return E_FAIL; } IDirect3DTexture8* color_map; hr = D3DXCreateTextureFromFile(m_spDevice.get(), _T("earth.bmp"), &color_map); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXCreateTextureFromFile failed!", L"Error", 0); return E_FAIL; } m_spColorMap.reset(color_map, [](IDirect3DTexture8* color_map){ color_map->Release(); }); IDirect3DTexture8* heightMap; hr = D3DXCreateTextureFromFile(m_spDevice.get(), _T("earthbump.bmp"), &heightMap); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXCreateTextureFromFile failed!", L"Error", 0); return E_FAIL; } m_spHeightMap.reset(heightMap, [](IDirect3DTexture8* heightMap) { heightMap->Release(); }); D3DSURFACE_DESC desc; m_spHeightMap->GetLevelDesc(0, &desc); IDirect3DTexture8* normalMap; hr = D3DXCreateTexture(m_spDevice.get(), desc.Width, desc.Height, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &normalMap); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXCreateTexture failed!", L"Error", 0); return E_FAIL; } m_spNormalMap.reset(normalMap, [](IDirect3DTexture8* normalMap){ normalMap->Release(); }); hr = D3DXComputeNormalMap(m_spNormalMap.get(), m_spHeightMap.get(), NULL, 0, D3DX_CHANNEL_RED, 10); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXComputeNormalMap failed!", L"Error", 0); return E_FAIL; } IDirect3DTexture8* power; hr = D3DXCreateTexture(m_spDevice.get(), 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &power); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXCreateTexture failed!", L"Error", 0); return E_FAIL; } m_spPower.reset(power, [](IDirect3DTexture8* power){ power->Release(); }); FLOAT fPower = 16.0f; hr = D3DXFillTexture(power, LightEval, &fPower); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXFillTexture failed!", L"Error", 0); return E_FAIL; } m_spDevice->SetViewport(&m_Viewport); m_spDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); m_spDevice->SetRenderState(D3DRS_ZENABLE, TRUE); m_spDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); m_spDevice->SetRenderState(D3DRS_LIGHTING, FALSE); m_spDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); //m_spDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); //m_spDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); //m_spDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE); //m_spDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); //m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); //m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); //m_spDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); //m_spDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); m_spDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR); m_spDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); //m_spDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR); //m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); //m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); m_spDevice->SetTextureStageState(2, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); m_spDevice->SetTextureStageState(2, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); D3DXVECTOR4 vLight(0.0f, 0.0f, 1.0f, 0.0f); D3DXVec4Normalize(&vLight, &vLight); m_spDevice->SetVertexShaderConstant(12, &vLight, 1); D3DXVECTOR4 half(0.5f, 0.5f, 0.5f, 0.5f); m_spDevice->SetVertexShaderConstant(33, &half, 1); return S_OK; }
HRESULT RacorX5::RestoreDeviceObjects() { HRESULT hr; IDirect3DDevice8* device; hr = m_spD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, m_iVP, &m_dpps, &device); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateDevice failed!", L"Error", 0); return E_FAIL; } m_spDevice.reset(device, [](IDirect3DDevice8* device) { device->Release(); }); VERTICES cp[9]; cp[0].vPosition = D3DXVECTOR3(-1, 1, 0); cp[0].uv = D3DXVECTOR2(0.0f, 0.0f); cp[1].vPosition = D3DXVECTOR3(0, 1, 0); cp[1].uv = D3DXVECTOR2(0.5f, 0.0f); cp[2].vPosition = D3DXVECTOR3(1, 1, 0); cp[2].uv = D3DXVECTOR2(1.0f, 0.0f); cp[3].vPosition = D3DXVECTOR3(-1, 0, 0); cp[3].uv = D3DXVECTOR2(0.0f, 0.5f); cp[4].vPosition = D3DXVECTOR3(0, 0, -2); cp[4].uv = D3DXVECTOR2(0.5f, 0.5f); cp[5].vPosition = D3DXVECTOR3(1, 0, 0); cp[5].uv = D3DXVECTOR2(1.0f, 0.5f); cp[6].vPosition = D3DXVECTOR3(-1, -1, 0); cp[6].uv = D3DXVECTOR2(0.0f, 1.0f); cp[7].vPosition = D3DXVECTOR3(0, -1, 0); cp[7].uv = D3DXVECTOR2(0.5f, 1.0f); cp[8].vPosition = D3DXVECTOR3(1, -1, 0); cp[8].uv = D3DXVECTOR2(1.0f, 1.0f); m_spBPatch->m_dwULevel = 4; m_spBPatch->m_dwVLevel = 4; m_spBPatch->Create(cp, 3, 3); m_spBPatch->RestoreDeviceObjects(m_spDevice.get()); DWORD dwDecl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3), D3DVSD_REG(3, D3DVSDT_FLOAT3), D3DVSD_REG(7, D3DVSDT_FLOAT2), D3DVSD_END() }; hr = CreateVSFromBinFile(m_spDevice.get(), dwDecl, L"point_light.vso", &m_dwVSH); if (FAILED(hr)) { MessageBox(m_hWnd, L"CreateVSFromBinFile failed!", L"Error", 0); return E_FAIL; } IDirect3DTexture8* backgroud; hr = D3DXCreateTextureFromFile(m_spDevice.get(), _T("ShaderX.tga"), &backgroud); if (FAILED(hr)) { MessageBox(m_hWnd, L"D3DXCreateTextureFromFile failed!", L"Error", 0); return E_FAIL; } m_spBackground.reset(backgroud, [](IDirect3DTexture8* background){ background->Release(); }); m_spDevice->SetViewport(&m_Viewport); m_spDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); m_spDevice->SetRenderState(D3DRS_ZENABLE, TRUE); m_spDevice->SetRenderState(D3DRS_LIGHTING, FALSE); m_spDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); //m_spDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); m_spDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE); m_spDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); m_spDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); m_spDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); m_spDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR); m_spDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR); m_spDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); m_spDevice->SetVertexShaderConstant(SPEC_POWER, D3DXVECTOR4(0,10,25,50),1); //D3DXVECTOR3 vLight(0, 0, 1); //m_spDevice->SetVertexShaderConstant(LIGHT_POSITION, vLight, 1); D3DXCOLOR matDiffuse(0.9f, 0.9f, 0.9f, 1.0f); m_spDevice->SetVertexShaderConstant(DIFFUSE_COLOR, &matDiffuse, 1); D3DXVECTOR4 Attenuation = { 1.0f, 0.0f, 0.0f, 0.0f }; m_spDevice->SetVertexShaderConstant(LIGHT_ATTENUATION, Attenuation, 1); return S_OK; }
HRESULT HelloShadowVolume::RestoreDeviceObjects() { HRESULT hr; IDirect3DDevice8* device; hr = m_spD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_dpps, &device); if (FAILED(hr)) { MessageBox(0, L"CreateDevice failed", 0, 0); return E_FAIL; } m_spDevice.reset(device, [](IDirect3DDevice8* device) { device->Release(); }); m_spDevice->SetRenderState(D3DRS_ZENABLE, TRUE); m_spDevice->SetRenderState(D3DRS_LIGHTING, FALSE); m_spDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); D3DVIEWPORT8 viewport = { 0, 0, m_iWidth, m_iHeight }; m_spDevice->SetViewport(&viewport); D3DXVECTOR3 eye(0.0f, 0.0f, 30.0f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtLH(&m_mtView, &eye, &target, &up); D3DXMatrixPerspectiveFovLH(&m_mtProj, 0.2*D3DX_PI, (float)m_iWidth / (float)m_iHeight, 1.0f, 100.f); m_cPlaneTint = { 0.7f, 0.6f, 0.4f, 1.0f }; ID3DXMesh* plane; //D3DXCreatePolygon(m_spDevice.get(), 2.0f, 4, &plane, NULL); CreatePlane(m_spDevice.get(), 15.0f, 10, &plane); //D3DXCreateSphere(m_spDevice.get(), 1.0f,20,20, &plane, NULL); IDirect3DVertexBuffer8* vb; IDirect3DIndexBuffer8* ib; plane->GetVertexBuffer(&vb); plane->GetIndexBuffer(&ib); m_spPlaneVB.reset(vb, [](IDirect3DVertexBuffer8* vb) { vb->Release(); }); m_spPlaneIB.reset(ib, [](IDirect3DIndexBuffer8* ib) { ib->Release(); }); m_dwPlaneNumVertices = plane->GetNumVertices(); m_dwPlaneNumFaces = plane->GetNumFaces(); plane->Release(); DWORD decl[] = { D3DVSD_STREAM(0), D3DVSD_REG(0, D3DVSDT_FLOAT3), D3DVSD_REG(3, D3DVSDT_FLOAT3), D3DVSD_END() }; hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"plane.vso", &m_dwPlaneVSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreateVSFromBinFile failed", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"plane.pso", &m_dwPlanePSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreatePSFromBinFile failed", 0); return E_FAIL; } D3DXMATRIX Rx, Tz; D3DXMatrixRotationX(&Rx, D3DX_PI*0.5f); D3DXMatrixTranslation(&Tz, 0.0f, -3.0f, 0.0f); m_mtPlaneWorld = Rx * Tz; ID3DXMesh* occluder; CreateOccluder(m_spDevice.get(), &occluder); IDirect3DVertexBuffer8* vbOccluder; IDirect3DIndexBuffer8* ibOccluder; occluder->GetVertexBuffer(&vbOccluder); occluder->GetIndexBuffer(&ibOccluder); m_spOccluderVB.reset(vbOccluder, [](IDirect3DVertexBuffer8* vb) { vb->Release(); }); m_spOccluderIB.reset(ibOccluder, [](IDirect3DIndexBuffer8* ib) { ib->Release(); }); m_dwOccluderNumVertices = occluder->GetNumVertices(); m_dwOccluderNumFaces = occluder->GetNumFaces(); occluder->Release(); hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"occluder.vso", &m_dwOccluderVSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreateVSFromBinFile failed", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"occluder.pso", &m_dwOccluderPSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreatePSFromBinFile failed", 0); return E_FAIL; } m_cOccluderTint = { 0.3f, 0.0f, 0.8f, 1.0f }; D3DXMATRIX Rz, T; D3DXMatrixTranslation(&T, 5.1f * cosf(0.5), 0.0f, 5.1f * sinf(0.5)); D3DXMatrixIdentity(&m_mtVolumeWorld); D3DXMatrixRotationZ(&Rz, 0.5f); m_mtOccluderWorld = T * Rz; ID3DXMesh* volume; CreateVolume(m_spDevice.get(), &volume); IDirect3DVertexBuffer8* vbVolume; IDirect3DIndexBuffer8* ibVolume; volume->GetVertexBuffer(&vbVolume); volume->GetIndexBuffer(&ibVolume); m_spVolumeVB.reset(vbVolume, [](IDirect3DVertexBuffer8* vb) { vb->Release(); }); m_spVolumeIB.reset(ibVolume, [](IDirect3DIndexBuffer8* ib) { ib->Release(); }); m_dwVolumeNumVertices = volume->GetNumVertices(); m_dwVolumeNumFaces = volume->GetNumFaces(); volume->Release(); hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"volume.vso", &m_dwVolumeVSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreateVSFromBinFile failed", 0); return E_FAIL; } hr = CreatePSFromBinFile(m_spDevice.get(), L"volume.pso", &m_dwVolumePSH); if (FAILED(hr)) { MessageBox(0, 0, L"CreatePSFromBinFile failed", 0); return E_FAIL; } m_cVolumeTint = { 0.7f, 0.0f, 0.0f, 1.0f }; D3DXMATRIX Sx; D3DXMatrixIdentity(&m_mtVolumeWorld); D3DXMatrixScaling(&Sx, 6.0f, 1.0f, 1.0f); D3DXMatrixRotationZ(&Rz, 0.5f); m_mtVolumeWorld = Sx * Rz; return S_OK; }