Games 3 dl4-example

243 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
243
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Games 3 dl4-example

  1. 1. Computer Game Technologies Java 3D Example Computer Game Technologies, 2012 1 Java 3D Examples• Examples will often be taken from AndrewDavison, “Killer Game Programming”, O’Reilly 2005• Code and extra chapters available fromhttp://fivedots.coe.psu.ac.th/~ad/jg/• Java applications that embed the Canvas3D in aJPanel for inclusion within a Swing GUI Computer Game Technologies, 2012 2 1
  2. 2. Checkers3DThe scene consists of – a dark green and blue tiled surface (and red center) – labels along the X and Z axes – a blue background – a floating sphere lit from two different directions – the user (viewer) can move through the scene by moving the mouse Computer Game Technologies, 2012 3 Checkers3D Scene Graph • View branch not shown ( from Davison’s lectures) Computer Game Technologies, 2012 4 2
  3. 3. Checkers3D Code Structure • Windowed Java application • Java Swing plus Java3D • Ch k Checkers3D 3D – Main class that extends JFrame – WrapCheckers3D JPanel added to JFrame • WrapCheckers3D – Holds all the 3D code – Extends JPanel – Canvas3D added to JPanel C 3D dd d P l – SimpleUniverse – createSceneGraph() method Computer Game Technologies, 2012 5 Checkers3D Codepublic class Checkers3D extends JFrame{ public Checkers3D() { super("Checkers3D"); Container c = getContentPane(); c.setLayout( new BorderLayout() ); WrapCheckers3D w3d = new WrapCheckers3D(); c.add(w3d, BorderLayout.CENTER); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); pack(); setResizable(false); // fixed size display setVisible(true); } // end of Checkers3D() public static void main(String[] args) { new Checkers3D(); }} // end of Checkers3D class Computer Game Technologies, 2012 6 3
  4. 4. WrapCheckers3D Codepublic class WrapCheckers3D extends JPanel// Holds the 3D canvas where the loaded image is displayed{::::::: private SimpleUniverse su; private BranchGroup sceneBG; private BoundingSphere bounds; // for environment nodes public WrapCheckers3D() // A panel holding a 3D canvas { setLayout( new BorderLayout() ); setOpaque( false ); setPreferredSize( new Dimension(PWIDTH, PHEIGHT)); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas3D = new Canvas3D(config); add("Center", canvas3D); canvas3D.setFocusable(true); // give focus to the canvas canvas3D.requestFocus(); Computer Game Technologies, 2012 7 WrapCheckers3D Code (2) su = new SimpleUniverse(canvas3D); createSceneGraph(); initUserPosition(); i itU P iti () // set users viewpoint t i i t orbitControls(canvas3D); // viewpoint controls su.addBranchGraph( sceneBG ); } // end of WrapCheckers3D() Computer Game Technologies, 2012 8 4
  5. 5. The Content Scene Graph • The sphere, lights and backgroundprivate void createSceneGraph() // initilise the scene { sceneBG = new BranchGroup(); bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE); lightScene(); // add the lights addBackground(); // add the sky sceneBG.addChild( new CheckerFloor().getBG() ); floatingSphere(); // add the floating sphere sceneBG.compile(); // fix the scene } // end of createSceneGraph() Computer Game Technologies, 2012 9 Bounds bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE); • Much 3D scene rendering is computationally expensive i – The effect of lights – Behaviours that animate objects • Bounds define when to bother calculating lighting, animation etc – setInfluencingBounds for lights – setSchedulingBounds for behaviours h d li B d f b h i • Calculations only done if the current view intersects the bounds Computer Game Technologies, 2012 10 5
  6. 6. Visible Object - The Sphere private void floatingSphere()// A shiny blue sphere located at (0,4,0){ // Create the blue appearance node Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f blue = new Color3f(0.3f, 0.3f, 0.8f); Color3f specular = new Color3f(0.9f, 0.9f, 0.9f); Material blueMat= new Material(blue, black, blue, specular, 25.0f); // sets ambient, emissive, diffuse, specular, shininess blueMat.setLightingEnable(true); Appearance blueApp = new Appearance(); blueApp.setMaterial(blueMat); Computer Game Technologies, 2012 11 The Sphere (2) // position the sphere Transform3D t3d = new Transform3D(); t3d.set( new Vector3f(0,4,0)); TransformGroup tg = new TransformGroup(t3d); tg.addChild(new Sphere(2.0f, tg addChild(new Sphere(2 0f blueApp)); // set its radius and appearance sceneBG.addChild(tg); } // end of floatingSphere() Computer Game Technologies, 2012 12 6
  7. 7. Lighting - Ambientprivate void lightScene() /* One ambient light, 2 directional lights */ { Color3f white = new Color3f(1 0f 1 0f 1 0f); Color3f(1.0f, 1.0f, 1.0f); // Set up the ambient light AmbientLight ambientLightNode = new AmbientLight(white); ambientLightNode.setInfluencingBounds(bounds); sceneBG.addChild(ambientLightNode); Computer Game Technologies, 2012 13 Lighting - Directional // Set up the directional lights Vector3f light1Direction = new Vector3f(-1.0f, -1.0f, -1.0f); // left, down, backwards Vector3f light2Direction = new Vector3f(1.0f, -1.0f, 1.0f); g ( , , ); // right, down, forwards DirectionalLight light1 = new DirectionalLight(white, light1Direction); light1.setInfluencingBounds(bounds); sceneBG.addChild(light1); DirectionalLight light2 = new DirectionalLight(white, light2Direction); light2.setInfluencingBounds(bounds); sceneBG.addChild(light2);} // end of lightScene() Computer Game Technologies, 2012 14 7
  8. 8. Coloured Background private void addBackground() // A blue sky { Background back = new Background(); back.setApplicationBounds( bounds ); pp ( back.setColor(0.17f, 0.65f, 0.92f); // sky colour sceneBG.addChild( back ); } // end of addBackground() Computer Game Technologies, 2012 15 Viewpoint Control private void orbitControls(Canvas3D c)/* OrbitBehaviour allows the user to rotate around the scene and zoom in and out. */{ OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL); ne OrbitBeha ior(c OrbitBeha ior REVERSE ALL) orbit.setSchedulingBounds(bounds); ViewingPlatform vp = su.getViewingPlatform(); vp.setViewPlatformBehavior(orbit);} // end of orbitControls() Computer Game Technologies, 2012 16 8
  9. 9. Initial Viewer Position • Viewer position needs to be set • Use the lookAt() method – Vi Viewer p siti n position – A point they are looking at – A vector specifying which way is up • Apply to viewer transform group Computer Game Technologies, 2012 17 Initialise Viewer Positionprivate void initUserPosition()// Set the users initial viewpoint using lookAt(){ ViewingPlatform vp = su.getViewingPlatform(); TransformGroup steerTG = vp.getViewPlatformTransform(); Transform3D t3d = new Transform3D(); steerTG.getTransform(t3d); // args are: viewer posn, where looking, up direction t3d.lookAt(USERPOSN, new Point3d(0,0,0), new Vector3d(0,1,0)); t3d.invert(); steerTG.setTransform(t3d); steerTG setTransform(t3d);} // end of initUserPosition() Computer Game Technologies, 2012 18 9
  10. 10. Alternative Viewer Position • setNominalViewingTransform() method – ViewPlatform utility method • Moves viewer position back along the Z-axis so Z axis that objects at the origin of size -1 to 1 along the x axis are fully viewable// SimpleUniverse is a Convenience Utility classSimpleUniverse simpleU = new SimpleUniverse(canvas3D);// This will move the ViewPlatform back a bit so the// objects in the scene can be viewed.simpleU.getViewingPlatform().setNominalViewingTransform(); Computer Game Technologies, 2012 19 The End Computer Game Technologies, 2012 20 10

×