objparse.java

来自「本项目是用JAVA3D开发的一款图形界面的3D漫游的类似引擎.至所以这么说.是因」· Java 代码 · 共 119 行

JAVA
119
字号
package cn.bz.collision;
/**
 * 此类专门用来导入obj文件.
 */
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import javax.media.j3d.BoundingBox;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Shape3D;
import javax.vecmath.Point3d;

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;

/**
 * BranchGroup
 * @author 李光
 *
 */
public class ObjParse extends BranchGroup{

	Scene scene = null;
	Hashtable hashtable = null ;
	List shape3DList = new ArrayList();
	
	public static KeyNavi keyNavi ;
	
	public ObjParse(String objFile) {
		addChild(createSceneGraph(objFile));
	}
	private BranchGroup createSceneGraph(String objFile){
		
		ObjectFile objectFile = new ObjectFile(ObjectFile.STRIPIFY);
		try {
			scene = objectFile.load(objFile);
			hashtable = scene.getNamedObjects();
			setColliBounds(hashtable);
			return scene.getSceneGroup();
		} 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 null ;
	}
	
	/*
	 * 这个只是一个简单的设置碰撞检测范围
	 */
	private void setColliBounds(Hashtable hashtable){
		Enumeration e = hashtable.elements();
		while(e.hasMoreElements())
		{
			Shape3D shape3D = (Shape3D)e.nextElement();
			shape3DList.add(shape3D);
		}
	}
	
	//返回obj文件中的object 个数
	public int getObjNumber(){
		return shape3DList.size();
	}
	
	public Shape3D getIndexShape3D(int i){
		return (Shape3D) shape3DList.get(i);
	}
	/*
	 * 对获得的shape3d对象进行碰撞范围扩大,以减少误差,防止进入指定范围
	 */
	public BoundingBox setOuterBoundingBox(BoundingBox boundingBox){
		if(boundingBox == null){
			throw new NullPointerException("范围没有设置,请设置对象范围");
		}
		Point3d pLower = new Point3d();
		Point3d pUpper = new Point3d();
		boundingBox.getLower(pLower);
		boundingBox.getUpper(pUpper);
		pLower.x = pLower.x - keyNavi.suduD - 0.5 ;
		pLower.z = pLower.z - keyNavi.suduD - 0.5 ;
		pUpper.x = pUpper.x + keyNavi.suduD + 0.5 ;
		pUpper.z = pUpper.z + keyNavi.suduD + 0.5 ;
		
		boundingBox.setLower(pLower);
		boundingBox.setUpper(pUpper);
		return boundingBox;
	}
	/*
	 * 对获得的shape3d对象进行碰撞范围减小,以减少误差,实现防止视野从指定范围内出来
	 */
	public BoundingBox setInnerBoundingBox(BoundingBox boundingBox){
		if(boundingBox == null){
			throw new NullPointerException("范围没有设置,请对象范围");
		}
		Point3d pLower = new Point3d();
		Point3d pUpper = new Point3d();
		boundingBox.getLower(pLower);
		boundingBox.getUpper(pUpper);
		pLower.x = pLower.x + keyNavi.suduD + 0.5 ;
		pLower.z = pLower.z + keyNavi.suduD + 0.5 ;
		pUpper.x = pUpper.x - keyNavi.suduD - 0.5 ;
		pUpper.z = pUpper.z - keyNavi.suduD - 0.5 ;
		
		boundingBox.setLower(pLower);
		boundingBox.setUpper(pUpper);
		return boundingBox;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?