1 /*
2 PLIB - A Suite of Portable Game Libraries
3 Copyright (C) 1998,2002 Steve Baker
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library 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
18
19 For further information visit http://plib.sourceforge.net
20
21 $Id: ssgTransform.cxx 1992 2004-12-01 08:28:05Z wolfram_kuss $
22 */
23
24
25 #include "ssgLocal.h"
26
copy_from(ssgTransform * src,int clone_flags)27 void ssgTransform::copy_from ( ssgTransform *src, int clone_flags )
28 {
29 ssgBaseTransform::copy_from ( src, clone_flags ) ;
30 }
31
clone(int clone_flags)32 ssgBase *ssgTransform::clone ( int clone_flags )
33 {
34 ssgTransform *b = new ssgTransform ;
35 b -> copy_from ( this, clone_flags ) ;
36 return b ;
37 }
38
39
ssgTransform(sgCoord * c)40 ssgTransform::ssgTransform ( sgCoord *c )
41 {
42 type = ssgTypeTransform () ;
43 setTransform ( c ) ;
44 }
45
ssgTransform(void)46 ssgTransform::ssgTransform (void)
47 {
48 type = ssgTypeTransform () ;
49 }
50
~ssgTransform(void)51 ssgTransform::~ssgTransform (void)
52 {
53 }
54
recalcBSphere(void)55 void ssgTransform::recalcBSphere (void)
56 {
57 ssgBranch::recalcBSphere () ;
58
59 if ( ! bsphere . isEmpty () )
60 bsphere . orthoXform ( transform ) ;
61 }
62
cull(sgFrustum * f,sgMat4 m,int test_needed)63 void ssgTransform::cull ( sgFrustum *f, sgMat4 m, int test_needed )
64 {
65 if ( ! preTravTests ( &test_needed, SSGTRAV_CULL ) )
66 return ;
67
68 int cull_result = cull_test ( f, m, test_needed ) ;
69
70 if ( cull_result == SSG_OUTSIDE )
71 return ;
72
73 sgMat4 tmp ;
74
75 sgCopyMat4 ( tmp, m ) ;
76 sgPreMultMat4 ( tmp, transform ) ;
77
78 _ssgPushMatrix ( tmp ) ;
79 glPushMatrix () ;
80 glLoadMatrixf ( (float *) tmp ) ;
81
82 for ( ssgEntity *e = getKid ( 0 ) ; e != NULL ; e = getNextKid() )
83 e -> cull ( f, tmp, cull_result != SSG_INSIDE ) ;
84
85 glPopMatrix () ;
86 _ssgPopMatrix () ;
87
88 postTravTests ( SSGTRAV_CULL ) ;
89 }
90
hot(sgVec3 s,sgMat4 m,int test_needed)91 void ssgTransform::hot ( sgVec3 s, sgMat4 m, int test_needed )
92 {
93 if ( ! preTravTests ( &test_needed, SSGTRAV_HOT ) )
94 return ;
95
96 int hot_result = hot_test ( s, m, test_needed ) ;
97
98 if ( hot_result == SSG_OUTSIDE )
99 return ;
100
101 sgMat4 tmp ;
102
103 sgCopyMat4 ( tmp, m ) ;
104 sgPreMultMat4 ( tmp, transform ) ;
105
106 _ssgPushPath ( this ) ;
107
108 for ( ssgEntity *e = getKid ( 0 ) ; e != NULL ; e = getNextKid() )
109 e -> hot ( s, tmp, hot_result != SSG_INSIDE ) ;
110
111 _ssgPopPath () ;
112
113 postTravTests ( SSGTRAV_HOT ) ;
114 }
115
los(sgVec3 s,sgMat4 m,int test_needed)116 void ssgTransform::los ( sgVec3 s, sgMat4 m, int test_needed )
117 {
118 if ( ! preTravTests ( &test_needed, SSGTRAV_LOS ) )
119 return ;
120
121 int los_result = los_test ( s, m, test_needed ) ;
122
123 if ( los_result == SSG_OUTSIDE )
124 return ;
125
126 sgMat4 tmp ;
127
128 sgCopyMat4 ( tmp, m ) ;
129 sgPreMultMat4 ( tmp, transform ) ;
130
131 _ssgPushPath ( this ) ;
132
133 for ( ssgEntity *e = getKid ( 0 ) ; e != NULL ; e = getNextKid() )
134 e -> los ( s, tmp, los_result != SSG_INSIDE ) ;
135
136 _ssgPopPath () ;
137
138 postTravTests ( SSGTRAV_LOS ) ;
139 }
140
isect(sgSphere * s,sgMat4 m,int test_needed)141 void ssgTransform::isect ( sgSphere *s, sgMat4 m, int test_needed )
142 {
143 if ( ! preTravTests ( &test_needed, SSGTRAV_ISECT ) )
144 return ;
145
146 int isect_result = isect_test ( s, m, test_needed ) ;
147
148 if ( isect_result == SSG_OUTSIDE )
149 return ;
150
151 sgMat4 tmp ;
152
153 sgCopyMat4 ( tmp, m ) ;
154 sgPreMultMat4 ( tmp, transform ) ;
155
156 _ssgPushPath ( this ) ;
157
158 for ( ssgEntity *e = getKid ( 0 ) ; e != NULL ; e = getNextKid() )
159 e -> isect ( s, tmp, isect_result != SSG_INSIDE ) ;
160
161 _ssgPopPath () ;
162
163 postTravTests ( SSGTRAV_ISECT ) ;
164 }
165
setTransform(sgVec3 xyz)166 void ssgTransform::setTransform ( sgVec3 xyz )
167 {
168 sgMat4 tmp_trans;
169 sgMakeTransMat4 ( tmp_trans, xyz ) ;
170 setTransform ( tmp_trans ) ;
171 }
172
setTransform(sgCoord * xform)173 void ssgTransform::setTransform ( sgCoord *xform )
174 {
175 sgMat4 tmp_trans;
176 sgMakeCoordMat4 ( tmp_trans, xform ) ;
177 setTransform ( tmp_trans ) ;
178 }
179
setTransform(sgCoord * xform,float sx,float sy,float sz)180 void ssgTransform::setTransform ( sgCoord *xform, float sx, float sy, float sz )
181 {
182 sgMat4 tmp_trans;
183 sgMakeCoordMat4 ( tmp_trans, xform ) ;
184 sgScaleVec3 ( tmp_trans[0], sx ) ;
185 sgScaleVec3 ( tmp_trans[1], sy ) ;
186 sgScaleVec3 ( tmp_trans[2], sz ) ;
187 setTransform ( tmp_trans );
188 }
189
setTransform(sgMat4 xform)190 void ssgTransform::setTransform ( sgMat4 xform )
191 {
192 if ( sgEqualVec4( xform[0], transform[0] ) &&
193 sgEqualVec4( xform[1], transform[1] ) &&
194 sgEqualVec4( xform[2], transform[2] ) &&
195 sgEqualVec4( xform[3], transform[3] ) )
196 return;
197
198 updateTransform () ;
199 sgCopyMat4 ( transform, xform ) ;
200 firsttime () ;
201 dirtyBSphere () ;
202 }
203
204
getNetTransform(sgMat4 xform)205 void ssgTransform::getNetTransform ( sgMat4 xform )
206 {
207 if ( getNumParents () > 0 )
208 {
209 getParent ( 0 ) -> getNetTransform ( xform ) ;
210 sgPreMultMat4 ( xform, transform ) ;
211 }
212 else
213 sgCopyMat4 ( xform, transform ) ;
214 }
215
216
getLastNetTransform(sgMat4 xform)217 void ssgTransform::getLastNetTransform ( sgMat4 xform )
218 {
219 sgMat4 last_xform ;
220 getLastTransform ( last_xform ) ;
221
222 if ( getNumParents () > 0 )
223 {
224 getParent ( 0 ) -> getLastNetTransform ( xform ) ;
225 sgPreMultMat4 ( xform, last_xform ) ;
226 }
227 else
228 sgCopyMat4 ( xform, last_xform ) ;
229 }
230
231
load(FILE * fd)232 int ssgTransform::load ( FILE *fd )
233 {
234 return ssgBaseTransform::load(fd) ;
235 }
236
save(FILE * fd)237 int ssgTransform::save ( FILE *fd )
238 {
239 return ssgBaseTransform::save(fd) ;
240 }
241
242
243