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 //-----------------------------------------------------------------------------
25 // a class to handle control points in a 2D view
26 // TODO: this one can be placed under an interface, and provided to the editor as service
31 void CControlPointsManagerBFace::Init (int iPts, CtrlPts_t *Pts, C2DView *p2DView, int TexSize[2],
32 _QERFaceData* pFaceData, OpenGLBinding *pQglTable)
37 // store the initial config
38 memcpy( &m_RefPts, Pts, sizeof( CtrlPts_t ) );
40 memset( m_TM, 0, sizeof( float[2][3] ) );
41 m_TM[0][0] = 1.0f; m_TM[1][1] = 1.0f;
43 m_TransOffset[0] = 0.0f; m_TransOffset[1] = 0.0f;
44 m_TexSize[0] = TexSize[0];
45 m_TexSize[1] = TexSize[1];
46 m_pFaceData = pFaceData;
48 CControlPointsManager::Init( p2DView, pQglTable );
51 bool CControlPointsManagerBFace::OnLButtonDown (int xPos, int yPos)
53 if (ManagerState == Idle)
57 // scan the point list to see if we selected something
58 for ( i=0; i<m_NumPoints; i++ )
59 if ( m_p2DView->DoesSelect( xPos, yPos, m_pPts->data[i] ) )
63 if (m_bGotAnchor && i == m_iAnchorPoint)
65 // this means we selected the Anchor, so we'll translate
68 // perhaps we won't use translation, but we can compute it anyway
69 ComputeTransOffset(i);
72 // we have an Anchor and selected another point
73 m_Anchor[0] = m_pPts->data[m_iAnchorPoint][0];
74 m_Anchor[1] = m_pPts->data[m_iAnchorPoint][1];
77 // send a repaint message
78 g_pToolWnd->Redraw ();
84 bool CControlPointsManagerBFace::OnMouseMove (int xPos, int yPos)
86 if (ManagerState == Drag)
90 // there's an anchor, we are rotating the shape
91 // we need to work in XY space for orthonormality
100 m_p2DView->GridForWindow( Pt, xPos, yPos );
101 V2[0] = Pt[0] - m_Anchor[0];
102 V2[1] = Pt[1] - m_Anchor[1];
104 V1[0] = m_RefPts.data[m_iDragPoint][0] - m_RefPts.data[m_iAnchorPoint][0];
105 V1[1] = m_RefPts.data[m_iDragPoint][1] - m_RefPts.data[m_iAnchorPoint][1];
107 // compute transformation from V1 to V2
108 // we need to work in XY orthonormal space
109 XYSpaceForSTSpace( V1, V1 );
110 XYSpaceForSTSpace( V2, V2 );
111 VectorNormalize( V2, V2 );
112 VectorNormalize( V1, V1 );
113 c = DotProduct( V1, V2 );
114 CrossProduct( V1, V2, cross );
115 s = VectorLength( cross );
116 // we compute the transformation matrix in XY space
117 // reference position of the Anchor in XY space
118 XYSpaceForSTSpace( XYRefAnchor, m_RefPts.data[m_iAnchorPoint] );
119 // current position of the Anchor in XY space
120 XYSpaceForSTSpace( XYAnchor, m_Anchor );
121 // compute transformation matrix
122 XYTM[0][0] = c; XYTM[1][1] = c;
125 XYTM[0][1] = s; XYTM[1][0] = -s;
126 XYTM[0][2] = -c*XYRefAnchor[0] - s*XYRefAnchor[1] + XYAnchor[0];
127 XYTM[1][2] = s*XYRefAnchor[0] - c*XYRefAnchor[1] + XYAnchor[1];
128 // express this transformation matrix in ST space
129 m_TM[0][0] = XYTM[0][0];
130 m_TM[1][0] = XYTM[1][0] * (float)m_TexSize[0] / (float)m_TexSize[1];
131 m_TM[0][1] = XYTM[0][1] * (float)m_TexSize[1] / (float)m_TexSize[0];
132 m_TM[1][1] = XYTM[1][1];
133 m_TM[0][2] = XYTM[0][2] / (float)m_TexSize[0];
134 m_TM[1][2] = XYTM[1][2] / (float)m_TexSize[1];
140 // no Anchor point is defined, we translate all points
141 m_p2DView->GridForWindow( m_pPts->data[m_iDragPoint], xPos, yPos );
142 m_TM[0][2] = m_pPts->data[m_iDragPoint][0] + m_TransOffset[0];
143 m_TM[1][2] = m_pPts->data[m_iDragPoint][1] + m_TransOffset[1];
147 // send a repaint message
148 g_pToolWnd->Redraw ();
155 bool CControlPointsManagerBFace::OnLButtonUp (int x, int y)
157 if (ManagerState == Drag)
159 // this button is gonna become our Anchor
161 m_iAnchorPoint = m_iDragPoint;
162 // let's get out of Drag mode
164 // send a repaint message
165 g_pToolWnd->Redraw ();
171 void CControlPointsManagerBFace::render()
175 m_pQglTable->m_pfn_qglColor3f(0, 1, 0);
176 m_pQglTable->m_pfn_qglPointSize(6);
177 m_pQglTable->m_pfn_qglBegin( GL_POINTS );
178 for ( i=0; i<m_NumPoints; i++ )
180 if ( ManagerState == Drag && i == m_iDragPoint )
181 m_pQglTable->m_pfn_qglColor3f(1, 0, 0);
182 else if ( m_bGotAnchor && i == m_iAnchorPoint )
183 m_pQglTable->m_pfn_qglColor3f(0, 0, 1);
184 m_pQglTable->m_pfn_qglVertex2f( m_pPts->data[i][0], m_pPts->data[i][1] );
185 m_pQglTable->m_pfn_qglColor3f(0, 1, 0);
187 m_pQglTable->m_pfn_qglEnd();
190 void CControlPointsManagerBFace::UpdateCtrlPts()
195 for ( i=0; i<m_NumPoints; i++ )
197 m_pPts->data[i][0] = m_RefPts.data[i][0]*m_TM[0][0]+m_RefPts.data[i][1]*m_TM[0][1]+m_TM[0][2];
198 m_pPts->data[i][1] = m_RefPts.data[i][0]*m_TM[1][0]+m_RefPts.data[i][1]*m_TM[1][1]+m_TM[1][2];
201 if (g_bPrefsUpdateCameraView)
204 // tell Radiant to update
205 // NOTE: little speed optimisation, disable window updates, and only update camera view
206 g_FuncTable.m_pfnSetScreenUpdate( false );
207 g_SelectedFaceTable.m_pfnSetFaceInfo( 0, m_pFaceData );
208 g_FuncTable.m_pfnSetScreenUpdate( true );
209 g_FuncTable.m_pfnSysUpdateWindows( W_CAMERA );
213 //++timo FIXME: we are using a global for the reference data, use a m_pCancelFaceData instead
214 void CControlPointsManagerBFace::Commit( )
216 brushprimit_texdef_t aux;
217 aux.coords[0][0] = m_TM[0][0]*g_CancelFaceData.brushprimit_texdef.coords[0][0] + m_TM[0][1]*g_CancelFaceData.brushprimit_texdef.coords[1][0];
218 aux.coords[0][1] = m_TM[0][0]*g_CancelFaceData.brushprimit_texdef.coords[0][1] + m_TM[0][1]*g_CancelFaceData.brushprimit_texdef.coords[1][1];
219 aux.coords[0][2] = m_TM[0][0]*g_CancelFaceData.brushprimit_texdef.coords[0][2] + m_TM[0][1]*g_CancelFaceData.brushprimit_texdef.coords[1][2] + m_TM[0][2];
220 aux.coords[1][0] = m_TM[1][0]*g_CancelFaceData.brushprimit_texdef.coords[0][0] + m_TM[1][1]*g_CancelFaceData.brushprimit_texdef.coords[1][0];
221 aux.coords[1][1] = m_TM[1][0]*g_CancelFaceData.brushprimit_texdef.coords[0][1] + m_TM[1][1]*g_CancelFaceData.brushprimit_texdef.coords[1][1];
222 aux.coords[1][2] = m_TM[1][0]*g_CancelFaceData.brushprimit_texdef.coords[0][2] + m_TM[1][1]*g_CancelFaceData.brushprimit_texdef.coords[1][2] + m_TM[1][2];
223 memcpy( &m_pFaceData->brushprimit_texdef, &aux, sizeof(brushprimit_texdef_t) );
226 void CControlPointsManagerBFace::ComputeTransOffset(int i)
228 // compute the translation offset used to counteract rotation
229 m_TransOffset[0] = -m_TM[0][0]*m_RefPts.data[i][0] - m_TM[0][1]*m_RefPts.data[i][1];
230 m_TransOffset[1] = -m_TM[1][0]*m_RefPts.data[i][0] - m_TM[1][1]*m_RefPts.data[i][1];
233 void CControlPointsManagerBFace::XYSpaceForSTSpace( float xy[2], const float st[2] )
235 xy[0] = st[0] * (float)m_TexSize[0];
236 xy[1] = st[1] * (float)m_TexSize[1];
240 ======================================================================
242 ======================================================================
245 void CControlPointsManagerPatch::Init( patchMesh_t* pWorkPatch, C2DView *p2DView, OpenGLBinding *pQglTable, patchMesh_t* pPatch )
247 CControlPointsManager::Init( p2DView, pQglTable );
249 m_pWorkPatch = pWorkPatch;
252 bool CControlPointsManagerPatch::OnLButtonDown (int xPos, int yPos)
254 if (ManagerState == Idle)
258 // scan the point list to see if we selected something
259 for ( i=0; i<m_pPatch->width; i++ )
260 for ( j=0; j<m_pPatch->height; j++ )
261 if ( m_p2DView->DoesSelect( xPos, yPos, m_pWorkPatch->ctrl[i][j].st ) )
267 // send a repaint message
268 g_pToolWnd->Redraw ();
274 bool CControlPointsManagerPatch::OnMouseMove (int xPos, int yPos)
276 if (ManagerState == Drag)
278 m_p2DView->GridForWindow( m_pWorkPatch->ctrl[ m_iDragPoint[0] ][ m_iDragPoint[1] ].st, xPos, yPos );
279 if (g_bPrefsUpdateCameraView)
282 // ask to rebuild the patch display data
283 m_pPatch->bDirty = true;
284 // send a repaint to the camera window as well
285 g_FuncTable.m_pfnSysUpdateWindows( W_CAMERA );
287 // send a repaint message
288 g_pToolWnd->Redraw ();
294 bool CControlPointsManagerPatch::OnLButtonUp (int x, int y)
296 if (ManagerState == Drag)
299 // send a repaint message
300 g_pToolWnd->Redraw ();
305 void CControlPointsManagerPatch::render()
309 m_pQglTable->m_pfn_qglColor3f(0, 1, 0);
310 m_pQglTable->m_pfn_qglPointSize(6);
311 m_pQglTable->m_pfn_qglBegin( GL_POINTS );
312 for ( i=0; i<m_pPatch->width; i++ )
313 for ( j=0; j<m_pPatch->height; j++ )
315 if ( ManagerState == Drag && i == m_iDragPoint[0] && j == m_iDragPoint[1] )
316 m_pQglTable->m_pfn_qglColor3f(1, 0, 0);
317 m_pQglTable->m_pfn_qglVertex2f( m_pWorkPatch->ctrl[i][j].st[0], m_pWorkPatch->ctrl[i][j].st[1] );
318 m_pQglTable->m_pfn_qglColor3f(0, 1, 0);
320 m_pQglTable->m_pfn_qglEnd();
323 void CControlPointsManagerPatch::Commit()
326 for ( i=0; i<m_pPatch->width; i++ )
327 for ( j=0; j<m_pPatch->height; j++ )
329 m_pPatch->ctrl[i][j].st[0] = m_pWorkPatch->ctrl[i][j].st[0];
330 m_pPatch->ctrl[i][j].st[1] = m_pWorkPatch->ctrl[i][j].st[1];