added plugins to win32/linux installers; fixed crash when plugins are disabled
[xonotic/netradiant.git] / radiant / brush.cpp
1 /*
2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
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 "brush.h"
23 #include "signal/signal.h"
24
25 Signal0 g_brushTextureChangedCallbacks;
26
27 void Brush_addTextureChangedCallback(const SignalHandler& handler)
28 {
29   g_brushTextureChangedCallbacks.connectLast(handler);
30 }
31
32 void Brush_textureChanged()
33 {
34   g_brushTextureChangedCallbacks();
35 }
36
37 QuantiseFunc Face::m_quantise;
38 EBrushType Face::m_type;
39 EBrushType FacePlane::m_type;
40 bool g_brush_texturelock_enabled = false;
41
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;
48
49 FaceInstanceSet g_SelectedFaceInstances;
50
51
52 struct SListNode
53 {
54   SListNode* m_next;
55 };
56
57 class ProximalVertex
58 {
59 public:
60   const SListNode* m_vertices;
61
62   ProximalVertex(const SListNode* next)
63     : m_vertices(next)
64   {
65   }
66
67   bool operator<(const ProximalVertex& other) const
68   {
69     if(!(operator==(other)))
70     {
71       return m_vertices < other.m_vertices;
72     }
73     return false;
74   }
75   bool operator==(const ProximalVertex& other) const
76   {
77     const SListNode* v = m_vertices;
78     std::size_t DEBUG_LOOP = 0;
79     do
80     {
81       if(v == other.m_vertices)
82         return true;
83       v = v->m_next;
84       //ASSERT_MESSAGE(DEBUG_LOOP < c_brush_maxFaces, "infinite loop");
85       if(!(DEBUG_LOOP < c_brush_maxFaces))
86       {
87         break;
88       }
89       ++DEBUG_LOOP;
90     }
91     while(v != m_vertices);
92     return false;
93   }
94 };
95
96 typedef Array<SListNode> ProximalVertexArray;
97 std::size_t ProximalVertexArray_index(const ProximalVertexArray& array, const ProximalVertex& vertex)
98 {
99   return vertex.m_vertices - array.data();
100 }
101
102
103
104 inline bool Brush_isBounded(const Brush& brush)
105 {
106   for(Brush::const_iterator i = brush.begin(); i != brush.end(); ++i)
107   {
108     if(!(*i)->is_bounded())
109     {
110       return false;
111     }
112   }
113   return true;
114 }
115
116 void Brush::buildBRep()
117 {
118   bool degenerate = buildWindings();
119
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)
123   {
124     if((*i)->contributes())
125     {
126       ++faces_size;
127     }
128     faceVerticesCount += (*i)->getWinding().numpoints;
129   }
130
131   if(degenerate || faces_size < 4 || faceVerticesCount != (faceVerticesCount>>1)<<1) // sum of vertices for each face of a valid polyhedron is always even
132   {
133     m_uniqueVertexPoints.resize(0);
134
135     vertex_clear();
136     edge_clear();
137
138     m_edge_indices.resize(0);
139     m_edge_faces.resize(0);
140
141     m_faceCentroidPoints.resize(0);
142     m_uniqueEdgePoints.resize(0);
143     m_uniqueVertexPoints.resize(0);
144
145     for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
146     {
147       (*i)->getWinding().resize(0);
148     }
149   }
150   else
151   {
152     {
153       typedef std::vector<FaceVertexId> FaceVertices;
154       FaceVertices faceVertices;
155       faceVertices.reserve(faceVerticesCount);
156
157       {
158         for(std::size_t i = 0; i != m_faces.size(); ++i)
159         {
160           for(std::size_t j = 0; j < m_faces[i]->getWinding().numpoints; ++j)
161           {
162             faceVertices.push_back(FaceVertexId(i, j));
163           }
164         }
165       }
166
167       IndexBuffer uniqueEdgeIndices;
168       typedef VertexBuffer<ProximalVertex> UniqueEdges;
169       UniqueEdges uniqueEdges;
170
171       uniqueEdgeIndices.reserve(faceVertices.size());
172       uniqueEdges.reserve(faceVertices.size());
173
174       {
175         ProximalVertexArray edgePairs;
176         edgePairs.resize(faceVertices.size());
177
178         {
179           for(std::size_t i=0; i<faceVertices.size(); ++i)
180           {
181             edgePairs[i].m_next = edgePairs.data() + absoluteIndex(next_edge(m_faces, faceVertices[i]));
182           }
183         }
184
185         {
186           UniqueVertexBuffer<ProximalVertex> inserter(uniqueEdges);
187           for(ProximalVertexArray::iterator i = edgePairs.begin(); i != edgePairs.end(); ++i)
188           {
189             uniqueEdgeIndices.insert(inserter.insert(ProximalVertex(&(*i))));
190           }
191         }
192
193         {
194           edge_clear();
195           m_select_edges.reserve(uniqueEdges.size());
196           for(UniqueEdges::iterator i = uniqueEdges.begin(); i != uniqueEdges.end(); ++i)
197           {
198             edge_push_back(faceVertices[ProximalVertexArray_index(edgePairs, *i)]);
199           }
200         }
201
202         {
203           m_edge_faces.resize(uniqueEdges.size());
204           for(std::size_t i=0; i<uniqueEdges.size(); ++i)
205           {
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);
208           }
209         }
210
211         {
212           m_uniqueEdgePoints.resize(uniqueEdges.size());
213           for(std::size_t i=0; i<uniqueEdges.size(); ++i)
214           {
215             FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index(edgePairs, uniqueEdges[i])];
216
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);
220           }
221         }
222
223       }
224     
225
226       IndexBuffer uniqueVertexIndices;
227       typedef VertexBuffer<ProximalVertex> UniqueVertices;
228       UniqueVertices uniqueVertices;
229
230       uniqueVertexIndices.reserve(faceVertices.size());
231       uniqueVertices.reserve(faceVertices.size());
232
233       {
234         ProximalVertexArray vertexRings;
235         vertexRings.resize(faceVertices.size());
236
237         {
238           for(std::size_t i=0; i<faceVertices.size(); ++i)
239           {
240             vertexRings[i].m_next = vertexRings.data() + absoluteIndex(next_vertex(m_faces, faceVertices[i]));
241           }
242         }
243
244         {
245           UniqueVertexBuffer<ProximalVertex> inserter(uniqueVertices);
246           for(ProximalVertexArray::iterator i = vertexRings.begin(); i != vertexRings.end(); ++i)
247           {
248             uniqueVertexIndices.insert(inserter.insert(ProximalVertex(&(*i))));
249           }
250         }
251
252         {
253           vertex_clear();
254           m_select_vertices.reserve(uniqueVertices.size());  
255           for(UniqueVertices::iterator i = uniqueVertices.begin(); i != uniqueVertices.end(); ++i)
256           {
257             vertex_push_back(faceVertices[ProximalVertexArray_index(vertexRings, (*i))]);
258           }
259         }
260
261         {
262           m_uniqueVertexPoints.resize(uniqueVertices.size());
263           for(std::size_t i=0; i<uniqueVertices.size(); ++i)
264           {
265             FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index(vertexRings, uniqueVertices[i])];
266
267             const Winding& winding = m_faces[faceVertex.getFace()]->getWinding();
268             m_uniqueVertexPoints[i] = pointvertex_for_windingpoint(winding[faceVertex.getVertex()].vertex, colour_vertex);
269           }
270         }
271       }
272
273       if((uniqueVertices.size() + faces_size) - uniqueEdges.size() != 2)
274       {
275         globalErrorStream() << "Final B-Rep: inconsistent vertex count\n";
276       }
277
278 #if BRUSH_CONNECTIVITY_DEBUG
279       if((uniqueVertices.size() + faces_size) - uniqueEdges.size() != 2)
280       {
281         for(Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i)
282         {
283           std::size_t faceIndex = std::distance(m_faces.begin(), i);
284
285           if(!(*i)->contributes())
286           {
287             globalOutputStream() << "face: " << Unsigned(faceIndex) << " does not contribute\n";
288           }
289
290           Winding_printConnectivity((*i)->getWinding());
291         }
292       }
293 #endif
294
295       // edge-index list for wireframe rendering
296       {
297         m_edge_indices.resize(uniqueEdgeIndices.size());
298
299         for(std::size_t i=0, count=0; i<m_faces.size(); ++i)
300         {
301           const Winding& winding = m_faces[i]->getWinding();
302           for(std::size_t j = 0; j < winding.numpoints; ++j)
303           {
304             const RenderIndex edge_index = uniqueEdgeIndices[count+j];
305
306             m_edge_indices[edge_index].first = uniqueVertexIndices[count + j];
307             m_edge_indices[edge_index].second = uniqueVertexIndices[count + Winding_next(winding, j)];
308           }
309           count += winding.numpoints;
310         }
311       }
312     }
313
314     {
315       m_faceCentroidPoints.resize(m_faces.size());
316       for(std::size_t i=0; i<m_faces.size(); ++i)
317       {
318         m_faces[i]->construct_centroid();
319         m_faceCentroidPoints[i] = pointvertex_for_windingpoint(m_faces[i]->centroid(), colour_vertex);
320       }
321     }
322   }
323 }
324
325
326 class FaceFilterWrapper : public Filter
327 {
328   FaceFilter& m_filter;
329   bool m_active;
330   bool m_invert;
331 public:
332   FaceFilterWrapper(FaceFilter& filter, bool invert) :
333     m_filter(filter),
334     m_invert(invert)
335   {
336   }
337   void setActive(bool active)
338   {
339     m_active = active;
340   }
341   bool active()
342   {
343     return m_active;
344   }
345   bool filter(const Face& face)
346   {
347     return m_invert ^ m_filter.filter(face);
348   }
349 };
350
351
352 typedef std::list<FaceFilterWrapper> FaceFilters;
353 FaceFilters g_faceFilters;
354
355 void add_face_filter(FaceFilter& filter, int mask, bool invert)
356 {
357   g_faceFilters.push_back(FaceFilterWrapper(filter, invert));
358   GlobalFilterSystem().addFilter(g_faceFilters.back(), mask);
359 }
360
361 bool face_filtered(Face& face)
362 {
363   for(FaceFilters::iterator i = g_faceFilters.begin(); i != g_faceFilters.end(); ++i)
364   {
365     if((*i).active() && (*i).filter(face))
366     {
367       return true;
368     }
369   }
370   return false;
371 }
372
373
374 class BrushFilterWrapper : public Filter
375 {
376   bool m_active;
377   bool m_invert;
378   BrushFilter& m_filter;
379 public:
380   BrushFilterWrapper(BrushFilter& filter, bool invert) : m_invert(invert), m_filter(filter)
381   {
382   }
383   void setActive(bool active)
384   {
385     m_active = active;
386   }
387   bool active()
388   {
389     return m_active;
390   }
391   bool filter(const Brush& brush)
392   {
393     return m_invert ^ m_filter.filter(brush);
394   }
395 };
396
397
398 typedef std::list<BrushFilterWrapper> BrushFilters;
399 BrushFilters g_brushFilters;
400
401 void add_brush_filter(BrushFilter& filter, int mask, bool invert)
402 {
403   g_brushFilters.push_back(BrushFilterWrapper(filter, invert));
404   GlobalFilterSystem().addFilter(g_brushFilters.back(), mask);
405 }
406
407 bool brush_filtered(Brush& brush)
408 {
409   for(BrushFilters::iterator i = g_brushFilters.begin(); i != g_brushFilters.end(); ++i)
410   {
411     if((*i).active() && (*i).filter(brush))
412     {
413       return true;
414     }
415   }
416   return false;
417 }
418
419