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) {
47 void DTrainDrawer::ClearSplines() {
48 for(list<splinePoint_t *>::const_iterator deadSpline = m_splineList.begin(); deadSpline != m_splineList.end(); deadSpline++) {
49 (*deadSpline)->m_pointList.clear();
50 (*deadSpline)->m_vertexList.clear();
57 void DTrainDrawer::ClearPoints() {
58 for(list<controlPoint_t *>::const_iterator deadPoint = m_pointList.begin(); deadPoint != m_pointList.end(); deadPoint++) {
65 void DTrainDrawer::Register() {
66 g_QglTable.m_pfnHookGL2DWindow( this );
67 g_QglTable.m_pfnHookGL3DWindow( this );
71 void DTrainDrawer::UnRegister() {
72 g_QglTable.m_pfnUnHookGL2DWindow( this );
73 g_QglTable.m_pfnUnHookGL3DWindow( this );
77 void CalculateSpline_r(vec3_t* v, int count, vec3_t out, float tension) {
85 VectorSubtract( v[1], v[0], dist );
86 VectorMA(v[0], tension, dist, out);
90 vec3_t* v2 = new vec3_t[count-1];
92 for( int i = 0; i < count-1; i++ ) {
93 VectorSubtract( v[i+1], v[i], dist );
94 VectorMA(v[i], tension, dist, v2[i]);
97 CalculateSpline_r( v2, count-1, out, tension);
102 void DTrainDrawer::Draw3D() {
108 g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
110 g_QglTable.m_pfn_qglDisable(GL_BLEND);
111 g_QglTable.m_pfn_qglDisable(GL_LINE_SMOOTH);
113 g_QglTable.m_pfn_qglPushMatrix();
115 g_QglTable.m_pfn_qglLineWidth(2.0f);
116 g_QglTable.m_pfn_qglColor4f(1.0f, 1.0f, 1.0f, 1.0f);
118 g_QglTable.m_pfn_qglEnable(GL_BLEND);
119 g_QglTable.m_pfn_qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
120 g_QglTable.m_pfn_qglDisable(GL_POLYGON_SMOOTH);
122 g_QglTable.m_pfn_qglDepthFunc(GL_ALWAYS);
124 for(list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
125 splinePoint_t* pSP = (*sp);
127 g_QglTable.m_pfn_qglBegin(GL_LINE_STRIP);
128 for(list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++) {
129 g_QglTable.m_pfn_qglVertex3fv((*v)._pnt);
131 g_QglTable.m_pfn_qglEnd();
135 g_QglTable.m_pfn_qglPopMatrix();
136 g_QglTable.m_pfn_qglPopAttrib();
139 void DTrainDrawer::Draw2D(VIEWTYPE vt) {
145 g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
147 g_QglTable.m_pfn_qglDisable(GL_BLEND);
148 g_QglTable.m_pfn_qglDisable(GL_LINE_SMOOTH);
150 g_QglTable.m_pfn_qglPushMatrix();
157 g_QglTable.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
160 g_QglTable.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
161 g_QglTable.m_pfn_qglRotatef(270.0f, 0.0f, 0.0f, 1.0f);
165 g_QglTable.m_pfn_qglLineWidth(1.0f);
166 g_QglTable.m_pfn_qglColor4f(1.0f, 0.0f, 0.0f, 0.5f);
168 g_QglTable.m_pfn_qglEnable(GL_BLEND);
169 g_QglTable.m_pfn_qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
170 g_QglTable.m_pfn_qglDisable(GL_POLYGON_SMOOTH);
172 g_QglTable.m_pfn_qglDepthFunc(GL_ALWAYS);
174 g_QglTable.m_pfn_qglColor4f(1.f, 0.f, 0.f, 1.f);
176 for(list<splinePoint_t* >::const_iterator sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
177 splinePoint_t* pSP = (*sp);
179 g_QglTable.m_pfn_qglBegin(GL_LINE_STRIP);
180 for(list<DPoint >::const_iterator v = pSP->m_vertexList.begin(); v != pSP->m_vertexList.end(); v++) {
181 g_QglTable.m_pfn_qglVertex3fv((*v)._pnt);
183 g_QglTable.m_pfn_qglEnd();
187 g_QglTable.m_pfn_qglPopMatrix();
188 g_QglTable.m_pfn_qglPopAttrib();
191 void AddSplineControl(const char* control, splinePoint_t* pSP) {
193 strncpy(cp.strName, control, 64);
195 pSP->m_pointList.push_front(cp);
198 void DTrainDrawer::BuildPaths() {
199 int count = g_FuncTable.m_pfnGetEntityCount();
203 for(int i = 0; i < count; i++) {
204 entity_s* ent = (entity_s*)g_FuncTable.m_pfnGetEntityHandle(i);
206 e.LoadEPairList(*g_EntityTable.m_pfnGetEntityKeyValList(ent));
208 const char* classname = e.m_Classname.GetBuffer();
211 const char* targetname;
214 e.SpawnString("targetname", NULL, &targetname);
215 e.SpawnVector("origin", "0 0 0", vOrigin);
217 if(!strcmp(classname, "info_train_spline_main")) {
219 Sys_Printf( "info_train_spline_main with no targetname" );
223 e.SpawnString("target", NULL, &target);
226 AddControlPoint( targetname, vOrigin );
228 splinePoint_t* pSP = AddSplinePoint( targetname, target, vOrigin );
230 e.SpawnString("control", NULL, &control);
233 AddSplineControl( control, pSP );
235 for(int j = 2;; j++) {
237 sprintf(buffer, "control%i", j);
239 e.SpawnString(buffer, NULL, &control);
244 AddSplineControl( control, pSP );
248 } else if(!strcmp(classname, "info_train_spline_control")) {
250 Sys_Printf( "info_train_spline_control with no targetname" );
254 AddControlPoint( targetname, vOrigin );
258 list<splinePoint_t* >::const_iterator sp;
259 for(sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
260 splinePoint_t* pSP = (*sp);
262 controlPoint_t* pTarget = FindControlPoint( pSP->strTarget );
265 Sys_Printf( "couldn't find target %s", pSP->strTarget );
270 pSP->pTarget = pTarget;
273 for(list<controlPoint_t >::iterator cp = pSP->m_pointList.begin(); cp != pSP->m_pointList.end(); cp++) {
274 controlPoint_t* pControl = FindControlPoint( (*cp).strName );
276 Sys_Printf( "couldn't find control %s", (*cp).strName );
280 VectorCopy(pControl->vOrigin, (*cp).vOrigin);
287 for(sp = m_splineList.begin(); sp != m_splineList.end(); sp++) {
288 splinePoint_t* pSP = (*sp);
295 int count = pSP->m_pointList.size() + 2;
296 vec3_t* v = new vec3_t[count];
298 VectorCopy(pSP->point.vOrigin, v[0]);
301 for(list<controlPoint_t>::reverse_iterator cp = pSP->m_pointList.rbegin(); cp != pSP->m_pointList.rend(); cp++) {
302 VectorCopy((*cp).vOrigin, v[i]);
305 VectorCopy(pSP->pTarget->vOrigin, v[i]);
307 for (float tension = 0.0f; tension <= 1.f; tension += 0.01f) {
308 CalculateSpline_r(v, count, out._pnt, tension);
309 pSP->m_vertexList.push_front(out);
314 VectorCopy(pSP->pTarget->vOrigin, out._pnt);
315 pSP->m_vertexList.push_front(out);
321 void DTrainDrawer::AddControlPoint(const char* name, vec_t* origin)
323 controlPoint_t* pCP = new controlPoint_t;
325 strncpy(pCP->strName, name, 64);
326 VectorCopy( origin, pCP->vOrigin );
328 m_pointList.push_back( pCP );
331 splinePoint_t* DTrainDrawer::AddSplinePoint(const char* name, const char* target, vec_t* origin)
333 splinePoint_t* pSP = new splinePoint_t;
335 strncpy(pSP->point.strName, name, 64);
336 strncpy(pSP->strTarget, target, 64);
337 VectorCopy( origin, pSP->point.vOrigin );
338 m_splineList.push_back( pSP );
343 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);