站 (Tue Apr 20 11:24:23 1999) , 站内信件
2. OBJ格式图形的缩小
运行前面的程序时,我们发现,显示效果不正确,原因
是图形太大,只能看到图形的一部分。因而我们编写了
Obj2.java程序,通过形体所在的坐标系的比例缩小,使形体
也相应地缩小。
//Obj2.java
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.ParsingErrorExceptio
import com.sun.j3d.loaders.IncorrectFormatExceptio
import com.sun.j3d.loaders.Scene;
import java.io.*;
import java.a let.A let;
import java.awt.BorderLayout;
import com.sun.j3d.utils.a let.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public cla Obj2 extends A let {
private double creaseAngle = 60.0;
private String filename = null;
public BranchGroup createSceneGraph(String args[]) {
BranchGroup objRoot = new BranchGroup();
Tra formGroup objScale = new Tra formGroup();
Tra form3D t3d = new Tra form3D();
t3d.setScale(0.3);
objScale.setTra form(t3d);
objRoot.addChild(objScale);
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
Bounding here bounds =
new Bounding here(new Point3d(0.0,0.0,0.0), 100.0);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
Tra formGroup objTra = new Tra formGroup();
objTra .setCapability(Tra formGroup.ALLOW_TRA FORM_WRITE);
objScale.addChild(objTra );
int flags = ObjectFile.RESIZE;
ObjectFile f = new ObjectFile(flags,
(float)(creaseAngle * Math.PI / 180.0));
Scene s = null;
try {
s = f.load(filename);
}
catch (FileNotFoundException e) {
System.err.println(e);
System.exit(1);
}
catch (ParsingErrorException e) {
System.err.println(e);
System.exit(1);
}
catch (IncorrectFormatException e) {
System.err.println(e);
System.exit(1);
}
objTra .addChild(s.getSceneGroup());
Tra form3D yAxis = new Tra form3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
0, 0,
4000, 0, 0,
0, 0, 0);
RotationInterpolator rotator =
new RotationInterpolator(rotationAlpha, objTra , yAxis,
0.0f, (float) Math.PI*2.0f);
rotator.setSchedulingBounds(bounds);
objTra .addChild(rotator);
objRoot.compile();
return objRoot;
}
public Obj2(String args[]) {
filename = args[0];
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center", c);
BranchGroup scene = createSceneGraph(args);
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTra form();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new Obj2(args), 400,400);
}
}
//end of Obj2.java
我们发现,和Obj1.java相比,程序增加了下面这段语句,
通过这几个语句,使坐标系发生了比例变换,成为了原来大小
的0.3倍:
Tra formGroup objScale = new Tra formGroup();
Tra form3D t3d = new Tra form3D();
t3d.setScale(0.3);
objScale.setTra form(t3d);
objRoot.addChild(objScale);
并将
objRoot.addChild(objTra );
改成了
objScale.addChild(objTra );
这表面,程序的场景图中,最根部的objRoot上面是
objScale,objScale上面才是objTra ,形体放在
objTra 这个BranchGroup中的。当然灯光是放在objRoot
这个BranchGroup中的。
程序的开头,我们给出了:
private double creaseAngle = 60.0;
它可以使程序运行时通过设定和VRML程序中同样的
creaseAngle来提高或降低图形的显示效果。
给出了creaseAngle,f的定义也相应地有了变化,即调用
的是ObjectFile的另一个构造函数:
ObjectFile f = new ObjectFile(flags,
(float)(creaseAngle * Math.PI / 180.0));
并在前面定义了flag:
int flags = ObjectFile.RESIZE;
本人未获得定义ObjectFile对象的源程序,故
猜测ObjectFile.RESIZE可能是一个开关参数,表示允许
改变尺寸。
我不停地追逐着我的梦,
却常常迷失了自己的方向,? ?
走过了一段艰难的路程?
才发现我所失去的竟已是我的所有...


