我的java3d学习(一)——显示obj模型
最近被3d漫游所吸引,所以尝试学习一下java3d。
类关系图:
最近对3d漫游感兴趣,尝试学习一下。Java3D入门资料收藏夹里有详细介绍,此处不再啰嗦了。
在网上下了个人物模型,写了个类载入效果如下:
模型下载链接:
http://www.blogjava.net/Files/Man/csMANmodule.part2.rar
http://www.blogjava.net/Files/Man/csMANmodule.part1.rar
纹理链接:
http://www.blogjava.net/images/blogjava_net/Man/42654/t_flooring.jpg
源代码:
main类
package com.zzl.j3d.viewer;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.net.URL;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Group;
import javax.media.j3d.Texture;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;
import com.zzl.j3d.loader.J3DLoader;
public class Main extends Applet{
private SimpleUniverse universe ;
private Canvas3D canvas;
private BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);
TransformGroup objTransG = null;
public void Main()
{}
public void setupView()
{
/** Add some view related things to view branch side of scene graph */
// add mouse interaction to the ViewingPlatform
OrbitBehavior orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL|OrbitBehavior.STOP_ZOOM);
orbit.setSchedulingBounds(bounds);
ViewingPlatform viewingPlatform = universe.getViewingPlatform();
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
viewingPlatform.setNominalViewingTransform();
viewingPlatform.setViewPlatformBehavior(orbit);
}
public void init()
{
setLayout(new BorderLayout());
GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
canvas = new Canvas3D(gc);
add("Center",canvas);
// Create a simple scene and attach it to the virtual universe
universe = new SimpleUniverse(canvas);
// 定义观察点,可用鼠标进行缩放、旋转
setupView();
J3DLoader loader = new J3DLoader();
// 定义场景的根结点
BranchGroup objRoot = new BranchGroup();
// 载入人物模型
Scene scene = loader.loadObj("Liit.obj");
// 定义载入位置
Transform3D pos1 = new Transform3D();
pos1.setTranslation(new Vector3f(0f,0.0f,0f));
// 因为此处所采用的模型载入时是平行于y=0面的,所以要绕x轴旋转90度
pos1.rotX(-1.57);
objTransG = new TransformGroup();
objTransG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTransG.setTransform(pos1);
objTransG.addChild(scene.getSceneGroup());
objRoot.addChild(objTransG);
// 载入纹理
URL texImage = loader.loadTexture("resource/flooring.jpg");
// 画了一个大矩形,作为地板
Group group = this.createSceneBackGround(texImage);
objRoot.addChild(group);
universe.addBranchGraph(objRoot);
}
public void destroy()
{
universe.removeAllLocales();
}
/**
* @param args
*/
public static void main(String[] args) {
new MainFrame(new Main(), 256, 256);
}
public Group createSceneBackGround(URL texImage)
{
return createGeometry(Texture.MULTI_LEVEL_POINT , -1.1f, texImage);
}
public Group createGeometry(int filter, float y, URL texImage)
{
Group topNode = new Group();
Appearance appearance = new Appearance();
TextureLoader tex = new TextureLoader(texImage, TextureLoader.GENERATE_MIPMAP , this);
Texture texture = tex.getTexture();
texture.setMinFilter(filter);
appearance.setTexture(texture);
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
appearance.setTextureAttributes(texAttr);
//TODO light
Transform3D pos2 = new Transform3D();
pos2.setTranslation(new Vector3f(0f,y,0f));
objTransG = new TransformGroup();
objTransG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTransG.setTransform(pos2);
Box box = new Box(6f,0.1f,8f,Primitive.GENERATE_NORMALS|Primitive.GENERATE_TEXTURE_COORDS,appearance);
objTransG.addChild(box);
topNode.addChild(objTransG);
return topNode;
}
}
模型、纹理载入类:
package com.zzl.j3d.loader;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
public class J3DLoader {
private static java.net.URL texImage = null;
/**
* 载入obj模型
* @param arg0 String 模型文件名
* @return Scene
*/
public Scene loadObj(String arg0)
{
int flags = ObjectFile.RESIZE;
ObjectFile f = new ObjectFile(flags,(float)(49.0 * Math.PI / 180.0));
// 创建场景叶节点
Scene s = null;
try {
s = f.load(arg0);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IncorrectFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParsingErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
/**
* 载入纹理图片
* @param args String 图片文件名
* @return URL
*/
public URL loadTexture(String args)
{
File f = new File(args);
try {
texImage = f.toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return texImage;
}
}