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 = true;
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;
56 class ProximalVertex {
58 const SListNode *m_vertices;
60 ProximalVertex(const SListNode *next)
65 bool operator<(const ProximalVertex &other) const
67 if (!(operator==(other))) {
68 return m_vertices < other.m_vertices;
73 bool operator==(const ProximalVertex &other) const
75 const SListNode *v = m_vertices;
76 std::size_t DEBUG_LOOP = 0;
78 if (v == other.m_vertices) {
82 //ASSERT_MESSAGE(DEBUG_LOOP < c_brush_maxFaces, "infinite loop");
83 if (!(DEBUG_LOOP < c_brush_maxFaces)) {
87 } while (v != m_vertices);
92 typedef Array<SListNode> ProximalVertexArray;
94 std::size_t ProximalVertexArray_index(const ProximalVertexArray &array, const ProximalVertex &vertex)
96 return vertex.m_vertices - array.data();
100 inline bool Brush_isBounded(const Brush &brush)
102 for (Brush::const_iterator i = brush.begin(); i != brush.end(); ++i) {
103 if (!(*i)->is_bounded()) {
110 void Brush::buildBRep()
112 bool degenerate = buildWindings();
114 std::size_t faces_size = 0;
115 std::size_t faceVerticesCount = 0;
116 for (Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
117 if ((*i)->contributes()) {
120 faceVerticesCount += (*i)->getWinding().numpoints;
123 if (degenerate || faces_size < 4 || faceVerticesCount != (faceVerticesCount >> 1)
124 << 1) { // sum of vertices for each face of a valid polyhedron is always even
125 m_uniqueVertexPoints.resize(0);
130 m_edge_indices.resize(0);
131 m_edge_faces.resize(0);
133 m_faceCentroidPoints.resize(0);
134 m_uniqueEdgePoints.resize(0);
135 m_uniqueVertexPoints.resize(0);
137 for (Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i) {
138 (*i)->getWinding().resize(0);
142 typedef std::vector<FaceVertexId> FaceVertices;
143 FaceVertices faceVertices;
144 faceVertices.reserve(faceVerticesCount);
147 for (std::size_t i = 0; i != m_faces.size(); ++i) {
148 for (std::size_t j = 0; j < m_faces[i]->getWinding().numpoints; ++j) {
149 faceVertices.push_back(FaceVertexId(i, j));
154 IndexBuffer uniqueEdgeIndices;
155 typedef VertexBuffer<ProximalVertex> UniqueEdges;
156 UniqueEdges uniqueEdges;
158 uniqueEdgeIndices.reserve(faceVertices.size());
159 uniqueEdges.reserve(faceVertices.size());
162 ProximalVertexArray edgePairs;
163 edgePairs.resize(faceVertices.size());
166 for (std::size_t i = 0; i < faceVertices.size(); ++i) {
167 edgePairs[i].m_next = edgePairs.data() + absoluteIndex(next_edge(m_faces, faceVertices[i]));
172 UniqueVertexBuffer<ProximalVertex> inserter(uniqueEdges);
173 for (ProximalVertexArray::iterator i = edgePairs.begin(); i != edgePairs.end(); ++i) {
174 uniqueEdgeIndices.insert(inserter.insert(ProximalVertex(&(*i))));
180 m_select_edges.reserve(uniqueEdges.size());
181 for (UniqueEdges::iterator i = uniqueEdges.begin(); i != uniqueEdges.end(); ++i) {
182 edge_push_back(faceVertices[ProximalVertexArray_index(edgePairs, *i)]);
187 m_edge_faces.resize(uniqueEdges.size());
188 for (std::size_t i = 0; i < uniqueEdges.size(); ++i) {
189 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index(edgePairs, uniqueEdges[i])];
190 m_edge_faces[i] = EdgeFaces(faceVertex.getFace(),
191 m_faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent);
196 m_uniqueEdgePoints.resize(uniqueEdges.size());
197 for (std::size_t i = 0; i < uniqueEdges.size(); ++i) {
198 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index(edgePairs, uniqueEdges[i])];
200 const Winding &w = m_faces[faceVertex.getFace()]->getWinding();
201 Vector3 edge = vector3_mid(w[faceVertex.getVertex()].vertex,
202 w[Winding_next(w, faceVertex.getVertex())].vertex);
203 m_uniqueEdgePoints[i] = pointvertex_for_windingpoint(edge, colour_vertex);
210 IndexBuffer uniqueVertexIndices;
211 typedef VertexBuffer<ProximalVertex> UniqueVertices;
212 UniqueVertices uniqueVertices;
214 uniqueVertexIndices.reserve(faceVertices.size());
215 uniqueVertices.reserve(faceVertices.size());
218 ProximalVertexArray vertexRings;
219 vertexRings.resize(faceVertices.size());
222 for (std::size_t i = 0; i < faceVertices.size(); ++i) {
223 vertexRings[i].m_next =
224 vertexRings.data() + absoluteIndex(next_vertex(m_faces, faceVertices[i]));
229 UniqueVertexBuffer<ProximalVertex> inserter(uniqueVertices);
230 for (ProximalVertexArray::iterator i = vertexRings.begin(); i != vertexRings.end(); ++i) {
231 uniqueVertexIndices.insert(inserter.insert(ProximalVertex(&(*i))));
237 m_select_vertices.reserve(uniqueVertices.size());
238 for (UniqueVertices::iterator i = uniqueVertices.begin(); i != uniqueVertices.end(); ++i) {
239 vertex_push_back(faceVertices[ProximalVertexArray_index(vertexRings, (*i))]);
244 m_uniqueVertexPoints.resize(uniqueVertices.size());
245 for (std::size_t i = 0; i < uniqueVertices.size(); ++i) {
246 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index(vertexRings,
249 const Winding &winding = m_faces[faceVertex.getFace()]->getWinding();
250 m_uniqueVertexPoints[i] = pointvertex_for_windingpoint(winding[faceVertex.getVertex()].vertex,
256 if ((uniqueVertices.size() + faces_size) - uniqueEdges.size() != 2) {
257 globalErrorStream() << "Final B-Rep: inconsistent vertex count\n";
260 #if BRUSH_CONNECTIVITY_DEBUG
261 if ( ( uniqueVertices.size() + faces_size ) - uniqueEdges.size() != 2 ) {
262 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
264 std::size_t faceIndex = std::distance( m_faces.begin(), i );
266 if ( !( *i )->contributes() ) {
267 globalOutputStream() << "face: " << Unsigned( faceIndex ) << " does not contribute\n";
270 Winding_printConnectivity( ( *i )->getWinding() );
275 // edge-index list for wireframe rendering
277 m_edge_indices.resize(uniqueEdgeIndices.size());
279 for (std::size_t i = 0, count = 0; i < m_faces.size(); ++i) {
280 const Winding &winding = m_faces[i]->getWinding();
281 for (std::size_t j = 0; j < winding.numpoints; ++j) {
282 const RenderIndex edge_index = uniqueEdgeIndices[count + j];
284 m_edge_indices[edge_index].first = uniqueVertexIndices[count + j];
285 m_edge_indices[edge_index].second = uniqueVertexIndices[count + Winding_next(winding, j)];
287 count += winding.numpoints;
293 m_faceCentroidPoints.resize(m_faces.size());
294 for (std::size_t i = 0; i < m_faces.size(); ++i) {
295 m_faces[i]->construct_centroid();
296 m_faceCentroidPoints[i] = pointvertex_for_windingpoint(m_faces[i]->centroid(), colour_vertex);
303 class FaceFilterWrapper : public Filter {
304 FaceFilter &m_filter;
308 FaceFilterWrapper(FaceFilter &filter, bool invert) :
314 void setActive(bool active)
324 bool filter(const Face &face)
326 return m_invert ^ m_filter.filter(face);
331 typedef std::list<FaceFilterWrapper> FaceFilters;
332 FaceFilters g_faceFilters;
334 void add_face_filter(FaceFilter &filter, int mask, bool invert)
336 g_faceFilters.push_back(FaceFilterWrapper(filter, invert));
337 GlobalFilterSystem().addFilter(g_faceFilters.back(), mask);
340 bool face_filtered(Face &face)
342 for (FaceFilters::iterator i = g_faceFilters.begin(); i != g_faceFilters.end(); ++i) {
343 if ((*i).active() && (*i).filter(face)) {
351 class BrushFilterWrapper : public Filter {
354 BrushFilter &m_filter;
356 BrushFilterWrapper(BrushFilter &filter, bool invert) : m_invert(invert), m_filter(filter)
360 void setActive(bool active)
370 bool filter(const Brush &brush)
372 return m_invert ^ m_filter.filter(brush);
377 typedef std::list<BrushFilterWrapper> BrushFilters;
378 BrushFilters g_brushFilters;
380 void add_brush_filter(BrushFilter &filter, int mask, bool invert)
382 g_brushFilters.push_back(BrushFilterWrapper(filter, invert));
383 GlobalFilterSystem().addFilter(g_brushFilters.back(), mask);
386 bool brush_filtered(Brush &brush)
388 for (BrushFilters::iterator i = g_brushFilters.begin(); i != g_brushFilters.end(); ++i) {
389 if ((*i).active() && (*i).filter(brush)) {