Graphics 版 (精华区)
发信人: seaman (翩翩少年), 信区: Graphics
标 题: JAVA3D学习系列(11)--Wavefront的OBJ图形调用
发信站: 哈工大紫丁香 (Tue Sep 28 19:13:51 1999), 转信
JAVA3D学习系列(11)---外部复杂形体的调用
(Wavefront的OBJ,Lightwave的LWS和LWO,WRL,DWG,3DS)
汕头大学机电系 张杰(jzhang@mailserv.stu.edu.cn)
****************VRML2.0交互式三维图形编程****************
JAVA3D学习系列中的例题将有非常多的书中的VRML程序与之相比较,
欢迎购买VRML2.0新书。
特殊购书方式:
1。作者售书
1。网上订购(email address: jzhang@mailserv.stu.edu.cn)
2。可以先获书,后汇款(不满意可退书),
只需将通信地址及邮编告知作者,即可在最短的时间内得到书。
3。书价为25元/本,免收邮购费用。
4。书为16开本,正文161页。
5. 购书可获盖有出版社财务章的收据。
6. 如果需要书中所有的源程序,我可以email一个打包程序
******************VRML2.0交互式三维图形编程*************
我们可以利用前面介绍的方法生成我们所需要的基本形体,
生成点、线、平面。但有的时候,我们需要用到其它格式的三
维形体,如VRML2.0格式的图形文件,AUTOCAD绘出的DWG格式的
三维形体,3DS MAX绘制出的复杂形体。对于这些形体,我们可
以非常方便地将其用到JAVA3D程序中去。下面我们介绍一些图形
格式在JAVA3D中的应用方法。
一. Wavefront的OBJ格式的图形文件的调用
1. OBJ格式图形的简单调用
JAVA3D编译环境所带的UTILITY有两个LOADER,一个可用来
调用Wavefront软件的OBJ格式的三维图形格式文件,一个可用来
调用Lightwave软件的LWS及LWO格式的三维图形格式文件。
假设我们有一个OBJ格式的文件(JAVA3D附带有两个OBJ,文
件名分别为galleon.obj和p51_mustang.obj,以及调用它们的一
个程序ObjLoad.java)。
我们编写的第一个程序Obj1.java介绍了OBJ图形的调用方法。
程序的便宜方法:
javac Obj1.java
程序的运行方法(假设图形在同一目录):
java Obj1 galleon.obj
//Obj1.java
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.Scene;
import java.io.*;
import java.applet.Applet;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class Obj1 extends Applet {
private String filename = null;
public BranchGroup createSceneGraph(String args[]) {
BranchGroup objRoot = new BranchGroup();
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
ObjectFile f = new ObjectFile( );
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);
}
objTrans.addChild(s.getSceneGroup());
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);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
objRoot.compile();
return objRoot;
}
public Obj1(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().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new Obj1(args), 400,400);
}
}
//end of Obj1.java
程序运行后我们发现,显示的效果不太正确,形体只显示了
一部分,我们将在Obj2.java将其效果修正一下。
我们来看程序,为了使JAVA3D能够调用Wavefront的OBJ格式
的图形文件,我们需要用到JAVA3D所附带的UTILITY。程序的头
四行import语句就是用来处理Wavefront的OBJ格式的图形文件,第
五行的import语句则是打开外部文件之用。
为了调用一个外部文件,我们给出一个定义:
private String filename = null;
为了使得形体得以看见,我们通过下面的一段程序,设定了
一个颜色并将其作为定向光的颜色,确定定向光的影响范围,并
使其放入进objRoot。
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
下面这段则是用来调用外部的OBJ文件之用:
ObjectFile f = new ObjectFile( );
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);
}
objTrans.addChild(s.getSceneGroup());
文件调用之后,其它部分的处理和前面的几个例子基本
上相同,这里就不介绍了,
2. OBJ格式图形的缩小
运行前面的程序时,我们发现,显示效果不正确,原因
是图形太大,只能看到图形的一部分。因而我们编写了
Obj2.java程序,通过形体所在的坐标系的比例缩小,使形体
也相应地缩小。
//Obj2.java
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.Scene;
import java.io.*;
import java.applet.Applet;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class Obj2 extends Applet {
private double creaseAngle = 60.0;
private String filename = null;
public BranchGroup createSceneGraph(String args[]) {
BranchGroup objRoot = new BranchGroup();
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.3);
objScale.setTransform(t3d);
objRoot.addChild(objScale);
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objScale.addChild(objTrans);
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);
}
objTrans.addChild(s.getSceneGroup());
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);
rotator.setSchedulingBounds(bounds);
objTrans.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().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new Obj2(args), 400,400);
}
}
//end of Obj2.java
我们发现,和Obj1.java相比,程序增加了下面这段语句,
通过这几个语句,使坐标系发生了比例变换,成为了原来大小
的0.3倍:
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.3);
objScale.setTransform(t3d);
objRoot.addChild(objScale);
并将
objRoot.addChild(objTrans);
改成了
objScale.addChild(objTrans);
这表面,程序的场景图中,最根部的objRoot上面是
objScale,objScale上面才是objTrans,形体放在
objTrans这个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可能是一个开关参数,表示允许
改变尺寸。
3.分成两个程序
上面给出的两个程序,程序不是很清晰,阅读起来比较困难,
为此,我们将Obj2.java分解成两个程序:
Obj3.java和objFile.java。
//Obj3.java
import java.applet.Applet;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
public class Obj3 extends Applet {
public BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.3);
objScale.setTransform(t3d);
objRoot.addChild(objScale);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objScale.addChild(objTrans);
BranchGroup b1 = new objFile("galleon.obj");
objTrans.addChild(b1);
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);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
objRoot.compile();
return objRoot;
}
public Obj3(String args[]) {
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center", c);
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new Obj3(args), 400,400);
}
}
//end of Obj3.java
--------------------------
//objFile.java
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.Scene;
import java.io.*;
import javax.media.j3d.*;
public class objFile extends BranchGroup{
private double creaseAngle = 60.0;
public objFile(String filename) {
BranchGroup obj = new BranchGroup( );
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);
}
obj.addChild(s.getSceneGroup( ) );
this.addChild(obj);
}
}
//end of objFile.java
4.同时调用两个形体,两者单独旋转。
前面的程序中,形体只有一个,为此,我们编了一个
程序,利用它可以调用两个OBJ形体,一左一右,它们分别绕
自身的轴旋转,当然,形体的生成仍然使用上面的
objFile.java程序。另外,我们还在程序中加了红色的背景。
//Obj4.java
import java.applet.Applet;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
public class Obj4 extends Applet {
public BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.3);
objScale.setTransform(t3d);
objRoot.addChild(objScale);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
Color3f bgColor = new Color3f(1.05f, 0.00f, 0.0f);
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
BranchGroup b1 = new objFile("1.obj");
BranchGroup b2 = new objFile("2.obj");
objScale.addChild(createObject (b1, -1.2f , 0.0f ));
objScale.addChild(createObject (b2, 1.2f , 0.0f ));
objRoot.compile();
return objRoot;
}
private Group createObject (BranchGroup b, float xpos, float ypos ) {
Transform3D t = new Transform3D ( );
t.setTranslation ( new Vector3f ( xpos, ypos, 0.0f ) );
TransformGroup objTrans = new TransformGroup(t);
TransformGroup spin = new TransformGroup();
spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
spin.addChild(b);
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, spin, 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);
objTrans.addChild ( spin );
return objTrans ;
}
public Obj4(String args[]) {
setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(null);
add("Center", c);
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new Obj4(args), 400,400);
}
}
//end of Obj4.java
--
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 202.192.158.89]
--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: sunsoft.bbs@bbs.net.]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:206.902毫秒