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
23 #include "DTrainDrawer.h"
27 #include "funchandlers.h"
29 #include "dialogs/dialogs-gtk.h"
31 DTrainDrawer::DTrainDrawer() {
39 DTrainDrawer::~DTrainDrawer( void ) {
48 void DTrainDrawer::ClearSplines() {
49 for ( list<splinePoint_t *>::const_iterator deadSpline = m_splineList.begin(); deadSpline != m_splineList.end(); deadSpline++ ) {
50 ( *deadSpline )->m_pointList.clear();
51 ( *deadSpline )->m_vertexList.clear();
52 delete ( *deadSpline );
58 void DTrainDrawer::ClearPoints() {
59 for ( list<controlPoint_t *>::const_iterator deadPoint = m_pointList.begin(); deadPoint != m_pointList.end(); deadPoint++ ) {
66 void DTrainDrawer::Register() {
67 g_QglTable.m_pfnHookGL2DWindow( this );
68 g_QglTable.m_pfnHookGL3DWindow( this );
72 void DTrainDrawer::UnRegister() {
73 g_QglTable.m_pfnUnHookGL2DWindow( this );
74 g_QglTable.m_pfnUnHookGL3DWindow( this );
78 void CalculateSpline_r( vec3_t* v, int count, vec3_t out, float tension ) {
86 VectorSubtract( v[1], v[0], dist );
87 VectorMA( v[0], tension, dist, out );
91 vec3_t* v2 = new vec3_t[count - 1];
93 for ( int i = 0; i < count - 1; i++ ) {
94 VectorSubtract( v[i + 1], v[i], dist );
95 VectorMA( v[i], tension, dist, v2[i] );
98 CalculateSpline_r( v2, count - 1, out, tension );
103 void DTrainDrawer::Draw3D() {
109 g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS );
111 g_QglTable.m_pfn_qglDisable( GL_BLEND );
112 g_QglTable.m_pfn_qglDisable( GL_LINE_SMOOTH );
114 g_QglTable.m_pfn_qglPushMatrix();
116 g_QglTable.m_pfn_qglLineWidth( 2.0f );
117 g_QglTable.m_pfn_qglColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
119 g_QglTable.m_pfn_qglEnable( GL_BLEND );
120 g_QglTable.m_pfn_qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
121 g_QglTable.m_pfn_qglDisable( GL_POLYGON_SMOOTH );
123 g_QglTable.m_pfn_qglDepthFunc( GL_ALWAYS );
125 for ( list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
126 splinePoint_t* pSP = ( *sp );
128 g_QglTable.m_pfn_qglBegin( GL_LINE_STRIP );
129 for ( list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++ ) {
130 g_QglTable.m_pfn_qglVertex3fv( ( *v )._pnt );
132 g_QglTable.m_pfn_qglEnd();
136 g_QglTable.m_pfn_qglPopMatrix();
137 g_QglTable.m_pfn_qglPopAttrib();
140 void DTrainDrawer::Draw2D( VIEWTYPE vt ) {
146 g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS );
148 g_QglTable.m_pfn_qglDisable( GL_BLEND );
149 g_QglTable.m_pfn_qglDisable( GL_LINE_SMOOTH );
151 g_QglTable.m_pfn_qglPushMatrix();
158 g_QglTable.m_pfn_qglRotatef( 270.0f, 1.0f, 0.0f, 0.0f );
161 g_QglTable.m_pfn_qglRotatef( 270.0f, 1.0f, 0.0f, 0.0f );
162 g_QglTable.m_pfn_qglRotatef( 270.0f, 0.0f, 0.0f, 1.0f );
166 g_QglTable.m_pfn_qglLineWidth( 1.0f );
167 g_QglTable.m_pfn_qglColor4f( 1.0f, 0.0f, 0.0f, 0.5f );
169 g_QglTable.m_pfn_qglEnable( GL_BLEND );
170 g_QglTable.m_pfn_qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
171 g_QglTable.m_pfn_qglDisable( GL_POLYGON_SMOOTH );
173 g_QglTable.m_pfn_qglDepthFunc( GL_ALWAYS );
175 g_QglTable.m_pfn_qglColor4f( 1.f, 0.f, 0.f, 1.f );
177 for ( list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
178 splinePoint_t* pSP = ( *sp );
180 g_QglTable.m_pfn_qglBegin( GL_LINE_STRIP );
181 for ( list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++ ) {
182 g_QglTable.m_pfn_qglVertex3fv( ( *v )._pnt );
184 g_QglTable.m_pfn_qglEnd();
188 g_QglTable.m_pfn_qglPopMatrix();
189 g_QglTable.m_pfn_qglPopAttrib();
192 void AddSplineControl( const char* control, splinePoint_t* pSP ) {
194 strncpy( cp.strName, control, 64 );
196 pSP->m_pointList.push_front( cp );
199 void DTrainDrawer::BuildPaths() {
200 int count = g_FuncTable.m_pfnGetEntityCount();
204 for ( int i = 0; i < count; i++ ) {
205 entity_s* ent = (entity_s*)g_FuncTable.m_pfnGetEntityHandle( i );
207 e.LoadEPairList( *g_EntityTable.m_pfnGetEntityKeyValList( ent ) );
209 const char* classname = e.m_Classname.GetBuffer();
212 const char* targetname;
215 e.SpawnString( "targetname", NULL, &targetname );
216 e.SpawnVector( "origin", "0 0 0", vOrigin );
218 if ( !strcmp( classname, "info_train_spline_main" ) ) {
220 Sys_Printf( "info_train_spline_main with no targetname" );
224 e.SpawnString( "target", NULL, &target );
227 AddControlPoint( targetname, vOrigin );
230 splinePoint_t* pSP = AddSplinePoint( targetname, target, vOrigin );
232 e.SpawnString( "control", NULL, &control );
235 AddSplineControl( control, pSP );
237 for ( int j = 2;; j++ ) {
239 sprintf( buffer, "control%i", j );
241 e.SpawnString( buffer, NULL, &control );
246 AddSplineControl( control, pSP );
251 else if ( !strcmp( classname, "info_train_spline_control" ) ) {
253 Sys_Printf( "info_train_spline_control with no targetname" );
257 AddControlPoint( targetname, vOrigin );
261 list<splinePoint_t* >::const_iterator sp;
262 for ( sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
263 splinePoint_t* pSP = ( *sp );
265 controlPoint_t* pTarget = FindControlPoint( pSP->strTarget );
268 Sys_Printf( "couldn't find target %s", pSP->strTarget );
273 pSP->pTarget = pTarget;
276 for ( list<controlPoint_t >::iterator cp = pSP->m_pointList.begin(); cp != pSP->m_pointList.end(); cp++ ) {
277 controlPoint_t* pControl = FindControlPoint( ( *cp ).strName );
279 Sys_Printf( "couldn't find control %s", ( *cp ).strName );
283 VectorCopy( pControl->vOrigin, ( *cp ).vOrigin );
290 for ( sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
291 splinePoint_t* pSP = ( *sp );
294 if ( !pSP->pTarget ) {
298 int count = pSP->m_pointList.size() + 2;
299 vec3_t* v = new vec3_t[count];
301 VectorCopy( pSP->point.vOrigin, v[0] );
304 for ( list<controlPoint_t>::reverse_iterator cp = pSP->m_pointList.rbegin(); cp != pSP->m_pointList.rend(); cp++ ) {
305 VectorCopy( ( *cp ).vOrigin, v[i] );
308 VectorCopy( pSP->pTarget->vOrigin, v[i] );
310 for ( float tension = 0.0f; tension <= 1.f; tension += 0.01f ) {
311 CalculateSpline_r( v, count, out._pnt, tension );
312 pSP->m_vertexList.push_front( out );
317 VectorCopy( pSP->pTarget->vOrigin, out._pnt );
318 pSP->m_vertexList.push_front( out );
324 void DTrainDrawer::AddControlPoint( const char* name, vec_t* origin ){
325 controlPoint_t* pCP = new controlPoint_t;
327 strncpy( pCP->strName, name, 64 );
328 VectorCopy( origin, pCP->vOrigin );
330 m_pointList.push_back( pCP );
333 splinePoint_t* DTrainDrawer::AddSplinePoint( const char* name, const char* target, vec_t* origin ){
334 splinePoint_t* pSP = new splinePoint_t;
336 strncpy( pSP->point.strName, name, 64 );
337 strncpy( pSP->strTarget, target, 64 );
338 VectorCopy( origin, pSP->point.vOrigin );
339 m_splineList.push_back( pSP );
344 controlPoint_t* DTrainDrawer::FindControlPoint( const char* name ){
345 for ( list<controlPoint_t*>::const_iterator cp = m_pointList.begin(); cp != m_pointList.end(); cp++ ) {
346 if ( !strcmp( name, ( *cp )->strName ) ) {
351 for ( list<splinePoint_t*>::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
352 if ( !strcmp( name, ( *sp )->point.strName ) ) {
353 return &( ( *sp )->point );