]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DBobView.cpp
f4d36de432c37878af681e78e53a09158c8331ad
[xonotic/netradiant.git] / contrib / bobtoolz / DBobView.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 // BobView.cpp: implementation of the DBobView class.
21 //
22 //////////////////////////////////////////////////////////////////////
23
24 #include "StdAfx.h"
25 #include "DBobView.h"
26 #include "DListener.h"
27 //#include "misc.h"
28 #include "funchandlers.h"
29
30 #include "gtkr_list.h"
31 #include "str.h"
32
33 #include "misc.h"
34
35 //////////////////////////////////////////////////////////////////////
36 // Construction/Destruction
37 //////////////////////////////////////////////////////////////////////
38
39 DBobView::DBobView()
40 {
41         nPathCount = 0;
42         refCount = 1;
43         
44         m_bHooked = FALSE;
45
46         path = NULL;
47         eyes = NULL;
48
49         boundingShow = BOUNDS_APEX;
50 }
51
52 DBobView::~DBobView()
53 {
54         if(path)
55                 delete[] path;
56
57         // oops forgot to remove our eyes, was causing access violation when it tried
58         // to talk to it's parent
59         if(eyes)
60                 delete eyes;
61
62         if(m_bHooked)
63                 UnRegister();
64
65         g_PathView = NULL;
66 }
67
68 //////////////////////////////////////////////////////////////////////
69 // Implementation
70 //////////////////////////////////////////////////////////////////////
71
72 void DBobView::Draw2D(VIEWTYPE vt)
73 {
74         if(!path)
75                 return;
76
77         __QGLTABLENAME.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
78
79         __QGLTABLENAME.m_pfn_qglDisable(GL_BLEND);
80         __QGLTABLENAME.m_pfn_qglEnable(GL_LINE_SMOOTH);
81
82         __QGLTABLENAME.m_pfn_qglPushMatrix();
83         
84         switch(vt)
85         {
86         case XY:
87                 break;
88         case XZ:
89                 __QGLTABLENAME.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
90                 break;
91         case YZ:
92                 __QGLTABLENAME.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
93                 __QGLTABLENAME.m_pfn_qglRotatef(270.0f, 0.0f, 0.0f, 1.0f);
94                 break;
95         }
96
97         __QGLTABLENAME.m_pfn_qglLineWidth(1.0f);
98         __QGLTABLENAME.m_pfn_qglColor4f(1.0f, 0.0f, 0.0f, 1.0f);
99
100         int i;
101
102         __QGLTABLENAME.m_pfn_qglBegin(GL_LINE_STRIP);
103
104         for(i = 0; i < nPathCount; i++)
105                 __QGLTABLENAME.m_pfn_qglVertex3fv(path[i]);
106
107         __QGLTABLENAME.m_pfn_qglEnd();
108
109         if(m_bShowExtra)
110         {
111                 // +mars
112                 // for the bounding box stuff
113                 __QGLTABLENAME.m_pfn_qglColor4f(0.25f, 0.75f, 0.75f, 1.0f);
114
115                 __QGLTABLENAME.m_pfn_qglTranslatef( 16.0f, 16.0f, 28.0f );
116
117                 __QGLTABLENAME.m_pfn_qglBegin( GL_LINE_STRIP );
118         
119                         for ( i = 0; i < nPathCount; i++ )
120                                 __QGLTABLENAME.m_pfn_qglVertex3fv( path[i] );
121
122                 __QGLTABLENAME.m_pfn_qglEnd();
123         
124         // ---------------
125
126                 __QGLTABLENAME.m_pfn_qglTranslatef( -16.0f, -16.0f, -28.0f );   // back to where we were
127                 __QGLTABLENAME.m_pfn_qglTranslatef( -16.0f, 16.0f, 28.0f );             // move to new postion
128         
129                 __QGLTABLENAME.m_pfn_qglBegin( GL_LINE_STRIP );
130
131                         for ( i = 0; i < nPathCount; i++ )
132                                 __QGLTABLENAME.m_pfn_qglVertex3fv( path[i] );
133
134                 __QGLTABLENAME.m_pfn_qglEnd();
135
136         // --------------
137
138                 __QGLTABLENAME.m_pfn_qglTranslatef( 16.0f, -16.0f, -28.0f );            // back to where we were
139                 __QGLTABLENAME.m_pfn_qglTranslatef( 16.0f, -16.0f, -28.0f );            // new pos
140
141                 __QGLTABLENAME.m_pfn_qglBegin( GL_LINE_STRIP );
142                 
143                         for ( i = 0; i < nPathCount; i++ )
144                                 __QGLTABLENAME.m_pfn_qglVertex3fv( path[i] );
145
146                 __QGLTABLENAME.m_pfn_qglEnd();
147
148         // ----------------
149
150                 __QGLTABLENAME.m_pfn_qglTranslatef( -16.0f, 16.0f, 28.0f );             // back to where we were
151
152 /*              __QGLTABLENAME.m_pfn_qglTranslatef( -16.0f, -16.0f, -28.0f );           // new pos
153
154                 __QGLTABLENAME.m_pfn_qglBegin( GL_LINE_STRIP );
155                 
156                 if ( boundingShow == BOUNDS_ALL )
157                 {
158                         for ( i = 0; i < nPathCount; i++ )
159                                 __QGLTABLENAME.m_pfn_qglVertex3fv( path[i] );
160                 }
161                 else if ( boundingShow == BOUNDS_APEX )
162                 {
163                         for ( i = (nPathCount/4); i < (nPathCount/4) * 3; i++ )
164                                 __QGLTABLENAME.m_pfn_qglVertex3fv( path[i] );
165                 }
166
167                 __QGLTABLENAME.m_pfn_qglEnd();*/ // djbob: er, um doesn't really seem to do anyhting
168         }
169
170         // -mars
171         
172         __QGLTABLENAME.m_pfn_qglPopMatrix();
173
174         __QGLTABLENAME.m_pfn_qglPopAttrib();
175 }
176
177 void DBobView::Draw3D()
178 {
179         if(!path)
180                 return;
181
182         __QGLTABLENAME.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
183
184         __QGLTABLENAME.m_pfn_qglDisable(GL_BLEND);
185         __QGLTABLENAME.m_pfn_qglEnable(GL_LINE_SMOOTH);
186
187         __QGLTABLENAME.m_pfn_qglLineWidth(1.0f);
188         __QGLTABLENAME.m_pfn_qglColor4f(1.0f, 0.0f, 0.0f, 1.0f);
189
190         __QGLTABLENAME.m_pfn_qglBegin(GL_LINE_STRIP);
191
192         for(int i = 0; i < nPathCount; i++)
193                 __QGLTABLENAME.m_pfn_qglVertex3fv(path[i]);
194
195         __QGLTABLENAME.m_pfn_qglEnd();
196
197         if(m_bShowExtra)
198         {
199                 // +mars
200                 // ahhh -- a nice C&P job :)
201                 // for the bounding box stuff
202                 __QGLTABLENAME.m_pfn_qglColor4f(0.25f, 0.75f, 0.75f, 1.0f);
203
204                 __QGLTABLENAME.m_pfn_qglTranslatef( 16.0f, 16.0f, 28.0f );
205
206                 __QGLTABLENAME.m_pfn_qglBegin( GL_LINE_STRIP );
207                 
208                 int i;  
209                 for ( i = 0; i < nPathCount; i++ )
210                         __QGLTABLENAME.m_pfn_qglVertex3fv( path[i] );
211
212                 __QGLTABLENAME.m_pfn_qglEnd();
213         
214         // ---------------
215
216                 __QGLTABLENAME.m_pfn_qglTranslatef( -16.0f, -16.0f, -28.0f );   // back to where we were
217                 __QGLTABLENAME.m_pfn_qglTranslatef( -16.0f, 16.0f, 28.0f );             // move to new postion
218         
219                 __QGLTABLENAME.m_pfn_qglBegin( GL_LINE_STRIP );
220                 
221                         for ( i = 0; i < nPathCount; i++ )
222                                 __QGLTABLENAME.m_pfn_qglVertex3fv( path[i] );
223
224                 __QGLTABLENAME.m_pfn_qglEnd();
225
226         // --------------
227
228                 __QGLTABLENAME.m_pfn_qglTranslatef( 16.0f, -16.0f, -28.0f );            // back to where we were
229                 __QGLTABLENAME.m_pfn_qglTranslatef( 16.0f, -16.0f, -28.0f );            // new pos
230
231                 __QGLTABLENAME.m_pfn_qglBegin( GL_LINE_STRIP );
232                 
233                         for ( i = 0; i < nPathCount; i++ )
234                                 __QGLTABLENAME.m_pfn_qglVertex3fv( path[i] );
235
236                 __QGLTABLENAME.m_pfn_qglEnd();
237
238         // ----------------
239
240                 __QGLTABLENAME.m_pfn_qglTranslatef( -16.0f, 16.0f, 28.0f );             // back to where we were
241                 __QGLTABLENAME.m_pfn_qglTranslatef( -16.0f, -16.0f, -28.0f );           // new pos
242
243                 __QGLTABLENAME.m_pfn_qglBegin( GL_LINE_STRIP );
244                 
245                         for ( i = 0; i < nPathCount; i++ )
246                                 __QGLTABLENAME.m_pfn_qglVertex3fv( path[i] );
247
248                 __QGLTABLENAME.m_pfn_qglEnd();
249         }
250         // -mars
251
252         __QGLTABLENAME.m_pfn_qglPopAttrib();
253 }
254
255 void DBobView::Register()
256 {
257         __QGLTABLENAME.m_pfnHookGL2DWindow( this );
258         __QGLTABLENAME.m_pfnHookGL3DWindow( this );
259         m_bHooked = TRUE;
260 }
261
262 void DBobView::UnRegister()
263 {
264         __QGLTABLENAME.m_pfnUnHookGL2DWindow( this );
265         __QGLTABLENAME.m_pfnUnHookGL3DWindow( this );
266         m_bHooked = FALSE;
267 }
268
269 void DBobView::SetPath(vec3_t *pPath)
270 {
271         if(path)
272                 delete[] path;
273
274         path = pPath;
275 }
276
277 #define LOCAL_GRAVITY -800.0f
278
279 bool DBobView::CalculateTrajectory(vec3_t start, vec3_t apex, float multiplier, int points, float varGravity)
280 {
281         if(apex[2] <= start[2])
282         {
283                 SetPath(NULL);
284                 return FALSE;
285         }
286         // ----think q3a actually would allow these
287         //scrub that, coz the plugin wont :]
288
289         vec3_t dist, speed;
290         VectorSubtract(apex, start, dist);
291
292         vec_t speed_z = (float)sqrt(-2*LOCAL_GRAVITY*dist[2]);
293         float flight_time = -speed_z/LOCAL_GRAVITY;
294
295
296         VectorScale(dist, 1/flight_time, speed);
297         speed[2] = speed_z;
298
299 //      Sys_Printf("Speed: (%.4f %.4f %.4f)\n", speed[0], speed[1], speed[2]);
300
301         vec3_t* pPath = new vec3_t[points];
302
303         float interval = multiplier*flight_time/points;
304         for(int i = 0; i < points; i++)
305         {
306                 float ltime = interval*i;
307
308                 VectorScale(speed, ltime, pPath[i]);
309                 VectorAdd(pPath[i], start, pPath[i]);
310
311                 // could do this all with vectors
312                 // vGrav = {0, 0, -800.0f}
313                 // VectorScale(vGrav, 0.5f*ltime*ltime, vAdd);
314                 // VectorScale(speed, ltime, pPath[i]);
315                 // _VectorAdd(pPath[i], start, pPath[i])
316                 // _VectorAdd(pPath[i], vAdd, pPath[i])
317
318                 pPath[i][2] = start[2] + (speed_z*ltime) + (varGravity*0.5f*ltime*ltime);
319         }
320
321         SetPath(pPath);
322         return TRUE;
323 }
324
325 void DBobView::Begin(const char* trigger, const char *target, float multiplier, int points, float varGravity, bool bNoUpdate, bool bShowExtra)
326 {
327         strcpy(entTrigger, trigger);
328         strcpy(entTarget, target);
329
330         fMultiplier = multiplier;
331         fVarGravity = varGravity;
332         nPathCount = points;
333         m_bShowExtra = bShowExtra;
334
335         Register();
336
337         if(UpdatePath())
338         {
339                 if(!bNoUpdate)
340                 {
341                         eyes = new DListener;
342                         eyes->parent = this;
343                         eyes->Register();
344                 }
345         }
346         else
347         {
348                 Sys_ERROR("Initialization Failure in DBobView::Begin");
349                 delete this;
350         }
351 }
352
353 bool DBobView::UpdatePath()
354 {
355         vec3_t start, apex;
356
357         if(GetEntityCentre(entTrigger, start))
358         {
359                 if(GetEntityCentre(entTarget, apex))
360                 {
361                         CalculateTrajectory(start, apex, fMultiplier, nPathCount, fVarGravity);
362                         return TRUE;
363                 }
364         }
365         return FALSE;
366 }