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