1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
/*
* Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
package javax.media.j3d;
/**
* The PolygonAttributes object defines attributes for rendering polygon
* primitives.
* Polygon primitives include triangles, triangle strips, triangle fans,
* and quads.
* The polygon attributes that can be defined are:</li>
* <p><ul>
* <li>Rasterization mode - defines how the polygon is drawn: as points,
* outlines, or filled.<p>
* <ul>
* <li>POLYGON_POINT - the polygon is rendered as points
* drawn at the vertices.</li><p>
* <li>POLYGON_LINE - the polygon is rendered as lines
* drawn between consecutive vertices.</li><p>
* <li>POLYGON_FILL - the polygon is rendered by filling the interior
* between the vertices. The default mode.</li>
* <p></ul>
* <li>Face culling - defines which polygons are culled (discarded)
* before they are converted to screen coordinates.<p>
* <ul>
* <li>CULL_NONE - disables face culling.</li>
* <li>CULL_BACK - culls all back-facing polygons. The default.</li>
* <li>CULL_FRONT - culls all front-facing polygons.</li>
* <p></ul>
* <li>Back-face normal flip - specifies whether vertex normals of
* back-facing polygons are flipped (negated) prior to lighting. The
* setting is either true, meaning to flip back-facing normals, or
* false. The default is false.</li>
* <p>
* <li>Offset - the depth values of all pixels generated by polygon
* rasterization can be offset by a value that is computed for that
* polygon. Two values are used to specify the offset:</li><p>
* <ul>
* <li>Offset bias - the constant polygon offset that is added to
* the final device coordinate Z value of polygon primitives.</li>
* <p>
* <li>Offset factor - the factor to be multiplied by the
* slope of the polygon and then added to the final, device coordinate
* Z value of the polygon primitives.</li><p>
* </ul>
* These values can be either positive or negative. The default
* for both of these values is 0.0.<p>
* </ul>
*
* @see Appearance
*/
public class PolygonAttributes extends NodeComponent {
/**
* Specifies that this PolygonAttributes object allows reading its
* cull face information.
*/
public static final int
ALLOW_CULL_FACE_READ = CapabilityBits.POLYGON_ATTRIBUTES_ALLOW_CULL_FACE_READ;
/**
* Specifies that this PolygonAttributes object allows writing its
* cull face information.
*/
public static final int
ALLOW_CULL_FACE_WRITE = CapabilityBits.POLYGON_ATTRIBUTES_ALLOW_CULL_FACE_WRITE;
/**
* Specifies that this PolygonAttributes object allows reading its
* back face normal flip flag.
*/
public static final int
ALLOW_NORMAL_FLIP_READ = CapabilityBits.POLYGON_ATTRIBUTES_ALLOW_NORMAL_FLIP_READ;
/**
* Specifies that this PolygonAttributes object allows writing its
* back face normal flip flag.
*/
public static final int
ALLOW_NORMAL_FLIP_WRITE = CapabilityBits.POLYGON_ATTRIBUTES_ALLOW_NORMAL_FLIP_WRITE;
/**
* Specifies that this PolygonAttributes object allows reading its
* polygon mode information.
*/
public static final int
ALLOW_MODE_READ = CapabilityBits.POLYGON_ATTRIBUTES_ALLOW_MODE_READ;
/**
* Specifies that this PolygonAttributes object allows writing its
* polygon mode information.
*/
public static final int
ALLOW_MODE_WRITE = CapabilityBits.POLYGON_ATTRIBUTES_ALLOW_MODE_WRITE;
/**
* Specifies that this PolygonAttributes object allows reading its
* polygon offset information.
*/
public static final int
ALLOW_OFFSET_READ = CapabilityBits.POLYGON_ATTRIBUTES_ALLOW_OFFSET_READ;
/**
* Specifies that this PolygonAttributes object allows writing its
* polygon offset information.
*/
public static final int
ALLOW_OFFSET_WRITE = CapabilityBits.POLYGON_ATTRIBUTES_ALLOW_OFFSET_WRITE;
// Polygon rasterization modes
/**
* Render polygonal primitives as points drawn at the vertices
* of the polygon.
*/
public static final int POLYGON_POINT = 0;
/**
* Render polygonal primitives as lines drawn between consecutive
* vertices of the polygon.
*/
public static final int POLYGON_LINE = 1;
/**
* Render polygonal primitives by filling the interior of the polygon.
*/
public static final int POLYGON_FILL = 2;
/**
* Don't perform any face culling.
*/
public static final int CULL_NONE = 0;
/**
* Cull all back-facing polygons. This is the default mode.
*/
public static final int CULL_BACK = 1;
/**
* Cull all front-facing polygons.
*/
public static final int CULL_FRONT = 2;
// Array for setting default read capabilities
private static final int[] readCapabilities = {
ALLOW_CULL_FACE_READ,
ALLOW_MODE_READ,
ALLOW_NORMAL_FLIP_READ,
ALLOW_OFFSET_READ
};
/**
* Constructs a PolygonAttributes object with default parameters.
* The default values are as follows:
* <ul>
* cull face : CULL_BACK<br>
* back face normal flip : false<br>
* polygon mode : POLYGON_FILL<br>
* polygon offset : 0.0<br>
* polygon offset factor : 0.0<br>
* </ul>
*/
public PolygonAttributes() {
// Just use defaults for all attributes
// set default read capabilities
setDefaultReadCapabilities(readCapabilities);
}
/**
* Constructs a PolygonAttributes object with specified values.
* @param polygonMode polygon rasterization mode; one of POLYGON_POINT,
* POLYGON_LINE, or POLYGON_FILL
* @param cullFace polygon culling mode; one of CULL_NONE,
* CULL_BACK, or CULL_FRONT
* @param polygonOffset constant polygon offset
*/
public PolygonAttributes(int polygonMode,
int cullFace,
float polygonOffset) {
this(polygonMode, cullFace, polygonOffset, false, 0.0f);
}
/**
* Constructs PolygonAttributes object with specified values.
* @param polygonMode polygon rasterization mode; one of POLYGON_POINT,
* POLYGON_LINE, or POLYGON_FILL
* @param cullFace polygon culling mode; one of CULL_NONE,
* CULL_BACK, or CULL_FRONT
* @param polygonOffset constant polygon offset
* @param backFaceNormalFlip back face normal flip flag; true or false
*/
public PolygonAttributes(int polygonMode,
int cullFace,
float polygonOffset,
boolean backFaceNormalFlip) {
this(polygonMode, cullFace, polygonOffset, backFaceNormalFlip, 0.0f);
}
/**
* Constructs PolygonAttributes object with specified values.
* @param polygonMode polygon rasterization mode; one of POLYGON_POINT,
* POLYGON_LINE, or POLYGON_FILL
* @param cullFace polygon culling mode; one of CULL_NONE,
* CULL_BACK, or CULL_FRONT
* @param polygonOffset constant polygon offset
* @param backFaceNormalFlip back face normal flip flag; true or false
* @param polygonOffsetFactor polygon offset factor for slope-based polygon
* offset
*
* @since Java 3D 1.2
*/
public PolygonAttributes(int polygonMode,
int cullFace,
float polygonOffset,
boolean backFaceNormalFlip,
float polygonOffsetFactor) {
if (polygonMode < POLYGON_POINT || polygonMode > POLYGON_FILL)
throw new IllegalArgumentException(J3dI18N.getString("PolygonAttributes0"));
if (cullFace < CULL_NONE || cullFace > CULL_FRONT)
throw new IllegalArgumentException(J3dI18N.getString("PolygonAttributes12"));
// set default read capabilities
setDefaultReadCapabilities(readCapabilities);
((PolygonAttributesRetained)this.retained).initPolygonMode(polygonMode);
((PolygonAttributesRetained)this.retained).initCullFace(cullFace);
((PolygonAttributesRetained)this.retained).initPolygonOffset(polygonOffset);
((PolygonAttributesRetained)this.retained).initBackFaceNormalFlip(backFaceNormalFlip);
((PolygonAttributesRetained)this.retained).initPolygonOffsetFactor(polygonOffsetFactor);
}
/**
* Sets the face culling for this
* appearance component object.
* @param cullFace the face to be culled, one of:
* CULL_NONE, CULL_FRONT, or CULL_BACK
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
public void setCullFace(int cullFace) {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_CULL_FACE_WRITE))
throw new CapabilityNotSetException(J3dI18N.getString("PolygonAttributes2"));
if (cullFace < CULL_NONE || cullFace > CULL_FRONT)
throw new IllegalArgumentException(J3dI18N.getString("PolygonAttributes3"));
if (isLive())
((PolygonAttributesRetained)this.retained).setCullFace(cullFace);
else
((PolygonAttributesRetained)this.retained).initCullFace(cullFace);
}
/**
* Gets the face culling for this
* appearance component object.
* @return the face to be culled
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
public int getCullFace() {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_CULL_FACE_READ))
throw new CapabilityNotSetException(J3dI18N.getString("PolygonAttributes4"));
return ((PolygonAttributesRetained)this.retained).getCullFace();
}
/**
* Sets the back face normal flip flag to the specified value.
* This flag indicates whether vertex normals of back facing polygons
* should be flipped (negated) prior to lighting. When this flag
* is set to true and back face culling is disabled, polygons are
* rendered as if the polygon had two sides with opposing normals.
* This feature is disabled by default.
* @param backFaceNormalFlip the back face normal flip flag
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
public void setBackFaceNormalFlip(boolean backFaceNormalFlip) {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_NORMAL_FLIP_WRITE))
throw new CapabilityNotSetException(J3dI18N.getString("PolygonAttributes5"));
if (isLive())
((PolygonAttributesRetained)this.retained).setBackFaceNormalFlip(backFaceNormalFlip);
else
((PolygonAttributesRetained)this.retained).initBackFaceNormalFlip(backFaceNormalFlip);
}
/**
* Gets the back face normal flip flag.
* @return the back face normal flip flag
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
public boolean getBackFaceNormalFlip() {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_NORMAL_FLIP_READ))
throw new CapabilityNotSetException(J3dI18N.getString("PolygonAttributes6"));
return ((PolygonAttributesRetained)this.retained).getBackFaceNormalFlip();
}
/**
* Sets the polygon rasterization mode for this
* appearance component object.
* @param polygonMode the polygon rasterization mode to be used; one of
* POLYGON_FILL, POLYGON_LINE, or POLYGON_POINT
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
public void setPolygonMode(int polygonMode) {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_MODE_WRITE))
throw new CapabilityNotSetException(J3dI18N.getString("PolygonAttributes7"));
if (polygonMode < POLYGON_POINT || polygonMode > POLYGON_FILL)
throw new IllegalArgumentException(J3dI18N.getString("PolygonAttributes8"));
if (isLive())
((PolygonAttributesRetained)this.retained).setPolygonMode(polygonMode);
else
((PolygonAttributesRetained)this.retained).initPolygonMode(polygonMode);
}
/**
* Gets the polygon rasterization mode for this
* appearance component object.
* @return the polygon rasterization mode
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
public int getPolygonMode() {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_MODE_READ))
throw new CapabilityNotSetException(J3dI18N.getString("PolygonAttributes9"));
return ((PolygonAttributesRetained)this.retained).getPolygonMode();
}
/**
* Sets the constant polygon offset to the specified value.
* This screen space
* offset is added to the final, device coordinate Z value of polygon
* primitives.
* @param polygonOffset the constant polygon offset
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
public void setPolygonOffset(float polygonOffset) {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_OFFSET_WRITE))
throw new CapabilityNotSetException(J3dI18N.getString("PolygonAttributes10"));
if (isLive())
((PolygonAttributesRetained)this.retained).setPolygonOffset(polygonOffset);
else
((PolygonAttributesRetained)this.retained).initPolygonOffset(polygonOffset);
}
/**
* Gets the constant polygon offset.
* @return the constant polygon offset
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*/
public float getPolygonOffset() {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_OFFSET_READ))
throw new CapabilityNotSetException(J3dI18N.getString("PolygonAttributes11"));
return ((PolygonAttributesRetained)this.retained).getPolygonOffset();
}
/**
* Sets the polygon offset factor to the specified value.
* This factor is multiplied by the slope of the polygon, and
* then added to the final, device coordinate Z value of polygon
* primitives.
* @param polygonOffsetFactor the polygon offset factor
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*
* @since Java 3D 1.2
*/
public void setPolygonOffsetFactor(float polygonOffsetFactor) {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_OFFSET_WRITE))
throw new CapabilityNotSetException(J3dI18N.getString("PolygonAttributes10"));
if (isLive())
((PolygonAttributesRetained)this.retained).
setPolygonOffsetFactor(polygonOffsetFactor);
else
((PolygonAttributesRetained)this.retained).
initPolygonOffsetFactor(polygonOffsetFactor);
}
/**
* Gets the polygon offset factor.
* @return the polygon offset factor.
* @exception CapabilityNotSetException if appropriate capability is
* not set and this object is part of live or compiled scene graph
*
* @since Java 3D 1.2
*/
public float getPolygonOffsetFactor() {
if (isLiveOrCompiled())
if(!this.getCapability(ALLOW_OFFSET_READ))
throw new CapabilityNotSetException(J3dI18N.getString("PolygonAttributes11"));
return ((PolygonAttributesRetained)this.retained).getPolygonOffsetFactor();
}
/**
* Creates a retained mode PolygonAttributesRetained object that this
* PolygonAttributes component object will point to.
*/
@Override
void createRetained() {
this.retained = new PolygonAttributesRetained();
this.retained.setSource(this);
}
/**
* @deprecated replaced with cloneNodeComponent(boolean forceDuplicate)
*/
@Override
public NodeComponent cloneNodeComponent() {
PolygonAttributes pga = new PolygonAttributes();
pga.duplicateNodeComponent(this);
return pga;
}
/**
* Copies all node information from <code>originalNodeComponent</code> into
* the current node. This method is called from the
* <code>duplicateNode</code> method. This routine does
* the actual duplication of all "local data" (any data defined in
* this object).
*
* @param originalNodeComponent the original node to duplicate.
* @param forceDuplicate when set to <code>true</code>, causes the
* <code>duplicateOnCloneTree</code> flag to be ignored. When
* <code>false</code>, the value of each node's
* <code>duplicateOnCloneTree</code> variable determines whether
* NodeComponent data is duplicated or copied.
*
* @see Node#cloneTree
* @see NodeComponent#setDuplicateOnCloneTree
*/
@Override
void duplicateAttributes(NodeComponent originalNodeComponent,
boolean forceDuplicate) {
super.duplicateAttributes(originalNodeComponent, forceDuplicate);
PolygonAttributesRetained attr = (PolygonAttributesRetained)
originalNodeComponent.retained;
PolygonAttributesRetained rt = (PolygonAttributesRetained) retained;
rt.initCullFace(attr.getCullFace());
rt.initBackFaceNormalFlip(attr.getBackFaceNormalFlip());
rt.initPolygonMode(attr.getPolygonMode());
rt.initPolygonOffset(attr.getPolygonOffset());
rt.initPolygonOffsetFactor(attr.getPolygonOffsetFactor());
}
}
|