2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
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
24 std::set<Callback> g_brushTextureChangedCallbacks;
26 void Brush_addTextureChangedCallback(const Callback& callback)
28 g_brushTextureChangedCallbacks.insert(callback);
31 void Brush_textureChanged()
33 std::for_each(g_brushTextureChangedCallbacks.begin(), g_brushTextureChangedCallbacks.end(), CallbackInvoke());
36 QuantiseFunc Face::m_quantise;
37 EBrushType Face::m_type;
38 EBrushType FacePlane::m_type;
39 bool g_brush_texturelock_enabled = false;
41 EBrushType Brush::m_type;
42 double Brush::m_maxWorldCoord = 0;
43 Shader* Brush::m_state_point;
44 Shader* BrushClipPlane::m_state = 0;
45 Shader* BrushInstance::m_state_selpoint;
46 Counter* BrushInstance::m_counter = 0;
48 FaceInstanceSet g_SelectedFaceInstances;
59 const SListNode* m_vertices;
61 ProximalVertex(const SListNode* next)
66 bool operator<(const ProximalVertex& other) const
68 if(!(operator==(other)))
70 return m_vertices < other.m_vertices;
74 bool operator==(const ProximalVertex& other) const
76 const SListNode* v = m_vertices;
77 std::size_t DEBUG_LOOP = 0;
80 if(v == other.m_vertices)
83 //ASSERT_MESSAGE(DEBUG_LOOP < c_brush_maxFaces, "infinite loop");
84 if(!(DEBUG_LOOP < c_brush_maxFaces))
90 while(v != m_vertices);
95 typedef Array<SListNode> ProximalVertexArray;
96 std::size_t ProximalVertexArray_index(const ProximalVertexArray& array, const ProximalVertex& vertex)
98 return vertex.m_vertices - array.data();
103 inline bool Brush_isBounded(const Brush& brush)
105 for(Brush::const_iterator i = brush.begin(); i != brush.end(); ++i)
107 if(!(*i)->is_bounded())
115 void Brush::buildBRep()
117 bool degenerate = buildWindings();
119 std::size_t faces_size = 0;
120 std::size_t faceVerticesCount = 0;
121 for(Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
123 if((*i)->contributes())
127 faceVerticesCount += (*i)->getWinding().numpoints;
130 if(degenerate || faces_size < 4 || faceVerticesCount != (faceVerticesCount>>1)<<1) // sum of vertices for each face of a valid polyhedron is always even
132 m_uniqueVertexPoints.resize(0);
137 m_edge_indices.resize(0);
138 m_edge_faces.resize(0);
140 m_faceCentroidPoints.resize(0);
141 m_uniqueEdgePoints.resize(0);
142 m_uniqueVertexPoints.resize(0);
144 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
146 (*i)->getWinding().resize(0);
152 typedef std::vector<FaceVertexId> FaceVertices;
153 FaceVertices faceVertices;
154 faceVertices.reserve(faceVerticesCount);
157 for(std::size_t i = 0; i != m_faces.size(); ++i)
159 for(std::size_t j = 0; j < m_faces[i]->getWinding().numpoints; ++j)
161 faceVertices.push_back(FaceVertexId(i, j));
166 IndexBuffer uniqueEdgeIndices;
167 typedef VertexBuffer<ProximalVertex> UniqueEdges;
168 UniqueEdges uniqueEdges;
170 uniqueEdgeIndices.reserve(faceVertices.size());
171 uniqueEdges.reserve(faceVertices.size());
174 ProximalVertexArray edgePairs;
175 edgePairs.resize(faceVertices.size());
178 for(std::size_t i=0; i<faceVertices.size(); ++i)
180 edgePairs[i].m_next = edgePairs.data() + absoluteIndex(next_edge(m_faces, faceVertices[i]));
185 UniqueVertexBuffer<ProximalVertex> inserter(uniqueEdges);
186 for(ProximalVertexArray::iterator i = edgePairs.begin(); i != edgePairs.end(); ++i)
188 uniqueEdgeIndices.insert(inserter.insert(ProximalVertex(&(*i))));
194 m_select_edges.reserve(uniqueEdges.size());
195 for(UniqueEdges::iterator i = uniqueEdges.begin(); i != uniqueEdges.end(); ++i)
197 edge_push_back(faceVertices[ProximalVertexArray_index(edgePairs, *i)]);
202 m_edge_faces.resize(uniqueEdges.size());
203 for(std::size_t i=0; i<uniqueEdges.size(); ++i)
205 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index(edgePairs, uniqueEdges[i])];
206 m_edge_faces[i] = EdgeFaces(faceVertex.getFace(), m_faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent);
211 m_uniqueEdgePoints.resize(uniqueEdges.size());
212 for(std::size_t i=0; i<uniqueEdges.size(); ++i)
214 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index(edgePairs, uniqueEdges[i])];
216 const Winding& w = m_faces[faceVertex.getFace()]->getWinding();
217 Vector3 edge = vector3_mid(w[faceVertex.getVertex()].vertex, w[Winding_next(w, faceVertex.getVertex())].vertex);
218 m_uniqueEdgePoints[i] = pointvertex_for_windingpoint(edge, colour_vertex);
225 IndexBuffer uniqueVertexIndices;
226 typedef VertexBuffer<ProximalVertex> UniqueVertices;
227 UniqueVertices uniqueVertices;
229 uniqueVertexIndices.reserve(faceVertices.size());
230 uniqueVertices.reserve(faceVertices.size());
233 ProximalVertexArray vertexRings;
234 vertexRings.resize(faceVertices.size());
237 for(std::size_t i=0; i<faceVertices.size(); ++i)
239 vertexRings[i].m_next = vertexRings.data() + absoluteIndex(next_vertex(m_faces, faceVertices[i]));
244 UniqueVertexBuffer<ProximalVertex> inserter(uniqueVertices);
245 for(ProximalVertexArray::iterator i = vertexRings.begin(); i != vertexRings.end(); ++i)
247 uniqueVertexIndices.insert(inserter.insert(ProximalVertex(&(*i))));
253 m_select_vertices.reserve(uniqueVertices.size());
254 for(UniqueVertices::iterator i = uniqueVertices.begin(); i != uniqueVertices.end(); ++i)
256 vertex_push_back(faceVertices[ProximalVertexArray_index(vertexRings, (*i))]);
261 m_uniqueVertexPoints.resize(uniqueVertices.size());
262 for(std::size_t i=0; i<uniqueVertices.size(); ++i)
264 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index(vertexRings, uniqueVertices[i])];
266 const Winding& winding = m_faces[faceVertex.getFace()]->getWinding();
267 m_uniqueVertexPoints[i] = pointvertex_for_windingpoint(winding[faceVertex.getVertex()].vertex, colour_vertex);
272 ASSERT_MESSAGE((uniqueVertices.size() + faces_size) - uniqueEdges.size() == 2, "Final B-Rep: inconsistent vertex count");
274 #if BRUSH_CONNECTIVITY_DEBUG
275 if((uniqueVertices.size() + faces_size) - uniqueEdges.size() != 2)
277 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
279 std::size_t faceIndex = std::distance(m_faces.begin(), i);
281 if(!(*i)->contributes())
283 globalOutputStream() << "face: " << Unsigned(faceIndex) << " does not contribute\n";
286 Winding_printConnectivity((*i)->getWinding());
291 // edge-index list for wireframe rendering
293 m_edge_indices.resize(uniqueEdgeIndices.size());
295 for(std::size_t i=0, count=0; i<m_faces.size(); ++i)
297 const Winding& winding = m_faces[i]->getWinding();
298 for(std::size_t j = 0; j < winding.numpoints; ++j)
300 const RenderIndex edge_index = uniqueEdgeIndices[count+j];
302 m_edge_indices[edge_index].first = uniqueVertexIndices[count + j];
303 m_edge_indices[edge_index].second = uniqueVertexIndices[count + Winding_next(winding, j)];
305 count += winding.numpoints;
311 m_faceCentroidPoints.resize(m_faces.size());
312 for(std::size_t i=0; i<m_faces.size(); ++i)
314 m_faces[i]->construct_centroid();
315 m_faceCentroidPoints[i] = pointvertex_for_windingpoint(m_faces[i]->centroid(), colour_vertex);
322 class FaceFilterWrapper : public Filter
324 FaceFilter& m_filter;
328 FaceFilterWrapper(FaceFilter& filter, bool invert) :
333 void setActive(bool active)
341 bool filter(const Face& face)
343 return m_invert ^ m_filter.filter(face);
348 typedef std::list<FaceFilterWrapper> FaceFilters;
349 FaceFilters g_faceFilters;
351 void add_face_filter(FaceFilter& filter, int mask, bool invert)
353 g_faceFilters.push_back(FaceFilterWrapper(filter, invert));
354 GlobalFilterSystem().addFilter(g_faceFilters.back(), mask);
357 bool face_filtered(Face& face)
359 for(FaceFilters::iterator i = g_faceFilters.begin(); i != g_faceFilters.end(); ++i)
361 if((*i).active() && (*i).filter(face))
370 class BrushFilterWrapper : public Filter
374 BrushFilter& m_filter;
376 BrushFilterWrapper(BrushFilter& filter, bool invert) : m_invert(invert), m_filter(filter)
379 void setActive(bool active)
387 bool filter(const Brush& brush)
389 return m_invert ^ m_filter.filter(brush);
394 typedef std::list<BrushFilterWrapper> BrushFilters;
395 BrushFilters g_brushFilters;
397 void add_brush_filter(BrushFilter& filter, int mask, bool invert)
399 g_brushFilters.push_back(BrushFilterWrapper(filter, invert));
400 GlobalFilterSystem().addFilter(g_brushFilters.back(), mask);
403 bool brush_filtered(Brush& brush)
405 for(BrushFilters::iterator i = g_brushFilters.begin(); i != g_brushFilters.end(); ++i)
407 if((*i).active() && (*i).filter(brush))