| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- //
- // 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 "DebugRenderer.h"
- #include "Log.h"
- #include "Material.h"
- #include "Octree.h"
- #include "Renderer.h"
- #include "Scene.h"
- #include "Sort.h"
- #include "Zone.h"
- #include "DebugNew.h"
- namespace Urho3D
- {
- const char* GEOMETRY_CATEGORY = "Geometry";
- SourceBatch::SourceBatch() :
- distance_(0.0f),
- geometry_(0),
- worldTransform_(&Matrix3x4::IDENTITY),
- numWorldTransforms_(1),
- geometryType_(GEOM_STATIC),
- overrideView_(false)
- {
- }
- SourceBatch::~SourceBatch()
- {
- }
- Drawable::Drawable(Context* context, unsigned char drawableFlags) :
- Component(context),
- drawableFlags_(drawableFlags),
- worldBoundingBoxDirty_(true),
- castShadows_(false),
- occluder_(false),
- occludee_(true),
- updateQueued_(false),
- viewMask_(DEFAULT_VIEWMASK),
- lightMask_(DEFAULT_LIGHTMASK),
- shadowMask_(DEFAULT_SHADOWMASK),
- zoneMask_(DEFAULT_ZONEMASK),
- viewFrameNumber_(0),
- distance_(0.0f),
- lodDistance_(0.0f),
- drawDistance_(0.0f),
- shadowDistance_(0.0f),
- sortValue_(0.0f),
- minZ_(0.0f),
- maxZ_(0.0f),
- lodBias_(1.0f),
- basePassFlags_(0),
- maxLights_(0),
- octant_(0),
- firstLight_(0),
- zone_(0),
- zoneDirty_(false)
- {
- }
- Drawable::~Drawable()
- {
- RemoveFromOctree();
- }
- void Drawable::RegisterObject(Context* context)
- {
- ATTRIBUTE(Drawable, VAR_INT, "Max Lights", maxLights_, 0, AM_DEFAULT);
- ATTRIBUTE(Drawable, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
- ATTRIBUTE(Drawable, VAR_INT, "Light Mask", lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
- ATTRIBUTE(Drawable, VAR_INT, "Shadow Mask", shadowMask_, DEFAULT_SHADOWMASK, AM_DEFAULT);
- ACCESSOR_ATTRIBUTE(Drawable, VAR_INT, "Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
- }
- void Drawable::OnSetEnabled()
- {
- bool enabled = IsEnabledEffective();
- if (enabled && !octant_)
- AddToOctree();
- else if (!enabled && octant_)
- RemoveFromOctree();
- }
- void Drawable::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
- {
- float distance = query.ray_.HitDistance(GetWorldBoundingBox());
- if (distance < query.maxDistance_)
- {
- RayQueryResult result;
- result.position_ = query.ray_.origin_ + distance * query.ray_.direction_;
- result.normal_ = -query.ray_.direction_;
- result.distance_ = distance;
- result.drawable_ = this;
- result.node_ = GetNode();
- result.subObject_ = M_MAX_UNSIGNED;
- results.Push(result);
- }
- }
- void Drawable::Update(const FrameInfo& frame)
- {
- }
- void Drawable::UpdateBatches(const FrameInfo& frame)
- {
- const BoundingBox& worldBoundingBox = GetWorldBoundingBox();
- const Matrix3x4& worldTransform = node_->GetWorldTransform();
- distance_ = frame.camera_->GetDistance(worldBoundingBox.Center());
- for (unsigned i = 0; i < batches_.Size(); ++i)
- {
- batches_[i].distance_ = distance_;
- batches_[i].worldTransform_ = &worldTransform;
- }
- float scale = worldBoundingBox.Size().DotProduct(DOT_SCALE);
- float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
- if (newLodDistance != lodDistance_)
- lodDistance_ = newLodDistance;
- }
- void Drawable::UpdateGeometry(const FrameInfo& frame)
- {
- }
- Geometry* Drawable::GetLodGeometry(unsigned batchIndex, unsigned level)
- {
- // By default return the visible batch geometry
- if (batchIndex < batches_.Size())
- return batches_[batchIndex].geometry_;
- else
- return 0;
- }
- bool Drawable::DrawOcclusion(OcclusionBuffer* buffer)
- {
- return true;
- }
- void Drawable::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
- {
- if (debug && IsEnabledEffective())
- debug->AddBoundingBox(GetWorldBoundingBox(), Color::GREEN, depthTest);
- }
- void Drawable::SetDrawDistance(float distance)
- {
- drawDistance_ = distance;
- MarkNetworkUpdate();
- }
- void Drawable::SetShadowDistance(float distance)
- {
- shadowDistance_ = distance;
- MarkNetworkUpdate();
- }
- void Drawable::SetLodBias(float bias)
- {
- lodBias_ = Max(bias, M_EPSILON);
- MarkNetworkUpdate();
- }
- void Drawable::SetViewMask(unsigned mask)
- {
- viewMask_ = mask;
- MarkNetworkUpdate();
- }
- void Drawable::SetLightMask(unsigned mask)
- {
- lightMask_ = mask;
- MarkNetworkUpdate();
- }
- void Drawable::SetShadowMask(unsigned mask)
- {
- shadowMask_ = mask;
- MarkNetworkUpdate();
- }
- void Drawable::SetZoneMask(unsigned mask)
- {
- zoneMask_ = mask;
- // Mark dirty to reset cached zone
- OnMarkedDirty(node_);
- MarkNetworkUpdate();
- }
- void Drawable::SetMaxLights(unsigned num)
- {
- maxLights_ = num;
- MarkNetworkUpdate();
- }
- void Drawable::SetCastShadows(bool enable)
- {
- castShadows_ = enable;
- MarkNetworkUpdate();
- }
- void Drawable::SetOccluder(bool enable)
- {
- occluder_ = enable;
- MarkNetworkUpdate();
- }
- void Drawable::SetOccludee(bool enable)
- {
- if (enable != occludee_)
- {
- occludee_ = enable;
- // Reinsert to octree to make sure octant occlusion does not erroneously hide this drawable
- if (octant_ && !updateQueued_)
- octant_->GetRoot()->QueueUpdate(this);
- MarkNetworkUpdate();
- }
- }
- void Drawable::SetShaderParameter(const String& name, const Variant& value)
- {
- MaterialShaderParameter newParam;
- newParam.name_ = name;
- newParam.value_ = value;
- StringHash nameHash(name);
- shaderParameters_[nameHash] = newParam;
- }
- const Variant& Drawable::GetShaderParameter(const String& name) const
- {
- HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = shaderParameters_.Find(name);
- return i != shaderParameters_.End() ? i->second_.value_ : Variant::EMPTY;
- }
- void Drawable::RemoveShaderParameter(const String& name)
- {
- StringHash nameHash(name);
- shaderParameters_.Erase(nameHash);
- }
- void Drawable::MarkForUpdate()
- {
- if (!updateQueued_ && octant_)
- octant_->GetRoot()->QueueUpdate(this);
- }
- const BoundingBox& Drawable::GetWorldBoundingBox()
- {
- if (worldBoundingBoxDirty_)
- {
- OnWorldBoundingBoxUpdate();
- worldBoundingBoxDirty_ = false;
- }
- return worldBoundingBox_;
- }
- bool Drawable::IsInView() const
- {
- // Note: in headless mode there is no renderer subsystem and no view frustum tests are performed, so return
- // always false in that case
- Renderer* renderer = GetSubsystem<Renderer>();
- return renderer && viewFrameNumber_ == renderer->GetFrameInfo().frameNumber_ && !viewCameras_.Empty();
- }
- bool Drawable::IsInView(Camera* camera) const
- {
- Renderer* renderer = GetSubsystem<Renderer>();
- return renderer && viewFrameNumber_ == renderer->GetFrameInfo().frameNumber_ && (!camera || viewCameras_.Contains(camera));
- }
- bool Drawable::IsInView(const FrameInfo& frame, bool anyCamera) const
- {
- return viewFrameNumber_ == frame.frameNumber_ && (anyCamera || viewCameras_.Contains(frame.camera_));
- }
- void Drawable::SetZone(Zone* zone, bool temporary)
- {
- zone_ = zone;
- // If the zone assignment was temporary (inconclusive) set the dirty flag so that it will be re-evaluated on the next frame
- zoneDirty_ = temporary;
- }
- void Drawable::SetSortValue(float value)
- {
- sortValue_ = value;
- }
- void Drawable::SetMinMaxZ(float minZ, float maxZ)
- {
- minZ_ = minZ;
- maxZ_ = maxZ;
- }
- void Drawable::MarkInView(const FrameInfo& frame)
- {
- if (frame.frameNumber_ != viewFrameNumber_)
- {
- viewFrameNumber_ = frame.frameNumber_;
- viewCameras_.Clear();
- }
-
- viewCameras_.Insert(frame.camera_);
- }
- void Drawable::MarkInView(unsigned frameNumber, Camera* camera)
- {
- if (frameNumber != viewFrameNumber_)
- {
- viewFrameNumber_ = frameNumber;
- viewCameras_.Clear();
- }
-
- if (camera)
- viewCameras_.Insert(camera);
- }
- void Drawable::LimitLights()
- {
- // Maximum lights value 0 means unlimited
- if (!maxLights_ || lights_.Size() <= maxLights_)
- return;
- // If more lights than allowed, move to vertex lights and cut the list
- const BoundingBox& box = GetWorldBoundingBox();
- for (unsigned i = 0; i < lights_.Size(); ++i)
- lights_[i]->SetIntensitySortValue(box);
- Sort(lights_.Begin(), lights_.End(), CompareDrawables);
- vertexLights_.Insert(vertexLights_.End(), lights_.Begin() + maxLights_, lights_.End());
- lights_.Resize(maxLights_);
- }
- void Drawable::LimitVertexLights()
- {
- if (vertexLights_.Size() <= MAX_VERTEX_LIGHTS)
- return;
- const BoundingBox& box = GetWorldBoundingBox();
- for (unsigned i = vertexLights_.Size() - 1; i < vertexLights_.Size(); --i)
- vertexLights_[i]->SetIntensitySortValue(box);
- Sort(vertexLights_.Begin(), vertexLights_.End(), CompareDrawables);
- vertexLights_.Resize(MAX_VERTEX_LIGHTS);
- }
- void Drawable::OnNodeSet(Node* node)
- {
- if (node)
- {
- AddToOctree();
- node->AddListener(this);
- }
- else
- RemoveFromOctree();
- }
- void Drawable::OnMarkedDirty(Node* node)
- {
- worldBoundingBoxDirty_ = true;
- if (!updateQueued_ && octant_)
- octant_->GetRoot()->QueueUpdate(this);
- // Mark zone assignment dirty when transform changes
- if (node == node_)
- zoneDirty_ = true;
- }
- void Drawable::AddToOctree()
- {
- // Do not add to octree when disabled
- if (!IsEnabledEffective())
- return;
- Scene* scene = GetScene();
- if (scene)
- {
- Octree* octree = scene->GetComponent<Octree>();
- if (octree)
- octree->InsertDrawable(this);
- else
- LOGERROR("No Octree component in scene, drawable will not render");
- }
- else
- {
- // We have a mechanism for adding detached nodes to an octree manually, so do not log this error
- //LOGERROR("Node is detached from scene, drawable will not render");
- }
- }
- void Drawable::RemoveFromOctree()
- {
- if (octant_)
- {
- Octree* octree = octant_->GetRoot();
- if (updateQueued_)
- octree->CancelUpdate(this);
-
- // Perform subclass specific deinitialization if necessary
- OnRemoveFromOctree();
-
- octant_->RemoveDrawable(this);
- }
- }
- }
|