SlideShare a Scribd company logo
1 of 10
Download to read offline
Computer Game Technologies

              Java 3D Example




                 Computer Game Technologies, 2012   1




           Java 3D Examples
• Examples will often be taken from Andrew
Davison, “Killer Game Programming”, O’Reilly 2005
• Code and extra chapters available from
http://fivedots.coe.psu.ac.th/~ad/jg/

• Java applications that embed the Canvas3D in a
JPanel for inclusion within a Swing GUI




                 Computer Game Technologies, 2012   2




                                                        1
Checkers3D
The 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
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 Code
public 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
WrapCheckers3D Code
public 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 user's viewpoint
                                              t     '   i    i t
       orbitControls(canvas3D);          // viewpoint controls

       su.addBranchGraph( sceneBG );

     } // end of WrapCheckers3D()




                       Computer Game Technologies, 2012            8




                                                                       4
The Content Scene Graph
   • The sphere, lights and background
private 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
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
Lighting - Ambient

private 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
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
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 Position
private void initUserPosition()
// Set the user's 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
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 class
SimpleUniverse 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

More Related Content

What's hot

OpenGL L07-Skybox and Terrian
OpenGL L07-Skybox and TerrianOpenGL L07-Skybox and Terrian
OpenGL L07-Skybox and TerrianMohammad Shaker
 
Advanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics APIAdvanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics APITomi Aarnio
 
CS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and CullingCS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and CullingMark Kilgard
 
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019Unity Technologies
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utilsroxlu
 
Introduction to Game Programming Tutorial
Introduction to Game Programming TutorialIntroduction to Game Programming Tutorial
Introduction to Game Programming TutorialRichard Jones
 
Converting Scene Data to DOTS – Unite Copenhagen 2019
Converting Scene Data to DOTS – Unite Copenhagen 2019Converting Scene Data to DOTS – Unite Copenhagen 2019
Converting Scene Data to DOTS – Unite Copenhagen 2019Unity Technologies
 
Computer graphics practical(jainam)
Computer graphics practical(jainam)Computer graphics practical(jainam)
Computer graphics practical(jainam)JAINAM KAPADIYA
 
The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202Mahmoud Samir Fayed
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platformJussi Pohjolainen
 
Mashup caravan android-talks
Mashup caravan android-talksMashup caravan android-talks
Mashup caravan android-talkshonjo2
 

What's hot (20)

OpenGL L07-Skybox and Terrian
OpenGL L07-Skybox and TerrianOpenGL L07-Skybox and Terrian
OpenGL L07-Skybox and Terrian
 
Advanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics APIAdvanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics API
 
modm
modmmodm
modm
 
OpenGL L05-Texturing
OpenGL L05-TexturingOpenGL L05-Texturing
OpenGL L05-Texturing
 
CS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and CullingCS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and Culling
 
Cgm Lab Manual
Cgm Lab ManualCgm Lab Manual
Cgm Lab Manual
 
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019
 
Introduction to Data Oriented Design
Introduction to Data Oriented DesignIntroduction to Data Oriented Design
Introduction to Data Oriented Design
 
A Step Towards Data Orientation
A Step Towards Data OrientationA Step Towards Data Orientation
A Step Towards Data Orientation
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utils
 
Introduction to Game Programming Tutorial
Introduction to Game Programming TutorialIntroduction to Game Programming Tutorial
Introduction to Game Programming Tutorial
 
Converting Scene Data to DOTS – Unite Copenhagen 2019
Converting Scene Data to DOTS – Unite Copenhagen 2019Converting Scene Data to DOTS – Unite Copenhagen 2019
Converting Scene Data to DOTS – Unite Copenhagen 2019
 
Computer graphics practical(jainam)
Computer graphics practical(jainam)Computer graphics practical(jainam)
Computer graphics practical(jainam)
 
OpenVX 1.0 Reference Guide
OpenVX 1.0 Reference GuideOpenVX 1.0 Reference Guide
OpenVX 1.0 Reference Guide
 
Developing games for Series 40 full-touch UI
Developing games for Series 40 full-touch UIDeveloping games for Series 40 full-touch UI
Developing games for Series 40 full-touch UI
 
The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
 
Mashup caravan android-talks
Mashup caravan android-talksMashup caravan android-talks
Mashup caravan android-talks
 
Lambda expressions in C++
Lambda expressions in C++Lambda expressions in C++
Lambda expressions in C++
 
OpenVX 1.1 Reference Guide
OpenVX 1.1 Reference GuideOpenVX 1.1 Reference Guide
OpenVX 1.1 Reference Guide
 

Viewers also liked

мо англ.мова ош№1
мо англ.мова ош№1мо англ.мова ош№1
мо англ.мова ош№1zhdmiskvo
 
Estonia and estonian people
Estonia and estonian peopleEstonia and estonian people
Estonia and estonian peopleSlavaDov
 
Mindfulness presentation
Mindfulness presentationMindfulness presentation
Mindfulness presentationMikeKewley
 
м. жданівка виставка 2013
м. жданівка виставка  2013м. жданівка виставка  2013
м. жданівка виставка 2013zhdmiskvo
 

Viewers also liked (8)

мо англ.мова ош№1
мо англ.мова ош№1мо англ.мова ош№1
мо англ.мова ош№1
 
Bloque II
Bloque IIBloque II
Bloque II
 
MOTIVACION
MOTIVACIONMOTIVACION
MOTIVACION
 
Estonia and estonian people
Estonia and estonian peopleEstonia and estonian people
Estonia and estonian people
 
Bloque II
Bloque IIBloque II
Bloque II
 
ош № 2
ош № 2ош № 2
ош № 2
 
Mindfulness presentation
Mindfulness presentationMindfulness presentation
Mindfulness presentation
 
м. жданівка виставка 2013
м. жданівка виставка  2013м. жданівка виставка  2013
м. жданівка виставка 2013
 

Similar to Games 3 dl4-example

Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5Takao Wada
 
WPF L03-3D Rendering and 3D Animation
WPF L03-3D Rendering and 3D AnimationWPF L03-3D Rendering and 3D Animation
WPF L03-3D Rendering and 3D AnimationMohammad Shaker
 
Beginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeks
Beginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeksBeginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeks
Beginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeksJinTaek Seo
 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconftutorialsruby
 
Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Korhan Bircan
 
Trident International Graphics Workshop 2014 4/5
Trident International Graphics Workshop 2014 4/5Trident International Graphics Workshop 2014 4/5
Trident International Graphics Workshop 2014 4/5Takao Wada
 
Introduction to open gl in android droidcon - slides
Introduction to open gl in android   droidcon - slidesIntroduction to open gl in android   droidcon - slides
Introduction to open gl in android droidcon - slidestamillarasan
 
Lab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsLab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsRup Chowdhury
 
Gdc09 Minigames
Gdc09 MinigamesGdc09 Minigames
Gdc09 MinigamesSusan Gold
 
Embed the nasa world wind java sdk in eclipse
Embed the nasa world wind java sdk in eclipseEmbed the nasa world wind java sdk in eclipse
Embed the nasa world wind java sdk in eclipseMohammad Fajar
 
Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019Unity Technologies
 
Forge - DevCon 2016: Visual Reporting with Connected Design Data
Forge - DevCon 2016: Visual Reporting with Connected Design DataForge - DevCon 2016: Visual Reporting with Connected Design Data
Forge - DevCon 2016: Visual Reporting with Connected Design DataAutodesk
 
Game development with Cocos2d
Game development with Cocos2dGame development with Cocos2d
Game development with Cocos2dVinsol
 
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLLeaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLgerbille
 
introduction to CUDA_C.pptx it is widely used
introduction to CUDA_C.pptx it is widely usedintroduction to CUDA_C.pptx it is widely used
introduction to CUDA_C.pptx it is widely usedHimanshu577858
 
Unconventional webapps with gwt:elemental & html5
Unconventional webapps with gwt:elemental & html5Unconventional webapps with gwt:elemental & html5
Unconventional webapps with gwt:elemental & html5firenze-gtug
 
Ujug07presentation
Ujug07presentationUjug07presentation
Ujug07presentationBill Adams
 
Tools for developing Android Games
 Tools for developing Android Games Tools for developing Android Games
Tools for developing Android GamesPlatty Soft
 

Similar to Games 3 dl4-example (20)

Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5Trident International Graphics Workshop 2014 1/5
Trident International Graphics Workshop 2014 1/5
 
WPF L03-3D Rendering and 3D Animation
WPF L03-3D Rendering and 3D AnimationWPF L03-3D Rendering and 3D Animation
WPF L03-3D Rendering and 3D Animation
 
Beginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeks
Beginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeksBeginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeks
Beginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeks
 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
 
Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)
 
Learn Java 3D
Learn Java 3D Learn Java 3D
Learn Java 3D
 
CGLabLec6.pptx
CGLabLec6.pptxCGLabLec6.pptx
CGLabLec6.pptx
 
Trident International Graphics Workshop 2014 4/5
Trident International Graphics Workshop 2014 4/5Trident International Graphics Workshop 2014 4/5
Trident International Graphics Workshop 2014 4/5
 
Introduction to open gl in android droidcon - slides
Introduction to open gl in android   droidcon - slidesIntroduction to open gl in android   droidcon - slides
Introduction to open gl in android droidcon - slides
 
Lab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer GraphicsLab Practices and Works Documentation / Report on Computer Graphics
Lab Practices and Works Documentation / Report on Computer Graphics
 
Gdc09 Minigames
Gdc09 MinigamesGdc09 Minigames
Gdc09 Minigames
 
Embed the nasa world wind java sdk in eclipse
Embed the nasa world wind java sdk in eclipseEmbed the nasa world wind java sdk in eclipse
Embed the nasa world wind java sdk in eclipse
 
Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019Custom SRP and graphics workflows - Unite Copenhagen 2019
Custom SRP and graphics workflows - Unite Copenhagen 2019
 
Forge - DevCon 2016: Visual Reporting with Connected Design Data
Forge - DevCon 2016: Visual Reporting with Connected Design DataForge - DevCon 2016: Visual Reporting with Connected Design Data
Forge - DevCon 2016: Visual Reporting with Connected Design Data
 
Game development with Cocos2d
Game development with Cocos2dGame development with Cocos2d
Game development with Cocos2d
 
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLLeaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGL
 
introduction to CUDA_C.pptx it is widely used
introduction to CUDA_C.pptx it is widely usedintroduction to CUDA_C.pptx it is widely used
introduction to CUDA_C.pptx it is widely used
 
Unconventional webapps with gwt:elemental & html5
Unconventional webapps with gwt:elemental & html5Unconventional webapps with gwt:elemental & html5
Unconventional webapps with gwt:elemental & html5
 
Ujug07presentation
Ujug07presentationUjug07presentation
Ujug07presentation
 
Tools for developing Android Games
 Tools for developing Android Games Tools for developing Android Games
Tools for developing Android Games
 

Games 3 dl4-example

  • 1. Computer Game Technologies Java 3D Example Computer Game Technologies, 2012 1 Java 3D Examples • Examples will often be taken from Andrew Davison, “Killer Game Programming”, O’Reilly 2005 • Code and extra chapters available from http://fivedots.coe.psu.ac.th/~ad/jg/ • Java applications that embed the Canvas3D in a JPanel for inclusion within a Swing GUI Computer Game Technologies, 2012 2 1
  • 2. Checkers3D The 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. 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 Code public 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. WrapCheckers3D Code public 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 user's viewpoint t ' i i t orbitControls(canvas3D); // viewpoint controls su.addBranchGraph( sceneBG ); } // end of WrapCheckers3D() Computer Game Technologies, 2012 8 4
  • 5. The Content Scene Graph • The sphere, lights and background private 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. 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. Lighting - Ambient private 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. 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. 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 Position private void initUserPosition() // Set the user's 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. 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 class SimpleUniverse 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