void DiRenderPipeline::SetGlobalParams(DiSceneManager*sm, DiCamera* cam) { const DiCamera* realCam = cam->GetLodCamera(); mShaderEnv->viewMatrix = realCam->GetViewMatrix(); realCam->GetProjectionMatrixRs(mShaderEnv->projMatrix); mShaderEnv->eyePosition = realCam->GetDerivedPosition(); mShaderEnv->eyeDirection = realCam->GetDirection(); mShaderEnv->farnearPlane = DiVec4(cam->GetFarClipDistance(),cam->GetNearClipDistance(),0,0); mShaderEnv->viewProjMatrix = mShaderEnv->projMatrix * mShaderEnv->viewMatrix; Driver->SetFillMode(cam->GetFillMode()); mShaderEnv->time = Driver->GetElapsedSecond(); mShaderEnv->viewportSize.x = (float)DiRenderWindow::ActiveWindow->GetWidth(); mShaderEnv->viewportSize.y = (float)DiRenderWindow::ActiveWindow->GetHeight(); mShaderEnv->viewportSize.z = 1.0f / mShaderEnv->viewportSize.x; mShaderEnv->viewportSize.w = 1.0f / mShaderEnv->viewportSize.y; mShaderEnv->texelOffsets.x = Driver->GetTexelOffset().x; mShaderEnv->texelOffsets.y = Driver->GetTexelOffset().y; mShaderEnv->texelOffsets.z = Driver->GetTexelOffset().x / mShaderEnv->viewportSize.x; mShaderEnv->texelOffsets.w = Driver->GetTexelOffset().y / mShaderEnv->viewportSize.y; // bind lights // global parameters mShaderEnv->globalAmbient = sm->GetAmbientColor(); mShaderEnv->numDirLights = (int)sm->GetDirLightNum(); for(int i=0; i < mShaderEnv->numDirLights; i++) { mShaderEnv->dirLightsColor[i] = sm->GetDirLight(i)->GetColor(); DiVec3 dir = sm->GetDirLight(i)->GetDirection().normalisedCopy(); mShaderEnv->dirLightsDir[i] = dir; } mShaderEnv->numPointLights = (int)sm->GetPointLightNum(); for(int i=0; i < mShaderEnv->numPointLights; i++) { mShaderEnv->pointLightsColor[i] = sm->GetPointLight(i)->GetColor(); DiVec3 pos = sm->GetPointLight(i)->GetPosition(); mShaderEnv->pointLightsPosition[i] = pos; mShaderEnv->pointLightsAttenuation[i].x = sm->GetPointLight(i)->GetAttenuationBegin(); mShaderEnv->pointLightsAttenuation[i].y = sm->GetPointLight(i)->GetAttenuationEnd(); } }
void SetupWater(DiSceneManager* sm) { // Render targets auto reflectMap = DiAssetManager::GetInstance().CreateOrReplaceAsset<DiTexture>("reflect_rt"); reflectMap->SetDimensions(512, 512); reflectMap->SetFormat(PF_A8R8G8B8); reflectMap->SetUsage(TU_RENDER_TARGET); reflectMap->SetAutoMipmap(false); reflectMap->SetAddressing(AM_CLAMP); reflectMap->CreateTexture(); auto reflectRT = reflectMap->GetRenderTarget(); auto refractMap = DiAssetManager::GetInstance().CreateOrReplaceAsset<DiTexture>("refract_rt"); refractMap->SetDimensions(512, 512); refractMap->SetFormat(PF_A8R8G8B8); refractMap->SetUsage(TU_RENDER_TARGET); refractMap->SetAutoMipmap(false); refractMap->SetAddressing(AM_CLAMP); refractMap->CreateTexture(); auto refractRT = refractMap->GetRenderTarget(); // Water plane DiCullNode* nodePlane = sm->GetRootNode()->CreateChild(); DiSimpleShapePtr model = make_shared<DiSimpleShape>(); model->CreatePlane(100); auto waterMat = DiMaterial::QuickCreate("pool_fresnel_v", "pool_fresnel_p"); auto shaderParam = waterMat->GetShaderParameter(); shaderParam->WriteFloat("scale", 1.0f); shaderParam->WriteFloat("scroll", 0.05f); shaderParam->WriteFloat("fresnelBias", -0.1f); shaderParam->WriteFloat("fresnelScale", 1.8f); shaderParam->WriteFloat("fresnelPower", 8.0f); shaderParam->WriteFloat("noiseScale", 0.05f); shaderParam->WriteFloat4("tintColour", DiVec4(0, 0.05f, 0.05f, 1)); shaderParam->WriteTexture2D("noiseMap", "waves2.dds"); shaderParam->WriteTexture2D("reflectMap", "reflect_rt"); shaderParam->WriteTexture2D("refractMap", "refract_rt"); model->SetMaterial(waterMat); nodePlane->AttachObject(model); nodePlane->SetScale(7, 1, 13); // Add to scene manager with the callbacks DiCamera* cam = sm->GetCamera(); cam->MoveRelative(DiVec3(0, 0, 700)); sm->AddExtraRenderTarget(refractRT, cam, [nodePlane](DiRenderTarget*) { nodePlane->SetVisible(false); }, [nodePlane](DiRenderTarget*) { nodePlane->SetVisible(true); } ); sm->AddExtraRenderTarget(reflectRT, cam, [nodePlane, cam](DiRenderTarget*) { nodePlane->SetVisible(false); cam->EnableReflection(DiPlane(DiVec3::UNIT_Y, 0)); }, [nodePlane, cam](DiRenderTarget*) { nodePlane->SetVisible(true); cam->DisableReflection(); } ); DiPostEffectManager* peMgr = DiBase::Driver->GetMainRenderWindow()->GetPostEffectManager(); DiPostEffect* bloom = peMgr->GetEffect("Bloom"); if (bloom) bloom->SetEnable(false); }