]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/brushmanip.cpp
Q3map2:
[xonotic/netradiant.git] / radiant / brushmanip.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 "brushmanip.h"
23
24
25 #include "gtkutil/widget.h"
26 #include "gtkutil/menu.h"
27 #include "gtkmisc.h"
28 #include "brushnode.h"
29 #include "map.h"
30 #include "texwindow.h"
31 #include "gtkdlgs.h"
32 #include "commands.h"
33 #include "mainframe.h"
34 #include "dialog.h"
35 #include "xywindow.h"
36 #include "preferences.h"
37
38 #include <list>
39
40 void Brush_ConstructCuboid( Brush& brush, const AABB& bounds, const char* shader, const TextureProjection& projection ){
41         const unsigned char box[3][2] = { { 0, 1 }, { 2, 0 }, { 1, 2 } };
42         Vector3 mins( vector3_subtracted( bounds.origin, bounds.extents ) );
43         Vector3 maxs( vector3_added( bounds.origin, bounds.extents ) );
44
45         brush.clear();
46         brush.reserve( 6 );
47
48         {
49                 for ( int i = 0; i < 3; ++i )
50                 {
51                         Vector3 planepts1( maxs );
52                         Vector3 planepts2( maxs );
53                         planepts2[box[i][0]] = mins[box[i][0]];
54                         planepts1[box[i][1]] = mins[box[i][1]];
55
56                         brush.addPlane( maxs, planepts1, planepts2, shader, projection );
57                 }
58         }
59         {
60                 for ( int i = 0; i < 3; ++i )
61                 {
62                         Vector3 planepts1( mins );
63                         Vector3 planepts2( mins );
64                         planepts1[box[i][0]] = maxs[box[i][0]];
65                         planepts2[box[i][1]] = maxs[box[i][1]];
66
67                         brush.addPlane( mins, planepts1, planepts2, shader, projection );
68                 }
69         }
70 }
71
72 inline float max_extent( const Vector3& extents ){
73         return std::max( std::max( extents[0], extents[1] ), extents[2] );
74 }
75
76 inline float max_extent_2d( const Vector3& extents, int axis ){
77         switch ( axis )
78         {
79         case 0:
80                 return std::max( extents[1], extents[2] );
81         case 1:
82                 return std::max( extents[0], extents[2] );
83         default:
84                 return std::max( extents[0], extents[1] );
85         }
86 }
87
88 const std::size_t c_brushPrism_minSides = 3;
89 const std::size_t c_brushPrism_maxSides = c_brush_maxFaces - 2;
90 const char* const c_brushPrism_name = "brushPrism";
91
92 void Brush_ConstructPrism( Brush& brush, const AABB& bounds, std::size_t sides, int axis, const char* shader, const TextureProjection& projection ){
93         if ( sides < c_brushPrism_minSides ) {
94                 globalErrorStream() << c_brushPrism_name << ": sides " << Unsigned( sides ) << ": too few sides, minimum is " << Unsigned( c_brushPrism_minSides ) << "\n";
95                 return;
96         }
97         if ( sides > c_brushPrism_maxSides ) {
98                 globalErrorStream() << c_brushPrism_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushPrism_maxSides ) << "\n";
99                 return;
100         }
101
102         brush.clear();
103         brush.reserve( sides + 2 );
104
105         Vector3 mins( vector3_subtracted( bounds.origin, bounds.extents ) );
106         Vector3 maxs( vector3_added( bounds.origin, bounds.extents ) );
107
108         float radius = max_extent_2d( bounds.extents, axis );
109         const Vector3& mid = bounds.origin;
110         Vector3 planepts[3];
111
112         planepts[2][( axis + 1 ) % 3] = mins[( axis + 1 ) % 3];
113         planepts[2][( axis + 2 ) % 3] = mins[( axis + 2 ) % 3];
114         planepts[2][axis] = maxs[axis];
115         planepts[1][( axis + 1 ) % 3] = maxs[( axis + 1 ) % 3];
116         planepts[1][( axis + 2 ) % 3] = mins[( axis + 2 ) % 3];
117         planepts[1][axis] = maxs[axis];
118         planepts[0][( axis + 1 ) % 3] = maxs[( axis + 1 ) % 3];
119         planepts[0][( axis + 2 ) % 3] = maxs[( axis + 2 ) % 3];
120         planepts[0][axis] = maxs[axis];
121
122         brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
123
124         planepts[0][( axis + 1 ) % 3] = mins[( axis + 1 ) % 3];
125         planepts[0][( axis + 2 ) % 3] = mins[( axis + 2 ) % 3];
126         planepts[0][axis] = mins[axis];
127         planepts[1][( axis + 1 ) % 3] = maxs[( axis + 1 ) % 3];
128         planepts[1][( axis + 2 ) % 3] = mins[( axis + 2 ) % 3];
129         planepts[1][axis] = mins[axis];
130         planepts[2][( axis + 1 ) % 3] = maxs[( axis + 1 ) % 3];
131         planepts[2][( axis + 2 ) % 3] = maxs[( axis + 2 ) % 3];
132         planepts[2][axis] = mins[axis];
133
134         brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
135
136         for ( std::size_t i = 0 ; i < sides ; ++i )
137         {
138                 double sv = sin( i * 3.14159265 * 2 / sides );
139                 double cv = cos( i * 3.14159265 * 2 / sides );
140
141                 planepts[0][( axis + 1 ) % 3] = static_cast<float>( floor( mid[( axis + 1 ) % 3] + radius * cv + 0.5 ) );
142                 planepts[0][( axis + 2 ) % 3] = static_cast<float>( floor( mid[( axis + 2 ) % 3] + radius * sv + 0.5 ) );
143                 planepts[0][axis] = mins[axis];
144
145                 planepts[1][( axis + 1 ) % 3] = planepts[0][( axis + 1 ) % 3];
146                 planepts[1][( axis + 2 ) % 3] = planepts[0][( axis + 2 ) % 3];
147                 planepts[1][axis] = maxs[axis];
148
149                 planepts[2][( axis + 1 ) % 3] = static_cast<float>( floor( planepts[0][( axis + 1 ) % 3] - radius * sv + 0.5 ) );
150                 planepts[2][( axis + 2 ) % 3] = static_cast<float>( floor( planepts[0][( axis + 2 ) % 3] + radius * cv + 0.5 ) );
151                 planepts[2][axis] = maxs[axis];
152
153                 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
154         }
155 }
156
157 const std::size_t c_brushCone_minSides = 3;
158 const std::size_t c_brushCone_maxSides = 32;
159 const char* const c_brushCone_name = "brushCone";
160
161 void Brush_ConstructCone( Brush& brush, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection ){
162         if ( sides < c_brushCone_minSides ) {
163                 globalErrorStream() << c_brushCone_name << ": sides " << Unsigned( sides ) << ": too few sides, minimum is " << Unsigned( c_brushCone_minSides ) << "\n";
164                 return;
165         }
166         if ( sides > c_brushCone_maxSides ) {
167                 globalErrorStream() << c_brushCone_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushCone_maxSides ) << "\n";
168                 return;
169         }
170
171         brush.clear();
172         brush.reserve( sides + 1 );
173
174         Vector3 mins( vector3_subtracted( bounds.origin, bounds.extents ) );
175         Vector3 maxs( vector3_added( bounds.origin, bounds.extents ) );
176
177         float radius = max_extent( bounds.extents );
178         const Vector3& mid = bounds.origin;
179         Vector3 planepts[3];
180
181         planepts[0][0] = mins[0]; planepts[0][1] = mins[1]; planepts[0][2] = mins[2];
182         planepts[1][0] = maxs[0]; planepts[1][1] = mins[1]; planepts[1][2] = mins[2];
183         planepts[2][0] = maxs[0]; planepts[2][1] = maxs[1]; planepts[2][2] = mins[2];
184
185         brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
186
187         for ( std::size_t i = 0 ; i < sides ; ++i )
188         {
189                 double sv = sin( i * 3.14159265 * 2 / sides );
190                 double cv = cos( i * 3.14159265 * 2 / sides );
191
192                 planepts[0][0] = static_cast<float>( floor( mid[0] + radius * cv + 0.5 ) );
193                 planepts[0][1] = static_cast<float>( floor( mid[1] + radius * sv + 0.5 ) );
194                 planepts[0][2] = mins[2];
195
196                 planepts[1][0] = mid[0];
197                 planepts[1][1] = mid[1];
198                 planepts[1][2] = maxs[2];
199
200                 planepts[2][0] = static_cast<float>( floor( planepts[0][0] - radius * sv + 0.5 ) );
201                 planepts[2][1] = static_cast<float>( floor( planepts[0][1] + radius * cv + 0.5 ) );
202                 planepts[2][2] = maxs[2];
203
204                 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
205         }
206 }
207
208 const std::size_t c_brushSphere_minSides = 3;
209 const std::size_t c_brushSphere_maxSides = 31;
210 const char* const c_brushSphere_name = "brushSphere";
211
212 void Brush_ConstructSphere( Brush& brush, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection ){
213         if ( sides < c_brushSphere_minSides ) {
214                 globalErrorStream() << c_brushSphere_name << ": sides " << Unsigned( sides ) << ": too few sides, minimum is " << Unsigned( c_brushSphere_minSides ) << "\n";
215                 return;
216         }
217         if ( sides > c_brushSphere_maxSides ) {
218                 globalErrorStream() << c_brushSphere_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushSphere_maxSides ) << "\n";
219                 return;
220         }
221
222         brush.clear();
223         brush.reserve( sides * sides );
224
225         float radius = max_extent( bounds.extents );
226         const Vector3& mid = bounds.origin;
227         Vector3 planepts[3];
228
229         double dt = 2 * c_pi / sides;
230         double dp = c_pi / sides;
231         for ( std::size_t i = 0; i < sides; i++ )
232         {
233                 for ( std::size_t j = 0; j < sides - 1; j++ )
234                 {
235                         double t = i * dt;
236                         double p = float(j * dp - c_pi / 2);
237
238                         planepts[0] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t, p ), radius ) );
239                         planepts[1] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t, p + dp ), radius ) );
240                         planepts[2] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t + dt, p + dp ), radius ) );
241
242                         brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
243                 }
244         }
245
246         {
247                 double p = ( sides - 1 ) * dp - c_pi / 2;
248                 for ( std::size_t i = 0; i < sides; i++ )
249                 {
250                         double t = i * dt;
251
252                         planepts[0] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t, p ), radius ) );
253                         planepts[1] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t + dt, p + dp ), radius ) );
254                         planepts[2] = vector3_added( mid, vector3_scaled( vector3_for_spherical( t + dt, p ), radius ) );
255
256                         brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
257                 }
258         }
259 }
260
261 const std::size_t c_brushRock_minSides = 10;
262 const std::size_t c_brushRock_maxSides = 1000;
263 const char* const c_brushRock_name = "brushRock";
264
265 void Brush_ConstructRock( Brush& brush, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection ){
266         if ( sides < c_brushRock_minSides ) {
267                 globalErrorStream() << c_brushRock_name << ": sides " << Unsigned( sides ) << ": too few sides, minimum is " << Unsigned( c_brushRock_minSides ) << "\n";
268                 return;
269         }
270         if ( sides > c_brushRock_maxSides ) {
271                 globalErrorStream() << c_brushRock_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushRock_maxSides ) << "\n";
272                 return;
273         }
274
275         brush.clear();
276         brush.reserve( sides * sides );
277
278         float radius = max_extent( bounds.extents );
279         const Vector3& mid = bounds.origin;
280         Vector3 planepts[3];
281
282         for ( std::size_t j = 0; j < sides; j++ )
283         {
284                 planepts[0][0] = rand() - ( RAND_MAX / 2 );
285                 planepts[0][1] = rand() - ( RAND_MAX / 2 );
286                 planepts[0][2] = rand() - ( RAND_MAX / 2 );
287                 vector3_normalise( planepts[0] );
288
289                 // find two vectors that are perpendicular to planepts[0]
290                 ComputeAxisBase( planepts[0], planepts[1], planepts[2] );
291
292                 planepts[0] = vector3_added( mid, vector3_scaled( planepts[0], radius ) );
293                 planepts[1] = vector3_added( planepts[0], vector3_scaled( planepts[1], radius ) );
294                 planepts[2] = vector3_added( planepts[0], vector3_scaled( planepts[2], radius ) );
295
296 #if 0
297                 // make sure the orientation is right
298                 if ( vector3_dot( vector3_subtracted( planepts[0], mid ), vector3_cross( vector3_subtracted( planepts[1], mid ), vector3_subtracted( planepts[2], mid ) ) ) > 0 ) {
299                         Vector3 h;
300                         h = planepts[1];
301                         planepts[1] = planepts[2];
302                         planepts[2] = h;
303                         globalOutputStream() << "flip\n";
304                 }
305                 else{
306                         globalOutputStream() << "no flip\n";
307                 }
308 #endif
309
310                 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
311         }
312 }
313
314 int GetViewAxis(){
315         switch ( GlobalXYWnd_getCurrentViewType() )
316         {
317         case XY:
318                 return 2;
319         case XZ:
320                 return 1;
321         case YZ:
322                 return 0;
323         }
324         return 2;
325 }
326
327 void Brush_ConstructPrefab( Brush& brush, EBrushPrefab type, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection ){
328         switch ( type )
329         {
330         case eBrushCuboid:
331         {
332                 UndoableCommand undo( "brushCuboid" );
333
334                 Brush_ConstructCuboid( brush, bounds, shader, projection );
335         }
336         break;
337         case eBrushPrism:
338         {
339                 int axis = GetViewAxis();
340                 StringOutputStream command;
341                 command << c_brushPrism_name << " -sides " << Unsigned( sides ) << " -axis " << axis;
342                 UndoableCommand undo( command.c_str() );
343
344                 Brush_ConstructPrism( brush, bounds, sides, axis, shader, projection );
345         }
346         break;
347         case eBrushCone:
348         {
349                 StringOutputStream command;
350                 command << c_brushCone_name << " -sides " << Unsigned( sides );
351                 UndoableCommand undo( command.c_str() );
352
353                 Brush_ConstructCone( brush, bounds, sides, shader, projection );
354         }
355         break;
356         case eBrushSphere:
357         {
358                 StringOutputStream command;
359                 command << c_brushSphere_name << " -sides " << Unsigned( sides );
360                 UndoableCommand undo( command.c_str() );
361
362                 Brush_ConstructSphere( brush, bounds, sides, shader, projection );
363         }
364         break;
365         case eBrushRock:
366         {
367                 StringOutputStream command;
368                 command << c_brushRock_name << " -sides " << Unsigned( sides );
369                 UndoableCommand undo( command.c_str() );
370
371                 Brush_ConstructRock( brush, bounds, sides, shader, projection );
372         }
373         break;
374         }
375 }
376
377
378 void ConstructRegionBrushes( scene::Node* brushes[6], const Vector3& region_mins, const Vector3& region_maxs ){
379         {
380                 // set mins
381                 Vector3 mins( region_mins[0] - 32, region_mins[1] - 32, region_mins[2] - 32 );
382
383                 // vary maxs
384                 for ( std::size_t i = 0; i < 3; i++ )
385                 {
386                         Vector3 maxs( region_maxs[0] + 32, region_maxs[1] + 32, region_maxs[2] + 32 );
387                         maxs[i] = region_mins[i];
388                         Brush_ConstructCuboid( *Node_getBrush( *brushes[i] ), aabb_for_minmax( mins, maxs ), texdef_name_default(), TextureProjection() );
389                 }
390         }
391
392         {
393                 // set maxs
394                 Vector3 maxs( region_maxs[0] + 32, region_maxs[1] + 32, region_maxs[2] + 32 );
395
396                 // vary mins
397                 for ( std::size_t i = 0; i < 3; i++ )
398                 {
399                         Vector3 mins( region_mins[0] - 32, region_mins[1] - 32, region_mins[2] - 32 );
400                         mins[i] = region_maxs[i];
401                         Brush_ConstructCuboid( *Node_getBrush( *brushes[i + 3] ), aabb_for_minmax( mins, maxs ), texdef_name_default(), TextureProjection() );
402                 }
403         }
404 }
405
406
407 class FaceSetTexdef
408 {
409 const TextureProjection& m_projection;
410 public:
411 FaceSetTexdef( const TextureProjection& projection ) : m_projection( projection ){
412 }
413 void operator()( Face& face ) const {
414         face.SetTexdef( m_projection );
415 }
416 };
417
418 void Scene_BrushSetTexdef_Selected( scene::Graph& graph, const TextureProjection& projection ){
419         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceSetTexdef( projection ) );
420         SceneChangeNotify();
421 }
422
423 void Scene_BrushSetTexdef_Component_Selected( scene::Graph& graph, const TextureProjection& projection ){
424         Scene_ForEachSelectedBrushFace( graph, FaceSetTexdef( projection ) );
425         SceneChangeNotify();
426 }
427
428
429 class FaceSetFlags
430 {
431 const ContentsFlagsValue& m_projection;
432 public:
433 FaceSetFlags( const ContentsFlagsValue& flags ) : m_projection( flags ){
434 }
435 void operator()( Face& face ) const {
436         face.SetFlags( m_projection );
437 }
438 };
439
440 void Scene_BrushSetFlags_Selected( scene::Graph& graph, const ContentsFlagsValue& flags ){
441         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceSetFlags( flags ) );
442         SceneChangeNotify();
443 }
444
445 void Scene_BrushSetFlags_Component_Selected( scene::Graph& graph, const ContentsFlagsValue& flags ){
446         Scene_ForEachSelectedBrushFace( graph, FaceSetFlags( flags ) );
447         SceneChangeNotify();
448 }
449
450 class FaceShiftTexdef
451 {
452 float m_s, m_t;
453 public:
454 FaceShiftTexdef( float s, float t ) : m_s( s ), m_t( t ){
455 }
456 void operator()( Face& face ) const {
457         face.ShiftTexdef( m_s, m_t );
458 }
459 };
460
461 void Scene_BrushShiftTexdef_Selected( scene::Graph& graph, float s, float t ){
462         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceShiftTexdef( s, t ) );
463         SceneChangeNotify();
464 }
465
466 void Scene_BrushShiftTexdef_Component_Selected( scene::Graph& graph, float s, float t ){
467         Scene_ForEachSelectedBrushFace( graph, FaceShiftTexdef( s, t ) );
468         SceneChangeNotify();
469 }
470
471 class FaceScaleTexdef
472 {
473 float m_s, m_t;
474 public:
475 FaceScaleTexdef( float s, float t ) : m_s( s ), m_t( t ){
476 }
477 void operator()( Face& face ) const {
478         face.ScaleTexdef( m_s, m_t );
479 }
480 };
481
482 void Scene_BrushScaleTexdef_Selected( scene::Graph& graph, float s, float t ){
483         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceScaleTexdef( s, t ) );
484         SceneChangeNotify();
485 }
486
487 void Scene_BrushScaleTexdef_Component_Selected( scene::Graph& graph, float s, float t ){
488         Scene_ForEachSelectedBrushFace( graph, FaceScaleTexdef( s, t ) );
489         SceneChangeNotify();
490 }
491
492 class FaceRotateTexdef
493 {
494 float m_angle;
495 public:
496 FaceRotateTexdef( float angle ) : m_angle( angle ){
497 }
498 void operator()( Face& face ) const {
499         face.RotateTexdef( m_angle );
500 }
501 };
502
503 void Scene_BrushRotateTexdef_Selected( scene::Graph& graph, float angle ){
504         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceRotateTexdef( angle ) );
505         SceneChangeNotify();
506 }
507
508 void Scene_BrushRotateTexdef_Component_Selected( scene::Graph& graph, float angle ){
509         Scene_ForEachSelectedBrushFace( graph, FaceRotateTexdef( angle ) );
510         SceneChangeNotify();
511 }
512
513
514 class FaceSetShader
515 {
516 const char* m_name;
517 public:
518 FaceSetShader( const char* name ) : m_name( name ) {}
519 void operator()( Face& face ) const {
520         face.SetShader( m_name );
521 }
522 };
523
524 void Scene_BrushSetShader_Selected( scene::Graph& graph, const char* name ){
525         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceSetShader( name ) );
526         SceneChangeNotify();
527 }
528
529 void Scene_BrushSetShader_Component_Selected( scene::Graph& graph, const char* name ){
530         Scene_ForEachSelectedBrushFace( graph, FaceSetShader( name ) );
531         SceneChangeNotify();
532 }
533
534 class FaceSetDetail
535 {
536 bool m_detail;
537 public:
538 FaceSetDetail( bool detail ) : m_detail( detail ){
539 }
540 void operator()( Face& face ) const {
541         face.setDetail( m_detail );
542 }
543 };
544
545 void Scene_BrushSetDetail_Selected( scene::Graph& graph, bool detail ){
546         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceSetDetail( detail ) );
547         SceneChangeNotify();
548 }
549
550 bool Face_FindReplaceShader( Face& face, const char* find, const char* replace ){
551         if ( shader_equal( face.GetShader(), find ) ) {
552                 face.SetShader( replace );
553                 return true;
554         }
555         return false;
556 }
557
558 class FaceFindReplaceShader
559 {
560 const char* m_find;
561 const char* m_replace;
562 public:
563 FaceFindReplaceShader( const char* find, const char* replace ) : m_find( find ), m_replace( replace ){
564 }
565 void operator()( Face& face ) const {
566         Face_FindReplaceShader( face, m_find, m_replace );
567 }
568 };
569
570 class FaceFindShader
571 {
572 const char* m_find;
573 const char* m_replace;
574 public:
575 FaceFindShader( const char* find ) : m_find( find ){
576 }
577 void operator()( FaceInstance& faceinst ) const {
578         if ( shader_equal( faceinst.getFace().GetShader(), m_find ) ) {
579                 faceinst.setSelected( SelectionSystem::eFace, true );
580         }
581 }
582 };
583
584 bool DoingSearch( const char *repl ){
585         return ( repl == NULL || ( strcmp( "textures/", repl ) == 0 ) );
586 }
587
588 void Scene_BrushFindReplaceShader( scene::Graph& graph, const char* find, const char* replace ){
589         if ( DoingSearch( replace ) ) {
590                 Scene_ForEachBrush_ForEachFaceInstance( graph, FaceFindShader( find ) );
591         }
592         else
593         {
594                 Scene_ForEachBrush_ForEachFace( graph, FaceFindReplaceShader( find, replace ) );
595         }
596 }
597
598 void Scene_BrushFindReplaceShader_Selected( scene::Graph& graph, const char* find, const char* replace ){
599         if ( DoingSearch( replace ) ) {
600                 Scene_ForEachSelectedBrush_ForEachFaceInstance( graph,
601                                                                                                                 FaceFindShader( find ) );
602         }
603         else
604         {
605                 Scene_ForEachSelectedBrush_ForEachFace( graph,
606                                                                                                 FaceFindReplaceShader( find, replace ) );
607         }
608 }
609
610 // TODO: find for components
611 // d1223m: dont even know what they are...
612 void Scene_BrushFindReplaceShader_Component_Selected( scene::Graph& graph, const char* find, const char* replace ){
613         if ( DoingSearch( replace ) ) {
614
615         }
616         else
617         {
618                 Scene_ForEachSelectedBrushFace( graph, FaceFindReplaceShader( find, replace ) );
619         }
620 }
621
622
623 class FaceFitTexture
624 {
625 float m_s_repeat, m_t_repeat;
626 public:
627 FaceFitTexture( float s_repeat, float t_repeat ) : m_s_repeat( s_repeat ), m_t_repeat( t_repeat ){
628 }
629 void operator()( Face& face ) const {
630         face.FitTexture( m_s_repeat, m_t_repeat );
631 }
632 };
633
634 void Scene_BrushFitTexture_Selected( scene::Graph& graph, float s_repeat, float t_repeat ){
635         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceFitTexture( s_repeat, t_repeat ) );
636         SceneChangeNotify();
637 }
638
639 void Scene_BrushFitTexture_Component_Selected( scene::Graph& graph, float s_repeat, float t_repeat ){
640         Scene_ForEachSelectedBrushFace( graph, FaceFitTexture( s_repeat, t_repeat ) );
641         SceneChangeNotify();
642 }
643
644 TextureProjection g_defaultTextureProjection;
645 const TextureProjection& TextureTransform_getDefault(){
646         TexDef_Construct_Default( g_defaultTextureProjection );
647         return g_defaultTextureProjection;
648 }
649
650 void Scene_BrushConstructPrefab( scene::Graph& graph, EBrushPrefab type, std::size_t sides, const char* shader ){
651         if ( GlobalSelectionSystem().countSelected() != 0 ) {
652                 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
653
654                 Brush* brush = Node_getBrush( path.top() );
655                 if ( brush != 0 ) {
656                         AABB bounds = brush->localAABB(); // copy bounds because the brush will be modified
657                         Brush_ConstructPrefab( *brush, type, bounds, sides, shader, TextureTransform_getDefault() );
658                         SceneChangeNotify();
659                 }
660         }
661 }
662
663 void Scene_BrushResize_Selected( scene::Graph& graph, const AABB& bounds, const char* shader ){
664         if ( GlobalSelectionSystem().countSelected() != 0 ) {
665                 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
666
667                 Brush* brush = Node_getBrush( path.top() );
668                 if ( brush != 0 ) {
669                         Brush_ConstructCuboid( *brush, bounds, shader, TextureTransform_getDefault() );
670                         SceneChangeNotify();
671                 }
672         }
673 }
674
675 bool Brush_hasShader( const Brush& brush, const char* name ){
676         for ( Brush::const_iterator i = brush.begin(); i != brush.end(); ++i )
677         {
678                 if ( shader_equal( ( *i )->GetShader(), name ) ) {
679                         return true;
680                 }
681         }
682         return false;
683 }
684
685 class BrushSelectByShaderWalker : public scene::Graph::Walker
686 {
687 const char* m_name;
688 public:
689 BrushSelectByShaderWalker( const char* name )
690         : m_name( name ){
691 }
692 bool pre( const scene::Path& path, scene::Instance& instance ) const {
693         if ( path.top().get().visible() ) {
694                 Brush* brush = Node_getBrush( path.top() );
695                 if ( brush != 0 && Brush_hasShader( *brush, m_name ) ) {
696                         Instance_getSelectable( instance )->setSelected( true );
697                 }
698         }
699         else{
700                 return false;
701         }
702         return true;
703 }
704 };
705
706 void Scene_BrushSelectByShader( scene::Graph& graph, const char* name ){
707         graph.traverse( BrushSelectByShaderWalker( name ) );
708 }
709
710 class FaceSelectByShader
711 {
712 const char* m_name;
713 public:
714 FaceSelectByShader( const char* name )
715         : m_name( name ){
716 }
717 void operator()( FaceInstance& face ) const {
718         printf( "checking %s = %s\n", face.getFace().GetShader(), m_name );
719         if ( shader_equal( face.getFace().GetShader(), m_name ) ) {
720                 face.setSelected( SelectionSystem::eFace, true );
721         }
722 }
723 };
724
725 void Scene_BrushSelectByShader_Component( scene::Graph& graph, const char* name ){
726         Scene_ForEachSelectedBrush_ForEachFaceInstance( graph, FaceSelectByShader( name ) );
727 }
728
729 class FaceGetTexdef
730 {
731 TextureProjection& m_projection;
732 mutable bool m_done;
733 public:
734 FaceGetTexdef( TextureProjection& projection )
735         : m_projection( projection ), m_done( false ){
736 }
737 void operator()( Face& face ) const {
738         if ( !m_done ) {
739                 m_done = true;
740                 face.GetTexdef( m_projection );
741         }
742 }
743 };
744
745
746 void Scene_BrushGetTexdef_Selected( scene::Graph& graph, TextureProjection& projection ){
747         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceGetTexdef( projection ) );
748 }
749
750 void Scene_BrushGetTexdef_Component_Selected( scene::Graph& graph, TextureProjection& projection ){
751 #if 1
752         if ( !g_SelectedFaceInstances.empty() ) {
753                 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
754                 faceInstance.getFace().GetTexdef( projection );
755         }
756 #else
757         FaceGetTexdef visitor( projection );
758         Scene_ForEachSelectedBrushFace( graph, visitor );
759 #endif
760 }
761
762 void Scene_BrushGetShaderSize_Component_Selected( scene::Graph& graph, size_t& width, size_t& height ){
763         if ( !g_SelectedFaceInstances.empty() ) {
764                 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
765                 width = faceInstance.getFace().getShader().width();
766                 height = faceInstance.getFace().getShader().height();
767         }
768 }
769
770
771 class FaceGetFlags
772 {
773 ContentsFlagsValue& m_flags;
774 mutable bool m_done;
775 public:
776 FaceGetFlags( ContentsFlagsValue& flags )
777         : m_flags( flags ), m_done( false ){
778 }
779 void operator()( Face& face ) const {
780         if ( !m_done ) {
781                 m_done = true;
782                 face.GetFlags( m_flags );
783         }
784 }
785 };
786
787
788 void Scene_BrushGetFlags_Selected( scene::Graph& graph, ContentsFlagsValue& flags ){
789 #if 1
790         if ( GlobalSelectionSystem().countSelected() != 0 ) {
791                 BrushInstance* brush = Instance_getBrush( GlobalSelectionSystem().ultimateSelected() );
792                 if ( brush != 0 ) {
793                         Brush_forEachFace( *brush, FaceGetFlags( flags ) );
794                 }
795         }
796 #else
797         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceGetFlags( flags ) );
798 #endif
799 }
800
801 void Scene_BrushGetFlags_Component_Selected( scene::Graph& graph, ContentsFlagsValue& flags ){
802 #if 1
803         if ( !g_SelectedFaceInstances.empty() ) {
804                 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
805                 faceInstance.getFace().GetFlags( flags );
806         }
807 #else
808         Scene_ForEachSelectedBrushFace( graph, FaceGetFlags( flags ) );
809 #endif
810 }
811
812
813 class FaceGetShader
814 {
815 CopiedString& m_shader;
816 mutable bool m_done;
817 public:
818 FaceGetShader( CopiedString& shader )
819         : m_shader( shader ), m_done( false ){
820 }
821 void operator()( Face& face ) const {
822         if ( !m_done ) {
823                 m_done = true;
824                 m_shader = face.GetShader();
825         }
826 }
827 };
828
829 void Scene_BrushGetShader_Selected( scene::Graph& graph, CopiedString& shader ){
830 #if 1
831         if ( GlobalSelectionSystem().countSelected() != 0 ) {
832                 BrushInstance* brush = Instance_getBrush( GlobalSelectionSystem().ultimateSelected() );
833                 if ( brush != 0 ) {
834                         Brush_forEachFace( *brush, FaceGetShader( shader ) );
835                 }
836         }
837 #else
838         Scene_ForEachSelectedBrush_ForEachFace( graph, FaceGetShader( shader ) );
839 #endif
840 }
841
842 void Scene_BrushGetShader_Component_Selected( scene::Graph& graph, CopiedString& shader ){
843 #if 1
844         if ( !g_SelectedFaceInstances.empty() ) {
845                 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
846                 shader = faceInstance.getFace().GetShader();
847         }
848 #else
849         FaceGetShader visitor( shader );
850         Scene_ForEachSelectedBrushFace( graph, visitor );
851 #endif
852 }
853
854
855 class filter_face_shader : public FaceFilter
856 {
857 const char* m_shader;
858 public:
859 filter_face_shader( const char* shader ) : m_shader( shader ){
860 }
861 bool filter( const Face& face ) const {
862         return shader_equal( face.GetShader(), m_shader );
863 }
864 };
865
866 class filter_face_shader_prefix : public FaceFilter
867 {
868 const char* m_prefix;
869 public:
870 filter_face_shader_prefix( const char* prefix ) : m_prefix( prefix ){
871 }
872 bool filter( const Face& face ) const {
873         return shader_equal_n( face.GetShader(), m_prefix, strlen( m_prefix ) );
874 }
875 };
876
877 class filter_face_flags : public FaceFilter
878 {
879 int m_flags;
880 public:
881 filter_face_flags( int flags ) : m_flags( flags ){
882 }
883 bool filter( const Face& face ) const {
884         return ( face.getShader().shaderFlags() & m_flags ) != 0;
885 }
886 };
887
888 class filter_face_contents : public FaceFilter
889 {
890 int m_contents;
891 public:
892 filter_face_contents( int contents ) : m_contents( contents ){
893 }
894 bool filter( const Face& face ) const {
895         return ( face.getShader().m_flags.m_contentFlags & m_contents ) != 0;
896 }
897 };
898
899
900
901 class FaceFilterAny
902 {
903 FaceFilter* m_filter;
904 bool& m_filtered;
905 public:
906 FaceFilterAny( FaceFilter* filter, bool& filtered ) : m_filter( filter ), m_filtered( filtered ){
907         m_filtered = false;
908 }
909 void operator()( Face& face ) const {
910         if ( m_filter->filter( face ) ) {
911                 m_filtered = true;
912         }
913 }
914 };
915
916 class filter_brush_any_face : public BrushFilter
917 {
918 FaceFilter* m_filter;
919 public:
920 filter_brush_any_face( FaceFilter* filter ) : m_filter( filter ){
921 }
922 bool filter( const Brush& brush ) const {
923         bool filtered;
924         Brush_forEachFace( brush, FaceFilterAny( m_filter, filtered ) );
925         return filtered;
926 }
927 };
928
929 class FaceFilterAll
930 {
931 FaceFilter* m_filter;
932 bool& m_filtered;
933 public:
934 FaceFilterAll( FaceFilter* filter, bool& filtered ) : m_filter( filter ), m_filtered( filtered ){
935         m_filtered = true;
936 }
937 void operator()( Face& face ) const {
938         if ( !m_filter->filter( face ) ) {
939                 m_filtered = false;
940         }
941 }
942 };
943
944 class filter_brush_all_faces : public BrushFilter
945 {
946 FaceFilter* m_filter;
947 public:
948 filter_brush_all_faces( FaceFilter* filter ) : m_filter( filter ){
949 }
950 bool filter( const Brush& brush ) const {
951         bool filtered;
952         Brush_forEachFace( brush, FaceFilterAll( m_filter, filtered ) );
953         return filtered;
954 }
955 };
956
957
958 filter_face_flags g_filter_face_clip( QER_CLIP );
959 filter_brush_all_faces g_filter_brush_clip( &g_filter_face_clip );
960
961 filter_face_shader g_filter_face_clip_q2( "textures/clip" );
962 filter_brush_all_faces g_filter_brush_clip_q2( &g_filter_face_clip_q2 );
963
964 filter_face_shader g_filter_face_weapclip( "textures/common/weapclip" );
965 filter_brush_all_faces g_filter_brush_weapclip( &g_filter_face_weapclip );
966
967 filter_face_shader g_filter_face_commonclip( "textures/common/clip" );
968 filter_brush_all_faces g_filter_brush_commonclip( &g_filter_face_commonclip );
969
970 filter_face_shader g_filter_face_fullclip( "textures/common/fullclip" );
971 filter_brush_all_faces g_filter_brush_fullclip( &g_filter_face_fullclip );
972
973 filter_face_shader g_filter_face_botclip( "textures/common/botclip" );
974 filter_brush_all_faces g_filter_brush_botclip( &g_filter_face_botclip );
975
976 filter_face_shader_prefix g_filter_face_caulk( "textures/common/caulk" );
977 filter_brush_all_faces g_filter_brush_caulk( &g_filter_face_caulk );
978
979 filter_face_shader_prefix g_filter_face_caulk_ja( "textures/system/caulk" );
980 filter_brush_all_faces g_filter_brush_caulk_ja( &g_filter_face_caulk_ja );
981
982 filter_face_flags g_filter_face_liquids( QER_LIQUID );
983 filter_brush_any_face g_filter_brush_liquids( &g_filter_face_liquids );
984
985 filter_face_shader_prefix g_filter_face_liquidsdir( "textures/liquids/" );
986 filter_brush_any_face g_filter_brush_liquidsdir( &g_filter_face_liquidsdir );
987
988 filter_face_shader g_filter_face_hint( "textures/common/hint" );
989 filter_brush_any_face g_filter_brush_hint( &g_filter_face_hint );
990
991 filter_face_shader g_filter_face_hintlocal( "textures/common/hintlocal" );
992 filter_brush_any_face g_filter_brush_hintlocal( &g_filter_face_hintlocal );
993
994 filter_face_shader g_filter_face_hint_q2( "textures/hint" );
995 filter_brush_any_face g_filter_brush_hint_q2( &g_filter_face_hint_q2 );
996
997 filter_face_shader g_filter_face_hint_ja( "textures/system/hint" );
998 filter_brush_any_face g_filter_brush_hint_ja( &g_filter_face_hint_ja );
999
1000 filter_face_shader g_filter_face_areaportal( "textures/common/areaportal" );
1001 filter_brush_any_face g_filter_brush_areaportal( &g_filter_face_areaportal );
1002
1003 filter_face_shader g_filter_face_visportal( "textures/editor/visportal" );
1004 filter_brush_any_face g_filter_brush_visportal( &g_filter_face_visportal );
1005
1006 filter_face_shader g_filter_face_clusterportal( "textures/common/clusterportal" );
1007 filter_brush_all_faces g_filter_brush_clusterportal( &g_filter_face_clusterportal );
1008
1009 filter_face_shader g_filter_face_lightgrid( "textures/common/lightgrid" );
1010 filter_brush_all_faces g_filter_brush_lightgrid( &g_filter_face_lightgrid );
1011
1012 filter_face_flags g_filter_face_translucent( QER_TRANS | QER_ALPHATEST );
1013 filter_brush_any_face g_filter_brush_translucent( &g_filter_face_translucent );
1014
1015 filter_face_contents g_filter_face_detail( BRUSH_DETAIL_MASK );
1016 filter_brush_all_faces g_filter_brush_detail( &g_filter_face_detail );
1017
1018 filter_face_shader_prefix g_filter_face_decals( "textures/decals/" );
1019 filter_brush_any_face g_filter_brush_decals( &g_filter_face_decals );
1020
1021
1022 void BrushFilters_construct(){
1023         add_brush_filter( g_filter_brush_clip, EXCLUDE_CLIP );
1024         add_brush_filter( g_filter_brush_clip_q2, EXCLUDE_CLIP );
1025         add_brush_filter( g_filter_brush_weapclip, EXCLUDE_CLIP );
1026         add_brush_filter( g_filter_brush_fullclip, EXCLUDE_CLIP );
1027         add_brush_filter( g_filter_brush_commonclip, EXCLUDE_CLIP );
1028         add_brush_filter( g_filter_brush_botclip, EXCLUDE_BOTCLIP );
1029         add_brush_filter( g_filter_brush_caulk, EXCLUDE_CAULK );
1030         add_brush_filter( g_filter_brush_caulk_ja, EXCLUDE_CAULK );
1031         add_face_filter( g_filter_face_caulk, EXCLUDE_CAULK );
1032         add_face_filter( g_filter_face_caulk_ja, EXCLUDE_CAULK );
1033         add_brush_filter( g_filter_brush_liquids, EXCLUDE_LIQUIDS );
1034         add_brush_filter( g_filter_brush_liquidsdir, EXCLUDE_LIQUIDS );
1035         add_brush_filter( g_filter_brush_hint, EXCLUDE_HINTSSKIPS );
1036         add_brush_filter( g_filter_brush_hintlocal, EXCLUDE_HINTSSKIPS );
1037         add_brush_filter( g_filter_brush_hint_q2, EXCLUDE_HINTSSKIPS );
1038         add_brush_filter( g_filter_brush_hint_ja, EXCLUDE_HINTSSKIPS );
1039         add_brush_filter( g_filter_brush_clusterportal, EXCLUDE_CLUSTERPORTALS );
1040         add_brush_filter( g_filter_brush_visportal, EXCLUDE_VISPORTALS );
1041         add_brush_filter( g_filter_brush_areaportal, EXCLUDE_AREAPORTALS );
1042         add_brush_filter( g_filter_brush_translucent, EXCLUDE_TRANSLUCENT );
1043         add_brush_filter( g_filter_brush_detail, EXCLUDE_DETAILS );
1044         add_brush_filter( g_filter_brush_detail, EXCLUDE_STRUCTURAL, true );
1045         add_brush_filter( g_filter_brush_lightgrid, EXCLUDE_LIGHTGRID );
1046         add_brush_filter( g_filter_brush_decals, EXCLUDE_DECALS );
1047 }
1048
1049 #if 0
1050
1051 void normalquantisation_draw(){
1052         glPointSize( 1 );
1053         glBegin( GL_POINTS );
1054         for ( std::size_t i = 0; i <= c_quantise_normal; ++i )
1055         {
1056                 for ( std::size_t j = 0; j <= c_quantise_normal; ++j )
1057                 {
1058                         Normal3f vertex( normal3f_normalised( Normal3f(
1059                                                                                                           static_cast<float>( c_quantise_normal - j - i ),
1060                                                                                                           static_cast<float>( i ),
1061                                                                                                           static_cast<float>( j )
1062                                                                                                           ) ) );
1063                         VectorScale( normal3f_to_array( vertex ), 64.f, normal3f_to_array( vertex ) );
1064                         glVertex3fv( normal3f_to_array( vertex ) );
1065                         vertex.x = -vertex.x;
1066                         glVertex3fv( normal3f_to_array( vertex ) );
1067                 }
1068         }
1069         glEnd();
1070 }
1071
1072 class RenderableNormalQuantisation : public OpenGLRenderable
1073 {
1074 public:
1075 void render( RenderStateFlags state ) const {
1076         normalquantisation_draw();
1077 }
1078 };
1079
1080 const float g_test_quantise_normal = 1.f / static_cast<float>( 1 << 3 );
1081
1082 class TestNormalQuantisation
1083 {
1084 void check_normal( const Normal3f& normal, const Normal3f& other ){
1085         spherical_t spherical = spherical_from_normal3f( normal );
1086         double longditude = RAD2DEG( spherical.longditude );
1087         double latitude = RAD2DEG( spherical.latitude );
1088         double x = cos( spherical.longditude ) * sin( spherical.latitude );
1089         double y = sin( spherical.longditude ) * sin( spherical.latitude );
1090         double z = cos( spherical.latitude );
1091
1092         ASSERT_MESSAGE( normal3f_dot( normal, other ) > 0.99, "bleh" );
1093 }
1094
1095 void test_normal( const Normal3f& normal ){
1096         Normal3f test = normal3f_from_spherical( spherical_from_normal3f( normal ) );
1097         check_normal( normal, test );
1098
1099         EOctant octant = normal3f_classify_octant( normal );
1100         Normal3f folded = normal3f_fold_octant( normal, octant );
1101         ESextant sextant = normal3f_classify_sextant( folded );
1102         folded = normal3f_fold_sextant( folded, sextant );
1103
1104         double scale = static_cast<float>( c_quantise_normal ) / ( folded.x + folded.y + folded.z );
1105
1106         double zbits = folded.z * scale;
1107         double ybits = folded.y * scale;
1108
1109         std::size_t zbits_q = static_cast<std::size_t>( zbits );
1110         std::size_t ybits_q = static_cast<std::size_t>( ybits );
1111
1112         ASSERT_MESSAGE( zbits_q <= ( c_quantise_normal / 8 ) * 3, "bleh" );
1113         ASSERT_MESSAGE( ybits_q <= ( c_quantise_normal / 2 ), "bleh" );
1114         ASSERT_MESSAGE( zbits_q + ( ( c_quantise_normal / 2 ) - ybits_q ) <= ( c_quantise_normal / 2 ), "bleh" );
1115
1116         std::size_t y_t = ( zbits_q < ( c_quantise_normal / 4 ) ) ? ybits_q : ( c_quantise_normal / 2 ) - ybits_q;
1117         std::size_t z_t = ( zbits_q < ( c_quantise_normal / 4 ) ) ? zbits_q : ( c_quantise_normal / 2 ) - zbits_q;
1118         std::size_t index = ( c_quantise_normal / 4 ) * y_t + z_t;
1119         ASSERT_MESSAGE( index <= ( c_quantise_normal / 4 ) * ( c_quantise_normal / 2 ), "bleh" );
1120
1121         Normal3f tmp( c_quantise_normal - zbits_q - ybits_q, ybits_q, zbits_q );
1122         tmp = normal3f_normalised( tmp );
1123
1124         Normal3f unfolded = normal3f_unfold_octant( normal3f_unfold_sextant( tmp, sextant ), octant );
1125
1126         check_normal( normal, unfolded );
1127
1128         double dot = normal3f_dot( normal, unfolded );
1129         float length = VectorLength( normal3f_to_array( unfolded ) );
1130         float inv_length = 1 / length;
1131
1132         Normal3f quantised = normal3f_quantised( normal );
1133         check_normal( normal, quantised );
1134 }
1135 void test2( const Normal3f& normal, const Normal3f& other ){
1136         if ( normal3f_quantised( normal ) != normal3f_quantised( other ) ) {
1137                 int bleh = 0;
1138         }
1139 }
1140
1141 static Normal3f normalise( float x, float y, float z ){
1142         return normal3f_normalised( Normal3f( x, y, z ) );
1143 }
1144
1145 float vec_rand(){
1146         return static_cast<float>( rand() - ( RAND_MAX / 2 ) );
1147 }
1148
1149 Normal3f normal3f_rand(){
1150         return normalise( vec_rand(), vec_rand(), vec_rand() );
1151 }
1152
1153 public:
1154 TestNormalQuantisation(){
1155         for ( int i = 4096; i > 0; --i )
1156                 test_normal( normal3f_rand() );
1157
1158         test_normal( normalise( 1, 0, 0 ) );
1159         test_normal( normalise( 0, 1, 0 ) );
1160         test_normal( normalise( 0, 0, 1 ) );
1161         test_normal( normalise( 1, 1, 0 ) );
1162         test_normal( normalise( 1, 0, 1 ) );
1163         test_normal( normalise( 0, 1, 1 ) );
1164
1165         test_normal( normalise( 10000, 10000, 10000 ) );
1166         test_normal( normalise( 10000, 10000, 10001 ) );
1167         test_normal( normalise( 10000, 10000, 10002 ) );
1168         test_normal( normalise( 10000, 10000, 10010 ) );
1169         test_normal( normalise( 10000, 10000, 10020 ) );
1170         test_normal( normalise( 10000, 10000, 10030 ) );
1171         test_normal( normalise( 10000, 10000, 10100 ) );
1172         test_normal( normalise( 10000, 10000, 10101 ) );
1173         test_normal( normalise( 10000, 10000, 10102 ) );
1174         test_normal( normalise( 10000, 10000, 10200 ) );
1175         test_normal( normalise( 10000, 10000, 10201 ) );
1176         test_normal( normalise( 10000, 10000, 10202 ) );
1177         test_normal( normalise( 10000, 10000, 10203 ) );
1178         test_normal( normalise( 10000, 10000, 10300 ) );
1179
1180
1181         test2( normalise( 10000, 10000, 10000 ), normalise( 10000, 10000, 10001 ) );
1182         test2( normalise( 10000, 10000, 10001 ), normalise( 10000, 10001, 10000 ) );
1183 }
1184 };
1185
1186 TestNormalQuantisation g_testNormalQuantisation;
1187
1188
1189 #endif
1190
1191 #if 0
1192 class TestSelectableObserver : public observer_template<const Selectable&>
1193 {
1194 public:
1195 void notify( const Selectable& arguments ){
1196         bool bleh = arguments.isSelected();
1197 }
1198 };
1199
1200 inline void test_bleh(){
1201         TestSelectableObserver test;
1202         ObservableSelectableInstance< SingleObservable< SelectionChangeCallback > > bleh;
1203         bleh.attach( test );
1204         bleh.setSelected( true );
1205         bleh.detach( test );
1206 }
1207
1208 class TestBleh
1209 {
1210 public:
1211 TestBleh(){
1212         test_bleh();
1213 }
1214 };
1215
1216 const TestBleh testbleh;
1217 #endif
1218
1219
1220 #if 0
1221 class TestRefcountedString
1222 {
1223 public:
1224 TestRefcountedString(){
1225         {
1226                 // copy construct
1227                 SmartString string1( "string1" );
1228                 SmartString string2( string1 );
1229                 SmartString string3( string2 );
1230         }
1231         {
1232                 // refcounted assignment
1233                 SmartString string1( "string1" );
1234                 SmartString string2( "string2" );
1235                 string1 = string2;
1236         }
1237         {
1238                 // copy assignment
1239                 SmartString string1( "string1" );
1240                 SmartString string2( "string2" );
1241                 string1 = string2.c_str();
1242         }
1243         {
1244                 // self-assignment
1245                 SmartString string1( "string1" );
1246                 string1 = string1;
1247         }
1248         {
1249                 // self-assignment via another reference
1250                 SmartString string1( "string1" );
1251                 SmartString string2( string1 );
1252                 string1 = string2;
1253         }
1254 }
1255 };
1256
1257 const TestRefcountedString g_testRefcountedString;
1258
1259 #endif
1260
1261 void Select_MakeDetail(){
1262         UndoableCommand undo( "brushSetDetail" );
1263         Scene_BrushSetDetail_Selected( GlobalSceneGraph(), true );
1264 }
1265
1266 void Select_MakeStructural(){
1267         UndoableCommand undo( "brushClearDetail" );
1268         Scene_BrushSetDetail_Selected( GlobalSceneGraph(), false );
1269 }
1270
1271 class BrushMakeSided
1272 {
1273 std::size_t m_count;
1274 public:
1275 BrushMakeSided( std::size_t count )
1276         : m_count( count ){
1277 }
1278 void set(){
1279         Scene_BrushConstructPrefab( GlobalSceneGraph(), eBrushPrism, m_count, TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
1280 }
1281 typedef MemberCaller<BrushMakeSided, &BrushMakeSided::set> SetCaller;
1282 };
1283
1284
1285 BrushMakeSided g_brushmakesided3( 3 );
1286 BrushMakeSided g_brushmakesided4( 4 );
1287 BrushMakeSided g_brushmakesided5( 5 );
1288 BrushMakeSided g_brushmakesided6( 6 );
1289 BrushMakeSided g_brushmakesided7( 7 );
1290 BrushMakeSided g_brushmakesided8( 8 );
1291 BrushMakeSided g_brushmakesided9( 9 );
1292
1293 inline int axis_for_viewtype( int viewtype ){
1294         switch ( viewtype )
1295         {
1296         case XY:
1297                 return 2;
1298         case XZ:
1299                 return 1;
1300         case YZ:
1301                 return 0;
1302         }
1303         return 2;
1304 }
1305
1306 class BrushPrefab
1307 {
1308 EBrushPrefab m_type;
1309 public:
1310 BrushPrefab( EBrushPrefab type )
1311         : m_type( type ){
1312 }
1313 void set(){
1314         DoSides( m_type, axis_for_viewtype( GetViewAxis() ) );
1315 }
1316 typedef MemberCaller<BrushPrefab, &BrushPrefab::set> SetCaller;
1317 };
1318
1319 BrushPrefab g_brushprism( eBrushPrism );
1320 BrushPrefab g_brushcone( eBrushCone );
1321 BrushPrefab g_brushsphere( eBrushSphere );
1322 BrushPrefab g_brushrock( eBrushRock );
1323
1324
1325 void FlipClip();
1326 void SplitClip();
1327 void Clip();
1328 void OnClipMode( bool enable );
1329 bool ClipMode();
1330
1331
1332 void ClipSelected(){
1333         if ( ClipMode() ) {
1334                 UndoableCommand undo( "clipperClip" );
1335                 Clip();
1336         }
1337 }
1338
1339 void SplitSelected(){
1340         if ( ClipMode() ) {
1341                 UndoableCommand undo( "clipperSplit" );
1342                 SplitClip();
1343         }
1344 }
1345
1346 void FlipClipper(){
1347         FlipClip();
1348 }
1349
1350
1351 Callback g_texture_lock_status_changed;
1352 BoolExportCaller g_texdef_movelock_caller( g_brush_texturelock_enabled );
1353 ToggleItem g_texdef_movelock_item( g_texdef_movelock_caller );
1354
1355 void Texdef_ToggleMoveLock(){
1356         g_brush_texturelock_enabled = !g_brush_texturelock_enabled;
1357         g_texdef_movelock_item.update();
1358         g_texture_lock_status_changed();
1359 }
1360
1361
1362
1363
1364
1365 void Brush_registerCommands(){
1366         GlobalToggles_insert( "TogTexLock", FreeCaller<Texdef_ToggleMoveLock>(), ToggleItem::AddCallbackCaller( g_texdef_movelock_item ), Accelerator( 'T', (GdkModifierType)GDK_SHIFT_MASK ) );
1367
1368         GlobalCommands_insert( "BrushPrism", BrushPrefab::SetCaller( g_brushprism ) );
1369         GlobalCommands_insert( "BrushCone", BrushPrefab::SetCaller( g_brushcone ) );
1370         GlobalCommands_insert( "BrushSphere", BrushPrefab::SetCaller( g_brushsphere ) );
1371         GlobalCommands_insert( "BrushRock", BrushPrefab::SetCaller( g_brushrock ) );
1372
1373         GlobalCommands_insert( "Brush3Sided", BrushMakeSided::SetCaller( g_brushmakesided3 ), Accelerator( '3', (GdkModifierType)GDK_CONTROL_MASK ) );
1374         GlobalCommands_insert( "Brush4Sided", BrushMakeSided::SetCaller( g_brushmakesided4 ), Accelerator( '4', (GdkModifierType)GDK_CONTROL_MASK ) );
1375         GlobalCommands_insert( "Brush5Sided", BrushMakeSided::SetCaller( g_brushmakesided5 ), Accelerator( '5', (GdkModifierType)GDK_CONTROL_MASK ) );
1376         GlobalCommands_insert( "Brush6Sided", BrushMakeSided::SetCaller( g_brushmakesided6 ), Accelerator( '6', (GdkModifierType)GDK_CONTROL_MASK ) );
1377         GlobalCommands_insert( "Brush7Sided", BrushMakeSided::SetCaller( g_brushmakesided7 ), Accelerator( '7', (GdkModifierType)GDK_CONTROL_MASK ) );
1378         GlobalCommands_insert( "Brush8Sided", BrushMakeSided::SetCaller( g_brushmakesided8 ), Accelerator( '8', (GdkModifierType)GDK_CONTROL_MASK ) );
1379         GlobalCommands_insert( "Brush9Sided", BrushMakeSided::SetCaller( g_brushmakesided9 ), Accelerator( '9', (GdkModifierType)GDK_CONTROL_MASK ) );
1380
1381         GlobalCommands_insert( "ClipSelected", FreeCaller<ClipSelected>(), Accelerator( GDK_Return ) );
1382         GlobalCommands_insert( "SplitSelected", FreeCaller<SplitSelected>(), Accelerator( GDK_Return, (GdkModifierType)GDK_SHIFT_MASK ) );
1383         GlobalCommands_insert( "FlipClip", FreeCaller<FlipClipper>(), Accelerator( GDK_Return, (GdkModifierType)GDK_CONTROL_MASK ) );
1384
1385         GlobalCommands_insert( "MakeDetail", FreeCaller<Select_MakeDetail>(), Accelerator( 'D', (GdkModifierType)GDK_MOD1_MASK ) );
1386         GlobalCommands_insert( "MakeStructural", FreeCaller<Select_MakeStructural>(), Accelerator( 'S', (GdkModifierType)GDK_MOD1_MASK ) );
1387 }
1388
1389 void Brush_constructMenu( GtkMenu* menu ){
1390         create_menu_item_with_mnemonic( menu, "Prism...", "BrushPrism" );
1391         create_menu_item_with_mnemonic( menu, "Cone...", "BrushCone" );
1392         create_menu_item_with_mnemonic( menu, "Sphere...", "BrushSphere" );
1393         create_menu_item_with_mnemonic( menu, "Rock...", "BrushRock" );
1394         menu_separator( menu );
1395         {
1396                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "CSG" );
1397                 if ( g_Layout_enableDetachableMenus.m_value ) {
1398                         menu_tearoff( menu_in_menu );
1399                 }
1400                 create_menu_item_with_mnemonic( menu_in_menu, "CSG _Subtract", "CSGSubtract" );
1401                 create_menu_item_with_mnemonic( menu_in_menu, "CSG _Merge", "CSGMerge" );
1402                 create_menu_item_with_mnemonic( menu_in_menu, "Make _Room", "CSGroom" );
1403                 create_menu_item_with_mnemonic( menu_in_menu, "CSG _Tool", "CSGTool" );
1404         }
1405         menu_separator( menu );
1406         {
1407                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Clipper" );
1408                 if ( g_Layout_enableDetachableMenus.m_value ) {
1409                         menu_tearoff( menu_in_menu );
1410                 }
1411
1412                 create_menu_item_with_mnemonic( menu_in_menu, "Clip selection", "ClipSelected" );
1413                 create_menu_item_with_mnemonic( menu_in_menu, "Split selection", "SplitSelected" );
1414                 create_menu_item_with_mnemonic( menu_in_menu, "Flip Clip orientation", "FlipClip" );
1415         }
1416         menu_separator( menu );
1417         create_menu_item_with_mnemonic( menu, "Make detail", "MakeDetail" );
1418         create_menu_item_with_mnemonic( menu, "Make structural", "MakeStructural" );
1419 //      create_menu_item_with_mnemonic( menu, "Snap selection to _grid", "SnapToGrid" );
1420
1421         create_check_menu_item_with_mnemonic( menu, "Texture Lock", "TogTexLock" );
1422         menu_separator( menu );
1423         create_menu_item_with_mnemonic( menu, "Copy Face Texture", "FaceCopyTexture" );
1424         create_menu_item_with_mnemonic( menu, "Paste Face Texture", "FacePasteTexture" );
1425
1426         command_connect_accelerator( "Brush3Sided" );
1427         command_connect_accelerator( "Brush4Sided" );
1428         command_connect_accelerator( "Brush5Sided" );
1429         command_connect_accelerator( "Brush6Sided" );
1430         command_connect_accelerator( "Brush7Sided" );
1431         command_connect_accelerator( "Brush8Sided" );
1432         command_connect_accelerator( "Brush9Sided" );
1433 }