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_PATCH_H)
23 #define INCLUDED_PATCH_H
26 /// \brief The patch primitive.
28 /// A 2-dimensional matrix of vertices that define a quadratic bezier surface.
29 /// The Boundary-Representation of this primitive is a triangle mesh.
30 /// The surface is recursively tesselated until the angle between each triangle
31 /// edge is smaller than a specified tolerance.
39 #include "renderable.h"
41 #include "selectable.h"
43 #include "debugging/debugging.h"
48 #include "math/frustum.h"
49 #include "string/string.h"
50 #include "stream/stringstream.h"
51 #include "stream/textstream.h"
52 #include "xml/xmlelement.h"
54 #include "transformlib.h"
55 #include "instancelib.h"
56 #include "selectionlib.h"
57 #include "traverselib.h"
60 #include "shaderlib.h"
61 #include "generic/callback.h"
62 #include "signal/signalfwd.h"
63 #include "texturelib.h"
65 #include "dragplanes.h"
73 extern int g_PatchSubdivideThreshold;
76 #define MIN_PATCH_WIDTH 3
77 #define MIN_PATCH_HEIGHT 3
79 extern std::size_t MAX_PATCH_WIDTH;
80 extern std::size_t MAX_PATCH_HEIGHT;
82 #define MAX_PATCH_ROWCTRL (((MAX_PATCH_WIDTH-1)-1)/2)
83 #define MAX_PATCH_COLCTRL (((MAX_PATCH_HEIGHT-1)-1)/2)
119 const std::size_t BEZIERCURVETREE_MAX_INDEX = std::size_t(1) << (std::numeric_limits<std::size_t>::digits - 1);
121 struct BezierCurveTree
124 BezierCurveTree* left;
125 BezierCurveTree* right;
128 inline bool BezierCurveTree_isLeaf(const BezierCurveTree* node)
130 return node->left == 0 && node->right == 0;
133 void BezierCurveTree_Delete(BezierCurveTree *pCurve);
136 inline VertexPointer vertexpointer_arbitrarymeshvertex(const ArbitraryMeshVertex* array)
138 return VertexPointer(VertexPointer::pointer(&array->vertex), sizeof(ArbitraryMeshVertex));
141 typedef PatchControl* PatchControlIter;
142 typedef const PatchControl* PatchControlConstIter;
144 inline void copy_ctrl(PatchControlIter ctrl, PatchControlConstIter begin, PatchControlConstIter end)
146 std::copy(begin, end, ctrl);
149 const Colour4b colour_corner(0, 255, 0, 255);
150 const Colour4b colour_inside(255, 0, 255, 255);
157 virtual bool filter(const Patch& patch) const = 0;
160 bool patch_filtered(Patch& patch);
161 void add_patch_filter(PatchFilter& filter, int mask, bool invert = false);
163 void Patch_addTextureChangedCallback(const SignalHandler& handler);
164 void Patch_textureChanged();
166 inline void BezierCurveTreeArray_deleteAll(Array<BezierCurveTree*>& curveTrees)
168 for(Array<BezierCurveTree*>::iterator i = curveTrees.begin(); i != curveTrees.end(); ++i)
170 BezierCurveTree_Delete(*i);
174 inline void PatchControlArray_invert(Array<PatchControl>& ctrl, std::size_t width, std::size_t height)
176 Array<PatchControl> tmp(width);
178 PatchControlIter from = ctrl.data() + (width * (height - 1));
179 PatchControlIter to = ctrl.data();
180 for(std::size_t h = 0; h != ((height - 1) >> 1); ++h, to += width, from -= width)
182 copy_ctrl(tmp.data(), to, to + width);
183 copy_ctrl(to, from, from + width);
184 copy_ctrl(from, tmp.data(), tmp.data() + width);
188 class PatchTesselation
192 : m_numStrips(0), m_lenStrips(0), m_nArrayWidth(0), m_nArrayHeight(0)
195 Array<ArbitraryMeshVertex> m_vertices;
196 Array<RenderIndex> m_indices;
197 std::size_t m_numStrips;
198 std::size_t m_lenStrips;
200 Array<std::size_t> m_arrayWidth;
201 std::size_t m_nArrayWidth;
202 Array<std::size_t> m_arrayHeight;
203 std::size_t m_nArrayHeight;
205 Array<BezierCurveTree*> m_curveTreeU;
206 Array<BezierCurveTree*> m_curveTreeV;
209 class RenderablePatchWireframe : public OpenGLRenderable
211 PatchTesselation& m_tess;
213 RenderablePatchWireframe(PatchTesselation& tess) : m_tess(tess)
216 void render(RenderStateFlags state) const
220 glVertexPointer(3, GL_FLOAT, 0, 0);
221 glDrawArrays(GL_TRIANGLE_FAN, 0, 0);
225 glVertexPointer(3, GL_FLOAT, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->vertex);
226 for(std::size_t i = 0; i <= m_tess.m_curveTreeV.size(); ++i)
228 glDrawArrays(GL_LINE_STRIP, GLint(n), GLsizei(m_tess.m_nArrayWidth));
230 if(i == m_tess.m_curveTreeV.size()) break;
232 if(!BezierCurveTree_isLeaf(m_tess.m_curveTreeV[i]))
233 glDrawArrays(GL_LINE_STRIP, GLint(m_tess.m_curveTreeV[i]->index), GLsizei(m_tess.m_nArrayWidth));
235 n += (m_tess.m_arrayHeight[i]*m_tess.m_nArrayWidth);
241 const ArbitraryMeshVertex* p = m_tess.m_vertices.data();
242 std::size_t n = m_tess.m_nArrayWidth * sizeof(ArbitraryMeshVertex);
243 for(std::size_t i = 0; i <= m_tess.m_curveTreeU.size(); ++i)
245 glVertexPointer(3, GL_FLOAT, GLsizei(n), &p->vertex);
246 glDrawArrays(GL_LINE_STRIP, 0, GLsizei(m_tess.m_nArrayHeight));
248 if(i == m_tess.m_curveTreeU.size()) break;
250 if(!BezierCurveTree_isLeaf(m_tess.m_curveTreeU[i]))
252 glVertexPointer(3, GL_FLOAT, GLsizei(n), &(m_tess.m_vertices.data() + (m_tess.m_curveTreeU[i]->index))->vertex);
253 glDrawArrays(GL_LINE_STRIP, 0, GLsizei(m_tess.m_nArrayHeight));
256 p += m_tess.m_arrayWidth[i];
262 class RenderablePatchFixedWireframe : public OpenGLRenderable
264 PatchTesselation& m_tess;
266 RenderablePatchFixedWireframe(PatchTesselation& tess) : m_tess(tess)
269 void render(RenderStateFlags state) const
271 glVertexPointer(3, GL_FLOAT, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->vertex);
272 const RenderIndex* strip_indices = m_tess.m_indices.data();
273 for(std::size_t i = 0; i<m_tess.m_numStrips; i++, strip_indices += m_tess.m_lenStrips)
275 glDrawElements(GL_QUAD_STRIP, GLsizei(m_tess.m_lenStrips), RenderIndexTypeID, strip_indices);
280 class RenderablePatchSolid : public OpenGLRenderable
282 PatchTesselation& m_tess;
284 RenderablePatchSolid(PatchTesselation& tess) : m_tess(tess)
287 void RenderNormals() const;
288 void render(RenderStateFlags state) const
291 if((state & RENDER_FILL) == 0)
293 RenderablePatchWireframe(m_tess).render(state);
298 if((state & RENDER_BUMP) != 0)
300 if(GlobalShaderCache().useShaderLanguage())
302 glNormalPointer(GL_FLOAT, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->normal);
303 glVertexAttribPointerARB(c_attr_TexCoord0, 2, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->texcoord);
304 glVertexAttribPointerARB(c_attr_Tangent, 3, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->tangent);
305 glVertexAttribPointerARB(c_attr_Binormal, 3, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->bitangent);
309 glVertexAttribPointerARB(11, 3, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->normal);
310 glVertexAttribPointerARB(8, 2, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->texcoord);
311 glVertexAttribPointerARB(9, 3, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->tangent);
312 glVertexAttribPointerARB(10, 3, GL_FLOAT, 0, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->bitangent);
317 glNormalPointer(GL_FLOAT, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->normal);
318 glTexCoordPointer(2, GL_FLOAT, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->texcoord);
320 glVertexPointer(3, GL_FLOAT, sizeof(ArbitraryMeshVertex), &m_tess.m_vertices.data()->vertex);
321 const RenderIndex* strip_indices = m_tess.m_indices.data();
322 for(std::size_t i = 0; i<m_tess.m_numStrips; i++, strip_indices += m_tess.m_lenStrips)
324 glDrawElements(GL_QUAD_STRIP, GLsizei(m_tess.m_lenStrips), RenderIndexTypeID, strip_indices);
334 // parametric surface defined by quadratic bezier control curves
338 public TransformNode,
356 xml_state_t(EState state)
363 const char* content() const
365 return m_content.c_str();
367 std::size_t write(const char* buffer, std::size_t length)
369 return m_content.write(buffer, length);
373 StringOutputStream m_content;
376 std::vector<xml_state_t> m_xml_state;
378 typedef Array<PatchControl> PatchControlArray;
380 class SavedState : public UndoMemento
386 const PatchControlArray& ctrl,
389 std::size_t subdivisions_x,
390 std::size_t subdivisions_y
396 m_patchDef3(patchDef3),
397 m_subdivisions_x(subdivisions_x),
398 m_subdivisions_y(subdivisions_y)
407 std::size_t m_width, m_height;
408 CopiedString m_shader;
409 PatchControlArray m_ctrl;
411 std::size_t m_subdivisions_x;
412 std::size_t m_subdivisions_y;
419 virtual void allocate(std::size_t size) = 0;
423 typedef UniqueSet<Observer*> Observers;
424 Observers m_observers;
428 AABB m_aabb_local; // local bbox
430 CopiedString m_shader;
434 std::size_t m_height;
437 std::size_t m_subdivisions_x;
438 std::size_t m_subdivisions_y;
441 UndoObserver* m_undoable_observer;
444 // dynamically allocated array of control points, size is m_width*m_height
445 PatchControlArray m_ctrl;
446 PatchControlArray m_ctrlTransformed;
448 PatchTesselation m_tess;
449 RenderablePatchSolid m_render_solid;
450 RenderablePatchWireframe m_render_wireframe;
451 RenderablePatchFixedWireframe m_render_wireframe_fixed;
453 static Shader* m_state_ctrl;
454 static Shader* m_state_lattice;
455 VertexBuffer<PointVertex> m_ctrl_vertices;
456 RenderableVertexBuffer m_render_ctrl;
457 IndexBuffer m_lattice_indices;
458 RenderableIndexBuffer m_render_lattice;
462 bool m_transformChanged;
463 Callback m_evaluateTransform;
464 Callback m_boundsChanged;
469 m_width = m_height = 0;
472 m_subdivisions_x = 0;
473 m_subdivisions_y = 0;
478 m_xml_state.push_back(xml_state_t::eDefault);
482 Callback m_lightsChanged;
484 static int m_CycleCapIndex;// = 0;
485 static EPatchType m_type;
487 STRING_CONSTANT(Name, "Patch");
489 Patch(scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged) :
491 m_shader(texdef_name_default()),
493 m_undoable_observer(0),
495 m_render_solid(m_tess),
496 m_render_wireframe(m_tess),
497 m_render_wireframe_fixed(m_tess),
498 m_render_ctrl(GL_POINTS, m_ctrl_vertices),
499 m_render_lattice(GL_LINES, m_lattice_indices, m_ctrl_vertices),
500 m_transformChanged(false),
501 m_evaluateTransform(evaluateTransform),
502 m_boundsChanged(boundsChanged)
506 Patch(const Patch& other, scene::Node& node, const Callback& evaluateTransform, const Callback& boundsChanged) :
508 m_shader(texdef_name_default()),
510 m_undoable_observer(0),
512 m_render_solid(m_tess),
513 m_render_wireframe(m_tess),
514 m_render_wireframe_fixed(m_tess),
515 m_render_ctrl(GL_POINTS, m_ctrl_vertices),
516 m_render_lattice(GL_LINES, m_lattice_indices, m_ctrl_vertices),
517 m_transformChanged(false),
518 m_evaluateTransform(evaluateTransform),
519 m_boundsChanged(boundsChanged)
523 m_patchDef3 = other.m_patchDef3;
524 m_subdivisions_x = other.m_subdivisions_x;
525 m_subdivisions_y = other.m_subdivisions_y;
526 setDims(other.m_width, other.m_height);
527 copy_ctrl(m_ctrl.data(), other.m_ctrl.data(), other.m_ctrl.data()+(m_width*m_height));
528 SetShader(other.m_shader.c_str());
529 controlPointsChanged();
532 Patch(const Patch& other) :
535 TransformNode(other),
543 m_undoable_observer(0),
545 m_render_solid(m_tess),
546 m_render_wireframe(m_tess),
547 m_render_wireframe_fixed(m_tess),
548 m_render_ctrl(GL_POINTS, m_ctrl_vertices),
549 m_render_lattice(GL_LINES, m_lattice_indices, m_ctrl_vertices),
550 m_transformChanged(false),
551 m_evaluateTransform(other.m_evaluateTransform),
552 m_boundsChanged(other.m_boundsChanged)
556 m_patchDef3 = other.m_patchDef3;
557 m_subdivisions_x = other.m_subdivisions_x;
558 m_subdivisions_y = other.m_subdivisions_y;
559 setDims(other.m_width, other.m_height);
560 copy_ctrl(m_ctrl.data(), other.m_ctrl.data(), other.m_ctrl.data()+(m_width*m_height));
561 SetShader(other.m_shader.c_str());
562 controlPointsChanged();
567 BezierCurveTreeArray_deleteAll(m_tess.m_curveTreeU);
568 BezierCurveTreeArray_deleteAll(m_tess.m_curveTreeV);
572 ASSERT_MESSAGE(m_observers.empty(), "Patch::~Patch: observers still attached");
575 InstanceCounter m_instanceCounter;
576 void instanceAttach(const scene::Path& path)
578 if(++m_instanceCounter.m_count == 1)
580 m_state->incrementUsed();
581 m_map = path_find_mapfile(path.begin(), path.end());
582 m_undoable_observer = GlobalUndoSystem().observer(this);
583 GlobalFilterSystem().registerFilterable(*this);
587 ASSERT_MESSAGE(path_find_mapfile(path.begin(), path.end()) == m_map, "node is instanced across more than one file");
590 void instanceDetach(const scene::Path& path)
592 if(--m_instanceCounter.m_count == 0)
595 m_undoable_observer = 0;
596 GlobalUndoSystem().release(this);
597 GlobalFilterSystem().unregisterFilterable(*this);
598 m_state->decrementUsed();
602 const char* name() const
606 void attach(const NameCallback& callback)
609 void detach(const NameCallback& callback)
613 void attach(Observer* observer)
615 observer->allocate(m_width * m_height);
617 m_observers.insert(observer);
619 void detach(Observer* observer)
621 m_observers.erase(observer);
624 void updateFiltered()
628 if(patch_filtered(*this))
630 m_node->enable(scene::Node::eFiltered);
634 m_node->disable(scene::Node::eFiltered);
639 void onAllocate(std::size_t size)
641 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
643 (*i)->allocate(size);
647 const Matrix4& localToParent() const
649 return g_matrix4_identity;
651 const AABB& localAABB() const
655 VolumeIntersectionValue intersectVolume(const VolumeTest& test, const Matrix4& localToWorld) const
657 return test.TestAABB(m_aabb_local, localToWorld);
659 void render_solid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
661 renderer.SetState(m_state, Renderer::eFullMaterials);
662 renderer.addRenderable(m_render_solid, localToWorld);
664 void render_wireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
666 renderer.SetState(m_state, Renderer::eFullMaterials);
669 renderer.addRenderable(m_render_wireframe_fixed, localToWorld);
673 renderer.addRenderable(m_render_wireframe, localToWorld);
677 void render_component(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
679 renderer.SetState(m_state_lattice, Renderer::eWireframeOnly);
680 renderer.SetState(m_state_lattice, Renderer::eFullMaterials);
681 renderer.addRenderable(m_render_lattice, localToWorld);
683 renderer.SetState(m_state_ctrl, Renderer::eWireframeOnly);
684 renderer.SetState(m_state_ctrl, Renderer::eFullMaterials);
685 renderer.addRenderable(m_render_ctrl, localToWorld);
687 void testSelect(Selector& selector, SelectionTest& test)
689 SelectionIntersection best;
690 IndexPointer::index_type* pIndex = m_tess.m_indices.data();
691 for(std::size_t s=0; s<m_tess.m_numStrips; s++)
693 test.TestQuadStrip(vertexpointer_arbitrarymeshvertex(m_tess.m_vertices.data()), IndexPointer(pIndex, m_tess.m_lenStrips), best);
694 pIndex += m_tess.m_lenStrips;
698 selector.addIntersection(best);
701 void transform(const Matrix4& matrix)
703 for(PatchControlIter i = m_ctrlTransformed.data(); i != m_ctrlTransformed.data() + m_ctrlTransformed.size(); ++i)
705 matrix4_transform_point(matrix, (*i).m_vertex);
708 if(matrix4_handedness(matrix) == MATRIX4_LEFTHANDED)
710 PatchControlArray_invert(m_ctrlTransformed, m_width, m_height);
714 void transformChanged()
716 m_transformChanged = true;
720 typedef MemberCaller<Patch, &Patch::transformChanged> TransformChangedCaller;
722 void evaluateTransform()
724 if(m_transformChanged)
726 m_transformChanged = false;
728 m_evaluateTransform();
732 void revertTransform()
734 m_ctrlTransformed = m_ctrl;
736 void freezeTransform()
740 ASSERT_MESSAGE(m_ctrlTransformed.size() == m_ctrl.size(), "Patch::freeze: size mismatch");
741 std::copy(m_ctrlTransformed.begin(), m_ctrlTransformed.end(), m_ctrl.begin());
744 void controlPointsChanged()
750 bool isValid() const;
752 void snapto(float snap)
756 for(PatchControlIter i = m_ctrl.data(); i != m_ctrl.data() + m_ctrl.size(); ++i)
758 vector3_snap((*i).m_vertex, snap);
761 controlPointsChanged();
767 void RenderDebug(RenderStateFlags state) const;
768 void RenderNormals(RenderStateFlags state) const;
770 void pushElement(const XMLElement& element)
772 switch(m_xml_state.back().state())
774 case xml_state_t::eDefault:
775 ASSERT_MESSAGE(string_equal(element.name(), "patch"), "parse error");
776 m_xml_state.push_back(xml_state_t::ePatch);
778 case xml_state_t::ePatch:
779 if(string_equal(element.name(), "matrix"))
781 setDims(atoi(element.attribute("width")), atoi(element.attribute("height")));
782 m_xml_state.push_back(xml_state_t::eMatrix);
784 else if(string_equal(element.name(), "shader"))
786 m_xml_state.push_back(xml_state_t::eShader);
790 ERROR_MESSAGE("parse error");
794 void popElement(const char* name)
796 switch(m_xml_state.back().state())
798 case xml_state_t::eDefault:
799 ERROR_MESSAGE("parse error");
801 case xml_state_t::ePatch:
803 case xml_state_t::eMatrix:
805 StringTokeniser content(m_xml_state.back().content());
807 for(PatchControlIter i = m_ctrl.data(), end = m_ctrl.data() + m_ctrl.size(); i != end; ++i)
809 (*i).m_vertex[0] = string_read_float(content.getToken());
810 (*i).m_vertex[1] = string_read_float(content.getToken());
811 (*i).m_vertex[2] = string_read_float(content.getToken());
812 (*i).m_texcoord[0] = string_read_float(content.getToken());
813 (*i).m_texcoord[1] = string_read_float(content.getToken());
815 controlPointsChanged();
818 case xml_state_t::eShader:
820 SetShader(m_xml_state.back().content());
824 ERROR_MESSAGE("parse error");
827 ASSERT_MESSAGE(!m_xml_state.empty(), "popping empty stack");
828 m_xml_state.pop_back();
830 std::size_t write(const char* buffer, std::size_t length)
832 switch(m_xml_state.back().state())
834 case xml_state_t::eDefault:
836 case xml_state_t::ePatch:
838 case xml_state_t::eMatrix:
839 case xml_state_t::eShader:
840 return m_xml_state.back().write(buffer, length);
843 ERROR_MESSAGE("parse error");
848 void exportXML(XMLImporter& importer)
850 StaticElement patchElement("patch");
851 importer.pushElement(patchElement);
854 const StaticElement element("shader");
855 importer.pushElement(element);
856 importer.write(m_shader.c_str(), strlen(m_shader.c_str()));
857 importer.popElement(element.name());
861 char width[16], height[16];
862 sprintf(width, "%u", Unsigned(m_width));
863 sprintf(height, "%u", Unsigned(m_height));
864 StaticElement element("matrix");
865 element.insertAttribute("width", width);
866 element.insertAttribute("height", height);
868 importer.pushElement(element);
870 for(PatchControlIter i = m_ctrl.data(), end = m_ctrl.data() + m_ctrl.size(); i != end; ++i)
872 importer << (*i).m_vertex[0]
873 << ' ' << (*i).m_vertex[1]
874 << ' ' << (*i).m_vertex[2]
875 << ' ' << (*i).m_texcoord[0]
876 << ' ' << (*i).m_texcoord[1];
879 importer.popElement(element.name());
882 importer.popElement(patchElement.name());
885 void UpdateCachedData();
887 const char *GetShader() const
889 return m_shader.c_str();
891 void SetShader(const char* name)
893 ASSERT_NOTNULL(name);
895 if(shader_equal(m_shader.c_str(), name))
900 if(m_instanceCounter.m_count != 0)
902 m_state->decrementUsed();
907 if(m_instanceCounter.m_count != 0)
909 m_state->incrementUsed();
913 Patch_textureChanged();
915 int getShaderFlags() const
919 return m_state->getFlags();
924 typedef PatchControl* iterator;
925 typedef const PatchControl* const_iterator;
929 return m_ctrl.data();
931 const_iterator begin() const
933 return m_ctrl.data();
937 return m_ctrl.data() + m_ctrl.size();
939 const_iterator end() const
941 return m_ctrl.data() + m_ctrl.size();
944 PatchControlArray& getControlPoints()
948 PatchControlArray& getControlPointsTransformed()
950 return m_ctrlTransformed;
953 void setDims (std::size_t w, std::size_t h);
954 std::size_t getWidth() const
958 std::size_t getHeight() const
962 PatchControl& ctrlAt(std::size_t row, std::size_t col)
964 return m_ctrl[row*m_width+col];
966 const PatchControl& ctrlAt(std::size_t row, std::size_t col) const
968 return m_ctrl[row*m_width+col];
971 void ConstructPrefab(const AABB& aabb, EPatchPrefab eType, int axis, std::size_t width = 3, std::size_t height = 3);
972 void constructPlane(const AABB& aabb, int axis, std::size_t width, std::size_t height);
974 void TransposeMatrix();
975 void Redisperse(EMatrixMajor mt);
976 void Smooth(EMatrixMajor mt);
977 void InsertRemove(bool bInsert, bool bColumn, bool bFirst);
978 Patch* MakeCap(Patch* patch, EPatchCap eType, EMatrixMajor mt, bool bFirst);
979 void ConstructSeam(EPatchCap eType, Vector3* p, std::size_t width);
981 void FlipTexture(int nAxis);
982 void TranslateTexture(float s, float t);
983 void ScaleTexture(float s, float t);
984 void RotateTexture(float angle);
985 void SetTextureRepeat(float s, float t); // call with s=1 t=1 for FIT
987 void NaturalTexture();
988 void ProjectTexture(int nAxis);
996 if(m_undoable_observer != 0)
998 m_undoable_observer->save(this);
1002 UndoMemento* exportState() const
1004 return new SavedState(m_width, m_height, m_ctrl, m_shader.c_str(), m_patchDef3, m_subdivisions_x, m_subdivisions_y);
1006 void importState(const UndoMemento* state)
1010 const SavedState& other = *(static_cast<const SavedState*>(state));
1012 // begin duplicate of SavedState copy constructor, needs refactoring
1016 m_width = other.m_width;
1017 m_height = other.m_height;
1018 SetShader(other.m_shader.c_str());
1019 m_ctrl = other.m_ctrl;
1020 onAllocate(m_ctrl.size());
1021 m_patchDef3 = other.m_patchDef3;
1022 m_subdivisions_x = other.m_subdivisions_x;
1023 m_subdivisions_y = other.m_subdivisions_y;
1026 // end duplicate code
1028 Patch_textureChanged();
1030 controlPointsChanged();
1033 static void constructStatic(EPatchType type)
1035 Patch::m_type = type;
1036 Patch::m_state_ctrl = GlobalShaderCache().capture("$POINT");
1037 Patch::m_state_lattice = GlobalShaderCache().capture("$LATTICE");
1040 static void destroyStatic()
1042 GlobalShaderCache().release("$LATTICE");
1043 GlobalShaderCache().release("$POINT");
1046 void captureShader()
1048 m_state = GlobalShaderCache().capture(m_shader.c_str());
1051 void releaseShader()
1053 GlobalShaderCache().release(m_shader.c_str());
1058 if(!shader_valid(GetShader()))
1060 globalErrorStream() << "patch has invalid texture name: '" << GetShader() << "'\n";
1064 void InsertPoints(EMatrixMajor mt, bool bFirst);
1065 void RemovePoints(EMatrixMajor mt, bool bFirst);
1067 void AccumulateBBox();
1069 void TesselateSubMatrixFixed(ArbitraryMeshVertex* vertices, std::size_t strideX, std::size_t strideY, unsigned int nFlagsX, unsigned int nFlagsY, PatchControl* subMatrix[3][3]);
1071 // uses binary trees representing bezier curves to recursively tesselate a bezier sub-patch
1072 void TesselateSubMatrix( const BezierCurveTree *BX, const BezierCurveTree *BY,
1073 std::size_t offStartX, std::size_t offStartY,
1074 std::size_t offEndX, std::size_t offEndY,
1075 std::size_t nFlagsX, std::size_t nFlagsY,
1076 Vector3& left, Vector3& mid, Vector3& right,
1077 Vector2& texLeft, Vector2& texMid, Vector2& texRight,
1080 // tesselates the entire surface
1081 void BuildTesselationCurves(EMatrixMajor major);
1082 void accumulateVertexTangentSpace(std::size_t index, Vector3 tangentX[6], Vector3 tangentY[6], Vector2 tangentS[6], Vector2 tangentT[6], std::size_t index0, std::size_t index1);
1083 void BuildVertexArray();
1086 inline bool Patch_importHeader(Patch& patch, Tokeniser& tokeniser)
1088 tokeniser.nextLine();
1089 RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, "{"));
1093 inline bool Patch_importShader(Patch& patch, Tokeniser& tokeniser)
1095 // parse shader name
1096 tokeniser.nextLine();
1097 const char* texture = tokeniser.getToken();
1100 Tokeniser_unexpectedError(tokeniser, texture, "#texture-name");
1103 if(string_equal(texture, "NULL"))
1105 patch.SetShader(texdef_name_default());
1109 StringOutputStream shader(string_length(GlobalTexturePrefix_get()) + string_length(texture));
1110 shader << GlobalTexturePrefix_get() << texture;
1111 patch.SetShader(shader.c_str());
1116 inline bool PatchDoom3_importShader(Patch& patch, Tokeniser& tokeniser)
1118 // parse shader name
1119 tokeniser.nextLine();
1120 const char *shader = tokeniser.getToken();
1123 Tokeniser_unexpectedError(tokeniser, shader, "#shader-name");
1126 if(string_equal(shader, "_emptyname"))
1128 shader = texdef_name_default();
1130 patch.SetShader(shader);
1134 inline bool Patch_importParams(Patch& patch, Tokeniser& tokeniser)
1136 tokeniser.nextLine();
1137 RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, "("));
1139 // parse matrix dimensions
1142 RETURN_FALSE_IF_FAIL(Tokeniser_getSize(tokeniser, c));
1143 RETURN_FALSE_IF_FAIL(Tokeniser_getSize(tokeniser, r));
1145 patch.setDims(c, r);
1148 if(patch.m_patchDef3)
1150 RETURN_FALSE_IF_FAIL(Tokeniser_getSize(tokeniser, patch.m_subdivisions_x));
1151 RETURN_FALSE_IF_FAIL(Tokeniser_getSize(tokeniser, patch.m_subdivisions_y));
1154 // ignore contents/flags/value
1156 RETURN_FALSE_IF_FAIL(Tokeniser_getInteger(tokeniser, tmp));
1157 RETURN_FALSE_IF_FAIL(Tokeniser_getInteger(tokeniser, tmp));
1158 RETURN_FALSE_IF_FAIL(Tokeniser_getInteger(tokeniser, tmp));
1160 RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, ")"));
1164 inline bool Patch_importMatrix(Patch& patch, Tokeniser& tokeniser)
1167 tokeniser.nextLine();
1168 RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, "("));
1170 for(std::size_t c=0; c<patch.getWidth(); c++)
1172 tokeniser.nextLine();
1173 RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, "("));
1174 for(std::size_t r=0; r<patch.getHeight(); r++)
1176 RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, "("));
1178 RETURN_FALSE_IF_FAIL(Tokeniser_getFloat(tokeniser, patch.ctrlAt(r,c).m_vertex[0]));
1179 RETURN_FALSE_IF_FAIL(Tokeniser_getFloat(tokeniser, patch.ctrlAt(r,c).m_vertex[1]));
1180 RETURN_FALSE_IF_FAIL(Tokeniser_getFloat(tokeniser, patch.ctrlAt(r,c).m_vertex[2]));
1181 RETURN_FALSE_IF_FAIL(Tokeniser_getFloat(tokeniser, patch.ctrlAt(r,c).m_texcoord[0]));
1182 RETURN_FALSE_IF_FAIL(Tokeniser_getFloat(tokeniser, patch.ctrlAt(r,c).m_texcoord[1]));
1184 RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, ")"));
1186 RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, ")"));
1189 tokeniser.nextLine();
1190 RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, ")"));
1194 inline bool Patch_importFooter(Patch& patch, Tokeniser& tokeniser)
1196 patch.controlPointsChanged();
1198 tokeniser.nextLine();
1199 RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, "}"));
1201 tokeniser.nextLine();
1202 RETURN_FALSE_IF_FAIL(Tokeniser_parseToken(tokeniser, "}"));
1206 class PatchTokenImporter : public MapImporter
1210 PatchTokenImporter(Patch& patch) : m_patch(patch)
1213 bool importTokens(Tokeniser& tokeniser)
1215 RETURN_FALSE_IF_FAIL(Patch_importHeader(m_patch, tokeniser));
1216 RETURN_FALSE_IF_FAIL(Patch_importShader(m_patch, tokeniser));
1217 RETURN_FALSE_IF_FAIL(Patch_importParams(m_patch, tokeniser));
1218 RETURN_FALSE_IF_FAIL(Patch_importMatrix(m_patch, tokeniser));
1219 RETURN_FALSE_IF_FAIL(Patch_importFooter(m_patch, tokeniser));
1225 class PatchDoom3TokenImporter : public MapImporter
1229 PatchDoom3TokenImporter(Patch& patch) : m_patch(patch)
1232 bool importTokens(Tokeniser& tokeniser)
1234 RETURN_FALSE_IF_FAIL(Patch_importHeader(m_patch, tokeniser));
1235 RETURN_FALSE_IF_FAIL(PatchDoom3_importShader(m_patch, tokeniser));
1236 RETURN_FALSE_IF_FAIL(Patch_importParams(m_patch, tokeniser));
1237 RETURN_FALSE_IF_FAIL(Patch_importMatrix(m_patch, tokeniser));
1238 RETURN_FALSE_IF_FAIL(Patch_importFooter(m_patch, tokeniser));
1244 inline void Patch_exportHeader(const Patch& patch, TokenWriter& writer)
1246 writer.writeToken("{");
1248 writer.writeToken(patch.m_patchDef3 ? "patchDef3" : "patchDef2");
1250 writer.writeToken("{");
1254 inline void Patch_exportShader(const Patch& patch, TokenWriter& writer)
1256 // write shader name
1257 if(*(shader_get_textureName(patch.GetShader())) == '\0')
1259 writer.writeToken("NULL");
1263 writer.writeToken(shader_get_textureName(patch.GetShader()));
1268 inline void PatchDoom3_exportShader(const Patch& patch, TokenWriter& writer)
1270 // write shader name
1271 if(*(shader_get_textureName(patch.GetShader())) == '\0')
1273 writer.writeString("_emptyname");
1277 writer.writeString(patch.GetShader());
1282 inline void Patch_exportParams(const Patch& patch, TokenWriter& writer)
1284 // write matrix dimensions
1285 writer.writeToken("(");
1286 writer.writeUnsigned(patch.getWidth());
1287 writer.writeUnsigned(patch.getHeight());
1288 if(patch.m_patchDef3)
1290 writer.writeUnsigned(patch.m_subdivisions_x);
1291 writer.writeUnsigned(patch.m_subdivisions_y);
1293 writer.writeInteger(0);
1294 writer.writeInteger(0);
1295 writer.writeInteger(0);
1296 writer.writeToken(")");
1300 inline void Patch_exportMatrix(const Patch& patch, TokenWriter& writer)
1303 writer.writeToken("(");
1305 for(std::size_t c=0; c<patch.getWidth(); c++)
1307 writer.writeToken("(");
1308 for(std::size_t r=0; r<patch.getHeight(); r++)
1310 writer.writeToken("(");
1312 writer.writeFloat(patch.ctrlAt(r,c).m_vertex[0]);
1313 writer.writeFloat(patch.ctrlAt(r,c).m_vertex[1]);
1314 writer.writeFloat(patch.ctrlAt(r,c).m_vertex[2]);
1315 writer.writeFloat(patch.ctrlAt(r,c).m_texcoord[0]);
1316 writer.writeFloat(patch.ctrlAt(r,c).m_texcoord[1]);
1318 writer.writeToken(")");
1320 writer.writeToken(")");
1323 writer.writeToken(")");
1327 inline void Patch_exportFooter(const Patch& patch, TokenWriter& writer)
1329 writer.writeToken("}");
1331 writer.writeToken("}");
1335 class PatchTokenExporter : public MapExporter
1337 const Patch& m_patch;
1339 PatchTokenExporter(Patch& patch) : m_patch(patch)
1342 void exportTokens(TokenWriter& writer) const
1344 Patch_exportHeader(m_patch, writer);
1345 Patch_exportShader(m_patch, writer);
1346 Patch_exportParams(m_patch, writer);
1347 Patch_exportMatrix(m_patch, writer);
1348 Patch_exportFooter(m_patch, writer);
1352 class PatchDoom3TokenExporter : public MapExporter
1354 const Patch& m_patch;
1356 PatchDoom3TokenExporter(Patch& patch) : m_patch(patch)
1359 void exportTokens(TokenWriter& writer) const
1361 Patch_exportHeader(m_patch, writer);
1362 PatchDoom3_exportShader(m_patch, writer);
1363 Patch_exportParams(m_patch, writer);
1364 Patch_exportMatrix(m_patch, writer);
1365 Patch_exportFooter(m_patch, writer);
1369 class PatchControlInstance
1372 PatchControl* m_ctrl;
1373 ObservedSelectable m_selectable;
1375 PatchControlInstance(PatchControl* ctrl, const SelectionChangeCallback& observer)
1376 : m_ctrl(ctrl), m_selectable(observer)
1380 void testSelect(Selector& selector, SelectionTest& test)
1382 SelectionIntersection best;
1383 test.TestPoint(m_ctrl->m_vertex, best);
1386 Selector_add(selector, m_selectable, best);
1389 void snapto(float snap)
1391 vector3_snap(m_ctrl->m_vertex, snap);
1396 class PatchInstance :
1397 public Patch::Observer,
1398 public scene::Instance,
1401 public SelectionTestable,
1402 public ComponentSelectionTestable,
1403 public ComponentEditable,
1404 public ComponentSnappable,
1405 public PlaneSelectable,
1406 public LightCullable
1410 InstanceTypeCastTable m_casts;
1414 InstanceStaticCast<PatchInstance, Selectable>::install(m_casts);
1415 InstanceContainedCast<PatchInstance, Bounded>::install(m_casts);
1416 InstanceContainedCast<PatchInstance, Cullable>::install(m_casts);
1417 InstanceStaticCast<PatchInstance, Renderable>::install(m_casts);
1418 InstanceStaticCast<PatchInstance, SelectionTestable>::install(m_casts);
1419 InstanceStaticCast<PatchInstance, ComponentSelectionTestable>::install(m_casts);
1420 InstanceStaticCast<PatchInstance, ComponentEditable>::install(m_casts);
1421 InstanceStaticCast<PatchInstance, ComponentSnappable>::install(m_casts);
1422 InstanceStaticCast<PatchInstance, PlaneSelectable>::install(m_casts);
1423 InstanceIdentityCast<PatchInstance>::install(m_casts);
1424 InstanceContainedCast<PatchInstance, Transformable>::install(m_casts);
1426 InstanceTypeCastTable& get()
1434 typedef std::vector<PatchControlInstance> PatchControlInstances;
1435 PatchControlInstances m_ctrl_instances;
1437 ObservedSelectable m_selectable;
1439 DragPlanes m_dragPlanes;
1441 mutable RenderablePointVector m_render_selected;
1442 mutable AABB m_aabb_component;
1444 static Shader* m_state_selpoint;
1446 const LightList* m_lightList;
1448 TransformModifier m_transform;
1451 typedef LazyStatic<TypeCasts> StaticTypeCasts;
1453 void lightsChanged()
1455 m_lightList->lightsChanged();
1457 typedef MemberCaller<PatchInstance, &PatchInstance::lightsChanged> LightsChangedCaller;
1459 STRING_CONSTANT(Name, "PatchInstance");
1461 PatchInstance(const scene::Path& path, scene::Instance* parent, Patch& patch) :
1462 Instance(path, parent, this, StaticTypeCasts::instance().get()),
1464 m_selectable(SelectedChangedCaller(*this)),
1465 m_dragPlanes(SelectedChangedComponentCaller(*this)),
1466 m_render_selected(GL_POINTS),
1467 m_transform(Patch::TransformChangedCaller(m_patch), ApplyTransformCaller(*this))
1469 m_patch.instanceAttach(Instance::path());
1470 m_patch.attach(this);
1472 m_lightList = &GlobalShaderCache().attach(*this);
1473 m_patch.m_lightsChanged = LightsChangedCaller(*this);
1475 Instance::setTransformChangedCallback(LightsChangedCaller(*this));
1479 Instance::setTransformChangedCallback(Callback());
1481 m_patch.m_lightsChanged = Callback();
1482 GlobalShaderCache().detach(*this);
1484 m_patch.detach(this);
1485 m_patch.instanceDetach(Instance::path());
1488 void selectedChanged(const Selectable& selectable)
1490 GlobalSelectionSystem().getObserver(SelectionSystem::ePrimitive)(selectable);
1491 GlobalSelectionSystem().onSelectedChanged(*this, selectable);
1493 Instance::selectedChanged();
1495 typedef MemberCaller1<PatchInstance, const Selectable&, &PatchInstance::selectedChanged> SelectedChangedCaller;
1497 void selectedChangedComponent(const Selectable& selectable)
1499 GlobalSelectionSystem().getObserver(SelectionSystem::eComponent)(selectable);
1500 GlobalSelectionSystem().onComponentSelection(*this, selectable);
1502 typedef MemberCaller1<PatchInstance, const Selectable&, &PatchInstance::selectedChangedComponent> SelectedChangedComponentCaller;
1508 Bounded& get(NullType<Bounded>)
1512 Cullable& get(NullType<Cullable>)
1516 Transformable& get(NullType<Transformable>)
1521 static void constructStatic()
1523 m_state_selpoint = GlobalShaderCache().capture("$SELPOINT");
1526 static void destroyStatic()
1528 GlobalShaderCache().release("$SELPOINT");
1532 void allocate(std::size_t size)
1534 m_ctrl_instances.clear();
1535 m_ctrl_instances.reserve(size);
1536 for(Patch::iterator i = m_patch.begin(); i != m_patch.end(); ++i)
1538 m_ctrl_instances.push_back(PatchControlInstance(&(*i), SelectedChangedComponentCaller(*this)));
1542 void setSelected(bool select)
1544 m_selectable.setSelected(select);
1546 bool isSelected() const
1548 return m_selectable.isSelected();
1552 void update_selected() const
1554 m_render_selected.clear();
1555 Patch::iterator ctrl = m_patch.getControlPointsTransformed().begin();
1556 for(PatchControlInstances::const_iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i, ++ctrl)
1558 if((*i).m_selectable.isSelected())
1560 const Colour4b colour_selected(0, 0, 255, 255);
1561 m_render_selected.push_back(PointVertex(reinterpret_cast<Vertex3f&>((*ctrl).m_vertex), colour_selected));
1567 void render(Renderer& renderer, const VolumeTest& volume) const
1569 if(GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
1570 && m_selectable.isSelected())
1572 renderer.Highlight(Renderer::eFace, false);
1574 m_patch.render(renderer, volume, localToWorld());
1576 if(GlobalSelectionSystem().ComponentMode() == SelectionSystem::eVertex)
1578 renderer.Highlight(Renderer::ePrimitive, false);
1580 m_patch.render_component(renderer, volume, localToWorld());
1582 renderComponentsSelected(renderer, volume);
1586 m_patch.render(renderer, volume, localToWorld());
1590 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
1592 m_patch.evaluateTransform();
1593 renderer.setLights(*m_lightList);
1594 m_patch.render_solid(renderer, volume, localToWorld());
1596 renderComponentsSelected(renderer, volume);
1599 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
1601 m_patch.evaluateTransform();
1602 m_patch.render_wireframe(renderer, volume, localToWorld());
1604 renderComponentsSelected(renderer, volume);
1607 void renderComponentsSelected(Renderer& renderer, const VolumeTest& volume) const
1609 m_patch.evaluateTransform();
1611 if(!m_render_selected.empty())
1613 renderer.Highlight(Renderer::ePrimitive, false);
1614 renderer.SetState(m_state_selpoint, Renderer::eWireframeOnly);
1615 renderer.SetState(m_state_selpoint, Renderer::eFullMaterials);
1616 renderer.addRenderable(m_render_selected, localToWorld());
1619 void renderComponents(Renderer& renderer, const VolumeTest& volume) const
1621 m_patch.evaluateTransform();
1622 if(GlobalSelectionSystem().ComponentMode() == SelectionSystem::eVertex)
1624 m_patch.render_component(renderer, volume, localToWorld());
1628 void testSelect(Selector& selector, SelectionTest& test)
1630 test.BeginMesh(localToWorld(), true);
1631 m_patch.testSelect(selector, test);
1634 void selectCtrl(bool select)
1636 for(PatchControlInstances::iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i)
1638 (*i).m_selectable.setSelected(select);
1641 bool isSelectedComponents() const
1643 for(PatchControlInstances::const_iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i)
1645 if((*i).m_selectable.isSelected())
1652 void setSelectedComponents(bool select, SelectionSystem::EComponentMode mode)
1654 if(mode == SelectionSystem::eVertex)
1658 else if(mode == SelectionSystem::eFace)
1660 m_dragPlanes.setSelected(select);
1663 const AABB& getSelectedComponentsBounds() const
1665 m_aabb_component = AABB();
1667 for(PatchControlInstances::const_iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i)
1669 if((*i).m_selectable.isSelected())
1671 aabb_extend_by_point_safe(m_aabb_component, (*i).m_ctrl->m_vertex);
1675 return m_aabb_component;
1678 void testSelectComponents(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
1680 test.BeginMesh(localToWorld());
1684 case SelectionSystem::eVertex:
1686 for(PatchControlInstances::iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i)
1688 (*i).testSelect(selector, test);
1697 bool selectedVertices()
1699 for(PatchControlInstances::iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i)
1701 if((*i).m_selectable.isSelected())
1709 void transformComponents(const Matrix4& matrix)
1711 if(selectedVertices())
1713 PatchControlIter ctrl = m_patch.getControlPointsTransformed().begin();
1714 for(PatchControlInstances::iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i, ++ctrl)
1716 if((*i).m_selectable.isSelected())
1718 matrix4_transform_point(matrix, (*ctrl).m_vertex);
1721 m_patch.UpdateCachedData();
1724 if(m_dragPlanes.isSelected()) // this should only be true when the transform is a pure translation.
1726 m_patch.transform(m_dragPlanes.evaluateTransform(vector4_to_vector3(matrix.t())));
1731 void selectPlanes(Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback)
1733 test.BeginMesh(localToWorld());
1735 m_dragPlanes.selectPlanes(m_patch.localAABB(), selector, test, selectedPlaneCallback);
1737 void selectReversedPlanes(Selector& selector, const SelectedPlanes& selectedPlanes)
1739 m_dragPlanes.selectReversedPlanes(m_patch.localAABB(), selector, selectedPlanes);
1743 void snapComponents(float snap)
1745 if(selectedVertices())
1748 for(PatchControlInstances::iterator i = m_ctrl_instances.begin(); i != m_ctrl_instances.end(); ++i)
1750 if((*i).m_selectable.isSelected())
1755 m_patch.controlPointsChanged();
1759 void evaluateTransform()
1761 Matrix4 matrix(m_transform.calculateTransform());
1763 if(m_transform.getType() == TRANSFORM_PRIMITIVE)
1765 m_patch.transform(matrix);
1769 transformComponents(matrix);
1772 void applyTransform()
1774 m_patch.revertTransform();
1775 evaluateTransform();
1776 m_patch.freezeTransform();
1778 typedef MemberCaller<PatchInstance, &PatchInstance::applyTransform> ApplyTransformCaller;
1781 bool testLight(const RendererLight& light) const
1783 return light.testAABB(worldAABB());
1788 template<typename TokenImporter, typename TokenExporter>
1790 public scene::Node::Symbiot,
1791 public scene::Instantiable,
1792 public scene::Cloneable
1794 typedef PatchNode<TokenImporter, TokenExporter> Self;
1798 InstanceTypeCastTable m_casts;
1802 NodeStaticCast<PatchNode, scene::Instantiable>::install(m_casts);
1803 NodeStaticCast<PatchNode, scene::Cloneable>::install(m_casts);
1804 NodeContainedCast<PatchNode, Snappable>::install(m_casts);
1805 NodeContainedCast<PatchNode, TransformNode>::install(m_casts);
1806 NodeContainedCast<PatchNode, Patch>::install(m_casts);
1807 NodeContainedCast<PatchNode, XMLImporter>::install(m_casts);
1808 NodeContainedCast<PatchNode, XMLExporter>::install(m_casts);
1809 NodeContainedCast<PatchNode, MapImporter>::install(m_casts);
1810 NodeContainedCast<PatchNode, MapExporter>::install(m_casts);
1811 NodeContainedCast<PatchNode, Nameable>::install(m_casts);
1813 InstanceTypeCastTable& get()
1821 InstanceSet m_instances;
1823 TokenImporter m_importMap;
1824 TokenExporter m_exportMap;
1828 typedef LazyStatic<TypeCasts> StaticTypeCasts;
1830 Snappable& get(NullType<Snappable>)
1834 TransformNode& get(NullType<TransformNode>)
1838 Patch& get(NullType<Patch>)
1842 XMLImporter& get(NullType<XMLImporter>)
1846 XMLExporter& get(NullType<XMLExporter>)
1850 MapImporter& get(NullType<MapImporter>)
1854 MapExporter& get(NullType<MapExporter>)
1858 Nameable& get(NullType<Nameable>)
1863 PatchNode(bool patchDef3 = false) :
1864 m_node(this, this, StaticTypeCasts::instance().get()),
1865 m_patch(m_node, InstanceSetEvaluateTransform<PatchInstance>::Caller(m_instances), InstanceSet::BoundsChangedCaller(m_instances)),
1866 m_importMap(m_patch),
1867 m_exportMap(m_patch)
1869 m_patch.m_patchDef3 = patchDef3;
1871 PatchNode(const PatchNode& other) :
1872 scene::Node::Symbiot(other),
1873 scene::Instantiable(other),
1874 scene::Cloneable(other),
1875 m_node(this, this, StaticTypeCasts::instance().get()),
1876 m_patch(other.m_patch, m_node, InstanceSetEvaluateTransform<PatchInstance>::Caller(m_instances), InstanceSet::BoundsChangedCaller(m_instances)),
1877 m_importMap(m_patch),
1878 m_exportMap(m_patch)
1893 const Patch& get() const
1898 scene::Node& clone() const
1900 return (new PatchNode(*this))->node();
1903 scene::Instance* create(const scene::Path& path, scene::Instance* parent)
1905 return new PatchInstance(path, parent, m_patch);
1907 void forEachInstance(const scene::Instantiable::Visitor& visitor)
1909 m_instances.forEachInstance(visitor);
1911 void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
1913 m_instances.insert(observer, path, instance);
1915 scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
1917 return m_instances.erase(observer, path);
1923 typedef PatchNode<PatchTokenImporter, PatchTokenExporter> PatchNodeQuake3;
1924 typedef PatchNode<PatchDoom3TokenImporter, PatchDoom3TokenExporter> PatchNodeDoom3;
1926 inline Patch* Node_getPatch(scene::Node& node)
1928 return NodeTypeCast<Patch>::cast(node);
1931 inline PatchInstance* Instance_getPatch(scene::Instance& instance)
1933 return InstanceTypeCast<PatchInstance>::cast(instance);
1936 template<typename Functor>
1937 class PatchSelectedVisitor : public SelectionSystem::Visitor
1939 const Functor& m_functor;
1941 PatchSelectedVisitor(const Functor& functor) : m_functor(functor)
1944 void visit(scene::Instance& instance) const
1946 PatchInstance* patch = Instance_getPatch(instance);
1954 template<typename Functor>
1955 inline void Scene_forEachSelectedPatch(const Functor& functor)
1957 GlobalSelectionSystem().foreachSelected(PatchSelectedVisitor<Functor>(functor));
1961 template<typename Functor>
1962 class PatchVisibleSelectedVisitor : public SelectionSystem::Visitor
1964 const Functor& m_functor;
1966 PatchVisibleSelectedVisitor(const Functor& functor) : m_functor(functor)
1969 void visit(scene::Instance& instance) const
1971 PatchInstance* patch = Instance_getPatch(instance);
1973 && instance.path().top().get().visible())
1980 template<typename Functor>
1981 inline void Scene_forEachVisibleSelectedPatchInstance(const Functor& functor)
1983 GlobalSelectionSystem().foreachSelected(PatchVisibleSelectedVisitor<Functor>(functor));
1986 template<typename Functor>
1987 class PatchForEachWalker : public scene::Graph::Walker
1989 const Functor& m_functor;
1991 PatchForEachWalker(const Functor& functor) : m_functor(functor)
1994 bool pre(const scene::Path& path, scene::Instance& instance) const
1996 if(path.top().get().visible())
1998 Patch* patch = Node_getPatch(path.top());
2008 template<typename Functor>
2009 inline void Scene_forEachVisiblePatch(const Functor& functor)
2011 GlobalSceneGraph().traverse(PatchForEachWalker<Functor>(functor));
2014 template<typename Functor>
2015 class PatchForEachSelectedWalker : public scene::Graph::Walker
2017 const Functor& m_functor;
2019 PatchForEachSelectedWalker(const Functor& functor) : m_functor(functor)
2022 bool pre(const scene::Path& path, scene::Instance& instance) const
2024 if(path.top().get().visible())
2026 Patch* patch = Node_getPatch(path.top());
2028 && Instance_getSelectable(instance)->isSelected())
2037 template<typename Functor>
2038 inline void Scene_forEachVisibleSelectedPatch(const Functor& functor)
2040 GlobalSceneGraph().traverse(PatchForEachSelectedWalker<Functor>(functor));
2043 template<typename Functor>
2044 class PatchForEachInstanceWalker : public scene::Graph::Walker
2046 const Functor& m_functor;
2048 PatchForEachInstanceWalker(const Functor& functor) : m_functor(functor)
2051 bool pre(const scene::Path& path, scene::Instance& instance) const
2053 if(path.top().get().visible())
2055 PatchInstance* patch = Instance_getPatch(instance);
2065 template<typename Functor>
2066 inline void Scene_forEachVisiblePatchInstance(const Functor& functor)
2068 GlobalSceneGraph().traverse(PatchForEachInstanceWalker<Functor>(functor));