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 ){
28 g_brushTextureChangedCallbacks.connectLast( handler );
31 void Brush_textureChanged(){
32 g_brushTextureChangedCallbacks();
35 QuantiseFunc Face::m_quantise;
36 EBrushType Face::m_type;
37 EBrushType FacePlane::m_type;
38 bool g_brush_texturelock_enabled = false;
40 EBrushType Brush::m_type;
41 double Brush::m_maxWorldCoord = 0;
42 Shader* Brush::m_state_point;
43 Shader* BrushClipPlane::m_state = 0;
44 Shader* BrushInstance::m_state_selpoint;
45 Counter* BrushInstance::m_counter = 0;
47 FaceInstanceSet g_SelectedFaceInstances;
58 const SListNode* m_vertices;
60 ProximalVertex( const SListNode* next )
64 bool operator<( const ProximalVertex& other ) const {
65 if ( !( operator==( other ) ) ) {
66 return m_vertices < other.m_vertices;
70 bool operator==( const ProximalVertex& other ) const {
71 const SListNode* v = m_vertices;
72 std::size_t DEBUG_LOOP = 0;
75 if ( v == other.m_vertices ) {
79 //ASSERT_MESSAGE(DEBUG_LOOP < c_brush_maxFaces, "infinite loop");
80 if ( !( DEBUG_LOOP < c_brush_maxFaces ) ) {
85 while ( v != m_vertices );
90 typedef Array<SListNode> ProximalVertexArray;
91 std::size_t ProximalVertexArray_index( const ProximalVertexArray& array, const ProximalVertex& vertex ){
92 return vertex.m_vertices - array.data();
97 inline bool Brush_isBounded( const Brush& brush ){
98 for ( Brush::const_iterator i = brush.begin(); i != brush.end(); ++i )
100 if ( !( *i )->is_bounded() ) {
107 void Brush::buildBRep(){
108 bool degenerate = buildWindings();
110 std::size_t faces_size = 0;
111 std::size_t faceVerticesCount = 0;
112 for ( Faces::const_iterator i = m_faces.begin(); i != m_faces.end(); ++i )
114 if ( ( *i )->contributes() ) {
117 faceVerticesCount += ( *i )->getWinding().numpoints;
120 if ( degenerate || faces_size < 4 || faceVerticesCount != ( faceVerticesCount >> 1 ) << 1 ) { // sum of vertices for each face of a valid polyhedron is always even
121 m_uniqueVertexPoints.resize( 0 );
126 m_edge_indices.resize( 0 );
127 m_edge_faces.resize( 0 );
129 m_faceCentroidPoints.resize( 0 );
130 m_uniqueEdgePoints.resize( 0 );
131 m_uniqueVertexPoints.resize( 0 );
133 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
135 ( *i )->getWinding().resize( 0 );
141 typedef std::vector<FaceVertexId> FaceVertices;
142 FaceVertices faceVertices;
143 faceVertices.reserve( faceVerticesCount );
146 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 )
150 faceVertices.push_back( FaceVertexId( i, j ) );
155 IndexBuffer uniqueEdgeIndices;
156 typedef VertexBuffer<ProximalVertex> UniqueEdges;
157 UniqueEdges uniqueEdges;
159 uniqueEdgeIndices.reserve( faceVertices.size() );
160 uniqueEdges.reserve( faceVertices.size() );
163 ProximalVertexArray edgePairs;
164 edgePairs.resize( faceVertices.size() );
167 for ( std::size_t i = 0; i < faceVertices.size(); ++i )
169 edgePairs[i].m_next = edgePairs.data() + absoluteIndex( next_edge( m_faces, faceVertices[i] ) );
174 UniqueVertexBuffer<ProximalVertex> inserter( uniqueEdges );
175 for ( ProximalVertexArray::iterator i = edgePairs.begin(); i != edgePairs.end(); ++i )
177 uniqueEdgeIndices.insert( inserter.insert( ProximalVertex( &( *i ) ) ) );
183 m_select_edges.reserve( uniqueEdges.size() );
184 for ( UniqueEdges::iterator i = uniqueEdges.begin(); i != uniqueEdges.end(); ++i )
186 edge_push_back( faceVertices[ProximalVertexArray_index( edgePairs, *i )] );
191 m_edge_faces.resize( uniqueEdges.size() );
192 for ( std::size_t i = 0; i < uniqueEdges.size(); ++i )
194 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index( edgePairs, uniqueEdges[i] )];
195 m_edge_faces[i] = EdgeFaces( faceVertex.getFace(), m_faces[faceVertex.getFace()]->getWinding()[faceVertex.getVertex()].adjacent );
200 m_uniqueEdgePoints.resize( uniqueEdges.size() );
201 for ( std::size_t i = 0; i < uniqueEdges.size(); ++i )
203 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index( edgePairs, uniqueEdges[i] )];
205 const Winding& w = m_faces[faceVertex.getFace()]->getWinding();
206 Vector3 edge = vector3_mid( w[faceVertex.getVertex()].vertex, w[Winding_next( w, faceVertex.getVertex() )].vertex );
207 m_uniqueEdgePoints[i] = pointvertex_for_windingpoint( edge, colour_vertex );
214 IndexBuffer uniqueVertexIndices;
215 typedef VertexBuffer<ProximalVertex> UniqueVertices;
216 UniqueVertices uniqueVertices;
218 uniqueVertexIndices.reserve( faceVertices.size() );
219 uniqueVertices.reserve( faceVertices.size() );
222 ProximalVertexArray vertexRings;
223 vertexRings.resize( faceVertices.size() );
226 for ( std::size_t i = 0; i < faceVertices.size(); ++i )
228 vertexRings[i].m_next = vertexRings.data() + absoluteIndex( next_vertex( m_faces, faceVertices[i] ) );
233 UniqueVertexBuffer<ProximalVertex> inserter( uniqueVertices );
234 for ( ProximalVertexArray::iterator i = vertexRings.begin(); i != vertexRings.end(); ++i )
236 uniqueVertexIndices.insert( inserter.insert( ProximalVertex( &( *i ) ) ) );
242 m_select_vertices.reserve( uniqueVertices.size() );
243 for ( UniqueVertices::iterator i = uniqueVertices.begin(); i != uniqueVertices.end(); ++i )
245 vertex_push_back( faceVertices[ProximalVertexArray_index( vertexRings, ( *i ) )] );
250 m_uniqueVertexPoints.resize( uniqueVertices.size() );
251 for ( std::size_t i = 0; i < uniqueVertices.size(); ++i )
253 FaceVertexId faceVertex = faceVertices[ProximalVertexArray_index( vertexRings, uniqueVertices[i] )];
255 const Winding& winding = m_faces[faceVertex.getFace()]->getWinding();
256 m_uniqueVertexPoints[i] = pointvertex_for_windingpoint( winding[faceVertex.getVertex()].vertex, colour_vertex );
261 if ( ( uniqueVertices.size() + faces_size ) - uniqueEdges.size() != 2 ) {
262 globalErrorStream() << "Final B-Rep: inconsistent vertex count\n";
265 #if BRUSH_CONNECTIVITY_DEBUG
266 if ( ( uniqueVertices.size() + faces_size ) - uniqueEdges.size() != 2 ) {
267 for ( Faces::iterator i = m_faces.begin(); i != m_faces.end(); ++i )
269 std::size_t faceIndex = std::distance( m_faces.begin(), i );
271 if ( !( *i )->contributes() ) {
272 globalOutputStream() << "face: " << Unsigned( faceIndex ) << " does not contribute\n";
275 Winding_printConnectivity( ( *i )->getWinding() );
280 // edge-index list for wireframe rendering
282 m_edge_indices.resize( uniqueEdgeIndices.size() );
284 for ( std::size_t i = 0, count = 0; i < m_faces.size(); ++i )
286 const Winding& winding = m_faces[i]->getWinding();
287 for ( std::size_t j = 0; j < winding.numpoints; ++j )
289 const RenderIndex edge_index = uniqueEdgeIndices[count + j];
291 m_edge_indices[edge_index].first = uniqueVertexIndices[count + j];
292 m_edge_indices[edge_index].second = uniqueVertexIndices[count + Winding_next( winding, j )];
294 count += winding.numpoints;
300 m_faceCentroidPoints.resize( m_faces.size() );
301 for ( std::size_t i = 0; i < m_faces.size(); ++i )
303 m_faces[i]->construct_centroid();
304 m_faceCentroidPoints[i] = pointvertex_for_windingpoint( m_faces[i]->centroid(), colour_vertex );
311 class FaceFilterWrapper : public Filter
313 FaceFilter& m_filter;
317 FaceFilterWrapper( FaceFilter& filter, bool invert ) :
321 void setActive( bool active ){
327 bool filter( const Face& face ){
328 return m_invert ^ m_filter.filter( face );
333 typedef std::list<FaceFilterWrapper> FaceFilters;
334 FaceFilters g_faceFilters;
336 void add_face_filter( FaceFilter& filter, int mask, bool invert ){
337 g_faceFilters.push_back( FaceFilterWrapper( filter, invert ) );
338 GlobalFilterSystem().addFilter( g_faceFilters.back(), mask );
341 bool face_filtered( Face& face ){
342 for ( FaceFilters::iterator i = g_faceFilters.begin(); i != g_faceFilters.end(); ++i )
344 if ( ( *i ).active() && ( *i ).filter( face ) ) {
352 class BrushFilterWrapper : public Filter
356 BrushFilter& m_filter;
358 BrushFilterWrapper( BrushFilter& filter, bool invert ) : m_invert( invert ), m_filter( filter ){
360 void setActive( bool active ){
366 bool filter( const Brush& brush ){
367 return m_invert ^ m_filter.filter( brush );
372 typedef std::list<BrushFilterWrapper> BrushFilters;
373 BrushFilters g_brushFilters;
375 void add_brush_filter( BrushFilter& filter, int mask, bool invert ){
376 g_brushFilters.push_back( BrushFilterWrapper( filter, invert ) );
377 GlobalFilterSystem().addFilter( g_brushFilters.back(), mask );
380 bool brush_filtered( Brush& brush ){
381 for ( BrushFilters::iterator i = g_brushFilters.begin(); i != g_brushFilters.end(); ++i )
383 if ( ( *i ).active() && ( *i ).filter( brush ) ) {