2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
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.
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.
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
22 #include "gtkr_list.h"
31 #include "DTrainDrawer.h"
34 #include "funchandlers.h"
36 #include "dialogs/dialogs-gtk.h"
38 DTrainDrawer::DTrainDrawer() {
46 DTrainDrawer::~DTrainDrawer(void) {
54 void DTrainDrawer::ClearSplines() {
55 for(list<splinePoint_t *>::const_iterator deadSpline = m_splineList.begin(); deadSpline != m_splineList.end(); deadSpline++) {
56 (*deadSpline)->m_pointList.clear();
57 (*deadSpline)->m_vertexList.clear();
64 void DTrainDrawer::ClearPoints() {
65 for(list<controlPoint_t *>::const_iterator deadPoint = m_pointList.begin(); deadPoint != m_pointList.end(); deadPoint++) {
72 void DTrainDrawer::Register() {
73 __QGLTABLENAME.m_pfnHookGL2DWindow( this );
74 __QGLTABLENAME.m_pfnHookGL3DWindow( this );
78 void DTrainDrawer::UnRegister() {
79 __QGLTABLENAME.m_pfnUnHookGL2DWindow( this );
80 __QGLTABLENAME.m_pfnUnHookGL3DWindow( this );
84 void CalculateSpline_r(vec3_t* v, int count, vec3_t out, float tension) {
92 VectorSubtract( v[1], v[0], dist );
93 VectorMA(v[0], tension, dist, out);
97 vec3_t* v2 = new vec3_t[count-1];
99 for( int i = 0; i < count-1; i++ ) {
100 VectorSubtract( v[i+1], v[i], dist );
101 VectorMA(v[i], tension, dist, v2[i]);
104 CalculateSpline_r( v2, count-1, out, tension);
109 void DTrainDrawer::Draw3D() {
115 __QGLTABLENAME.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
117 __QGLTABLENAME.m_pfn_qglDisable(GL_BLEND);
118 __QGLTABLENAME.m_pfn_qglDisable(GL_LINE_SMOOTH);
120 __QGLTABLENAME.m_pfn_qglPushMatrix();
122 __QGLTABLENAME.m_pfn_qglLineWidth(2.0f);
123 __QGLTABLENAME.m_pfn_qglColor4f(1.0f, 1.0f, 1.0f, 1.0f);
125 __QGLTABLENAME.m_pfn_qglEnable(GL_BLEND);
126 __QGLTABLENAME.m_pfn_qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
127 __QGLTABLENAME.m_pfn_qglDisable(GL_POLYGON_SMOOTH);
129 __QGLTABLENAME.m_pfn_qglDepthFunc(GL_ALWAYS);
131 for(list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
132 splinePoint_t* pSP = (*sp);
134 __QGLTABLENAME.m_pfn_qglBegin(GL_LINE_STRIP);
135 for(list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++) {
136 __QGLTABLENAME.m_pfn_qglVertex3fv((*v)._pnt);
138 __QGLTABLENAME.m_pfn_qglEnd();
142 __QGLTABLENAME.m_pfn_qglPopMatrix();
143 __QGLTABLENAME.m_pfn_qglPopAttrib();
146 void DTrainDrawer::Draw2D(VIEWTYPE vt) {
152 __QGLTABLENAME.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
154 __QGLTABLENAME.m_pfn_qglDisable(GL_BLEND);
155 __QGLTABLENAME.m_pfn_qglDisable(GL_LINE_SMOOTH);
157 __QGLTABLENAME.m_pfn_qglPushMatrix();
164 __QGLTABLENAME.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
167 __QGLTABLENAME.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
168 __QGLTABLENAME.m_pfn_qglRotatef(270.0f, 0.0f, 0.0f, 1.0f);
172 __QGLTABLENAME.m_pfn_qglLineWidth(1.0f);
173 __QGLTABLENAME.m_pfn_qglColor4f(1.0f, 0.0f, 0.0f, 0.5f);
175 __QGLTABLENAME.m_pfn_qglEnable(GL_BLEND);
176 __QGLTABLENAME.m_pfn_qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
177 __QGLTABLENAME.m_pfn_qglDisable(GL_POLYGON_SMOOTH);
179 __QGLTABLENAME.m_pfn_qglDepthFunc(GL_ALWAYS);
181 __QGLTABLENAME.m_pfn_qglColor4f(1.f, 0.f, 0.f, 1.f);
183 for(list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
184 splinePoint_t* pSP = (*sp);
186 __QGLTABLENAME.m_pfn_qglBegin(GL_LINE_STRIP);
187 for(list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++) {
188 __QGLTABLENAME.m_pfn_qglVertex3fv((*v)._pnt);
190 __QGLTABLENAME.m_pfn_qglEnd();
194 __QGLTABLENAME.m_pfn_qglPopMatrix();
195 __QGLTABLENAME.m_pfn_qglPopAttrib();
198 void AddSplineControl(const char* control, splinePoint_t* pSP) {
200 strncpy(cp.strName, control, 64);
202 pSP->m_pointList.push_front(cp);
205 void DTrainDrawer::BuildPaths() {
207 int count = g_FuncTable.m_pfnGetEntityCount();
211 for(int i = 0; i < count; i++) {
212 entity_s* ent = (entity_s*)g_FuncTable.m_pfnGetEntityHandle(i);
214 e.LoadEPairList(*g_EntityTable.m_pfnGetEntityKeyValList(ent));
216 const char* classname = e.m_Classname.GetBuffer();
219 const char* targetname;
222 e.SpawnString("targetname", NULL, &targetname);
223 e.SpawnVector("origin", "0 0 0", vOrigin);
225 if(!strcmp(classname, "info_train_spline_main")) {
227 Sys_Printf( "info_train_spline_main with no targetname" );
231 e.SpawnString("target", NULL, &target);
234 AddControlPoint( targetname, vOrigin );
236 splinePoint_t* pSP = AddSplinePoint( targetname, target, vOrigin );
238 e.SpawnString("control", NULL, &control);
241 AddSplineControl( control, pSP );
243 for(int j = 2;; j++) {
245 sprintf(buffer, "control%i", j);
247 e.SpawnString(buffer, NULL, &control);
252 AddSplineControl( control, pSP );
256 } else if(!strcmp(classname, "info_train_spline_control")) {
258 Sys_Printf( "info_train_spline_control with no targetname" );
262 AddControlPoint( targetname, vOrigin );
266 list<splinePoint_t* >::const_iterator sp;
267 for(sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
268 splinePoint_t* pSP = (*sp);
270 controlPoint_t* pTarget = FindControlPoint( pSP->strTarget );
273 Sys_Printf( "couldn't find target %s", pSP->strTarget );
278 pSP->pTarget = pTarget;
281 for(list<controlPoint_t >::iterator cp = pSP->m_pointList.begin(); cp != pSP->m_pointList.end(); cp++) {
282 controlPoint_t* pControl = FindControlPoint( (*cp).strName );
284 Sys_Printf( "couldn't find control %s", (*cp).strName );
288 VectorCopy(pControl->vOrigin, (*cp).vOrigin);
295 for(sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
296 splinePoint_t* pSP = (*sp);
303 int count = pSP->m_pointList.size() + 2;
304 vec3_t* v = new vec3_t[count];
306 VectorCopy(pSP->point.vOrigin, v[0]);
309 for(list<controlPoint_t>::reverse_iterator cp = pSP->m_pointList.rbegin(); cp != pSP->m_pointList.rend(); cp++) {
310 VectorCopy((*cp).vOrigin, v[i]);
313 VectorCopy(pSP->pTarget->vOrigin, v[i]);
315 for (float tension = 0.0f; tension <= 1.f; tension += 0.01f) {
316 CalculateSpline_r(v, count, out._pnt, tension);
317 pSP->m_vertexList.push_front(out);
322 VectorCopy(pSP->pTarget->vOrigin, out._pnt);
323 pSP->m_vertexList.push_front(out);
329 void DTrainDrawer::AddControlPoint(const char* name, vec_t* origin)
331 controlPoint_t* pCP = new controlPoint_t;
333 strncpy(pCP->strName, name, 64);
334 VectorCopy( origin, pCP->vOrigin );
336 m_pointList.push_back( pCP );
339 splinePoint_t* DTrainDrawer::AddSplinePoint(const char* name, const char* target, vec_t* origin)
341 splinePoint_t* pSP = new splinePoint_t;
343 strncpy(pSP->point.strName, name, 64);
344 strncpy(pSP->strTarget, target, 64);
345 VectorCopy( origin, pSP->point.vOrigin );
346 m_splineList.push_back( pSP );
351 controlPoint_t* DTrainDrawer::FindControlPoint(const char* name)
353 for(list<controlPoint_t*>::const_iterator cp = m_pointList.begin(); cp != m_pointList.end(); cp++) {
354 if(!strcmp(name, (*cp)->strName)) {
359 for(list<splinePoint_t*>::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
360 if(!strcmp(name, (*sp)->point.strName)) {
361 return &((*sp)->point);