Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Richard Jones, 2009 CO641 Computer Graphics 1 CO641 Computer Graphics and Animation X3d / VRML Richard Jones SW107

Similar presentations


Presentation on theme: "© Richard Jones, 2009 CO641 Computer Graphics 1 CO641 Computer Graphics and Animation X3d / VRML Richard Jones SW107"— Presentation transcript:

1 © Richard Jones, 2009 CO641 Computer Graphics 1 CO641 Computer Graphics and Animation X3d / VRML Richard Jones R.E.Jones@kent.ac.uk SW107 http://www.cs.kent.ac.uk/teaching/10/modules/CO/6/41/rej/

2 © Richard Jones, 2009 CO641 Computer Graphics 2 3D worlds Objects (International Space Station) Tours: building

3 © Richard Jones, 2009 CO641 Computer Graphics 3 Constructing 3d worlds OpenGL www.openGL.org DirectX www.microsoft.com/directx Java 3d java.sun.com/products/java-media/3D/ www.java3d.org X3D www.web3d.org/x3d/ Virtual Reality Modelling Language (VRML) www.web3d.org/x3d/vrml/

4 © Richard Jones, 2009 CO641 Computer Graphics 4 VRML Markup language for 3D scenes Widely supported, non-proprietary, scene description language animation (scripts, sensors, interpolators, event routes) VRML 2.0 (VRML97) specifications. VRML files MIME type is "model/vrml ” Filename extensions is ".wrl ” Plugins for browsers Cortona (good for developing) Cosmoplayer (nicer user interface but crashes nastily)

5 © Richard Jones, 2009 CO641 Computer Graphics 5 X3D Open software standard. Successor to VRML. New features, APIs, encoding formats, stricter conformance, componentised architecture (profiles). XML encoding and Scene Authoring Interface. Event model, VRML left many decisions to the browser implementer. Compatible with Scalable Vector Graphics (SVG). New nodes.

6 © Richard Jones, 2009 CO641 Computer Graphics 6 Why? Simple syntax Learning curve is not steep Focus on graphics rather than complexities of library Easy to get quite nice images Simple animation models Helps Understanding of scene graph Understanding hierarchies Understanding of transformations

7 © Richard Jones, 2009 CO641 Computer Graphics 7 Syntax Transform { children Shape { appearance Appearance { material Material { diffuseColor 0 1 0 } } geometry { Cylinder { height 0.1 radius 0.5 } } } }.wrl,.x3dv.x3d

8 © Richard Jones, 2009 CO641 Computer Graphics 8 X3D/VRML basics X3D/VRML comes with a fixed set of objects (called nodes). X3D/VRML is a programming language. We ’ ll want to structure the code and create new (specific) nodes The prototyping feature allows complex objects to be created, reused, and changing certain characteristics when desired. It ’ s important to use good programming practice…just like you would in Java.

9 © Richard Jones, 2009 CO641 Computer Graphics 9 Creating objects X3d/VRML has a notion of a node A fundamental building block Objects –Cylinder, Cone, Box, SpotLight Containers –Shape (contains geometry node and appearance node) Hierarchies Nodes can contain nodes Each node contains fields (the parameters) Special nodes include: Hyperlinking, collision detection The node hierarchy is the scene graph.

10 © Richard Jones, 2009 CO641 Computer Graphics 10 Shape node Shape nodes are basic containers for geometry (Cylinder, Box, etc..) They allow two child nodes: appearance and geometry. #VRML V2.0 utf8 Shape { appearance Appearance { material Material { } } geometry Cylinder { } } #X3D V3.0 utf-8 mandatory file headers

11 © Richard Jones, 2009 CO641 Computer Graphics 11 VRML and HTML Embed VRML in a web page Embeded VRML test <embed src= "world.wrl" border=0 height="250" width="600">

12 © Richard Jones, 2009 CO641 Computer Graphics 12 Appearance node Add properties: colour, smoothness, shininess etc. Commonly used fields: material (diffuseColor, shininess, ambientIntensity, transparency) texture (ImageTexture, MovieTexture, PixelTexture) Material { diffuseColor 0.5 0.5 0.5 # red green blue = Gray shininess0.5 ambientIntensity 0.35 }

13 © Richard Jones, 2009 CO641 Computer Graphics 13 Geometry node #VRML V2.0 utf8 Shape { appearance Appearance { material Material { diffuseColor.5 0.5 shininess.5 } geometry Cylinder { radius 3 height6 sideTRUE topFALSE bottomTRUE } Important: centre of the object (0,0)

14 © Richard Jones, 2009 CO641 Computer Graphics 14 Scene graphs A VRML world is a hierarchy of nodes, including Shapes and transformations. Useful to think of this as a graph. Use transformations to scale, rotate and move objects.

15 © Richard Jones, 2009 CO641 Computer Graphics 15 Transformations Transform { rotation 1 0 0 1.57 translation 4 3.5 0 scale 1.7 1 0.7 children [ Inline { url “ frog.wrl ” } ] } Default values used for omitted fields. Order is always: scale—rotate—translate. axis (defined by origin to this point) angle in radians:  radians = 180° right-hand coords scaled relative to the origin x y z

16 © Richard Jones, 2009 CO641 Computer Graphics 16 Simple Example #X3D V3.0 utf-8 # first child - a red sphere # red NB: use a good layout style for readability.

17 © Richard Jones, 2009 CO641 Computer Graphics 17 #VRML V2.0 utf8 Transform { children [ NavigationInfo { headlight FALSE } # We'll add our own light DirectionalLight { direction 0 0 -1} # Light illuminating scene Transform { # Third child - a red sphere translation 3 0 1 children [ Shape { geometry Sphere { radius 2.3 } appearance Appearance { material Material { diffuseColor 1 0 0 } # Red } ] } Transform { # Fourth child - a blue box translation -2.4.2 1 rotation 0 1 1.9 children [ Shape { geometry Box { } appearance Appearance { material Material { diffuseColor 0 0 1 } # Blue } ] } ] # end of children for world } DRAW THE SCENE GRAPH

18 © Richard Jones, 2009 CO641 Computer Graphics 18 Title and information As with HTML, you can also add a title and information about the file: #VRML V2.0 utf8 WorldInfo { title “ A virtual picture of a house ” info “ VRML model of a house ” }...

19 © Richard Jones, 2009 CO641 Computer Graphics 19 Programming style 1.Start with the header #VRML 2.0 utf8 2.Global nodes WorldInfo metadata & scene setup. 3.Prototypes a ‘ constructor ’ — must be defined before you can create an instance of the prototyped type. 4.Scene hierarchy most scene hierarchies have one root node. 5.Scripts & Interpolators, placed anywhere often placed in complex nodes or after scene hierarchy. 6.Routes must appear after the nodes and fields they refer to often they are placed in complex nodes or after the scene hierarchy 7.Time sensors can appear after/before scene hierarchy.

20 © Richard Jones, 2009 CO641 Computer Graphics 20 Reuse It ’ s good software engineering practice to reuse code — in any language. There are 3 ways to do this: 1.Inline 2.DEF/USE 3.PROTO (and EXTERNPROTO)

21 © Richard Jones, 2009 CO641 Computer Graphics 21 Inline #VRML V2.0 utf8 Inline { url ” LowerPlatform.wrl ” } #X3D V3.0 utf-8 Reads its children from any URL (can be delayed until the Inline is displayed). Optional fields bboxSize and bboxCenter

22 © Richard Jones, 2009 CO641 Computer Graphics 22 DEF/USE A simple naming mechanism. Use for multiple instances, repetition... Appearance { texture DEF GRAINY ImageTexture {url ” grainy.jpg ” } # names a texture map... }... Appearance { texture USE GRAINY } # names a texture map......

23 © Richard Jones, 2009 CO641 Computer Graphics 23 Prototypes These are IMPORTANT They are the key to good practice, and the reason why most tools (which don ’ t use them) generate hard to maintain models. A PROTO defines a new node type in terms of pre-defined nodes (either built in nodes, or other prototypes). The type of the prototype is given by its root node. Its good practice to utilize prototypes rather than DEF/USE — make all your nodes into a prototype.

24 © Richard Jones, 2009 CO641 Computer Graphics 24 PROTO PROTO Seat [ ] { Shape { appearance Appearance { material Material { diffuseColor 0 0 1 } geometry Box {} } Once you ’ ve built your prototype, you can use it as below… Seat{} This gives a blue seat — it ’ s always blue.

25 © Richard Jones, 2009 CO641 Computer Graphics 25 PROTO parameters PROTO Seat [ field SFColor seatColor.5.5.5 ] { Shape { appearance Appearance { material Material { diffuseColor IS seatColor } geometry Box {} } By default Seat{} the seat is grey. But we can instantiate a seat of any colour, e.g. red Seat{seatColor 1 0 0} Note that only seatColor is public.

26 © Richard Jones, 2009 CO641 Computer Graphics 26 Node specifications The ISO standard gives the interface details to each node, etc. (refer to the spec), e.g. Sphere { field SFFloat radius 1 } Text { exposedField MFString string [] exposedField SFNode fontStyle NULL exposedField MFFloat length[] exposedField SFFloat maxExtent0.0 } SFNode (MFNode) The field is a single node (any number of nodes) field The field can only be set in the ‘constructor’ exposedField The field automatically has a set_ method ( set_string ) so that it can be changed by other nodes (e.g. scripts)

27 © Richard Jones, 2009 CO641 Computer Graphics 27 Advanced PROTO #VRML V2.0 utf8 PROTO SizedPositionedObject [ field SFColor baseColor.5.5.5 field SFVec3f position 0 0 0 field SFNode model NULL ] { Transform { children [ Shape { appearance Appearance { material Material { diffuseColor IS baseColor } geometry IS model } ] translation IS position } Group { children [ SizedPositionedObject { model Cone{} baseColor 1 0 0 position -3 0 0 } SizedPositionedObject { model Cylinder{} baseColor 1 1 0 position 0 0 0} ] }

28 © Richard Jones, 2009 CO641 Computer Graphics 28 Other VRML nodes 1.Box 2.Cylinder 3.Cone 4.Sphere 5.Coodinate 6.Pointsets 7.Indexed LineSets 8.Indexed Face Sets 9.Text 10. DirectionalLight 11. PointLight 12. SpotLight 13. AudioClip (sound) ElevationGrid (for terrain modelling) Extrusion

29 © Richard Jones, 2009 CO641 Computer Graphics 29 BoxCone Cone { field SFFloat bottomRadius 1 # (0,  ) field SFFloat height 2 # (0,  ) field SFBool side TRUE field SFBool bottom TRUE } Box { field SFVec3f size 2 2 2 # (0,  ) }

30 © Richard Jones, 2009 CO641 Computer Graphics 30 Shape nodes & fields... Fields specify attributes of the node Predefined name and can contain a certain type of value (integer “ 2 ” or float “ 2.523 ” ) Node using { } uses default values e.g. Cylinder has default radius of 1 and default height of 2 (0,0) at the middle #VRML V2.0 utf8 Shape { appearance Appearance { material Material { } } geometry Cylinder { radius 3 height 6 side TRUE top FALSE bottom TRUE }

31 © Richard Jones, 2009 CO641 Computer Graphics 31 Coordinate node Every piece of vertex-based geometry must include a list of the vertices to be used. The Coordinate nodes allow the vertices to be described in 3D space Coordinate { point [ -1 -1 0, # point 0, 1 -1 0, # point 1, 1 1 0, # point 2, -1 1 0, # point 3, ] # end point } 0 1 3 2

32 © Richard Jones, 2009 CO641 Computer Graphics 32 geometry IndexedFaceSet { coord Coordinate { point [ # base of 3dtriangle 0 0 1, # point 0, center front 1 1 -1, # point 1, back right -1 1 -1, # point 2, back left 0 -1 -1, # point 3, top of 3dtriangle ] # end point } # end coord coordIndex [ # Need to go counter-clockwise # from the OUTSIDE. 0, 1, 2, 0, -1, 3,2,1,-1, # front 3,0,2,-1, # left 3,1,0,-1, # right ] # end coordIndex } # end geometry Indexed Face Sets

33 © Richard Jones, 2009 CO641 Computer Graphics 33 geometry IndexedFaceSet { coordCoordinate { point[ -5 -5 0, 5 -5 0, 5 5 0, -5 5 0, 0 0 5 ] } colorColor { color[ 1 0 0, 0 1 0, 0 0 1, 1 1 0, 0 1 1 ] } coordIndex[ 0, 1, 4, -1, 1, 2, 4, -1, 2, 3, 4, -1, 3, 0, 4, -1, 3, 2, 1, 0, -1 ] normalNULL texCoordNULL colorPerVertexTRUE } Indexed Face Sets Color per vertex..

34 © Richard Jones, 2009 CO641 Computer Graphics 34 #VRML V2.0 utf8 Shape { geometry Text { string “hello world” } Text Can also specify size, family (of fonts) and style geometry Text { string “hello world” fontStyle FontStyle { size 2.5 family “SERIF” style “ITALIC” }

35 © Richard Jones, 2009 CO641 Computer Graphics 35 #VRML V2.0 utf8 Shape { geometry IndexedLineSet { coord Coordinate { point [ #base of pyramid -2 0 2, # point 0, left and close -2 0 -2, # point 1, left and back 2 0 -2, # point 2, right and back 2 0 2, # point 3, right and close #top of pyramid 0 2 0, #point 4, top of pyramid ] # end point } # end coord coordIndex [ #base 0, 1, 2, 3, 0, -1, #vertical edges 0, 4, -1, 1, 4, -1, 2, 4, -1, 3, 4, -1, ] # end coordIndex } Indexed Line Set

36 © Richard Jones, 2009 CO641 Computer Graphics 36 ElevationGrid “ The ElevationGrid node specifies a uniform rectangular grid of varying height in the Y=0 plane of the local coordinate system. ”

37 © Richard Jones, 2009 CO641 Computer Graphics 37 VRML Links - anchor #VRML V2.0 utf8 Group { children [ Anchor { url "http://www.cs.ukc.ac.uk" children [ Shape { geometry Sphere { radius 2.3} appearance Appearance { material Material { diffuseColor 0 1 0 # Green } ] }# end Anchor ] }

38 © Richard Jones, 2009 CO641 Computer Graphics 38 Level Of Detail #VRML V2.0 utf8 LOD { range [ 10, 30, 60] level [ Shape { geometry Sphere { } } Shape { geometry Box { } } Shape { geometry Cone {} } ] }

39 © Richard Jones, 2009 CO641 Computer Graphics 39 Billboard Group { children [ Billboard { axisOfRotation 0 1 0 children [ Shape { appearance Appearance { texture ImageTexture { url "sphere.gif“ } } geometry Sphere { } } ] } ] } A special grouping node All children nodes will automatically face the observer The children nodes will rotate in a user defined axis Billboard { children [ ] axisOfRotation 0 1 0 bboxCenter 0 0 0 bboxSize -1 -1 -1 } Y-axis

40 © Richard Jones, 2009 CO641 Computer Graphics 40 Collision Transform { translation 700 100 -100 children [ Collision { collide TRUE proxy Shape { geometry Box { size 4 2 1 } } #children [ # Inline { url “complex.wrl”} #] } ] } Collision of the Avatar with objects. e.g. prevent you walking through a wall. Quite processor intensive. Best to put collision only over nodes that require it. Also simplify the geometry & use collision proxies. By default, collision detection is on.

41 © Richard Jones, 2009 CO641 Computer Graphics 41 I CAD X3D Profiles Target a common market or functionality. Interactive Core Interchange Immersive Full

42 © Richard Jones, 2009 CO641 Computer Graphics 42 Core: basic defs (e.g. ROUTE), no geometry, appearance,… Interchange: exchange models between 3D applications, geometries, appearances, keyframe animation Immersive: closest to VrML97, Interactive + new nodes Full: Interactive + Humanoid Animations and other advanced components

43 © Richard Jones, 2009 CO641 Computer Graphics 43 Interaction Anchor load up an HTML file or another VRML file Inline Load up an external VRML file into this one. Collision detection whilst moving round a scene Events: Sensor –effect the world from a mouse event –detect that the mouse is over me - and then change the world... Scripts (user defined programs..) Interpolators (pre-defined) Timers tick, tick... then after 6 seconds do something

44 © Richard Jones, 2009 CO641 Computer Graphics 44 Events As well as fields, some nodes have events. input events output events ROUTE events between nodes Group { children [ DEF LIGHT_CONTROL TouchSensor {} Inline{ url [ “lightswitch.wrl” ] } ] } DEF LIGHT PointLight { location -6 4 2 on FALSE } Inline {url [ “table.wrl” ] } ROUTE LIGHT_CONTROL.isActive TO LIGHT.on isActive on TouchSensor PointLight

45 © Richard Jones, 2009 CO641 Computer Graphics 45 Plane sensor Make object move ONLY along a plane Group { children [ DEF DRAG_ME PlaneSensor { minPosition 0 0 # start at 0,0 maxPosition 10 0 # move to 10,0 } DEF OBJ Transform { translation 0 0 0 children [ Inline { url [“geometry_to_move.wrl” ] } ] } ] } ROUTE DRAG_ME.translation_changed TO OBF.translation

46 © Richard Jones, 2009 CO641 Computer Graphics 46 Forms of animation Flip book predefined set of images consecutively replayed works on 2D bitmap images interaction: stop,play,rewind Key frame define some key frames interpolate between (in-betweening) interaction: stop,play,rewind,trajectory,re-action Procedural start with an object in space use physical laws (and other equations) to effect the object interaction: stop, play, rewind, mass, gravity, shape... gravity =10 reflection =90 o

47 © Richard Jones, 2009 CO641 Computer Graphics 47 Flip Book animation in VRML Have a series of predefined VRML geometry files. Place them in a switch statement, a choice Switch between them using a timer node Group { children [ DEF TOUCH TouchSensor { }, DEF TOGGLE Script {..holds current choice, and if called moves to next.... at end moves to first choice }, DEF SWITCH Switch { whichChoice 0 choice [ Inline { url "frame1.wrl" }, Inline { url "frame2.wrl" }, Inline { url "frame3.wrl" }, ] } ] ROUTE TOUCH.touchTime TO TOGGLE.toggle ROUTE TOGGLE.which TO SWITCH.whichChoice }

48 © Richard Jones, 2009 CO641 Computer Graphics 48 Interpolator Nodes Interpolator nodes are designed for linear key-framed animation. An interpolator node defines a piecewise-linear function, f(t), over the interval (-infinity, +infinity). The piecewise-linear function is defined by n values of t, called keys, and the n corresponding values of f(t), called keyValues. The keys must be monotonically non-decreasing, otherwise the results are undefined. The keys are not restricted to any interval.

49 © Richard Jones, 2009 CO641 Computer Graphics 49 Interpolators The following node types are interpolator nodes, each based on the type of value that is interpolated: ColorInterpolator CoordinateInterpolator NormalInterpolator OrientationInterpolator PositionInterpolator ScalarInterpolator Interpolator semantics: eventIn SFFloat set_fraction eventOut [S|M]F value_changed exposedField MFFloat key [...] exposedField MF keyValue [...]

50 © Richard Jones, 2009 CO641 Computer Graphics 50 Key frame in VRML Use an Engine (such as an Interpolator) Moving a cube (left to right) x + 3 enterTime startTime fraction set_fraction TriggerTimerEngine e.g Position- Interpolator Touch SensorTimerMover intensity Target Object value_changed

51 © Richard Jones, 2009 CO641 Computer Graphics 51 Key frame in VRML Group { children [ DEF TARGET_Xform Transform { children [ Shape { geometry Box { } } ] }, DEF TIMER TimeSensor { cycleInterval 2 loop TRUE }, DEF MOVER_POS PositionInterpolator { key [ 0.0, 0.5, 1 ] keyValue [ 0 0 0, 3 0 0, 0 0 0 ] } ] ROUTE TIMER.fraction TO MOVER_POS.set_fraction ROUTE MOVER_POS.value_changed TO TARGET_Xform.translation } Key frames... 2 seconds

52 © Richard Jones, 2009 CO641 Computer Graphics 52 Key frame animation example Using PositionInterpolator Timer

53 © Richard Jones, 2009 CO641 Computer Graphics 53 Script nodes The script node allows code to be incorporated. The script can be in various Scripting languages, JavaScript (or ECMASCRIPT) is the most commonly used. A script node activates when it receives an event. Script nodes receive events in timestamp order. Scripts that have access to other nodes (via SFNode/MFNode fields or eventIns) and that have their directOutput field set to TRUE may directly post eventIns to those nodes. They may also read the last value sent from any of the node's eventOuts.

54 © Richard Jones, 2009 CO641 Computer Graphics 54 Accessing eventIns and eventOuts of other nodes The script can access any eventIn or eventOut of any node to which it has access. access and modify an exposed field of another node (i.e., sends a set_translation eventIn to the Transform node) using ECMAScript: DEF SomeNode Transform { } DEF SomeScript Script { field SFNode tnode USE SomeNode eventIn SFVec3f pos directOutput TRUE url "javascript: function pos (value, timestamp) { tnode.set_translation = value; }" }

55 © Richard Jones, 2009 CO641 Computer Graphics 55 #VRML V2.0 utf8 Group { children [ DEF TOUCH TouchSensor {}, DEF TOGGLE Script { eventIn SFTime toggle eventOut SFInt32 which field SFInt32 thisVal 0 url "javascript: function toggle() { thisVal = thisVal >= 2 ? 0 : thisVal+1; which = thisVal; }" }, DEF SWITCH Switch { whichChoice 0 choice [ Inline { url "arrowDOWN.wrl" }, Inline { url "arrowUP.wrl" }, Inline { url "object.wrl" }, ] } ] ROUTE TOUCH.touchTime TO TOGGLE.toggle ROUTE TOGGLE.which TO SWITCH.whichChoice }

56 © Richard Jones, 2009 CO641 Computer Graphics 56 Initialize() shutdown() Initialize() method: invoked before the browser presents the world to the user before any events are processed by nodes in the same VRML file as the Script node. Events generated by the initialize() method have timestamps less than any other events generated by the Script node. Allowing script initialization tasks to be performed prior to the user interacting with the world. Shutdown() method invoked when the corresponding Script node is deleted or the world containing the Script node is unloaded or replaced by another world. used as a clean-up operation, e.g. remove temporary files. Events routed to those being removed will not get through! nothing of the script may be invoked after the shutdown() method has completed, though the shutdown() method may invoke methods or send events while shutting down.

57 © Richard Jones, 2009 CO641 Computer Graphics 57 Trick - Head Up Display (HUD) Head Up Displays usually called HUDs are a simple collection of nodes which ensure that objects will remain in the same position relative to the user regardless of the movement or rotation. They sense when the viewer has moved, then move the world to match appropriately.

58 © Richard Jones, 2009 CO641 Computer Graphics 58 # ProximitySensor to ensure HUD geometry moves with user # used in all HUD systems DEF GlobalProx ProximitySensor { size 1000 1000 1000 } DEF HUD Transform { children [ # needed to prevent collisions with nearby HUD geometry # not needed if far away (a backdrop) or just lighting Collision { collide FALSE children [..HUD geometry and/or lighting ] } ] } # Route user position and orientation to HUD ROUTE GlobalProx.position_changed TO HUD.translation ROUTE GlobalProx.orientation_changed TO HUD.rotation


Download ppt "© Richard Jones, 2009 CO641 Computer Graphics 1 CO641 Computer Graphics and Animation X3d / VRML Richard Jones SW107"

Similar presentations


Ads by Google