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