ok
[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
22 #include "gtkr_list.h"
23 #include "str.h"
24
25 #include "DPoint.h"
26 #include "DPlane.h"
27 #include "DBrush.h"
28 #include "DEPair.h"
29 #include "DPatch.h"
30 #include "DEntity.h"
31 #include "DTrainDrawer.h"
32
33 #include "misc.h"
34 #include "funchandlers.h"
35
36 #include "dialogs/dialogs-gtk.h"
37
38 DTrainDrawer::DTrainDrawer() {
39         refCount = 1;
40         m_bHooked = FALSE;
41         m_bDisplay = FALSE;
42
43         BuildPaths();
44 }
45
46 DTrainDrawer::~DTrainDrawer(void) {
47         if(m_bHooked)
48                 UnRegister();
49
50         ClearPoints();
51         ClearSplines();
52 }
53
54 void DTrainDrawer::ClearSplines() {
55         for(list<splinePoint_t *>::const_iterator deadSpline = m_splineList.begin(); deadSpline != m_splineList.end(); deadSpline++) {
56                 (*deadSpline)->m_pointList.clear();
57                 (*deadSpline)->m_vertexList.clear();
58                 delete (*deadSpline);
59         }
60
61         m_splineList.clear();
62 }
63
64 void DTrainDrawer::ClearPoints() {
65         for(list<controlPoint_t *>::const_iterator deadPoint = m_pointList.begin(); deadPoint != m_pointList.end(); deadPoint++) {
66                 delete *deadPoint;
67         }
68
69         m_pointList.clear();
70 }
71
72 void DTrainDrawer::Register() {
73         __QGLTABLENAME.m_pfnHookGL2DWindow( this );
74         __QGLTABLENAME.m_pfnHookGL3DWindow( this );
75         m_bHooked = TRUE;
76 }
77
78 void DTrainDrawer::UnRegister() {
79         __QGLTABLENAME.m_pfnUnHookGL2DWindow( this );
80         __QGLTABLENAME.m_pfnUnHookGL3DWindow( this );
81         m_bHooked = FALSE;
82 }
83
84 void CalculateSpline_r(vec3_t* v, int count, vec3_t out, float tension) {
85         vec3_t dist;
86
87         if(count < 2) {
88                 return;
89         }
90
91         if(count == 2) {
92                 VectorSubtract( v[1], v[0], dist );
93                 VectorMA(v[0], tension, dist, out);
94                 return;
95         }
96
97         vec3_t* v2 = new vec3_t[count-1];
98
99         for( int i = 0; i < count-1; i++ ) {
100                 VectorSubtract( v[i+1], v[i], dist );
101                 VectorMA(v[i], tension, dist, v2[i]);
102         }
103
104         CalculateSpline_r( v2, count-1, out, tension);
105
106         delete[] v2;
107 }
108
109 void DTrainDrawer::Draw3D() {
110
111         if(!m_bDisplay) {
112                 return;
113         }
114
115         __QGLTABLENAME.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
116
117         __QGLTABLENAME.m_pfn_qglDisable(GL_BLEND);
118         __QGLTABLENAME.m_pfn_qglDisable(GL_LINE_SMOOTH);
119
120         __QGLTABLENAME.m_pfn_qglPushMatrix();
121         
122         __QGLTABLENAME.m_pfn_qglLineWidth(2.0f);
123         __QGLTABLENAME.m_pfn_qglColor4f(1.0f, 1.0f, 1.0f, 1.0f);
124
125         __QGLTABLENAME.m_pfn_qglEnable(GL_BLEND);
126         __QGLTABLENAME.m_pfn_qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
127         __QGLTABLENAME.m_pfn_qglDisable(GL_POLYGON_SMOOTH);
128
129         __QGLTABLENAME.m_pfn_qglDepthFunc(GL_ALWAYS);
130
131         for(list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
132                 splinePoint_t* pSP = (*sp);
133
134                 __QGLTABLENAME.m_pfn_qglBegin(GL_LINE_STRIP);
135                         for(list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++) {
136                                 __QGLTABLENAME.m_pfn_qglVertex3fv((*v)._pnt);
137                         }
138                 __QGLTABLENAME.m_pfn_qglEnd();
139
140         }
141
142         __QGLTABLENAME.m_pfn_qglPopMatrix();
143         __QGLTABLENAME.m_pfn_qglPopAttrib();
144 }
145
146 void DTrainDrawer::Draw2D(VIEWTYPE vt) {
147
148         if(!m_bDisplay) {
149                 return;
150         }
151
152         __QGLTABLENAME.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
153
154         __QGLTABLENAME.m_pfn_qglDisable(GL_BLEND);
155         __QGLTABLENAME.m_pfn_qglDisable(GL_LINE_SMOOTH);
156
157         __QGLTABLENAME.m_pfn_qglPushMatrix();
158         
159         switch(vt)
160         {
161         case XY:
162                 break;
163         case XZ:
164                 __QGLTABLENAME.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
165                 break;
166         case YZ:
167                 __QGLTABLENAME.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
168                 __QGLTABLENAME.m_pfn_qglRotatef(270.0f, 0.0f, 0.0f, 1.0f);
169                 break;
170         }
171
172         __QGLTABLENAME.m_pfn_qglLineWidth(1.0f);
173         __QGLTABLENAME.m_pfn_qglColor4f(1.0f, 0.0f, 0.0f, 0.5f);
174
175         __QGLTABLENAME.m_pfn_qglEnable(GL_BLEND);
176         __QGLTABLENAME.m_pfn_qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
177         __QGLTABLENAME.m_pfn_qglDisable(GL_POLYGON_SMOOTH);
178
179         __QGLTABLENAME.m_pfn_qglDepthFunc(GL_ALWAYS);
180
181         __QGLTABLENAME.m_pfn_qglColor4f(1.f, 0.f, 0.f, 1.f);
182
183         for(list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
184                 splinePoint_t* pSP = (*sp);
185
186                 __QGLTABLENAME.m_pfn_qglBegin(GL_LINE_STRIP);
187                         for(list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++) {
188                                 __QGLTABLENAME.m_pfn_qglVertex3fv((*v)._pnt);
189                         }
190                 __QGLTABLENAME.m_pfn_qglEnd();
191
192         }
193
194         __QGLTABLENAME.m_pfn_qglPopMatrix();
195         __QGLTABLENAME.m_pfn_qglPopAttrib();
196 }
197
198 void AddSplineControl(const char* control, splinePoint_t* pSP) {
199         controlPoint_t cp;
200         strncpy(cp.strName,     control, 64);
201
202         pSP->m_pointList.push_front(cp);
203 }
204
205 void DTrainDrawer::BuildPaths() {
206 #if 0
207         int count = g_FuncTable.m_pfnGetEntityCount();
208
209         DEntity e;
210
211         for(int i = 0; i < count; i++) {
212                 entity_s* ent = (entity_s*)g_FuncTable.m_pfnGetEntityHandle(i);
213                 e.ClearEPairs();
214                 e.LoadEPairList(*g_EntityTable.m_pfnGetEntityKeyValList(ent));
215
216                 const char* classname = e.m_Classname.GetBuffer();
217                 const char* target;
218                 const char* control;
219                 const char* targetname;
220                 vec3_t          vOrigin;
221
222                 e.SpawnString("targetname", NULL, &targetname);
223                 e.SpawnVector("origin", "0 0 0", vOrigin);
224
225                 if(!strcmp(classname, "info_train_spline_main")) {
226                         if(!targetname) {
227                                 Sys_Printf( "info_train_spline_main with no targetname" );
228                                 return;
229                         }
230
231                         e.SpawnString("target", NULL, &target);
232
233                         if(!target) {
234                                 AddControlPoint( targetname, vOrigin );
235                         } else {
236                                 splinePoint_t* pSP = AddSplinePoint( targetname, target, vOrigin );
237
238                                 e.SpawnString("control", NULL, &control);
239
240                                 if(control) {
241                                         AddSplineControl( control, pSP );
242
243                                         for(int j = 2;; j++) {
244                                                 char buffer[16];
245                                                 sprintf(buffer, "control%i", j);
246
247                                                 e.SpawnString(buffer, NULL, &control);
248                                                 if(!control) {
249                                                         break;
250                                                 }
251                                         
252                                                 AddSplineControl( control, pSP );
253                                         }
254                                 }
255                         }
256                 } else if(!strcmp(classname, "info_train_spline_control")) {
257                         if(!targetname) {
258                                 Sys_Printf( "info_train_spline_control with no targetname" );
259                                 return;
260                         }
261
262                         AddControlPoint( targetname, vOrigin );
263                 }
264         }
265
266   list<splinePoint_t* >::const_iterator sp;
267         for(sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
268                 splinePoint_t* pSP = (*sp);
269
270                 controlPoint_t* pTarget = FindControlPoint( pSP->strTarget );
271
272                 if(!pTarget) {
273                         Sys_Printf( "couldn't find target %s", pSP->strTarget );
274                         return;
275 //                      continue;
276                 }
277
278                 pSP->pTarget = pTarget;
279
280
281                 for(list<controlPoint_t >::iterator cp = pSP->m_pointList.begin(); cp != pSP->m_pointList.end(); cp++) {                        
282                         controlPoint_t* pControl = FindControlPoint( (*cp).strName );
283                         if(!pControl) {
284                                 Sys_Printf( "couldn't find control %s", (*cp).strName );
285                                 return;
286                         }
287
288                         VectorCopy(pControl->vOrigin, (*cp).vOrigin);
289                 }
290         }
291
292         m_bDisplay = TRUE;
293         Register();
294
295         for(sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
296                 splinePoint_t* pSP = (*sp);
297                 DPoint out;
298
299                 if(!pSP->pTarget) {
300                         continue;
301                 }
302
303                 int count = pSP->m_pointList.size() + 2;
304                 vec3_t* v = new vec3_t[count];
305
306                 VectorCopy(pSP->point.vOrigin, v[0]);
307
308                 int i = 1;
309                 for(list<controlPoint_t>::reverse_iterator cp = pSP->m_pointList.rbegin(); cp != pSP->m_pointList.rend(); cp++) {
310                         VectorCopy((*cp).vOrigin, v[i]);
311                         i++;
312                 }
313                 VectorCopy(pSP->pTarget->vOrigin, v[i]);
314
315                 for (float tension = 0.0f; tension <= 1.f; tension += 0.01f) {
316                         CalculateSpline_r(v, count, out._pnt, tension);
317                         pSP->m_vertexList.push_front(out);
318                 }
319
320                 delete[] v;
321
322                 VectorCopy(pSP->pTarget->vOrigin, out._pnt);
323                 pSP->m_vertexList.push_front(out);
324         }
325
326 #endif
327 }
328
329 void DTrainDrawer::AddControlPoint(const char* name, vec_t* origin)
330 {
331         controlPoint_t* pCP = new controlPoint_t;
332
333         strncpy(pCP->strName, name, 64);
334         VectorCopy( origin, pCP->vOrigin );
335
336         m_pointList.push_back( pCP );
337 }
338
339 splinePoint_t* DTrainDrawer::AddSplinePoint(const char* name, const char* target, vec_t* origin)
340 {
341         splinePoint_t* pSP = new splinePoint_t;
342
343         strncpy(pSP->point.strName, name,               64);
344         strncpy(pSP->strTarget,         target,         64);
345         VectorCopy( origin, pSP->point.vOrigin );
346         m_splineList.push_back( pSP );
347
348         return pSP;
349 }
350
351 controlPoint_t* DTrainDrawer::FindControlPoint(const char* name)
352 {
353         for(list<controlPoint_t*>::const_iterator cp = m_pointList.begin(); cp != m_pointList.end(); cp++) {
354                 if(!strcmp(name, (*cp)->strName)) {
355                         return (*cp);
356                 }
357         }
358
359         for(list<splinePoint_t*>::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
360                 if(!strcmp(name, (*sp)->point.strName)) {
361                         return &((*sp)->point);
362                 }
363         }
364
365         return NULL;
366 }