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
20 #include "DTrainDrawer.h"
33 #include "funchandlers.h"
35 #include "iglrender.h"
37 #include "math/matrix.h"
39 #include "dialogs/dialogs-gtk.h"
41 DTrainDrawer::DTrainDrawer() {
46 GlobalShaderCache().attachRenderable( *this );
49 DTrainDrawer::~DTrainDrawer( void ) {
50 GlobalShaderCache().detachRenderable( *this );
57 void DTrainDrawer::ClearSplines() {
58 for ( std::list<splinePoint_t *>::const_iterator deadSpline = m_splineList.begin(); deadSpline != m_splineList.end(); deadSpline++ ) {
59 ( *deadSpline )->m_pointList.clear();
60 ( *deadSpline )->m_vertexList.clear();
61 delete ( *deadSpline );
67 void DTrainDrawer::ClearPoints() {
68 for ( std::list<controlPoint_t *>::const_iterator deadPoint = m_pointList.begin(); deadPoint != m_pointList.end(); deadPoint++ ) {
75 void CalculateSpline_r( vec3_t* v, int count, vec3_t out, float tension ) {
83 VectorSubtract( v[1], v[0], dist );
84 VectorMA( v[0], tension, dist, out );
88 vec3_t* v2 = new vec3_t[count - 1];
90 for ( int i = 0; i < count - 1; i++ ) {
91 VectorSubtract( v[i + 1], v[i], dist );
92 VectorMA( v[i], tension, dist, v2[i] );
95 CalculateSpline_r( v2, count - 1, out, tension );
100 void DTrainDrawer::render( RenderStateFlags state ) const {
101 for ( std::list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
102 splinePoint_t* pSP = ( *sp );
104 glBegin( GL_LINE_STRIP );
105 for ( std::list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++ ) {
106 glVertex3fv( ( *v )._pnt );
113 const char* DTrainDrawer_state_wireframe = "$bobtoolz/traindrawer/wireframe";
114 const char* DTrainDrawer_state_solid = "$bobtoolz/traindrawer/solid";
116 void DTrainDrawer::constructShaders(){
118 GlobalOpenGLStateLibrary().getDefaultState( state );
119 state.m_state = RENDER_COLOURWRITE | RENDER_DEPTHWRITE | RENDER_BLEND;
120 state.m_sort = OpenGLState::eSortOverlayFirst;
121 state.m_linewidth = 1;
122 state.m_colour[0] = 1;
123 state.m_colour[1] = 0;
124 state.m_colour[2] = 0;
125 state.m_colour[3] = 1;
126 state.m_linewidth = 1;
127 GlobalOpenGLStateLibrary().insert( DTrainDrawer_state_wireframe, state );
129 state.m_colour[0] = 1;
130 state.m_colour[1] = 1;
131 state.m_colour[2] = 1;
132 state.m_colour[3] = 1;
133 state.m_linewidth = 2;
134 GlobalOpenGLStateLibrary().insert( DTrainDrawer_state_solid, state );
136 m_shader_wireframe = GlobalShaderCache().capture( DTrainDrawer_state_wireframe );
137 m_shader_solid = GlobalShaderCache().capture( DTrainDrawer_state_solid );
140 void DTrainDrawer::destroyShaders(){
141 GlobalOpenGLStateLibrary().erase( DTrainDrawer_state_wireframe );
142 GlobalOpenGLStateLibrary().erase( DTrainDrawer_state_solid );
143 GlobalShaderCache().release( DTrainDrawer_state_wireframe );
144 GlobalShaderCache().release( DTrainDrawer_state_solid );
148 void DTrainDrawer::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
153 renderer.SetState( m_shader_wireframe, Renderer::eWireframeOnly );
154 renderer.SetState( m_shader_solid, Renderer::eFullMaterials );
155 renderer.addRenderable( *this, g_matrix4_identity );
157 void DTrainDrawer::renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
158 renderSolid( renderer, volume );
161 void AddSplineControl( const char* control, splinePoint_t* pSP ) {
163 strncpy( cp.strName, control, 64 );
165 pSP->m_pointList.push_front( cp );
168 class EntityBuildPaths
171 DTrainDrawer& drawer;
173 EntityBuildPaths( DTrainDrawer& drawer ) : drawer( drawer ){
175 void operator()( scene::Instance& instance ) const {
177 e.LoadEPairList( Node_getEntity( instance.path().top() ) );
179 const char* classname = e.m_Classname.GetBuffer();
182 const char* targetname;
185 e.SpawnString( "targetname", NULL, &targetname );
186 e.SpawnVector( "origin", "0 0 0", vOrigin );
188 if ( !strcmp( classname, "info_train_spline_main" ) ) {
190 globalOutputStream() << "info_train_spline_main with no targetname";
194 e.SpawnString( "target", NULL, &target );
197 drawer.AddControlPoint( targetname, vOrigin );
200 splinePoint_t* pSP = drawer.AddSplinePoint( targetname, target, vOrigin );
202 e.SpawnString( "control", NULL, &control );
205 AddSplineControl( control, pSP );
207 for ( int j = 2;; j++ ) {
209 sprintf( buffer, "control%i", j );
211 e.SpawnString( buffer, NULL, &control );
216 AddSplineControl( control, pSP );
221 else if ( !strcmp( classname, "info_train_spline_control" ) ) {
223 globalOutputStream() << "info_train_spline_control with no targetname";
227 drawer.AddControlPoint( targetname, vOrigin );
232 void DTrainDrawer::BuildPaths() {
233 Scene_forEachEntity( EntityBuildPaths( *this ) );
235 std::list<splinePoint_t* >::const_iterator sp;
236 for ( sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
237 splinePoint_t* pSP = ( *sp );
239 controlPoint_t* pTarget = FindControlPoint( pSP->strTarget );
242 globalOutputStream() << "couldn't find target " << pSP->strTarget;
247 pSP->pTarget = pTarget;
250 for ( std::list<controlPoint_t >::iterator cp = pSP->m_pointList.begin(); cp != pSP->m_pointList.end(); cp++ ) {
251 controlPoint_t* pControl = FindControlPoint( ( *cp ).strName );
253 globalOutputStream() << "couldn't find control " << ( *cp ).strName;
257 VectorCopy( pControl->vOrigin, ( *cp ).vOrigin );
263 for ( sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
264 splinePoint_t* pSP = ( *sp );
267 if ( !pSP->pTarget ) {
271 std::size_t count = pSP->m_pointList.size() + 2;
272 vec3_t* v = new vec3_t[count];
274 VectorCopy( pSP->point.vOrigin, v[0] );
277 for ( std::list<controlPoint_t>::reverse_iterator cp = pSP->m_pointList.rbegin(); cp != pSP->m_pointList.rend(); cp++ ) {
278 VectorCopy( ( *cp ).vOrigin, v[i] );
281 VectorCopy( pSP->pTarget->vOrigin, v[i] );
283 for ( float tension = 0.0f; tension <= 1.f; tension += 0.01f ) {
284 CalculateSpline_r( v, static_cast<int>( count ), out._pnt, tension );
285 pSP->m_vertexList.push_front( out );
290 VectorCopy( pSP->pTarget->vOrigin, out._pnt );
291 pSP->m_vertexList.push_front( out );
297 void DTrainDrawer::AddControlPoint( const char* name, vec_t* origin ){
298 controlPoint_t* pCP = new controlPoint_t;
300 strncpy( pCP->strName, name, 64 );
301 VectorCopy( origin, pCP->vOrigin );
303 m_pointList.push_back( pCP );
306 splinePoint_t* DTrainDrawer::AddSplinePoint( const char* name, const char* target, vec_t* origin ){
307 splinePoint_t* pSP = new splinePoint_t;
309 strncpy( pSP->point.strName, name, 64 );
310 strncpy( pSP->strTarget, target, 64 );
311 VectorCopy( origin, pSP->point.vOrigin );
312 m_splineList.push_back( pSP );
317 controlPoint_t* DTrainDrawer::FindControlPoint( const char* name ){
318 for ( std::list<controlPoint_t*>::const_iterator cp = m_pointList.begin(); cp != m_pointList.end(); cp++ ) {
319 if ( !strcmp( name, ( *cp )->strName ) ) {
324 for ( std::list<splinePoint_t*>::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++ ) {
325 if ( !strcmp( name, ( *sp )->point.strName ) ) {
326 return &( ( *sp )->point );