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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
/*
* $RCSfile$
*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
* NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
* USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
* ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
* INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or
* intended for use in the design, construction, operation or
* maintenance of any nuclear facility.
*
* $Revision$
* $Date$
* $State$
*/
package org.jdesktop.j3d.examples.dot3;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jdesktop.j3d.examples.Resources;
import org.jogamp.java3d.AmbientLight;
import org.jogamp.java3d.Appearance;
import org.jogamp.java3d.Background;
import org.jogamp.java3d.Behavior;
import org.jogamp.java3d.BoundingSphere;
import org.jogamp.java3d.BranchGroup;
import org.jogamp.java3d.Canvas3D;
import org.jogamp.java3d.DirectionalLight;
import org.jogamp.java3d.GeometryArray;
import org.jogamp.java3d.ImageComponent2D;
import org.jogamp.java3d.Material;
import org.jogamp.java3d.PolygonAttributes;
import org.jogamp.java3d.Shape3D;
import org.jogamp.java3d.TexCoordGeneration;
import org.jogamp.java3d.Texture;
import org.jogamp.java3d.Texture2D;
import org.jogamp.java3d.TextureAttributes;
import org.jogamp.java3d.TextureUnitState;
import org.jogamp.java3d.WakeupCriterion;
import org.jogamp.java3d.WakeupOnElapsedFrames;
import org.jogamp.java3d.utils.behaviors.vp.OrbitBehavior;
import org.jogamp.java3d.utils.geometry.GeometryInfo;
import org.jogamp.java3d.utils.image.TextureLoader;
import org.jogamp.java3d.utils.universe.SimpleUniverse;
import org.jogamp.java3d.utils.universe.ViewingPlatform;
import org.jogamp.vecmath.Color3f;
import org.jogamp.vecmath.Point3d;
import org.jogamp.vecmath.Vector3f;
/**
* This example program is contributed by Alessandro Borges
*/
/**
* <pre>
* DOT3 per-pixel lighting demo.
* It uses a Normal map and a Light map, both coded as independent textures.
* Each pixel color is a vector coded, where color range [0,255] is mapped
* as vector in range [-1.0,+1.0].
*
* A math operation called DOT3 applied to Light vector and Normal vector results
* a scalar value, interpreted as light intensity. This operation is made for each
* pixel on texture.
* Light Intensity = DOT3(light, normal);
*
* This technique allows complex lighting effects, as bumps, on low polygon count
* geometries.
* </pre>
*
*/
public class Dot3Demo extends JFrame {
// a external control panel for this demo
private TextureControlPanel ctrlPanel = null;
// default bounds used in this application
private BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
100.0);
// TextureUnitStates used in this application
TextureUnitState tuLightMap;
TextureUnitState tuDOT3NormalMap;
TextureUnitState tuColor;
/** Where the TUs are applied **/
TextureUnitState[] tusArr;
/** appearance will be changed at runtime **/
Appearance appearance;
/** polygonAttributes will be changed at runtime **/
PolygonAttributes polygonAttributes;
// textures used
Texture textureColor;
Texture textureDOT3NormalMap;
Texture2D textureLightMap;
// needs for runtime updates on lightMap
ImageComponent2D imageLightMap;
// default texture names used
String textureColorName= "main/resources/images/wood.jpg";
String textureDOT3NormalMapName = "main/resources/images/Java3Ddot3.jpg";
/**
* Constructor.
*/
public Dot3Demo() {
super("Java3D DOT3 demo");
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
private void init() throws Exception {
this.setSize(new Dimension(400, 400));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
this.getContentPane().add(mainPanel, null);
mainPanel.setLayout(new BorderLayout());
// get default configuration for 3D
GraphicsConfiguration conf = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas = new Canvas3D(conf);
// create simpleUniverse
SimpleUniverse su = new SimpleUniverse(canvas);
// create sceneGraph and add it to universe
BranchGroup sceneGraph = createSceneGraph();
su.addBranchGraph(sceneGraph);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
su.getViewingPlatform().setNominalViewingTransform();
// Ensure at least 5 msec per frame (i.e., < 200Hz)
su.getViewer().getView().setMinimumFrameCycleTime(5);
// add the behaviors to the ViewingPlatform
ViewingPlatform viewingPlatform = su.getViewingPlatform();
viewingPlatform.setNominalViewingTransform();
// add orbit behavior to ViewingPlatform
OrbitBehavior orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL |
OrbitBehavior.STOP_ZOOM);
orbit.setSchedulingBounds(bounds);
viewingPlatform.setViewPlatformBehavior(orbit);
mainPanel.add(canvas, BorderLayout.CENTER);
this.setVisible(true);
//create a control panel to user interaction
ctrlPanel = new TextureControlPanel(this);
ctrlPanel.setVisible(true);
ctrlPanel.setLocation(410,10);
}
/**
* loads all needed textures, and creates light map texture
*/
private void loadTextures() {
try {
//java.net.URL urlColor = new java.net.URL("file:" + textureColorName);
//java.net.URL urlDot3 = new java.net.URL("file:" + textureDOT3NormalMapName);
java.net.URL urlColor = Resources.getResource(textureColorName);
java.net.URL urlDot3 = Resources.getResource(textureDOT3NormalMapName);
// loading textures
textureColor = new TextureLoader(urlColor,this).getTexture();
textureDOT3NormalMap = new TextureLoader(urlDot3,this) .getTexture();
// create Image for textureLightMap
BufferedImage image = new BufferedImage(256,256,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setPaint(new Color(130,130,250));
graphics.fillRect(0,0,image.getWidth(),image.getHeight());
graphics.dispose();
imageLightMap = new ImageComponent2D(ImageComponent2D.FORMAT_RGB,image,false,false);
imageLightMap.setCapability(ImageComponent2D.ALLOW_IMAGE_WRITE);
imageLightMap.setCapability(ImageComponent2D.ALLOW_IMAGE_READ);
//create textureLightMap with above imageLightMap
textureLightMap = new Texture2D(Texture2D.BASE_LEVEL,Texture2D.RGB,256,256);
textureLightMap.setImage(0,imageLightMap);
textureLightMap.setMagFilter(Texture2D.NICEST);
textureLightMap.setMinFilter(Texture2D.NICEST);
// application with update textureLightMap at runtime, so lets enable some caps
textureLightMap.setCapability(Texture2D.ALLOW_ENABLE_WRITE);
textureLightMap.setCapability(Texture2D.ALLOW_ENABLE_READ);
textureLightMap.setCapability(Texture2D.ALLOW_IMAGE_WRITE);
textureLightMap.setCapability(Texture2D.ALLOW_IMAGE_READ);
} catch(Exception e) {
System.err.println("Failed to load textures");
e.printStackTrace();
}
}
/**
* setup TextureUnitStates used in this demo. *
* @return
*/
private TextureUnitState[] setupTextureUnitState() {
//texture Attributes for DOT3 normal map
TextureAttributes textAttDot3 = new TextureAttributes();
// lightMap uses TextureAttributes with default REPLACE mode
TextureAttributes textAttLightMap = new TextureAttributes();
TextureAttributes texAttColor = new TextureAttributes();
texAttColor.setTextureMode(TextureAttributes.COMBINE);
//CombineRgbMode could be also COMBINE_ADD or COMBINE_ADD_SIGNED, with
//different results
texAttColor.setCombineRgbMode(TextureAttributes.COMBINE_MODULATE);
// increase light depth effect
texAttColor.setCombineRgbScale(2);
textAttDot3.setTextureMode(TextureAttributes.COMBINE);
textAttDot3.setCombineRgbMode(TextureAttributes.COMBINE_DOT3);
textAttDot3.setCombineAlphaMode(TextureAttributes.COMBINE_DOT3);
textAttDot3.setTextureBlendColor(1.f,1.0f,1.0f,0.0f);
// increase light intesity
textAttDot3.setCombineRgbScale(2);
// setup functions
textAttDot3.setCombineRgbFunction(0,TextureAttributes.COMBINE_SRC_COLOR);
textAttDot3.setCombineRgbFunction(1,TextureAttributes.COMBINE_SRC_COLOR);
textAttDot3.setCombineRgbFunction(2,TextureAttributes.COMBINE_SRC_COLOR);
//combine with previous TUS, lightMap
textAttDot3.setCombineRgbSource(0,TextureAttributes.COMBINE_PREVIOUS_TEXTURE_UNIT_STATE);
textAttDot3.setCombineRgbSource(1,TextureAttributes.COMBINE_TEXTURE_COLOR );
textAttDot3.setCombineRgbSource(2,TextureAttributes.COMBINE_OBJECT_COLOR);
TexCoordGeneration tcg1=null;
// SphereMap tcg can add nice dynamic effects for curved surfaces, because it
// distributes texture like a light bean over geometry.
// It os not used in this demo, but you can try yourself at home
// with *complex* lightmaps, i.e., spherical light distributions,
// multi light sorces, degradee, waves,etc
/*
tcg1 = new TexCoordGeneration(TexCoordGeneration.SPHERE_MAP,
TexCoordGeneration.TEXTURE_COORDINATE_3);
*/
// create TUS
tuLightMap = new TextureUnitState(textureLightMap,textAttLightMap,tcg1);
tuDOT3NormalMap = new TextureUnitState(textureDOT3NormalMap,textAttDot3,null);
tuColor = new TextureUnitState(textureColor,texAttColor,null);
// this TUS array is used by geometry at runtime
TextureUnitState[] tus = new TextureUnitState[3];
tus[0] = tuLightMap;
tus[1] = tuDOT3NormalMap;
tus[2] = tuColor;
// enable texture units for read/write at runtime
for (int i = 0; i < tus.length; i++) {
tus[i].setCapability(TextureUnitState.ALLOW_STATE_WRITE);
tus[i].setCapability(TextureUnitState.ALLOW_STATE_READ);
}
return tus;
}
/**
* creates a single Quad geometry with 4 TextureCoordinateMaps, for multitexture use.<br>
* Dimension is scale*(2m , 1m)
* @param scale a scale for this quad
* @return quad geometry for multitexture use
*/
private GeometryArray createGeometry(float scale) {
// vertex coordinates
float[] verts = { 2.0f, -1.0f, 0.0f,
2.0f, 1.0f, 0.0f,
-2.0f, 1.0f, 0.0f,
-2.0f, -1.0f, 0.0f };
// 2D texture Coords - each texture unit will use one set of this
float[] texCoords = { 1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f};
// all texture units will use texCoords from unit 0
int[] texCoordSetMap = {0,0,0,0};
// normals
Vector3f normal = new Vector3f( 0.0f, 0.0f, 1.0f);
Vector3f[] normals = { normal, normal, normal, normal} ;
// resize quad dimension
for(int i = 0;i<verts.length;i++) {
verts[i] *= scale;
}
// create geometry using GeometryInfo
GeometryInfo gi = new GeometryInfo(GeometryInfo.QUAD_ARRAY);
gi.setCoordinates(verts);
gi.setNormals(normals);
// preparing for multitexture
// To get up to 4 TUS, it needs 4 sets of 2D texture
gi.setTextureCoordinateParams(4, 2);
gi.setTexCoordSetMap(texCoordSetMap);
// this demo needs just 3 TUS, but geometry
// is prepared for up to 4 TUS stages
gi.setTextureCoordinates(0,texCoords);
gi.setTextureCoordinates(1,texCoords);
gi.setTextureCoordinates(2,texCoords);
gi.setTextureCoordinates(3,texCoords);
return gi.getGeometryArray();
}
/**
* Creates scenegraphs
* @return a BranchGroup with all needed objects in scene
*/
private BranchGroup createSceneGraph() {
BranchGroup bgRoot = new BranchGroup();
CheckNewLightMapBehavior checkNewLightMapBehavior = new CheckNewLightMapBehavior();
bgRoot.addChild(checkNewLightMapBehavior);
// a blue background
Background background = new Background(0.4f,0.4f,0.8f);
background.setApplicationBounds(bounds);
bgRoot.addChild(background);
AmbientLight alit = new AmbientLight(true,new Color3f(0.4f,0.4f,0.4f));
bgRoot.addChild(alit);
// Set up some directional lights
// DOT3 doesnot need light, because it is a perpixel lighting technique
//but we add this lights to show
// geometry when using non-DOT3 lighting, as color texture only and
// light map texture mode
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
bgRoot.addChild(light1);
DirectionalLight light2
= new DirectionalLight(light2Color, light2Direction);
light2.setInfluencingBounds(bounds);
bgRoot.addChild(light2);
//loading color and DOT3 normal map textures from disk,
//and creating light map at runtime
loadTextures();
//our single Quad geometry, enabled for multitexture
GeometryArray geo = createGeometry(0.4f);
// a appearance for our geometry
appearance = new Appearance();
// polygon and texture unit will be updated at runtime
// so we must enabled read/write operations for then
appearance.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_READ);
appearance.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);
appearance.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_READ);
appearance.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE);
//use a default material. It is necessary when running
//on non per-pixel lighting mod, i.e., using non DOT3 textures
appearance.setMaterial(new Material());
polygonAttributes = new PolygonAttributes();
polygonAttributes.setCapability(PolygonAttributes.ALLOW_MODE_WRITE);
polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE);
appearance.setPolygonAttributes(polygonAttributes);
// uses a TUS dot3 enabled
tusArr = setupTextureUnitState();
appearance.setTextureUnitState(tusArr);
// joining geometry and appearance in a shape3D
Shape3D shape3D = new Shape3D(geo,appearance);
shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
bgRoot.addChild(shape3D);
bgRoot.compile();
return bgRoot;
}
/**
* Toggles wireframe mode
* @param mode true for wireframe, false for fill polygon
*/
public void setWireframeMode(boolean mode) {
if(mode)
polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_LINE);
else
polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL);
}
/**
* This method togles on/off textures and updates TextureUnitState in correct Order.
* Some video drivers does not accept TextureUnitState arrays with null values among
* non-null values
* @param showLightMap togles LightMap texture
* @param showDot3 togles DOT3 Normal texture
* @param showColor togles Color texture
*/
public void showTextures(boolean showLightMap, boolean showDot3, boolean showColor) {
int bitSet = 0;
bitSet |= showLightMap ? 4 : 0;
bitSet |= showDot3 ? 2 : 0;
bitSet |= showColor ? 1 : 0;
tusArr[0] = null;
tusArr[1] = null;
tusArr[2] = null;
switch (bitSet) {
case 7: { //all bit == all tus
tusArr[0] = tuLightMap;
tusArr[1] = tuDOT3NormalMap;
tusArr[2] = tuColor;
}
break;
case 6: { //no Color
tusArr[0] = tuLightMap;
tusArr[1] = tuDOT3NormalMap;
}
break;
case 5: { //no Dot3
tusArr[0] = tuLightMap;
tusArr[1] = tuColor;
}
break;
case 4: { //lightMap only
tusArr[0] = tuLightMap;
}
break;
case 3: { //no LightMap
tusArr[0] = tuDOT3NormalMap;
tusArr[1] = tuColor;
}
break;
case 2: { //Dot3 Only
tusArr[0] = tuDOT3NormalMap;
}
break;
case 1: { // Color Only
tusArr[0] = tuColor;
}
break;
default: { // case 0, no textures shows at all
}
break;
}
appearance.setTextureUnitState(tusArr);
}
/**
* updates LightMap texture.
* This method is called from checkNewLightMapBehavior
* @param image new image to be applied
*/
public void updateLighMap(BufferedImage image) {
imageLightMap.setSubImage(image,image.getWidth(),image.getHeight(),0,0,0,0);
}
private BufferedImage tempImage;
private boolean lockTempImage = false;
/**
* main method
* @param args
*/
public static void main(String[] args) {System.setProperty("sun.awt.noerasebackground", "true");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Dot3Demo();
}
});
}
/**
* A internal class to check if there is a new Light Map to be applied
*/
class CheckNewLightMapBehavior extends Behavior {
WakeupOnElapsedFrames wakeup = new WakeupOnElapsedFrames(0);
public CheckNewLightMapBehavior() {// auto enable and set schedulling bounds
setEnable(true);
setSchedulingBounds(bounds);
}
@Override
public void initialize() {
wakeupOn(wakeup);
}
@Override
public void processStimulus(Iterator<WakeupCriterion> e) {
// check if there are a new light map ready to use
if (ctrlPanel.hasTextureImageReady()) {
updateLighMap(ctrlPanel.getTextureImage());
}
//wake up on next frame
wakeupOn(wakeup);
}
}
}
|