2 Copyright (C) 1999-2007 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
26 int FindPoint (vec3_t point)
30 for (i=0 ; i<g_qeglobals.d_numpoints ; i++)
33 if (fabs(point[j] - g_qeglobals.d_points[i][j]) > 0.1)
39 VectorCopy (point, g_qeglobals.d_points[g_qeglobals.d_numpoints]);
40 //qeglobals.d_points[g_qeglobals.d_numpoints] = point;
41 if (g_qeglobals.d_numpoints < MAX_POINTS-1)
43 g_qeglobals.d_numpoints++;
46 return g_qeglobals.d_numpoints-1;
50 int FindEdge (int p1, int p2, face_t *f)
54 for (i=0 ; i<g_qeglobals.d_numedges ; i++)
55 if (g_qeglobals.d_edges[i].p1 == p2 && g_qeglobals.d_edges[i].p2 == p1)
57 g_qeglobals.d_edges[i].f2 = f;
59 Sys_Printf("g_qeglobals.d_edges[%d].f2 = %p\n", i, f);
64 g_qeglobals.d_edges[g_qeglobals.d_numedges].p1 = p1;
65 g_qeglobals.d_edges[g_qeglobals.d_numedges].p2 = p2;
66 g_qeglobals.d_edges[g_qeglobals.d_numedges].f1 = f;
68 Sys_Printf("g_qeglobals.d_edges[%d].f1 = %p\n", g_qeglobals.d_numedges, f);
71 if (g_qeglobals.d_numedges < MAX_EDGES-1)
73 g_qeglobals.d_numedges++;
76 return g_qeglobals.d_numedges-1;
79 void MakeFace (brush_t* b, face_t *f)
85 w = Brush_MakeFaceWinding (b, f);
88 for (i=0 ; i<w->numpoints ; i++)
89 pnum[i] = FindPoint (w->points[i]);
90 for (i=0 ; i<w->numpoints ; i++)
91 FindEdge (pnum[i], pnum[(i+1)%w->numpoints], f);
96 void SetupVertexSelection (void)
101 g_qeglobals.d_numpoints = 0;
102 g_qeglobals.d_numedges = 0;
104 for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
106 if (b->patchBrush || b->owner->eclass->fixedsize)
107 continue; // don't make edge and vertex handles for patchbrushes
108 for (f=b->brush_faces ; f ; f=f->next)
113 void SelectFaceEdge (brush_t* b, face_t *f, int p1, int p2)
121 Sys_Printf("SelectFaceEdge %p %p\n", b, f);
124 w = Winding_Clone(f->face_winding);//Brush_MakeFaceWinding (b, f);
127 for (i=0 ; i<w->numpoints ; i++)
128 pnum[i] = FindPoint (w->points[i]);
130 for (i=0 ; i<w->numpoints ; i++)
131 if (pnum[i] == p1 && pnum[(i+1)%w->numpoints] == p2)
133 VectorCopy (g_qeglobals.d_points[pnum[i]], f->planepts[0]);
134 VectorCopy (g_qeglobals.d_points[pnum[(i+1)%w->numpoints]], f->planepts[1]);
135 VectorCopy (g_qeglobals.d_points[pnum[(i+2)%w->numpoints]], f->planepts[2]);
136 for (j=0 ; j<3 ; j++)
138 for (k=0 ; k<3 ; k++)
140 f->planepts[j][k] = floor(f->planepts[j][k]/g_qeglobals.d_gridsize+0.5)*g_qeglobals.d_gridsize;
144 AddPlanept (f->planepts[0]);
145 AddPlanept (f->planepts[1]);
149 if (i == w->numpoints)
150 Sys_Printf ("SelectFaceEdge: failed\n");
155 void SelectVertex (int p1)
162 for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
164 for (f=b->brush_faces ; f ; f=f->next)
166 w = Brush_MakeFaceWinding (b, f);
169 for (i=0 ; i<w->numpoints ; i++)
171 if (FindPoint (w->points[i]) == p1)
173 VectorCopy (w->points[(i+w->numpoints-1)%w->numpoints], f->planepts[0]);
174 VectorCopy (w->points[i], f->planepts[1]);
175 VectorCopy (w->points[(i+1)%w->numpoints], f->planepts[2]);
176 // NOTE: used to be a planepts clamping to grid here
178 AddPlanept (f->planepts[1]);
188 #define SELECT_EPSILON 8
190 void SelectVertexByRay (vec3_t org, vec3_t dir)
193 float d, bestd = VEC_MAX;
194 vec_t epsilon, divergence;
196 ray_construct_for_vec3(&ray, org, dir);
198 // find the point closest to the ray
200 if ((fabs(org[0]) == g_MaxWorldCoord || fabs(org[1]) == g_MaxWorldCoord || fabs(org[2]) == g_MaxWorldCoord)
201 && (fabs(dir[0]) == 1.0f || fabs(dir[1]) == 1.0f || fabs(dir[2]) == 1.0f)) // very unlikely unless 2d view
204 epsilon = SELECT_EPSILON / g_pParentWnd->GetXYWnd()->Scale(); // compensate for zoom level
208 divergence = SELECT_EPSILON / (g_pParentWnd->GetCamWnd()->Camera()->width*0.5); // radius / focal length
212 for (i=0 ; i<g_qeglobals.d_numpoints ; i++)
214 d = ray_intersect_point(&ray, g_qeglobals.d_points[i], epsilon, divergence);
225 Sys_Printf ("Click didn't hit a vertex\n");
228 Sys_Printf ("hit vertex\n");
229 g_qeglobals.d_move_points[g_qeglobals.d_num_move_points++] = g_qeglobals.d_points[besti];
230 if (!g_PrefsDlg.m_bVertexSplit)
232 SelectVertex (besti);
236 // TTimo: NOTE: we should not have to put extern funcs like that
237 // those should be defined in qe3.h
238 extern void AddPatchMovePoint(vec3_t v, bool bMulti, bool bFull);
239 extern int PointInMoveList(float *pf);
240 void SelectCurvePointByRay (vec3_t org, vec3_t dir, int buttons)
243 float d, bestd = VEC_MAX;
245 vec_t epsilon, divergence;
247 ray_construct_for_vec3(&ray, org, dir);
249 // find the point closest to the ray
251 if ((fabs(org[0]) == g_MaxWorldCoord || fabs(org[1]) == g_MaxWorldCoord || fabs(org[2]) == g_MaxWorldCoord)
252 && (fabs(dir[0]) == 1.0f || fabs(dir[1]) == 1.0f || fabs(dir[2]) == 1.0f)) // very unlikely unless 2d view
255 epsilon = SELECT_EPSILON / g_pParentWnd->GetXYWnd()->Scale(); // compensate for zoom level
259 divergence = SELECT_EPSILON / (g_pParentWnd->GetCamWnd()->Camera()->width*0.5); // radius / focal length
264 g_qeglobals.d_numpoints = 0;
266 for (brush_t *pb = selected_brushes.next ; pb != &selected_brushes ; pb = pb->next)
270 patchMesh_t* p = pb->pPatch;
272 for (i = 0 ; i < p->width ; i++ )
274 for ( j = 0 ; j < p->height ; j++ )
276 d = ray_intersect_point(&ray, p->ctrl[i][j].xyz, epsilon, divergence);
283 if (PointInMoveList(*pPointBest) != -1 && PointInMoveList(p->ctrl[i][j].xyz) == -1)
284 continue; // choose selected points with preference over unselected
286 pPointBest = &p->ctrl[i][j].xyz;
293 if (pPointBest == NULL)
295 if (g_pParentWnd->ActiveXY()->AreaSelectOK())
297 g_qeglobals.d_select_mode = sel_area;
298 VectorCopy(org, g_qeglobals.d_vAreaTL);
299 VectorCopy(org, g_qeglobals.d_vAreaBR);
304 AddPatchMovePoint(pPointBest[0], buttons & MK_CONTROL, buttons & MK_SHIFT);
308 // had to use the #define DBG_WNDG to identify
309 // the first loop that checks the best edge is broken in release-optimized build
310 // unrolled the mid[] loop and forced floating consistency on seems to fix
312 #pragma optimize( "p", on )
314 void SelectEdgeByRay (vec3_t org, vec3_t dir)
317 float d, bestd = VEC_MAX;
320 vec_t epsilon, divergence;
322 ray_construct_for_vec3(&ray, org, dir);
324 // find the edge closest to the ray
326 if ((fabs(org[0]) == g_MaxWorldCoord || fabs(org[1]) == g_MaxWorldCoord || fabs(org[2]) == g_MaxWorldCoord)
327 && (fabs(dir[0]) == 1.0f || fabs(dir[1]) == 1.0f || fabs(dir[2]) == 1.0f)) // very unlikely unless 2d view
330 epsilon = SELECT_EPSILON / g_pParentWnd->GetXYWnd()->Scale(); // compensate for zoom level
334 divergence = SELECT_EPSILON / (g_pParentWnd->GetCamWnd()->Camera()->width*0.5); // radius / focal length
338 for (i=0 ; i<g_qeglobals.d_numedges ; i++)
340 mid[0] = 0.5f*(g_qeglobals.d_points[g_qeglobals.d_edges[i].p1][0] + g_qeglobals.d_points[g_qeglobals.d_edges[i].p2][0]);
341 mid[1] = 0.5f*(g_qeglobals.d_points[g_qeglobals.d_edges[i].p1][1] + g_qeglobals.d_points[g_qeglobals.d_edges[i].p2][1]);
342 mid[2] = 0.5f*(g_qeglobals.d_points[g_qeglobals.d_edges[i].p1][2] + g_qeglobals.d_points[g_qeglobals.d_edges[i].p2][2]);
344 d = ray_intersect_point(&ray, mid, epsilon, divergence);
347 Sys_Printf("d: %f\n", d);
352 Sys_Printf("bestd = d\n");
361 Sys_Printf ("Click didn't hit an edge\n");
364 Sys_Printf ("Hit edge\n");
366 // make the two faces that border the edge use the two edge points
367 // as primary drag points
368 g_qeglobals.d_num_move_points = 0;
369 e = &g_qeglobals.d_edges[besti];
371 Sys_Printf("besti: %d\n", besti);
374 Sys_Printf ("e->f1 == NULL e->f2 %p\n", e->f2);
378 Sys_Printf ("e->f1 %p e->f2 == NULL\n",e->f1);
381 for (brush_t* b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
383 SelectFaceEdge (b, e->f1, e->p1, e->p2);
384 SelectFaceEdge (b, e->f2, e->p2, e->p1);