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