bool ETHRenderEntity::DrawProjShadow(const float maxHeight, const float minHeight, const ETHSceneProperties& sceneProps, const ETHLight& light, ETHSpriteEntity *pParent, const bool maxOpacity, const bool drawToTarget, const float targetAngle, const Vector3& v3TargetPos) { if (!m_pSprite || IsHidden()) return false; VideoPtr video = m_provider->GetVideo(); ETHShaderManagerPtr shaderManager = m_provider->GetShaderManager(); SpritePtr pShadow = shaderManager->GetProjShadow(); Vector3 v3LightPos; Vector3 v3ParentPos(0,0,0); const Vector3 v3EntityPos = GetPosition(); if (pParent) { v3ParentPos = pParent->GetPosition(); v3LightPos = Vector3(v3ParentPos.x, v3ParentPos.y, 0) + light.pos; } else { v3LightPos = light.pos; } // if the object is higher than the light, then the shadow shouldn't be cast on the floor if (v3LightPos.z < v3EntityPos.z) { return true; } const float scale = (m_properties.shadowScale <= 0.0f) ? 1.0f : m_properties.shadowScale; const float opacity = (m_properties.shadowOpacity <= 0.0f) ? 1.0f : m_properties.shadowOpacity; const Vector2 v2Size = GetCurrentSize(); Vector2 v2ShadowSize(v2Size.x, v2Size.y); Vector2 v2ShadowPos(v3EntityPos.x, v3EntityPos.y); // if we are drawing to a target of a rotated entity if (drawToTarget && targetAngle != 0) { // rotate the shadow position according to entity angle Matrix4x4 matRot = RotateZ(-DegreeToRadian(targetAngle)); Vector3 newShadowPos(v2ShadowPos, 0); newShadowPos = newShadowPos - v3TargetPos; newShadowPos = Multiply(newShadowPos, matRot); newShadowPos = newShadowPos + v3TargetPos; v2ShadowPos.x = newShadowPos.x; v2ShadowPos.y = newShadowPos.y; // rotate the light source to cast it correctly Vector3 newPos = v3LightPos - v3TargetPos; newPos = Multiply(newPos, matRot); v3LightPos = newPos + v3TargetPos; } Vector3 diff = v3EntityPos - v3LightPos; const float squaredDist = DP3(diff, diff); float squaredRange = light.range * light.range; if (squaredDist > squaredRange) { return true; } v2ShadowSize.x *= _ETH_SHADOW_SCALEX * scale; // calculate the correct shadow length according to the light height if ((GetPosition().z+v2Size.y) < light.pos.z) // if the light is over the entity { const float planarDist = Distance(GetPositionXY(), ETHGlobal::ToVector2(v3LightPos)); const float verticalDist = Abs((v3EntityPos.z+v2Size.y)-v3LightPos.z); const float totalDist = (planarDist/verticalDist)*Abs(v3LightPos.z); v2ShadowSize.y = totalDist-planarDist; // clamp shadow length to the object's height. This is not realistic // but it looks better for the real-time shadows. v2ShadowSize.y = Min(v2Size.y*_ETH_SHADOW_FAKE_STRETCH, v2ShadowSize.y); } else { v2ShadowSize.y *= ((drawToTarget) ? _ETH_SHADOW_SCALEY : _ETH_SHADOW_SCALEY/4); } // specify a minimum length for the shadow v2ShadowSize.y = Max(v2ShadowSize.y, v2Size.y); ShaderPtr pVS = video->GetVertexShader(); pVS->SetConstant(GS_L("shadowLength"), v2ShadowSize.y * m_properties.shadowLengthScale); pVS->SetConstant(GS_L("entityZ"), Max(m_shadowZ, v3EntityPos.z)); pVS->SetConstant(GS_L("shadowZ"), m_shadowZ); pVS->SetConstant(GS_L("lightPos"), v3LightPos); video->SetSpriteDepth( (GetType() == ETHEntityProperties::ET_VERTICAL) ? ETHEntity::ComputeDepth(m_shadowZ, maxHeight, minHeight) : Max(0.0f, ComputeDepth(maxHeight, minHeight) - m_layrableMinimumDepth)); v2ShadowSize.y = 1.0f; Vector2 lightPos2(v3LightPos.x, v3LightPos.y); const float shadowAngle = ::GetAngle((lightPos2 - Vector2(v3EntityPos.x, v3EntityPos.y))) + DegreeToRadian(targetAngle); squaredRange = Max(squaredDist, squaredRange); float attenBias = 1; // adjust brightness according to ambient light if (!maxOpacity) { attenBias = (1-(squaredDist/squaredRange)); //fade the color according to the light brightness const float colorLen = Max(Max(light.color.x, light.color.y), light.color.z); attenBias *= Min(colorLen, 1.0f); //fade the color according to the ambient light brightness const Vector3 &ambientColor = sceneProps.ambient; const float ambientColorLen = 1.0f-((ambientColor.x + ambientColor.y + ambientColor.z)/3.0f); attenBias = Min(attenBias*ambientColorLen, 1.0f); attenBias *= Max(Min((1-(GetPosition().z/Max(GetCurrentSize().y, 1.0f))), 1.0f), 0.0f); } GS_BYTE alpha = static_cast<GS_BYTE>(attenBias*255.0f*opacity); if (alpha < 8) return true; Color dwShadowColor(alpha,255,255,255); pShadow->SetOrigin(Vector2(0.5f, 0.79f)); pShadow->SetRectMode(Sprite::RM_THREE_TRIANGLES); pShadow->DrawShaped(v2ShadowPos, v2ShadowSize, dwShadowColor, dwShadowColor, dwShadowColor, dwShadowColor, RadianToDegree(shadowAngle)); return true; }
void ManagerLight::init() { // point lights float dif[4] = { 0.7f, 0.7f, 0.3f, 1.0f }; Vector diff(dif, 4); float specPointf[4] = { 0.1f, 0.1f, 0.1f, 1.0f }; Vector specPoint(specPointf, 4); float lpos1[4] = { 0.0f, -1.0f, 0.5f, 1.0f }; Vector lightPos1(lpos1, 4); Light *light1 = new Light(0, Light::POINT_LIGHT, game); light1->setPosition(lightPos1); light1->setDiffuse(diff); light1->setAmbient(diff); light1->setSpecular(specPoint); lights.push_back(light1); float lpos2[4] = { 10.0f, -1.0f, 0.5f, 1.0f }; Vector lightPos2(lpos2, 4); Light *light2 = new Light(1, Light::POINT_LIGHT, game); light2->setPosition(lightPos2); light2->setDiffuse(diff); light2->setAmbient(diff); light2->setSpecular(specPoint); lights.push_back(light2); float lpos3[4] = { -10.0f, -1.0f, 0.5f, 1.0f }; Vector lightPos3(lpos3, 4); Light *light3 = new Light(2, Light::POINT_LIGHT, game); light3->setPosition(lightPos3); light3->setDiffuse(diff); light3->setAmbient(diff); light3->setSpecular(specPoint); lights.push_back(light3); float lpos4[4] = { 0.0f, -13.0f, 0.5f, 1.0f }; Vector lightPos4(lpos4, 4); Light *light4 = new Light(3, Light::POINT_LIGHT, game); light4->setPosition(lightPos4); light4->setDiffuse(diff); light4->setAmbient(diff); light4->setSpecular(specPoint); lights.push_back(light4); float lpos5[4] = { 10.0f, -13.0f, 0.5f, 1.0f }; Vector lightPos5(lpos5, 4); Light *light5 = new Light(4, Light::POINT_LIGHT, game); light5->setPosition(lightPos5); light5->setDiffuse(diff); light5->setAmbient(diff); light5->setSpecular(specPoint); lights.push_back(light5); float lpos6[4] = { -10.0f, -13.0f, 0.5f, 1.0f }; Vector lightPos6(lpos6, 4); Light *light6 = new Light(5, Light::POINT_LIGHT, game); light6->setPosition(lightPos6); light6->setDiffuse(diff); light6->setAmbient(diff); light6->setSpecular(specPoint); lights.push_back(light6); // directional light float difDirf[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; Vector diffDir(difDirf, 4); float ldir[4] = { -1.0f, -1.0f, -1.0f, 0.0f }; Vector lightDir(ldir, 4); Light *dirLight = new Light(6, Light::DIR_LIGHT, game); dirLight->setDiffuse(diffDir); dirLight->setAmbient(diffDir); dirLight->setSpecular(diffDir); dirLight->setPosition(lightDir); this->dirLight = dirLight; lights.push_back(dirLight); // spot light float difSpotf[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; Vector difSpot(difSpotf, 4); float lSpotPosf[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; Vector lSpotPos(lSpotPosf, 4); float lSpotDirf[3] = { 0.0f, 0.0f, -1.0f }; Vector lSpotDir(lSpotDirf, 3); Light *spot = new Light(7, Light::SPOT_LIGHT, game); spot->setDiffuse(difSpot); spot->setAmbient(difSpot); spot->setSpecular(difSpot); spot->setPosition(lSpotPos); spot->setDirection(lSpotDir); spot->setCutoff(0.5f); spot->setExponent(0.1f); spotLight = spot; lights.push_back(spot); }