Well as of this morning I went digging around in some Java3D websites/tutorials/downloads, and I found out that Java3D really isn't that complicated if you KNOW what to program/apply/setup.
I'm use to java2D, example: drawing out a shape,texturing it, and letting the KeyListener/cycle update everything, the movement, the graphics.
But it's not that easy in Java3D J found out..
So far what I have is a basic .obj file being loaded via J3D (No knowledge on J3D so i can't add movement), basic lighting, and a light color bieng applied to the model.
Picture of status:

Here's the code: (Only one class at the moment)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
public class Display extends JFrame implements Runnable, ActionListener, KeyListener {
public static float absX = 1.9F; public static float absY = 1.2F; public static float absZ = 6F; public static Timer time; public int sleep = 10;
private Canvas3D canvas;
private SimpleUniverse universe;
private BranchGroup root;
public static void main(String[] args) { try { new Display().setVisible(true); } catch (IOException ex) { ex.printStackTrace(); } }
public Display() throws IOException { configureWindow(); configureCanvas(); conigureUniverse(); addModelToUniverse(); addLightsToUniverse(); root.compile(); universe.addBranchGraph(root); time = new Timer(sleep, this); time.start(); }
private void configureWindow() { setTitle("Basic Java3D Program"); setSize(640, 480); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int absX1 = (screenSize.width - getWidth()) / 2; int absY1 = (screenSize.height - getHeight()) / 2; setLocation(200, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void configureCanvas() { canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration()); canvas.setDoubleBufferEnable(true); getContentPane().add(canvas, BorderLayout.CENTER); canvas.addKeyListener(this); }
private void conigureUniverse() { universe = new SimpleUniverse(canvas); universe.getViewingPlatform().setNominalViewingTransform(); }
private void addModelToUniverse() throws IOException { vpTranslation = new Transform3D(); Scene scene = getSceneFromFile("bin/res/objects/male02.obj"); root = scene.getSceneGroup(); ViewingPlatform vp = universe.getViewingPlatform(); TransformGroup vpGroup = vp.getMultiTransformGroup().getTransformGroup( 0); Vector3f translationVector = new Vector3f(absX, absY, absZ); vpTranslation.setTranslation(translationVector); vpGroup.setTransform(vpTranslation); }
public Transform3D vpTranslation;
private void addLightsToUniverse() { Bounds influenceRegion = new BoundingSphere(); Color3f lightColor = new Color3f(Color.BLUE); Vector3f lightDirection = new Vector3f(-1F, -1F, -1F); DirectionalLight light = new DirectionalLight(lightColor, lightDirection); light.setInfluencingBounds(influenceRegion); root.addChild(light); Background background = new Background(new Color3f(Color.LIGHT_GRAY)); background.setApplicationBounds(influenceRegion); root.addChild(background); }
public static Scene getSceneFromFile(String location) throws IOException { ObjectFile file = new ObjectFile(ObjectFile.RESIZE); return file.load(new FileReader(location)); }
public void keyPressed(KeyEvent k) { int key = k.getKeyCode(); int right = 39; if (key == right) { absY += 2000; Vector3f translationVector = new Vector3f(absX, absY, absZ); vpTranslation.setTranslation(translationVector); } System.out.println(key); }
public void keyReleased(KeyEvent k) { }
public void keyTyped(KeyEvent k) { }
public void actionPerformed(ActionEvent e) { run(); }
public void run() { try { absX += 0.1f; repaint(); Thread.sleep(sleep); } catch (InterruptedException e) { e.printStackTrace(); } } } |
My Questions:
1:
Did I setup everything right?2:
Would I repaint the JFrame, or the Canvas3D?3:
How can I interact with the model? (Via rotation/translation/camera angles).4:
Is there any methods that 'Override' a loop method for constant updating/initialization?5: As seen in the picture above, is there any way with Graphics2D or even Graphics to render above that layer? (Example: render fps count?)