0 Replies - 5307 Views - Last Post: 02 October 2011 - 05:54 PM Rate Topic: -----

#1 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Java 3D API Clearing Canvas/Universe

Posted 02 October 2011 - 05:54 PM

Hi Guys,

I have been playing around with the Java 3D API, after reading too many tutorials it is still not clear, I do have something working in so much as there is a cube on the screen that can be rotated, but I dont know how to clear old branchgroups from the universe so all the old ones are there aswell, I dont fully understand the Canvas3D, SimpleUniverse, BranchGroup relationship or the rendering loop or how to use multiple buffers when working with J3D. Hopefully if someone would be so kind as to explain, or help me get this cube rotating on the screen as expected these things will become clearer. I am not even sure this is the right setup for a J3D app.


import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;

public final class JDoomFrame extends JFrame{
    
    public static void main(String[] args) 
    {
        JDoomFrame jdf = new JDoomFrame();
    }
    
    public JDoomFrame()
    {              
        init();
    }
    
    public void init()
    {
        /*
          Was going full screen exclusive mode but getting that 
          to work with canvas3d and Java 3D wasn't happening,
          without wrapping applets in frames etc...  So we cheat
        */
                
        //Set fullscreen
        this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
        //Restrict window resizing
        this.setResizable(false);
        //Remove window decoration
        this.setUndecorated(true);
        //Dont repaint the window
        this.setIgnoreRepaint(true);
        //When window wants to close, hit it with a sledge hammer for now
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Add 3D canvas
        this.add(new JDoom(), BorderLayout.CENTER);
        //Show it to the world
        this.setVisible(true);
    }
}




import java.awt.event.*;
import javax.swing.Timer;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.SimpleUniverse;

public final class JDoom extends Canvas3D implements ActionListener{
    
    public static final int TITLESCREEN = 0;
    public static final int GAME = 1;
    public static final int LOOSE = 2;
    
    private final int BUFFERS = 2;
     
    private State state;
    private Timer timer;
    
    public JDoom()
    {
        super(SimpleUniverse.getPreferredConfiguration());
        init();
    }
    
    public void init()
    {       
        //Center of attention
        this.setFocusable(true);
        this.requestFocus();
        //Add user input listener
        this.addKeyListener(InputManager.getManager()); 
        //Default state
        this.state = new TitleScreen(this);
        //Start timer
        timer = new Timer(30, this);
        timer.start();
    }
    
    public void changeState(int i)
    {
        switch(i)
        {
            case TITLESCREEN: state = new TitleScreen(this);
                break;
            default: /*Nothing*/;
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent e)
    {
        //Called every x milliseconds 
        //this.getGraphicsContext3D().clear();
        //this.getGraphicsContext3D().flush(true);
        state.input();
        state.update();
        state.render();       
    }
}




import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;

public class TitleScreen extends State{
    
    private InputManager userInput = InputManager.getManager();
    private SimpleUniverse universe;   
    private Locale locale;
    private BranchGroup temp, temp2;
    private double xRotation = 0.4;
    private double yRotation = 0.4;
    private float scale = 0.1f;
    
    public TitleScreen(JDoom r)
    {
        root = r;
        universe = new SimpleUniverse(root); 
        universe.getViewingPlatform().setNominalViewingTransform();
        temp = createSceneGraph();
        universe.addBranchGraph(temp);
    }
       
    @Override
    public void render()
    {
        temp = createSceneGraph();
        universe.addBranchGraph(temp);
    }
    
    @Override
    public void update()
    {
        
    }
    
    public BranchGroup createSceneGraph()
    {      
        BranchGroup objRoot = new BranchGroup();

        Transform3D rotate = new Transform3D();
        Transform3D tempRotate = new Transform3D();

        rotate.rotX(Math.PI/xRotation);
        tempRotate.rotY(Math.PI/yRotation);
        rotate.mul(tempRotate);
        TransformGroup objRotate = new TransformGroup(rotate);

        objRotate.addChild(new ColorCube(scale));
        objRoot.addChild(objRotate);
        objRoot.compile();
        return(objRoot);
    }
    
    @Override
    public void input()
    {
        if(userInput.KEYESC)
        {
            System.exit(0);
        }
        
        if(userInput.KEYUP)
        {
            xRotation -= 0.1d;
        }
        
        if(userInput.KEYDOWN)
        {
            xRotation += 0.1d;
        }
        
        if(userInput.KEYLEFT)
        {
            yRotation -= 0.1d;
        }
        
        if(userInput.KEYRIGHT)
        {
            yRotation += 0.1d;
        }
    }
}


The 2 other classes not related are

InputManager is just a singleton for user input
State is just an abstract class with 3 overridable methods implemented by any game states

Is This A Good Question/Topic? 0
  • +

Page 1 of 1