001 package com.croftsoft.core.media.jogl.render;
002
003 import javax.media.opengl.GL;
004
005 import com.croftsoft.core.lang.NullException;
006 import com.croftsoft.core.lang.lifecycle.Updatable;
007 import com.croftsoft.core.math.MathLib;
008 import com.croftsoft.core.media.jogl.JoglRenderer;
009 import com.croftsoft.core.media.jogl.JoglLib;
010
011 /***********************************************************************
012 * Displays a spinning colored cube.
013 *
014 * The angular velocity is subjected to uniform random accelerations.
015 * The maximum magnitude of the random accelerations is set by the
016 * perturbation factor.
017 *
018 * @version
019 * $Date: 2008/05/17 01:32:43 $
020 * @since
021 * 2005-08-15
022 * @author
023 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
024 ***********************************************************************/
025
026 public final class JoglSpinCube
027 implements JoglRenderer
028 ////////////////////////////////////////////////////////////////////////
029 ////////////////////////////////////////////////////////////////////////
030 ////////////////////////////////////////////////////////////////////////
031 {
032
033 public static interface Model
034 ////////////////////////////////////////////////////////////////////////
035 ////////////////////////////////////////////////////////////////////////
036 {
037
038 double getX ( );
039
040 double getY ( );
041
042 double getZ ( );
043
044 double getRotX ( );
045
046 double getRotY ( );
047
048 double getRotZ ( );
049
050 ////////////////////////////////////////////////////////////////////////
051 ////////////////////////////////////////////////////////////////////////
052 }
053
054 public static final class ModelImp
055 implements Model, Updatable
056 ////////////////////////////////////////////////////////////////////////
057 ////////////////////////////////////////////////////////////////////////
058 {
059
060 private static final double
061 DELTA_MAX_INIT = 1,
062 PERTURBATION_FACTOR_INIT = 0.01;
063
064 private double
065 deltaMax,
066 deltaRotX,
067 deltaRotY,
068 deltaRotZ,
069 rotX,
070 rotY,
071 rotZ,
072 perturbationFactor,
073 x,
074 y,
075 z;
076
077 ////////////////////////////////////////////////////////////////////////
078 ////////////////////////////////////////////////////////////////////////
079
080 public ModelImp ( )
081 ////////////////////////////////////////////////////////////////////////
082 {
083 deltaMax = DELTA_MAX_INIT;
084
085 perturbationFactor = PERTURBATION_FACTOR_INIT;
086 }
087
088 ////////////////////////////////////////////////////////////////////////
089 // lifecycle methods
090 ////////////////////////////////////////////////////////////////////////
091
092 public void update ( )
093 ////////////////////////////////////////////////////////////////////////
094 {
095 deltaRotX += perturbationFactor * ( 2 * Math.random ( ) - 1 );
096
097 deltaRotY += perturbationFactor * ( 2 * Math.random ( ) - 1 );
098
099 deltaRotZ += perturbationFactor * ( 2 * Math.random ( ) - 1 );
100
101 deltaRotX = MathLib.clip ( deltaRotX, -deltaMax, deltaMax );
102
103 deltaRotY = MathLib.clip ( deltaRotY, -deltaMax, deltaMax );
104
105 deltaRotZ = MathLib.clip ( deltaRotZ, -deltaMax, deltaMax );
106
107 rotX = MathLib.wrap ( rotX + deltaRotX, 0, 360 );
108
109 rotY = MathLib.wrap ( rotY + deltaRotY, 0, 360 );
110
111 rotZ = MathLib.wrap ( rotZ + deltaRotZ, 0, 360 );
112 }
113
114 ////////////////////////////////////////////////////////////////////////
115 // accessor / mutator methods
116 ////////////////////////////////////////////////////////////////////////
117
118 public double getDeltaMax ( )
119 {
120 return deltaMax;
121 }
122
123 public void setDeltaMax ( double deltaMax )
124 {
125 this.deltaMax = deltaMax;
126 }
127
128 public double getDeltaRotX ( )
129 {
130 return deltaRotX;
131 }
132
133 public void setDeltaRotX ( double deltaRotX )
134 {
135 this.deltaRotX = deltaRotX;
136 }
137
138 public double getDeltaRotY ( )
139 {
140 return deltaRotY;
141 }
142
143 public void setDeltaRotY ( double deltaRotY )
144 {
145 this.deltaRotY = deltaRotY;
146 }
147
148 public double getDeltaRotZ ( )
149 {
150 return deltaRotZ;
151 }
152
153 public void setDeltaRotZ ( double deltaRotZ )
154 {
155 this.deltaRotZ = deltaRotZ;
156 }
157
158 public double getRotX ( )
159 {
160 return rotX;
161 }
162
163 public void setRotX ( double rotX )
164 {
165 this.rotX = rotX;
166 }
167
168 public double getRotY ( )
169 {
170 return rotY;
171 }
172
173 public void setRotY ( double rotY )
174 {
175 this.rotY = rotY;
176 }
177
178 public double getRotZ ( )
179 {
180 return rotZ;
181 }
182
183 public void setRotZ ( double rotZ )
184 {
185 this.rotZ = rotZ;
186 }
187
188 public double getPerturbationFactor ( )
189 {
190 return perturbationFactor;
191 }
192
193 public void setPerturbationFactor ( double perturbationFactor )
194 {
195 this.perturbationFactor = perturbationFactor;
196 }
197
198 public double getX ( )
199 {
200 return x;
201 }
202
203 public void setX ( double x )
204 {
205 this.x = x;
206 }
207
208 public double getY ( )
209 {
210 return y;
211 }
212
213 public void setY ( double y )
214 {
215 this.y = y;
216 }
217
218 public double getZ ( )
219 {
220 return z;
221 }
222
223 public void setZ ( double z )
224 {
225 this.z = z;
226 }
227
228 ////////////////////////////////////////////////////////////////////////
229 // more mutator methods
230 ////////////////////////////////////////////////////////////////////////
231
232 public void setAngularDisplacement (
233 final double rotX,
234 final double rotY,
235 final double rotZ )
236 ////////////////////////////////////////////////////////////////////////
237 {
238 this.rotX = rotX;
239
240 this.rotY = rotY;
241
242 this.rotZ = rotZ;
243 }
244
245 public void setAngularVelocity (
246 final double deltaRotX,
247 final double deltaRotY,
248 final double deltaRotZ )
249 ////////////////////////////////////////////////////////////////////////
250 {
251 this.deltaRotX = deltaRotX;
252
253 this.deltaRotY = deltaRotY;
254
255 this.deltaRotZ = deltaRotZ;
256 }
257
258 public void setMaximumAngularVelocity ( final double deltaMax )
259 ////////////////////////////////////////////////////////////////////////
260 {
261 this.deltaMax = deltaMax;
262 }
263
264 public void setXYZ (
265 final double x,
266 final double y,
267 final double z )
268 ////////////////////////////////////////////////////////////////////////
269 {
270 this.x = x;
271
272 this.y = y;
273
274 this.z = z;
275 }
276
277 ////////////////////////////////////////////////////////////////////////
278 ////////////////////////////////////////////////////////////////////////
279 }
280
281 ////////////////////////////////////////////////////////////////////////
282 ////////////////////////////////////////////////////////////////////////
283
284 private final Model model;
285
286 //
287
288 private int cubeDisplayList;
289
290 ////////////////////////////////////////////////////////////////////////
291 // constructor methods
292 ////////////////////////////////////////////////////////////////////////
293
294 /***********************************************************************
295 * Main constructor.
296 ***********************************************************************/
297 public JoglSpinCube ( final Model model )
298 ////////////////////////////////////////////////////////////////////////
299 {
300 NullException.check ( this.model = model );
301 }
302
303 ////////////////////////////////////////////////////////////////////////
304 // interface JoglDisplayable method
305 ////////////////////////////////////////////////////////////////////////
306
307 public void init ( final GL gl )
308 ////////////////////////////////////////////////////////////////////////
309 {
310 cubeDisplayList = JoglLib.createCubeDisplayList ( gl );
311 }
312
313 public void destroy ( final GL gl )
314 ////////////////////////////////////////////////////////////////////////
315 {
316 gl.glDeleteLists ( cubeDisplayList, 1 );
317 }
318
319 public void setBounds (
320 final GL gl,
321 final int x,
322 final int y,
323 final int width,
324 final int height )
325 ////////////////////////////////////////////////////////////////////////
326 {
327 // empty
328 }
329
330 public void render ( final GL gl )
331 ////////////////////////////////////////////////////////////////////////
332 {
333 gl.glPushAttrib ( GL.GL_ALL_ATTRIB_BITS );
334
335 gl.glEnable ( GL.GL_DEPTH_TEST );
336
337 gl.glMatrixMode ( GL.GL_MODELVIEW );
338
339 gl.glPushMatrix ( );
340
341 gl.glTranslated (
342 model.getX ( ),
343 model.getY ( ),
344 model.getZ ( ) );
345
346 gl.glRotated ( model.getRotX ( ), 1, 0, 0 );
347
348 gl.glRotated ( model.getRotY ( ), 0, 1, 0 );
349
350 gl.glRotated ( model.getRotZ ( ), 0, 0, 1 );
351
352 gl.glCallList ( cubeDisplayList );
353
354 gl.glPopMatrix ( );
355
356 gl.glPopAttrib ( );
357 }
358
359 ////////////////////////////////////////////////////////////////////////
360 ////////////////////////////////////////////////////////////////////////
361 ////////////////////////////////////////////////////////////////////////
362 }