aaa INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET OpenSceneGraph Modeling Mikael Drugge Virtual Environments Spring 2005 Based on material from http://www.openscenegraph.org/ 1 L Agenda • Introduction • Loading models • MilkShape • Lightsources • StateSets • Materials • Textures 2 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Introduction 3 L bbb 1
aaa What makes the difference...? 4 L What causes the difference...? • Lightsources - Can change e.g. the ”mood” of a scene via the lighting • 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 5 L Creating models • Modeling via code - Easy for simple models - But, requires a fair bit of imagination - For example: What’s this? 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); 6 L bbb 2
aaa Creating models with modeling programs • A number of 3D modeling tools exist... - 3D Studio, for graphics and raytracing - AutoCAD, for CAD applications - Maya, for graphics and animation - Imagine, an old Amiga raytracer - Wingz, modeler for POVray - MilkShape, a modeling program 7 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET MilkShape 8 L 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…) • Skeletal animation capabilities • Supports 37 file formats from 27 games/engines 9 L bbb 3
aaa Introduction to MilkShape 3D • Why have we chosen to use MilkShape? - It is fairly intuitive to use and learn - It’s powerful enough for this course - Handles a lot of different formats - Allows for 30-day free trial - It’s fairly cheap also 10 L Example screenshot of MilkShape 11 L The coordinate system in MilkShape Y Y X Z Z X 12 L bbb 4
aaa Demo of MilkShape! • Learning by doing, so let’s create something... 13 L Demo of MilkShape! • Let’s see the result in OSG... 14 L Loading Objects Exporting • Autodesk 3DS • LightWave 6.5x LWO - Flattens all edges • Wavefront OBJ - Materials in an extra MTL file • 3DS or LWO recommended 15 L bbb 5
aaa INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Loading models into OSG 16 L Remember this? Plugins... JPG 3DS PNG OSG GIF LWO OSGUtil OSG ... ... Scenegraph Traversers Enhancements Rendering elements Tree/DAG nodes OSGDB Database loading Plug-Ins Plug-In management OSGText OSGSim OSGParticle import openscenegraph.osgDB.osgDBNamespace; Handles text, Simulation (skybox, Special effects fonts, ... lights, time-of-day) (fire, smoke...) 17 L Loading objects – code example import openscenegraph.osgDB.osgDBNamespace; This will load the model into a Node Node node = osgDBNamespace.readNodeFile(“somefile.lwo”); scene.addChild(node); Then just handle it like any other node 18 L bbb 6
aaa What to think about for external models • Retrieving the dimensions for the model - Getting the BoundingSphere via node.getBound() + getRadius() - E.g. for ”normalizing” the size of an unknown object 19 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Lightsources 20 L Lights and Lightsources • LightSource - Extends the Group node + addChild() still possible - Contains one Light + setLight() + getLight() LightSource Light 21 L bbb 7
aaa 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 22 L The Light • Light can be point source or spotlight - setPosition(), positions the light - setDirection(), aims the spotlight in the given direction • Many functions to tweak the lighting - setConstantAttenuation(), also Linear and Quadratic + Light intensity diminishes over a distance 23 L The Light • More spotlight functions - setSpotCutoff(distance) - setSpotExponent(exp) - (Not much documentation about these two functions – experiment!) 24 L bbb 8
aaa The Light – shadows? • No shadows - Shadows are expensive to compute - Possible to achieve in e.g. raytracing - OSG does not support real time shadows + Some engines do, however, e.g. AgentFX by Agency9 25 L Example 26 L Lighting Lightsources – example code Light light1 = new Light(); Create a light light1.setLightNum(0); for GL light #0 Purple light light1.setDiffuse( new Vec4fReference(1f,0f,1f, 1f)); light1.setPosition( new Vec4fReference(x, y, z, 1f)); Note this! 27 L bbb 9
aaa Lightsources – example code // create lightsource and set its light LightSource ls = new LightSource(); ls.setLight(light1); // turn on the lightsource ls.setLocalStateSetModes( STATEATTRIBUTEValues.ON_Val); 28 L Lightsources – example code // create a “stateset” for the root node StateSet stateset = new StateSet(); root.setStateSet(stateset); (We’ll come back to StateSets in a few slides, so don’t worry) // turn the lightsource on for this stateset ls.setStateSetModes(stateset, STATEATTRIBUTEValues.ON_Val); 29 L Lightsources – example code // then add the lightsource to the scene root.addChild(ls); • Repeat the process if you need more lightsources - You should reuse the same StateSet however • Keep in mind you can alter light properties during run-time - Colour, position, direction, etc... - Turn lights on and off, etc... 30 L bbb 10
aaa Summary – how to add a lightsource 1. Create a new LightSource 2. Create a new Light and associate with a GL light 3. Adjust the light parameters; colours, attenuation... 4. Make the lightsource use your light (via setLight()) 5. Create a stateset for the part of the scenegraph tree that should be illuminated by the lightsource 6. Turn on the lightsource in the stateset 7. Add the lightsource to the scene 31 L Some things to think about • setDiffuse() may be all you need, BUT • For materials where specularity, etc. are defined, the lightsource needs to emit light that affects these too - If not, the material properties will seem to have no effect • I.e. make sure you set the colour for all components - setDiffuse(somecolour); - setSpecular(somecolour); - etc... 32 L INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET StateSets 33 L bbb 11
aaa What is a StateSet? • It encapsulates OpenGL state modes and attributes - Used for textures, lightsources, materials, transparency... • E.g. specifies textures for Drawables - Drawables can share a single StateSet • Sharing statesets is recommended! Why? - It minimizes state changes in the GL pipeline - Changing the state in the pipeline is expensive! - Thus, sharing the same StateSet is a good thing 34 L StateSets – example code revisited // create a “stateset” for the root node StateSet stateset = new StateSet(); root.setStateSet(stateset); I.e. create a stateset that will apply from the root node and downwards. (The entire scene will use this stateset, meaning all of it will be illuminated by the lightsource in 35 L the example a few slides ago.) INSTITUTIONEN FÖR SYSTEMTEKNIK LULEÅ TEKNISKA UNIVERSITET Materials 36 L bbb 12
aaa Introduction to Materials • Not all objects are made of the same thing - Metals, glass, plastics, wood, paper, skin, crystal... • Different properties, depending on - How rough/smooth the surface of the object is - How the light refracts when hitting an object - The colour of the object and the light • Note! - The theory behind all this will be presented in a future lecture, this is mainly a primer to get you started 37 L Materials in OSG • The ”Material” class - Encapsulates OpenGL’s 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); 38 L The Material class – some functions Front and/or Back faces • setDiffuse(MATERIALFace, Vec4fReference) • setSpecular(...) • setAmbient(...) • setEmission(...) Vec4f(R, G, B, Alpha) • setShininess(...) • setAlpha/setTransparency(...) 39 L bbb 13
Recommend
More recommend