| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585 |
- //
- // Copyright (c) 2008-2014 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Camera.h"
- #include "Context.h"
- #include "Drawable.h"
- #include "Node.h"
- #include "DebugNew.h"
- namespace Urho3D
- {
- extern const char* SCENE_CATEGORY;
- static const float DEFAULT_NEARCLIP = 0.1f;
- static const float DEFAULT_FARCLIP = 1000.0f;
- static const float DEFAULT_FOV = 45.0f;
- static const float DEFAULT_ORTHOSIZE = 20.0f;
- static const char* fillModeNames[] =
- {
- "Solid",
- "Wireframe",
- "Point",
- 0
- };
- static const Matrix4 flipMatrix(
- 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, -1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- Camera::Camera(Context* context) :
- Component(context),
- viewDirty_(true),
- projectionDirty_(true),
- frustumDirty_(true),
- orthographic_(false),
- nearClip_(DEFAULT_NEARCLIP),
- farClip_(DEFAULT_FARCLIP),
- fov_(DEFAULT_FOV),
- orthoSize_(DEFAULT_ORTHOSIZE),
- aspectRatio_(1.0f),
- zoom_(1.0f),
- lodBias_(1.0f),
- viewMask_(DEFAULT_VIEWMASK),
- viewOverrideFlags_(VO_NONE),
- fillMode_(FILL_SOLID),
- projectionOffset_(Vector2::ZERO),
- reflectionPlane_(Plane::UP),
- clipPlane_(Plane::UP),
- autoAspectRatio_(true),
- flipVertical_(false),
- useReflection_(false),
- useClipping_(false)
- {
- reflectionMatrix_ = reflectionPlane_.ReflectionMatrix();
- }
- Camera::~Camera()
- {
- }
- void Camera::RegisterObject(Context* context)
- {
- context->RegisterFactory<Camera>(SCENE_CATEGORY);
- ACCESSOR_ATTRIBUTE(Camera, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Near Clip", GetNearClip, SetNearClip, float, DEFAULT_NEARCLIP, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Far Clip", GetFarClip, SetFarClip, float, DEFAULT_FARCLIP, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "FOV", GetFov, SetFov, float, DEFAULT_FOV, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Aspect Ratio", GetAspectRatio, SetAspectRatio, float, 1.0f, AM_DEFAULT);
- ENUM_ATTRIBUTE(Camera, "Fill Mode", fillMode_, fillModeNames, FILL_SOLID, AM_DEFAULT);
- ATTRIBUTE(Camera, VAR_BOOL, "Auto Aspect Ratio", autoAspectRatio_, true, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_BOOL, "Orthographic", IsOrthographic, SetOrthographic, bool, false, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Orthographic Size", GetOrthoSize, SetOrthoSize, float, DEFAULT_ORTHOSIZE, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "Zoom", GetZoom, SetZoom, float, 1.0f, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_FLOAT, "LOD Bias", GetLodBias, SetLodBias, float, 1.0f, AM_DEFAULT);
- ATTRIBUTE(Camera, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
- ATTRIBUTE(Camera, VAR_INT, "View Override Flags", viewOverrideFlags_, VO_NONE, AM_DEFAULT);
- REF_ACCESSOR_ATTRIBUTE(Camera, VAR_VECTOR2, "Projection Offset", GetProjectionOffset, SetProjectionOffset, Vector2, Vector2::ZERO, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_VECTOR4, "Reflection Plane", GetReflectionPlaneAttr, SetReflectionPlaneAttr, Vector4, Vector4(0.0f, 1.0f, 0.0f, 0.0f), AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_VECTOR4, "Clip Plane", GetClipPlaneAttr, SetClipPlaneAttr, Vector4, Vector4(0.0f, 1.0f, 0.0f, 0.0f), AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_BOOL, "Use Reflection", GetUseReflection, SetUseReflection, bool, false, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Camera, VAR_BOOL, "Use Clipping", GetUseClipping, SetUseClipping, bool, false, AM_DEFAULT);
- }
- void Camera::SetNearClip(float nearClip)
- {
- nearClip_ = Max(nearClip, M_MIN_NEARCLIP);
- frustumDirty_ = true;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetFarClip(float farClip)
- {
- farClip_ = Max(farClip, M_MIN_NEARCLIP);
- frustumDirty_ = true;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetFov(float fov)
- {
- fov_ = Clamp(fov, 0.0f, M_MAX_FOV);
- frustumDirty_ = true;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetOrthoSize(float orthoSize)
- {
- orthoSize_ = orthoSize;
- aspectRatio_ = 1.0f;
- frustumDirty_ = true;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetOrthoSize(const Vector2& orthoSize)
- {
- orthoSize_ = orthoSize.y_;
- aspectRatio_ = orthoSize.x_ / orthoSize.y_;
- frustumDirty_ = true;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetAspectRatio(float aspectRatio)
- {
- aspectRatio_ = aspectRatio;
- frustumDirty_ = true;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetZoom(float zoom)
- {
- zoom_ = Max(zoom, M_EPSILON);
- frustumDirty_ = true;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetLodBias(float bias)
- {
- lodBias_ = Max(bias, M_EPSILON);
- MarkNetworkUpdate();
- }
- void Camera::SetViewMask(unsigned mask)
- {
- viewMask_ = mask;
- MarkNetworkUpdate();
- }
- void Camera::SetViewOverrideFlags(unsigned flags)
- {
- viewOverrideFlags_ = flags;
- MarkNetworkUpdate();
- }
- void Camera::SetFillMode(FillMode mode)
- {
- fillMode_ = mode;
- MarkNetworkUpdate();
- }
- void Camera::SetOrthographic(bool enable)
- {
- orthographic_ = enable;
- frustumDirty_ = true;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetAutoAspectRatio(bool enable)
- {
- autoAspectRatio_ = enable;
- MarkNetworkUpdate();
- }
- void Camera::SetProjectionOffset(const Vector2& offset)
- {
- projectionOffset_ = offset;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetUseReflection(bool enable)
- {
- useReflection_ = enable;
- viewDirty_ = true;
- frustumDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetReflectionPlane(const Plane& plane)
- {
- reflectionPlane_ = plane;
- reflectionMatrix_ = reflectionPlane_.ReflectionMatrix();
- viewDirty_ = true;
- frustumDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetUseClipping(bool enable)
- {
- useClipping_ = enable;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetClipPlane(const Plane& plane)
- {
- clipPlane_ = plane;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- void Camera::SetFlipVertical(bool enable)
- {
- flipVertical_ = enable;
- projectionDirty_ = true;
- MarkNetworkUpdate();
- }
- float Camera::GetNearClip() const
- {
- // Orthographic camera has always near clip at 0 to avoid trouble with shader depth parameters,
- // and unlike in perspective mode there should be no depth buffer precision issue
- if (!orthographic_)
- return nearClip_;
- else
- return 0.0f;
- }
- Frustum Camera::GetSplitFrustum(float nearClip, float farClip) const
- {
- Frustum ret;
-
- Matrix3x4 worldTransform = GetEffectiveWorldTransform();
- nearClip = Max(nearClip, GetNearClip());
- farClip = Min(farClip, farClip_);
- if (farClip < nearClip)
- farClip = nearClip;
- if (!orthographic_)
- ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip, worldTransform);
- else
- ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip, worldTransform);
- return ret;
- }
- Frustum Camera::GetViewSpaceFrustum() const
- {
- Frustum ret;
- if (!orthographic_)
- ret.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_);
- else
- ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_);
- return ret;
- }
- Frustum Camera::GetViewSpaceSplitFrustum(float nearClip, float farClip) const
- {
- Frustum ret;
- nearClip = Max(nearClip, GetNearClip());
- farClip = Min(farClip, farClip_);
- if (farClip < nearClip)
- farClip = nearClip;
- if (!orthographic_)
- ret.Define(fov_, aspectRatio_, zoom_, nearClip, farClip);
- else
- ret.DefineOrtho(orthoSize_, aspectRatio_, zoom_, nearClip, farClip);
- return ret;
- }
- Ray Camera::GetScreenRay(float x, float y) const
- {
- Ray ret;
- // If projection is invalid, just return a ray pointing forward
- if (!IsProjectionValid())
- {
- ret.origin_ = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
- ret.direction_ = node_ ? node_->GetWorldDirection() : Vector3::FORWARD;
- return ret;
- }
- Matrix4 viewProjInverse = (GetProjection(false) * GetView()).Inverse();
- // The parameters range from 0.0 to 1.0. Expand to normalized device coordinates (-1.0 to 1.0) & flip Y axis
- x = 2.0f * x - 1.0f;
- y = 1.0f - 2.0f * y;
- Vector3 near(x, y, 0.0f);
- Vector3 far(x, y, 1.0f);
- ret.origin_ = viewProjInverse * near;
- ret.direction_ = ((viewProjInverse * far) - ret.origin_).Normalized();
- return ret;
- }
- Vector2 Camera::WorldToScreenPoint(const Vector3& worldPos) const
- {
- Vector3 eyeSpacePos = GetView() * worldPos;
- Vector2 ret;
- if(eyeSpacePos.z_ > 0.0f)
- {
- Vector3 screenSpacePos = GetProjection(false) * eyeSpacePos;
- ret.x_ = screenSpacePos.x_;
- ret.y_ = screenSpacePos.y_;
- }
- else
- {
- ret.x_ = (-eyeSpacePos.x_ > 0.0f) ? -1.0f : 1.0f;
- ret.y_ = (-eyeSpacePos.y_ > 0.0f) ? -1.0f : 1.0f;
- }
- ret.x_ = (ret.x_ / 2.0f) + 0.5f;
- ret.y_ = 1.0f - ((ret.y_ / 2.0f) + 0.5f);
- return ret;
- }
- Vector3 Camera::ScreenToWorldPoint(const Vector3& screenPos) const
- {
- Ray ray = GetScreenRay(screenPos.x_, screenPos.y_);
- return ray.origin_ + ray.direction_ * screenPos.z_;
- }
- const Frustum& Camera::GetFrustum() const
- {
- if (frustumDirty_)
- {
- Matrix3x4 worldTransform = GetEffectiveWorldTransform();
-
- if (!orthographic_)
- frustum_.Define(fov_, aspectRatio_, zoom_, GetNearClip(), farClip_, worldTransform);
- else
- frustum_.DefineOrtho(orthoSize_, aspectRatio_, zoom_, GetNearClip(), farClip_, worldTransform);
- frustumDirty_ = false;
- }
- return frustum_;
- }
- const Matrix4& Camera::GetProjection() const
- {
- if (projectionDirty_)
- {
- projection_ = GetProjection(true);
- projectionDirty_ = false;
- }
- return projection_;
- }
- Matrix4 Camera::GetProjection(bool apiSpecific) const
- {
- Matrix4 ret(Matrix4::ZERO);
- // Whether to construct matrix using OpenGL or Direct3D clip space convention
- #ifdef USE_OPENGL
- bool openGLFormat = apiSpecific;
- #else
- bool openGLFormat = false;
- #endif
- if (!orthographic_)
- {
- float nearClip = GetNearClip();
- float h = (1.0f / tanf(fov_ * M_DEGTORAD * 0.5f)) * zoom_;
- float w = h / aspectRatio_;
- float q, r;
- if (openGLFormat)
- {
- q = (farClip_ + nearClip) / (farClip_ - nearClip);
- r = -2.0f * farClip_ * nearClip / (farClip_ - nearClip);
- }
- else
- {
- q = farClip_ / (farClip_ - nearClip);
- r = -q * nearClip;
- }
- ret.m00_ = w;
- ret.m02_ = projectionOffset_.x_ * 2.0f;
- ret.m11_ = h;
- ret.m12_ = projectionOffset_.y_ * 2.0f;
- ret.m22_ = q;
- ret.m23_ = r;
- ret.m32_ = 1.0f;
- }
- else
- {
- // Disregard near clip, because it does not affect depth precision as with perspective projection
- float h = (1.0f / (orthoSize_ * 0.5f)) * zoom_;
- float w = h / aspectRatio_;
- float q, r;
- if (openGLFormat)
- {
- q = 2.0f / farClip_;
- r = -1.0f;
- }
- else
- {
- q = 1.0f / farClip_;
- r = 0.0f;
- }
- ret.m00_ = w;
- ret.m03_ = projectionOffset_.x_ * 2.0f;
- ret.m11_ = h;
- ret.m13_ = projectionOffset_.y_ * 2.0f;
- ret.m22_ = q;
- ret.m23_ = r;
- ret.m33_ = 1.0f;
- }
- if (flipVertical_)
- ret = flipMatrix * ret;
- return ret;
- }
- void Camera::GetFrustumSize(Vector3& near, Vector3& far) const
- {
- near.z_ = GetNearClip();
- far.z_ = farClip_;
- if (!orthographic_)
- {
- float halfViewSize = tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
- near.y_ = near.z_ * halfViewSize;
- near.x_ = near.y_ * aspectRatio_;
- far.y_ = far.z_ * halfViewSize;
- far.x_ = far.y_ * aspectRatio_;
- }
- else
- {
- float halfViewSize = orthoSize_ * 0.5f / zoom_;
- near.y_ = far.y_ = halfViewSize;
- near.x_ = far.x_ = near.y_ * aspectRatio_;
- }
- if (flipVertical_)
- {
- near.y_ = -near.y_;
- far.y_ = -far.y_;
- }
- }
- float Camera::GetHalfViewSize() const
- {
- if (!orthographic_)
- return tanf(fov_ * M_DEGTORAD * 0.5f) / zoom_;
- else
- return orthoSize_ * 0.5f / zoom_;
- }
- float Camera::GetDistance(const Vector3& worldPos) const
- {
- if (!orthographic_)
- {
- const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
- return (worldPos - cameraPos).Length();
- }
- else
- return Abs((GetView() * worldPos).z_);
- }
- float Camera::GetDistanceSquared(const Vector3& worldPos) const
- {
- if (!orthographic_)
- {
- const Vector3& cameraPos = node_ ? node_->GetWorldPosition() : Vector3::ZERO;
- return (worldPos - cameraPos).LengthSquared();
- }
- else
- {
- float distance = (GetView() * worldPos).z_;
- return distance * distance;
- }
- }
- float Camera::GetLodDistance(float distance, float scale, float bias) const
- {
- float d = Max(lodBias_ * bias * scale * zoom_, M_EPSILON);
- if (!orthographic_)
- return distance / d;
- else
- return orthoSize_ / d;
- }
- Matrix3x4 Camera::GetEffectiveWorldTransform() const
- {
- Matrix3x4 worldTransform = node_ ? Matrix3x4(node_->GetWorldPosition(), node_->GetWorldRotation(), 1.0f) : Matrix3x4::IDENTITY;
- return useReflection_ ? reflectionMatrix_ * worldTransform : worldTransform;
- }
- bool Camera::IsProjectionValid() const
- {
- return farClip_ > GetNearClip();
- }
- const Matrix3x4& Camera::GetView() const
- {
- if (viewDirty_)
- {
- // Note: view matrix is unaffected by node or parent scale
- view_ = GetEffectiveWorldTransform().Inverse();
- viewDirty_ = false;
- }
-
- return view_;
- }
- void Camera::SetReflectionPlaneAttr(Vector4 value)
- {
- SetReflectionPlane(Plane(value));
- }
- void Camera::SetClipPlaneAttr(Vector4 value)
- {
- SetClipPlane(Plane(value));
- }
- Vector4 Camera::GetReflectionPlaneAttr() const
- {
- return reflectionPlane_.ToVector4();
- }
- Vector4 Camera::GetClipPlaneAttr() const
- {
- return clipPlane_.ToVector4();
- }
- void Camera::OnNodeSet(Node* node)
- {
- if (node)
- node->AddListener(this);
- }
- void Camera::OnMarkedDirty(Node* node)
- {
- frustumDirty_ = true;
- viewDirty_ = true;
- }
- }
|