1 / 44

Java 3D API, Sound

Java 3D API, Sound. James Atlas July 31, 2008. Review. J2EE. Today. Java 3D Java Media Framework (Sound). What is Java 3D?. Java 3D is: Standard extension to the Java 2 JDK An interface for writing programs to display and interact with 3D graphics

benard
Download Presentation

Java 3D API, Sound

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java 3D API, Sound James Atlas July 31, 2008

  2. Review • J2EE James Atlas - CISC370

  3. Today • Java 3D • Java Media Framework (Sound) James Atlas - CISC370

  4. What is Java 3D? • Java 3D is: • Standard extension to the Java 2 JDK • An interface for writing programs to display and interact with 3D graphics • Provides a collection of high-level constructs for creating and manipulating 3D geometry and structures for rendering that geometry • It is now a community source project developed on java.net (https://java3d.dev.java.net/) James Atlas - CISC370

  5. What can Java 3D do? • Java 3D includes a rich and extensible feature set for • Building shapes • Composing behaviors • Interacting with the environment and the user • Controlling rendering details • Java 3D enables: • Quick development of complex 3D applications • Fast and efficient implementation on a variety of platforms, from embedded systems to PCs to high-end workstations James Atlas - CISC370

  6. Advantages and Disadvantages • Advantages • It enables fast Development, so developers do not need to manipulate rendering details, they can focus on visual contents • Cross-platform • Disadvantages • It has lower speed than C++/OpenGL James Atlas - CISC370

  7. Online Java 3D Resources • Homepage of Java 3D • https://java3d.dev.java.net/ • Java 3D API Documentation, Examples and Downloads • https://java3d.dev.java.net/binary-builds.html • Java 3D Tutorial (Chapter 0-7, highly recommended) • http://java.sun.com/developer/onlineTraining/java3d/index.html • Java 3D interest group mail archive (searchable): • http://archives.java.sun.com/archives/java3d-interest.html • The Java 3D Community Site • http://www.j3d.org/ James Atlas - CISC370

  8. Java3D API useful packages • Core classes • javax.media.j3d package, it includes the lowest-level classes necessary in Java 3D programming • Utility classes • com.sun.j3d.utils package, it is convenient and powerful additions to the core • It has 4 major categories: content loaders, scene graph construction aids, geometry classes, and convenience utilities • Abstract Windowing Toolkit (AWT) • java.awt package, it creates a window to display the rendering • Vector math classes • javax.vecmath package, it defines vector math classes for points, vectors, matrices, and other mathematical objects James Atlas - CISC370

  9. Virtual Universe and Scene Graph • A Java 3D program describes a virtual universe,which is to be rendered • A virtual universe is created from a scene graph • The scene graph is assembled from objects to define the geometry, sound, lights, location, orientation, and appearance of visual and audio objects James Atlas - CISC370

  10. How to Build a Scene Graph • A scene graph is composed of nodes and arcs. A node is a data element, and arc is a relationship between data elements • The nodes in the scene graph are the instances of Java 3D classes. The arcs represent the two kinds of relationship between the Java 3D instances James Atlas - CISC370

  11. Two relationships • Parent-child relationship • A parent node can have any number of children but only one parent • A child node can have one parent and no children • Reference relationship • A reference associates a NodeComponentobject with a scene graph Node. NodeComponent objects define the Geometry, Appearance, Material and Texture attributes used to render the visual objects • For a single node, it can have many references James Atlas - CISC370

  12. A Scene Graph Example James Atlas - CISC370

  13. Concepts • Each scene graph has a single Virtual Universe • The Virtual Universe object has a Locale object (landmark used to determine the location of visual objects). It is the root of multiple sub-graphs • A BranchGroup object is the root of a sub-graph (branch graph) • Two kinds of sub-graph • view branch graph • viewing parameters (viewing location and direction) • content branch graph • Geometry, Appearance, Material, Texture, … James Atlas - CISC370

  14. SceneGraphObject class • SceneGraphObject is the superclass for nearly every Core and Utility Java 3D class. It has two subclasses: • Node • Group • BranchGroup, TransformGroup • Leaf • Shape3D, Light, Behavior, and Sound (have no children but may reference NodeComponents) • NodeComponent • Geometry, Appearance, Material, Texture, … (may be referenced by A Leaf) James Atlas - CISC370

  15. Some Key Classes • Node and NodeComponent • superclasses for all the items that are added to a scene graph • BranchGroup • A Container for other objects in the scene • Every item in the scene graph is either a BranchGroup, or is a child of a BranchGroup • A BrachGroup can be a child of another BranchGroup • Shape3D • Contains information about an object in the scene • Contains references to Geometry, Appearance, Material, Texture, … • Appearance • Defines the way a shape appears in the scene, including texture, and attributes for many other settings • TransformGroup • Allows translations, scales and rotations to be preformed • Geometry • Superclass for all classes that allow you to add form to a shape • Subclasses: LineArray, TriangleArray, .... James Atlas - CISC370

  16. Shape • A Shape is composed of Geometry and Appearance: Shape Geometry Appearance James Atlas - CISC370

  17. Geometry • There are several primitive Geometry classes available: • Box • Cylinder • Cone • Sphere • Text2D James Atlas - CISC370

  18. Appearance • Color • Texture • Material • Other attributes (point, polygon, rendering, etc) James Atlas - CISC370

  19. A Recipe for Writing Java 3D Programs • 1. Create a Canvas3D object • 2. Create a VirtualUniverse object • 3. Create a Locale object, attaching it to the VirtualUniverse object • 4. Construct a view branch graph • a. Create a View object • b. Create a ViewPlatform object • c. Create a PhysicalBody object • d. Create a PhysicalEnvironment object • e. Attach ViewPlatform, PhysicalBody, PhysicalEnvironment, and Canvas3D objects to View object • 5. Construct content branch graph(s) • 6. Compile branch graph(s) • 7. Insert subgraphs into the Locale James Atlas - CISC370

  20. The Simplest Recipe for Writing Java 3D Programs • 1. Create a SimpleUniverse object • 2. Construct content branch • 3. Insert content branch into the SimpleUniverse • 4. Optionally set nominal transformation (a normalization transform so you can see the whole scene) • Example Hello3d James Atlas - CISC370

  21. The SimpleUniverse Class • The SimpleUniverse object constructor creates a scene graph including VirtualUniverse and Locale objects, and a complete view branch graph • The view branch graph created by SimpleUniverse uses instances of ViewingPlatform and Viewer convenience classes in place of the core classes used to create the view branch graph • It significantly reduces the time and effort needed to create the view branch graph James Atlas - CISC370

  22. Other Features • Appearance • Defining your own shapes • Transformations • Lighting • Textures • Animation • Interaction James Atlas - CISC370

  23. Appearance Color3f ambient = new Color3f(0.5f, 0.5f, 0.5f); Color3f emissive = new Color3f(0.0f, 0.0f, 0.5f); Color3f diffuse = new Color3f(0.0f, 0.0f, 1.0f); Color3f specular = new Color3f(1.0f, 1.0f, 1.0f); float shininess = 20.0f; Appearance shinyBlueApp = new Appearance(); Material shinyBlueMat = new Material(ambient, emissive, diffuse, specular, shininess); shinyBlueApp.setMaterial(shinyBlueMat); bg.addChild(new Cylinder(0.5f, 4.0f, shinyBlueApp)); James Atlas - CISC370

  24. Define your own shape types • Use inheritance to extend Shape3d • Use QuadArrays to define faces, coordinates, etc. • p1 = new Point3d(-30,0,45); • p2 = new Point3d(30,20,45); • QuadArray polygon = new QuadArray(4, QuadArray.COORDINATES|QuadArray.NORMALS); • polygon.setCoordinate(0, p1); • polygon.setCoordinate(1, p2); James Atlas - CISC370

  25. Transformations • All the usual suspects… • rotation • translation • scaling • Anything that is a child node (in the scene graph) of a TransformGroup is affected by that TransformGroup’s Transforms! • TransformDemo.java James Atlas - CISC370

  26. Transformation //set up the transformation Transform3D trans = new Transform3D(); trans.setTranslation(new Vector3f(-3.0f,0.0f,0.0f)); trans.setScale(new Vector3d(4.0,0.25,4.0)); trans.setRotation(new AxisAngle4d(1.0,1.0,0.0,Math.PI/2.0)); //set up a group to use the transformation TransformGroup tg = new TransformGroup(trans); bg.addChild(tg); tg.addChild(new Cylinder(0.5f, 4.0f, shinyBlueApp)); James Atlas - CISC370

  27. Nesting groups • You can add Shapes directly to the BranchGroup • Or you can add them to a TransformGroup and make that a child of the BranchGroup… • You can nest the TransformGroups James Atlas - CISC370

  28. Translation trans.setTranslation(new Vector3f(-3.0f,0.0f,0.0f)); James Atlas - CISC370

  29. Scaling trans.setScale(new Vector3d(4.0,0.25,4.0)); James Atlas - CISC370

  30. Rotation trans.set(new AxisAngle4d(1.0,1.0,0.0,Math.PI/2.0)); James Atlas - CISC370

  31. Lighting protected BranchGroup createContentBranch(){ BranchGroup bg = new BranchGroup(); . . . addLights(bg); return bg; } protected void addLights(BranchGroup b){ . . . } James Atlas - CISC370

  32. Ambient Light protected void addLights(BranchGroup bg){ BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0); Color3f ambientColour = new Color3f(0.2f,0.2f,0.2f); AmbientLight ambLight= new AmbientLight(ambientColour); ambLight.setInfluencingBounds(bounds); bg.addChild(ambLight); } James Atlas - CISC370

  33. Directional Light protected void addLights(BranchGroup bg){ Color3f dirColour = new Color3f(1.0f,1.0f,1.0f); Vector3f lightDir = new Vector3f(-1.0f,-1.0f,-1.0f); DirectionalLight dirLight= new DirectionalLight(dirColour,lightDir); dirLight.setInfluencingBounds(bounds); bg.addChild(dirLight); } James Atlas - CISC370

  34. Other lights • Point lights • Spot lights James Atlas - CISC370

  35. Textures TextureLoader tl = new TextureLoader("./resources/wood.jpg", null); Texture texture = tl.getTexture(); texture.setBoundaryModeS(Texture.WRAP); texture.setBoundaryModeT(Texture.WRAP); texture.setBoundaryColor( new Color4f( 0.0f, 1.0f, 0.0f, 0.0f ) ); // Set up the texture attributes // could be REPLACE, BLEND or DECAL instead of MODULATE TextureAttributes texAttr = new TextureAttributes(); texAttr.setTextureMode(TextureAttributes.REPLACE); Appearance ap = new Appearance(); ap.setTexture(texture); ap.setTextureAttributes(texAttr); //set up the material ap.setMaterial(new Material()); // Create a ball to demonstrate textures int primflags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS; Sphere sphere = new Sphere(0.5f, primflags, ap); group.addChild(sphere); James Atlas - CISC370

  36. Animation // Create the TransformGroup node and initialize it to the // identity. Enable the TRANSFORM_WRITE capability so that // our behavior code can modify it at run time. Add it to // the root of the subgraph. TransformGroup objTrans = new TransformGroup(); objTrans.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE); objRoot.addChild(objTrans); // Create a simple Shape3D node; add it to the scene graph. objTrans.addChild(new ColorCube(0.4)); // Create a new Behavior object that will perform the // desired operation on the specified transform and add // it into the scene graph. Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, 4000); RotationInterpolator rotator = new RotationInterpolator( rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI*2.0f); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); rotator.setSchedulingBounds(bounds); objRoot.addChild(rotator); James Atlas - CISC370

  37. Interaction Canvas3D c = new Canvas3D(config); c.addKeyListener(this); c.addMouseListener(this); frame.getContentPane().add("Center", c); .. publicvoid keyPressed(KeyEvent e) { // Invoked when a key has been pressed. if (e.getKeyChar() == 's') { xloc = xloc + .1f; } if (e.getKeyChar() == 'a') { xloc = xloc - .1f; } trans.setTranslation(new Vector3f(xloc, height, 0.0f)); objTrans.setTransform(trans); } James Atlas - CISC370

  38. Java 3D Game example • https://java3d.dev.java.net/applets/FourByFour.html James Atlas - CISC370

  39. Java Sound James Atlas - CISC370

  40. File Formats Supported .au or .snd : usually stores 8-bit m-law encoded samples, but can also store 8 or 16 bit linear samples .aif : usually stores 8 or 16 bit linear encoded samples .wav :Can store 8 or 16 bit samples using linear or m-law encoded samples .midi : follows the midi data format Note: The file header indicates the actual format Frames and Frame Rates Sample Frame Stores all the samples taken at an instant of time # of bytes in a frame = # of bytes in a sample X number of channels Frame Rate The number of frames per second of sound In most cases frame rate is same as sample rate In compressed sound, the frame rate will be less than sample rate. Java Sound Preliminaries James Atlas - CISC370

  41. Line Mixer DataLine Port TargetDataLine Clip SourceDataLine Java Sound API James Atlas - CISC370

  42. System Resources Audio Line: Any resource that is a source or destination of sampled sound data A line can encapsulate several channels Example: input/output ports on the sound card Lines have controls (gain and pan control) Mixer: Receives input from one or more source data lines and outputs the result of combining the input to an output line called a target data line Other Sound Sources A file or more generally a URL Terminology: A source data line is a source for a mixer, not a source for you; you write to it A target data line is the output from the mixer; your read from it Source Data Lines Target Data Lines Sound i/p Sound o/p MIXER Sound i/p Sound o/p Sound i/p Java Sound API James Atlas - CISC370

  43. Packages: javax.sound.sampled javax.sound.midi javax.sound.sampled.spi javax.sound.sampled.midi The AudioSystem class Establish whether a particular resource is available Get a ref. to the object that encapsulates the resource Call methods to operate the resource AudioStreamInput class Represents a stream that is a source of sampled sound data with a specific format You can create an AudioStreamInput object from a local sound file, from another input stream or a URL You can Read data from it Write its contents to an output stream Convert its format Java Sound API (…contd) James Atlas - CISC370

  44. Resource Descriptor Classes The Line, DataLine, Mixer and Port interface definitions each include an inner class with the name Info. Objects of these class types encapsulate data specifying an object of the corresponding type Specifies a Line object by its Class Line.Info Mixer.Info DataLine.Info Port.Info Specifies a DataLine by the audio formats to be supported, the buffer size limits, and the Class of the data line Specifies a Mixer object by its name, vendor version and description Specifies a Port object by its Class, its name, and whether it is a source or target line James Atlas - CISC370

More Related