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