//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has been // created, which will happen during application initialization and windowed/full screen // toggles. This is the best location to create D3DPOOL_MANAGED resources since these // resources need to be reloaded whenever the device is destroyed. Resources created // here should be released in the OnDestroyDevice callback. //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_SettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); // Initialize the font V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont ) ); // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the // shader debugger. Debugging vertex shaders requires either REF or software vertex // processing, and debugging pixel shaders requires REF. The // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the // shader debugger. It enables source level debugging, prevents instruction // reordering, prevents dead code elimination, and forces the compiler to compile // against the next higher available software target, which ensures that the // unoptimized shaders do not exceed the shader model limitations. Setting these // flags will cause slower rendering since the shaders will be unoptimized and // forced into software. See the DirectX documentation for more information about // using the shader debugger. DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3DXSHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DXSHADER_DEBUG; #endif #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif // Determine which LDPRT texture and SH coefficient cubemap formats are supported IDirect3D9* pD3D = DXUTGetD3D9Object(); D3DCAPS9 Caps; pd3dDevice->GetDeviceCaps( &Caps ); D3DDISPLAYMODE DisplayMode; pd3dDevice->GetDisplayMode( 0, &DisplayMode ); GetSupportedTextureFormat( pD3D, &Caps, DisplayMode.Format, &g_fmtTexture, &g_fmtCubeMap ); if( D3DFMT_UNKNOWN == g_fmtTexture || D3DFMT_UNKNOWN == g_fmtCubeMap ) return E_FAIL; // Create the skybox g_Skybox.OnCreateDevice( pd3dDevice, 50, L"Light Probes\\rnl_cross.dds", L"SkyBox.fx" ); V( D3DXSHProjectCubeMap( 6, g_Skybox.GetEnvironmentMap(), g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1], g_fSkyBoxLightSH[2] ) ); // Now compute the SH projection of the skybox... LPDIRECT3DCUBETEXTURE9 pSHCubeTex = NULL; V( D3DXCreateCubeTexture( pd3dDevice, 256, 1, 0, D3DFMT_A16B16G16R16F, D3DPOOL_MANAGED, &pSHCubeTex ) ); SHCubeProj projData; projData.Init( g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1], g_fSkyBoxLightSH[2] ); V( D3DXFillCubeTexture( pSHCubeTex, SHCubeFill, &projData ) ); g_Skybox.InitSH( pSHCubeTex ); // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, TEXT( "LocalDeformablePRT.fx" ) ) ); // If this fails, there should be debug output as to they the .fx file failed to compile V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect, NULL ) ); V_RETURN( LoadTechniqueObjects( "bat" ) ); V_RETURN( g_LightControl.StaticOnD3D9CreateDevice( pd3dDevice ) ); g_LightControl.SetRadius( 2.0f ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -5.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, 0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); // Set the model's initial orientation D3DXQUATERNION quatRotation; D3DXQuaternionRotationYawPitchRoll( &quatRotation, -0.5f, 0.7f, 0.0f ); g_Camera.SetWorldQuat( quatRotation ); return hr; }
void CDemo::initialize( IDingusAppContext& appContext ) { int i; gAppContext = &appContext; CSharedTextureBundle& stb = CSharedTextureBundle::getInstance(); CSharedSurfaceBundle& ssb = CSharedSurfaceBundle::getInstance(); CD3DDevice& dx = CD3DDevice::getInstance(); G_INPUTCTX->addListener( *this ); // -------------------------------- // rendertargets stb.registerTexture( RT_SCENE, *new CScreenBasedTextureCreator(1,1,1,D3DUSAGE_RENDERTARGET,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT) ); DINGUS_REGISTER_STEX_SURFACE( ssb, RT_SCENE ); stb.registerTexture( RT_SCENE_SCALED, *new CScreenBasedDivTextureCreator(SZ_SCENE_SCALED,SZ_SCENE_SCALED,BB_DIVISIBLE_BY,1,D3DUSAGE_RENDERTARGET,D3DFMT_A16B16G16R16F,D3DPOOL_DEFAULT) ); DINGUS_REGISTER_STEX_SURFACE( ssb, RT_SCENE_SCALED ); stb.registerTexture( RT_LUM[0], *new CFixedTextureCreator(1,1,1,D3DUSAGE_RENDERTARGET,FMT_LUMINANCE,D3DPOOL_DEFAULT) ); DINGUS_REGISTER_STEX_SURFACE( ssb, RT_LUM[0] ); gClearSurface( RGET_S_SURF(RT_LUM[0]) ); stb.registerTexture( RT_LUM[1], *new CFixedTextureCreator(1,1,1,D3DUSAGE_RENDERTARGET,FMT_LUMINANCE,D3DPOOL_DEFAULT) ); DINGUS_REGISTER_STEX_SURFACE( ssb, RT_LUM[1] ); gClearSurface( RGET_S_SURF(RT_LUM[1]) ); for( i = 0; i < NUM_TONEMAP_RTS; ++i ) { int size = 1 << (2*i); // 1,4,16,... stb.registerTexture( RT_TONEMAP[i], *new CFixedTextureCreator(size,size,1,D3DUSAGE_RENDERTARGET, FMT_LUMINANCE,D3DPOOL_DEFAULT) ); DINGUS_REGISTER_STEX_SURFACE( ssb, RT_TONEMAP[i] ); } stb.registerTexture( RT_BRIGHT_PASS, *new CScreenBasedDivTextureCreator(SZ_SCENE_SCALED,SZ_SCENE_SCALED,BB_DIVISIBLE_BY,1,D3DUSAGE_RENDERTARGET,FMT_BRIGHT_PASS,D3DPOOL_DEFAULT) ); DINGUS_REGISTER_STEX_SURFACE( ssb, RT_BRIGHT_PASS ); stb.registerTexture( RT_BLOOM, *new CScreenBasedDivTextureCreator(SZ_SCENE_SCALED,SZ_SCENE_SCALED,BB_DIVISIBLE_BY,1,D3DUSAGE_RENDERTARGET,FMT_BRIGHT_PASS,D3DPOOL_DEFAULT) ); DINGUS_REGISTER_STEX_SURFACE( ssb, RT_BLOOM ); for( i = 0; i < NUM_BLOOM_TMP_TEXTURES; ++i ) { stb.registerTexture( RT_BLOOM_TMP[i], *new CScreenBasedDivTextureCreator(SZ_SCENE_SCALED,SZ_SCENE_SCALED,BB_DIVISIBLE_BY,1,D3DUSAGE_RENDERTARGET,FMT_BRIGHT_PASS,D3DPOOL_DEFAULT) ); DINGUS_REGISTER_STEX_SURFACE( ssb, RT_BLOOM_TMP[i] ); } gShadowInit(); // -------------------------------- // common params gGlobalCullMode = D3DCULL_NONE; G_RENDERCTX->getGlobalParams().addIntRef( "iCull", &gGlobalCullMode ); G_RENDERCTX->getGlobalParams().addFloatRef( "fTime", &gTimeParam ); G_RENDERCTX->getGlobalParams().addFloatRef( "fMiddleGray", &gHDRMiddleGray ); G_RENDERCTX->getGlobalParams().addFloatRef( "fEnvIntensity", &gEnvIntensity ); G_RENDERCTX->getGlobalParams().addVector4Ref( "vSHAr", gEnvSHConstAr ); G_RENDERCTX->getGlobalParams().addVector4Ref( "vSHAg", gEnvSHConstAg ); G_RENDERCTX->getGlobalParams().addVector4Ref( "vSHAb", gEnvSHConstAb ); G_RENDERCTX->getGlobalParams().addVector4Ref( "vSHBr", gEnvSHConstBr ); G_RENDERCTX->getGlobalParams().addVector4Ref( "vSHBg", gEnvSHConstBg ); G_RENDERCTX->getGlobalParams().addVector4Ref( "vSHBb", gEnvSHConstBb ); G_RENDERCTX->getGlobalParams().addVector4Ref( "vSHC", gEnvSHConstC ); // -------------------------------- // HDR environment CD3DCubeTexture* env = RGET_CUBETEX("HdrEnv"); D3DXSHProjectCubeMap( ENV_SH_ORDER, env->getObject(), gEnvSHR, gEnvSHG, gEnvSHB ); //SVector3 lightDir( -0.4f, 1.0f, -0.8f ); //lightDir.normalize(); //D3DXSHEvalDirectionalLight( ENV_SH_ORDER, &lightDir, 1.0f, 1.0f, 1.0f, gEnvSHR, gEnvSHG, gEnvSHB ); // -------------------------------- // scene gMeshLight = new CRenderableMesh( *RGET_MESH("Sphere"), 0, NULL, -1 ); gMeshLight->getParams().setEffect( *RGET_FX("objectLight") ); gMeshLight->getParams().addVector3Ref( "vPos", gLightPos ); gMeshCaster = new CRenderableMesh( *RGET_MESH("StAnnaPos"), 0, NULL, 0 ); gMeshCaster->getParams().setEffect( *RGET_FX("caster") ); gMeshGround = new CRenderableMesh( *RGET_MESH("billboard"), 0, NULL, 0 ); gMeshGround->getParams().setEffect( *RGET_FX("objectGround") ); gMeshGround->getParams().addTexture( "tShadow", *RGET_S_TEX(RT_SHADOW) ); gMeshGround->getParams().addFloatRef( "fSize", &gSceneRadius ); gMeshGround->getParams().addVector3Ref( "vPos", gSceneCenter ); gMeshNormal = new CRenderableMesh( *RGET_MESH("StAnna"), 0, NULL, 0 ); gMeshNormal->getParams().setEffect( *RGET_FX("object") ); gMeshNormal->getParams().addCubeTexture( "tEnv", *env ); gMeshNormal->getParams().addTexture( "tShadow", *RGET_S_TEX(RT_SHADOW) ); gLoadMeshAO(); CDeviceResourceManager::getInstance().addListener( *this ); activateResource(); gSceneCenter = gMeshNormal->getMesh().getTotalAABB().getCenter(); gSceneRadius = SVector3(gMeshNormal->getMesh().getTotalAABB().getMax() - gMeshNormal->getMesh().getTotalAABB().getMin()).length() * 0.5f; gCamYaw = 0.0f; gCamPitch = 0.1f; gCamDist = gSceneRadius * 2.0f; // -------------------------------- // postprocessing quads gQuadDownsampleBB = new CRenderableQuad( renderquad::SCoordRect() ); gQuadDownsampleBB->getParams().setEffect( *RGET_FX("downsample4x4") ); gQuadDownsampleBB->getParams().addPtr( "vSmpOffsets", sizeof(gSampleOffsets[0])*4, gSampleOffsets ); gQuadDownsampleBB->getParams().addTexture( "tBase", *RGET_S_TEX(RT_SCENE) ); gQuadSampleAvgLum = new CRenderableQuad( renderquad::SCoordRect(0,0,1,1) ); gQuadSampleAvgLum->getParams().setEffect( *RGET_FX("sampleAvgLum") ); gQuadSampleAvgLum->getParams().addPtr( "vSmpOffsets", sizeof(gSampleOffsets[0])*9, gSampleOffsets ); gQuadSampleAvgLum->getParams().addTexture( "tBase", *RGET_S_TEX(RT_SCENE_SCALED) ); gQuadResampleAvgLum = new CRenderableQuad( renderquad::SCoordRect(0,0,1,1) ); gQuadResampleAvgLum->getParams().setEffect( *RGET_FX("resampleAvgLum") ); gQuadResampleAvgLum->getParams().addPtr( "vSmpOffsets", sizeof(gSampleOffsets), gSampleOffsets ); gQuadResampleAvgLumExp = new CRenderableQuad( renderquad::SCoordRect(0,0,1,1) ); gQuadResampleAvgLumExp->getParams().setEffect( *RGET_FX("resampleAvgLumExp") ); gQuadResampleAvgLumExp->getParams().addPtr( "vSmpOffsets", sizeof(gSampleOffsets), gSampleOffsets ); gQuadCalcAdaptedLum = new CRenderableQuad( renderquad::SCoordRect(0,0,1,1) ); gQuadCalcAdaptedLum->getParams().setEffect( *RGET_FX("calcAdaptedLum") ); gQuadCalcAdaptedLum->getParams().addFloatRef( "fDeltaTime", &gDeltaTimeParam ); gQuadBrightPass = new CRenderableQuad( renderquad::SCoordRect(0,0,1,1) ); gQuadBrightPass->getParams().setEffect( *RGET_FX("brightPass") ); gQuadIterativeBloom = new CRenderableQuad( renderquad::SCoordRect(0,0,1,1) ); gQuadIterativeBloom->getParams().setEffect( *RGET_FX("bloom") ); gQuadIterativeBloom->getParams().addPtr( "vSmpOffsets", sizeof(gSampleOffsets[0])*4, gSampleOffsets ); gQuadFinalScenePass = new CRenderableQuad( renderquad::SCoordRect(0,0,1,1) ); gQuadFinalScenePass->getParams().setEffect( *RGET_FX("finalScenePass") ); CMesh* msky = RGET_MESH("skybox"); gMeshSkybox = new CRenderableMesh( *msky, 0, NULL, -2 ); gMeshSkybox->getParams().setEffect( *RGET_FX("skybox") ); gMeshSkybox->getParams().addCubeTexture( "tEnv", *env ); gMeshSkybox->getParams().addVector3Ref( "vPos", gCamera.mWorldMat.getOrigin() ); // -------------------------------- // GUI gUIDlgHUD = new CUIDialog(); gUIDlgHUD->setCallback( gUICallback ); const int hctl = 16; // FPS gUIDlgHUD->addStatic( 0, "", 5, 460, 200, 20, false, &gUIFPS ); // tweakables gUIDlgHUD->addStatic( 0, "Middle gray:", 5, 5, 100, hctl ); gUIDlgHUD->addSlider( 0, 100, 5, 70, hctl, 0, 4, 2, false, &gUISldMiddleGray ); gUIDlgHUD->addStatic( 0, "", 180, 5, 100, hctl, false, &gUILabMiddleGray ); int y = 5; gUIDlgHUD->addStatic( 0, "Env intens:", 300, y += 20, 100, hctl ); gUIDlgHUD->addSlider( 0, 360, y, 200, hctl, 1, 100, 10, false, &gUISldEnvIntensity ); gUIDlgHUD->addStatic( 0, "", 570, y, 100, hctl, false, &gUILabEnvIntensity ); gUIDlgHUD->addStatic( 0, "Light intens:", 300, y += 20, 100, hctl ); gUIDlgHUD->addSlider( 0, 360, y, 200, hctl, 1, 100, 10, false, &gUISldLightIntensity ); gUIDlgHUD->addStatic( 0, "", 570, y, 100, hctl, false, &gUILabLightIntensity ); gUIDlgHUD->addStatic( 0, "Light yaw:", 300, y += 20, 100, hctl ); gUIDlgHUD->addSlider( 0, 360, y, 120, hctl, 0, 360, 45, false, &gUISldLightYaw ); gUIDlgHUD->addStatic( 0, "Light pitch:", 300, y += 20, 100, hctl ); gUIDlgHUD->addSlider( 0, 360, y, 120, hctl, 10, 80, 60, false, &gUISldLightPitch ); }