Professional Documents
Culture Documents
Openscenegraph Tutorial Part Two
Openscenegraph Tutorial Part Two
Mikael Drugge Virtual Environments II Spring 2006 Based on material from http://www.openscenegraph.org/
SMM011
Agenda
Realism Loading models MilkShape Lightsources StateSets Materials Textures
SMM011 2
Introduction
SMM011
SMM011
Materials
Diffuse colour, the base colour Specularity/hardness, making plastics, metals, etc... Reflections, (not in the GL pipeline, but in e.g. raytracing) Emission, making objects glow or shine as if by light
Textures
Image mapping Bump mapping
SMM011 5
Creating models
Modeling via code
Easy for simple models But, requires a fair bit of imagination For example:
Sphere(0,-0.2,0,2); Sphere(1.3,1.4,0,1); Sphere(-1.3,1.4,0,1); Sphere(0,0,1.9,0.3);
SMM011 6
Whats this?
SMM011
MilkShape
SMM011
Introduction to MilkShape 3D
A basic low-polygon modeler Originally designed for a game (Half-Life) Supports basic operations
Select, move, rotate, scale, extrude, etc Low-level editing of vertices and faces Primitives (spheres, boxes, cylinders)
Introduction to MilkShape 3D
Why have we chosen to use MilkShape?
It is fairly intuitive to use and learn Its powerful enough for this course Handles a lot of different formats Allows for 30-day free trial
SMM011
10
SMM011
11
X Z
X
SMM011 12
Demo of MilkShape!
Learning by doing, so lets create something...
SMM011
13
Demo of MilkShape!
Lets see the result in OSG...
SMM011
14
Loading Objects
Exporting
Autodesk 3DS LightWave 6.5x LWO
Flattens all edges
Wavefront OBJ
Materials in an extra MTL file
SMM011
15
SMM011
16
Plug-Ins
OSGSim OSGText OSGParticle import openscenegraph.osgDB.osgDBNamespace; Handles text, Simulation (skybox, Special effects lights, time-of-day) (fire, smoke...) fonts, ...
SMM011 17
SMM011
19
Lightsources
SMM011
20
LightSource Light
SMM011
21
The Light
Encapsulates OpenGL glLight() functionality
setLightNum(), sets which GL light to operate on
setAmbient(), ambient colour of the light setDiffuse(), diffuse colour component setSpecular(), specular component Example fire in a fireplace
setDiffuse(orange), for the primary illumination setAmbient(darkred), for simulating the ambient light
SMM011 22
The Light
Light can be point source or spotlight
setPosition(), positions the light setDirection(), aims the spotlight in the given direction
SMM011
23
The Light
More spotlight functions
setSpotCutoff(distance) setSpotExponent(exp) (Not much documentation about these two functions experiment!)
SMM011
24
SMM011
25
Example
SMM011
26
Lighting
SMM011
Note this!
27
SMM011
28
I.e. make sure you set the colour for all components
setDiffuse(somecolour); setSpecular(somecolour); etc...
SMM011 32
StateSets
SMM011
33
What is a StateSet?
It encapsulates OpenGL state modes and attributes
Used for textures, lightsources, materials, transparency...
Materials
SMM011
36
Introduction to Materials
Not all objects are made of the same thing
Metals, glass, plastics, wood, paper, skin, crystal...
SMM011
37
Materials in OSG
The Material class
Encapsulates OpenGLs glMaterial state
Usage
Create a new Material and set its properties Apply the material to a StateSet via stateset.setAttribute(material); Apply the StateSet to e.g. a Geode geode.setStateSet(stateset);
SMM011 38
SMM011
40
setEmission
Can make an object look like it radiates light Instead of using a real lightsource theyre expensive E.g. to create a torch, make the flame emissive and place a lightsource within it, also use an ambient component
SMM011
41
SMM011
42
SMM011
44
Example a window
// you know this... shape = new ShapeDrawable( new Box(new Vec3fReference(-1f, -1f, 0f), 1f, 0.2f, 2f)); geode = new Geode(); stateset = new StateSet(); material = new Material(); // set the diffuse colour component to a lightblue nuance material.setDiffuse(MATERIALFace.FRONT_AND_BACK, new Vec4fReference(0.4f, 0.8f, 1f, 1f));
SMM011 45
Example a window
stateset.setMode(GL.GL_BLEND, STATEATTRIBUTEValues.ON_Val); stateset.setRenderingHint( STATESETRenderingHint.TRANSPARENT_BIN_Val); material.setTransparency( MATERIALFace.FRONT_AND_BACK, 0.6f); The higher the number
the more transparency
SMM011 46
Example a window
// set properties (you know this already ) stateset.setAttribute(material); geode.setStateSet(stateset); geode.addDrawable(shape); root.addChild(geode); Transparency is easy (from an API perspective), just remember to enable the GL_BLEND mode
SMM011 47
SMM011
48
Looks
Textures
SMM011
49
Introduction to Textures
Why do we need textures?
Difficult and expensive to model everything with polygons E.g. wooden surfaces, wallpapers, labels, faces, etc... Bumpmapping to simulate e.g. the texture of an orange Image maps are often cheaper in terms of computation
SMM011
50
Textures in OSG
The Texture class
Encapsulates OpenGLs texture functionality Texture1D/2D/3D subclasses for 1D/2D/3D Example Texture2D Y
X
SMM011 51
Texture coordinates
ShapeDrawables have texture coordinates defined
I.e. create the shape and just apply the texture to it
SMM011
52
Texture coordinates
// a rectangle
0 Vec2Array texcoords = new Vec2Array(); texcoords.push_back(new Vec2f(0.00f, 0.00f)); texcoords.push_back(new Vec2f(1.00f, 0.00f)); texcoords.push_back(new Vec2f(1.00f, 1.00f)); texcoords.push_back(new Vec2f(0.00f, 1.00f)); bbGeometry.setTexCoordArray(0, texcoords);
SMM011 53
Questions? Break...
SMM011
54
Agenda
Callbacks Animation Kinematics Morphing
SMM011
55
Callbacks
SMM011
56
Introduction to Callbacks
A callback is a function associated with an event
When the event occurs, the callback is invoked Developer registers callbacks, which are then automatically invoked by the system as needed
SMM011
57
Callbacks in OSG
Callbacks are user-defined functions executed upon tree traversal (e.g. when updating, drawing or culling)
Can associate a callback with a certain node Upon traversal, every node with a callback gets it executed
SMM011
58
Callbacks in OSG
Remember this code from the event loop?
viewer.sync(); viewer.update(); viewer.frame();
SMM011
59
Callbacks in OSG
Why not do it like this?
viewer.sync(); viewer.update(); ... put animation code in here ... viewer.frame();
The effect will be the same, but callbacks provide a better interface (simpler, cleaner, less cluttered, easier to maintain) Callbacks may increase performance on multithreaded systems
Callbacks code can execute in parallel
SMM011 60
Callbacks in OSG
In the Node class
setCullCallback(...) setEventCallback(...) setUpdateCallback()
Callbacks in OSG
A callback can access user data via
node.setUserData() node.getUserData()
The user data can be used to store node-specific information, such as the rotation of a gun turret User data should extend the Referenced class
(Reference counted class for garbage collection)
SMM011
62
Animation
SMM011
64
What is animation?
A motion picture made by photographing successive positions of inanimate objects (as puppets or mechanical parts)
...from the Merriam-Webster dictionary.
SMM011
65
SMM011
66
Quality of animations
Some cartoons look really good, some others dont Why? Manual animation by hand drawing is expensive! 24 fps, 90 minute movie, results in 129600 frames One way to make it cheaper is to reduce the number of frames via the framerate Some low budget TV cartoons use e.g. 12 or 6 fps Or, use a computer to do most of the work
SMM011 67
Keyframe Animation
Keyframe
A frame where objects are Positioned Oriented Scaled ...in a predefined way Based on time (normally)
00:00:00
00:00:30
SMM011
68
Keyframe Animation
Interpolating in between keyframes
You dont want to specify every single frame (Like we did earlier) Easier to set-up keyframes Let the computer interpolate
SMM011
70
SMM011
71
30s, (-5,10,32)
SMM011
72
Note that rotations are interpolated in between keyframes! What does this mean?
SMM011 73
Animation Path
Loop/repeat mode
Looping Repeat forever No loop Just play once Swinging Play once, then play backwards, then repeat
SMM011 74
SMM011
76
Example demonstration
AnimDemo.java
Moves in a square
SMM011
77
Anim Demo
Example demonstration
AnimDemo2.java
Moves and rotates
SMM011
79
Anim Demo2
SMM011
80
SMM011
81
What problems can this cause? Hint think about the performance of different computers...
loop { sync_time(); animate(); draw(); }
You need to keep track of real clock time, and do animation based on time, not on CPU performance!
SMM011 83
SMM011
84
SMM011
85
Matrix manipulators
The MatrixManipulator class
Abstract base class extending the standard GUIEventHandler Defines interface for controlling cameras in response to GUI events Subclasses (test with 1, 2, 3 in standard Viewer) TrackballManipulator The standard viewer mode DriveManipulator Left button accelerates, right breaks FlightManipulator Works like a flight simulator
SMM011 86
KeySwitchMatrixManipulator
Allows for easily changing among multiple manipulators E.g. make a certain key k switch to another manipulator
NodeTrackerManipulator
Make viewer track a node with/without rotations
TerrainManipulator
Make viewer follow the terrain (for rolling hills, etc)
SMM011 87
88
SMM011
89
Kinematics
Involves describing the position of parts based on joint angles
p = f()
Inverse kinematics
In kinematics we study
p = f()
I.e. based on a desired position of the object in the scene, what are the angles () to achieve it? E.g. a robotic arm
How to make the hand catch an object What angles should the joints be at May be none, one, or a set of solutions!
SMM011 91
Summary of kinematics
Its good to know the appropriate terms
Kinematics, p = f(), compute part position based on angles Inverse kinematics, = g(p), get angles for desired position
Morphing
SMM011
93
What is Morphing?
Extend the idea of animation to include vertices Change the shape of an object
Define certain shapes for different keyframes Interpolate in between the keyframes Produces a gradual shift of an objects shape
Some examples
Changing the face of a human into something else Turning a floor into the shape of a human being
SMM011 94
SMM011
95
3 4
0
97
1 2
3 4
3
SMM011
1
98
SMM011
99
SMM011
102
Summary
SMM011
103
Summary
Introduction
We often want our virtual world to look like the real world
Modeling objects
Introduction to MilkShape and 3D modeling programs Loading the creations as nodes into OSG
Lightsources
How to illuminate a scene and alter its properties
Materials
Basic GL parameters for changing the look of objects
Textures
An introduction on how to use textures within OSG
SMM011 104
Summary
Callbacks
Definition of callbacks, how to implement them in OSG
Animation
Basics of animation, frames and keyframes Computer and hand-drawn animation, similarities and differences Animating objects with OSG functions or manually
Kinematics
A brief introduction to the area Applicable in e.g. robotics
Morphing
Idea behind morphing, some examples on when to use it What to think about, pseudo-code for linear morphing
SMM011 105
Questions?
SMM011
106