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