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
23 #include "signal/signal.h"
25 Signal0 g_brushTextureChangedCallbacks;
27 void Brush_addTextureChangedCallback(const SignalHandler& handler)
29 g_brushTextureChangedCallbacks.connectLast(handler);
32 void Brush_textureChanged()
34 g_brushTextureChangedCallbacks();
37 QuantiseFunc Face::m_quantise;
38 EBrushType Face::m_type;
39 EBrushType FacePlane::m_type;
40 bool g_brush_texturelock_enabled = false;
42 EBrushType Brush::m_type;
43 double Brush::m_maxWorldCoord = 0;
44 Shader* Brush::m_state_point;
45 Shader* BrushClipPlane::m_state = 0;
46 Shader* BrushInstance::m_state_selpoint;
47 Counter* BrushInstance::m_counter = 0;
49 FaceInstanceSet g_SelectedFaceInstances;
60 const SListNode* m_vertices;
62 ProximalVertex(const SListNode* next)
67 bool operator<(const ProximalVertex& other) const
69 if(!(operator==(other)))
71 return m_vertices < other.m_vertices;
75 bool operator==(const ProximalVertex& other) const
77 const SListNode* v = m_vertices;
78 std::size_t DEBUG_LOOP = 0;
81 if(v == other.m_vertices)
84 //ASSERT_MESSAGE(DEBUG_LOOP < c_brush_maxFaces, "infinite loop");
85 if(!(DEBUG_LOOP < c_brush_maxFaces))
91 while(v != m_vertices);
96 typedef Array<SListNode> ProximalVertexArray;
97 std::size_t ProximalVertexArray_index(const ProximalVertexArray& array, const ProximalVertex& vertex)
99 return vertex.m_vertices - array.data();
104 inline bool Brush_isBounded(const Brush& brush)
106 for(Brush::const_iterator i = brush.begin(); i != brush.end(); ++i)
108 if(!(*i)->is_bounded())
116 void Brush::buildBRep()
118 bool degenerate = buildWindings();
120 std::size_t faces_size = 0;
121 std::size_t faceVerticesCount = 0;
122 for(Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i)
124 if((*i)->contributes())
128 faceVerticesCount += (*i)->getWinding().numpoints;
131 if(degenerate || faces_size < 4 || faceVerticesCount != (faceVerticesCount>>1)<<1) // sum of vertices for each face of a valid polyhedron is always even
133 m_uniqueVertexPoints.resize(0);
138 m_edge_indices.resize(0);
139 m_edge_faces.resize(0);
141 m_faceCentroidPoints.resize(0);
142 m_uniqueEdgePoints.resize(0);
143 m_uniqueVertexPoints.resize(0);
145 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
147 (*i)->getWinding().resize(0);
153 typedef std::vector<FaceVertexId> FaceVertices;
154 FaceVertices faceVertices;
155 faceVertices.reserve(faceVerticesCount);
158 for(std::size_t i = 0; i != m_faces.size(); ++i)
160 for(std::size_t j = 0; j < m_faces[i]->getWinding().numpoints; ++j)
162 faceVertices.push_back(FaceVertexId(i, j));
167 IndexBuffer uniqueEdgeIndices;
168 typedef VertexBuffer<ProximalVertex> UniqueEdges;
169 UniqueEdges uniqueEdges;
171 uniqueEdgeIndices.reserve(faceVertices.size());
172 uniqueEdges.reserve(faceVertices.size());
175 ProximalVertexArray edgePairs;
176 edgePairs.resize(faceVertices.size());
179 for(std::size_t i=0; i<faceVertices.size(); ++i)
181 edgePairs[i].m_next = edgePairs.data() + absoluteIndex(next_edge(m_faces, faceVertices[i]));
186 UniqueVertexBuffer<ProximalVertex> inserter(uniqueEdges);
187 for(ProximalVertexArray::iterator i = edgePairs.begin(); i != edgePairs.end(); ++i)
189 uniqueEdgeIndices.insert(inserter.insert(ProximalVertex(&(*i))));
195 m_select_edges.reserve(uniqueEdges.size());
196 for(UniqueEdges::iterator i = uniqueEdges.begin(); i != uniqueEdges.end(); ++i)
198 edge_push_back(faceVertices[ProximalVertexArray_index(edgePairs, *i)]);
203 m_edge_faces.resize(uniqueEdges.size());
204 for(std::size_t i=0; i<uniqueEdges.size(); ++i)
206 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index(edgePairs, uniqueEdges[i])];
207 m_edge_faces[i] = EdgeFaces(faceVertex.getFace(), m_faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent);
212 m_uniqueEdgePoints.resize(uniqueEdges.size());
213 for(std::size_t i=0; i<uniqueEdges.size(); ++i)
215 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index(edgePairs, uniqueEdges[i])];
217 const Winding& w = m_faces[faceVertex.getFace()]->getWinding();
218 Vector3 edge = vector3_mid(w[faceVertex.getVertex()].vertex, w[Winding_next(w, faceVertex.getVertex())].vertex);
219 m_uniqueEdgePoints[i] = pointvertex_for_windingpoint(edge, colour_vertex);
226 IndexBuffer uniqueVertexIndices;
227 typedef VertexBuffer<ProximalVertex> UniqueVertices;
228 UniqueVertices uniqueVertices;
230 uniqueVertexIndices.reserve(faceVertices.size());
231 uniqueVertices.reserve(faceVertices.size());
234 ProximalVertexArray vertexRings;
235 vertexRings.resize(faceVertices.size());
238 for(std::size_t i=0; i<faceVertices.size(); ++i)
240 vertexRings[i].m_next = vertexRings.data() + absoluteIndex(next_vertex(m_faces, faceVertices[i]));
245 UniqueVertexBuffer<ProximalVertex> inserter(uniqueVertices);
246 for(ProximalVertexArray::iterator i = vertexRings.begin(); i != vertexRings.end(); ++i)
248 uniqueVertexIndices.insert(inserter.insert(ProximalVertex(&(*i))));
254 m_select_vertices.reserve(uniqueVertices.size());
255 for(UniqueVertices::iterator i = uniqueVertices.begin(); i != uniqueVertices.end(); ++i)
257 vertex_push_back(faceVertices[ProximalVertexArray_index(vertexRings, (*i))]);
262 m_uniqueVertexPoints.resize(uniqueVertices.size());
263 for(std::size_t i=0; i<uniqueVertices.size(); ++i)
265 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index(vertexRings, uniqueVertices[i])];
267 const Winding& winding = m_faces[faceVertex.getFace()]->getWinding();
268 m_uniqueVertexPoints[i] = pointvertex_for_windingpoint(winding[faceVertex.getVertex()].vertex, colour_vertex);
273 if((uniqueVertices.size() + faces_size) - uniqueEdges.size() != 2)
275 globalErrorStream() << "Final B-Rep: inconsistent vertex count\n";
278 #if BRUSH_CONNECTIVITY_DEBUG
279 if((uniqueVertices.size() + faces_size) - uniqueEdges.size() != 2)
281 for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
283 std::size_t faceIndex = std::distance(m_faces.begin(), i);
285 if(!(*i)->contributes())
287 globalOutputStream() << "face: " << Unsigned(faceIndex) << " does not contribute\n";
290 Winding_printConnectivity((*i)->getWinding());
295 // edge-index list for wireframe rendering
297 m_edge_indices.resize(uniqueEdgeIndices.size());
299 for(std::size_t i=0, count=0; i<m_faces.size(); ++i)
301 const Winding& winding = m_faces[i]->getWinding();
302 for(std::size_t j = 0; j < winding.numpoints; ++j)
304 const RenderIndex edge_index = uniqueEdgeIndices[count+j];
306 m_edge_indices[edge_index].first = uniqueVertexIndices[count + j];
307 m_edge_indices[edge_index].second = uniqueVertexIndices[count + Winding_next(winding, j)];
309 count += winding.numpoints;
315 m_faceCentroidPoints.resize(m_faces.size());
316 for(std::size_t i=0; i<m_faces.size(); ++i)
318 m_faces[i]->construct_centroid();
319 m_faceCentroidPoints[i] = pointvertex_for_windingpoint(m_faces[i]->centroid(), colour_vertex);
326 class FaceFilterWrapper : public Filter
328 FaceFilter& m_filter;
332 FaceFilterWrapper(FaceFilter& filter, bool invert) :
337 void setActive(bool active)
345 bool filter(const Face& face)
347 return m_invert ^ m_filter.filter(face);
352 typedef std::list<FaceFilterWrapper> FaceFilters;
353 FaceFilters g_faceFilters;
355 void add_face_filter(FaceFilter& filter, int mask, bool invert)
357 g_faceFilters.push_back(FaceFilterWrapper(filter, invert));
358 GlobalFilterSystem().addFilter(g_faceFilters.back(), mask);
361 bool face_filtered(Face& face)
363 for(FaceFilters::iterator i = g_faceFilters.begin(); i != g_faceFilters.end(); ++i)
365 if((*i).active() && (*i).filter(face))
374 class BrushFilterWrapper : public Filter
378 BrushFilter& m_filter;
380 BrushFilterWrapper(BrushFilter& filter, bool invert) : m_invert(invert), m_filter(filter)
383 void setActive(bool active)
391 bool filter(const Brush& brush)
393 return m_invert ^ m_filter.filter(brush);
398 typedef std::list<BrushFilterWrapper> BrushFilters;
399 BrushFilters g_brushFilters;
401 void add_brush_filter(BrushFilter& filter, int mask, bool invert)
403 g_brushFilters.push_back(BrushFilterWrapper(filter, invert));
404 GlobalFilterSystem().addFilter(g_brushFilters.back(), mask);
407 bool brush_filtered(Brush& brush)
409 for(BrushFilters::iterator i = g_brushFilters.begin(); i != g_brushFilters.end(); ++i)
411 if((*i).active() && (*i).filter(brush))