]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DTrainDrawer.cpp
38abb659c0ada0912335d009de7b13bc183dc440
[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 "DTrainDrawer.h"
21
22 #include <list>
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
32 #include "misc.h"
33 #include "funchandlers.h"
34
35 #include "iglrender.h"
36 #include "math/matrix.h"
37
38 #include "dialogs/dialogs-gtk.h"
39
40 DTrainDrawer::DTrainDrawer() {
41         m_bDisplay = false;
42
43         BuildPaths();
44   constructShaders();
45   GlobalShaderCache().attachRenderable(*this);
46 }
47
48 DTrainDrawer::~DTrainDrawer(void) {
49   GlobalShaderCache().detachRenderable(*this);
50   destroyShaders();
51
52         ClearPoints();
53         ClearSplines();
54 }
55
56 void DTrainDrawer::ClearSplines() {
57         for(std::list<splinePoint_t *>::const_iterator deadSpline = m_splineList.begin(); deadSpline != m_splineList.end(); deadSpline++) {
58                 (*deadSpline)->m_pointList.clear();
59                 (*deadSpline)->m_vertexList.clear();
60                 delete (*deadSpline);
61         }
62
63         m_splineList.clear();
64 }
65
66 void DTrainDrawer::ClearPoints() {
67         for(std::list<controlPoint_t *>::const_iterator deadPoint = m_pointList.begin(); deadPoint != m_pointList.end(); deadPoint++) {
68                 delete *deadPoint;
69         }
70
71         m_pointList.clear();
72 }
73
74 void CalculateSpline_r(vec3_t* v, int count, vec3_t out, float tension) {
75         vec3_t dist;
76
77         if(count < 2) {
78                 return;
79         }
80
81         if(count == 2) {
82                 VectorSubtract( v[1], v[0], dist );
83                 VectorMA(v[0], tension, dist, out);
84                 return;
85         }
86
87         vec3_t* v2 = new vec3_t[count-1];
88
89         for( int i = 0; i < count-1; i++ ) {
90                 VectorSubtract( v[i+1], v[i], dist );
91                 VectorMA(v[i], tension, dist, v2[i]);
92         }
93
94         CalculateSpline_r( v2, count-1, out, tension);
95
96         delete[] v2;
97 }
98
99 void DTrainDrawer::render(RenderStateFlags state) const
100 {
101         for(std::list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
102                 splinePoint_t* pSP = (*sp);
103
104                 glBegin(GL_LINE_STRIP);
105                         for(std::list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++) {
106                                 glVertex3fv((*v)._pnt);
107                         }
108                 glEnd();
109
110         }
111 }
112
113 const char* DTrainDrawer_state_wireframe = "$bobtoolz/traindrawer/wireframe";
114 const char* DTrainDrawer_state_solid = "$bobtoolz/traindrawer/solid";
115
116 void DTrainDrawer::constructShaders()
117 {
118   OpenGLState state;
119   GlobalOpenGLStateLibrary().getDefaultState(state);
120   state.m_state = RENDER_COLOURWRITE|RENDER_DEPTHWRITE|RENDER_BLEND;
121   state.m_sort = OpenGLState::eSortOverlayFirst;
122   state.m_linewidth = 1;
123   state.m_colour[0] = 1;
124   state.m_colour[1] = 0;
125   state.m_colour[2] = 0;
126   state.m_colour[3] = 1;
127   state.m_linewidth = 1;
128   GlobalOpenGLStateLibrary().insert(DTrainDrawer_state_wireframe, state);
129
130   state.m_colour[0] = 1;
131   state.m_colour[1] = 1;
132   state.m_colour[2] = 1;
133   state.m_colour[3] = 1;
134   state.m_linewidth = 2;
135   GlobalOpenGLStateLibrary().insert(DTrainDrawer_state_solid, state);
136
137   m_shader_wireframe = GlobalShaderCache().capture(DTrainDrawer_state_wireframe);
138   m_shader_solid = GlobalShaderCache().capture(DTrainDrawer_state_solid);
139 }
140
141 void DTrainDrawer::destroyShaders()
142 {
143   GlobalOpenGLStateLibrary().erase(DTrainDrawer_state_wireframe);
144   GlobalOpenGLStateLibrary().erase(DTrainDrawer_state_solid);
145   GlobalShaderCache().release(DTrainDrawer_state_wireframe);
146   GlobalShaderCache().release(DTrainDrawer_state_solid);
147 }
148
149
150 void DTrainDrawer::renderSolid(Renderer& renderer, const VolumeTest& volume) const
151 {
152         if(!m_bDisplay) {
153                 return;
154         }
155
156   renderer.SetState(m_shader_wireframe, Renderer::eWireframeOnly);
157   renderer.SetState(m_shader_solid, Renderer::eFullMaterials);
158   renderer.addRenderable(*this, g_matrix4_identity);
159 }
160 void DTrainDrawer::renderWireframe(Renderer& renderer, const VolumeTest& volume) const
161 {
162   renderSolid(renderer, volume);
163 }
164
165 void AddSplineControl(const char* control, splinePoint_t* pSP) {
166         controlPoint_t cp;
167         strncpy(cp.strName,     control, 64);
168
169         pSP->m_pointList.push_front(cp);
170 }
171
172 void DTrainDrawer::BuildPaths() {
173 #if 0
174         int count = g_FuncTable.m_pfnGetEntityCount();
175
176         DEntity e;
177
178         for(int i = 0; i < count; i++) {
179                 entity_s* ent = (entity_s*)g_FuncTable.m_pfnGetEntityHandle(i);
180                 e.ClearEPairs();
181                 e.LoadEPairList(*g_EntityTable.m_pfnGetEntityKeyValList(ent));
182
183                 const char* classname = e.m_Classname.GetBuffer();
184                 const char* target;
185                 const char* control;
186                 const char* targetname;
187                 vec3_t          vOrigin;
188
189                 e.SpawnString("targetname", NULL, &targetname);
190                 e.SpawnVector("origin", "0 0 0", vOrigin);
191
192                 if(!strcmp(classname, "info_train_spline_main")) {
193                         if(!targetname) {
194                                 Sys_Printf( "info_train_spline_main with no targetname" );
195                                 return;
196                         }
197
198                         e.SpawnString("target", NULL, &target);
199
200                         if(!target) {
201                                 AddControlPoint( targetname, vOrigin );
202                         } else {
203                                 splinePoint_t* pSP = AddSplinePoint( targetname, target, vOrigin );
204
205                                 e.SpawnString("control", NULL, &control);
206
207                                 if(control) {
208                                         AddSplineControl( control, pSP );
209
210                                         for(int j = 2;; j++) {
211                                                 char buffer[16];
212                                                 sprintf(buffer, "control%i", j);
213
214                                                 e.SpawnString(buffer, NULL, &control);
215                                                 if(!control) {
216                                                         break;
217                                                 }
218                                         
219                                                 AddSplineControl( control, pSP );
220                                         }
221                                 }
222                         }
223                 } else if(!strcmp(classname, "info_train_spline_control")) {
224                         if(!targetname) {
225                                 Sys_Printf( "info_train_spline_control with no targetname" );
226                                 return;
227                         }
228
229                         AddControlPoint( targetname, vOrigin );
230                 }
231         }
232
233   std::list<splinePoint_t* >::const_iterator sp;
234         for(sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
235                 splinePoint_t* pSP = (*sp);
236
237                 controlPoint_t* pTarget = FindControlPoint( pSP->strTarget );
238
239                 if(!pTarget) {
240                         Sys_Printf( "couldn't find target %s", pSP->strTarget );
241                         return;
242 //                      continue;
243                 }
244
245                 pSP->pTarget = pTarget;
246
247
248                 for(std::list<controlPoint_t >::iterator cp = pSP->m_pointList.begin(); cp != pSP->m_pointList.end(); cp++) {                   
249                         controlPoint_t* pControl = FindControlPoint( (*cp).strName );
250                         if(!pControl) {
251                                 Sys_Printf( "couldn't find control %s", (*cp).strName );
252                                 return;
253                         }
254
255                         VectorCopy(pControl->vOrigin, (*cp).vOrigin);
256                 }
257         }
258
259         m_bDisplay = TRUE;
260         Register();
261
262         for(sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
263                 splinePoint_t* pSP = (*sp);
264                 DPoint out;
265
266                 if(!pSP->pTarget) {
267                         continue;
268                 }
269
270                 int count = pSP->m_pointList.size() + 2;
271                 vec3_t* v = new vec3_t[count];
272
273                 VectorCopy(pSP->point.vOrigin, v[0]);
274
275                 int i = 1;
276                 for(std::list<controlPoint_t>::reverse_iterator cp = pSP->m_pointList.rbegin(); cp != pSP->m_pointList.rend(); cp++) {
277                         VectorCopy((*cp).vOrigin, v[i]);
278                         i++;
279                 }
280                 VectorCopy(pSP->pTarget->vOrigin, v[i]);
281
282                 for (float tension = 0.0f; tension <= 1.f; tension += 0.01f) {
283                         CalculateSpline_r(v, count, out._pnt, tension);
284                         pSP->m_vertexList.push_front(out);
285                 }
286
287                 delete[] v;
288
289                 VectorCopy(pSP->pTarget->vOrigin, out._pnt);
290                 pSP->m_vertexList.push_front(out);
291         }
292
293 #endif
294 }
295
296 void DTrainDrawer::AddControlPoint(const char* name, vec_t* origin)
297 {
298         controlPoint_t* pCP = new controlPoint_t;
299
300         strncpy(pCP->strName, name, 64);
301         VectorCopy( origin, pCP->vOrigin );
302
303         m_pointList.push_back( pCP );
304 }
305
306 splinePoint_t* DTrainDrawer::AddSplinePoint(const char* name, const char* target, vec_t* origin)
307 {
308         splinePoint_t* pSP = new splinePoint_t;
309
310         strncpy(pSP->point.strName, name,               64);
311         strncpy(pSP->strTarget,         target,         64);
312         VectorCopy( origin, pSP->point.vOrigin );
313         m_splineList.push_back( pSP );
314
315         return pSP;
316 }
317
318 controlPoint_t* DTrainDrawer::FindControlPoint(const char* name)
319 {
320         for(std::list<controlPoint_t*>::const_iterator cp = m_pointList.begin(); cp != m_pointList.end(); cp++) {
321                 if(!strcmp(name, (*cp)->strName)) {
322                         return (*cp);
323                 }
324         }
325
326         for(std::list<splinePoint_t*>::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
327                 if(!strcmp(name, (*sp)->point.strName)) {
328                         return &((*sp)->point);
329                 }
330         }
331
332         return NULL;
333 }