| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of OGRE |
|---|
| 4 | (Object-oriented Graphics Rendering Engine) |
|---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
|---|
| 6 | |
|---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
|---|
| 8 | Also see acknowledgements in Readme.html |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | |
|---|
| 24 | You may alternatively use this source under the terms of a specific version of |
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
|---|
| 26 | Torus Knot Software Ltd. |
|---|
| 27 | ----------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | #include "OgreStableHeaders.h" |
|---|
| 30 | #include "OgreLight.h" |
|---|
| 31 | |
|---|
| 32 | #include "OgreException.h" |
|---|
| 33 | #include "OgreSceneNode.h" |
|---|
| 34 | #include "OgreCamera.h" |
|---|
| 35 | #include "OgreSceneManager.h" |
|---|
| 36 | |
|---|
| 37 | namespace Ogre { |
|---|
| 38 | //----------------------------------------------------------------------- |
|---|
| 39 | Light::Light() |
|---|
| 40 | : mLightType(LT_POINT), |
|---|
| 41 | mPosition(Vector3::ZERO), |
|---|
| 42 | mDiffuse(ColourValue::White), |
|---|
| 43 | mSpecular(ColourValue::Black), |
|---|
| 44 | mDirection(Vector3::UNIT_Z), |
|---|
| 45 | mSpotOuter(Degree(40.0f)), |
|---|
| 46 | mSpotInner(Degree(30.0f)), |
|---|
| 47 | mSpotFalloff(1.0f), |
|---|
| 48 | mRange(100000), |
|---|
| 49 | mAttenuationConst(1.0f), |
|---|
| 50 | mAttenuationLinear(0.0f), |
|---|
| 51 | mAttenuationQuad(0.0f), |
|---|
| 52 | mPowerScale(1.0f), |
|---|
| 53 | mDerivedPosition(Vector3::ZERO), |
|---|
| 54 | mDerivedDirection(Vector3::UNIT_Z), |
|---|
| 55 | mDerivedTransformDirty(false), |
|---|
| 56 | mCustomShadowCameraSetup() |
|---|
| 57 | { |
|---|
| 58 | } |
|---|
| 59 | //----------------------------------------------------------------------- |
|---|
| 60 | Light::Light(const String& name) : MovableObject(name), |
|---|
| 61 | mLightType(LT_POINT), |
|---|
| 62 | mPosition(Vector3::ZERO), |
|---|
| 63 | mDiffuse(ColourValue::White), |
|---|
| 64 | mSpecular(ColourValue::Black), |
|---|
| 65 | mDirection(Vector3::UNIT_Z), |
|---|
| 66 | mSpotOuter(Degree(40.0f)), |
|---|
| 67 | mSpotInner(Degree(30.0f)), |
|---|
| 68 | mSpotFalloff(1.0f), |
|---|
| 69 | mRange(100000), |
|---|
| 70 | mAttenuationConst(1.0f), |
|---|
| 71 | mAttenuationLinear(0.0f), |
|---|
| 72 | mAttenuationQuad(0.0f), |
|---|
| 73 | mPowerScale(1.0f), |
|---|
| 74 | mDerivedPosition(Vector3::ZERO), |
|---|
| 75 | mDerivedDirection(Vector3::UNIT_Z), |
|---|
| 76 | mDerivedTransformDirty(false), |
|---|
| 77 | mCustomShadowCameraSetup() |
|---|
| 78 | { |
|---|
| 79 | } |
|---|
| 80 | //----------------------------------------------------------------------- |
|---|
| 81 | Light::~Light() |
|---|
| 82 | { |
|---|
| 83 | } |
|---|
| 84 | //----------------------------------------------------------------------- |
|---|
| 85 | void Light::setType(LightTypes type) |
|---|
| 86 | { |
|---|
| 87 | mLightType = type; |
|---|
| 88 | } |
|---|
| 89 | //----------------------------------------------------------------------- |
|---|
| 90 | Light::LightTypes Light::getType(void) const |
|---|
| 91 | { |
|---|
| 92 | return mLightType; |
|---|
| 93 | } |
|---|
| 94 | //----------------------------------------------------------------------- |
|---|
| 95 | void Light::setPosition(Real x, Real y, Real z) |
|---|
| 96 | { |
|---|
| 97 | mPosition.x = x; |
|---|
| 98 | mPosition.y = y; |
|---|
| 99 | mPosition.z = z; |
|---|
| 100 | mDerivedTransformDirty = true; |
|---|
| 101 | |
|---|
| 102 | } |
|---|
| 103 | //----------------------------------------------------------------------- |
|---|
| 104 | void Light::setPosition(const Vector3& vec) |
|---|
| 105 | { |
|---|
| 106 | mPosition = vec; |
|---|
| 107 | mDerivedTransformDirty = true; |
|---|
| 108 | } |
|---|
| 109 | //----------------------------------------------------------------------- |
|---|
| 110 | const Vector3& Light::getPosition(void) const |
|---|
| 111 | { |
|---|
| 112 | return mPosition; |
|---|
| 113 | } |
|---|
| 114 | //----------------------------------------------------------------------- |
|---|
| 115 | void Light::setDirection(Real x, Real y, Real z) |
|---|
| 116 | { |
|---|
| 117 | mDirection.x = x; |
|---|
| 118 | mDirection.y = y; |
|---|
| 119 | mDirection.z = z; |
|---|
| 120 | mDerivedTransformDirty = true; |
|---|
| 121 | } |
|---|
| 122 | //----------------------------------------------------------------------- |
|---|
| 123 | void Light::setDirection(const Vector3& vec) |
|---|
| 124 | { |
|---|
| 125 | mDirection = vec; |
|---|
| 126 | mDerivedTransformDirty = true; |
|---|
| 127 | } |
|---|
| 128 | //----------------------------------------------------------------------- |
|---|
| 129 | const Vector3& Light::getDirection(void) const |
|---|
| 130 | { |
|---|
| 131 | return mDirection; |
|---|
| 132 | } |
|---|
| 133 | //----------------------------------------------------------------------- |
|---|
| 134 | void Light::setSpotlightRange(const Radian& innerAngle, const Radian& outerAngle, Real falloff) |
|---|
| 135 | { |
|---|
| 136 | |
|---|
| 137 | if (mLightType != LT_SPOTLIGHT) |
|---|
| 138 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, |
|---|
| 139 | "setSpotlightRange is only valid for spotlights.", |
|---|
| 140 | "Light::setSpotlightRange"); |
|---|
| 141 | |
|---|
| 142 | mSpotInner =innerAngle; |
|---|
| 143 | mSpotOuter = outerAngle; |
|---|
| 144 | mSpotFalloff = falloff; |
|---|
| 145 | } |
|---|
| 146 | //----------------------------------------------------------------------- |
|---|
| 147 | void Light::setSpotlightInnerAngle(const Radian& val) |
|---|
| 148 | { |
|---|
| 149 | mSpotInner = val; |
|---|
| 150 | } |
|---|
| 151 | //----------------------------------------------------------------------- |
|---|
| 152 | void Light::setSpotlightOuterAngle(const Radian& val) |
|---|
| 153 | { |
|---|
| 154 | mSpotOuter = val; |
|---|
| 155 | } |
|---|
| 156 | //----------------------------------------------------------------------- |
|---|
| 157 | void Light::setSpotlightFalloff(Real val) |
|---|
| 158 | { |
|---|
| 159 | mSpotFalloff = val; |
|---|
| 160 | } |
|---|
| 161 | //----------------------------------------------------------------------- |
|---|
| 162 | const Radian& Light::getSpotlightInnerAngle(void) const |
|---|
| 163 | { |
|---|
| 164 | return mSpotInner; |
|---|
| 165 | } |
|---|
| 166 | //----------------------------------------------------------------------- |
|---|
| 167 | const Radian& Light::getSpotlightOuterAngle(void) const |
|---|
| 168 | { |
|---|
| 169 | return mSpotOuter; |
|---|
| 170 | } |
|---|
| 171 | //----------------------------------------------------------------------- |
|---|
| 172 | Real Light::getSpotlightFalloff(void) const |
|---|
| 173 | { |
|---|
| 174 | return mSpotFalloff; |
|---|
| 175 | } |
|---|
| 176 | //----------------------------------------------------------------------- |
|---|
| 177 | void Light::setDiffuseColour(Real red, Real green, Real blue) |
|---|
| 178 | { |
|---|
| 179 | mDiffuse.r = red; |
|---|
| 180 | mDiffuse.b = blue; |
|---|
| 181 | mDiffuse.g = green; |
|---|
| 182 | } |
|---|
| 183 | //----------------------------------------------------------------------- |
|---|
| 184 | void Light::setDiffuseColour(const ColourValue& colour) |
|---|
| 185 | { |
|---|
| 186 | mDiffuse = colour; |
|---|
| 187 | } |
|---|
| 188 | //----------------------------------------------------------------------- |
|---|
| 189 | const ColourValue& Light::getDiffuseColour(void) const |
|---|
| 190 | { |
|---|
| 191 | return mDiffuse; |
|---|
| 192 | } |
|---|
| 193 | //----------------------------------------------------------------------- |
|---|
| 194 | void Light::setSpecularColour(Real red, Real green, Real blue) |
|---|
| 195 | { |
|---|
| 196 | mSpecular.r = red; |
|---|
| 197 | mSpecular.b = blue; |
|---|
| 198 | mSpecular.g = green; |
|---|
| 199 | } |
|---|
| 200 | //----------------------------------------------------------------------- |
|---|
| 201 | void Light::setSpecularColour(const ColourValue& colour) |
|---|
| 202 | { |
|---|
| 203 | mSpecular = colour; |
|---|
| 204 | } |
|---|
| 205 | //----------------------------------------------------------------------- |
|---|
| 206 | const ColourValue& Light::getSpecularColour(void) const |
|---|
| 207 | { |
|---|
| 208 | return mSpecular; |
|---|
| 209 | } |
|---|
| 210 | //----------------------------------------------------------------------- |
|---|
| 211 | void Light::setAttenuation(Real range, Real constant, |
|---|
| 212 | Real linear, Real quadratic) |
|---|
| 213 | { |
|---|
| 214 | mRange = range; |
|---|
| 215 | mAttenuationConst = constant; |
|---|
| 216 | mAttenuationLinear = linear; |
|---|
| 217 | mAttenuationQuad = quadratic; |
|---|
| 218 | } |
|---|
| 219 | //----------------------------------------------------------------------- |
|---|
| 220 | Real Light::getAttenuationRange(void) const |
|---|
| 221 | { |
|---|
| 222 | return mRange; |
|---|
| 223 | } |
|---|
| 224 | //----------------------------------------------------------------------- |
|---|
| 225 | Real Light::getAttenuationConstant(void) const |
|---|
| 226 | { |
|---|
| 227 | return mAttenuationConst; |
|---|
| 228 | } |
|---|
| 229 | //----------------------------------------------------------------------- |
|---|
| 230 | Real Light::getAttenuationLinear(void) const |
|---|
| 231 | { |
|---|
| 232 | return mAttenuationLinear; |
|---|
| 233 | } |
|---|
| 234 | //----------------------------------------------------------------------- |
|---|
| 235 | Real Light::getAttenuationQuadric(void) const |
|---|
| 236 | { |
|---|
| 237 | return mAttenuationQuad; |
|---|
| 238 | } |
|---|
| 239 | //----------------------------------------------------------------------- |
|---|
| 240 | void Light::setPowerScale(Real power) |
|---|
| 241 | { |
|---|
| 242 | mPowerScale = power; |
|---|
| 243 | } |
|---|
| 244 | //----------------------------------------------------------------------- |
|---|
| 245 | Real Light::getPowerScale(void) const |
|---|
| 246 | { |
|---|
| 247 | return mPowerScale; |
|---|
| 248 | } |
|---|
| 249 | //----------------------------------------------------------------------- |
|---|
| 250 | void Light::update(void) const |
|---|
| 251 | { |
|---|
| 252 | if (mDerivedTransformDirty) |
|---|
| 253 | { |
|---|
| 254 | if (mParentNode) |
|---|
| 255 | { |
|---|
| 256 | // Ok, update with SceneNode we're attached to |
|---|
| 257 | const Quaternion& parentOrientation = mParentNode->_getDerivedOrientation(); |
|---|
| 258 | const Vector3& parentPosition = mParentNode->_getDerivedPosition(); |
|---|
| 259 | mDerivedDirection = parentOrientation * mDirection; |
|---|
| 260 | mDerivedPosition = (parentOrientation * mPosition) + parentPosition; |
|---|
| 261 | } |
|---|
| 262 | else |
|---|
| 263 | { |
|---|
| 264 | mDerivedPosition = mPosition; |
|---|
| 265 | mDerivedDirection = mDirection; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | mDerivedTransformDirty = false; |
|---|
| 269 | } |
|---|
| 270 | } |
|---|
| 271 | //----------------------------------------------------------------------- |
|---|
| 272 | void Light::_notifyAttached(Node* parent, bool isTagPoint) |
|---|
| 273 | { |
|---|
| 274 | mDerivedTransformDirty = true; |
|---|
| 275 | |
|---|
| 276 | MovableObject::_notifyAttached(parent, isTagPoint); |
|---|
| 277 | } |
|---|
| 278 | //----------------------------------------------------------------------- |
|---|
| 279 | void Light::_notifyMoved(void) |
|---|
| 280 | { |
|---|
| 281 | mDerivedTransformDirty = true; |
|---|
| 282 | |
|---|
| 283 | MovableObject::_notifyMoved(); |
|---|
| 284 | } |
|---|
| 285 | //----------------------------------------------------------------------- |
|---|
| 286 | const AxisAlignedBox& Light::getBoundingBox(void) const |
|---|
| 287 | { |
|---|
| 288 | // Null, lights are not visible |
|---|
| 289 | static AxisAlignedBox box; |
|---|
| 290 | return box; |
|---|
| 291 | } |
|---|
| 292 | //----------------------------------------------------------------------- |
|---|
| 293 | void Light::_updateRenderQueue(RenderQueue* queue) |
|---|
| 294 | { |
|---|
| 295 | // Do nothing |
|---|
| 296 | } |
|---|
| 297 | //----------------------------------------------------------------------- |
|---|
| 298 | const String& Light::getMovableType(void) const |
|---|
| 299 | { |
|---|
| 300 | return LightFactory::FACTORY_TYPE_NAME; |
|---|
| 301 | } |
|---|
| 302 | //----------------------------------------------------------------------- |
|---|
| 303 | const Vector3& Light::getDerivedPosition(void) const |
|---|
| 304 | { |
|---|
| 305 | update(); |
|---|
| 306 | return mDerivedPosition; |
|---|
| 307 | } |
|---|
| 308 | //----------------------------------------------------------------------- |
|---|
| 309 | const Vector3& Light::getDerivedDirection(void) const |
|---|
| 310 | { |
|---|
| 311 | update(); |
|---|
| 312 | return mDerivedDirection; |
|---|
| 313 | } |
|---|
| 314 | //----------------------------------------------------------------------- |
|---|
| 315 | void Light::setVisible(bool visible) |
|---|
| 316 | { |
|---|
| 317 | MovableObject::setVisible(visible); |
|---|
| 318 | } |
|---|
| 319 | //----------------------------------------------------------------------- |
|---|
| 320 | Vector4 Light::getAs4DVector(void) const |
|---|
| 321 | { |
|---|
| 322 | Vector4 ret; |
|---|
| 323 | if (mLightType == Light::LT_DIRECTIONAL) |
|---|
| 324 | { |
|---|
| 325 | ret = -(getDerivedDirection()); // negate direction as 'position' |
|---|
| 326 | ret.w = 0.0; // infinite distance |
|---|
| 327 | } |
|---|
| 328 | else |
|---|
| 329 | { |
|---|
| 330 | ret = getDerivedPosition(); |
|---|
| 331 | ret.w = 1.0; |
|---|
| 332 | } |
|---|
| 333 | return ret; |
|---|
| 334 | } |
|---|
| 335 | //----------------------------------------------------------------------- |
|---|
| 336 | const PlaneBoundedVolume& Light::_getNearClipVolume(const Camera* const cam) const |
|---|
| 337 | { |
|---|
| 338 | // First check if the light is close to the near plane, since |
|---|
| 339 | // in this case we have to build a degenerate clip volume |
|---|
| 340 | mNearClipVolume.planes.clear(); |
|---|
| 341 | mNearClipVolume.outside = Plane::NEGATIVE_SIDE; |
|---|
| 342 | |
|---|
| 343 | Real n = cam->getNearClipDistance(); |
|---|
| 344 | // Homogenous position |
|---|
| 345 | Vector4 lightPos = getAs4DVector(); |
|---|
| 346 | // 3D version (not the same as _getDerivedPosition, is -direction for |
|---|
| 347 | // directional lights) |
|---|
| 348 | Vector3 lightPos3 = Vector3(lightPos.x, lightPos.y, lightPos.z); |
|---|
| 349 | |
|---|
| 350 | // Get eye-space light position |
|---|
| 351 | // use 4D vector so directional lights still work |
|---|
| 352 | Vector4 eyeSpaceLight = cam->getViewMatrix() * lightPos; |
|---|
| 353 | // Find distance to light, project onto -Z axis |
|---|
| 354 | Real d = eyeSpaceLight.dotProduct( |
|---|
| 355 | Vector4(0, 0, -1, -n) ); |
|---|
| 356 | #define THRESHOLD 1e-6 |
|---|
| 357 | if (d > THRESHOLD || d < -THRESHOLD) |
|---|
| 358 | { |
|---|
| 359 | // light is not too close to the near plane |
|---|
| 360 | // First find the worldspace positions of the corners of the viewport |
|---|
| 361 | const Vector3 *corner = cam->getWorldSpaceCorners(); |
|---|
| 362 | int winding = (d < 0) ^ cam->isReflected() ? +1 : -1; |
|---|
| 363 | // Iterate over world points and form side planes |
|---|
| 364 | Vector3 normal; |
|---|
| 365 | Vector3 lightDir; |
|---|
| 366 | for (unsigned int i = 0; i < 4; ++i) |
|---|
| 367 | { |
|---|
| 368 | // Figure out light dir |
|---|
| 369 | lightDir = lightPos3 - (corner[i] * lightPos.w); |
|---|
| 370 | // Cross with anticlockwise corner, therefore normal points in |
|---|
| 371 | normal = (corner[i] - corner[(i+winding)%4]) |
|---|
| 372 | .crossProduct(lightDir); |
|---|
| 373 | normal.normalise(); |
|---|
| 374 | mNearClipVolume.planes.push_back(Plane(normal, corner[i])); |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | // Now do the near plane plane |
|---|
| 378 | normal = cam->getFrustumPlane(FRUSTUM_PLANE_NEAR).normal; |
|---|
| 379 | if (d < 0) |
|---|
| 380 | { |
|---|
| 381 | // Behind near plane |
|---|
| 382 | normal = -normal; |
|---|
| 383 | } |
|---|
| 384 | const Vector3& cameraPos = cam->getDerivedPosition(); |
|---|
| 385 | mNearClipVolume.planes.push_back(Plane(normal, cameraPos)); |
|---|
| 386 | |
|---|
| 387 | // Finally, for a point/spot light we can add a sixth plane |
|---|
| 388 | // This prevents false positives from behind the light |
|---|
| 389 | if (mLightType != LT_DIRECTIONAL) |
|---|
| 390 | { |
|---|
| 391 | // Direction from light perpendicular to near plane |
|---|
| 392 | mNearClipVolume.planes.push_back(Plane(-normal, lightPos3)); |
|---|
| 393 | } |
|---|
| 394 | } |
|---|
| 395 | else |
|---|
| 396 | { |
|---|
| 397 | // light is close to being on the near plane |
|---|
| 398 | // degenerate volume including the entire scene |
|---|
| 399 | // we will always require light / dark caps |
|---|
| 400 | mNearClipVolume.planes.push_back(Plane(Vector3::UNIT_Z, -n)); |
|---|
| 401 | mNearClipVolume.planes.push_back(Plane(-Vector3::UNIT_Z, n)); |
|---|
| 402 | } |
|---|
| 403 | |
|---|
| 404 | return mNearClipVolume; |
|---|
| 405 | } |
|---|
| 406 | //----------------------------------------------------------------------- |
|---|
| 407 | const PlaneBoundedVolumeList& Light::_getFrustumClipVolumes(const Camera* const cam) const |
|---|
| 408 | { |
|---|
| 409 | |
|---|
| 410 | // Homogenous light position |
|---|
| 411 | Vector4 lightPos = getAs4DVector(); |
|---|
| 412 | // 3D version (not the same as _getDerivedPosition, is -direction for |
|---|
| 413 | // directional lights) |
|---|
| 414 | Vector3 lightPos3 = Vector3(lightPos.x, lightPos.y, lightPos.z); |
|---|
| 415 | |
|---|
| 416 | const Vector3 *clockwiseVerts[4]; |
|---|
| 417 | |
|---|
| 418 | // Get worldspace frustum corners |
|---|
| 419 | const Vector3* corners = cam->getWorldSpaceCorners(); |
|---|
| 420 | int winding = cam->isReflected() ? +1 : -1; |
|---|
| 421 | |
|---|
| 422 | bool infiniteViewDistance = (cam->getFarClipDistance() == 0); |
|---|
| 423 | |
|---|
| 424 | mFrustumClipVolumes.clear(); |
|---|
| 425 | for (unsigned short n = 0; n < 6; ++n) |
|---|
| 426 | { |
|---|
| 427 | // Skip far plane if infinite view frustum |
|---|
| 428 | if (infiniteViewDistance && n == FRUSTUM_PLANE_FAR) |
|---|
| 429 | continue; |
|---|
| 430 | |
|---|
| 431 | const Plane& plane = cam->getFrustumPlane(n); |
|---|
| 432 | Vector4 planeVec(plane.normal.x, plane.normal.y, plane.normal.z, plane.d); |
|---|
| 433 | // planes face inwards, we need to know if light is on negative side |
|---|
| 434 | Real d = planeVec.dotProduct(lightPos); |
|---|
| 435 | if (d < -1e-06) |
|---|
| 436 | { |
|---|
| 437 | // Ok, this is a valid one |
|---|
| 438 | // clockwise verts mean we can cross-product and always get normals |
|---|
| 439 | // facing into the volume we create |
|---|
| 440 | |
|---|
| 441 | mFrustumClipVolumes.push_back(PlaneBoundedVolume()); |
|---|
| 442 | PlaneBoundedVolume& vol = mFrustumClipVolumes.back(); |
|---|
| 443 | switch(n) |
|---|
| 444 | { |
|---|
| 445 | case(FRUSTUM_PLANE_NEAR): |
|---|
| 446 | clockwiseVerts[0] = corners + 3; |
|---|
| 447 | clockwiseVerts[1] = corners + 2; |
|---|
| 448 | clockwiseVerts[2] = corners + 1; |
|---|
| 449 | clockwiseVerts[3] = corners + 0; |
|---|
| 450 | break; |
|---|
| 451 | case(FRUSTUM_PLANE_FAR): |
|---|
| 452 | clockwiseVerts[0] = corners + 7; |
|---|
| 453 | clockwiseVerts[1] = corners + 6; |
|---|
| 454 | clockwiseVerts[2] = corners + 5; |
|---|
| 455 | clockwiseVerts[3] = corners + 4; |
|---|
| 456 | break; |
|---|
| 457 | case(FRUSTUM_PLANE_LEFT): |
|---|
| 458 | clockwiseVerts[0] = corners + 2; |
|---|
| 459 | clockwiseVerts[1] = corners + 6; |
|---|
| 460 | clockwiseVerts[2] = corners + 5; |
|---|
| 461 | clockwiseVerts[3] = corners + 1; |
|---|
| 462 | break; |
|---|
| 463 | case(FRUSTUM_PLANE_RIGHT): |
|---|
| 464 | clockwiseVerts[0] = corners + 7; |
|---|
| 465 | clockwiseVerts[1] = corners + 3; |
|---|
| 466 | clockwiseVerts[2] = corners + 0; |
|---|
| 467 | clockwiseVerts[3] = corners + 4; |
|---|
| 468 | break; |
|---|
| 469 | case(FRUSTUM_PLANE_TOP): |
|---|
| 470 | clockwiseVerts[0] = corners + 0; |
|---|
| 471 | clockwiseVerts[1] = corners + 1; |
|---|
| 472 | clockwiseVerts[2] = corners + 5; |
|---|
| 473 | clockwiseVerts[3] = corners + 4; |
|---|
| 474 | break; |
|---|
| 475 | case(FRUSTUM_PLANE_BOTTOM): |
|---|
| 476 | clockwiseVerts[0] = corners + 7; |
|---|
| 477 | clockwiseVerts[1] = corners + 6; |
|---|
| 478 | clockwiseVerts[2] = corners + 2; |
|---|
| 479 | clockwiseVerts[3] = corners + 3; |
|---|
| 480 | break; |
|---|
| 481 | }; |
|---|
| 482 | |
|---|
| 483 | // Build a volume |
|---|
| 484 | // Iterate over world points and form side planes |
|---|
| 485 | Vector3 normal; |
|---|
| 486 | Vector3 lightDir; |
|---|
| 487 | for (unsigned int i = 0; i < 4; ++i) |
|---|
| 488 | { |
|---|
| 489 | // Figure out light dir |
|---|
| 490 | lightDir = lightPos3 - (*(clockwiseVerts[i]) * lightPos.w); |
|---|
| 491 | Vector3 edgeDir = *(clockwiseVerts[i]) - *(clockwiseVerts[(i+winding)%4]); |
|---|
| 492 | // Cross with anticlockwise corner, therefore normal points in |
|---|
| 493 | normal = edgeDir.crossProduct(lightDir); |
|---|
| 494 | normal.normalise(); |
|---|
| 495 | vol.planes.push_back(Plane(normal, *(clockwiseVerts[i]))); |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | // Now do the near plane (this is the plane of the side we're |
|---|
| 499 | // talking about, with the normal inverted (d is already interpreted as -ve) |
|---|
| 500 | vol.planes.push_back( Plane(-plane.normal, plane.d) ); |
|---|
| 501 | |
|---|
| 502 | // Finally, for a point/spot light we can add a sixth plane |
|---|
| 503 | // This prevents false positives from behind the light |
|---|
| 504 | if (mLightType != LT_DIRECTIONAL) |
|---|
| 505 | { |
|---|
| 506 | // re-use our own plane normal |
|---|
| 507 | vol.planes.push_back(Plane(plane.normal, lightPos3)); |
|---|
| 508 | } |
|---|
| 509 | } |
|---|
| 510 | } |
|---|
| 511 | |
|---|
| 512 | return mFrustumClipVolumes; |
|---|
| 513 | } |
|---|
| 514 | //----------------------------------------------------------------------- |
|---|
| 515 | uint32 Light::getTypeFlags(void) const |
|---|
| 516 | { |
|---|
| 517 | return SceneManager::LIGHT_TYPE_MASK; |
|---|
| 518 | } |
|---|
| 519 | //----------------------------------------------------------------------- |
|---|
| 520 | const String& Light::getAnimableDictionaryName(void) const |
|---|
| 521 | { |
|---|
| 522 | return LightFactory::FACTORY_TYPE_NAME; |
|---|
| 523 | } |
|---|
| 524 | //----------------------------------------------------------------------- |
|---|
| 525 | void Light::initialiseAnimableDictionary(StringVector& vec) const |
|---|
| 526 | { |
|---|
| 527 | vec.push_back("diffuseColour"); |
|---|
| 528 | vec.push_back("specularColour"); |
|---|
| 529 | vec.push_back("attenuation"); |
|---|
| 530 | vec.push_back("spotlightInner"); |
|---|
| 531 | vec.push_back("spotlightOuter"); |
|---|
| 532 | vec.push_back("spotlightFalloff"); |
|---|
| 533 | |
|---|
| 534 | } |
|---|
| 535 | //----------------------------------------------------------------------- |
|---|
| 536 | class LightDiffuseColourValue : public AnimableValue |
|---|
| 537 | { |
|---|
| 538 | protected: |
|---|
| 539 | Light* mLight; |
|---|
| 540 | public: |
|---|
| 541 | LightDiffuseColourValue(Light* l) :AnimableValue(COLOUR) |
|---|
| 542 | { mLight = l; } |
|---|
| 543 | void setValue(const ColourValue& val) |
|---|
| 544 | { |
|---|
| 545 | mLight->setDiffuseColour(val); |
|---|
| 546 | } |
|---|
| 547 | void applyDeltaValue(const ColourValue& val) |
|---|
| 548 | { |
|---|
| 549 | setValue(mLight->getDiffuseColour() + val); |
|---|
| 550 | } |
|---|
| 551 | void setCurrentStateAsBaseValue(void) |
|---|
| 552 | { |
|---|
| 553 | setAsBaseValue(mLight->getDiffuseColour()); |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | }; |
|---|
| 557 | //----------------------------------------------------------------------- |
|---|
| 558 | class LightSpecularColourValue : public AnimableValue |
|---|
| 559 | { |
|---|
| 560 | protected: |
|---|
| 561 | Light* mLight; |
|---|
| 562 | public: |
|---|
| 563 | LightSpecularColourValue(Light* l) :AnimableValue(COLOUR) |
|---|
| 564 | { mLight = l; } |
|---|
| 565 | void setValue(const ColourValue& val) |
|---|
| 566 | { |
|---|
| 567 | mLight->setSpecularColour(val); |
|---|
| 568 | } |
|---|
| 569 | void applyDeltaValue(const ColourValue& val) |
|---|
| 570 | { |
|---|
| 571 | setValue(mLight->getSpecularColour() + val); |
|---|
| 572 | } |
|---|
| 573 | void setCurrentStateAsBaseValue(void) |
|---|
| 574 | { |
|---|
| 575 | setAsBaseValue(mLight->getSpecularColour()); |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | }; |
|---|
| 579 | //----------------------------------------------------------------------- |
|---|
| 580 | class LightAttenuationValue : public AnimableValue |
|---|
| 581 | { |
|---|
| 582 | protected: |
|---|
| 583 | Light* mLight; |
|---|
| 584 | public: |
|---|
| 585 | LightAttenuationValue(Light* l) :AnimableValue(VECTOR4) |
|---|
| 586 | { mLight = l; } |
|---|
| 587 | void setValue(const Vector4& val) |
|---|
| 588 | { |
|---|
| 589 | mLight->setAttenuation(val.x, val.y, val.z, val.w); |
|---|
| 590 | } |
|---|
| 591 | void applyDeltaValue(const Vector4& val) |
|---|
| 592 | { |
|---|
| 593 | setValue(mLight->getAs4DVector() + val); |
|---|
| 594 | } |
|---|
| 595 | void setCurrentStateAsBaseValue(void) |
|---|
| 596 | { |
|---|
| 597 | setAsBaseValue(mLight->getAs4DVector()); |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | }; |
|---|
| 601 | //----------------------------------------------------------------------- |
|---|
| 602 | class LightSpotlightInnerValue : public AnimableValue |
|---|
| 603 | { |
|---|
| 604 | protected: |
|---|
| 605 | Light* mLight; |
|---|
| 606 | public: |
|---|
| 607 | LightSpotlightInnerValue(Light* l) :AnimableValue(REAL) |
|---|
| 608 | { mLight = l; } |
|---|
| 609 | void setValue(Real val) |
|---|
| 610 | { |
|---|
| 611 | mLight->setSpotlightInnerAngle(Radian(val)); |
|---|
| 612 | } |
|---|
| 613 | void applyDeltaValue(Real val) |
|---|
| 614 | { |
|---|
| 615 | setValue(mLight->getSpotlightInnerAngle().valueRadians() + val); |
|---|
| 616 | } |
|---|
| 617 | void setCurrentStateAsBaseValue(void) |
|---|
| 618 | { |
|---|
| 619 | setAsBaseValue(mLight->getSpotlightInnerAngle().valueRadians()); |
|---|
| 620 | } |
|---|
| 621 | |
|---|
| 622 | }; |
|---|
| 623 | //----------------------------------------------------------------------- |
|---|
| 624 | class LightSpotlightOuterValue : public AnimableValue |
|---|
| 625 | { |
|---|
| 626 | protected: |
|---|
| 627 | Light* mLight; |
|---|
| 628 | public: |
|---|
| 629 | LightSpotlightOuterValue(Light* l) :AnimableValue(REAL) |
|---|
| 630 | { mLight = l; } |
|---|
| 631 | void setValue(Real val) |
|---|
| 632 | { |
|---|
| 633 | mLight->setSpotlightOuterAngle(Radian(val)); |
|---|
| 634 | } |
|---|
| 635 | void applyDeltaValue(Real val) |
|---|
| 636 | { |
|---|
| 637 | setValue(mLight->getSpotlightOuterAngle().valueRadians() + val); |
|---|
| 638 | } |
|---|
| 639 | void setCurrentStateAsBaseValue(void) |
|---|
| 640 | { |
|---|
| 641 | setAsBaseValue(mLight->getSpotlightOuterAngle().valueRadians()); |
|---|
| 642 | } |
|---|
| 643 | |
|---|
| 644 | }; |
|---|
| 645 | //----------------------------------------------------------------------- |
|---|
| 646 | class LightSpotlightFalloffValue : public AnimableValue |
|---|
| 647 | { |
|---|
| 648 | protected: |
|---|
| 649 | Light* mLight; |
|---|
| 650 | public: |
|---|
| 651 | LightSpotlightFalloffValue(Light* l) :AnimableValue(REAL) |
|---|
| 652 | { mLight = l; } |
|---|
| 653 | void setValue(Real val) |
|---|
| 654 | { |
|---|
| 655 | mLight->setSpotlightFalloff(val); |
|---|
| 656 | } |
|---|
| 657 | void applyDeltaValue(Real val) |
|---|
| 658 | { |
|---|
| 659 | setValue(mLight->getSpotlightFalloff() + val); |
|---|
| 660 | } |
|---|
| 661 | void setCurrentStateAsBaseValue(void) |
|---|
| 662 | { |
|---|
| 663 | setAsBaseValue(mLight->getSpotlightFalloff()); |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | }; |
|---|
| 667 | //----------------------------------------------------------------------- |
|---|
| 668 | AnimableValuePtr Light::createAnimableValue(const String& valueName) |
|---|
| 669 | { |
|---|
| 670 | if (valueName == "diffuseColour") |
|---|
| 671 | { |
|---|
| 672 | return AnimableValuePtr( |
|---|
| 673 | new LightDiffuseColourValue(this)); |
|---|
| 674 | } |
|---|
| 675 | else if(valueName == "specularColour") |
|---|
| 676 | { |
|---|
| 677 | return AnimableValuePtr( |
|---|
| 678 | new LightSpecularColourValue(this)); |
|---|
| 679 | } |
|---|
| 680 | else if (valueName == "attenuation") |
|---|
| 681 | { |
|---|
| 682 | return AnimableValuePtr( |
|---|
| 683 | new LightAttenuationValue(this)); |
|---|
| 684 | } |
|---|
| 685 | else if (valueName == "spotlightInner") |
|---|
| 686 | { |
|---|
| 687 | return AnimableValuePtr( |
|---|
| 688 | new LightSpotlightInnerValue(this)); |
|---|
| 689 | } |
|---|
| 690 | else if (valueName == "spotlightOuter") |
|---|
| 691 | { |
|---|
| 692 | return AnimableValuePtr( |
|---|
| 693 | new LightSpotlightOuterValue(this)); |
|---|
| 694 | } |
|---|
| 695 | else if (valueName == "spotlightFalloff") |
|---|
| 696 | { |
|---|
| 697 | return AnimableValuePtr( |
|---|
| 698 | new LightSpotlightFalloffValue(this)); |
|---|
| 699 | } |
|---|
| 700 | else |
|---|
| 701 | { |
|---|
| 702 | return MovableObject::createAnimableValue(valueName); |
|---|
| 703 | } |
|---|
| 704 | } |
|---|
| 705 | //----------------------------------------------------------------------- |
|---|
| 706 | void Light::setCustomShadowCameraSetup(const ShadowCameraSetupPtr& customShadowSetup) |
|---|
| 707 | { |
|---|
| 708 | mCustomShadowCameraSetup = customShadowSetup; |
|---|
| 709 | } |
|---|
| 710 | //----------------------------------------------------------------------- |
|---|
| 711 | void Light::resetCustomShadowCameraSetup() |
|---|
| 712 | { |
|---|
| 713 | mCustomShadowCameraSetup.setNull(); |
|---|
| 714 | } |
|---|
| 715 | //----------------------------------------------------------------------- |
|---|
| 716 | const ShadowCameraSetupPtr& Light::getCustomShadowCameraSetup() const |
|---|
| 717 | { |
|---|
| 718 | return mCustomShadowCameraSetup; |
|---|
| 719 | } |
|---|
| 720 | //----------------------------------------------------------------------- |
|---|
| 721 | //----------------------------------------------------------------------- |
|---|
| 722 | String LightFactory::FACTORY_TYPE_NAME = "Light"; |
|---|
| 723 | //----------------------------------------------------------------------- |
|---|
| 724 | const String& LightFactory::getType(void) const |
|---|
| 725 | { |
|---|
| 726 | return FACTORY_TYPE_NAME; |
|---|
| 727 | } |
|---|
| 728 | //----------------------------------------------------------------------- |
|---|
| 729 | MovableObject* LightFactory::createInstanceImpl( const String& name, |
|---|
| 730 | const NameValuePairList* params) |
|---|
| 731 | { |
|---|
| 732 | |
|---|
| 733 | return new Light(name); |
|---|
| 734 | |
|---|
| 735 | } |
|---|
| 736 | //----------------------------------------------------------------------- |
|---|
| 737 | void LightFactory::destroyInstance( MovableObject* obj) |
|---|
| 738 | { |
|---|
| 739 | delete obj; |
|---|
| 740 | } |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | } // Namespace |
|---|