]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DTrainDrawer.cpp
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / contrib / bobtoolz / DTrainDrawer.cpp
1 /*
2    BobToolz plugin for GtkRadiant
3    Copyright (C) 2001 Gordon Biggans
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include "StdAfx.h"
21 #include "DPoint.h"
22
23 #include "DTrainDrawer.h"
24 #include "DEPair.h"
25
26 #include "misc.h"
27 #include "funchandlers.h"
28
29 #include "dialogs/dialogs-gtk.h"
30
31 DTrainDrawer::DTrainDrawer() {
32         refCount = 1;
33         m_bHooked = FALSE;
34         m_bDisplay = FALSE;
35
36         BuildPaths();
37 }
38
39 DTrainDrawer::~DTrainDrawer( void ) {
40         if ( m_bHooked ) {
41                 UnRegister();
42         }
43
44         ClearPoints();
45         ClearSplines();
46 }
47
48 void DTrainDrawer::ClearSplines() {
49         for ( list<splinePoint_t *>::const_iterator deadSpline = m_splineList.begin(); deadSpline != m_splineList.end(); deadSpline++ ) {
50                 ( *deadSpline )->m_pointList.clear();
51                 ( *deadSpline )->m_vertexList.clear();
52                 delete ( *deadSpline );
53         }
54
55         m_splineList.clear();
56 }
57
58 void DTrainDrawer::ClearPoints() {
59         for ( list<controlPoint_t *>::const_iterator deadPoint = m_pointList.begin(); deadPoint != m_pointList.end(); deadPoint++ ) {
60                 delete *deadPoint;
61         }
62
63         m_pointList.clear();
64 }
65
66 void DTrainDrawer::Register() {
67         g_QglTable.m_pfnHookGL2DWindow( this );
68         g_QglTable.m_pfnHookGL3DWindow( this );
69         m_bHooked = TRUE;
70 }
71
72 void DTrainDrawer::UnRegister() {
73         g_QglTable.m_pfnUnHookGL2DWindow( this );
74         g_QglTable.m_pfnUnHookGL3DWindow( this );
75         m_bHooked = FALSE;
76 }
77
78 void CalculateSpline_r( vec3_t* v, int count, vec3_t out, float tension ) {
79         vec3_t dist;
80
81         if ( count < 2 ) {
82                 return;
83         }
84
85         if ( count == 2 ) {
86                 VectorSubtract( v[1], v[0], dist );
87                 VectorMA( v[0], tension, dist, out );
88                 return;
89         }
90
91         vec3_t* v2 = new vec3_t[count - 1];
92
93         for ( int i = 0; i < count - 1; i++ ) {
94                 VectorSubtract( v[i + 1], v[i], dist );
95                 VectorMA( v[i], tension, dist, v2[i] );
96         }
97
98         CalculateSpline_r( v2, count - 1, out, tension );
99
100         delete[] v2;
101 }
102
103 void DTrainDrawer::Draw3D() {
104
105         if ( !m_bDisplay ) {
106                 return;
107         }
108
109         g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS );
110
111         g_QglTable.m_pfn_qglDisable( GL_BLEND );
112         g_QglTable.m_pfn_qglDisable( GL_LINE_SMOOTH );
113
114         g_QglTable.m_pfn_qglPushMatrix();
115
116         g_QglTable.m_pfn_qglLineWidth( 2.0f );
117         g_QglTable.m_pfn_qglColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
118
119         g_QglTable.m_pfn_qglEnable( GL_BLEND );
120         g_QglTable.m_pfn_qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
121         g_QglTable.m_pfn_qglDisable( GL_POLYGON_SMOOTH );
122
123         g_QglTable.m_pfn_qglDepthFunc( GL_ALWAYS );
124
125         for ( list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
126                 splinePoint_t* pSP = ( *sp );
127
128                 g_QglTable.m_pfn_qglBegin( GL_LINE_STRIP );
129                 for ( list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++ ) {
130                         g_QglTable.m_pfn_qglVertex3fv( ( *v )._pnt );
131                 }
132                 g_QglTable.m_pfn_qglEnd();
133
134         }
135
136         g_QglTable.m_pfn_qglPopMatrix();
137         g_QglTable.m_pfn_qglPopAttrib();
138 }
139
140 void DTrainDrawer::Draw2D( VIEWTYPE vt ) {
141
142         if ( !m_bDisplay ) {
143                 return;
144         }
145
146         g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS );
147
148         g_QglTable.m_pfn_qglDisable( GL_BLEND );
149         g_QglTable.m_pfn_qglDisable( GL_LINE_SMOOTH );
150
151         g_QglTable.m_pfn_qglPushMatrix();
152
153         switch ( vt )
154         {
155         case XY:
156                 break;
157         case XZ:
158                 g_QglTable.m_pfn_qglRotatef( 270.0f, 1.0f, 0.0f, 0.0f );
159                 break;
160         case YZ:
161                 g_QglTable.m_pfn_qglRotatef( 270.0f, 1.0f, 0.0f, 0.0f );
162                 g_QglTable.m_pfn_qglRotatef( 270.0f, 0.0f, 0.0f, 1.0f );
163                 break;
164         }
165
166         g_QglTable.m_pfn_qglLineWidth( 1.0f );
167         g_QglTable.m_pfn_qglColor4f( 1.0f, 0.0f, 0.0f, 0.5f );
168
169         g_QglTable.m_pfn_qglEnable( GL_BLEND );
170         g_QglTable.m_pfn_qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
171         g_QglTable.m_pfn_qglDisable( GL_POLYGON_SMOOTH );
172
173         g_QglTable.m_pfn_qglDepthFunc( GL_ALWAYS );
174
175         g_QglTable.m_pfn_qglColor4f( 1.f, 0.f, 0.f, 1.f );
176
177         for ( list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
178                 splinePoint_t* pSP = ( *sp );
179
180                 g_QglTable.m_pfn_qglBegin( GL_LINE_STRIP );
181                 for ( list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++ ) {
182                         g_QglTable.m_pfn_qglVertex3fv( ( *v )._pnt );
183                 }
184                 g_QglTable.m_pfn_qglEnd();
185
186         }
187
188         g_QglTable.m_pfn_qglPopMatrix();
189         g_QglTable.m_pfn_qglPopAttrib();
190 }
191
192 void AddSplineControl( const char* control, splinePoint_t* pSP ) {
193         controlPoint_t cp;
194         strncpy( cp.strName, control, 64 );
195
196         pSP->m_pointList.push_front( cp );
197 }
198
199 void DTrainDrawer::BuildPaths() {
200         int count = g_FuncTable.m_pfnGetEntityCount();
201
202         DEntity e;
203
204         for ( int i = 0; i < count; i++ ) {
205                 entity_s* ent = (entity_s*)g_FuncTable.m_pfnGetEntityHandle( i );
206                 e.ClearEPairs();
207                 e.LoadEPairList( *g_EntityTable.m_pfnGetEntityKeyValList( ent ) );
208
209                 const char* classname = e.m_Classname.GetBuffer();
210                 const char* target;
211                 const char* control;
212                 const char* targetname;
213                 vec3_t vOrigin;
214
215                 e.SpawnString( "targetname", NULL, &targetname );
216                 e.SpawnVector( "origin", "0 0 0", vOrigin );
217
218                 if ( !strcmp( classname, "info_train_spline_main" ) ) {
219                         if ( !targetname ) {
220                                 Sys_Printf( "info_train_spline_main with no targetname" );
221                                 return;
222                         }
223
224                         e.SpawnString( "target", NULL, &target );
225
226                         if ( !target ) {
227                                 AddControlPoint( targetname, vOrigin );
228                         }
229                         else {
230                                 splinePoint_t* pSP = AddSplinePoint( targetname, target, vOrigin );
231
232                                 e.SpawnString( "control", NULL, &control );
233
234                                 if ( control ) {
235                                         AddSplineControl( control, pSP );
236
237                                         for ( int j = 2;; j++ ) {
238                                                 char buffer[16];
239                                                 sprintf( buffer, "control%i", j );
240
241                                                 e.SpawnString( buffer, NULL, &control );
242                                                 if ( !control ) {
243                                                         break;
244                                                 }
245
246                                                 AddSplineControl( control, pSP );
247                                         }
248                                 }
249                         }
250                 }
251                 else if ( !strcmp( classname, "info_train_spline_control" ) ) {
252                         if ( !targetname ) {
253                                 Sys_Printf( "info_train_spline_control with no targetname" );
254                                 return;
255                         }
256
257                         AddControlPoint( targetname, vOrigin );
258                 }
259         }
260
261         list<splinePoint_t* >::const_iterator sp;
262         for ( sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
263                 splinePoint_t* pSP = ( *sp );
264
265                 controlPoint_t* pTarget = FindControlPoint( pSP->strTarget );
266
267                 if ( !pTarget ) {
268                         Sys_Printf( "couldn't find target %s", pSP->strTarget );
269                         return;
270 //                      continue;
271                 }
272
273                 pSP->pTarget = pTarget;
274
275
276                 for ( list<controlPoint_t >::iterator cp = pSP->m_pointList.begin(); cp != pSP->m_pointList.end(); cp++ ) {
277                         controlPoint_t* pControl = FindControlPoint( ( *cp ).strName );
278                         if ( !pControl ) {
279                                 Sys_Printf( "couldn't find control %s", ( *cp ).strName );
280                                 return;
281                         }
282
283                         VectorCopy( pControl->vOrigin, ( *cp ).vOrigin );
284                 }
285         }
286
287         m_bDisplay = TRUE;
288         Register();
289
290         for ( sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
291                 splinePoint_t* pSP = ( *sp );
292                 DPoint out;
293
294                 if ( !pSP->pTarget ) {
295                         continue;
296                 }
297
298                 int count = pSP->m_pointList.size() + 2;
299                 vec3_t* v = new vec3_t[count];
300
301                 VectorCopy( pSP->point.vOrigin, v[0] );
302
303                 int i = 1;
304                 for ( list<controlPoint_t>::reverse_iterator cp = pSP->m_pointList.rbegin(); cp != pSP->m_pointList.rend(); cp++ ) {
305                         VectorCopy( ( *cp ).vOrigin, v[i] );
306                         i++;
307                 }
308                 VectorCopy( pSP->pTarget->vOrigin, v[i] );
309
310                 for ( float tension = 0.0f; tension <= 1.f; tension += 0.01f ) {
311                         CalculateSpline_r( v, count, out._pnt, tension );
312                         pSP->m_vertexList.push_front( out );
313                 }
314
315                 delete[] v;
316
317                 VectorCopy( pSP->pTarget->vOrigin, out._pnt );
318                 pSP->m_vertexList.push_front( out );
319         }
320
321
322 }
323
324 void DTrainDrawer::AddControlPoint( const char* name, vec_t* origin ){
325         controlPoint_t* pCP = new controlPoint_t;
326
327         strncpy( pCP->strName, name, 64 );
328         VectorCopy( origin, pCP->vOrigin );
329
330         m_pointList.push_back( pCP );
331 }
332
333 splinePoint_t* DTrainDrawer::AddSplinePoint( const char* name, const char* target, vec_t* origin ){
334         splinePoint_t* pSP = new splinePoint_t;
335
336         strncpy( pSP->point.strName, name,       64 );
337         strncpy( pSP->strTarget,     target,     64 );
338         VectorCopy( origin, pSP->point.vOrigin );
339         m_splineList.push_back( pSP );
340
341         return pSP;
342 }
343
344 controlPoint_t* DTrainDrawer::FindControlPoint( const char* name ){
345         for ( list<controlPoint_t*>::const_iterator cp = m_pointList.begin(); cp != m_pointList.end(); cp++ ) {
346                 if ( !strcmp( name, ( *cp )->strName ) ) {
347                         return ( *cp );
348                 }
349         }
350
351         for ( list<splinePoint_t*>::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
352                 if ( !strcmp( name, ( *sp )->point.strName ) ) {
353                         return &( ( *sp )->point );
354                 }
355         }
356
357         return NULL;
358 }