Title: Java 3D, GLUT input
1Java 3D, GLUT input
2Java 3D
- API for writing 3D graphics
- applications/applets
- can mix with regular Java, such as AWT events
- Write once, view anywhere
- Scene Graph
- tree data structure
- describes entire scene (Virtual Universe)
3Simple Scene Graph Example
4Assembling a Scene Graph
- Create a Canvas3D object
- Construct viewing branch graph (can use
SimpleUniverse convenience utility) - VirtualUniverse object
- high-resolution Locale object
- ViewPlatform object
- which references a View object
- which in turn references PhysicalBody,
- PhysicalEnvironment, and the earlier Canvas3D
objects
5Assembling a Scene Graph
- Construct content branch graph
- for Geometry, Appearance, Behavior, etc.
- this branch graph can get quite complex
- Optionally compile branch graphs
- Insert both branch graphs into the Locale
6Terminology
- live
- attached to scene graph tree
- compiled into optimized format
- prior to attachment to main scene graph
- cannot undo compile !!!
- some actions rely upon live or compiled states
- for example, once live or compiled, capabilities
cannot be changed
7SceneGraph Traversal
- Java 3D renderer chooses traversal order
- not restricted to left-to-right or top-to-bottom
- except for spatially bounded attributes, such as
light sources, fog - open to parallel processing
8The Java 3D Renderer
- starts running in an infinite loop
- conceptually performs the following operations
- while(true)
- Process input
- If (request to exit) break
- Perform Behaviors
- Traverse the scene graph
- and render visible objects
-
- Cleanup and exit
9Packages
- typical import statements
- import javax .media.j3d.
- import javax .vecmath .
- import com.sun.j3d. utils .applet. MainFrame
- import com.sun.j3d. utils .geometry. ColorCube
- import com.sun.j3d. utils .universe.
- import java .applet.Applet
- import java .awt.BorderLayout
- import java .awt.Frame
- import java .awt.event.
10General Java 3D Facts
- default SimpleUniverse virtual world
- coordinate system
- right-handed coordinate system
- back up several units in z
- look toward origin
- angles are always in radians
- most set() methods have corresponding get()
methods - physical world units are in meters
11SceneGraphObject class
- abstract class represents any scene graph
component - methods common to everything in scene graph
- controls object capabilities
- setCapability() method very useful
- enables operations to be allowed when live or
compiled - if already live or compiled , capability cannot
be changed - superclass for Node and NodeComponent classes
12Node
- superclass of Group and Leaf classes
- Node objects can be put directly into the scene
graph - NodeComponent objects cannot be in a scene graph
tree, but can be referenced
13Leaf
- has no children
- may reference NodeComponent objects
- superclass for elements used in rendering
- such as geometry, lights, sounds
- Shape3D--important subclass
14Group
- may contain child node objects
- superclass of important BranchGroup and
TransformGroup nodes - addChild() method is used most often
15NodeComponent
- superclass for Geometry and Appearance classes
- and 14 other Java 3D classes
- Geometry may include coordinates, colors,
normals, texture coordinates - Appearance objects may specify color, texture
parameters, culling, shading, etc.
16Hello3D Applet/Application
- static program rendering one 3D object
SimpleUniverse
17Hello3D.java Constructor
- // Scene graph constructed
- Hello3D()
- setLayout(new BorderLayout())
- Canvas3D c new Canvas3D(null)
- add("Center", c)
- BranchGroup scene createSceneGraph()
- SimpleUniverse u new SimpleUniverse(c)
- u.addBranchGraph(scene) // makes it live
-
- public static void main(String args)
- Frame frame
- new MainFrame(new Hello3D(), 256, 256)
-
18Create Hello3D Scene Graph
- public class Hello3D extends Applet
- public BranchGroup createSceneGraph()
- BranchGroup objRoot new BranchGroup()
- objRoot.addChild(new ColorCube())
- return objRoot
-
19Interaction
- Interaction is an important component of graphics
applications - Most modern APIs, such as OpenGL, doesnt support
interaction directly (why?) - We need additional libraries, such as GLUT, for
device interaction
20Input Devices
- We can think about input devices in two ways
- Physical device that can be described by their
real-world physical properties. - (mouse, keyboard, joystick)
- Logical devices that is characterized by its
high-level interface with the user program. It is
an abstraction of device data. - (functions, think of the windows device driver)
21Physical Device Types
22Keyboard
- Returns character coders with specific meanings
23Choice
- Returns a choice that has been selected from a
number of discrete options
- Button box - Function keypad
24Locator Devices
- Return a position and/or orientation
- Mouse
- Trackball
- Tablet
- Joystick
- Touch screen
253D Input Devices
- Return 3D position and /or orientation
- Digitizer
- 3D Spaceball
- Glove
- Tracker
263D Input Devices
Digitizer - 3D model
273D Input Devices
3D Spaceball
283D Input Devices
293D Input Devices
Motion Tracker - Magnetic - Acoustic -
Inertial - Optical - GPS
30Logical Device
- Characterized from the perspective of user
application program - High-level interface with the user program
- An abstraction of device data
31Logical Device Types
- String
- Returns ASCII strings
- Locator
- Returns position and or orientation
- Pick
- Returns the identifier of object
- Choice
- Returns a choice that has been selected from a
number of options - Dial
- Return analog input (continuous control)
- Stroke
- Returns an array of locations
32Describe Input Device Behavior
- Ways to describe input device behavior
- Measure what the device returns to user program
- Trigger when the device returns those
measurements
33 Ways to Read Input Device
- Sample mode
- Request mode
- Event mode
34Sample Mode
- Hi, What is its input right now? Give me the
data immediately!
Measure Process
- - No trigger needed
- Return immediately
- Prepare the data before function call
35Request Mode
- Do not return the measure until the device is
triggered
Measure Process
Trigger Process
- - Wait for triggering
- Think of the C function call scanf()
36Event Mode
- Wait until a device is triggered and user does
something
- Two asynchronous processes
- - Event generation (device trigger)
- User request (event query)
- Event generation and process are independent
- Multiple input devices
37Event Mode
- Lets think of the Windows event mechanism
- - Generate a event by a device trigger
- Place the event in the event queue
- Examine the event queue and process
38Event Mode
- Two Asynchronous Processes
Trigger Process
Event Queue
Measure Process
39Interactive GraphicsProgramming
- We will focus on
- Event-driven Input mode
- Callback mechanism to do event process
- GLUT programming
40Callback Functions
- Callback Function
- Routine to call when some event happens.
- e.g. mouse/keyboard, window changes, etc.
- GLUT uses a callback mechanism to do its event
processing.
41Event-driven Input Programming
- Decide input events
- mouse, keyboard, window
- Write callback functions
- respond to each input event,
- Register callback functions
- tell the system to handle the event,
- Event processing loop
- Wait and process
42GLUT Event Callback Functions
- GLUT can handle the most commonly used input
events - Ex
- Key action
- Mouse action
- Idle (called when nothing else is going on)
- Window events (contents, resized or moved)
43Keyboard Event
Its called when a key is struck on the keyboard
glutKeyboardFunc ( keyboard_callback_func )
void keyboard_callback_func ( unsigned char key,
int x, int y ) switch (key)
case q exit (0)
break
44Mouse Event
Its called when a mouse button is
depressed/released
glutMouseFunc ( mouse_callback_func )
- void mouse_callback_func ( int button, int
state, int x, int y ) - button which button is depressed/released.
- GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON,
GLUT_RIGHT_BUTTON - state action state.
- GLUT_DOWN, GLUT_UP
- x, y position
45Mouse Event
void mouse_callback_func ( int button, int
state, int x, int y ) if ( button
GLUT_LEFT_BUTTON state GLUT_DOWN )
exit (0)
46You Must Register Callback
void main (int argc, char argv)
glutDisplayFunc ( display ) //
display callback
glutReshapeFunc ( resize ) // window
resize glutKeyboardFunc ( key )
// keyboard callback
47Main Function
void main (int argc, char argv)
glutInit ( argc, argv )
glutInitDisplayMode (GLUT_SINGLE GLUT_RGB)
glutInitWindowSize (256, 256)
glutCreateWindow (My Program") myinit
()
glutDisplayFunc ( square )
// register callback
glutMainLoop ()
48OpenGL Initialization
void myinit (void) glClearColor (1.0,
1.0, 1.0, 0.0) // background
glMatrixMode(GL_PROJECTION)
glLoadIdentity() gluOrtho2D (0.0, 256.0,
0.0, 256.0) glMatrixMode(GL_MODELVIEW)
glClear(GL_COLOR_BUFFER_BIT) //
clear the window
49Display Callback Function
void square (void) typedef GLfloat
point2D2 point2D vertices4
100.0, 100.0,200.0, 100.0,200.0,
200.0,100.0, 200.0
glClear(GL_COLOR_BUFFER_BIT) // clear
the window glColor3f (0.0, 0.0, 1.0)
glBegin(GL_POLYGON) glVertex2fv
(vertices0) glVertex2fv
(vertices1) glVertex2fv
(vertices2) glVertex2fv
(vertices3) glEnd()
glFlush()
50What it Looks Like
51Add Interaction
void main (int argc, char argv)
glutInit ( argc, argv )
glutInitDisplayMode (GLUT_SINGLE GLUT_RGB)
glutInitWindowSize (256, 256)
glutCreateWindow (My Program") myinit
() glutKeyboardFunc ( keyboard_callback_func )
glutDisplayFunc ( square )
// register display callback glutMainLoop
()
52Keyboard Callback Function
void keyboard_callback_func ( unsigned char key,
int x, int y ) switch (key)
case q exit (0)
break case a // change
some display state break
53Idle Callbacks
- Use for animation and continuous update
- glutIdleFunc( idle )
- void idle( void )
-
- t dt
- glutPostRedisplay()
-
54How would we apply this?