]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/nullmodel.cpp
Merge branch 'transfilterfix' into 'master'
[xonotic/netradiant.git] / radiant / nullmodel.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #include "nullmodel.h"
23
24 #include "debugging/debugging.h"
25
26 #include "iscenegraph.h"
27 #include "irender.h"
28 #include "iselection.h"
29 #include "iundo.h"
30 #include "ientity.h"
31 #include "ireference.h"
32 #include "igl.h"
33 #include "cullable.h"
34 #include "renderable.h"
35 #include "selectable.h"
36
37 #include "math/frustum.h"
38 #include "scenelib.h"
39 #include "instancelib.h"
40 #include "entitylib.h"
41
42 class NullModel :
43         public Bounded,
44         public Cullable {
45     Shader *m_state;
46     AABB m_aabb_local;
47     RenderableSolidAABB m_aabb_solid;
48     RenderableWireframeAABB m_aabb_wire;
49 public:
50     NullModel() : m_aabb_local(Vector3(0, 0, 0), Vector3(8, 8, 8)), m_aabb_solid(m_aabb_local),
51                   m_aabb_wire(m_aabb_local)
52     {
53         m_state = GlobalShaderCache().capture("");
54     }
55
56     ~NullModel()
57     {
58         GlobalShaderCache().release("");
59     }
60
61     VolumeIntersectionValue intersectVolume(const VolumeTest &volume, const Matrix4 &localToWorld) const
62     {
63         return volume.TestAABB(m_aabb_local, localToWorld);
64     }
65
66     const AABB &localAABB() const
67     {
68         return m_aabb_local;
69     }
70
71     void renderSolid(Renderer &renderer, const VolumeTest &volume, const Matrix4 &localToWorld) const
72     {
73         renderer.SetState(m_state, Renderer::eFullMaterials);
74         renderer.addRenderable(m_aabb_solid, localToWorld);
75     }
76
77     void renderWireframe(Renderer &renderer, const VolumeTest &volume, const Matrix4 &localToWorld) const
78     {
79         renderer.addRenderable(m_aabb_wire, localToWorld);
80     }
81
82     void testSelect(Selector &selector, SelectionTest &test, const Matrix4 &localToWorld)
83     {
84         test.BeginMesh(localToWorld);
85
86         SelectionIntersection best;
87         aabb_testselect(m_aabb_local, test, best);
88         if (best.valid()) {
89             selector.addIntersection(best);
90         }
91     }
92 };
93
94 class NullModelInstance : public scene::Instance, public Renderable, public SelectionTestable {
95     class TypeCasts {
96         InstanceTypeCastTable m_casts;
97     public:
98         TypeCasts()
99         {
100             InstanceContainedCast<NullModelInstance, Bounded>::install(m_casts);
101             InstanceContainedCast<NullModelInstance, Cullable>::install(m_casts);
102             InstanceStaticCast<NullModelInstance, Renderable>::install(m_casts);
103             InstanceStaticCast<NullModelInstance, SelectionTestable>::install(m_casts);
104         }
105
106         InstanceTypeCastTable &get()
107         {
108             return m_casts;
109         }
110     };
111
112     NullModel &m_nullmodel;
113 public:
114
115     typedef LazyStatic<TypeCasts> StaticTypeCasts;
116
117     Bounded &get(NullType<Bounded>)
118     {
119         return m_nullmodel;
120     }
121
122     Cullable &get(NullType<Cullable>)
123     {
124         return m_nullmodel;
125     }
126
127     NullModelInstance(const scene::Path &path, scene::Instance *parent, NullModel &nullmodel) :
128             Instance(path, parent, this, StaticTypeCasts::instance().get()),
129             m_nullmodel(nullmodel)
130     {
131     }
132
133     void renderSolid(Renderer &renderer, const VolumeTest &volume) const
134     {
135         m_nullmodel.renderSolid(renderer, volume, Instance::localToWorld());
136     }
137
138     void renderWireframe(Renderer &renderer, const VolumeTest &volume) const
139     {
140         m_nullmodel.renderWireframe(renderer, volume, Instance::localToWorld());
141     }
142
143     void testSelect(Selector &selector, SelectionTest &test)
144     {
145         m_nullmodel.testSelect(selector, test, Instance::localToWorld());
146     }
147 };
148
149 class NullModelNode : public scene::Node::Symbiot, public scene::Instantiable {
150     class TypeCasts {
151         NodeTypeCastTable m_casts;
152     public:
153         TypeCasts()
154         {
155             NodeStaticCast<NullModelNode, scene::Instantiable>::install(m_casts);
156         }
157
158         NodeTypeCastTable &get()
159         {
160             return m_casts;
161         }
162     };
163
164
165     scene::Node m_node;
166     InstanceSet m_instances;
167     NullModel m_nullmodel;
168 public:
169
170     typedef LazyStatic<TypeCasts> StaticTypeCasts;
171
172     NullModelNode() : m_node(this, this, StaticTypeCasts::instance().get())
173     {
174         m_node.m_isRoot = true;
175     }
176
177     void release()
178     {
179         delete this;
180     }
181
182     scene::Node &node()
183     {
184         return m_node;
185     }
186
187     scene::Instance *create(const scene::Path &path, scene::Instance *parent)
188     {
189         return new NullModelInstance(path, parent, m_nullmodel);
190     }
191
192     void forEachInstance(const scene::Instantiable::Visitor &visitor)
193     {
194         m_instances.forEachInstance(visitor);
195     }
196
197     void insert(scene::Instantiable::Observer *observer, const scene::Path &path, scene::Instance *instance)
198     {
199         m_instances.insert(observer, path, instance);
200     }
201
202     scene::Instance *erase(scene::Instantiable::Observer *observer, const scene::Path &path)
203     {
204         return m_instances.erase(observer, path);
205     }
206 };
207
208 NodeSmartReference NewNullModel()
209 {
210     return NodeSmartReference((new NullModelNode)->node());
211 }
212
213 void NullModel_construct()
214 {
215 }
216
217 void NullModel_destroy()
218 {
219 }