📄 view_follow.java
字号:
/*
* Created on 2005-7-18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package View_Autoget;
import javax.media.j3d.Alpha;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.PositionPathInterpolator;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Quat4f;
import com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.RotPosPathInterpolator;
/**
* @author carso
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class View_follow {
/*
* float radius = 5.0f;
* int sections = 90;
* int period = 10000;
* //Array to store the coordinates along the circumpherence
* Point3f[] positions = new Point3f[sections + 1];
* //Array containing relative time stamps in the range 0.0..1.0
* //Constant velocity if the scale is linear
* float[] knots = new float[sections + 1];
* Quat4f[] quats = new Quat4f[sections + 1];
*
* 注意:在使用View_follow之前请先确定已经设置好了knots,quats,sections
* 否则,可能会出现NullPointerException之类的异常
*/
private int sections;
private Point3f[] positions;
private float[] knots;
private Quat4f[] quats;
private int period;
public void setKnots(float[] knot){
this.knots=knot;
}
public void setQuats(Quat4f[] quat){
this.quats=quat;
}
public void setPositions(Point3f[] position){
this.positions=position;
}
public View_follow(int section){
this.sections=section;
positions = new Point3f[sections + 1];
knots = new float[sections + 1];
quats = new Quat4f[sections + 1];
period = 10000;
}
public BranchGroup createViewGraph(Canvas3D canvas,TransformGroup tg) {
// All according to cookbook recipe
BranchGroup objRoot = new BranchGroup();
View view = new View();
ViewPlatform viewPlatform = new ViewPlatform();
//view.setViewPolicy();
PhysicalBody physicalBody = new PhysicalBody();
PhysicalEnvironment physicalEnvironment = new PhysicalEnvironment();
view.addCanvas3D(canvas);
view.setPhysicalBody(physicalBody);
view.setPhysicalEnvironment(physicalEnvironment);
view.attachViewPlatform(viewPlatform);
//The TransformGroup must be inserted into the BranchGraph
objRoot.addChild(tg);
// Here, the interpolators are defined, that modify tg. tgRot becomes
// the TransformGroup to which the ViewPlatform is attached.
TransformGroup tgRot = createDynamicTransform(tg, objRoot);
tgRot.addChild(viewPlatform);
objRoot.compile();
return objRoot;
}
public TransformGroup createDynamicTransform (TransformGroup orbitTG, BranchGroup objRoot) {
//Computing positions and their time stamps. To close the circle, the
//last point equals the first
/*以下代码设置是作为围绕一个特定物体(点)旋转的坐标
for (int i = 0; i <= sections; i++) {
positions[i] = new Point3f(
(float)Math.sin(i * 2 * Math.PI / sections) * radius,
0.0f,
(float)Math.cos(i * 2 * Math.PI / sections) * radius
);
knots[i] = i * (1.0f / sections);
}
*/
/*设置好position[]和knots[],此处的代码已经移到了Axis_info.Axis_info中
float j=-52.0f;
for(int i=0;i<=sections;i++)
{
positions[i]=new Point3f(
j+i,
3.0f,
5.0f
);
quats[i]=new Quat4f(0.0f,i,0.0f,0.0f);
knots[i]=i*(1.0f/sections);
}*/
Transform3D orbit_temp=new Transform3D();
//orbitTG.setTransform(orbit_temp);
orbit_temp.rotY(1.57);
//Define PositionPathInterpolator
//orbitTG is the TransformGroup to which the ViewPlatform is attached
//用PositionPathInterpolator也能实现跟随效果
//此处实现的是摄象头的移动,要实现物体跟随的效果,物体运动的诡计需要与此处摄象头移动的
//轨迹吻合
PositionPathInterpolator rotOrbitInt = new PositionPathInterpolator(
//RotPosPathInterpolator rotOrbitInt = new RotPosPathInterpolator(
new Alpha(-1, period),
orbitTG, // target
orbit_temp, // identity
knots,
//quats,
positions);
rotOrbitInt.setSchedulingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 400.0));
//Define new TransformGroup and RotationInterpolator
TransformGroup tgRot = new TransformGroup();
tgRot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tgRot.addChild(new ColorCube());
/*
RotationInterpolator rotYInt = new RotationInterpolator(
new Alpha(-1, period),
tgRot
);
rotYInt.setSchedulingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 10.0));
*/
//Add the two interpolators to the BranchGroup
objRoot.addChild(rotOrbitInt);
//objRoot.addChild(rotYInt);
//objRoot.addChild(rotYInt);
orbitTG.addChild(tgRot);
return tgRot;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -