3D Programming in Java Outline An ideal API Modeling ...

21 downloads 1022 Views 485KB Size Report
3D models. Photographic print. Photography: Computer. Graphics: Define this ... PHIGS. ▫ Programmer's Hierarchical Interactive ... X Windows extension (PEX).
Modeling „

3D Programming in Java

Define geometry of object „ „

„

Joe Geigel [email protected]

„ „

Outline „

„ „

„

„

PHIGS OpenGL Scene Graphs

„ „

„ „

GL4Java Java3D VRML/X3D

„

An ideal API Photography:

real scene

Placing/orienting the object in your world „

3d Programming in Java „

Lighting camera

photo processing

Photographic print

„

Place lights in your world „

processing

„ „

3D models

Define this

Translation Rotation Scaling

Can be hierarchical

„

Computer Graphics:

Primitive shapes Constructive Solid Geometry Mathematics / Parametric

Transformation

History of 3d APIs „

The polygon is king! Other ways:

camera model

tone reproduction

Ambient Directional Point Light Spot Light

synthetic image

API gives you this

1

Shading „ „

Determines color of object at given points Dependent upon: „ „ „

„

PHIGS

Surface characteristics / position Light position / color Viewer position

„

„ „

Varies by „ „

Illumimation model Shading Model

„ „

Phong Model V

Programmer’s Hierarchical Interactive Graphics System ISO Standard (1989) API definitions w/bindings for different languages All done in software X Windows extension (PEX)

PHIGS N

„

What PHIGS provides „

R

Geometric Primitives „

S „

Lights

„

Transformation

„

Viewing

„

L(V ) = k a La + k d ∑i Li (Si • N) + k s ∑i Li (R i • V) ke { 1442443 144 42444 3 ambient

diffuse

„

Ray Tracing

„

„

specular

Global Illumination „

Radiosity

Triangle Strips, Quadrilateral Mesh, B-spline Surface, Text Point, Directional, Spot Rotation, scaling, translation, hierarchical Parallel and projective perspectives

PHIGS „

There are four steps to visualizing a model in PHIGS: 1.

2.

3.

4.

Define the model in the PHIGS graphics database. Create a PHIGS workstation to display the model. Link the model's description to the workstation. Tell the workstation to display the model.

2

PHIGS „

PHIGS and X-Windows

OpenGL „

OpenGL is a “C” CG API that „

„ „

„

Note that OpenGL is „ „

Problems with PHIGS „

„

Most popular implementation too tied to X-Windows Didn’t keep up with Graphics research „

„

Functional NOT Object Oriented Low level access to graphics hardware

OpenGL demos/code „

Thanks to Nate Robbins

„

Thanks to SIGGRAPH 2001/2

„

E.g. No texture mapping

„

http://www.xmission.com/~nate/opengl.html http://www.opengl.org/resources/tutorials/s2001/

All software based.

Meanwhile… „

SGI puts graphics in hardware

„

IRIS GL

„

„

„

„

Generate high quality color images by rendering with geometric and image primitives Create interactive 3D applications OS and Windowing System independent

IRIS machines

OpenGL „

What OpenGL provides: „

„

low level C-library that interfaced to SGI graphics hardware. later standardized in a platform independent manner (OpenGL)

Direct link into Graphics Hardware

Objects

„

„

only knows about polygons (different kinds of polygons, but just polygons) Polygons are specified by vertex list

Shapes Demo

3

OpenGL Utility Libraries „

GLU (OpenGL Utilities) „ „

„

„

Higher level primitives (quatratics, NURBS) Part of OpenGL

„ „

What OpenGL provides „

Transformations „ „ „

Routines for scaling, translation, and rotation Routines for direct manipulation of transformation matrix Maintains a stack of transformation matricies for support of object hierarchy

Texture Mapping „

„

Platform independent Windowing System Not officially part of OpenGL Maintained by Nate Robbins

OpenGL

Also supports „

GLUT (OpenGL Utility Toolkit) „

„

OpenGL

„

GLUT Basics „

Application Structure „ „ „

Configure and open window Initialize OpenGL state Register input callback functions „ „

„

Transformation Demo

„

„

OpenGL „

Lighting and Shading „

OpenGL models a “state machine” „

„

„ „

Point and Directional Lights Basic Phong Shading „ „ „

„

Current color, texture, material properties, pattern, drawing style, etc are maintained Primitives are drawn using current “state”

Ambient Diffuse Specular

Lighting Demos

Mapping an image onto geometry

Fog, atmospheric effects Animation (double buffering)

render resize input: keyboard, mouse, etc.

Enter event processing loop

Sample Program void main( int argc, char** argv ) { int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode( mode ); glutCreateWindow( argv[0] ); init(); glutDisplayFunc( display ); glutReshapeFunc( resize ); glutKeyboardFunc( key ); glutIdleFunc( idle ); glutMainLoop(); }

4

OpenGL Initialization „

Set up whatever state you’re going to use

void init( void ) { glClearColor( 0.0, 0.0, 0.0, 1.0 ); glClearDepth( 1.0 );

Idle Callbacks „

Use for animation and continuous update „

glutIdleFunc( idle );

}

void idle( void ) { t += dt; glutPostRedisplay(); }

GLUT Callback Functions

User Input Callbacks

glEnable( GL_LIGHT0 ); glEnable( GL_LIGHTING ); glEnable( GL_DEPTH_TEST );

„

Routine to call when something happens „ „ „

„

window resize or redraw user input animation

“Register” callbacks with GLUT glutDisplayFunc( display ); glutIdleFunc( idle ); glutKeyboardFunc( keyboard );

Rendering Callback „

Do all of your drawing here glutDisplayFunc ( display );

void display( void ) { glClear( GL_COLOR_BUFFER_BIT ); glBegin( GL_TRIANGLE_STRIP ); glVertex3fv( v[0] ); glVertex3fv( v[1] ); glVertex3fv( v[2] ); glVertex3fv( v[3] ); glEnd(); glutSwapBuffers(); }

„

Process user input „

glutKeyboardFunc( keyboard );

void keyboard( unsigned char key, int x, int y ) { switch( key ) { case ‘q’ : case ‘Q’ : exit( EXIT_SUCCESS ); break; case ‘r’ : case ‘R’ : rotate = GL_TRUE; glutPostRedisplay(); break; } }

History „

1992 – Inventor was introduced by SGI „ „ „ „

„

Object oriented wrapper around Iris GL C++ class library Corresponding file format (ASCII) Later standardized to a platform independent library (Open Inventor) The scene graph was born

5

Scene Graphs „ „

Scene is represented by a tree structure Inventor Graph Nodes „ „ „ „ „

Scene Graphs „

„ „

Shape nodes -- represent physical objects Lighting Nodes -- introduces lights to a scene Camera Nodes -- Introduces a camera model Property nodes – appearance / transformations Group nodes – „ „

„

„

locally grouped subtrees Allowed for definition of reusable “objects”

Scene Graphs

Inventor - Rendering

„

Like OpenGL, Inventor assumes a state machine. Unlike OpenGL, entire state can be placed on a stack. Visiting a shape node introduces a new object with “current” properties Visiting a property node is equivalent to making a given material current. Entering a group node pushes current state on stack Leaving a group node pops current state off stack.

Scene Graphs „

Inventor -- Interactive aspects: „

Event model „ „

„

Sensors „ „

„

„

Graph Actions „ „

„

Traversal of scene graph for a particular purpose. Separation of scene from rendering (or other action) Examples „ „ „ „ „

Rendering Searching Compute Bounding Box Interactivity / Picking Write to file

Tracks changes in scene and responds Performs node change at given time intervals

Manipulators „

Scene Graphs

Ability to define and respond to events Messages sent (routed) to nodes changing node state

Interface to input devices (e.g. mouse, trackball)

Meanwhile… „

In 1994 „

Mark Pesce & Tony Parisi & Peter Kennard develop Labyrinth, a prototype threedimensional interface to the Web

6

VRML „

Meaning „ „

„

„

Hierarchical Directed Acyclic Graph Nodes

Virtual Reality Modeling Language NOT Virtual Reality Markup Language

„

Node Types

„

V-R-M-L VERR-MAL

„ „ „

From the VRML Consortium „

„

“an open standard for 3D multimedia and shared virtual worlds on the Internet.”

„ „

VRML - History „

„

„

Pronunciation „

„

VRML – Scene Graph Have data fields & children Geometry Appearance Transformation Sound Grouping Viewpoint

VRML – Scene Graph example

Milestones „ „ „

„ „

1992 – SGI Inventor Toolkit October 1994 – version 1.0 (geometry) August 1996 – version 2.0 (animation, interaction, behavior) December 1997 – VRML97 ISO Standard X3D – XML based VRML

Xform

Shape appearance

geometry

Sphere

Material

Radius = 1.0

VRML – Technical Details „ „ „ „

Scene Graph Structure Object Oriented (sort of…) Event Architecture Viewpoint Selection

Translation = (1.0, 1.0, 6.5)

Transform { translation 1.0 1.0 6.5 children [ Shape { appearance Appearance { material Material { diffuseColor 1.0 0.0 0.0 } } geometry Sphere { radius 1.0 } } ] }

Color = red

VRML – Defining new nodes Define Seat (location, mycolor)

Xform

Translation = location

Shape geometry

Sphere

Radius = 1.0

appearance

Material

Color = mycolor

PROTO Seat [ exposedField SFVec3f location 0.0 0.0 0.0 exposedField SFColor mycolor 1.0 1.0 1.0 ] Transform { translation IS location children [ Shape { appearance Appearance { material Material { diffuseColor IS myColor } } geometry Sphere { radius 1.0 } } ] } Seat

{ location 1.0 1.0 6.5 mycolor 1.0 0.0 0.0}

7

VRML – Naming nodes example PROTO Seat [ exposedField SFVec3f location 0 0 0 exposedField SFColor mycolor 1.0 1.0 1.0 ] …

Seat3

Seat2

Seat4

Seat5

Seat1

DEF Seat1 Seat { location 1.0 1.0 6.5 mycolor 1.0 0.0 0.0} DEF Seat2 Seat { location 2.0 1.0 6.5 mycolor 1.0 0.0 0.0} DEF Seat3 Seat { location 3.0 1.0 6.5 mycolor 1.0 0.0 0.0} DEF Seat4 Seat { location 4.0 1.0 6.5 mycolor 1.0 0.0 0.0} DEF Seat5 Seat { location 5.0 1.0 6.5 mycolor 1.0 0.0 0.0}

VRML – Viewpoint Selection „

Viewpoint Node „

„

„

Defines position, direction, orientation, fov of a camera in virtual space

Many Viewpoint Nodes can exist in a graph Only one Viewpoint “active” at any one time.

Color / Location of each seat can be modified by ROUTing messages

VRML – Event Architecture „

Sensor „ „

„

Script Node „

„

User Interaction Primitives Changes node field value based on user input. Functional means of changing node field values

External Authoring Interface (EAI) „

„

Allows external software components (e.g. applets) to modify node field values. Allows for creation of new nodes on the fly

VRML – Script node „

Caveats „ „

„

Java and Javascript supported Interfaces to access time and graph state for each language The spec does NOT require support of any particular language for VRML standard compliance.

VRML – The future „

X3D „ „ „ „ „

VRML expressed using XML 3D Interchange format Strong validation and conformance Plays nice with other XML Media format DTD available at: „

http://www.web3d.org/specifications/x3d-3.0.dtd

VRML – The future

8

Isn’t this the JAVA User’s Group? „

Yes…as well see after the break!

9