1 L Agenda Introduction Loading models MilkShape - - PDF document

1 l
SMART_READER_LITE
LIVE PREVIEW

1 L Agenda Introduction Loading models MilkShape - - PDF document

aaa INSTITUTIONEN FR 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


slide-1
SLIDE 1

aaa bbb 1

1 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

OpenSceneGraph Modeling

Mikael Drugge Virtual Environments Spring 2005 Based on material from http://www.openscenegraph.org/

2 L

Agenda

  • Introduction
  • Loading models
  • MilkShape
  • Lightsources
  • StateSets
  • Materials
  • Textures

3 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Introduction

slide-2
SLIDE 2

aaa bbb 2

4 L

What makes the difference...?

5 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

6 L

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);

What’s this?

slide-3
SLIDE 3

aaa bbb 3

7 L

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

8 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

MilkShape

9 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
slide-4
SLIDE 4

aaa bbb 4

10 L

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 

11 L

Example screenshot of MilkShape

12 L

The coordinate system in MilkShape

X Y Y Z Z X

slide-5
SLIDE 5

aaa bbb 5

13 L

Demo of MilkShape!

  • Learning by doing, so let’s create something...

14 L

Demo of MilkShape!

  • Let’s see the result in OSG...

Loading Objects 15 L

Exporting

  • Autodesk 3DS
  • LightWave 6.5x LWO
  • Flattens all edges
  • Wavefront OBJ
  • Materials in an extra MTL file
  • 3DS or LWO recommended
slide-6
SLIDE 6

aaa bbb 6

16 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Loading models into OSG

17 L

Remember this? Plugins...

OSG Scenegraph Rendering elements Tree/DAG nodes OSGDB Database loading Plug-In management OSGText Handles text, fonts, ... Plug-Ins OSGUtil Traversers Enhancements OSGSim Simulation (skybox, lights, time-of-day) OSGParticle Special effects (fire, smoke...) JPG PNG GIF ... 3DS OSG LWO ... import openscenegraph.osgDB.osgDBNamespace;

18 L

Loading objects – code example

import openscenegraph.osgDB.osgDBNamespace; Node node =

  • sgDBNamespace.readNodeFile(“somefile.lwo”);

scene.addChild(node); This will load the model into a Node Then just handle it like any other node

slide-7
SLIDE 7

aaa bbb 7

19 L

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

20 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Lightsources

21 L

Lights and Lightsources

  • LightSource
  • Extends the Group node

+ addChild() still possible

  • Contains one Light

+ setLight() + getLight()

LightSource Light

slide-8
SLIDE 8

aaa bbb 8

22 L

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

23 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

24 L

The Light

  • More spotlight functions
  • setSpotCutoff(distance)
  • setSpotExponent(exp)
  • (Not much documentation about

these two functions – experiment!)

slide-9
SLIDE 9

aaa bbb 9

25 L

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

26 L

Example

Lighting 27 L

Lightsources – example code

Light light1 = new Light(); light1.setLightNum(0); light1.setDiffuse( new Vec4fReference(1f,0f,1f, 1f)); light1.setPosition( new Vec4fReference(x, y, z, 1f)); Create a light for GL light #0 Purple light Note this!

slide-10
SLIDE 10

aaa bbb 10

28 L

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);

29 L

Lightsources – example code

// create a “stateset” for the root node StateSet stateset = new StateSet(); root.setStateSet(stateset); // turn the lightsource on for this stateset ls.setStateSetModes(stateset, STATEATTRIBUTEValues.ON_Val); (We’ll come back to StateSets in a few slides, so don’t worry)

30 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...
slide-11
SLIDE 11

aaa bbb 11

31 L

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

32 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...

33 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

StateSets

slide-12
SLIDE 12

aaa bbb 12

34 L

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

35 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 the example a few slides ago.)

36 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Materials

slide-13
SLIDE 13

aaa bbb 13

37 L

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

38 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);

39 L

The Material class – some functions

  • setDiffuse(MATERIALFace, Vec4fReference)
  • setSpecular(...)
  • setAmbient(...)
  • setEmission(...)
  • setShininess(...)
  • setAlpha/setTransparency(...)

Vec4f(R, G, B, Alpha) Front and/or Back faces

slide-14
SLIDE 14

aaa bbb 14

40 L

Materials – some examples and hints

  • setDiffuse
  • The ”base colour” of an object
  • setSpecular & setShininess
  • Plastics often have a white specularity, and are ”hard”, i.e.

they have a small area where this specularity occurs

  • Common mistake when making metals: making them too

”hard” and white – it’s better with a softer setting and a specularity in the same nuance as the diffuse colour

41 L

Materials – some examples and hints

  • setAmbient
  • Simulates the ”background noise” of all lightsources
  • Instead of black ”shadows”, use the ambient colour
  • setEmission
  • Can make an object look like it radiates light

+ Instead of using a real lightsource – they’re expensive

  • E.g. to create a torch, make the flame emissive and place a

lightsource within it, also use an ambient component

42 L

Materials – some examples and hints

  • setAlpha/setTransparency
  • Sets the Alpha value of ambient, diffuse, specular and

emission colours

  • setAlpha(value) = setTransparency(1 – value)
  • Useful for making e.g. windows, water, holograms, etc...
slide-15
SLIDE 15

aaa bbb 15

43 L

Example – creating an orange plastic ball

shape = new ShapeDrawable( new Sphere(new Vec3fReference(0f, 0f, 0f), 1f)); geode = new Geode(); stateset = new StateSet(); material = new Material(); material.setDiffuse(MATERIALFace.FRONT_AND_BACK, new Vec4fReference(1.0f, 0.5f, 0.1f, 1f)); material.setSpecular(MATERIALFace.FRONT_AND_BACK, new Vec4fReference(1.0f, 1.0f, 1.0f, 1f)); material.setShininess(MATERIALFace.FRONT_AND_BACK, 63f);

  • Orange colour
  • White specularity
  • Make it a bit harder

44 L

Example – creating an orange plastic ball

stateset.setAttribute(material); geode.setStateSet(stateset); geode.addDrawable(shape); ...

Associate material with the stateset Apply the stateset to the geode with the sphere object

45 L

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));

slide-16
SLIDE 16

aaa bbb 16

46 L

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 You need to enable blending to get transparency Set a rendering hint to use transparent bin

47 L

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

48 L

Example – demo of Looks.java

Looks

slide-17
SLIDE 17

aaa bbb 17

49 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Textures

50 L

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
  • (The underlying theory behind textures will be covered

in a future lecture, see this as a primer)

51 L

Textures in OSG

  • The ”Texture” class
  • Encapsulates OpenGL’s texture functionality
  • Texture1D/2D/3D – subclasses for 1D/2D/3D

X Y Example Texture2D

slide-18
SLIDE 18

aaa bbb 18

52 L

Adding textures to PrimitiveGL.java

53 L

Adding textures to PrimitiveGL.java

..... // define texture coordinates wrapping it around the pyramid Vec2Array texcoords = new Vec2Array(); texcoords.push_back(new Vec2fReference(0.00f, 0.00f)); // coord for v0 texcoords.push_back(new Vec2fReference(0.25f, 0.00f)); // coord for v1 texcoords.push_back(new Vec2fReference(0.50f, 0.00f)); // coord for v2 texcoords.push_back(new Vec2fReference(0.75f, 0.00f)); // coord for v3 texcoords.push_back(new Vec2fReference(0.50f, 1.00f)); // coord for v4 pyramidGeometry.setTexCoordArray(0, texcoords);

54 L

Adding textures to PrimitiveGL.java

// create a 2D texture object Texture2D myTexture = new Texture2D(); // load an image via the plugins (e.g. JPG, GIF, TGA, etc...) Image texImage =

  • sgDBNamespace.readImageFile(file);

// assign image to the texture myTexture.setImage(texImage); (The plugins should work without additional code!)

slide-19
SLIDE 19

aaa bbb 19

55 L

Adding textures to PrimitiveGL.java

  • Wake-up question!  What do we do next...?

StateSet stateset = new StateSet(); stateset.setTextureAttributeAndModes( 0, myTexture, STATEATTRIBUTEValues.ON_Val); ... pyramidGeode.setStateSet(stateset);

56 L

Demo of the result

  • TextureDemo.java
  • Other examples
  • BillboardingNode
  • BillboardingTree

Texture Demo 57 L

Texture coordinates

  • ShapeDrawables have texture coordinates defined
  • I.e. create the shape and just apply the texture to it
  • For a primitive GL rectangle, it’s also very simple
  • An example follows...
slide-20
SLIDE 20

aaa bbb 20

58 L

Texture coordinates

// a rectangle 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); 1 3 2

59 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Summary

60 L

Summary

  • Introduction
  • We sometimes 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
slide-21
SLIDE 21

aaa bbb 21

61 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Questions?