2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_RENDERER_H)
23 #define INCLUDED_RENDERER_H
26 #include "renderable.h"
27 #include "iselection.h"
30 #include "math/frustum.h"
33 inline Cullable* Instance_getCullable(scene::Instance& instance)
35 return InstanceTypeCast<Cullable>::cast(instance);
38 inline Renderable* Instance_getRenderable(scene::Instance& instance)
40 return InstanceTypeCast<Renderable>::cast(instance);
43 inline VolumeIntersectionValue Cullable_testVisible(scene::Instance& instance, const VolumeTest& volume, VolumeIntersectionValue parentVisible)
45 if(parentVisible == c_volumePartial)
47 Cullable* cullable = Instance_getCullable(instance);
50 return cullable->intersectVolume(volume, instance.localToWorld());
56 template<typename _Walker>
59 const VolumeTest& m_volume;
60 const _Walker& m_walker;
62 CullingWalker(const VolumeTest& volume, const _Walker& walker)
63 : m_volume(volume), m_walker(walker)
66 bool pre(const scene::Path& path, scene::Instance& instance, VolumeIntersectionValue parentVisible) const
68 VolumeIntersectionValue visible = Cullable_testVisible(instance, m_volume, parentVisible);
69 if(visible != c_volumeOutside)
71 return m_walker.pre(path, instance);
75 void post(const scene::Path& path, scene::Instance& instance, VolumeIntersectionValue parentVisible) const
77 return m_walker.post(path, instance);
81 template<typename Walker_>
82 class ForEachVisible : public scene::Graph::Walker
84 const VolumeTest& m_volume;
85 const Walker_& m_walker;
86 mutable std::vector<VolumeIntersectionValue> m_state;
88 ForEachVisible(const VolumeTest& volume, const Walker_& walker)
89 : m_volume(volume), m_walker(walker)
91 m_state.push_back(c_volumePartial);
93 bool pre(const scene::Path& path, scene::Instance& instance) const
95 VolumeIntersectionValue visible = (path.top().get().visible()) ? m_state.back() : c_volumeOutside;
97 if(visible == c_volumePartial)
99 visible = m_volume.TestAABB(instance.worldAABB());
102 m_state.push_back(visible);
104 if(visible == c_volumeOutside)
110 return m_walker.pre(path, instance, m_state.back());
113 void post(const scene::Path& path, scene::Instance& instance) const
115 if(m_state.back() != c_volumeOutside)
117 m_walker.post(path, instance, m_state.back());
124 template<typename Functor>
125 inline void Scene_forEachVisible(scene::Graph& graph, const VolumeTest& volume, const Functor& functor)
127 graph.traverse(ForEachVisible< CullingWalker<Functor> >(volume, CullingWalker<Functor>(volume, functor)));
130 class RenderHighlighted
132 Renderer& m_renderer;
133 const VolumeTest& m_volume;
135 RenderHighlighted(Renderer& renderer, const VolumeTest& volume)
136 : m_renderer(renderer), m_volume(volume)
139 void render(const Renderable& renderable) const
141 switch(m_renderer.getStyle())
143 case Renderer::eFullMaterials:
144 renderable.renderSolid(m_renderer, m_volume);
146 case Renderer::eWireframeOnly:
147 renderable.renderWireframe(m_renderer, m_volume);
151 typedef ConstMemberCaller1<RenderHighlighted, const Renderable&, &RenderHighlighted::render> RenderCaller;
153 bool pre(const scene::Path& path, scene::Instance& instance, VolumeIntersectionValue parentVisible) const
155 m_renderer.PushState();
157 if(Cullable_testVisible(instance, m_volume, parentVisible) != c_volumeOutside)
159 Renderable* renderable = Instance_getRenderable(instance);
162 renderable->viewChanged();
165 Selectable* selectable = Instance_getSelectable(instance);
166 if(selectable != 0 && selectable->isSelected())
168 if(GlobalSelectionSystem().Mode() != SelectionSystem::eComponent)
170 m_renderer.Highlight(Renderer::eFace);
174 renderable->renderComponents(m_renderer, m_volume);
176 m_renderer.Highlight(Renderer::ePrimitive);
187 void post(const scene::Path& path, scene::Instance& instance, VolumeIntersectionValue parentVisible) const
189 m_renderer.PopState();
193 inline void Scene_Render(Renderer& renderer, const VolumeTest& volume)
195 GlobalSceneGraph().traverse(ForEachVisible<RenderHighlighted>(volume, RenderHighlighted(renderer, volume)));
196 GlobalShaderCache().forEachRenderable(RenderHighlighted::RenderCaller(RenderHighlighted(renderer, volume)));