2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "brushmanip.h"
25 #include "gtkutil/widget.h"
26 #include "gtkutil/menu.h"
28 #include "brushnode.h"
30 #include "texwindow.h"
33 #include "mainframe.h"
36 #include "preferences.h"
39 #include <gdk/gdkkeysyms.h>
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 ) );
50 for ( int i = 0; i < 3; ++i )
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]];
57 brush.addPlane( maxs, planepts1, planepts2, shader, projection );
61 for ( int i = 0; i < 3; ++i )
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]];
68 brush.addPlane( mins, planepts1, planepts2, shader, projection );
73 inline float max_extent( const Vector3& extents ){
74 return std::max( std::max( extents[0], extents[1] ), extents[2] );
77 inline float max_extent_2d( const Vector3& extents, int axis ){
81 return std::max( extents[1], extents[2] );
83 return std::max( extents[0], extents[2] );
85 return std::max( extents[0], extents[1] );
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";
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";
98 if ( sides > c_brushPrism_maxSides ) {
99 globalErrorStream() << c_brushPrism_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushPrism_maxSides ) << "\n";
104 brush.reserve( sides + 2 );
106 Vector3 mins( vector3_subtracted( bounds.origin, bounds.extents ) );
107 Vector3 maxs( vector3_added( bounds.origin, bounds.extents ) );
109 float radius = max_extent_2d( bounds.extents, axis );
110 const Vector3& mid = bounds.origin;
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];
123 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
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];
135 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
137 for ( std::size_t i = 0 ; i < sides ; ++i )
139 double sv = sin( i * 3.14159265 * 2 / sides );
140 double cv = cos( i * 3.14159265 * 2 / sides );
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];
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];
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];
154 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
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";
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";
167 if ( sides > c_brushCone_maxSides ) {
168 globalErrorStream() << c_brushCone_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushCone_maxSides ) << "\n";
173 brush.reserve( sides + 1 );
175 Vector3 mins( vector3_subtracted( bounds.origin, bounds.extents ) );
176 Vector3 maxs( vector3_added( bounds.origin, bounds.extents ) );
178 float radius = max_extent( bounds.extents );
179 const Vector3& mid = bounds.origin;
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];
186 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
188 for ( std::size_t i = 0 ; i < sides ; ++i )
190 double sv = sin( i * 3.14159265 * 2 / sides );
191 double cv = cos( i * 3.14159265 * 2 / sides );
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];
197 planepts[1][0] = mid[0];
198 planepts[1][1] = mid[1];
199 planepts[1][2] = maxs[2];
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];
205 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
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";
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";
218 if ( sides > c_brushSphere_maxSides ) {
219 globalErrorStream() << c_brushSphere_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushSphere_maxSides ) << "\n";
224 brush.reserve( sides * sides );
226 float radius = max_extent( bounds.extents );
227 const Vector3& mid = bounds.origin;
230 double dt = 2 * c_pi / sides;
231 double dp = c_pi / sides;
232 for ( std::size_t i = 0; i < sides; i++ )
234 for ( std::size_t j = 0; j < sides - 1; j++ )
237 double p = float(j * dp - c_pi / 2);
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 ) );
243 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
248 double p = ( sides - 1 ) * dp - c_pi / 2;
249 for ( std::size_t i = 0; i < sides; i++ )
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 ) );
257 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
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";
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";
271 if ( sides > c_brushRock_maxSides ) {
272 globalErrorStream() << c_brushRock_name << ": sides " << Unsigned( sides ) << ": too many sides, maximum is " << Unsigned( c_brushRock_maxSides ) << "\n";
277 brush.reserve( sides * sides );
279 float radius = max_extent( bounds.extents );
280 const Vector3& mid = bounds.origin;
283 for ( std::size_t j = 0; j < sides; j++ )
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] );
290 // find two vectors that are perpendicular to planepts[0]
291 ComputeAxisBase( planepts[0], planepts[1], planepts[2] );
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 ) );
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 ) {
302 planepts[1] = planepts[2];
304 globalOutputStream() << "flip\n";
307 globalOutputStream() << "no flip\n";
311 brush.addPlane( planepts[0], planepts[1], planepts[2], shader, projection );
316 switch ( GlobalXYWnd_getCurrentViewType() )
328 void Brush_ConstructPrefab( Brush& brush, EBrushPrefab type, const AABB& bounds, std::size_t sides, const char* shader, const TextureProjection& projection ){
333 UndoableCommand undo( "brushCuboid" );
335 Brush_ConstructCuboid( brush, bounds, shader, projection );
340 int axis = GetViewAxis();
341 StringOutputStream command;
342 command << c_brushPrism_name << " -sides " << Unsigned( sides ) << " -axis " << axis;
343 UndoableCommand undo( command.c_str() );
345 Brush_ConstructPrism( brush, bounds, sides, axis, shader, projection );
350 StringOutputStream command;
351 command << c_brushCone_name << " -sides " << Unsigned( sides );
352 UndoableCommand undo( command.c_str() );
354 Brush_ConstructCone( brush, bounds, sides, shader, projection );
359 StringOutputStream command;
360 command << c_brushSphere_name << " -sides " << Unsigned( sides );
361 UndoableCommand undo( command.c_str() );
363 Brush_ConstructSphere( brush, bounds, sides, shader, projection );
368 StringOutputStream command;
369 command << c_brushRock_name << " -sides " << Unsigned( sides );
370 UndoableCommand undo( command.c_str() );
372 Brush_ConstructRock( brush, bounds, sides, shader, projection );
379 void ConstructRegionBrushes( scene::Node* brushes[6], const Vector3& region_mins, const Vector3& region_maxs ){
382 Vector3 mins( region_mins[0] - 32, region_mins[1] - 32, region_mins[2] - 32 );
385 for ( std::size_t i = 0; i < 3; i++ )
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() );
395 Vector3 maxs( region_maxs[0] + 32, region_maxs[1] + 32, region_maxs[2] + 32 );
398 for ( std::size_t i = 0; i < 3; i++ )
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() );
408 void Scene_BrushSetTexdef_Selected( scene::Graph& graph, const TextureProjection& projection ){
409 Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
410 face.SetTexdef(projection);
415 void Scene_BrushSetTexdef_Component_Selected( scene::Graph& graph, const TextureProjection& projection ){
416 Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
417 face.SetTexdef(projection);
423 void Scene_BrushSetFlags_Selected( scene::Graph& graph, const ContentsFlagsValue& flags ){
424 Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
425 face.SetFlags(flags);
430 void Scene_BrushSetFlags_Component_Selected( scene::Graph& graph, const ContentsFlagsValue& flags ){
431 Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
432 face.SetFlags(flags);
437 void Scene_BrushShiftTexdef_Selected( scene::Graph& graph, float s, float t ){
438 Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
439 face.ShiftTexdef(s, t);
444 void Scene_BrushShiftTexdef_Component_Selected( scene::Graph& graph, float s, float t ){
445 Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
446 face.ShiftTexdef(s, t);
451 void Scene_BrushScaleTexdef_Selected( scene::Graph& graph, float s, float t ){
452 Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
453 face.ScaleTexdef(s, t);
458 void Scene_BrushScaleTexdef_Component_Selected( scene::Graph& graph, float s, float t ){
459 Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
460 face.ScaleTexdef(s, t);
465 void Scene_BrushRotateTexdef_Selected( scene::Graph& graph, float angle ){
466 Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
467 face.RotateTexdef(angle);
472 void Scene_BrushRotateTexdef_Component_Selected( scene::Graph& graph, float angle ){
473 Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
474 face.RotateTexdef(angle);
480 void Scene_BrushSetShader_Selected( scene::Graph& graph, const char* name ){
481 Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
482 face.SetShader(name);
487 void Scene_BrushSetShader_Component_Selected( scene::Graph& graph, const char* name ){
488 Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
489 face.SetShader(name);
494 void Scene_BrushSetDetail_Selected( scene::Graph& graph, bool detail ){
495 Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
496 face.setDetail(detail);
501 bool Face_FindReplaceShader( Face& face, const char* find, const char* replace ){
502 if ( shader_equal( face.GetShader(), find ) ) {
503 face.SetShader( replace );
509 bool DoingSearch( const char *repl ){
510 return ( repl == NULL || ( strcmp( "textures/", repl ) == 0 ) );
513 void Scene_BrushFindReplaceShader( scene::Graph& graph, const char* find, const char* replace ){
514 if ( DoingSearch( replace ) ) {
515 Scene_ForEachBrush_ForEachFaceInstance(graph, [&](FaceInstance &faceinst) {
516 if (shader_equal(faceinst.getFace().GetShader(), find)) {
517 faceinst.setSelected(SelectionSystem::eFace, true);
523 Scene_ForEachBrush_ForEachFace(graph, [&](Face &face) { Face_FindReplaceShader(face, find, replace); });
527 void Scene_BrushFindReplaceShader_Selected( scene::Graph& graph, const char* find, const char* replace ){
528 if ( DoingSearch( replace ) ) {
529 Scene_ForEachSelectedBrush_ForEachFaceInstance(graph, [&](FaceInstance &faceinst) {
530 if (shader_equal(faceinst.getFace().GetShader(), find)) {
531 faceinst.setSelected(SelectionSystem::eFace, true);
537 Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
538 Face_FindReplaceShader(face, find, replace);
543 // TODO: find for components
544 // d1223m: dont even know what they are...
545 void Scene_BrushFindReplaceShader_Component_Selected( scene::Graph& graph, const char* find, const char* replace ){
546 if ( DoingSearch( replace ) ) {
551 Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
552 Face_FindReplaceShader(face, find, replace);
558 void Scene_BrushFitTexture_Selected( scene::Graph& graph, float s_repeat, float t_repeat ){
559 Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
560 face.FitTexture(s_repeat, t_repeat);
565 void Scene_BrushFitTexture_Component_Selected( scene::Graph& graph, float s_repeat, float t_repeat ){
566 Scene_ForEachSelectedBrushFace(graph, [&](Face &face) {
567 face.FitTexture(s_repeat, t_repeat);
572 TextureProjection g_defaultTextureProjection;
574 const TextureProjection& TextureTransform_getDefault(){
575 TexDef_Construct_Default( g_defaultTextureProjection );
576 return g_defaultTextureProjection;
579 void Scene_BrushConstructPrefab( scene::Graph& graph, EBrushPrefab type, std::size_t sides, const char* shader ){
580 if ( GlobalSelectionSystem().countSelected() != 0 ) {
581 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
583 Brush* brush = Node_getBrush( path.top() );
585 AABB bounds = brush->localAABB(); // copy bounds because the brush will be modified
586 Brush_ConstructPrefab( *brush, type, bounds, sides, shader, TextureTransform_getDefault() );
592 void Scene_BrushResize_Selected( scene::Graph& graph, const AABB& bounds, const char* shader ){
593 if ( GlobalSelectionSystem().countSelected() != 0 ) {
594 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
596 Brush* brush = Node_getBrush( path.top() );
598 Brush_ConstructCuboid( *brush, bounds, shader, TextureTransform_getDefault() );
604 bool Brush_hasShader( const Brush& brush, const char* name ){
605 for ( Brush::const_iterator i = brush.begin(); i != brush.end(); ++i )
607 if ( shader_equal( ( *i )->GetShader(), name ) ) {
614 class BrushSelectByShaderWalker : public scene::Graph::Walker
618 BrushSelectByShaderWalker( const char* name )
622 bool pre( const scene::Path& path, scene::Instance& instance ) const {
623 if ( path.top().get().visible() ) {
624 Brush* brush = Node_getBrush( path.top() );
625 if ( brush != 0 && Brush_hasShader( *brush, m_name ) ) {
626 Instance_getSelectable( instance )->setSelected( true );
633 void Scene_BrushSelectByShader( scene::Graph& graph, const char* name ){
634 graph.traverse( BrushSelectByShaderWalker( name ) );
637 void Scene_BrushSelectByShader_Component( scene::Graph& graph, const char* name ){
638 Scene_ForEachSelectedBrush_ForEachFaceInstance(graph, [&](FaceInstance &face) {
639 printf("checking %s = %s\n", face.getFace().GetShader(), name);
640 if (shader_equal(face.getFace().GetShader(), name)) {
641 face.setSelected( SelectionSystem::eFace, true );
646 void Scene_BrushGetTexdef_Selected( scene::Graph& graph, TextureProjection& projection ){
648 Scene_ForEachSelectedBrush_ForEachFace(graph, [&](Face &face) {
651 face.GetTexdef(projection);
656 void Scene_BrushGetTexdef_Component_Selected( scene::Graph& graph, TextureProjection& projection ){
658 if ( !g_SelectedFaceInstances.empty() ) {
659 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
660 faceInstance.getFace().GetTexdef( projection );
663 FaceGetTexdef visitor( projection );
664 Scene_ForEachSelectedBrushFace( graph, visitor );
668 void Scene_BrushGetShaderSize_Component_Selected( scene::Graph& graph, size_t& width, size_t& height ){
669 if ( !g_SelectedFaceInstances.empty() ) {
670 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
671 width = faceInstance.getFace().getShader().width();
672 height = faceInstance.getFace().getShader().height();
677 void Scene_BrushGetFlags_Selected( scene::Graph& graph, ContentsFlagsValue& flags ){
679 if ( GlobalSelectionSystem().countSelected() != 0 ) {
680 BrushInstance* brush = Instance_getBrush( GlobalSelectionSystem().ultimateSelected() );
683 Brush_forEachFace(*brush, [&](Face &face) {
686 face.GetFlags(flags);
692 Scene_ForEachSelectedBrush_ForEachFace( graph, FaceGetFlags( flags ) );
696 void Scene_BrushGetFlags_Component_Selected( scene::Graph& graph, ContentsFlagsValue& flags ){
698 if ( !g_SelectedFaceInstances.empty() ) {
699 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
700 faceInstance.getFace().GetFlags( flags );
703 Scene_ForEachSelectedBrushFace( graph, FaceGetFlags( flags ) );
708 void Scene_BrushGetShader_Selected( scene::Graph& graph, CopiedString& shader ){
710 if ( GlobalSelectionSystem().countSelected() != 0 ) {
711 BrushInstance* brush = Instance_getBrush( GlobalSelectionSystem().ultimateSelected() );
714 Brush_forEachFace(*brush, [&](Face &face) {
717 shader = face.GetShader();
723 Scene_ForEachSelectedBrush_ForEachFace( graph, FaceGetShader( shader ) );
727 void Scene_BrushGetShader_Component_Selected( scene::Graph& graph, CopiedString& shader ){
729 if ( !g_SelectedFaceInstances.empty() ) {
730 FaceInstance& faceInstance = g_SelectedFaceInstances.last();
731 shader = faceInstance.getFace().GetShader();
734 FaceGetShader visitor( shader );
735 Scene_ForEachSelectedBrushFace( graph, visitor );
740 class filter_face_shader : public FaceFilter
742 const char* m_shader;
744 filter_face_shader( const char* shader ) : m_shader( shader ){
747 bool filter( const Face& face ) const {
748 return shader_equal( face.GetShader(), m_shader );
752 class filter_face_shader_prefix : public FaceFilter
754 const char* m_prefix;
756 filter_face_shader_prefix( const char* prefix ) : m_prefix( prefix ){
759 bool filter( const Face& face ) const {
760 return shader_equal_n( face.GetShader(), m_prefix, strlen( m_prefix ) );
764 class filter_face_flags : public FaceFilter
768 filter_face_flags( int flags ) : m_flags( flags ){
771 bool filter( const Face& face ) const {
772 return ( face.getShader().shaderFlags() & m_flags ) != 0;
776 class filter_face_contents : public FaceFilter
780 filter_face_contents( int contents ) : m_contents( contents ){
783 bool filter( const Face& face ) const {
784 return ( face.getShader().m_flags.m_contentFlags & m_contents ) != 0;
790 class filter_brush_any_face : public BrushFilter
792 FaceFilter* m_filter;
794 filter_brush_any_face( FaceFilter* filter ) : m_filter( filter ){
797 bool filter( const Brush& brush ) const {
798 bool filtered = false;
799 Brush_forEachFace(brush, [&](Face &face) {
800 if (m_filter->filter(face)) {
808 class filter_brush_all_faces : public BrushFilter
810 FaceFilter* m_filter;
812 filter_brush_all_faces( FaceFilter* filter ) : m_filter( filter ){
814 bool filter( const Brush& brush ) const {
815 bool filtered = true;
816 Brush_forEachFace(brush, [&](Face &face) {
817 if (!m_filter->filter(face)) {
826 filter_face_flags g_filter_face_clip( QER_CLIP );
827 filter_brush_all_faces g_filter_brush_clip( &g_filter_face_clip );
829 filter_face_shader g_filter_face_clip_q2( "textures/clip" );
830 filter_brush_all_faces g_filter_brush_clip_q2( &g_filter_face_clip_q2 );
832 filter_face_shader g_filter_face_weapclip( "textures/common/weapclip" );
833 filter_brush_all_faces g_filter_brush_weapclip( &g_filter_face_weapclip );
835 filter_face_shader g_filter_face_commonclip( "textures/common/clip" );
836 filter_brush_all_faces g_filter_brush_commonclip( &g_filter_face_commonclip );
838 filter_face_shader g_filter_face_fullclip( "textures/common/fullclip" );
839 filter_brush_all_faces g_filter_brush_fullclip( &g_filter_face_fullclip );
841 filter_face_shader g_filter_face_botclip( "textures/common/botclip" );
842 filter_brush_all_faces g_filter_brush_botclip( &g_filter_face_botclip );
844 filter_face_shader_prefix g_filter_face_caulk( "textures/common/caulk" );
845 filter_brush_all_faces g_filter_brush_caulk( &g_filter_face_caulk );
847 filter_face_shader_prefix g_filter_face_caulk_ja( "textures/system/caulk" );
848 filter_brush_all_faces g_filter_brush_caulk_ja( &g_filter_face_caulk_ja );
850 filter_face_shader_prefix g_filter_face_liquids( "textures/liquids/" );
851 filter_brush_any_face g_filter_brush_liquids( &g_filter_face_liquids );
853 filter_face_shader g_filter_face_hint( "textures/common/hint" );
854 filter_brush_any_face g_filter_brush_hint( &g_filter_face_hint );
856 filter_face_shader g_filter_face_hintlocal( "textures/common/hintlocal" );
857 filter_brush_any_face g_filter_brush_hintlocal( &g_filter_face_hintlocal );
859 filter_face_shader g_filter_face_hint_q2( "textures/hint" );
860 filter_brush_any_face g_filter_brush_hint_q2( &g_filter_face_hint_q2 );
862 filter_face_shader g_filter_face_hint_ja( "textures/system/hint" );
863 filter_brush_any_face g_filter_brush_hint_ja( &g_filter_face_hint_ja );
865 filter_face_shader g_filter_face_subtlehint( "textures/common/subtlehint" );
866 filter_brush_any_face g_filter_brush_subtlehint( &g_filter_face_subtlehint );
868 filter_face_shader g_filter_face_areaportal( "textures/common/areaportal" );
869 filter_brush_all_faces g_filter_brush_areaportal( &g_filter_face_areaportal );
871 filter_face_shader g_filter_face_visportal( "textures/editor/visportal" );
872 filter_brush_any_face g_filter_brush_visportal( &g_filter_face_visportal );
874 filter_face_shader g_filter_face_clusterportal( "textures/common/clusterportal" );
875 filter_brush_all_faces g_filter_brush_clusterportal( &g_filter_face_clusterportal );
877 filter_face_shader g_filter_face_lightgrid( "textures/common/lightgrid" );
878 filter_brush_all_faces g_filter_brush_lightgrid( &g_filter_face_lightgrid );
880 filter_face_flags g_filter_face_translucent( QER_TRANS );
881 filter_brush_all_faces g_filter_brush_translucent( &g_filter_face_translucent );
883 filter_face_contents g_filter_face_detail( BRUSH_DETAIL_MASK );
884 filter_brush_all_faces g_filter_brush_detail( &g_filter_face_detail );
886 filter_face_shader_prefix g_filter_face_decals( "textures/decals/" );
887 filter_brush_any_face g_filter_brush_decals( &g_filter_face_decals );
890 void BrushFilters_construct(){
891 add_brush_filter( g_filter_brush_clip, EXCLUDE_CLIP );
892 add_brush_filter( g_filter_brush_clip_q2, EXCLUDE_CLIP );
893 add_brush_filter( g_filter_brush_weapclip, EXCLUDE_CLIP );
894 add_brush_filter( g_filter_brush_fullclip, EXCLUDE_CLIP );
895 add_brush_filter( g_filter_brush_commonclip, EXCLUDE_CLIP );
896 add_brush_filter( g_filter_brush_botclip, EXCLUDE_BOTCLIP );
897 add_brush_filter( g_filter_brush_caulk, EXCLUDE_CAULK );
898 add_brush_filter( g_filter_brush_caulk_ja, EXCLUDE_CAULK );
899 add_face_filter( g_filter_face_caulk, EXCLUDE_CAULK );
900 add_face_filter( g_filter_face_caulk_ja, EXCLUDE_CAULK );
901 add_brush_filter( g_filter_brush_liquids, EXCLUDE_LIQUIDS );
902 add_brush_filter( g_filter_brush_hint, EXCLUDE_HINTSSKIPS );
903 add_brush_filter( g_filter_brush_hintlocal, EXCLUDE_HINTSSKIPS );
904 add_brush_filter( g_filter_brush_hint_q2, EXCLUDE_HINTSSKIPS );
905 add_brush_filter( g_filter_brush_hint_ja, EXCLUDE_HINTSSKIPS );
906 add_brush_filter( g_filter_brush_subtlehint, EXCLUDE_HINTSSKIPS );
907 add_brush_filter( g_filter_brush_clusterportal, EXCLUDE_CLUSTERPORTALS );
908 add_brush_filter( g_filter_brush_visportal, EXCLUDE_VISPORTALS );
909 add_brush_filter( g_filter_brush_areaportal, EXCLUDE_AREAPORTALS );
910 add_brush_filter( g_filter_brush_translucent, EXCLUDE_TRANSLUCENT );
911 add_brush_filter( g_filter_brush_detail, EXCLUDE_DETAILS );
912 add_brush_filter( g_filter_brush_detail, EXCLUDE_STRUCTURAL, true );
913 add_brush_filter( g_filter_brush_lightgrid, EXCLUDE_LIGHTGRID );
914 add_brush_filter( g_filter_brush_decals, EXCLUDE_DECALS );
919 void normalquantisation_draw(){
921 glBegin( GL_POINTS );
922 for ( std::size_t i = 0; i <= c_quantise_normal; ++i )
924 for ( std::size_t j = 0; j <= c_quantise_normal; ++j )
926 Normal3f vertex( normal3f_normalised( Normal3f(
927 static_cast<float>( c_quantise_normal - j - i ),
928 static_cast<float>( i ),
929 static_cast<float>( j )
931 VectorScale( normal3f_to_array( vertex ), 64.f, normal3f_to_array( vertex ) );
932 glVertex3fv( normal3f_to_array( vertex ) );
933 vertex.x = -vertex.x;
934 glVertex3fv( normal3f_to_array( vertex ) );
940 class RenderableNormalQuantisation : public OpenGLRenderable
943 void render( RenderStateFlags state ) const {
944 normalquantisation_draw();
948 const float g_test_quantise_normal = 1.f / static_cast<float>( 1 << 3 );
950 class TestNormalQuantisation
952 void check_normal( const Normal3f& normal, const Normal3f& other ){
953 spherical_t spherical = spherical_from_normal3f( normal );
954 double longditude = RAD2DEG( spherical.longditude );
955 double latitude = RAD2DEG( spherical.latitude );
956 double x = cos( spherical.longditude ) * sin( spherical.latitude );
957 double y = sin( spherical.longditude ) * sin( spherical.latitude );
958 double z = cos( spherical.latitude );
960 ASSERT_MESSAGE( normal3f_dot( normal, other ) > 0.99, "bleh" );
963 void test_normal( const Normal3f& normal ){
964 Normal3f test = normal3f_from_spherical( spherical_from_normal3f( normal ) );
965 check_normal( normal, test );
967 EOctant octant = normal3f_classify_octant( normal );
968 Normal3f folded = normal3f_fold_octant( normal, octant );
969 ESextant sextant = normal3f_classify_sextant( folded );
970 folded = normal3f_fold_sextant( folded, sextant );
972 double scale = static_cast<float>( c_quantise_normal ) / ( folded.x + folded.y + folded.z );
974 double zbits = folded.z * scale;
975 double ybits = folded.y * scale;
977 std::size_t zbits_q = static_cast<std::size_t>( zbits );
978 std::size_t ybits_q = static_cast<std::size_t>( ybits );
980 ASSERT_MESSAGE( zbits_q <= ( c_quantise_normal / 8 ) * 3, "bleh" );
981 ASSERT_MESSAGE( ybits_q <= ( c_quantise_normal / 2 ), "bleh" );
982 ASSERT_MESSAGE( zbits_q + ( ( c_quantise_normal / 2 ) - ybits_q ) <= ( c_quantise_normal / 2 ), "bleh" );
984 std::size_t y_t = ( zbits_q < ( c_quantise_normal / 4 ) ) ? ybits_q : ( c_quantise_normal / 2 ) - ybits_q;
985 std::size_t z_t = ( zbits_q < ( c_quantise_normal / 4 ) ) ? zbits_q : ( c_quantise_normal / 2 ) - zbits_q;
986 std::size_t index = ( c_quantise_normal / 4 ) * y_t + z_t;
987 ASSERT_MESSAGE( index <= ( c_quantise_normal / 4 ) * ( c_quantise_normal / 2 ), "bleh" );
989 Normal3f tmp( c_quantise_normal - zbits_q - ybits_q, ybits_q, zbits_q );
990 tmp = normal3f_normalised( tmp );
992 Normal3f unfolded = normal3f_unfold_octant( normal3f_unfold_sextant( tmp, sextant ), octant );
994 check_normal( normal, unfolded );
996 double dot = normal3f_dot( normal, unfolded );
997 float length = VectorLength( normal3f_to_array( unfolded ) );
998 float inv_length = 1 / length;
1000 Normal3f quantised = normal3f_quantised( normal );
1001 check_normal( normal, quantised );
1003 void test2( const Normal3f& normal, const Normal3f& other ){
1004 if ( normal3f_quantised( normal ) != normal3f_quantised( other ) ) {
1009 static Normal3f normalise( float x, float y, float z ){
1010 return normal3f_normalised( Normal3f( x, y, z ) );
1014 return static_cast<float>( rand() - ( RAND_MAX / 2 ) );
1017 Normal3f normal3f_rand(){
1018 return normalise( vec_rand(), vec_rand(), vec_rand() );
1022 TestNormalQuantisation(){
1023 for ( int i = 4096; i > 0; --i )
1024 test_normal( normal3f_rand() );
1026 test_normal( normalise( 1, 0, 0 ) );
1027 test_normal( normalise( 0, 1, 0 ) );
1028 test_normal( normalise( 0, 0, 1 ) );
1029 test_normal( normalise( 1, 1, 0 ) );
1030 test_normal( normalise( 1, 0, 1 ) );
1031 test_normal( normalise( 0, 1, 1 ) );
1033 test_normal( normalise( 10000, 10000, 10000 ) );
1034 test_normal( normalise( 10000, 10000, 10001 ) );
1035 test_normal( normalise( 10000, 10000, 10002 ) );
1036 test_normal( normalise( 10000, 10000, 10010 ) );
1037 test_normal( normalise( 10000, 10000, 10020 ) );
1038 test_normal( normalise( 10000, 10000, 10030 ) );
1039 test_normal( normalise( 10000, 10000, 10100 ) );
1040 test_normal( normalise( 10000, 10000, 10101 ) );
1041 test_normal( normalise( 10000, 10000, 10102 ) );
1042 test_normal( normalise( 10000, 10000, 10200 ) );
1043 test_normal( normalise( 10000, 10000, 10201 ) );
1044 test_normal( normalise( 10000, 10000, 10202 ) );
1045 test_normal( normalise( 10000, 10000, 10203 ) );
1046 test_normal( normalise( 10000, 10000, 10300 ) );
1049 test2( normalise( 10000, 10000, 10000 ), normalise( 10000, 10000, 10001 ) );
1050 test2( normalise( 10000, 10000, 10001 ), normalise( 10000, 10001, 10000 ) );
1054 TestNormalQuantisation g_testNormalQuantisation;
1060 class TestSelectableObserver : public observer_template<const Selectable&>
1063 void notify( const Selectable& arguments ){
1064 bool bleh = arguments.isSelected();
1068 inline void test_bleh(){
1069 TestSelectableObserver test;
1070 ObservableSelectableInstance< SingleObservable< SelectionChangeCallback > > bleh;
1071 bleh.attach( test );
1072 bleh.setSelected( true );
1073 bleh.detach( test );
1084 const TestBleh testbleh;
1089 class TestRefcountedString
1092 TestRefcountedString(){
1095 SmartString string1( "string1" );
1096 SmartString string2( string1 );
1097 SmartString string3( string2 );
1100 // refcounted assignment
1101 SmartString string1( "string1" );
1102 SmartString string2( "string2" );
1107 SmartString string1( "string1" );
1108 SmartString string2( "string2" );
1109 string1 = string2.c_str();
1113 SmartString string1( "string1" );
1117 // self-assignment via another reference
1118 SmartString string1( "string1" );
1119 SmartString string2( string1 );
1125 const TestRefcountedString g_testRefcountedString;
1129 void Select_MakeDetail(){
1130 UndoableCommand undo( "brushSetDetail" );
1131 Scene_BrushSetDetail_Selected( GlobalSceneGraph(), true );
1134 void Select_MakeStructural(){
1135 UndoableCommand undo( "brushClearDetail" );
1136 Scene_BrushSetDetail_Selected( GlobalSceneGraph(), false );
1139 class BrushMakeSided
1141 std::size_t m_count;
1143 BrushMakeSided( std::size_t count )
1148 Scene_BrushConstructPrefab( GlobalSceneGraph(), eBrushPrism, m_count, TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
1151 typedef MemberCaller<BrushMakeSided, void(), &BrushMakeSided::set> SetCaller;
1155 BrushMakeSided g_brushmakesided3( 3 );
1156 BrushMakeSided g_brushmakesided4( 4 );
1157 BrushMakeSided g_brushmakesided5( 5 );
1158 BrushMakeSided g_brushmakesided6( 6 );
1159 BrushMakeSided g_brushmakesided7( 7 );
1160 BrushMakeSided g_brushmakesided8( 8 );
1161 BrushMakeSided g_brushmakesided9( 9 );
1163 inline int axis_for_viewtype( int viewtype ){
1178 EBrushPrefab m_type;
1180 BrushPrefab( EBrushPrefab type )
1185 DoSides( m_type, axis_for_viewtype( GetViewAxis() ) );
1188 typedef MemberCaller<BrushPrefab, void(), &BrushPrefab::set> SetCaller;
1191 BrushPrefab g_brushprism( eBrushPrism );
1192 BrushPrefab g_brushcone( eBrushCone );
1193 BrushPrefab g_brushsphere( eBrushSphere );
1194 BrushPrefab g_brushrock( eBrushRock );
1203 void OnClipMode( bool enable );
1208 void ClipSelected(){
1210 UndoableCommand undo( "clipperClip" );
1215 void SplitSelected(){
1217 UndoableCommand undo( "clipperSplit" );
1227 Callback<void()> g_texture_lock_status_changed;
1228 ConstReferenceCaller<bool, void(const Callback<void(bool)> &), PropertyImpl<bool>::Export> g_texdef_movelock_caller( g_brush_texturelock_enabled );
1229 ToggleItem g_texdef_movelock_item( g_texdef_movelock_caller );
1231 void Texdef_ToggleMoveLock(){
1232 g_brush_texturelock_enabled = !g_brush_texturelock_enabled;
1233 g_texdef_movelock_item.update();
1234 g_texture_lock_status_changed();
1238 void Brush_registerCommands(){
1239 GlobalToggles_insert( "TogTexLock", makeCallbackF(Texdef_ToggleMoveLock), ToggleItem::AddCallbackCaller( g_texdef_movelock_item ), Accelerator( 'T', (GdkModifierType)GDK_SHIFT_MASK ) );
1241 GlobalCommands_insert( "BrushPrism", BrushPrefab::SetCaller( g_brushprism ) );
1242 GlobalCommands_insert( "BrushCone", BrushPrefab::SetCaller( g_brushcone ) );
1243 GlobalCommands_insert( "BrushSphere", BrushPrefab::SetCaller( g_brushsphere ) );
1244 GlobalCommands_insert( "BrushRock", BrushPrefab::SetCaller( g_brushrock ) );
1246 GlobalCommands_insert( "Brush3Sided", BrushMakeSided::SetCaller( g_brushmakesided3 ), Accelerator( '3', (GdkModifierType)GDK_CONTROL_MASK ) );
1247 GlobalCommands_insert( "Brush4Sided", BrushMakeSided::SetCaller( g_brushmakesided4 ), Accelerator( '4', (GdkModifierType)GDK_CONTROL_MASK ) );
1248 GlobalCommands_insert( "Brush5Sided", BrushMakeSided::SetCaller( g_brushmakesided5 ), Accelerator( '5', (GdkModifierType)GDK_CONTROL_MASK ) );
1249 GlobalCommands_insert( "Brush6Sided", BrushMakeSided::SetCaller( g_brushmakesided6 ), Accelerator( '6', (GdkModifierType)GDK_CONTROL_MASK ) );
1250 GlobalCommands_insert( "Brush7Sided", BrushMakeSided::SetCaller( g_brushmakesided7 ), Accelerator( '7', (GdkModifierType)GDK_CONTROL_MASK ) );
1251 GlobalCommands_insert( "Brush8Sided", BrushMakeSided::SetCaller( g_brushmakesided8 ), Accelerator( '8', (GdkModifierType)GDK_CONTROL_MASK ) );
1252 GlobalCommands_insert( "Brush9Sided", BrushMakeSided::SetCaller( g_brushmakesided9 ), Accelerator( '9', (GdkModifierType)GDK_CONTROL_MASK ) );
1254 GlobalCommands_insert( "ClipSelected", makeCallbackF(ClipSelected), Accelerator( GDK_KEY_Return ) );
1255 GlobalCommands_insert( "SplitSelected", makeCallbackF(SplitSelected), Accelerator( GDK_KEY_Return, (GdkModifierType)GDK_SHIFT_MASK ) );
1256 GlobalCommands_insert( "FlipClip", makeCallbackF(FlipClipper), Accelerator( GDK_KEY_Return, (GdkModifierType)GDK_CONTROL_MASK ) );
1258 GlobalCommands_insert( "MakeDetail", makeCallbackF(Select_MakeDetail), Accelerator( 'M', (GdkModifierType)GDK_CONTROL_MASK ) );
1259 GlobalCommands_insert( "MakeStructural", makeCallbackF(Select_MakeStructural), Accelerator( 'S', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
1262 void Brush_constructMenu( ui::Menu menu ){
1263 create_menu_item_with_mnemonic( menu, "Prism...", "BrushPrism" );
1264 create_menu_item_with_mnemonic( menu, "Cone...", "BrushCone" );
1265 create_menu_item_with_mnemonic( menu, "Sphere...", "BrushSphere" );
1266 create_menu_item_with_mnemonic( menu, "Rock...", "BrushRock" );
1267 menu_separator( menu );
1269 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "CSG" );
1270 if ( g_Layout_enableDetachableMenus.m_value ) {
1271 menu_tearoff( menu_in_menu );
1273 create_menu_item_with_mnemonic( menu_in_menu, "Make _Hollow", "CSGHollow" );
1274 create_menu_item_with_mnemonic( menu_in_menu, "Make _Room", "CSGRoom" );
1275 create_menu_item_with_mnemonic( menu_in_menu, "CSG _Subtract", "CSGSubtract" );
1276 create_menu_item_with_mnemonic( menu_in_menu, "CSG _Merge", "CSGMerge" );
1278 menu_separator( menu );
1280 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Clipper" );
1281 if ( g_Layout_enableDetachableMenus.m_value ) {
1282 menu_tearoff( menu_in_menu );
1285 create_menu_item_with_mnemonic( menu_in_menu, "Clip selection", "ClipSelected" );
1286 create_menu_item_with_mnemonic( menu_in_menu, "Split selection", "SplitSelected" );
1287 create_menu_item_with_mnemonic( menu_in_menu, "Flip Clip orientation", "FlipClip" );
1289 menu_separator( menu );
1290 create_menu_item_with_mnemonic( menu, "Make detail", "MakeDetail" );
1291 create_menu_item_with_mnemonic( menu, "Make structural", "MakeStructural" );
1292 // create_menu_item_with_mnemonic( menu, "Snap selection to _grid", "SnapToGrid" );
1294 create_check_menu_item_with_mnemonic( menu, "Texture Lock", "TogTexLock" );
1295 menu_separator( menu );
1296 create_menu_item_with_mnemonic( menu, "Copy Face Texture", "FaceCopyTexture" );
1297 create_menu_item_with_mnemonic( menu, "Paste Face Texture", "FacePasteTexture" );
1299 command_connect_accelerator( "Brush3Sided" );
1300 command_connect_accelerator( "Brush4Sided" );
1301 command_connect_accelerator( "Brush5Sided" );
1302 command_connect_accelerator( "Brush6Sided" );
1303 command_connect_accelerator( "Brush7Sided" );
1304 command_connect_accelerator( "Brush8Sided" );
1305 command_connect_accelerator( "Brush9Sided" );