📄 simplefog.java
字号:
//SimpleFog.java
//演示雾效
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.io.IOException;import java.util.Hashtable;import java.util.Properties;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
public class SimpleFog
extends Applet
implements WindowListener
{
// GUI objects for our subclasses
SimpleFog sf = null;
Frame frame = null;
Canvas3D canvas = null;
TransformGroup ViewTransform = null;
TransformGroup SceneTransform = null;
// Private GUI objects and state
DirectionalLight headlight = null;
// ExamineViewerBehavior myEB = null;
WalkViewerBehavior myWB = null;
float scalingFactor = 0.005f; String filename ="747.3ds";
public static void main( String[] args ){
SimpleFog mySF = new SimpleFog( );
mySF.initialize( );
mySF.buildUniverse( );
mySF.showFrame( );
}
//构造函数
public SimpleFog( ){
// Do nothing
}
//初始化实例,创建AWT的窗口框架和3D的画布
protected void initialize( ){
sf = this;
// 创建程序主框架窗口
frame = new Frame( );
frame.setSize( 250, 200 );
frame.setTitle( "Simple Fog Demo" );
frame.setLayout( new BorderLayout( ) );
// 创建关闭窗口的行为
frame.addWindowListener( this );
// 创建3D画布
canvas = new Canvas3D( null );
canvas.setSize( 250, 200 );
frame.add( "Center", canvas );
}
//创建虚拟空间,包括视点、头灯、步行对象
protected void buildUniverse( )
{
//创建虚拟空间
SimpleUniverse su = new SimpleUniverse(
null, // 默认的高精度坐标系统
1, // MultiTransformGroup中Transforms的数目
canvas, // 3D画布
null ); // 默认的配置文件的url
// 获得观察者
Viewer viewer = su.getViewer( );
//获得观察平台
ViewingPlatform viewingPlatform = su.getViewingPlatform( );
//获得包含观察平台Transform
ViewTransform = viewingPlatform.getViewPlatformTransform( );
// 创见虚拟空间的范围
BoundingSphere bounds = new BoundingSphere(
new Point3d( 0.0, 0.0, 0.0 ), 100000.0 );
//创建几何平台
PlatformGeometry pg = new PlatformGeometry( );
//创建头灯,始终使观察者眼前可见
headlight = new DirectionalLight( );
headlight.setColor( White );
headlight.setDirection( new Vector3f( 0.0f, 0.0f, -1.0f ) );
headlight.setInfluencingBounds( bounds );
headlight.setCapability( Light.ALLOW_STATE_WRITE );
headlight.setEnable (true);
pg.addChild( headlight );
//将几何平台添加到可见的观察平台
viewingPlatform.setPlatformGeometry( pg );
// 创建场景
// 创建根分支节点
BranchGroup sceneRoot = new BranchGroup( );
// 创建变换分支节点,并允许修改
SceneTransform = new TransformGroup( );
SceneTransform.setCapability(
TransformGroup.ALLOW_TRANSFORM_READ );
SceneTransform.setCapability(
TransformGroup.ALLOW_TRANSFORM_WRITE );
SceneTransform.setCapability(
Group.ALLOW_CHILDREN_EXTEND );
// 创见场景中物体
Group scene = this.buildScene( );
SceneTransform.addChild( scene );
sceneRoot.addChild( SceneTransform );
Background bg = new Background(); //指定背景颜色// bg.setColor(new Color3f(0.1f,0.4f,0.7f));
bg.setColor(new Color3f(0.0f,0.0f,0.0f)); //指定背景的有效范围 bg.setApplicationBounds( bounds ); sceneRoot.addChild( bg );
TextureLoader myLoader = new TextureLoader( "bg.jpg", this ); ImageComponent2D myImage = myLoader.getImage( ); bg.setImage( myImage );
//添加白色平行光 Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f); Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f); DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction); light2.setInfluencingBounds(bounds); sceneRoot.addChild(light2);
//添加白色的指数雾效
ExponentialFog myEF = new ExponentialFog( );
myEF.setColor( new Color3f( 1.0f, 1.0f, 1.0f ) );
myEF.setDensity( 3.0f );
// Set the influencing bounds
myEF.setInfluencingBounds( bounds );
sceneRoot.addChild(myEF);
/* LinearFog myLF = new LinearFog( );
myLF.setColor( new Color3f( 1.0f, 1.0f, 1.0f ) );
myLF.setColor(new Color3f(0.0f,0.0f,0.0f));
myLF.setFrontDistance( 2.4 );
myLF.setBackDistance( 3.4 );
//Set the influencing bounds
myLF.setInfluencingBounds( bounds );
sceneRoot.addChild(myLF);
*/
Clip myClip = new Clip( );
myClip.setBackDistance( 3.0 );
//Set its application bounds
myClip.setApplicationBounds( bounds );
sceneRoot.addChild (myClip);
/* myEB = new ExamineViewerBehavior(
SceneTransform, // Transform gorup to modify
frame ); // Parent frame for cusor changes
myEB.setSchedulingBounds( bounds );
myEB.setEnable (true);
sceneRoot.addChild( myEB );
*/
//创建步行行为对象,
myWB = new WalkViewerBehavior(
ViewTransform, // 步行所依附的变换节点
frame ); // 接受鼠标消息的窗口框架
myWB.setSchedulingBounds( bounds );
myWB.setEnable (true);
sceneRoot.addChild( myWB );
// 优化
sceneRoot.compile( );
su.addBranchGraph( sceneRoot );
//设置观察者的初始位置
reset( );
}
//创建场景中的景物
public Group buildScene( )
{
// 创建根场景
Group scene = new Group( );
//缩放物体 Transform3D scaleT3 = new Transform3D(); scaleT3.setScale( scalingFactor ); TransformGroup scaleTG = new TransformGroup( scaleT3 ); scene.addChild( scaleTG ); //物体绕y轴旋转90度 Transform3D rotyT3 = new Transform3D(); rotyT3.rotY(Math.PI/2); TransformGroup rotyTG = new TransformGroup( rotyT3 ); scaleTG.addChild (rotyTG); //物体绕z轴旋转45度 Transform3D rotzT3 = new Transform3D(); rotzT3.rotZ(Math.PI/4); TransformGroup rotzTG = new TransformGroup( rotzT3 ); rotyTG.addChild (rotzTG); try{ //导入3DS模型 Load3DS l = new Load3DS( filename, this, new Hashtable(), new Hashtable(), new Properties() ); rotzTG.addChild( l.root() ); } catch ( IOException e ){ System.err.println( "Exception: " + e.getMessage() ); e.printStackTrace( System.err ); }
return scene;
}
// 创建初始的观察点
public void reset( )
{
Transform3D trans = new Transform3D( );
SceneTransform.setTransform( trans );
trans.set( new Vector3f( 0.0f, 0.0f, 3.0f ) );
ViewTransform.setTransform( trans );
}
//显示窗口主框架
public void showFrame( )
{
frame.show( );
}
//正常退出程序
public void quit( )
{
System.exit( 0 );
}
//处理窗口消息,主要是处理关闭窗口的消息
public void windowClosing( WindowEvent event )
{
quit( );
}
public void windowClosed( WindowEvent event )
{
}
public void windowOpened( WindowEvent event )
{
}
public void windowIconified( WindowEvent event )
{
}
public void windowDeiconified( WindowEvent event )
{
}
public void windowActivated( WindowEvent event )
{
}
public void windowDeactivated( WindowEvent event )
{
}
// Well known colors, positions, and directions
public final static Color3f White = new Color3f( 1.0f, 1.0f, 1.0f );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -