Here is the code TextureTest from the java3D demo.
I've modified it so that the texture and lighting are enabled together (by
adding a GENERATE_NORMALS to the Box and adding a default Material
to the appearance to enable lighting).
Now this code is working fine with Java3D1.1.1 on SGI 320 and STB Velocity 4400
but doesnt work (texture but no lighting) with Java3D1.1.2 ...
Thanks for the help and suggestions.
Fred
Doug Gehringer wrote:
> Do you have a simple test case for your problem? For example, can you make it
> happen using one of the example programs?
>
> Thanks
>
> Doug Gehringer
> Sun Microsystems
/*
* @(#)TextureTest.java 1.33 98/06/08 15:41:51
*
* Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* 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 AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE 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 SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.Box;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.image.BufferedImage;
import java.awt.image.ComponentColorModel;
import java.awt.image.WritableRaster;
import java.awt.image.DataBufferByte;
import java.awt.color.ColorSpace;
import java.awt.Transparency;
public class TextureTest extends Applet {
public BranchGroup createSceneGraph() {
Color3f aColor = new Color3f(0.2f, 0.2f, 0.2f);
Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
Color3f dColor = new Color3f(0.2f, 1.0f, 0.6f);
Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
Color3f lColor2 = new Color3f(0.7f, 0.7f, 0.35f);
Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
Vector3f lDir2 = new Vector3f(0.0f, 0.0f, 1.0f);
Color3f alColor = new Color3f(1.0f, 1.0f, 1.0f);
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Create the transform group node and initialize it to the identity.
// Enable the TRANSFORM_WRITE capability so that our behavior code
// can modify it at runtime. Add it to the root of the subgraph.
//
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
//
// Create some simple geometry (without an appearance node).
// Create a new shape leaf node using the specified geometry
// and add it into the scene graph.
//
int width = 16;
int height = 16;
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8};
ComponentColorModel colorModel =
new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, 0);
WritableRaster raster =
colorModel.createCompatibleWritableRaster(width, height);
BufferedImage bImage =
new BufferedImage(colorModel, raster, false, null);
byte[] byteData = ((DataBufferByte)raster.getDataBuffer()).getData();
int i, j, k, cc;
for (i=0;i < height;i++){
for (j=0;j < width;j++){
if (((( i & 0x2) == 0)^((j & 0x02)) == 0)) cc =255;
else cc = 0;
k = (i*15 + j) *3;
byteData[k] = (byte)cc;
byteData[k+1] = (byte)cc;
byteData[k+2] = (byte)cc;
}
}
ImageComponent2D pArray = new ImageComponent2D(
ImageComponent.FORMAT_RGB, width, height);
pArray.set(bImage);
Texture2D tex = new Texture2D(Texture.BASE_LEVEL,
Texture.RGB, width, height);
tex.setImage(0, pArray);
tex.setEnable(true);
// Create a simple shape leaf node
Appearance a = new Appearance();
a.setTexture(tex);
// added line
a.setMaterial(new Material());
Box textureCube = new Box(0.4f, 0.4f, 0.4f,
Box.GENERATE_TEXTURE_COORDS|Box.GENERATE_NORMALS, a);
objTrans.addChild(textureCube);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
AmbientLight aLgt = new AmbientLight(alColor);
aLgt.setInfluencingBounds(bounds);
DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
lgt1.setInfluencingBounds(bounds);
DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
lgt2.setInfluencingBounds(bounds);
objRoot.addChild(aLgt);
objRoot.addChild(lgt1);
objTrans.addChild(lgt2);
//
// Create a new Behavior object that will perform the desired
// operation on the specified transform object and add it into the
// scene graph.
//
Transform3D yAxis = new Transform3D();
Alpha rotorAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
0, 0,
4000, 0, 0,
0, 0, 0);
RotationInterpolator rotator =
new RotationInterpolator(rotorAlpha,
objTrans,
yAxis,
0.0f, (float) Math.PI*2.0f);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
// Have Java 3D perform optimizations on this scene graph.
objRoot.compile();
return objRoot;
}
public TextureTest() {
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center", c);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
//
// The following allows TextureTest to be run as an application
// as well as an applet
//
public static void main(String[] args) {
new MainFrame(new TextureTest(), 256, 256);
}
}
/*
* @(#)TextureImage.java 1.14 98/04/08 14:19:01
*
* Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* 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 AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE 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 SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.geometry.Box;
import javax.media.j3d.*;
import javax.vecmath.*;
public class TextureImage extends Applet {
public BranchGroup createSceneGraph(String[] args) {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Create the transform group node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
// Create appearance object for textured cube
Appearance app = new Appearance();
if (args.length > 0) {
Texture tex = new TextureLoader(args[0], this).getTexture();
app.setTexture(tex);
if (tex == null)
System.out.println("Warning: Texture is disabled");
}
else {
System.out.println("Warning: Texture is disabled");
System.out.println("Usage: java TextureImage <image file name> [-f
ImageComponent format]");
}
// Create textured cube and add it to the scene graph.
Box textureCube = new Box(0.4f, 0.4f, 0.4f,
Box.GENERATE_TEXTURE_COORDS, app);
objTrans.addChild(textureCube);
// Create a new Behavior object that will perform the desired
// operation on the specified transform object and add it into
// the scene graph.
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
0, 0,
4000, 0, 0,
0, 0, 0);
RotationInterpolator rotator =
new RotationInterpolator(rotationAlpha, objTrans, yAxis,
0.0f, (float) Math.PI*2.0f);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
// Have Java 3D perform optimizations on this scene graph.
objRoot.compile();
return objRoot;
}
public TextureImage(String[] args) {
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center", c);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph(args);
SimpleUniverse u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
//
// The following allows TextureImage to be run as an application
// as well as an applet
//
public static void main(String[] args) {
new MainFrame(new TextureImage(args), 256, 256);
}
}