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