Have done a test case but not that small lol, Ive tried to consolidate evrything i was doing into one class file.. i couldn't replicate the problem at first evrything worked ok!!!.... then i decided to add a texture << there we go something todo with multipass and textures.
import java.util.Enumeration;
import java.util.Hashtable;
import net.jtank.input.*;
import org.openmali.vecmath2.*;
import org.xith3d.base.Xith3DEnvironment;
import org.xith3d.loaders.texture.*;
import org.xith3d.loop.*;
import org.xith3d.render.*;
import org.xith3d.render.config.*;
import org.xith3d.scenegraph.*;
import org.xith3d.scenegraph.primitives.*;
import org.xith3d.ui.hud.HUD;
import org.xith3d.ui.hud.listeners.ButtonListener;
import org.xith3d.ui.hud.widgets.Button;
public class RenderpassTestCase extends InputAdapterRenderLoop implements ButtonListener{
private Xith3DEnvironment env=null;
private Tuple3f eyePos=new Vector3f(0.0f,0.0f,0.0f);
private Tuple3f viewFocus = new Vector3f( 0.0f, 0.0f, -1.0f );
private Tuple3f vecUp = new Vector3f( 0.0f, 1.0f, 0.0f );
private Canvas3D canvas=null;
private Hashtable<String,Body> bodies=new Hashtable<String,Body>();
private GeomObject sun1=null;
private GeomObject planet1=null;
private GalaxyLayer galLayer=null;
private ScaledLayer layer4=null;
private ScaledLayer layer3=null;
private ScaledLayer layer2=null;
private ScaledLayer layer1=null;
private HUD testHud=null;
private Point3f userPos=new Point3f(0.0f,0.0f,14959800000.0f);
/** Creates a new instance of StarEngine */
public RenderpassTestCase() {
super(128f);
env = new Xith3DEnvironment( this );
canvas=Canvas3DFactory.createWindowed(OpenGLLayer.JOGL_SWING, 800, 600, "Stars");
// canvas=Canvas3DFactory.createWindowed(OpenGLLayer.JOGL_AWT, 800, 600, "Stars");
canvas.setBackgroundColor(new Colorf(0.0f,0.0f,0.0f));
this.getInputManager().registerKeyboardAndMouse( canvas );
env.addCanvas( canvas );
env.getView().lookAt( eyePos, viewFocus, vecUp );
env.getView().setFrontClipDistance(0.1f);
env.getView().setBackClipDistance(30000.0f);
TextureLoader.getInstance().addTextureStreamLocator(new TextureStreamLocatorFile( "D:\\Java\\XithStars\\images\\") );
galLayer=new GalaxyLayer(100000);
layer4=new ScaledLayer(0.00000001f);
createDefaultDistant();
layer3=new ScaledLayer(0.00001f);
layer2=new ScaledLayer(0.001f);
layer1=new ScaledLayer(1.0f);
// testHud=new HUD(canvas,new Tuple2f(800.0f,600.0f),this.getOperationScheduler(),this);
env.addPerspectiveBranch(galLayer);
env.addPerspectiveBranch(layer4);
env.addPerspectiveBranch(layer3);
env.addPerspectiveBranch(layer2);
env.addPerspectiveBranch(layer1);
// env.addHUD(testHud,this.getInputManager());
this.begin();
}
public void createDefaultDistant(){
// create sun
sun1=new GeomObject(700000000.0f);
// new Body(<name>, <orbit time millis>, <orbit distance metres>, <spin time millis>, <parent body>, <geometry Group to represent this body>)
// Note sun has no parent body so set to null
// Note also GeomObject's are not added, they are added by engine when it has sorted which renderpass to add them to.
Body sunBody=new Body("Sol",1,0.0f,1000,null,sun1);
bodies.put("SUN1",sunBody);
// create planet 1
planet1=new GeomObject(7000000.0f);
// new Body(<name>, <orbit time millis>, <orbit distance metres>, <spin time millis>, <parent body>, <geometry Group to represent this body>)
bodies.put("PLANET1",new Body("Planet1",60000,14959800000.0f,100000,sunBody,planet1));
}
// String name = user name of Group for debugging purposes
// GeomObject tg = the Group to be moved
// int lvl = the lvl of layer/renderpass to move the Group to.
public void moveToView(String name,GeomObject tg, int lvl) {
System.out.println("Moving "+name+" to view "+lvl);
Group g=(Group)tg.getParent();
if(g!=null)g.removeChild(tg);
if(lvl==4){
// tg.setCurrLvl(3);
layer4.addChild(tg);
} else if(lvl==3){
// tg.setCurrLvl(2);
layer3.addChild(tg);
} else if(lvl==2){
// tg.setCurrLvl(1);
layer2.addChild(tg);
} else if(lvl==1){
// tg.setCurrLvl(0);
layer1.addChild(tg);
}
}
public void onKeyReleased(KeyReleasedEvent e) {
switch (e.getKeyCode()) {
case KeyCode.VK_ESCAPE:
this.end();
break;
}
}
protected void onFPSCountIntervalHit(float fps) {
super.onFPSCountIntervalHit( fps );
canvas.setTitle( "RenderpassTestCase, FPS: " + (int)fps );
}
protected void prepareNextFrame(long gameTime, long frameTime, TimingMode tm) {
super.prepareNextFrame( gameTime, frameTime, TimingMode.MILLISECONDS );
// userz-=0.1;
long time=System.currentTimeMillis();
Enumeration e=bodies.elements();
while(e.hasMoreElements()){
Body b=(Body)e.nextElement();
b.update(time,userPos);
Point3f p=new Point3f((float)b.getX(),0.0f,(float)b.getZ());
float dist=userPos.distance(p);
// System.out.println(b.getName()+" Dist= "+dist);
if(dist<=30000.0f && b.getCurrView()!=1){
moveToView(b.getName(),b.getGeomObject(),1);
b.setCurrView(1);
}else if(dist<=100000.0f && b.getCurrView()!=2){
moveToView(b.getName(),b.getGeomObject(),2);
b.setCurrView(2);
}else if(dist<=100000000.0f && b.getCurrView()!=3){
moveToView(b.getName(),b.getGeomObject(),3);
b.setCurrView(3);
}else if(dist>100000000.0f && b.getCurrView()!=4){
moveToView(b.getName(),b.getGeomObject(),4);
b.setCurrView(4);
}
}
env.getView().lookAt( eyePos, planet1.getPosition(), vecUp );
}
public void onButtonClicked(Button button, Object object) {
if(button.getUserObject().equals("EXIT_BUTTON"))this.end();
}
public static void main(String[] args) {
new RenderpassTestCase();
}
class GeomObject extends TransformGroup{
private TransformGroup spinTrans=null;
private Shape3D geom[]=new Shape3D[4];
private int currLvl=-1;
/**
* Creates a new instance of GeomObject
*/
public GeomObject(float radius) {
super();
spinTrans=new TransformGroup();
Texture tex = TextureLoader.getInstance().getTexture( "space\\anims\\sun1\\sphMap001.jpg" );
GeoSphere l3 = new GeoSphere( radius,6,tex);
Appearance app3 = l3.getAppearance();
app3.setMaterial( new Material( Colorf.ORANGE, Colorf.YELLOW,Colorf.WHITE, Colorf.RED,0.8f, Material.AMBIENT, true, false ) );
l3.setAppearance( app3 );
spinTrans.addChild(l3);
super.addChild(spinTrans);
}
public void incSpin(float amount){
Transform3D t1=new Transform3D();
Transform3D t2=new Transform3D();
t1.rotY(amount);
spinTrans.getTransform(t2);
t2.mul(t1);
spinTrans.setTransform(t2);
}
}
public class Body {
private String name="UNKNOWN";
private long orbPeriod=0;
private long revPeriod=0;
private double orbInc=0.0f;
private float revInc=0.0f;
private float orbDist=0.0f;
private Body par=null;
private GeomObject obj=null;
private long lasttime=0;
private double x=0.0f;
private double z=0.0f;
private double px=0.0f;
private double pz=0.0f;
private int currView=0;
/** Creates a new instance of Body */
public Body(String name,long orbitPeriod,float orbDistance,long spinTime,Body parent,GeomObject object) {
this.name=name;
orbPeriod=orbitPeriod;
orbDist=orbDistance;
revPeriod=spinTime;
par=parent;
obj=object;
orbInc=(double)(Math.PI*2.0f)/orbPeriod;
revInc=(float)(Math.PI*2.0f)/revPeriod;
System.out.println(name+" orbInc="+orbInc+" , revInc="+revInc);
}
public String getName(){
return name;
}
public int getCurrView(){
return currView;
}
public void setCurrView(int cv){
currView=cv;
}
public GeomObject getGeomObject(){
return obj;
}
public double getX(){
return x;
}
public double getZ(){
return z;
}
public void update(long time,Point3f userPos){
long upTime=time-lasttime;
lasttime=time;
/// do orbit rotation
if(par!=null){
px=par.getX();
pz=par.getZ();
}
x=(Math.sin(time*orbInc)*orbDist)+px;
z=(Math.cos(time*orbInc)*orbDist)+pz;
// System.out.println(name+" x="+x+" , z="+z+" : SysTime="+time);
Transform3D t3d=new Transform3D(new Vector3f((float)x-userPos.getX(),0.0f-userPos.getY(),(float)z-userPos.getZ()));
obj.setTransform(t3d);
// do spin rotation
obj.incSpin((float)upTime*revInc);
}
}
public class RenderLayer extends BranchGroup{
private TransformGroup scaleTrans=null;
/** Creates a new instance of RenderLayer */
public RenderLayer(float scale) {
super();
scaleTrans=new TransformGroup();
Transform3D t3d=new Transform3D();
t3d.setScale(scale);
scaleTrans.setTransform(t3d);
super.addChild(scaleTrans);
}
public void addChild(Node n){
scaleTrans.addChild(n);
}
public void removeChild(Node n){
scaleTrans.removeChild(n);
}
}
public class ScaledLayer extends RenderLayer{
public ScaledLayer(float scale) {
super(scale);
}
}
public class GalaxyLayer extends RenderLayer{
private Points pa=null;
public GalaxyLayer(int numStars) {
super(1.0f);
float x,y,z;
Vector3f pnts[]=new Vector3f[numStars];
for(int i=0;i<=numStars-1;i++){
x=(float)Math.random()*200.0f;
y=(float)Math.random()*200.0f;
z=(float)Math.random()*200.0f;
pnts[i]=new Vector3f(x-100.0f,y-100.0f,z-100.0f);
}
pa=new Points(pnts,1.0f,true,new Colorf(1f,1f,1f));
this.addChild(pa);
}
}
}
have fun!
John