View Javadoc

1   package com.insanityengine.ghia.m3;
2   
3   /***
4    *
5    * <P>
6    * Methods pertaining to like the display on the like screen and stuff 
7    * which has like width, height and maybe some kinda depth thingie.
8    * </P>
9    *
10   * @author BrianHammond
11   *
12   * $Header: /usr/local/cvsroot/ghia/src/java/com/insanityengine/ghia/m3/Frustrum.java,v 1.7 2005/03/19 17:50:02 brian Exp $
13   *
14   */
15  
16  public class Frustrum {
17  
18  	/***
19  	 *
20  	 * Constructor
21  	 *
22  	 */
23  	public Frustrum() {
24  	}
25  
26  	/***
27  	 *
28  	 * Constructor
29  	 *
30  	 * @param width of the display (in pixels)
31  	 * @param height of the display (in pixels)
32  	 * @param depth of the display (in gubertaters)
33  	 *
34  	 */
35  	public Frustrum( int width, int height ) {
36  		setSize( width, height );
37  	}
38  	
39  	/***
40  	 *
41  	 * Constructor
42  	 *
43  	 * @param width of the display (in pixels)
44  	 * @param height of the display (in pixels)
45  	 * @param depth of the display (in gubertaters)
46  	 *
47  	 */
48  	public Frustrum( int width, int height, float depth ) {
49  		setSize( width, height, depth );
50  	}
51  	
52  	/***
53  	 *
54  	 * Set the size of the display
55  	 *
56  	 * @param width of the display (in pixels)
57  	 * @param height of the display (in pixels)
58  	 *
59  	 */
60  	public final void setSize( int width, int height ) {
61  		setSize( width, height, DEFAULT_DEPTH );
62  	}
63  	
64  	/***
65  	 *
66  	 * Set the size of the display
67  	 *
68  	 * @param width of the display (in pixels)
69  	 * @param height of the display (in pixels)
70  	 * @param depth of the display (in gubertaters)
71  	 *
72  	 */
73  	public final void setSize( int width, int height, float depth ) {
74  		this.width = width;
75  		this.height = height;
76  		this.depth = depth;
77  		w2 = width / 2;
78  		h2 = height / 2;
79  	}
80  
81  	/***
82  	 *
83  	 * Set the depth of the display
84  	 *
85  	 * @param depth of the display (in gubertaters)
86  	 *
87  	 */
88  	public final void setDepth( float depth ) {
89  		this.depth = depth;
90  	}
91  
92  	/***
93  	 *
94  	 * Set the depth of the display
95  	 *
96  	 * @param depth of the display (in gubertaters)
97  	 *
98  	 */
99  	public final float getDepth() {
100 		return depth;
101 	}
102 
103 	/***
104 	 *
105 	 * Test the polygon to see if it is entirely offscreen, ie:
106 	 * outside the viewing frustrum. 
107 	 *
108 	 * @param number of points
109 	 * @param ptz in the polygon
110 	 *
111 	 * @return true if the polygon is entirely offscreen, else false
112 	 *
113 	 */
114 	public final boolean offScreen( int number, Pt3 ptz[] ) {
115 		return Frustrum.offScreen( width, height, depth, number, ptz );
116 	}
117 
118 	/***
119 	 *
120 	 * Convert from points in space to points on the screen 
121 	 * with (0,0,0) in the middle of the display
122 	 *
123 	 * @param number of points to convert
124 	 * @param ptz in the polygon
125 	 *
126 	 */
127 	public final void toScreen( int number, Pt3 ptz[] ) {
128 		toScreen( w2, h2, depth, number, ptz );
129 	}
130 
131 	/***
132 	 *
133 	 * Convert from a point in space to point on the screen 
134 	 * with (0,0,0) in the middle of the display
135 	 *
136 	 * @param point to convert
137 	 *
138 	 */
139 	public final void toScreen( Pt3 point ) {
140 		toScreen( w2, h2, point );
141 	}
142 	
143 	///////////////////////////////////////////////////////////////////
144 
145 	/***
146 	 *
147 	 * Convert from points in space to points on the screen 
148 	 * with (0,0,0) in the middle of the display
149 	 *
150 	 * @param middleX of the display (in pixels)
151 	 * @param middleY of the display (in pixels)
152 	 *
153 	 * @param number of points to convert
154 	 * @param ptz in the polygon
155 	 *
156 	 */
157 	public static final void toScreen( int middleX, int middleY, float depth, int number, Pt3 ptz[] ) {
158 		for ( int i = 0 ; i < number ; i++ ) {
159 			toScreen( middleX, middleY, depth, ptz[ i ] );
160 		}
161 	}
162 
163 	/***
164 	 *
165 	 * Convert from a point in space to point on the screen 
166 	 * with (0,0,0) in the middle of the display
167 	 *
168 	 * @param point to convert
169 	 *
170 	 */
171 	public static final void toScreen( int middleX, int middleY, float depth, Pt3 point ) {
172 //if ( point.z < 0 ) System.out.println( "" + " point.z = " + point.z  + " depth = " + depth + " zs=" + zScale( point.z, depth ) );
173 		point.x = middleX + ( point.x * middleX * 0.5f ) * zScale( point.z, depth );
174 		point.y = middleY + ( point.y * middleY * 0.5f ) * zScale( point.z, depth );
175 	}
176 
177 	/***
178 	 *
179 	 *
180 	 *
181 	 *
182 	 */
183 	public static final float zScale( float z, float depth ) {
184 		return ( ( depth - z ) / depth + 0.1f );
185 		//return 1;
186 	}
187 
188 	/***
189 	 *
190 	 * Convert from a point in space to point on the screen 
191 	 * with (0,0,0) in the middle of the display
192 	 *
193 	 * @param point to convert
194 	 *
195 	 */
196 	public static final void toScreen( int middleX, int middleY, Pt3 point ) {
197 		toScreen( middleX, middleY, DEFAULT_DEPTH, point );
198 	}
199 
200 	/***
201 	 *
202 	 * Test the polygon to see if it is entirely offscreen, ie:
203 	 * outside the viewing frustrum. 
204 	 *
205 	 * May be slightly slower than offScreen method
206 	 *
207 	 * @param width of the display (in pixels)
208 	 * @param height of the display (in pixels)
209 	 * @param depth of the display (in gubertaters)
210 	 *
211 	 * @param number of points
212 	 * @param ptz in the polygon
213 	 *
214 	 * @return true if the polygon is entirely offscreen, else false
215 	 *
216 	 */
217 	public static final boolean offScreenClassic( int width, int height, float depth, int number, Pt3 ptz[] ) {
218 		int lf_count, rt_count, up_count, dn_count, bk_count;
219 
220 		lf_count = rt_count = up_count = dn_count = bk_count = 0;
221 
222 		for ( int i = 0 ; i < number ; i++ ) {
223 			if ( ptz[ i ].x < 0 ) lf_count++;
224 			if ( ptz[ i ].x > width ) rt_count++;
225 			if ( ptz[ i ].y < 0 ) up_count++;
226 			if ( ptz[ i ].y > height ) dn_count++;
227 			if ( ptz[ i ].z < depth ) bk_count++;
228 		}
229 		
230 		return ( 
231 			false
232 			|| ( number == lf_count )
233 			|| ( number == rt_count )
234 			|| ( number == up_count )
235 			|| ( number == dn_count )
236 			|| ( number == bk_count )
237 		);
238 	}
239 
240 	//////////////////
241 
242 	/***
243 	 *
244 	 * Test if all the points are off the left side of the display
245 	 *
246 	 * @param number of points
247 	 * @param ptz in the polygon
248 	 *
249 	 * @return true if the polygon is entirely offscreen, else false
250 	 *
251 	 */
252 	public static final boolean wayLeft( int number, Pt3 ptz[] ) {
253 		for ( int i = 0 ; i < number ; i++ ) {
254 			if ( ptz[ i ].x >= 0 ) return false;
255 		}
256 		return true;
257 	}
258 		
259 	/***
260 	 *
261 	 * Test if all the points are off the right side of the display
262 	 *
263 	 * @param width of the display
264 	 *
265 	 * @param number of points
266 	 * @param ptz in the polygon
267 	 *
268 	 * @return true if the polygon is entirely offscreen, else false
269 	 *
270 	 */
271 	public static final boolean wayRight( int width, int number, Pt3 ptz[] ) {
272 		for ( int i = 0 ; i < number ; i++ ) {
273 			if ( ptz[ i ].x < width ) return false;
274 		}
275 		return true;
276 	}
277 	
278 	/***
279 	 *
280 	 * Test if all the points are off the top side of the display
281 	 *
282 	 * @param number of points
283 	 * @param ptz in the polygon
284 	 *
285 	 * @return true if the polygon is entirely offscreen, else false
286 	 *
287 	 */
288 	public static final boolean wayUp( int number, Pt3 ptz[] ) {
289 		for ( int i = 0 ; i < number ; i++ ) {
290 			if ( ptz[ i ].y >= 0 ) return false;
291 		}
292 		return true;
293 	}
294 	
295 	/***
296 	 *
297 	 * Test if all the points are off the bottom side of the display
298 	 *
299 	 * @param height of the display
300 	 *
301 	 * @param number of points
302 	 * @param ptz in the polygon
303 	 *
304 	 * @return true if the polygon is entirely offscreen, else false
305 	 *
306 	 */
307 	public static final boolean wayDown( int height, int number, Pt3 ptz[] ) {
308 		for ( int i = 0 ; i < number ; i++ ) {
309 			if ( ptz[ i ].y < height ) return false;
310 		}
311 		return true;
312 	}
313 
314 	/***
315 	 *
316 	 * Test if all the points are off the back side of the display
317 	 *
318 	 * @param depth of the display
319 	 *
320 	 * @param number of points
321 	 * @param ptz in the polygon
322 	 *
323 	 * @return true if the polygon is entirely offscreen, else false
324 	 *
325 	 */
326 	public static final boolean wayBack( float depth, int number, Pt3 ptz[] ) {
327 		for ( int i = 0 ; i < number ; i++ ) {
328 			if ( ptz[ i ].z > depth ) return false;
329 		}
330 		return true;
331 	}
332 	
333 	/***
334 	 *
335 	 * Test if all the points are off the back side of the display
336 	 *
337 	 * @param depth of the display
338 	 *
339 	 * @param number of points
340 	 * @param ptz in the polygon
341 	 *
342 	 * @return true if the polygon is entirely offscreen, else false
343 	 *
344 	 */
345 	public static final boolean wayFront( int number, Pt3 ptz[] ) {
346 		for ( int i = 0 ; i < number ; i++ ) {
347 			if ( ptz[ i ].z < 0 ) return false;
348 		}
349 		return true;
350 	}
351 
352 	/***
353 	 *
354 	 * Test the polygon to see if it is entirely offscreen, ie:
355 	 * outside the viewing frustrum. 
356 	 *
357 	 * May be slightly faster than offScreenClassic method
358 	 *
359 	 * @param width of the display (in pixels)
360 	 * @param height of the display (in pixels)
361 	 * @param depth of the display (in gubertaters)
362 	 *
363 	 * @param number of points
364 	 * @param ptz in the polygon
365 	 *
366 	 * @return true if the polygon is entirely offscreen, else false
367 	 *
368 	 */
369 	public static final boolean offScreen( int width, int height, float depth, int number, Pt3 ptz[] ) {
370 		return (
371 			wayLeft( number, ptz ) 
372 			||
373 			wayRight( width, number, ptz ) 
374 			||
375 			wayUp( number, ptz ) 
376 			||
377 			wayDown( height, number, ptz ) 
378 /*
379 			||
380 			wayBack( depth, number, ptz ) 
381 			||
382 			wayFront( number, ptz ) 
383 */
384 		);
385 	}
386 	
387 	///////////////////////////////////////////////////
388 
389 	private int width = 0;
390 	private int height = 0;
391 	private int w2 = 0, h2 = 0;
392 	private float depth = DEFAULT_DEPTH;//M3_Constants.bigNumber;
393 
394 	public static final int DEFAULT_DEPTH = -1000;
395 
396 };
397 
398 /***
399  *
400  * $Log: Frustrum.java,v $
401  * Revision 1.7  2005/03/19 17:50:02  brian
402  * repackaging
403  *
404  * Revision 1.6  2005/02/17 03:31:13  brian
405  * good stuff???
406  *
407  * Revision 1.5  2004/09/13 03:06:41  brian
408  * fix up Frustrum.toScreen, still working on JoglRenderer's texture support... sigh...
409  *
410  * Revision 1.4  2004/09/11 14:37:21  brian
411  * added RendererConsumer and stuff
412  *
413  * Revision 1.3  2004/09/01 01:10:42  brian
414  * fix class level javadoc placement
415  *
416  * Revision 1.2  2004/09/01 00:11:06  brian
417  * author, log and header stuff
418  *
419  *
420  */