Download - Documentt1
![Page 1: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/1.jpg)
SMM011 1
OpenSceneGraphTutorial
Mikael DruggeVirtual Environments II
Spring 2006
Based on material from http://www.openscenegraph.org/
![Page 2: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/2.jpg)
SMM011 2
Agenda
• Introduction to OpenSceneGraph (OSG)• Overview of core classes• Creating primitives• Transformations• Configuring the viewer• Event handlers• Special nodes• Picking objects
![Page 3: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/3.jpg)
SMM011 3
Introduction to OpenSceneGraph
![Page 4: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/4.jpg)
SMM011 4
What is OpenSceneGraph?
• A scenegraph system– One of the largest in the open source community
• Used for– Visual simulation, scientific modeling,
games, training, virtual reality, etc...• Some examples...
![Page 5: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/5.jpg)
SMM011 5
![Page 6: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/6.jpg)
SMM011 6
![Page 7: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/7.jpg)
SMM011 7
Some visual examples...
![Page 8: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/8.jpg)
SMM011 8
![Page 9: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/9.jpg)
SMM011 9
![Page 10: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/10.jpg)
SMM011 10
What is OpenSceneGraph?
• Tree structure (Directed Acyclic Graph)• Scene management
– Object oriented approach to graphics– Defines and manages a set of objects in a 3D world
• E.g. airports, offices, solar systems, etc...– Hierarchical structures
• E.g. cars, humans, robotic arms...• Optimizing graphics rendering
– Culling, sorting, level of detail, ...
![Page 11: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/11.jpg)
SMM011 11
Implementation of OpenSceneGraph
• C++ API built on OpenGL• Cross-platform
– Supports all platforms having OpenGL and C++• E.g. Windows, MacOSX, BSD, Linux, Solaris,
IRIX, Sony Playstation, etc…• (Not the XBox though.)
![Page 12: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/12.jpg)
SMM011 12
Layers
Applications
OpenGL
Graphics hardware
OpenSceneGraph
![Page 13: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/13.jpg)
SMM011 13
Design of OpenSceneGraphOSG
ScenegraphRendering elements
Tree/DAG nodes
OSGUtilTraversers
Enhancements
OSGDBDatabase loading
Plug-In management
OSGTextHandles text,
fonts, ...
OSGSimSimulation (skybox,lights, time-of-day)
OSGParticleSpecial effects(fire, smoke...)
Plug-Ins
JPGPNGGIF...
3DSOSGLWO
...
![Page 14: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/14.jpg)
SMM011 14
Accessing OSG from Java
![Page 15: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/15.jpg)
SMM011 15
JavaOSG
Java
OpenGL
Graphics hardware
OpenSceneGraph
JavaOSG / C++ glue
![Page 16: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/16.jpg)
SMM011 16
JavaOSG – Introduction
• Bindings for accessing OSG from Java– A set of native libraries (DLL:s in Windows)– A set of corresponding jar- and class-files for Java– Cross-platform
• C++ and Java code is similar, but not identical– Read the Javadocs (local copy on the course web)– Examples should help you get started
• JavaOSG webpage– http://www.noodleheaven.net/JavaOSG/javaosg.html
![Page 17: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/17.jpg)
SMM011 17
JavaOSG – Caveats
• JavaOSG is under development– ”Beta”, but stable enough– Not all of OSG is available
• Some features/functionality not yet implemented• A few problems caused by language differences, e.g.
multiple inheritance allowed in C++ but not in Java– API may change from one version to the next
Use JavaOSG version 0.3.3 for consistency
![Page 18: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/18.jpg)
SMM011 18
JavaOSG – Some known problems
• Java-specific documentation lacking– Still better than the alternatives =/– Examples, slides, javadocs, and OSG C++ code
• Compiling is a bit slow– Yes it is, the jar-files are quite large
• Callbacks– Some callbacks are troublesome and aren’t called
• Picking external models– Tricky, but there are some ways around this
• Learn the principles, not the tool
![Page 19: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/19.jpg)
SMM011 19
Installing
• OpenSceneGraph (use version 0.9.8)– http://www.openscenegraph.org/– Pre-compiled binaries with installer for Windows
• JavaOSG (use version 0.3.3)– http://www.noodleheaven.net/JavaOSG/javaosg.html– Pre-compiled libraries and jar-files for Windows
• Instructions– http://www.sm.luth.se/csee/courses/smm/009/osg_install.html
![Page 20: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/20.jpg)
SMM011 20
An overview of some OSG core classes
![Page 21: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/21.jpg)
SMM011 21
Nodes in the scenegraph tree (a subset)
• ”Node”, the base class– ”Group”, holds a set of child nodes
• ”Transform”, transforms all children by a 4x4 matrix• ”Switch”, switches between children, e.g. traffic lights• ”LOD”, level of detail, switch based on distance to viewer• ”LightSource”, leaf node defining a light in the scene
– ”Geode”, leaf node for grouping Drawables• ”Billboard”, orients Drawables to always face the viewer
![Page 22: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/22.jpg)
SMM011 22
Nodes in the scenegraph tree (a subset)
• ”Drawable”, abstract base class for drawable graphics– ”Geometry”, holds vertices, normals, faces, texture coords, ...– ”Text”, for drawing text– ”DrawPixels”, encapsulates drawing images using glDrawPixels
• ”StateSet”, encapsulates OpenGL states and attributes• ”Texture”, encapsulates OpenGL texture functionality
![Page 23: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/23.jpg)
SMM011 23
Example
Group
Transform
Geode
DrawableDrawable
Geode
DrawableDrawable
Matrix
The ”root” of the scene
Matrix definestransformation
![Page 24: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/24.jpg)
SMM011 24
Creating OpenGL primitives
![Page 25: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/25.jpg)
SMM011 25
How do we create this colour pyramid?
![Page 26: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/26.jpg)
SMM011 26
Creating the viewer...
import openscenegraph.osg.*;import openscenegraph.osgProducer.*;import openscenegraph.osgDB.osgDBNamespace;import noodle.noodleGlue.ShortPointer;import noodle.noodleGlue.IntReference;
public class PrimitiveGL {
public static void main(String[] args) {
//// create viewer//Viewer viewer = new Viewer();
viewer.setUpViewer(VIEWERViewerOptions.STANDARD_SETTINGS_Val);
![Page 27: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/27.jpg)
SMM011 27
Creating the geometry...
//// create the model//Group root = new Group();
// create a geometry for holding OpenGL primitives...Geometry pyramidGeometry = new Geometry();
// ..this will be a pyramid, so we specify the verticesVec3Array pyramidVertices = new Vec3Array();pyramidVertices.push_back(new Vec3fReference(-5, -5, 0)); // left front (0)pyramidVertices.push_back(new Vec3fReference( 5, -5, 0)); // right front (1)pyramidVertices.push_back(new Vec3fReference( 5, 5, 0)); // right back (2)pyramidVertices.push_back(new Vec3fReference(-5, 5, 0)); // left back (3)pyramidVertices.push_back(new Vec3fReference( 0, 0, 10)); // peak (4)
// ..then add the vertices to the geometrypyramidGeometry.setVertexArray(pyramidVertices);
0 123
4
![Page 28: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/28.jpg)
SMM011 28
Specifying the faces...
// next, we need to specify the 5 faces of the pyramid// (4 triangular sides, 1 quadratic base), like this...
{ // baseshort indices[] = {3, 2, 1, 0};ShortPointer indices_ptr = new ShortPointer(indices);pyramidGeometry.addPrimitiveSet(
new DrawElementsUShort(PRIMITIVESETMode.QUADS_Val,indices.length,indices_ptr));
}
{ // side 1short indices[] = {0,1,4};ShortPointer indices_ptr = new ShortPointer(indices);pyramidGeometry.addPrimitiveSet(
new DrawElementsUShort(PRIMITIVESETMode.TRIANGLES_Val,indices.length,indices_ptr));
}
// side 2, 3 and 4 are designed in a similar fashion
0 1
4
![Page 29: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/29.jpg)
SMM011 29
Defining the normals...
Vec3Array normals = new Vec3Array();normals.push_back(new Vec3fReference(-1f,-1f, 0f)); // left frontnormals.push_back(new Vec3fReference( 1f,-1f, 0f)); // right frontnormals.push_back(new Vec3fReference( 1f, 1f, 0f)); // right backnormals.push_back(new Vec3fReference(-1f, 1f, 0f)); // left backnormals.push_back(new Vec3fReference( 0f, 0f, 1f)); // peakpyramidGeometry.setNormalArray(normals);pyramidGeometry.setNormalBinding(
GEOMETRYAttributeBinding.BIND_PER_VERTEX);
![Page 30: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/30.jpg)
SMM011 30
Colouring the pyramid...// create an array of coloursVec4Array colors = new Vec4Array();colors.push_back(new Vec4fReference(1f,0f,0f, 1f)); // redcolors.push_back(new Vec4fReference(0f,1f,0f, 1f)); // greencolors.push_back(new Vec4fReference(0f,0f,1f, 1f)); // bluecolors.push_back(new Vec4fReference(1f,1f,1f, 1f)); // white
// declare a variable matching vertex array elements to colour array elementsUIntArray colorIndexArray = new UIntArray();IntReference intref;intref = new IntReference(); intref.setValue(0); colorIndexArray.push_back(intref);intref = new IntReference(); intref.setValue(1); colorIndexArray.push_back(intref);intref = new IntReference(); intref.setValue(2); colorIndexArray.push_back(intref);intref = new IntReference(); intref.setValue(3); colorIndexArray.push_back(intref);intref = new IntReference(); intref.setValue(0); colorIndexArray.push_back(intref);
// associate the array of colors with the geometrypyramidGeometry.setColorArray(colors);pyramidGeometry.setColorIndices(colorIndexArray);pyramidGeometry.setColorBinding(GEOMETRYAttributeBinding.BIND_PER_VERTEX);
![Page 31: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/31.jpg)
SMM011 31
Adding the geometry to the scene...// create a geode (geometry node) holding the pyramid geometryGeode pyramidGeode = new Geode();pyramidGeode.addDrawable(pyramidGeometry);
// add geode to our model
root.addChild(pyramidGeode);
// add model to viewerviewer.setSceneData(root);
![Page 32: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/32.jpg)
SMM011 32
Start running the viewer...//// create windows and start running thread//viewer.realize();
//// event loop//while(!viewer.done()) {
//// the drawing process//viewer.sync();viewer.update();viewer.frame();
}}
![Page 33: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/33.jpg)
SMM011 33
Done! Compile and run...
• javac PrimitiveGL.java• java PrimitiveGL
![Page 34: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/34.jpg)
SMM011 34
A closer look at some of the code
![Page 35: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/35.jpg)
SMM011 35
The Viewer
viewer.setUpViewer(VIEWERViewerOptions.STANDARD_SETTINGS_Val);
• Configures the viewer with ”standard settings”– Mouse handler for rotating and moving around the scene– Keyboard handler with some useful key mappings
• Esc - set the viewer.done() flag, e.g. to exit from the event loop• F - toggle full screen / window• L - toggle lighting• S - statistics about graphics performance• W - toggle solid / wireframe / vertices
HelloWorld
![Page 36: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/36.jpg)
SMM011 36
The Viewer
• Other optionsNO_EVENT_HANDLERS - no handlers installedESCAPE_SETS_DONE - exit by pressing EscapeHEAD_LIGHT_SOURCE - add a lightsource in front
• Settings can be combined (or’ed together), e.g.
viewer.setUpViewer(VIEWERViewerOptions.ESCAPE_SETS_DONE_Val |VIEWERViewerOptions.HEAD_LIGHT_SOURCE_Val
);
![Page 37: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/37.jpg)
SMM011 37
The Viewer’s event loop
viewer.sync();– Waits for all draw and cull threads to complete
viewer.update();– Traverse the scene with an update visitor invoking
node update and animation callbacksviewer.frame();
– Start traversing the scene for drawing and culling
while (!viewer.done()) {viewer.sync();viewer.update();viewer.frame();
}
![Page 38: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/38.jpg)
SMM011 38
Group, Geode, GeometryGroup root = new Group();...Geode pGeode = new Geode();root.addChild(pGeode);...Geometry pGeometry = new Geometry();...pGeode.addDrawable(pGeometry);...viewer.setSceneData(root);
Group
Geode
GeometryGeometryVerticesVertices
FacesFaces
ColoursColours
![Page 39: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/39.jpg)
SMM011 39
Multiple Geodes/Geometries
Group root = new Group();...root.addChild(pGeode1);root.addChild(pGeode2);root.addChild(pGeode3);
...// sharing geometrypGeode1.addDrawable(pGeometry);pGeode2.addDrawable(pGeometry);pGeode3.addDrawable(pGeometry);
GeometryGeometryVerticesVertices
FacesFaces
ColoursColours
pGeode1
root
pGeode2 pGeode3
![Page 40: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/40.jpg)
SMM011 40
Using the OSG built-in primitives
![Page 41: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/41.jpg)
SMM011 41
Primitive shapes
• OSG comes with a number of primitive shapes– Box, Sphere, Cone, Cylinder, Capsule– Plus some special shapes, e.g. InfinitePlane...
![Page 42: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/42.jpg)
SMM011 42
Using the primitive shapes
myGeode.addDrawable(new ShapeDrawable(new Shape(...)));
Geode
DrawableDrawable
ShapeDrawable
Box
![Page 43: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/43.jpg)
SMM011 43
Example – Creating a cylinder
Geode cylGeode = new Geode();
ShapeDrawable cylShapeDrawable = new ShapeDrawable(new Cylinder(
new Vec3fReference(0,0,0), // center1, // radius4)); // height
cylShapeDrawable.setColor(new Vec4fReference(1f, 0f, 1f, 1f) ); // magenta
cylGeode.addDrawable(cylShapeDrawable);
![Page 44: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/44.jpg)
SMM011 44
Transformations in OSG
![Page 45: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/45.jpg)
SMM011 45
Transformations
• Transformations apply to all child nodes in the tree• Allows hierarchies, e.g. limbs on a human body
T
TG G
TG
T
TG G
TG
T
TGG
T
G
![Page 46: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/46.jpg)
SMM011 46
Transform nodes
• ”Transform”– ”MatrixTransform”
• Has a 4x4 matrix (RefMatrixd) representing a transformation– ”PositionAttitudeTransform”
• Sets transform via Vec3 position and Quat attitude– ”AutoTransform”
• Automatically aligns children with screen coordinates
![Page 47: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/47.jpg)
SMM011 47
MatrixTransform
• Contains a 4x4 matrix– With JavaOSG, the matrix is a ”RefMatrixd”– getMatrix()– setMatrix()
• Matrix operations– makeIdentity()– makeTranslate(x, y, z)– makeRotate(angle, x, y, z,)– makeScale(x, y, z)– preMult() / postMult() for multiplying matrices
![Page 48: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/48.jpg)
SMM011 48
Example – MatrixTransform
MatrixTransform mt = new MatrixTransform();
// getting and translating the matrixRefMatrixd matrix = mt.getMatrix();matrix.makeTranslate(x,y,z);
// directly setting the matrixmt.setMatrix(RefMatrixd.translate(x,y,z));
// adding child nodes (e.g. a geode)mt.addChild(...);
![Page 49: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/49.jpg)
SMM011 49
Example – Multiplying matrices
RefMatrixd matrix = new RefMatrixd();
// multiplying matricesmatrix.makeIdentity();matrix.preMult(positionMatrix);matrix.preMult(rotationMatrix);
// setting the transform’s matrixmt.setMatrix(matrix)
![Page 50: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/50.jpg)
SMM011 50
Example – PositionAttitudeTransform
PositionAttitudeTransform pat =new PositionAttitudeTransform();
// positioningVec3dReference pos = new Vec3dReference(x,y,z);pat.setPosition(pos);
// rotatingQuat rot = new Quat();rot.setAxis(Vec3d.ZAxis);rot.setAngle(rotation);pat.setAttitude(rot);
![Page 51: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/51.jpg)
SMM011 51
Example – RobotArm.java
Group
Transform
Geode
GeodeMatrix(rot)
Geode
Transform
Transform
Matrix(rot)
Matrix(rot)
Matrix(pos)
Matrix(pos)
Matrix(pos)
![Page 52: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/52.jpg)
SMM011 52
Break...
Questions so far?
![Page 53: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/53.jpg)
SMM011 53
Agenda for 2nd half
• Configuring the viewer• Event handlers• Special nodes• Picking objects
![Page 54: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/54.jpg)
SMM011 54
Configuring the viewer and camera
![Page 55: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/55.jpg)
SMM011 55
Viewer – using a matrix to set the view
Matrix matrix;
// examplesmatrix.makeRotate(angle, x,y,z);matrix.makeTranslate(x,y,z);matrix.preMult(...)
// set the viewviewer.setViewByMatrix(matrix);
![Page 56: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/56.jpg)
SMM011 56
Viewer.setViewByMatrix()
• Must be called between update() and frame()
while (!viewer.done()) {viewer.sync();viewer.update();...viewer.setViewByMatrix(matrix);...viewer.frame();
}
![Page 57: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/57.jpg)
SMM011 57
Viewer.setViewByMatrix() – translations
translate(-10, 0, -25)translate(0, 0, -25)translate(10, 0, -25)
translate(0, 5, -25)
Y
XZ
• Translations become”inverted” comparedto translating objects.
• You can think of it as”translating the world”.
![Page 58: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/58.jpg)
SMM011 58
Viewer.setViewByMatrix() – rotations
rotate(30, 0,1,0)rotate(0, 0,1,0)rotate(-30, 0,1,0)
rotate(15, 1,0,0)
Y
XZ
• Rotations become”inverted” comparedto rotating objects.
• You can think of it as”rotating the world”.
![Page 59: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/59.jpg)
SMM011 59
Example – how do we obtain this view?
matrix.makeIdentity();
matrix.postMult(Matrix.translate(10,-20,-25));
matrix.postMult(Matrix.rotate(Math.PI/8, 0,1,0));
matrix.postMult(Matrix.rotate(Math.PI/6, 1,0,0));
viewer.setViewByMatrix(matrix);
Y
X
Z
Manip.ViewerSimple
![Page 60: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/60.jpg)
SMM011 60
Viewer vs. Camera
• Different ”interfaces” for roughly the same functionality– setViewByMatrix(), view determined by a 4x4 matrix– setViewByLookAt(), similar to gluLookAt() in OpenGL– setLensPerspective(), adjusts the lens
• Viewer can be constructedfrom a CameraConfig– CameraConfig can have
one or more Cameras• Allows multiple views
![Page 61: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/61.jpg)
SMM011 61
The Camera
• Contains a Lens– Lens gives control over the OpenGL PROJECTION matrix– (The OpenGL MODELVIEW matrix is controlled
through the camera’s position and attitude)
![Page 62: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/62.jpg)
SMM011 62
Relevant examples on the course web
• ManipulateViewerSimple.java– Static positioning of viewer via matrix operations
• ManipulateViewer.java– Viewer moves around in a circle
• MoveCamera.java– User moves camera
• MultiView.java– Multiple cameras viewing the same scene
MultiView
MoveCamera
Manip.Viewer
![Page 63: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/63.jpg)
SMM011 63
Event handlers for user input
![Page 64: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/64.jpg)
SMM011 64
Creating the handler
• Extend the GUIEventHandler class• Implement your own handle() function
– Invoked upon keyboard and mouse eventsclass MyHandler extends GUIEventHandler {
public boolean handle(GUIEventAdapter event,GUIActionAdapter action) {
...}
}
![Page 65: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/65.jpg)
SMM011 65
Creating the handler
public boolean handle(GUIEventAdapter event,GUIActionAdapter action) {
• event– Holds mouse button status, coordinates, key pressed, ...
• action– The Viewer implements the GUIActionAdapter interface– Access the Viewer from where the event originated– Can call useful functions in response to an event, e.g.
• requestWarpPointer(x,y)• getSpeed()
![Page 66: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/66.jpg)
SMM011 66
Example – Handling events
public boolean handle(...) {if (event.getEventType == GUIEVENTADAPTEREventType.KEYDOWN) {
switch(event.getKey()) {...
}}
if (event.getEventType == GUIEVENTADAPTEREventType.DRAG) {float x = event.getX();float y = event.getY();...
}return true;
}
TRUE means no otherhandlers will be invoked,FALSE means they will be
UserInput
![Page 67: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/67.jpg)
SMM011 67
Special nodes
![Page 68: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/68.jpg)
SMM011 68
Special nodes
• Nodes for switching states for an object– The ”Switch” node
• Nodes for optimizing rendering performance– The ”LOD” node– The ”Billboard” node
• Nodes for presenting text– The ”Text” node
![Page 69: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/69.jpg)
SMM011 69
The ”Switch” node
• How to represent a traffic light– Can be either red or green– Add the different appearances to the Switch node
• switchNode.addChild(trafficRed);• switchNode.addChild(trafficGreen);• Then add some logic to control the switch node
• What about a box that can be opened and closed– Has two different states, either opened or closed– Can be solved by rotating its lid, maybe that’s better?
![Page 70: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/70.jpg)
SMM011 70
The ”Switch” node – an example
Switch s = new Switch();...s.insertChild(0, nodeA);s.insertChild(1, nodeB);s.insertChild(2, nodeC);...
// in e.g. event handlers.setSingleChildOn(1);
Switch
nodeA
nodeB
nodeC
SwitchingNode
![Page 71: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/71.jpg)
SMM011 71
Level of detail – the problem
• Example: how to represent and draw a tree– A tree has many details (leaves, branches, textures...)
• Requires probably a few millions of polygons• Easy if we only want to see one instance close-up
– What happens when we want a whole forest?• Draw all trees? Billions of polygons? Not feasible!• Reduce polygon count? No, we still want details!
![Page 72: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/72.jpg)
SMM011 72
Level of detail – one solution
• Human vision and screen resolution are limited– Can you really make out a leaf on a tree ~1 km away?– Will a tree that far away occupy more than a few pixels?
• We can reduce details for objects far away– For close-ups, use a highly detailed object– For medium distances, some details are unimportant– For very long distances, we can use a very simple object
![Page 73: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/73.jpg)
SMM011 73
Level of detail – LOD in OSG
• ”Level of Detail” (LOD)– Like a Switch node but switches based on distance to viewer
• Works like a regular group nodelod.addChild(detailedNode);
• Set visible range for each child (unique or overlapping)lod.setRange(childNumber, near, far);
![Page 74: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/74.jpg)
SMM011 74
Level of detail – example
LOD lod = new LOD();...
lod.addChild(detailedNode);lod.setRange(0, 0, 10);
lod.addChild(notSoDetailedNode);lod.setRange(1, 10, 100);
lod.addChild(noDetailsAtAllNode);lod.setRange(2, 100, 25000);
LOD
![Page 75: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/75.jpg)
SMM011 75
Level of detail – example on the web
• Look at ”LODingNode.java”
LODingNode
![Page 76: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/76.jpg)
SMM011 76
Billboards
• The idea– Instead of modeling a detailed object, use an image map– Make the image always face the user
• Images come in 2D, but we want a ”3D” object• Suitable for representing e.g. trees in a forest
• Benefits– Cheaper to render (image map vs. numerous polygons)– Natural things are difficult because they’re, well, natural
BillboardNode
![Page 77: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/77.jpg)
SMM011 77
Billboards – some issues to consider
• The object will look somewhat fake, it’s not really 3D• Works best with viewer + objects on the same level
– E.g. walking on the ground in a billboard forest looks good,but flying over the forest – looking down – causes problems
• Don’t reuse the same image again and again and again– Variation is the key among numerous billboards
• Maybe suitable as a LOD child node when far away
BillboardTree
![Page 78: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/78.jpg)
SMM011 78
Billboards – the node in OSG
• Billboard is a subclass of Geode– addDrawable(...)
• Can change– The axis (or point) of rotation– The direction in which the billboard will face
• setNormal(...)
![Page 79: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/79.jpg)
SMM011 79
Billboards – adding textures
• Textures are used to map an image to the billboard• Typically, a 2D plane is used as a drawable
– E.g. a GL primitive, rectangle or triangle• Texture mapping is straightforward in this case
– (Textures, both in OSG and in general, will be covered in future lectures, so this is just a primer to get you started)
![Page 80: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/80.jpg)
SMM011 80
Billboards – adding textures
Image image = osgDBNamespace.readImageFile(”tree.jpg”);
Texture2D texture = new Texture2D();texture.setImage(image);
StateSet stateset = new StateSet();stateset.setTextureAttributeAndModes(0,
texture, STATEATTRIBUTEValues.ON_Val);
bb.setStateSet(stateset);
Any file formatsupported bythe plugins
Turns texture 0 ON and associates it with the billboard.
![Page 81: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/81.jpg)
SMM011 81
Billboards – an example
Billboard bb = new Billboard();...bb.addDrawable(...);
BillboardingNode.java BillboardingTree
![Page 82: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/82.jpg)
SMM011 82
Text objects
• Labeling objects, augmenting them with information• Text nodes behave just like any other node
– Translates, rotates, scales, become occluded, etc...– E.g. add text next to a geode, and it will stay with it
• Can auto-align to always face the screen– Makes it easier to read
• Fonts– Can use standard system fonts (Arial, Courier, Times, ...)
![Page 83: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/83.jpg)
SMM011 83
Text objects – an example
import openscenegraph.osgText.*;...
// create text objectText label = new Text();
// set font size and colourlabel.setCharacterSize(0.4f);label.setFont("/fonts/arial.ttf");label.setColor(new Vec4fReference(1f,1f,0f,1f));
// the text to display (changeable during run-time)label.setText("Sphere");
![Page 84: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/84.jpg)
SMM011 84
Text objects – an example
label.setAxisAlignment(TEXTAxisAlignment.XY_PLANE); label.setAlignment(TEXTAlignmentType.CENTER_TOP);label.setDrawMode(TEXTDrawModeMask.TEXT_Val);label.setPosition(new Vec3fReference(0,-0.5f,-1.0f));
• Text is a subclass of Drawable...
Geode geode = new Geode();geode.addDrawable(label);scene.addChild(geode);
Set to SCREEN to face the viewer
TextDemo
![Page 85: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/85.jpg)
SMM011 85
Picking
![Page 86: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/86.jpg)
SMM011 86
About picking
• The principle1. Project a ray from the mouse pointer into the screen2. Detect the surfaces which the ray intersects with3. Get hold of the corresponding nodes
![Page 87: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/87.jpg)
SMM011 87
Picking in OpenSceneGraph
• Example on the web– Picking.java
• Changes colour on the object(s) in the projected ray’s path.
• The code?
Picking
![Page 88: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/88.jpg)
SMM011 88
Picking in OpenSceneGraph – code
• Put this code in your event handler’s ”handle()” function
INTERSECTVISITORHitList hitlist =new INTERSECTVISITORHitList();
if (viewer.computeIntersections(event.getXnormalized(),event.getYnormalized(),0, hitlist)) {...
}Fills the hitlist with all intersections for the ray at (X,Y)
![Page 89: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/89.jpg)
SMM011 89
Picking in OpenSceneGraph – code
• ...then, go through the hitlist
if ( ! hitlist.empty()) {for (int i=0; i<hitlist.size(); i++) {
Hit hit = hitlist.at(i);
Geode pg = (Geode)hit.getGeode();
...}
}
This function is not in the current javadocs, but you will need it!
Depth sorted – the first hit is also the foremost.
![Page 90: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/90.jpg)
SMM011 90
Picking in OpenSceneGraph – code
• Can make it even more object oriented...– Create e.g. a ”PickableGeode”
private class PickableGeode extends Geode {public void pick() { /* do something */ }
}E.g. setColour(rgb)
![Page 91: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/91.jpg)
SMM011 91
Picking in OpenSceneGraph – code
• ...then, in the event handler...
if (hit.getGeode() instanceof PickableGeode) {PickableGeode pg =
(PickableGeode)hit.getGeode();
pg.pick();}
Remember to check this before casting if you mix different geodes
Perform the action on the object
![Page 92: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/92.jpg)
SMM011 92
Picking in OpenSceneGraph – hints
• Hitlist contains one hit for each intersection– Possible to get multiple hits for the same Geode– For example, a box will give 2 hits
• 1 Hit for the front face + 1 Hit for the back face– Be careful with code that toggles some state
• 1st hit toggles on, 2nd hit toggles it back off!• Depth sorted hits
– The geodes are not necessarily sorted• Consider e.g. A1, A2, B3, B4, A5, A6... B
A
![Page 93: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/93.jpg)
SMM011 93
Beyond picking... Intersections!
• You can execute arbitrary intersection tests in a scene– Picking is just a specific case of such a test
• Workflow for a test– Create the ray as a LineSegment with start and stop coordinates– Create an IntersectVisitor– Add the LineSegment to the IntersectVisitor– Start a traversal with the IntersectVisitor at a start node (e.g. the root)
• We use a hitlist and proceed just like when picking– Retrieve the resulting hits from the test
• Get hold of the nodes (and coordinates) for the hit
![Page 94: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/94.jpg)
SMM011 94
Intersections – example
• How to create a forest covering rolling hills and stones?
![Page 95: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/95.jpg)
SMM011 95
Intersections – example
Repeat intersection test to place all trees at the appropriate height level
For each tree, do• Get XY coords• Compute the
intersection with ground
• Store Z value(Ray with max/min elevation allowed)
![Page 96: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/96.jpg)
SMM011 96
Intersections – more about the Hit
• Retrieving the coordinates for an intersection– getLocalIntersectPoint()– getLocalIntersectNormal()
– getWorldIntersectPoint()– getWorldIntersectNormal()
• Returns the intersectionin world-oriented coords
![Page 97: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/97.jpg)
SMM011 97
Intersections – more about the Hit
• Most intersections take place with tesselated objects– But this can be overridden by the intersected objects– E.g. a sphere can override its tesselated representation with
a true sphere intersection calculation, giving more accuracy
![Page 98: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/98.jpg)
SMM011 98
Intersections – more about the Hit
• Some examples on what to use all the information in a Hit for...– Bouncing balls against objects– Implementing physics engines
• Grounding avatars• Sliding down slopes
– Computing damage based on the angle of impact
• Punching objects• Holes vs ricochets
![Page 99: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/99.jpg)
SMM011 99
Intersections – end notes
• Intersection tests are very useful, not just in picking
![Page 100: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/100.jpg)
SMM011 100
Exercise assignment
![Page 101: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/101.jpg)
SMM011 101
Getting started...
• Exercise assignment to get you started with OSG– Download ”GetStarted.java” from the course web– Follow the instructions and hand in your solution– Complete the assignment individually– Deadline next friday
![Page 102: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/102.jpg)
SMM011 102
Summary
• Introduction to OpenSceneGraph• Accessing OSG from Java
– Done through the Java bindings• Overview of core classes
– Nodes, Groups, Geodes, Drawables, Transform nodes• Creating OpenGL primitives• Using built-in primitives• Transformations
![Page 103: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/103.jpg)
SMM011 103
Summary
• Configuring the viewer– How to manipulate the viewer/camera
• Event handlers for user input– Creating and registering the handler– Handling keyboard and mouse events
• Special nodes– Switching object appearance during run-time (e.g. Switch)– Using nodes to optimize rendering performance (e.g. LOD)– Presenting text for the user
• Picking objects in 3D– Advantages of using a scenegraph – How to implement it, what to think about – Picking as an application of intersection tests in general
![Page 104: Documentt1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5477c8fab4af9fc1698b4636/html5/thumbnails/104.jpg)
SMM011 104
Questions?