View Javadoc

1   package com.insanityengine.ghia.apps;
2   
3   import java.applet.*;
4   
5   import java.awt.*;
6   import java.awt.image.*;
7   import java.awt.event.*;
8   
9   import javax.swing.*;
10  import javax.swing.event.*;
11  
12  import java.util.*;
13  
14  import com.insanityengine.ghia.m3.*;
15  import com.insanityengine.ghia.util.*;
16  import com.insanityengine.ghia.pixels.*;
17  import com.insanityengine.ghia.libograf.*;
18  
19  /***
20   *
21   * <P>
22   * This play pretty is that kinda pipe screen saver thing you like.
23   * It needs to be used with a renderer which implements a real pixel
24   * buffer type interface...
25   * </P>
26   *
27   * <IMG SRC="http://ghia.sourceforge.net/images/old_snaps/Piping.gif"/>
28   *
29   * @author BrianHammond
30   *
31   */
32  
33  public class Piping extends AppletBase {
34  
35  	/////////////////////////////////////////////////////////////
36  
37  	public final static void main( String argv[] ) {
38  		Piping lite = new Piping();
39  		lite.frame( 600, 600 );
40  	}
41  	
42  	/////////////////////////////////////////////////////////////
43  	
44  	public Piping() {
45  		initCirclePoints();
46  		reset();
47  	}
48  	
49  	/////////////////////////////////////////////////////////////
50  	
51  	public void initImpl( int w, int h ) {
52  		gl.getRenderer().setShadingMethod( M3_Constants.NORM_SHADE );
53  		gl.getRenderer().clear();
54  	}
55  
56  	public final void drawImpl() {
57  		
58  		gl.identity();
59  		gl.rotate( rotation );
60  		gl.scale( scale );
61  
62  		pipe.draw( gl );
63  
64  		if ( paused ) {
65  			reset();
66  			paused = false;
67  		}
68  	}
69  
70  	public final void reset() {
71  		if ( null != gl ) gl.getRenderer().clear();
72  		scale.set( ( float ) ( 0.01 + 0.05 * Math.random() ) );
73  		rotation.set(
74  			( float ) ( Math.PI * 2 * Math.random() )
75  			,
76  			( float ) ( Math.PI * 2 * Math.random() )
77  			,
78  			( float ) ( Math.PI * 2 * Math.random() )
79  		);
80  		pipe.reset();
81  	}
82  
83  	////////////////////////////////////////////////////////////
84  
85  	Piper pipe = new Piper();
86  	private static Pt2 circlePts [] = null;
87  	private Pt3 scale = new Pt3();
88  	private Pt3 rotation = new Pt3();
89  
90  	////////////////////////////////////////////////////////////
91  	
92  	private void initCirclePoints() {
93  		if ( null == circlePts ) {
94  
95  			circlePts = new Pt2[ 16 ];
96  			int i = 0;
97  			double rad = 1f;
98  			double inc = Math.PI * 2 / circlePts.length;
99  
100 			for ( double f = 0 ; f < Math.PI * 2 ; f += inc, ++i ) {
101 				System.out.println( "" + " f = " + f );
102 
103 				circlePts[ i ] = new Pt2(
104 					rad * Math.cos( f )
105 					,
106 					rad * Math.sin( f )
107 				);
108 			}
109 		}
110 	}
111 
112 	////////////////////////////////////////////////////////////
113 
114 	/***
115 	 *
116 	 * This class is used to draw the pipes
117 	 *
118 	 */
119 	private class Piper {
120 
121 		/***
122 		 *
123 		 * Constructor 
124 		 *
125 		 */
126 		public Piper() {
127 			int[] sk = { 0x000000FF };
128 			skin = new ImageSkin( 1, 1, sk );
129 		}
130 
131 		public void reset() {
132 			count = 0;
133 		}
134 
135 		/***
136 		 *
137 		 * Reset to a random point and direction
138 		 *
139 		 */
140 		public void reset( LiboGrafInterface gl ) {
141 			mat.identity();
142 			
143 			// translate to some point
144 			mat.translate( 
145 				new Pt3(
146 					( float ) ( Math.random() - Math.random() ),
147 					( float ) ( Math.random() - Math.random() ),
148 					( float ) ( Math.random() - Math.random() )
149 				)
150 			);
151 			
152 			// random color
153 			skin.getPixels()[ 0 ] = ( int ) ( 0x00FFFFFF * Math.random() );
154 
155 			// for some period of time
156 			count = Stdlib.irand() % 20 + 10;
157 
158 			// pick a random angle
159 			newAngle( gl );
160 		}
161 
162 		/***
163 		 *
164 		 * Turn 90 degrees in a random dimension
165 		 *
166 		 */
167 		private void newAngle( LiboGrafInterface gl ) {
168 
169 			// 80% chance of a real change...
170 			if ( 0.80f > Math.random() ) {
171 			
172 				float f = ( 44.0f / 7.0f * 0.25f ) * ( Stdlib.rand() % 4 );
173 				
174 				float currx, curry, currz;
175 				currx = curry = currz = 0;
176 
177 				int next = Stdlib.irand() % 3;
178 				if ( -1 != last && last != next ) {
179 					com.insanityengine.ghia.GeomLoaded.GeomLoadedSphere2.staticDraw( gl );
180 				}
181 
182 				switch( next ) {
183 					case 0: currx = f; break;
184 					case 1: curry = f; break;
185 					case 2: currz = f; break;
186 				}
187 
188 				last = next;
189 
190 				// point some 90 degree angle
191 				mat.rotate( new Pt3( currx, curry, currz ) );
192 				
193 				if ( 0 == Stdlib.irand() % 2 ) inc *= -1;
194 			}
195 		}
196 
197 		/***
198 		 *
199 		 * Draw the pipe and move on
200 		 *
201 		 * @param gl context to draw in
202 		 *
203 		 */
204 		public void draw( LiboGrafInterface gl ) {
205 
206 			inc = ( float ) ( 10 + Math.random() * 30 );
207 			
208 			if ( 0 >= count ) reset( gl );
209 
210 			gl.multiply( mat );
211 
212 			gl.getRenderer().setSkin( skin );
213 
214 			drawPipe( gl );
215 	
216 			// move on...
217 			mat.translate( new Pt3( 0, 0, inc ) );
218 			
219 			// change direction
220 			newAngle( gl );
221 
222 			count--;
223 		}
224 
225 		/***
226 		 *
227 		 * Draw the pipe using the LiboGrafInterface
228 		 *
229 		 * @param gl to draw to
230 		 *
231 		 */
232 		private void drawPipe( LiboGrafInterface gl ) {
233 			
234 			for ( int i = 0, j ; i < circlePts.length ; i++ ) {
235 				j = ( circlePts.length == i ) ? ( i + 1 ) : ( 0 );
236 				
237 				gl.startPolygon();
238 					ptIt( gl, circlePts[ i ].getX(), circlePts[ i ].getY(), 0 );
239 					ptIt( gl, circlePts[ j ].getX(), circlePts[ j ].getY(), 0 );
240 					ptIt( gl, circlePts[ j ].getX(), circlePts[ j ].getY(), inc );
241 					ptIt( gl, circlePts[ i ].getX(), circlePts[ i ].getY(), inc );
242 				gl.stopPolygon();
243 			}
244 		}
245 
246 		/***
247 		 *
248 		 * Add a point to the LiboGrafInterface
249 		 *
250 		 * @param gl to addPoint to
251 		 * @param x coordinate
252 		 * @param y coordinate
253 		 * @param z coordinate
254 		 *
255 		 */
256 		private void ptIt( LiboGrafInterface gl, double x, double y, double z ) {
257 			pt.set( ( float ) x, ( float ) y, ( float ) z );
258 			gl.addPoint( pt );
259 		}
260 
261 		//////////////////////////////////////////////////////
262 
263 		private Pt3 pt = new Pt3();
264 	
265 		private int last = -1;	
266 		private int count = 0;
267 		private float inc = 10.0f;
268 
269 		private ImageSkin skin = null;
270 
271 		private Matrix mat = new Matrix();
272 	};
273 	
274 };
275 
276 /***
277  *
278  * $Log: Piping.java,v $
279  * Revision 1.6  2005/03/26 23:42:41  brian
280  * fix links
281  *
282  * Revision 1.5  2005/03/19 17:50:02  brian
283  * repackaging
284  *
285  * Revision 1.4  2005/03/11 23:35:31  brian
286  * major refactoring to add in com.insanityengine.ghia.libograf.State bitz
287  *
288  * Revision 1.3  2004/09/27 00:26:19  brian
289  * added image link to dox
290  *
291  * Revision 1.2  2004/09/22 03:17:15  brian
292  * be less lame...
293  *
294  *
295  */