我按书上说的
// 混合纹理与光照
TextureAttributes texatt = new TextureAttributes();
texatt.setTextureMode(TextureAttributes.COMBINE);
ap.setTextureAttributes(texatt);
这么做,有纹理贴图但没有光照的效果
大家帮我看看,Earth.jpg你们随便找张正方的图就行了
谢啦下面是我的代码:import java.awt.*;
import java.net.URL;import javax.media.j3d.*;
import javax.swing.JApplet;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;@SuppressWarnings("serial")
public class Earth3D extends JApplet {
public static void main(String[] args) {
new MainFrame(new Earth3D(), 640, 480);
} private SimpleUniverse su; public void init() {
GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
Canvas3D cv = new Canvas3D(gc);// 创建一个Canvas3d对象
setLayout(new BorderLayout());
add(cv);
BranchGroup bg = createSceneGraph();// 创建场景图的根节点
bg.compile();// 编译场景图
su = new SimpleUniverse(cv);// 创建和设置SimpleUniverse对象
su.getViewingPlatform().setNominalViewingTransform();
su.addBranchGraph(bg);
su.getViewer().getView().setMinimumFrameCycleTime(40); OrbitBehavior orbit = new OrbitBehavior(cv, OrbitBehavior.REVERSE_ALL);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
100.0);
orbit.setSchedulingBounds(bounds);
su.getViewingPlatform().setViewPlatformBehavior(orbit);
} public void destroy() {
su.cleanup();
} private BranchGroup createSceneGraph() {
BranchGroup root = new BranchGroup();
TransformGroup spin = new TransformGroup();
spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
root.addChild(spin);
// 倾斜
Transform3D tr = new Transform3D();
tr.setScale(0.3);
tr.setRotation(new AxisAngle4d(0, 0, 1, Math.PI / 12));
TransformGroup tg = new TransformGroup(tr);
spin.addChild(tg);
// 放置球体
Appearance ap=createAppearance();
tg.addChild(new Sphere(1f, Primitive.GENERATE_TEXTURE_COORDS, 30,
ap));
// 旋转
BoundingSphere bounds = new BoundingSphere();
Alpha alpha = new Alpha(-1, 10000);
RotationInterpolator rotator = new RotationInterpolator(alpha, spin,
tr, 0f, (float) (2 * Math.PI));
rotator.setSchedulingBounds(bounds);
spin.addChild(rotator);
// 添加背景与光源
Background background = new Background(0.0f, 0.0f, 0.0f);
background.setApplicationBounds(bounds);
PointLight ptlight = new PointLight(new Color3f(Color.red),
new Point3f(3f, 3f, 3f), new Point3f(1f, 0f, 0f));
ptlight.setInfluencingBounds(bounds);
root.addChild(ptlight);
return root;
} private Appearance createAppearance() {
Appearance ap = new Appearance();
URL filename = getClass().getClassLoader().getResource(
"earth.jpg");
TextureLoader loader = new TextureLoader(filename, this);
 ImageComponent2D image = loader.getImage();// 打开纹理图片
Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGB,
image.getWidth(), image.getHeight());
texture.setImage(0, image);
texture.setMagFilter(Texture.BASE_LEVEL_LINEAR);
ap.setTexture(texture);
//ap.setTexture(loader.getTexture());
// 混合纹理与光照
TextureAttributes texatt = new TextureAttributes();
texatt.setTextureMode(TextureAttributes.COMBINE);
ap.setTextureAttributes(texatt);
ap.setMaterial(new Material());
return ap;
}