1 package com.insanityengine.ghia.pixels;
2
3 import java.awt.*;
4 import java.awt.image.*;
5 import java.awt.event.*;
6 import java.applet.*;
7
8 import com.insanityengine.ghia.m3.*;
9 import com.insanityengine.ghia.libograf.*;
10
11 /***
12 *
13 * <P>
14 * This provides a decent implements of most of RendererInterface
15 * with the exception of the <i>drawPolygon</i> method. It combines
16 * a PixelImage with a ZeeBuffer to provide core rendering functionality.
17 * </P>
18 *
19 * @author BrianHammond
20 *
21 * $Header: /usr/local/cvsroot/ghia/src/java/com/insanityengine/ghia/pixels/BufferBase.java,v 1.3 2005/03/19 17:50:02 brian Exp $
22 *
23 */
24
25 public class BufferBase extends PixelImage {
26
27
28
29 /***
30 *
31 * Constructor
32 *
33 */
34 public BufferBase() {}
35
36 /***
37 *
38 * Initialize the BufferBase, the component is not critical
39 *
40 * @param width of the display
41 * @param height of the display
42 * @param component to use to create offscreen buffer
43 *
44 */
45 public void init( int width, int height, Component component ) {
46 init( width, height );
47 }
48
49 /***
50 *
51 * Initialize the BufferBase, the component is not critical
52 *
53 * @param width of the display
54 * @param height of the display
55 *
56 */
57 public void init( int width, int height ) {
58 super.init( width, height );
59 zbuffer.init( width, height );
60 frustrum.setSize( width, height );
61 }
62
63 /***
64 *
65 * Draw a polygon
66 *
67 * @param count of points
68 * @param ptz the array of Pt3 to draw
69 *
70 */
71 public void drawPolygon( int pt_idx, Pt3 ptz[] ) {
72
73 }
74
75 /***
76 *
77 * Set the current ImageSkin
78 *
79 * @param skin to use
80 *
81 */
82 public final void setSkin( ImageSkin skin ) {
83 this.skin = skin;
84 }
85
86 /***
87 *
88 * Set the current normal
89 *
90 * @param point to use as the current normal
91 *
92 */
93 public final void setNormal( Pt3 n ) {
94 normal.set( n );
95 }
96
97 /***
98 *
99 * Set the far value for the zbuffer
100 *
101 * @param z to use as far value for the ZeeBuffer
102 *
103 */
104 public final void setZFar( float z ) {
105 zbuffer.setFar( z );
106 }
107
108 /***
109 *
110 * Get the far value for the zbuffer
111 *
112 * @return the far value for the zbuffer
113 *
114 */
115 public final float getZFar() {
116 return zbuffer.getFar();
117 }
118
119 /***
120 *
121 * Set blending policy
122 *
123 * @param blend or not...
124 *
125 */
126 public final void setBlending( boolean blend ) {
127 blending = blend;
128 }
129
130 /***
131 *
132 * Set depth testing policy
133 *
134 * @param testDepth false disables test
135 *
136 */
137 public final void setDepthTesting( boolean b ) {
138 depthtest = b;
139 }
140
141 /***
142 *
143 * Set the shading method
144 *
145 * @param method may be listed in M3_Constants
146 *
147 */
148 public final void setShadingMethod( int n ) {
149 shade_method = n;
150 if ( M3_Constants.PHONG_SHADE == n && null == phongo ) {
151 initPhongShading( 64 );
152 }
153 }
154
155 /***
156 *
157 * Set the texture function
158 *
159 * @param function may be listed in M3_Constants
160 *
161 */
162 public final void setTextureFunction( int funk ) {
163 if ( null != skin ) skin.setFunction( funk );
164 }
165
166 /***
167 *
168 * Set the pixel at index to color if this is the closes z
169 * value so far
170 *
171 * @param index to try to set
172 * @param z value to try with
173 * @param color to use (ARGB)
174 *
175 */
176 public final void setColor( int index, float z, int color ) {
177 if ( zbuffer.set( index, z ) ) super.setColor( index, color );
178 }
179
180 /***
181 *
182 * Set the pixel at index corresponding to (x,y) to color
183 * if this is the closes z value so far
184 *
185 * @param x coordinate
186 * @param y coordinate
187 * @param z value to try with
188 * @param color to use (ARGB)
189 *
190 */
191 public final void setColor( float x, float y, float z, int color ) {
192 setColor( ( int ) ( x + y * w ), color );
193 }
194
195 /***
196 *
197 * Set the pixel at index corresponding to (x,y) to color
198 * if this is the closes z value so far
199 *
200 * @param x coordinate
201 * @param y coordinate
202 * @param z value to try with
203 * @param alpha color component
204 * @param red color component
205 * @param green color component
206 * @param blue color component
207 *
208 */
209 public final void setColor( float x, float y, float z, int alpha, int red, int green, int blue ) {
210 setColor(
211 x,
212 y,
213 z,
214 alpha << 24 | red << 16 | green << 8 | blue
215 );
216 }
217
218 /***
219 *
220 * Clear the depth buffer
221 *
222 */
223 public final void clearDepthBuffer() {
224 zbuffer.clear();
225 }
226
227 /***
228 *
229 * Clear the pixel/color buffer
230 *
231 */
232 public final void clearColorBuffer() {
233 super.clear();
234 }
235
236 /***
237 *
238 * Clear the buffer
239 *
240 */
241 public final void clear() {
242 clearDepthBuffer();
243 clearColorBuffer();
244 }
245
246
247
248 private Pt3 tri_lame[] = new Pt3[ 3 ];
249
250 private boolean blending = false;
251
252 private boolean depthtest = true;
253
254
255
256 protected Pt3 normal = new Pt3();
257
258 protected int phongo[] = null;
259 protected int shade_method = M3_Constants.NONE_SHADE;
260 protected int textFunk = M3_Constants.TEXT_WRAP;
261
262 protected ImageSkin skin = null;
263 protected ZeeBuffer zbuffer = new ZeeBuffer();
264 protected Frustrum frustrum = new Frustrum();
265
266
267
268 private final void initPhongShading( int n ) {
269 phongo = new int[ 256 ];
270
271 int clrL = ( int ) 232;
272 int clrI = ( int ) ( 255 - clrL );
273
274 int phF1 = ( int ) ( 255 - n );
275 int phF2 = ( int ) ( n - 255 );
276
277 for ( int i = 0 ; i < phF1 ; i++ ) {
278 phongo[ i ] = ( int ) ( clrL * i / phF1 );
279 }
280 for ( int i = phF1 ; i < 256 ; i++ ) {
281 phongo[ i ] = ( int ) (
282 clrL + ( clrI * ( i + phF2 ) / n )
283 );
284 }
285 }
286 };
287
288 /***
289 *
290 * $Log: BufferBase.java,v $
291 * Revision 1.3 2005/03/19 17:50:02 brian
292 * repackaging
293 *
294 * Revision 1.2 2005/03/12 04:58:47 brian
295 * call the methods set instead of copy
296 *
297 * Revision 1.1 2004/09/02 13:17:16 brian
298 * the big reorg
299 *
300 * Revision 1.15 2004/09/01 01:10:42 brian
301 * fix class level javadoc placement
302 *
303 * Revision 1.14 2004/09/01 00:11:06 brian
304 * author, log and header stuff
305 *
306 *
307 */