纳金网
标题:
java3D的材质设计
[打印本页]
作者:
yeu1233
时间:
2012-9-18 11:38
标题:
java3D的材质设计
import java.awt.*;
import javax.media.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.*;
import com.sun.j3d.utils.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
/*
* 创建物体外观对象:Appearance.
* 创建物体的材质对象Material,它通过5过属性指定物体如何显示,其中有4个属性是颜色:环境(Ambient)
* 放射(Emissive),漫射(Diffuse)和镜射(Specular),第5个属性是发光(Shininess),这些属性
* 的值均为数字类型.每个颜色属性指定了物体的在特有情况下如何发光,比如,环境色反射在环境中发射
* 方向无法确定的光线,漫射光反射来自一个方向的光线,所以当光线垂直摄入表面时会更亮.对大多数
* 物体可以确定给环境光和满射光成分制定同一颜色,给放射光指定黑色,
* 通过Appearance的setMaterial方法设置物体的材质
* 创建物体的纹理对象Texture.TextureLoader类根据图片生成纹理,调用他的getTexture方法获
* 取纹理对象Texture,需要设置Texture的边界模型和颜色
* 通过Appearance的setTexture方法设置物体的纹理*/
public class Grahoics3D {
static class PictureBall{
public PictureBall(){
SimpleUniverse universe=new SimpleUniverse();
BranchGroup group=new BranchGroup();
//建立颜色
Color3f black=new Color3f(0.0f,0.0f,0.0f);
Color3f white=new Color3f(1.0f,1.0f,1.0f);
Color3f red=new Color3f(0.7f,0.7f,0.7f);
//建立纹理贴图
//必须是2的幂,例如128像素宽,256像素高
//当载入图片后可以指定想要如何使用图片,例如:RGB使用图片的颜色
//LUMINANCE使用图片显示黑白
TextureLoader loader=new TextureLoader("D:/javap/23.jpg","RGB",new Container());
Texture texture=loader.getTexture();//创建纹理对象
texture.setBoundaryModeS(Texture.WRAP);
texture.setBoundaryModeT(Texture.WRAP);
texture.setBoundaryColor(new Color4f(0.0f,1.0f,0.0f,0.0f));
//建立纹理属性
//可以用PEPLACE,BLEND或MODULATE
TextureAttributes textAttr=new TextureAttributes();
textAttr.setTextureMode(TextureAttributes.MODULATE);
Appearance ap=new Appearance();
ap.setTexture(texture);
ap.setTextureAttributes(textAttr);
//建立材质,5个参数分别为(环境,放射,漫射,镜射,发光)
ap.setMaterial(new Material(red,red,black,black,50.0f));
//创建一个球来展示纹理
//如果使用一个简单物理如球体,则需要通过设置"原始标记"允许纹理化
int primflags=Primitive.GENERATE_TEXTURE_COORDS;
Sphere sphere=new Sphere(0.5f,primflags,ap);
group.addChild(sphere);
//创建灯光,定向光源
Color3f light1Color=new Color3f(1f,1f,1f);
BoundingSphere bounds=new BoundingSphere(new Point3d(0.0,0.0,0.0),1000.0);
Vector3f light1Direction=new Vector3f(4.0f,-7.0f,-12.0f);
DirectionalLight light1=new DirectionalLight(light1Color,light1Direction);
light1.setInfluencingBounds(bounds);
group.addChild(light1);
//建立一个环境光源
AmbientLight ambientLight=new AmbientLight(new Color3f(0.5f,0.5f,0.5f));
group.addChild(ambientLight);
//注视球体
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(group);
}
}
public static void main(String args[]){
PictureBall pictureBall=new PictureBall();
}
}
欢迎光临 纳金网 (http://rs.narkii.com/club/)
Powered by Discuz! X2.5