HRESULT KG3DSceneShadowMap::SetParametersForShader(LPD3DXEFFECT pShader) { KGLOG_PROCESS_ERROR(pShader); pShader->SetTexture("tShadowmap1",m_ShadowLevel[0].m_lpSMShadowMap); pShader->SetTexture("tShadowmap2",m_ShadowLevel[1].m_lpSMShadowMap); pShader->SetTexture("tShadowmap3",m_ShadowLevel[2].m_lpSMShadowMap); pShader->SetMatrix("matLightVP1",&m_ShadowLevel[0].m_matVP); pShader->SetMatrix("matLightVP2",&m_ShadowLevel[1].m_matVP); pShader->SetMatrix("matLightVP3",&m_ShadowLevel[2].m_matVP); /* D3DXVECTOR4 UV[9]; float x = 1.0F/m_ShadowLevel[0].m_dwShadowmapSize; float y = 1.0F/m_ShadowLevel[0].m_dwShadowmapSize; UV[0] = D3DXVECTOR4( 0, 0,0,0.3F); UV[1] = D3DXVECTOR4(-x, 0,0,0.3F); UV[2] = D3DXVECTOR4( x, 0,0,0.3F); UV[3] = D3DXVECTOR4( 0,-y,0,0.3F); UV[4] = D3DXVECTOR4( 0, y,0,0.3F); UV[5] = D3DXVECTOR4(-x, y,0,0.3F); UV[6] = D3DXVECTOR4( x, y,0,0.3F); UV[7] = D3DXVECTOR4( x,-y,0,0.3F); UV[8] = D3DXVECTOR4( x,-y,0,0.8F); pShader->SetVectorArray("g_avSampleOffsets",UV,9); */ return S_OK; Exit0: return E_FAIL; }
// 3D 물체등을 그린다. void RenderScene() { // 뷰 행렬을 만든다. D3DXMATRIXA16 matView; D3DXVECTOR3 vEyePt(gWorldCameraPosition.x, gWorldCameraPosition.y, gWorldCameraPosition.z); D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f); D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec); // 투영행렬을 만든다. D3DXMATRIXA16 matProjection; D3DXMatrixPerspectiveFovLH(&matProjection, FOV, ASPECT_RATIO, NEAR_PLANE, FAR_PLANE); // 프레임마다 0.4도씩 회전을 시킨다. gRotationY += 0.4f * PI / 180.0f; if (gRotationY > 2 * PI) { gRotationY -= 2 * PI; } // 월드행렬을 만든다. D3DXMATRIXA16 matWorld; D3DXMatrixRotationY(&matWorld, gRotationY); // 월드/뷰/투영행렬을 미리 곱한다. D3DXMATRIXA16 matWorldView; D3DXMATRIXA16 matWorldViewProjection; D3DXMatrixMultiply(&matWorldView, &matWorld, &matView); D3DXMatrixMultiply(&matWorldViewProjection, &matWorldView, &matProjection); // 쉐이더 전역변수들을 설정 gpEnvironmentMappingShader->SetMatrix("gWorldMatrix", &matWorld); gpEnvironmentMappingShader->SetMatrix("gWorldViewProjectionMatrix", &matWorldViewProjection); gpEnvironmentMappingShader->SetVector("gWorldLightPosition", &gWorldLightPosition); gpEnvironmentMappingShader->SetVector("gWorldCameraPosition", &gWorldCameraPosition); gpEnvironmentMappingShader->SetVector("gLightColor", &gLightColor); gpEnvironmentMappingShader->SetTexture("DiffuseMap_Tex", gpStoneDM); gpEnvironmentMappingShader->SetTexture("SpecularMap_Tex", gpStoneSM); gpEnvironmentMappingShader->SetTexture("NormalMap_Tex", gpStoneNM); gpEnvironmentMappingShader->SetTexture("EnvironmentMap_Tex", gpSnowENV); // 쉐이더를 시작한다. UINT numPasses = 0; gpEnvironmentMappingShader->Begin(&numPasses, NULL); { for (UINT i = 0; i < numPasses; ++i) { gpEnvironmentMappingShader->BeginPass(i); { // 구체를 그린다. gpTeapot->DrawSubset(0); } gpEnvironmentMappingShader->EndPass(); } } gpEnvironmentMappingShader->End(); }
//--------------------------------------------------------------------------------------- // ワールド変換行列設定(&シェーダへ設定) //--------------------------------------------------------------------------------------- void SetWorldMatrixShader(LPD3DXMATRIX pWorld) { g_pFX->SetMatrix(g_hMatWVP, &(*pWorld * g_mView * g_mProj)); g_pFX->SetMatrix(g_hMatW, pWorld); g_pFX->SetFloatArray(g_hLight, &g_vLight.x, 4); g_pFX->SetFloatArray(g_hCamera, &g_vCamera.x, 3); }
void SetupFX() { // 텍스처와 행렬값을 ID3DXEffect(여기서는 정점쉐이더)에 전달한다. g_pEffect->SetTexture( "tex0", g_pTexture ); g_pEffect->SetMatrix( "matW", &g_matWorld ); g_pEffect->SetMatrix( "matV", &g_matView ); g_pEffect->SetMatrix( "matP", &g_matProj ); }
//************************************************************************************************************* void RenderScene(LPD3DXEFFECT effect, int what) { D3DXMATRIX inv; D3DXVECTOR4 uv(1, 1, 1, 1); D3DXVECTOR4 spec(1, 1, 1, 1); for( int i = 0; i < numobjects; ++i ) { SceneObject& obj = objects[i]; if( obj.behavior == what ) { D3DXMatrixInverse(&inv, 0, &obj.world); effect->SetMatrix("matWorld", &obj.world); effect->SetMatrix("matWorldInv", &inv); if( obj.type == FLOOR ) { uv.x = uv.y = 3; spec = D3DXVECTOR4(0.2f, 0.2f, 0.2f, 20.0f); effect->SetVector("uv", &uv); effect->SetVector("matSpecular", &spec); effect->CommitChanges(); device->SetTexture(0, texture2); box->DrawSubset(0); } else if( obj.type == CRATE ) { uv.x = uv.y = 1; spec = D3DXVECTOR4(0.2f, 0.2f, 0.2f, 20.0f); effect->SetVector("uv", &uv); effect->SetVector("matSpecular", &spec); effect->CommitChanges(); device->SetTexture(0, texture3); box->DrawSubset(0); } else if( obj.type == SKULL ) { uv.x = uv.y = 1; spec = D3DXVECTOR4(0.75f, 0.75f, 0.75f, 80.0f); effect->SetVector("uv", &uv); effect->SetVector("matSpecular", &spec); effect->CommitChanges(); device->SetTexture(0, texture1); skull->DrawSubset(0); } } } }
// 3D 물체등을 그린다. void RenderScene() { // 뷰행렬 초기화. D3DXMATRIXA16 matView; // D3DXVECTOR3 vEyePt(0.0f, 0.0f, -200.0f); D3DXVECTOR3 vEyePt(gWorldCameraPosition.x, gWorldCameraPosition.y, gWorldCameraPosition.z); D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f); D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec); // 투영행렬 초기화. D3DXMATRIXA16 matProjection; D3DXMatrixPerspectiveFovLH(&matProjection, FOV, ASPECT_RATIO, NEAR_PLANE, FAR_PLANE); // 회전. gRotationY += ((0.4f * PI) / 180.0f); if (gRotationY > 2 * PI) gRotationY -= 2 * PI; // 월드행렬 초기화. D3DXMATRIXA16 matWorld; // D3DXMatrixIdentity(&matWorld); D3DXMatrixRotationY(&matWorld, gRotationY); // 쉐이더에 전달. gpTextureMappingShader->SetMatrix("gWorldMatrix", &matWorld); gpTextureMappingShader->SetMatrix("gViewMatrix", &matView); gpTextureMappingShader->SetMatrix("gProjectionMatrix", &matProjection); gpTextureMappingShader->SetVector("gWorldLightPosition", &gWorldLightPosition); gpTextureMappingShader->SetVector("gWorldCameraPosition", &gWorldCameraPosition); // gpTextureMappingShader->SetTexture("DiffuseMap_Tex", gpEarthDM); // 쉐이더 적용. UINT numPasses = 0; gpTextureMappingShader->Begin(&numPasses, NULL); for (UINT i = 0; i < numPasses; ++i) { gpTextureMappingShader->BeginPass(i); gpSphere->DrawSubset(0); gpTextureMappingShader->EndPass(); } gpTextureMappingShader->End(); }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { static float time = 0; D3DXMATRIX viewproj; D3DXMatrixMultiply(&viewproj, &view, &proj); D3DXMatrixScaling(&world, 0.1f, 0.1f, 0.1f); time += elapsedtime; device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); device->SetTransform(D3DTS_WORLD, &world); device->SetTransform(D3DTS_VIEW, &view); device->SetTransform(D3DTS_PROJECTION, &proj); mesh.Update(elapsedtime, &world); effect->SetMatrix("matViewProj", &viewproj); if( SUCCEEDED(device->BeginScene()) ) { mesh.Draw(); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }
void DXPointLight::DrawShadowMap(LPDIRECT3DDEVICE9 device, LPD3DXEFFECT effect, void (*drawcallback)(LPD3DXEFFECT)) { if( !shadowmap || !needsredraw ) return; LPDIRECT3DSURFACE9 surface = NULL; D3DXMATRIX vp; effect->SetVector("lightPos", (D3DXVECTOR4*)&position); for( int j = 0; j < 6; ++j ) { GetViewProjMatrix(vp, j); effect->SetMatrix("matViewProj", &vp); shadowmap->GetCubeMapSurface((D3DCUBEMAP_FACES)j, 0, &surface); device->SetRenderTarget(0, surface); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff000000, 1.0f, 0); drawcallback(effect); surface->Release(); } if( shadowtype == Static ) needsredraw = false; }
static void AddImpulse(void) { static Vector4 vPosition(0.0f, 0.0f, 0.0f, 0.0f); //Vector4 vDiff = vPosition - g_vPosition; Vector4 vDiff = g_vPosition - vPosition; Vector4 vLength = vDiff.Length(); if ( vLength[0]<2.0f ) return; Vector4 vDir = vDiff / vLength; Vector4 vVec0(vDir[1],-vDir[0], 0.0f, 0.0f); Vector4 vVec1(vDir[0], vDir[1], 0.0f, 0.0f); vPosition = g_vPosition; Vector4 vVec0_old = g_orient_matrix[0]; Vector4 vVec1_old = g_orient_matrix[1]; Vector4 vVec0_new = VectorLerp(vVec0_old, vVec0, 0.2f); Vector4 vVec1_new = VectorLerp(vVec1_old, vVec1, 0.2f); vVec0_new.Normalize(); vVec1_new.Normalize(); Vector4 vVec2_new = Vector3CrossProduct(vVec0_new, vVec1_new); g_orient_matrix.Identity(); g_orient_matrix[0] = vVec0_new; g_orient_matrix[1] = vVec1_new; g_orient_matrix[2] = vVec2_new; LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); device->SetRenderTarget(0, g_pSurfaces[TEX_HEIGHT1]); device->SetDepthStencilSurface(NULL); Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 world_matrix; world_matrix.Scale_Replace(g_fRippleSize, g_fRippleSize, 1.0f); world_matrix[3] = g_vPosition; Matrix4x4 wvp_matrix = g_orient_matrix * world_matrix * view_matrix * g_proj_matrix; D3DXHANDLE shader = g_pWaterEffect->GetTechniqueByName("AddImpulse"); D3DXHANDLE wvp_matrix_var = g_pWaterEffect->GetParameterByName(NULL, "wvp_matrix"); D3DXHANDLE force_var = g_pWaterEffect->GetParameterByName(NULL, "fForce"); g_pWaterEffect->SetTechnique(shader); g_pWaterEffect->SetMatrix(wvp_matrix_var, (D3DXMATRIX *)&wvp_matrix); g_pWaterEffect->SetFloat(force_var, 0.05f); g_pWaterEffect->Begin(NULL, 0); g_pWaterEffect->BeginPass(0); g_Model_DX9.Render(0); g_pWaterEffect->EndPass(); g_pWaterEffect->End(); vPosition = g_vPosition; }
HRESULT KG3DPointLightShadow::ExportToCubeSurface(KG3DRenderTargetTool* pTool,LPDIRECT3DSURFACE9 pDestSurface,BOOL bOutputDepth) { HRESULT hr = E_FAIL; KGLOG_PROCESS_ERROR(pTool); pTool->BeginNewRenderTarget(NULL,NULL,NULL); ////////////////////////////////////////////////////////////////////////// hr = g_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, 0x00808080, 1, 0); KGLOG_COM_PROCESS_ERROR(hr); { D3DXVECTOR2 A(0,0); D3DXVECTOR2 C(m_vShadowMap.m_dwShadowmapSize,m_vShadowMap.m_dwShadowmapSize); if(bOutputDepth) { D3DXMATRIX matInv; D3DXMatrixInverse(&matInv,NULL,&m_vShadowMap.m_vCameraLight.GetProjectionMatrix()); LPD3DXEFFECT pShader = g_cGraphicsTool.GetDefaultShader(DEFST_LINEARDEPTHOUTPUT); pShader->SetMatrix("invProj",&matInv); pShader->SetTechnique("tec0"); UINT uPass = 0; pShader->Begin(&uPass,0); pShader->BeginPass(0); hr = g_pd3dDevice->BeginScene(); KGLOG_COM_PROCESS_ERROR(hr); g_cGraphicsTool.DrawScreenRect(&A,&C,0,0xFFFFFFFF,m_vShadowMap.m_lpSMShadowMap); hr = g_pd3dDevice->EndScene(); KGLOG_COM_PROCESS_ERROR(hr); pShader->EndPass(); pShader->End(); } else { g_cGraphicsTool.DrawScreenRect(&A,&C,0,0xFFFFFFFF,m_vShadowMap.m_lpSMColorMap); } } hr = S_OK; Exit0: pTool->EndNewRenderTarget(); pTool->ExportRenderData(pDestSurface,FALSE); return hr; }
//************************************************************************************************************* void DrawShadowVolume(const ShadowCaster& caster) { extrude->SetMatrix("matWorld", &caster.world); extrude->CommitChanges(); device->SetVertexDeclaration(shadowdecl); device->SetStreamSource(0, caster.vertices, 0, sizeof(D3DXVECTOR4)); device->SetIndices(caster.indices); device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, caster.numvertices, 0, caster.numfaces); }
//************************************************************************************************************* void DrawScene(LPD3DXEFFECT effect) { D3DXMATRIX world; D3DXMATRIX inv; D3DXVECTOR4 uv(3, 3, 0, 0); D3DXMatrixScaling(&world, 5, 0.1f, 5); D3DXMatrixInverse(&inv, NULL, &world); effect->SetMatrix("matWorld", &world); effect->SetMatrix("matWorldInv", &inv); effect->SetVector("uv", &uv); effect->Begin(0, 0); effect->BeginPass(0); { device->SetTexture(0, texture2); shadowreceiver->DrawSubset(0); if( !drawsilhouette ) { device->SetTexture(0, texture1); uv.x = uv.y = 1; effect->SetVector("uv", &uv); for( int i = 0; i < NUM_OBJECTS; ++i ) { D3DXMatrixInverse(&inv, NULL, &objects[i].world); effect->SetMatrix("matWorld", &objects[i].world); effect->SetMatrix("matWorldInv", &inv); effect->CommitChanges(); objects[i].object->DrawSubset(0); } } } effect->EndPass(); effect->End(); }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { D3DXMATRIX vp, inv; D3DXVECTOR3 axis(0, 1, 0); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); device->SetTexture(0, tex); device->SetTexture(1, normalmap); D3DXMatrixMultiply(&vp, &view, &proj); D3DXMatrixInverse(&inv, NULL, &view); effect->SetVector("eyePos", (D3DXVECTOR4*)inv.m[3]); D3DXMatrixRotationAxis(&world, &axis, timeGetTime() / 1000.0f); D3DXMatrixInverse(&inv, NULL, &world); effect->SetMatrix("matWorld", &world); effect->SetMatrix("matWorldInv", &inv); effect->SetMatrix("matViewProj", &vp); if( SUCCEEDED(device->BeginScene()) ) { effect->Begin(NULL, 0); effect->BeginPass(0); { mesh->DrawSubset(0); } effect->EndPass(); effect->End(); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }
void RenderFrameDX9(void) { LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); device->BeginScene(); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x0, 1.0f, 0); Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 world_matrix = g_Control.GetObjectMatrix(); Matrix4x4 wv_matrix = world_matrix * view_matrix; Matrix4x4 wvp_matrix = wv_matrix * g_proj_matrix; DrawImage(g_pTexture, &g_ImageInfo); D3DXHANDLE shader = g_pRefractionFX->GetTechniqueByName("Refraction"); D3DXHANDLE texture = g_pRefractionFX->GetParameterByName(NULL, "BackgroundImage"); D3DXHANDLE wv_matrix_h = g_pRefractionFX->GetParameterByName(NULL, "wv_matrix"); D3DXHANDLE wvp_matrix_h = g_pRefractionFX->GetParameterByName(NULL, "wvp_matrix"); D3DXHANDLE objectcolor_h = g_pRefractionFX->GetParameterByName(NULL, "object_color"); g_pRefractionFX->SetTechnique(shader); g_pRefractionFX->SetTexture(texture, g_pTexture); g_pRefractionFX->SetMatrix(wv_matrix_h, (D3DXMATRIX *)&wv_matrix); g_pRefractionFX->SetMatrix(wvp_matrix_h, (D3DXMATRIX *)&wvp_matrix); g_pRefractionFX->SetVector(objectcolor_h, (D3DXVECTOR4 *)&g_vObjectColor); g_pRefractionFX->Begin(NULL, 0); g_pRefractionFX->BeginPass(0); g_Model_DX9.Render(0); g_pRefractionFX->EndPass(); g_pRefractionFX->End(); device->EndScene(); device->Present( NULL, NULL, NULL, NULL ); }
void Manipulator::RenderSphere(LPD3DXEFFECT effect, const Matrix& projection, const Matrix& view, const D3DXVECTOR3& color, const Transform& world) { D3DXMATRIX wvp = world.GetMatrix() * view.GetMatrix() * projection.GetMatrix(); effect->SetMatrix(DxConstant::WordViewProjection, &wvp); effect->SetFloatArray(DxConstant::VertexColor, &color.x, 3); UINT nPasses = 0; effect->Begin(&nPasses, 0); for(UINT iPass = 0; iPass < nPasses; iPass++) { effect->BeginPass(iPass); m_sphere->DrawSubset(0); effect->EndPass(); } effect->End(); }
static void RenderParticle(void) { LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); int w, h; GutGetWindowSize(w, h); Matrix4x4 wvp_matrix = g_Control.GetObjectMatrix() * g_Control.GetViewMatrix() * g_proj_matrix; Vector4 ScreenSize( (float)w, (float)h, 0.0f, 0.0f); float fTanW = FastMath::Tan( FastMath::DegToRad(g_fFOV) ); device->SetRenderTarget(0, g_pMainFramebuffer); device->SetDepthStencilSurface(g_pMainDepthbuffer); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x0, 1.0f, 0); D3DXHANDLE shader = g_pParticleRenderFX->GetTechniqueByName("RenderParticle"); D3DXHANDLE positiontex_var = g_pParticleRenderFX->GetParameterByName(NULL, "PositionTex"); D3DXHANDLE velocitytex_var = g_pParticleRenderFX->GetParameterByName(NULL, "VelocityTex"); D3DXHANDLE wvp_matrix_var = g_pParticleRenderFX->GetParameterByName(NULL, "wvp_matrix"); D3DXHANDLE tan_fov_var = g_pParticleRenderFX->GetParameterByName(NULL, "fTanW"); D3DXHANDLE screensize_var = g_pParticleRenderFX->GetParameterByName(NULL, "ScreenSize"); D3DXHANDLE diffusetex_var = g_pParticleRenderFX->GetParameterByName(NULL, "DiffuseTex"); g_pParticleRenderFX->SetTechnique(shader); g_pParticleRenderFX->SetTexture(positiontex_var, g_pTextures[TEX_POSITION1]); g_pParticleRenderFX->SetTexture(velocitytex_var, g_pTextures[TEX_VELOCITY1]); g_pParticleRenderFX->SetMatrix(wvp_matrix_var, (D3DXMATRIX*)&wvp_matrix); g_pParticleRenderFX->SetVector(screensize_var, (D3DXVECTOR4*)&ScreenSize); g_pParticleRenderFX->SetFloat(tan_fov_var, fTanW); g_pParticleRenderFX->SetTexture(diffusetex_var, g_pParticleTexture); int num_processed_particles = (g_num_particles / g_texture_width) * g_texture_width; if ( num_processed_particles ) { device->SetFVF(D3DFVF_TEX1); g_pParticleRenderFX->Begin(NULL,0); g_pParticleRenderFX->BeginPass(0); //device->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); device->DrawPrimitiveUP(D3DPT_POINTLIST, num_processed_particles, g_pParticleArray, sizeof(sParticle)); //device->SetRenderState(D3DRS_POINTSPRITEENABLE, FALSE); g_pParticleRenderFX->EndPass(); g_pParticleRenderFX->End(); } }
void CollisionMesh::DrawMesh(const Matrix& projection, const Matrix& view, const D3DXVECTOR3& color) { if(m_draw && m_geometry) { LPD3DXEFFECT shader = m_geometry->GetShader(); D3DXMATRIX wvp = m_world.GetMatrix() * view.GetMatrix() * projection.GetMatrix(); shader->SetMatrix(DxConstant::WordViewProjection, &wvp); shader->SetTechnique(DxConstant::DefaultTechnique); shader->SetFloatArray(DxConstant::VertexColor, &(color.x), 3); UINT nPasses = 0; shader->Begin(&nPasses, 0); for(UINT pass = 0; pass < nPasses; ++pass) { shader->BeginPass(pass); m_geometry->GetMesh()->DrawSubset(0); shader->EndPass(); } shader->End(); } }
void DXDirectionalLight::DrawShadowMap(LPDIRECT3DDEVICE9 device, LPD3DXEFFECT effect, void (*drawcallback)(LPD3DXEFFECT)) { if( !shadowmap || !needsredraw ) return; LPDIRECT3DSURFACE9 surface = NULL; D3DXMATRIX vp; GetViewProjMatrix(vp, D3DXVECTOR3(0, 0, 0)); effect->SetVector("lightPos", &direction); effect->SetMatrix("matViewProj", &vp); shadowmap->GetSurfaceLevel(0, &surface); device->SetRenderTarget(0, surface); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff000000, 1.0f, 0); drawcallback(effect); surface->Release(); if( shadowtype == Static ) needsredraw = false; }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { static float time = 0; D3DXMATRIX inv; D3DXVECTOR3 axis(0, 1, 0); D3DXVECTOR4 texelsize(1.0f / (float)screenwidth, 1.0f / (float)screenheight, 0, 1); LPDIRECT3DSURFACE9 oldtarget = NULL; time += elapsedtime; D3DXMatrixRotationAxis(&inv, &axis, time); D3DXMatrixScaling(&world, 0.3f, 0.3f, 0.3f); //D3DXMatrixScaling(&world, 0.6f, 0.6f, 0.6f); D3DXMatrixMultiply(&world, &world, &inv); D3DXMatrixInverse(&inv, NULL, &world); device->SetTexture(0, texture); device->SetTexture(1, intensity); effect->SetMatrix("matWorld", &world); effect->SetMatrix("matWorldInv", &inv); effect->SetMatrix("matView", &view); effect->SetMatrix("matProj", &proj); if( useedgedetect ) { device->GetRenderTarget(0, &oldtarget); device->SetRenderTarget(0, colorsurface); device->SetRenderTarget(1, normalsurface); } device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0xff6694ed, 1.0f, 0); if( SUCCEEDED(device->BeginScene()) ) { // draw scene + normals/depth effect->SetTechnique("celshading"); effect->Begin(NULL, 0); effect->BeginPass(0); { mesh->DrawSubset(0); } effect->EndPass(); effect->End(); if( useedgedetect ) { // edge detection device->SetVertexDeclaration(vertexdecl); device->SetRenderTarget(0, edgesurface); device->SetRenderTarget(1, NULL); device->SetTexture(0, normaltarget); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); effect->SetTechnique("edgedetect"); effect->SetVector("texelSize", &texelsize); effect->Begin(NULL, 0); effect->BeginPass(0); { device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, vertices, sizeof(D3DXVECTOR4) + sizeof(D3DXVECTOR2)); } effect->EndPass(); effect->End(); // put together device->SetRenderTarget(0, oldtarget); device->SetTexture(0, colortarget); device->SetTexture(2, edgetarget); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); oldtarget->Release(); effect->SetTechnique("final"); effect->Begin(NULL, 0); effect->BeginPass(0); { device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, vertices, sizeof(D3DXVECTOR4) + sizeof(D3DXVECTOR2)); } effect->EndPass(); effect->End(); } else { D3DXMATRIX offproj; // use the stencil buffer device->SetRenderState(D3DRS_COLORWRITEENABLE, 0); device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); device->SetRenderState(D3DRS_STENCILENABLE, TRUE); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); device->SetRenderState(D3DRS_STENCILREF, 1); device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); device->SetTransform(D3DTS_WORLD, &world); device->SetTransform(D3DTS_VIEW, &view); float thickness = 3.5f; // render object 4 times with offseted frustum for( float i = -thickness; i < thickness + 1; i += 2 * thickness ) { for( float j = -thickness; j < thickness + 1; j += 2 * thickness ) { D3DXMatrixTranslation(&offproj, i / (float)screenwidth, j / (float)screenheight, 0); D3DXMatrixMultiply(&offproj, &proj, &offproj); device->SetTransform(D3DTS_PROJECTION, &offproj); mesh->DrawSubset(0); } } // erase area in the center device->SetRenderState(D3DRS_STENCILREF, 0); device->SetTransform(D3DTS_PROJECTION, &proj); mesh->DrawSubset(0); // now render outlines device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_ALPHA); device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_NOTEQUAL); device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP); device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP); device->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1); device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_CONSTANT); device->SetTextureStageState(0, D3DTSS_CONSTANT, 0); { device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, vertices, sizeof(D3DXVECTOR4) + sizeof(D3DXVECTOR2)); } device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); device->SetRenderState(D3DRS_ZENABLE, TRUE); device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); device->SetRenderState(D3DRS_STENCILENABLE, FALSE); } device->SetTexture(2, NULL); // render text device->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); device->SetTexture(0, text); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, textvertices, 6 * sizeof(float)); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_ZENABLE, TRUE); device->SetTexture(0, NULL); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { D3DXMATRIX world, view, proj; D3DXMATRIX skyworld, viewproj; D3DXVECTOR3 eye(0, 0, -5); D3DXVECTOR3 look(0, 0, 0); D3DXVECTOR3 up(0, 1, 0); D3DXVECTOR2 orient = cameraangle.smooth(alpha); D3DXMatrixRotationYawPitchRoll(&view, orient.x, orient.y, 0); D3DXVec3TransformCoord(&eye, &eye, &view); D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, (float)screenwidth / (float)screenheight, 0.1f, 50); D3DXMatrixLookAtLH(&view, &eye, &look, &up); D3DXMatrixMultiply(&viewproj, &view, &proj); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); device->SetTransform(D3DTS_VIEW, &view); device->SetTransform(D3DTS_PROJECTION, &proj); for( int i = 0; i < NUM_DWARFS; ++i ) dwarfs[i].Update(elapsedtime, &dwarfmatrices[i]); effect->SetMatrix("matViewProj", &viewproj); if( SUCCEEDED(device->BeginScene()) ) { // render sky device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); D3DXMatrixScaling(&skyworld, 20, 20, 20); skyeffect->SetMatrix("matWorld", &skyworld); D3DXMatrixIdentity(&skyworld); skyeffect->SetMatrix("matWorldSky", &skyworld); skyeffect->SetMatrix("matViewProj", &viewproj); skyeffect->SetVector("eyePos", (D3DXVECTOR4*)&eye); skyeffect->Begin(0, 0); skyeffect->BeginPass(0); { device->SetTexture(0, skytex); skymesh->DrawSubset(0); } skyeffect->EndPass(); skyeffect->End(); device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); // render ground D3DXMatrixScaling(&world, 5, 0.1f, 5); world._42 = -0.05f; device->SetTransform(D3DTS_WORLD, &world); device->SetTexture(0, texture2); mesh->DrawSubset(0); // dwarfs for( int i = 0; i < NUM_DWARFS; ++i ) dwarfs[i].Draw(); // fire D3DXMatrixTranslation(&world, 0, 0.25f, 0); device->SetTransform(D3DTS_WORLD, &world); system1.Draw(world, view); // render text device->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); device->SetTexture(0, text); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, textvertices, 6 * sizeof(float)); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_ZENABLE, TRUE); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }
// draw 3D objects and so on void RenderScene() { ///////////////////////// // 1. draw the scene into the render target ///////////////////////// // current hardware backbuffer LPDIRECT3DSURFACE9 pHWBackBuffer = NULL; gpD3DDevice->GetRenderTarget(0, &pHWBackBuffer); // draw onto the render target LPDIRECT3DSURFACE9 pSceneSurface = NULL; if (SUCCEEDED(gpSceneRenderTarget->GetSurfaceLevel(0, &pSceneSurface))) { gpD3DDevice->SetRenderTarget(0, pSceneSurface); pSceneSurface->Release(); pSceneSurface = NULL; } // clear what's drawn in the last frame gpD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0xFF000000, 1.0f, 0); // make the view matrix D3DXMATRIXA16 matView; D3DXVECTOR3 vEyePt(gWorldCameraPosition.x, gWorldCameraPosition.y, gWorldCameraPosition.z); D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f); D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f); D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec); // projection matrix D3DXMATRIXA16 matProjection; D3DXMatrixPerspectiveFovLH(&matProjection, FOV, ASPECT_RATIO, NEAR_PLANE, FAR_PLANE); // for each frame, we roate 0.4 degree gRotationY += 0.4f * PI / 180.0f; if (gRotationY > 2 * PI) { gRotationY -= 2 * PI; } // world matrix D3DXMATRIXA16 matWorld; D3DXMatrixRotationY(&matWorld, gRotationY); // concatenate world/view/projection matrices D3DXMATRIXA16 matWorldView; D3DXMATRIXA16 matWorldViewProjection; D3DXMatrixMultiply(&matWorldView, &matWorld, &matView); D3DXMatrixMultiply(&matWorldViewProjection, &matWorldView, &matProjection); // set shader global variables gpEnvironmentMappingShader->SetMatrix("gWorldMatrix", &matWorld); gpEnvironmentMappingShader->SetMatrix("gWorldViewProjectionMatrix", &matWorldViewProjection); gpEnvironmentMappingShader->SetVector("gWorldLightPosition", &gWorldLightPosition); gpEnvironmentMappingShader->SetVector("gWorldCameraPosition", &gWorldCameraPosition); gpEnvironmentMappingShader->SetVector("gLightColor", &gLightColor); gpEnvironmentMappingShader->SetTexture("DiffuseMap_Tex", gpStoneDM); gpEnvironmentMappingShader->SetTexture("SpecularMap_Tex", gpStoneSM); gpEnvironmentMappingShader->SetTexture("NormalMap_Tex", gpStoneNM); gpEnvironmentMappingShader->SetTexture("EnvironmentMap_Tex", gpSnowENV); // start a shader UINT numPasses = 0; gpEnvironmentMappingShader->Begin(&numPasses, NULL); { for (UINT i = 0; i < numPasses; ++i) { gpEnvironmentMappingShader->BeginPass(i); { // draw a sphere gpTeapot->DrawSubset(0); } gpEnvironmentMappingShader->EndPass(); } } gpEnvironmentMappingShader->End(); ///////////////////////// // 2. apply post-processing ///////////////////////// // use hardware backbuffer gpD3DDevice->SetRenderTarget(0, pHWBackBuffer); pHWBackBuffer->Release(); pHWBackBuffer = NULL; // post process effect to use LPD3DXEFFECT effectToUse = gpNoEffect; if (gPostProcessIndex == 1) { effectToUse = gpGrayScale; } else if (gPostProcessIndex == 2) { effectToUse = gpSepia; } effectToUse->SetTexture("SceneTexture_Tex", gpSceneRenderTarget); effectToUse->Begin(&numPasses, NULL); { for (UINT i = 0; i < numPasses; ++i) { effectToUse->BeginPass(i); { // draw a fullscreen quad gpD3DDevice->SetStreamSource(0, gpFullscreenQuadVB, 0, sizeof(float)* 5); gpD3DDevice->SetIndices(gpFullscreenQuadIB); gpD3DDevice->SetVertexDeclaration(gpFullscreenQuadDecl); gpD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 6, 0, 2); } effectToUse->EndPass(); } } effectToUse->End(); }
//----------------------------------------------------------------------------- // Name: Render // Desc: //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::Render() { HRESULT hr; if(FAILED(hr = m_pd3dDevice->BeginScene())) return hr; // Draw Environment FLOAT fAspectRatio = (FLOAT)m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height; D3DXMatrixPerspectiveFovRH(&m_matProjection, D3DXToRadian(60.0f), fAspectRatio, 0.1f, 2000.0f); m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &m_matProjection); if(m_bDrawEnvironment) { D3DXMATRIX mat(m_matView); mat._41 = mat._42 = mat._43 = 0.0f; m_pd3dDevice->SetTransform(D3DTS_VIEW, &mat); m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); m_pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); m_Environment.Draw(); m_pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL); m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE); } else { m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0); } m_pd3dDevice->SetTransform(D3DTS_VIEW, &m_matView); // Draw water if(m_bDrawWater) { // Setup matrices if(m_pEffect->IsParameterUsed("mENV")) { D3DXMATRIX matP(m_matPosition); matP._41 = matP._42 = matP._43 = 0.0f; D3DXMATRIX mat; D3DXMatrixScaling(&mat, 1.0f, 1.0f, -1.0f); D3DXMatrixMultiply(&mat, &matP, &mat); // matCube m_pEffect->SetMatrix("mENV", &mat); } // Draw water UINT uPasses; m_pEffect->Begin(&uPasses, 0); for(UINT uPass = 0; uPass < uPasses; uPass++) { m_pEffect->Pass(uPass); m_Water.DrawSurface(); } m_pEffect->End(); } // Show info m_pFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats ); m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats ); TCHAR szText[100]; wsprintf( szText, _T("Using Technique %d"), m_iTechnique ); m_pFontSmall->DrawText( 2, 40, D3DCOLOR_ARGB(255,255,100,100), szText ); if( m_bShowHelp ) { m_pFontSmall->DrawText( 2, 60, D3DCOLOR_ARGB(255,100,100,200), _T("Keyboard controls:") ); m_pFontSmall->DrawText( 20, 80, D3DCOLOR_ARGB(255,100,100,200), _T("Add Drop\n") _T("Next Technique\n") _T("Next Tech. (no validate)\n") _T("Prev Technique\n") _T("Prev Tech. (no validate)\n") _T("Move\nTurn\nPitch\nSlide\n") _T("Help\nChange device\nExit") ); m_pFontSmall->DrawText( 210, 80, D3DCOLOR_ARGB(255,100,100,200), _T("D\n") _T("PageDn\nShift-PageDn\n") _T("PageUp\nShift-PageUp\n") _T("W,S\nE,Q\nA,Z\nArrow keys\n") _T("F1\nF2\nEsc") ); } else { m_pFontSmall->DrawText( 2, 60, D3DCOLOR_ARGB(255,100,100,200), _T("Press F1 for help") ); } if(FAILED(hr = m_pd3dDevice->EndScene())) return hr; return S_OK; }
//----------------------------------------------------------------------------- // Name: RestoreDeviceObjects // Desc: //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::RestoreDeviceObjects() { HRESULT hr; // Restore the font m_pFont->RestoreDeviceObjects(); m_pFontSmall->RestoreDeviceObjects(); // Create light D3DLIGHT8 light; ZeroMemory(&light, sizeof(light)); light.Type = D3DLIGHT_DIRECTIONAL; light.Diffuse.r = m_colorLight.r; light.Diffuse.g = m_colorLight.g; light.Diffuse.b = m_colorLight.b; light.Diffuse.a = m_colorLight.a; light.Specular.r = 1.0f; light.Specular.g = 1.0f; light.Specular.b = 1.0f; light.Specular.a = 0.0f; light.Direction.x = m_vecLight.x; light.Direction.y = m_vecLight.y; light.Direction.z = m_vecLight.z; m_pd3dDevice->SetLight(0, &light); m_pd3dDevice->LightEnable(0, TRUE); // Create material D3DMATERIAL8 material; ZeroMemory(&material, sizeof(material)); material.Diffuse.a = 1.0f; material.Specular.r = 0.5f; material.Specular.g = 0.5f; material.Specular.b = 0.5f; material.Power = 20.0f; m_pd3dDevice->SetMaterial(&material); // Setup render states m_pd3dDevice->SetVertexShader(D3DFVF_XYZ); m_pd3dDevice->SetTransform(D3DTS_VIEW, &m_matView); m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_matIdentity); m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); m_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, FALSE); m_pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL); m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); m_pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT); m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_CURRENT); m_pd3dDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR); m_pd3dDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); m_pd3dDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_POINT); m_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0); m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); m_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); m_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); m_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT); m_pd3dDevice->SetTextureStageState(1, D3DTSS_MINFILTER, D3DTEXF_LINEAR); m_pd3dDevice->SetTextureStageState(1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); m_pd3dDevice->SetTextureStageState(1, D3DTSS_MIPFILTER, D3DTEXF_POINT); m_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0); // Create caustic texture D3DDISPLAYMODE mode; m_pd3dDevice->GetDisplayMode(&mode); if(FAILED(hr = D3DXCreateTexture(m_pd3dDevice, WATER_CAUSTICS_SIZE, WATER_CAUSTICS_SIZE, 1, D3DUSAGE_RENDERTARGET, mode.Format, D3DPOOL_DEFAULT, &m_pCausticTex)) && FAILED(hr = D3DXCreateTexture(m_pd3dDevice, WATER_CAUSTICS_SIZE, WATER_CAUSTICS_SIZE, 1, 0, mode.Format, D3DPOOL_DEFAULT, &m_pCausticTex))) { return hr; } D3DSURFACE_DESC desc; m_pCausticTex->GetSurfaceLevel(0, &m_pCausticSurf); m_pCausticSurf->GetDesc(&desc); if(FAILED(hr = D3DXCreateRenderToSurface(m_pd3dDevice, desc.Width, desc.Height, desc.Format, FALSE, D3DFMT_UNKNOWN, &m_pRenderToSurface))) { return hr; } // Shader TCHAR sz[512]; DXUtil_FindMediaFile(sz, _T("water.sha")); if(FAILED(hr = D3DXCreateEffectFromFile(m_pd3dDevice, sz, &m_pEffect, NULL))) return hr; m_pEffect->SetMatrix("mID", &m_matIdentity); m_pEffect->SetMatrix("mENV", &m_matIdentity); m_pEffect->SetTexture("tFLR", m_pFloorTex); m_pEffect->SetTexture("tCAU", m_pCausticTex); m_pEffect->SetTexture("tENV", m_pSkyCubeTex); if(FAILED(hr = GetNextTechnique(0, FALSE))) return hr; // Set surfaces if(FAILED(hr = m_Environment.SetSurfaces( m_pSkyTex[D3DCUBEMAP_FACE_NEGATIVE_X], m_pSkyTex[D3DCUBEMAP_FACE_POSITIVE_X], m_pSkyTex[D3DCUBEMAP_FACE_NEGATIVE_Y], m_pSkyTex[D3DCUBEMAP_FACE_POSITIVE_Y], m_pSkyTex[D3DCUBEMAP_FACE_POSITIVE_Z], m_pSkyTex[D3DCUBEMAP_FACE_NEGATIVE_Z]))) { return hr; } // OnResetDevice if(FAILED(hr = m_Water.OnResetDevice())) return hr; if(FAILED(hr = m_Environment.OnResetDevice())) return hr; return S_OK; }
void RenderWithIrregularPCF( const D3DXMATRIX& viewproj, const D3DXVECTOR3& eye, const D3DXMATRIX& lightview, const D3DXMATRIX& lightproj, const D3DXVECTOR4& lightpos, const D3DXVECTOR4& clipplanes, const D3DXVECTOR4& texelsize) { LPDIRECT3DSURFACE9 oldsurface = NULL; LPDIRECT3DSURFACE9 shadowsurface = NULL; D3DXVECTOR4 noisesize(16.0f, 16.0f, 0, 1); // STEP 1: render shadow map shadowmap->GetSurfaceLevel(0, &shadowsurface); device->GetRenderTarget(0, &oldsurface); device->SetRenderTarget(0, shadowsurface); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); //device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); shadowsurface->Release(); pcfirreg->SetTechnique("shadowmap"); pcfirreg->SetMatrix("lightView", &lightview); pcfirreg->SetMatrix("lightProj", &lightproj); pcfirreg->SetVector("clipPlanes", &clipplanes); pcfirreg->Begin(NULL, 0); pcfirreg->BeginPass(0); { RenderScene(pcfirreg, 1); // caster RenderScene(pcfirreg, 3); // caster & receiver } pcfirreg->EndPass(); pcfirreg->End(); // STEP 2: render scene //device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); device->SetRenderTarget(0, oldsurface); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); device->SetRenderState(D3DRS_SRGBWRITEENABLE, TRUE); oldsurface->Release(); device->SetTexture(1, shadowmap); device->SetTexture(2, noise); pcfirreg->SetTechnique("irregular_light"); //pcfirreg->SetTechnique("irregular_screen"); pcfirreg->SetMatrix("matViewProj", &viewproj); pcfirreg->SetMatrix("lightView", &lightview); pcfirreg->SetMatrix("lightProj", &lightproj); pcfirreg->SetVector("clipPlanes", &clipplanes); pcfirreg->SetVector("lightPos", &lightpos); pcfirreg->SetVector("eyePos", (D3DXVECTOR4*)&eye); pcfirreg->SetVector("texelSize", &texelsize); pcfirreg->SetVector("noiseSize", &noisesize); pcfirreg->Begin(NULL, 0); pcfirreg->BeginPass(0); { RenderScene(pcfirreg, 2); // receiver RenderScene(pcfirreg, 3); // caster & receiver } pcfirreg->EndPass(); pcfirreg->End(); device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE); }
void sc_lights_frmSun(SC_SCREEN* screen) { if(screen.views.sun != NULL) { if( screen.settings.lights.sunPos.x == 0 && screen.settings.lights.sunPos.y == 0 && screen.settings.lights.sunPos.z == 0 ) //global sun { VECTOR distantSunPos; vec_for_angle(distantSunPos, sun_angle); vec_scale(distantSunPos, 9999999); screen.materials.sun.skill1 = floatv(distantSunPos.x); screen.materials.sun.skill2 = floatv(distantSunPos.y); screen.materials.sun.skill3 = floatv(distantSunPos.z); } else //local sun { screen.materials.sun.skill1 = floatv(screen.settings.lights.sunPos.x); screen.materials.sun.skill2 = floatv(screen.settings.lights.sunPos.y); screen.materials.sun.skill3 = floatv(screen.settings.lights.sunPos.z); } screen.materials.sun.skill4 = floatv(0); //local sun lightrange (not used) //sun color screen.materials.sun.skill5 = floatv(sun_color.red/255); screen.materials.sun.skill6 = floatv(sun_color.green/255); screen.materials.sun.skill7 = floatv(sun_color.blue/255); screen.materials.sun.skill8 = floatv(screen.views.main.clip_far); #ifndef SC_A7 // PSSM main loop if(screen.settings.lights.sunPssmSplits>0 && screen.settings.lights.sunShadows == 1) { // update the views _after_ the camera was updated! // proc_mode = PROC_LATE; // set up the split distances and the shadow view sc_lights_pssm_split(screen.views.main, screen.settings.lights.sunPssmSplits, screen.settings.lights.sunPssmSplitWeight, screen.settings.lights.sunShadowRange); //pssm_split(screen.views.main, screen.settings.lights.sunPssmSplits, screen.settings.lights.sunPssmSplitWeight); //pssm_viewcpy(screen.views.main, screen.views.sun); // set up the split view transformation matrices D3DXMATRIX matSplit[4]; int i=0; for(i=0; i<screen.settings.lights.sunPssmSplits; i++) { // look from the sun onto the scene screen.views.sunShadowDepth[i]->pan = 180 + sun_angle.pan; screen.views.sunShadowDepth[i]->tilt = -sun_angle.tilt; vec_set(screen.views.sunShadowDepth[i]->x,sun_pos); // calculate the split view clipping borders and transformation matrix view_to_split(screen.views.main, pssm_splitdist[i],pssm_splitdist[i+1], screen.views.sunShadowDepth[i], &matSplit[i]); LPD3DXEFFECT fx = screen.views.sunShadowDepth[i]->material->d3deffect; if(fx) fx->SetMatrix("matSplitViewProj",&matSplit[i]); // create a texture matrix from the split view proj matrix D3DXMatrixMultiply(&matSplit[i],&matSplit[i],pssm_texscale(screen.settings.lights.sunShadowResolution)); #ifdef DEBUG_PSSM DEBUG_BMAP(screen.views.sunShadowDepth[i]->bmap,300 + i*220,0.2); var pssm_fps = 16/time_frame; DEBUG_VAR(pssm_fps,200); DEBUG_VAR(pssm_splitdist[i+1],220 + i*20); #endif //set depthmapshader maxDepth screen.views.sunShadowDepth[i].material.skill4 = floatv(screen.views.sunShadowDepth[i].clip_far); } //put matrices to world space //mat_multiply(matSplit[0], matViewInv); // use a DX function to copy the 4 texture matrices to the shadow shader LPD3DXEFFECT fx = screen.views.sun->material->d3deffect; if(fx) { fx->SetMatrixArray("matTex",matSplit,screen.settings.lights.sunPssmSplits); if(screen.views.sunShadowDepth[0] != NULL) fx->SetTexture("shadowTex1",screen.renderTargets.sunShadowDepth[0].d3dtex); if(screen.views.sunShadowDepth[1] != NULL) fx->SetTexture("shadowTex2",screen.renderTargets.sunShadowDepth[1].d3dtex); if(screen.views.sunShadowDepth[2] != NULL) fx->SetTexture("shadowTex3",screen.renderTargets.sunShadowDepth[2].d3dtex); if(screen.views.sunShadowDepth[3] != NULL) fx->SetTexture("shadowTex4",screen.renderTargets.sunShadowDepth[3].d3dtex); //fx->SetFloat("shadowBias", screen.settings.lights.sunShadowBias); //fx->SetInt("shadowmapSize", (screen.settings.lights.sunShadowResolution) ); } //same for shadowEdge acceleration LPD3DXEFFECT fx = screen.views.sunEdge->material->d3deffect; if(fx) { fx->SetMatrixArray("matTex",matSplit,screen.settings.lights.sunPssmSplits); if(screen.views.sunShadowDepth[0] != NULL) fx->SetTexture("shadowTex1",screen.renderTargets.sunShadowDepth[0].d3dtex); if(screen.views.sunShadowDepth[1] != NULL) fx->SetTexture("shadowTex2",screen.renderTargets.sunShadowDepth[1].d3dtex); if(screen.views.sunShadowDepth[2] != NULL) fx->SetTexture("shadowTex3",screen.renderTargets.sunShadowDepth[2].d3dtex); if(screen.views.sunShadowDepth[3] != NULL) fx->SetTexture("shadowTex4",screen.renderTargets.sunShadowDepth[3].d3dtex); //fx->SetFloat("shadowBias", screen.settings.lights.sunShadowBias); //fx->SetInt("shadowmapSize", (screen.settings.lights.sunShadowResolution) ); } //and for the actual shadow view LPD3DXEFFECT fx = screen.views.sunShadow->material->d3deffect; if(fx) { fx->SetMatrixArray("matTex",matSplit,screen.settings.lights.sunPssmSplits); if(screen.views.sunShadowDepth[0] != NULL) fx->SetTexture("shadowTex1",screen.renderTargets.sunShadowDepth[0].d3dtex); if(screen.views.sunShadowDepth[1] != NULL) fx->SetTexture("shadowTex2",screen.renderTargets.sunShadowDepth[1].d3dtex); if(screen.views.sunShadowDepth[2] != NULL) fx->SetTexture("shadowTex3",screen.renderTargets.sunShadowDepth[2].d3dtex); if(screen.views.sunShadowDepth[3] != NULL) fx->SetTexture("shadowTex4",screen.renderTargets.sunShadowDepth[3].d3dtex); //fx->SetFloat("shadowBias", screen.settings.lights.sunShadowBias); //fx->SetInt("shadowmapSize", (screen.settings.lights.sunShadowResolution) ); } screen.views.sun.material.skill4 = floatv(screen.views.sunShadowDepth[0].clip_far); #ifdef DEBUG_PSSM DEBUG_BMAP(screen.views.sun->bmap,20,0.2); #endif } #endif } }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { LPDIRECT3DSURFACE9 oldtarget = NULL; D3DXMATRIX vp, inv, tmp1, tmp2; D3DXVECTOR3 axis(0, 1, 0); D3DXVECTOR3 eye(0, 0, -5); D3DXVECTOR3 look(0, 0, 0); D3DXVECTOR3 up(0, 1, 0); D3DXVECTOR2 cangle = cameraangle.smooth(alpha); D3DXVECTOR2 oangle = objectangle.smooth(alpha); float expo = exposure.smooth(alpha); D3DXMatrixRotationYawPitchRoll(&world, cangle.x, cangle.y, 0); D3DXVec3TransformCoord(&eye, &eye, &world); D3DXMatrixLookAtLH(&view, &eye, &look, &up); D3DXMatrixMultiply(&vp, &view, &proj); D3DXMatrixInverse(&inv, NULL, &view); memcpy(&eye, inv.m[3], 3 * sizeof(float)); if( mesh == mesh1 ) { // skullocc D3DXMatrixScaling(&world, 0.4f, 0.4f, 0.4f); world._42 = -1.5f; } else if( mesh == mesh2 ) { // knot D3DXMatrixScaling(&world, 0.8f, 0.8f, 0.8f); } else { // teapot D3DXMatrixScaling(&world, 1.5f, 1.5f, 1.5f); } D3DXMatrixRotationYawPitchRoll(&tmp1, oangle.x, oangle.y, 0); D3DXMatrixMultiply(&world, &world, &tmp1); D3DXMatrixInverse(&inv, NULL, &world); fresnel->SetVector("eyePos", (D3DXVECTOR4*)&eye); fresnel->SetMatrix("matWorld", &world); fresnel->SetMatrix("matWorldInv", &inv); fresnel->SetMatrix("matViewProj", &vp); D3DXMatrixScaling(&world, 20, 20, 20); skyeffect->SetMatrix("matWorld", &world); D3DXMatrixIdentity(&world); skyeffect->SetMatrix("matWorldSky", &world); skyeffect->SetMatrix("matViewProj", &vp); memcpy(tmpvert, quadvertices, 36 * sizeof(float)); if( SUCCEEDED(device->BeginScene()) ) { device->SetRenderState(D3DRS_SRGBWRITEENABLE, false); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); // STEP 1: render sky device->GetRenderTarget(0, &oldtarget); if( firstframe ) { device->SetRenderTarget(0, aftersurfaces[0]); device->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0); device->SetRenderTarget(0, aftersurfaces[1]); device->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0); device->SetRenderTarget(0, avglumsurfaces[4]); device->Clear(0, NULL, D3DCLEAR_TARGET, 0x11111111, 1.0f, 0); device->SetRenderTarget(0, avglumsurfaces[5]); device->Clear(0, NULL, D3DCLEAR_TARGET, 0x11111111, 1.0f, 0); firstframe = false; } device->SetRenderTarget(0, scenesurface); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetTexture(0, skytexture); skyeffect->Begin(NULL, 0); skyeffect->BeginPass(0); { skymesh->DrawSubset(0); } skyeffect->EndPass(); skyeffect->End(); device->SetRenderState(D3DRS_ZENABLE, TRUE); // STEP 2: render object device->SetTexture(0, texture); device->SetTexture(1, fresneltexture); device->SetTexture(2, skytexture); device->SetTexture(3, roughspecular); fresnel->Begin(NULL, 0); fresnel->BeginPass(0); { mesh->DrawSubset(0); } fresnel->EndPass(); fresnel->End(); device->SetVertexDeclaration(vertexdecl); // STEP 3: measure average luminance MeasureLuminance(); // STEP 4: adapt luminance to eye AdaptLuminance(elapsedtime); // STEP 5: bright pass BrightPass(); // STEP 6: downsample bright pass texture DownSample(); // STEP 7: blur downsampled textures Blur(); // STEP 8: ghost LensFlare(); // STEP 9: star Star(); // STEP 10: final combine hdreffect->SetTechnique("final"); hdreffect->SetFloat("targetluminance", targetluminance); device->SetRenderTarget(0, oldtarget); device->SetTexture(0, scenetarget); // scene device->SetTexture(1, blurtargets[0]); // blur device->SetTexture(2, blurtargets[1]); // star device->SetTexture(3, ghosttargets[0]); // ghost device->SetTexture(4, afterimages[1 - afterimagetex]); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); device->SetRenderState(D3DRS_SRGBWRITEENABLE, true); oldtarget->Release(); hdreffect->Begin(NULL, 0); hdreffect->BeginPass(0); { device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, quadvertices, sizeof(D3DXVECTOR4) + sizeof(D3DXVECTOR2)); } hdreffect->EndPass(); hdreffect->End(); if( drawhelp ) { // render text device->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); device->SetTexture(0, text); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, textvertices, 6 * sizeof(float)); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_ZENABLE, TRUE); } // clean up device->SetTexture(1, NULL); device->SetTexture(2, NULL); device->SetTexture(3, NULL); device->SetTexture(4, NULL); device->SetTexture(5, NULL); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { static float time = 0; D3DXMATRIX view, proj, vp; D3DXMATRIX world; D3DXMATRIX inv; D3DXVECTOR4 amblight(0.2f, 0.2f, 0.2f, 1); D3DXVECTOR4 intensity(0.8f, 0.8f, 0.8f, 1); D3DXVECTOR4 zero(0, 0, 0, 1); D3DXVECTOR3 lightpos(0, 0, -10); D3DXVECTOR3 eye(0, 0, -5.2f); D3DXVECTOR3 look(0, 0.5f, 0); D3DXVECTOR3 up(0, 1, 0); D3DXVECTOR3 p1, p2; D3DXVECTOR2 orient = cameraangle.smooth(alpha); D3DXVECTOR2 light = lightangle.smooth(alpha); time += elapsedtime; // setup light D3DXMatrixRotationYawPitchRoll(&view, light.x, light.y, 0); D3DXVec3TransformCoord(&lightpos, &lightpos, &view); // TODO: no need to calculate every frame for( int i = 0; i < NUM_OBJECTS; ++i ) { FindSilhouette(objects[i], (D3DXVECTOR3&)lightpos); ExtrudeSilhouette(objects[i], (D3DXVECTOR3&)lightpos); } // setup camera D3DXMatrixRotationYawPitchRoll(&view, orient.x, orient.y, 0); D3DXVec3TransformCoord(&eye, &eye, &view); D3DXMatrixLookAtLH(&view, &eye, &look, &up); D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, (float)screenwidth / (float)screenheight, 0.1f, 20); // put far plane to infinity proj._33 = 1; proj._43 = -0.1f; D3DXMatrixMultiply(&vp, &view, &proj); D3DXMatrixScaling(&world, 5, 0.1f, 5); // specular effect uniforms specular->SetMatrix("matViewProj", &vp); specular->SetVector("eyePos", (D3DXVECTOR4*)&eye); specular->SetVector("lightPos", (D3DXVECTOR4*)&lightpos); specular->SetVector("ambient", &zero); // it's a f**k-up specular->SetVector("lightColor", &intensity); // lazy to tonemap ambient->SetMatrix("matViewProj", &vp); ambient->SetVector("ambient", &amblight); if( SUCCEEDED(device->BeginScene()) ) { device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0xff6694ed, 1.0f, 0); // STEP 1: z pass ambient->SetTechnique("ambientlight"); ambient->SetMatrix("matViewProj", &vp); DrawScene(ambient); // STEP 2: draw shadow with depth fail method device->SetRenderState(D3DRS_COLORWRITEENABLE, 0); device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); device->SetRenderState(D3DRS_STENCILENABLE, TRUE); device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP); device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP); device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_INCR); device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); extrude->SetTechnique("extrude"); extrude->SetMatrix("matViewProj", &vp); extrude->Begin(0, 0); extrude->BeginPass(0); { for( int i = 0; i < NUM_OBJECTS; ++i ) DrawShadowVolume(objects[i]); device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_DECR); device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); for( int i = 0; i < NUM_OBJECTS; ++i ) DrawShadowVolume(objects[i]); } extrude->EndPass(); extrude->End(); device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_ALPHA); // STEP 3: multipass lighting device->SetRenderState(D3DRS_ZENABLE, TRUE); device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP); device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_GREATER); device->SetRenderState(D3DRS_STENCILREF, 1); DrawScene(specular); device->SetRenderState(D3DRS_STENCILENABLE, FALSE); device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); if( drawsilhouette ) { amblight = D3DXVECTOR4(1, 1, 0, 0.5f); // reuse whatever we can... extrude->SetVector("ambient", &amblight); extrude->Begin(0, 0); extrude->BeginPass(0); device->SetVertexDeclaration(shadowdecl); for( int i = 0; i < NUM_OBJECTS; ++i ) { const ShadowCaster& caster = objects[i]; D3DXVECTOR4* verts = (D3DXVECTOR4*)malloc(caster.silhouette.size() * 2 * sizeof(D3DXVECTOR4)); for( size_t j = 0; j < caster.silhouette.size(); ++j ) { const Edge& e = caster.silhouette[j]; verts[j * 2 + 0] = D3DXVECTOR4(e.v1, 1); verts[j * 2 + 1] = D3DXVECTOR4(e.v2, 1); } extrude->SetMatrix("matWorld", &caster.world); extrude->CommitChanges(); device->DrawPrimitiveUP(D3DPT_LINELIST, caster.silhouette.size(), verts, sizeof(D3DXVECTOR4)); free(verts); } extrude->EndPass(); extrude->End(); extrude->SetVector("ambient", &zero); } if( drawvolume ) { device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); amblight = D3DXVECTOR4(1, 1, 0, 0.5f); extrude->SetVector("ambient", &amblight); extrude->Begin(0, 0); extrude->BeginPass(0); for( int i = 0; i < NUM_OBJECTS; ++i ) DrawShadowVolume(objects[i]); extrude->EndPass(); extrude->End(); extrude->SetVector("ambient", &zero); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); } // render text device->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); device->SetTexture(0, text); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, textvertices, 6 * sizeof(float)); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_ZENABLE, TRUE); device->SetTexture(0, 0); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }
void RenderScene(const D3DXMATRIX& viewproj, const D3DXVECTOR3& eye, const D3DXVECTOR4& lightpos, bool refl) { D3DXMATRIX world, inv; // sky device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); device->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, 1); D3DXMatrixScaling(&world, 20, 20, 20); skyeffect->SetMatrix("matWorld", &world); D3DXMatrixIdentity(&world); skyeffect->SetMatrix("matWorldSky", &world); skyeffect->SetMatrix("matViewProj", &viewproj); skyeffect->SetVector("eyePos", (D3DXVECTOR4*)&eye); skyeffect->Begin(0, 0); skyeffect->BeginPass(0); { device->SetTexture(0, skytex); skymesh->DrawSubset(0); } skyeffect->EndPass(); skyeffect->End(); device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); if( !refl ) { // sand D3DXMatrixTranslation(&world, 0, -2, 0); ambient->SetMatrix("matViewProj", &viewproj); ambient->SetMatrix("matWorld", &world); ambient->Begin(0, 0); ambient->BeginPass(0); { device->SetTexture(0, sand); sandplane->DrawSubset(0, DXObject::Opaque); } ambient->EndPass(); ambient->End(); } else device->SetRenderState(D3DRS_CLIPPLANEENABLE, D3DCLIPPLANE0); // palm D3DXMatrixScaling(&world, 6, 6, 6); D3DXMatrixInverse(&inv, 0, &world); world._42 = -2; specular->SetMatrix("matViewProj", &viewproj); specular->SetMatrix("matWorld", &world); specular->SetMatrix("matWorldInv", &inv); specular->SetVector("eyePos", (D3DXVECTOR4*)&eye); specular->SetVector("lightPos", &lightpos); specular->SetVector("lightColor", &lightcolor); specular->Begin(0, 0); specular->BeginPass(0); { device->SetTexture(0, bark); palm->DrawSubset(0, DXObject::Opaque); device->SetTexture(0, leaves); palm->DrawSubset(1, DXObject::Opaque); } specular->EndPass(); specular->End(); device->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, 0); if( refl ) device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); }
//The user-provided entry point for a graphical Windows-based application. int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nShowCmd) { // Fill out the WNDCLASS structure WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = windowMessageCallback; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = ::GetModuleHandle(NULL); wc.hIcon = ::LoadIcon(0, IDI_APPLICATION); wc.hCursor = ::LoadCursor(0, IDC_ARROW); wc.hbrBackground = static_cast<HBRUSH>(::GetStockObject(BLACK_BRUSH)); wc.lpszMenuName = 0; wc.lpszClassName = gClassName; // Register the WNDCLASS description if (!::RegisterClass(&wc)) { return false; } // Create the window HWND hwnd = ::CreateWindow(gClassName, TEXT("client"), WS_OVERLAPPEDWINDOW, 0, 0, 512, 512, GetDesktopWindow(), 0, ::GetModuleHandle(0), 0); if (hwnd ==0) { return 0; } // Show the window ::ShowWindow(hwnd, SW_NORMAL); // Update the window ::UpdateWindow(hwnd); // IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION); if (NULL == d3d9) { return 0; } D3DPRESENT_PARAMETERS pp; memset(&pp, 0, sizeof(D3DPRESENT_PARAMETERS )); pp.BackBufferWidth = 0; pp.BackBufferHeight = 0; pp.BackBufferFormat = D3DFMT_UNKNOWN; pp.BackBufferCount = 1; pp.MultiSampleType = D3DMULTISAMPLE_NONE; pp.MultiSampleQuality = 0; pp.SwapEffect = D3DSWAPEFFECT_DISCARD; pp.hDeviceWindow = hwnd; pp.Windowed = TRUE; pp.EnableAutoDepthStencil = TRUE; pp.AutoDepthStencilFormat = D3DFMT_D24S8; pp.Flags = 0; pp.FullScreen_RefreshRateInHz = 0; pp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp, &gDevice); if (gDevice == NULL) { return 0; } D3DXCreateEffectFromFile(gDevice, TEXT("e:/ZenBin/data/shader/Position.fx"), NULL, NULL, D3DXSHADER_DEBUG, NULL, &gEffect, NULL); // D3DXCreateTextureFromFileEx(gDevice, TEXT("e:/ZenBin/data/image/AID_hportal.bmp"), D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &gTexture); int verticesNum = pow((double)2, (double)gMaxLOD) + 1; // gDevice->CreateVertexBuffer(verticesNum*verticesNum*sizeof(sVertex), 0, gVertexFVF, D3DPOOL_MANAGED, &gVB, 0); // gDevice->CreateIndexBuffer((verticesNum - 1)*(verticesNum - 1)*2*3*2, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &gIB, 0); // std::vector<sVertex> vertices; for (size_t z = 0; z != verticesNum; z++) for (size_t x = 0; x != verticesNum; x++) { sVertex v; v.x = x*3; v.z = z*3; v.y = 0; //v.u = v.x*0.01f; //v.v = v.y*0.01f; vertices.push_back(v); } { void* data; gVB->Lock(0, 0, &data, 0); memcpy(data, &vertices[0], vertices.size()*sizeof(sVertex)); gVB->Unlock(); } generateLOD(0); // Message Structure MSG msg; ::ZeroMemory(&msg, sizeof(msg)); // Loop until getting a WM_QUIT message while(true) { if (::PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { break; } ::TranslateMessage(&msg); ::DispatchMessage(&msg); } else { gDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0x000020, 1, 0); gDevice->BeginScene(); gDevice->SetFVF(gVertexFVF); gDevice->SetStreamSource(0, gVB, 0, sizeof(sVertex)); gDevice->SetIndices(gIB); gEffect->SetTexture("lay0", gTexture); UINT pPasses = 0; gEffect->SetTechnique("PP0"); gViewMatrix.column(0, Vector4(1, 0, 0, 0)); gViewMatrix.column(1, Vector4(0, 0, 1, 0)); gViewMatrix.column(2, Vector4(0, -1, 0, 150)); gViewMatrix.column(3, Vector4(0, 0, 0, 1)); gEffect->SetMatrix("gView", &gViewMatrix); D3DXMatrixPerspectiveFovLH(&gProjectionMatrix, 1.5, 1, 0.01, 1000); gEffect->SetMatrix("gProjection", &gProjectionMatrix); gEffect->Begin(&pPasses, D3DXFX_DONOTSAVESTATE); for (int i = 0; i != pPasses; ++i) { gEffect->BeginPass(i); gDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, gMaxLOD*gMaxLOD, 0, gIndices.size()/3); gEffect->EndPass(); } gEffect->End(); gDevice->EndScene(); gDevice->Present(NULL, NULL, 0, NULL); } } // if (gIB) { gIB->Release(); gIB = NULL; } // if (gVB) { gVB->Release(); gVB = NULL; } // if (gTexture) { gTexture->Release(); gTexture = NULL; } // if (gEffect) { gEffect->Release(); gEffect = NULL; } // if (gDevice) { gDevice->Release(); gDevice = NULL; } // d3d9->Release(); d3d9 = NULL; return 0; }
void Render(float alpha, float elapsedtime) { static float time = 0; LPDIRECT3DSURFACE9 backbuffer = 0; D3DXMATRIX view, proj, viewproj; D3DXMATRIX world, inv; D3DXVECTOR4 texelsize; D3DXVECTOR4 lightpos(-600, 350, 1000, 1); D3DXVECTOR4 refllight; D3DXVECTOR3 eye(0, 0, -5.0f); D3DXVECTOR3 look(0, 1.2f, 0); D3DXVECTOR3 refleye, refllook; D3DXVECTOR3 up(0, 1, 0); D3DXVECTOR2 orient = cameraangle.smooth(alpha); D3DXMatrixRotationYawPitchRoll(&view, orient.x, orient.y, 0); D3DXVec3TransformCoord(&eye, &eye, &view); eye.y += 1.2f; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 2, (float)screenwidth / (float)screenheight, 0.1f, 30); time += elapsedtime; if( SUCCEEDED(device->BeginScene()) ) { device->GetRenderTarget(0, &backbuffer); // STEP 1: render reflection texture device->SetRenderTarget(0, reflectsurf); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); D3DXPLANE plane(0, 1, 0, 1); refleye = eye - 2 * D3DXPlaneDotCoord(&plane, &eye) * (D3DXVECTOR3&)plane; refllook = look - 2 * D3DXPlaneDotCoord(&plane, &look) * (D3DXVECTOR3&)plane; refllight = lightpos - 2 * D3DXPlaneDot(&plane, &lightpos) * (D3DXVECTOR4&)plane; refllight.w = 1; D3DXMatrixLookAtLH(&view, &refleye, &refllook, &up); D3DXMatrixMultiply(&viewproj, &view, &proj); D3DXMatrixInverse(&inv, 0, &viewproj); D3DXMatrixTranspose(&inv, &inv); D3DXPlaneTransform(&plane, &plane, &inv); device->SetClipPlane(0, &plane.a); RenderScene(viewproj, refleye, refllight, true); // STEP 2: render scene (later used for refraction) D3DXMatrixLookAtLH(&view, &eye, &look, &up); D3DXMatrixMultiply(&viewproj, &view, &proj); device->SetRenderTarget(0, refractsurf); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); RenderScene(viewproj, eye, lightpos, false); // render water surface into alpha channel for masking device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA); D3DXMatrixTranslation(&world, 0, -1, 0); device->SetTransform(D3DTS_WORLD, &world); device->SetTransform(D3DTS_VIEW, &view); device->SetTransform(D3DTS_PROJECTION, &proj); waterplane->DrawSubset(0, DXObject::Opaque); device->SetRenderState(D3DRS_COLORWRITEENABLE, 0x0f); // STEP 3: light shafts quadvertices[6] = quadvertices[24] = quadvertices[30] = (float)screenwidth - 0.5f; quadvertices[13] = quadvertices[19] = quadvertices[31] = (float)screenheight - 0.5f; RenderLightShafts(view, proj, eye, lightpos); // STEP 4: gamma correct device->SetRenderTarget(0, sceneldrsurf); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetVertexDeclaration(quaddecl); bloom->SetTechnique("gammacorrect"); bloom->Begin(0, 0); bloom->BeginPass(0); { device->SetTexture(0, refraction); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, quadvertices, 6 * sizeof(float)); } bloom->EndPass(); bloom->End(); device->SetRenderState(D3DRS_ZENABLE, TRUE); // STEP 5: water surface device->SetRenderState(D3DRS_SRGBWRITEENABLE, TRUE); D3DXMatrixTranslation(&world, 0, -1, 0); D3DXMatrixIdentity(&inv); water->SetMatrix("matViewProj", &viewproj); water->SetMatrix("matWorld", &world); water->SetMatrix("matWorldInv", &inv); water->SetVector("eyePos", (D3DXVECTOR4*)&eye); water->SetVector("lightPos", &lightpos); water->SetVector("lightColor", &lightcolor); water->SetFloat("time", time); water->Begin(0, 0); water->BeginPass(0); { device->SetTexture(0, refraction); device->SetTexture(1, reflection); device->SetTexture(2, waves); waterplane->DrawSubset(0, DXObject::Opaque); } water->EndPass(); water->End(); device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE); // STEP 6: downsample & blur quadvertices[6] = quadvertices[24] = quadvertices[30] = (float)screenwidth * 0.5f - 0.5f; quadvertices[13] = quadvertices[19] = quadvertices[31] = (float)screenheight * 0.5f - 0.5f; device->SetRenderTarget(0, bloomsurf1); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetVertexDeclaration(quaddecl); texelsize.x = 1.0f / screenwidth; texelsize.y = 1.0f / screenheight; bloom->SetTechnique("downsample"); bloom->SetVector("texelSize", &texelsize); bloom->Begin(0, 0); bloom->BeginPass(0); { device->SetTexture(0, sceneldr); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, quadvertices, 6 * sizeof(float)); } bloom->EndPass(); bloom->End(); device->SetRenderTarget(0, bloomsurf2); texelsize.x = 2.0f / screenwidth; texelsize.y = 2.0f / screenheight; bloom->SetTechnique("blur"); bloom->SetVector("texelSize", &texelsize); bloom->Begin(0, 0); bloom->BeginPass(0); { device->SetTexture(0, bloomtex1); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, quadvertices, 6 * sizeof(float)); } bloom->EndPass(); bloom->End(); // STEP 7: add light shafts quadvertices[6] = quadvertices[24] = quadvertices[30] = (float)screenwidth - 0.5f; quadvertices[13] = quadvertices[19] = quadvertices[31] = (float)screenheight - 0.5f; device->SetRenderTarget(0, backbuffer); godray->SetTechnique("final"); godray->Begin(0, 0); godray->BeginPass(0); { device->SetTexture(0, sceneldr); device->SetTexture(1, occluders); device->SetTexture(2, bloomtex2); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, quadvertices, 6 * sizeof(float)); } godray->EndPass(); godray->End(); backbuffer->Release(); device->SetRenderState(D3DRS_ZENABLE, TRUE); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }