Java 3D, GLUT input - PowerPoint PPT Presentation

About This Presentation
Title:

Java 3D, GLUT input

Description:

such as geometry, lights, sounds. Shape3D--important subclass. Group ... Physical device that can be described by their real-world physical properties. ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 55
Provided by: LeeMcC
Category:
Tags: glut | input | java

less

Transcript and Presenter's Notes

Title: Java 3D, GLUT input


1
Java 3D, GLUT input
2
Java 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)

3
Simple Scene Graph Example
4
Assembling 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

5
Assembling 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

6
Terminology
  • 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

7
SceneGraph 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

8
The 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

9
Packages
  • 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.

10
General 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

11
SceneGraphObject 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

12
Node
  • 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

13
Leaf
  • has no children
  • may reference NodeComponent objects
  • superclass for elements used in rendering
  • such as geometry, lights, sounds
  • Shape3D--important subclass

14
Group
  • may contain child node objects
  • superclass of important BranchGroup and
    TransformGroup nodes
  • addChild() method is used most often

15
NodeComponent
  • 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.

16
Hello3D Applet/Application
  • static program rendering one 3D object

SimpleUniverse
17
Hello3D.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)

18
Create Hello3D Scene Graph
  • public class Hello3D extends Applet
  • public BranchGroup createSceneGraph()
  • BranchGroup objRoot new BranchGroup()
  • objRoot.addChild(new ColorCube())
  • return objRoot

19
Interaction
  • 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

20
Input 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)

21
Physical Device Types
  • Keyboard
  • Choice
  • Locators

22
Keyboard
  • Returns character coders with specific meanings

23
Choice
  • Returns a choice that has been selected from a
    number of discrete options

- Button box - Function keypad
24
Locator Devices
  • Return a position and/or orientation
  • Mouse
  • Trackball
  • Tablet
  • Joystick
  • Touch screen

25
3D Input Devices
  • Return 3D position and /or orientation
  • Digitizer
  • 3D Spaceball
  • Glove
  • Tracker

26
3D Input Devices
Digitizer - 3D model
27
3D Input Devices
3D Spaceball
28
3D Input Devices
29
3D Input Devices
Motion Tracker - Magnetic - Acoustic -
Inertial - Optical - GPS
30
Logical Device
  • Characterized from the perspective of user
    application program
  • High-level interface with the user program
  • An abstraction of device data

31
Logical 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

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

34
Sample 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

35
Request 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()

36
Event 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

37
Event 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

38
Event Mode
  • Two Asynchronous Processes

Trigger Process
Event Queue
Measure Process
39
Interactive GraphicsProgramming
  • We will focus on
  • Event-driven Input mode
  • Callback mechanism to do event process
  • GLUT programming

40
Callback 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.

41
Event-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

42
GLUT 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)

43
Keyboard 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
44
Mouse 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

45
Mouse Event
void mouse_callback_func ( int button, int
state, int x, int y ) if ( button
GLUT_LEFT_BUTTON state GLUT_DOWN )
exit (0)
46
You Must Register Callback
void main (int argc, char argv)
glutDisplayFunc ( display ) //
display callback
glutReshapeFunc ( resize ) // window
resize glutKeyboardFunc ( key )
// keyboard callback
47
Main 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 ()
48
OpenGL 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
49
Display 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()
50
What it Looks Like
51
Add 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
()
52
Keyboard 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
53
Idle Callbacks
  • Use for animation and continuous update
  • glutIdleFunc( idle )
  • void idle( void )
  • t dt
  • glutPostRedisplay()

54
How would we apply this?
  • To Pong
Write a Comment
User Comments (0)
About PowerShow.com