java me 08-mobile3d

61
Java™ Platform, Micro Edition Part 8 Mobile 3D Graphics v3.0a 25 April 2009 1 Andreas Jakl, 2009

Upload: hemanth-raju

Post on 01-Dec-2014

1.711 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Java me 08-mobile3d

Java™ Platform, Micro Edition

Part 8 – Mobile 3D Graphics

v3.0a – 25 April 20091 Andreas Jakl, 2009

Page 2: Java me 08-mobile3d

Disclaimer

● These slides are provided free of charge at http://www.symbianresources.com and are used during Java ME courses at the University of Applied Sciences in Hagenberg, Austria at the Mobile Computing department ( http://www.fh-ooe.at/mc )

● Respecting the copyright laws, you are allowed to use them:

for your own, personal, non-commercial use

in the academic environment

● In all other cases (e.g. for commercial training), please contact [email protected]

● The correctness of the contents of these materials cannot be guaranteed. Andreas Jakl is not liable for incorrect information or damage that may arise from using the materials.

● This document contains copyright materials which are proprietary to Sun or various mobile device manufacturers, including Nokia, SonyEricsson and Motorola. Sun, Sun Microsystems, the Sun Logo and the Java™ Platform, Micro Edition are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.

Andreas Jakl, 20092

Page 3: Java me 08-mobile3d

Agenda

● Mobile 3D – Overview

● JSR 184

● Scene graph

● Your first m3g file with Blender

● Display, load and modify the 3D scene

● Objects and materials

Andreas Jakl, 20093

Page 4: Java me 08-mobile3d

IntroductionMobile 3D

Andreas Jakl, 20094

Page 5: Java me 08-mobile3d

For comparison: Current C++ / Open GL ES based N-Gage games (Symbian OS)(This is what is currently possible with mid-/higher class mobile phones)

3D Java Game Examples

Andreas Jakl, 20095

Brothers in Arms: Earned in Blood (Gameloft)

Tornado Mania! 3D (Digital Chocolate)

Planet Riders 3D (Fishlabs)

Blades & Magic 3D (Fishlabs)

Massive Snowboarding 3D (Gameloft)

Star Wars: The Force Unleashed (Universomo /

LucasArts)

One (Digital Legends)

Hooked On: Creatures of the Deep (Infinite Dreams)

Page 6: Java me 08-mobile3d

Benchmark Your Phone

● Futuremark SPMarkJava JSR 184http://www.futuremark.com/products/spmark/spmarkjavajsr184/

● JBenchmark 3D/HD:http://www.jbenchmark.com/

Andreas Jakl, 20096

Page 7: Java me 08-mobile3d

3D APIs for Java ME

● Mobile 3D Graphics API (JSR 184)

Java based 3D graphics library

Supports immediate and retained mode (low and high level)

Optional package

● Open GL ES (JSR 239)

Provide Java bindings to the Open GL ES native 3D graphics library

OpenGL ES can also be used in native code (Symbian OS / C++)

● Mascot Capsule

Like JSR 184, additionally supported by e.g. SonyEricsson

● (Java 3D from Java SE is too bloated for mobile phones: ~40 MB)

7 Andreas Jakl, 2009

Page 8: Java me 08-mobile3d

3D APIs

● M3G builds on the feature set of OpenGL ES

● Both APIs are designed concurrently

Andreas Jakl, 20098

OpenGL ES

Graphics Hardware

Native C / C++

Applications M3G (JSR 184)

Java Applications

Page 9: Java me 08-mobile3d

Performance

● Java is slow on mobile phones

● KVM most widely used today

● Nokia (and others) migrating to HotSpot VM from Sun

● But HotSpot takes a lot of RAM problematic in real life

9

0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9 1

Vertex Transformation

Image downsampling

Relative speed

HotSpot

Jazelle™

KVM

Assembly

Andreas Jakl, 2009Benchmarked on an ARM926EJ-S processor with hand-optimized Java and assembly codeDiagram from Tomi Aarnio (see sources slide at the end)

Page 10: Java me 08-mobile3d

The Basics of JSR 184Get started

Andreas Jakl, 200910

Page 11: Java me 08-mobile3d

JSR 184

● Hardware independent

Uses newer hardware + 3D accelerators if present

● Separate code from content

m3g file format contains models, animation, appearance, textures, ...

Control logic in source code

● Tightly integrated with LCDUI (from MIDP, requires floating point support of CLDC 1.1+)

● Built in high level & low level API

Scene graphs, keyframe animation, bones, ...

11 Andreas Jakl, 2009

Page 12: Java me 08-mobile3d

Modes

● Immediate Mode

Create objects programmatically

Uses triangles as primitives

Low level

● Retained mode

Create objects in 3D app

Scene graphs

Mixing is possible(e.g. insert immediate object into a scene graph)

12 Andreas Jakl, 2009

Keeps graphics processing code in

native code (no Java)!

Page 13: Java me 08-mobile3d

Tools

● Blender (Freeware, http://www.blender.org/)

m3g exporter plug-in: http://www.nelson-games.de/bl2m3g/default.html

Blender: Powerful, but difficult to learn UI

Written in Python

● Commercial

3DS Max (integrated)

Maya

Lightwave

13 Andreas Jakl, 2009

Page 14: Java me 08-mobile3d

JSR 184 – Synchronous Structure

● All APIs are synchronous

Calls return when completed

Create own thread for loading larger scenes!

● Structure

No callbacks (interfaces, events, abstract methods)

Do not override any methods

14 Andreas Jakl, 2009

Page 15: Java me 08-mobile3d

m3g File Format

● Compact binary format

● Stores complete scene graph in file

Objects are serialized

Includes camera(s), lightning and objects

● Can be compressed

● Viewer:http://www.mascotcapsule.com/toolkit/m3g/en/index.php

15 Andreas Jakl, 2009

Page 16: Java me 08-mobile3d

Scene Graph

Andreas Jakl, 200916

World

Group

Sprite3D Group

Group Mesh

MorphingMesh

SkinnedMesh

Camera Light

Background

User object

World is a top-level node containing the whole scene

Groups allow the application to treat multiple nodes as a

single unit

Sprite3D is a 2D image with a

3D position

An arbitrary user object can be associated with

any scene object

Based on the scene graph image from the JSR 184 API documentation

Morphing and skinned meshes are animated geometry objects

Mesh defines the 3D geometry of a visible object

Groups can be nested inside other groups

Background defines the background against which the 3D scene is rendered

Light defines a light source in the scene

Camera defines a viewpoint

Page 17: Java me 08-mobile3d

Graph vs. Tree

● Scene graph has a tree structure

Node can belong to at most one group at a time

But components (VertexArrays, textures, ...) can be shared

No cyclic structures allowed

Andreas Jakl, 200917

Node

Component

Node Component

Node

Group Group

Page 18: Java me 08-mobile3d

Scene Graph – Example

Andreas Jakl, 200918

The car

The landscape scene group

Tree

The tree consists of two meshes

The tree has no texture image

Grass block

Grass texture

World root node

Camera and light

Page 19: Java me 08-mobile3d

Node

● Abstract base class for all scene graph nodes

● Contains information about:

Transformation

Parent

Alignment (also automatic!)

Visibility

...

Andreas Jakl, 200919

Node

Camera LightGroup Mesh Sprite3D

World

Page 20: Java me 08-mobile3d

Key Classes

Graphics3D

Loader

World

3D graphics contextHandles all rendering

Utility to load m3g and png files(entire scene graphs or single objects)

Root node of the scene graph

20 Andreas Jakl, 2009

Page 21: Java me 08-mobile3d

Graphics3D

● Stores global state

Rendering target, viewport, depth buffer, back buffer

Camera, light sources

Rendering quality hints (Antialiasing, dithering, true colorrendering)

● Each node has its own local state

Geometry & appearance (material, textures, ...)

Transformation relative to the parent or world

21 Andreas Jakl, 2009

Graphics3D

Page 22: Java me 08-mobile3d

Graphics3D – Rendering Modes

● Retained mode

Render entire world (scenegraph)

Uses lights and camera nodes from the world

● Immediate mode

Render scene graph nodes or submesh

Uses lights and camera from Graphics3D object

Andreas Jakl, 200922

Page 23: Java me 08-mobile3d

Graphics3D – Render Targets

● Graphics3D can render to any Graphics object or Image2D

possible to render to textures (eg. environment mapping)

Andreas Jakl, 200923

World

Graphics3D

Graphics

Image2D

Canvas

Image

CustomItem

M3G(JSR 184)

MIDP

Page 24: Java me 08-mobile3d

Graphics3D – Example

● Target has to be bound and released

● Do not modify the target from the outside in between

Andreas Jakl, 200924

public class MyCanvas extends Canvas

{

Graphics3D myG3D = Graphics3D.getInstance();

public void paint(Graphics g) {

try {

myG3D.bindTarget(g);

// ... update the scene ...

// ... render the scene ...

} finally {

myG3D.releaseTarget();

}

}

Page 25: Java me 08-mobile3d

World Node

● Top level node for a scene graph

● Contains

Other nodes that compose the scene

Background

Camera, Light, Fog

● Requires an active camera for rendering

Andreas Jakl, 200925

World

Page 26: Java me 08-mobile3d

Example – Goal

● Create an m3g file with Blender

● Load and display the file in a MIDlet

● Rotate the object

Andreas Jakl, 200926

Page 27: Java me 08-mobile3d

BlenderThe basics of

Andreas Jakl, 200927

Page 28: Java me 08-mobile3d

Blender – Interface

Andreas Jakl, 200928

3D Viewport

Buttons Window

Camera

Light

Sample Cube

Page 29: Java me 08-mobile3d

Blender – Windows

Andreas Jakl, 200929

Highlighted(slightly brighter

colour)

The highlighted window receiveskeyboard inputs:

Change the window type:

... you can split, join and resize windows anddrag mini windows (with the )

Page 30: Java me 08-mobile3d

Blender – 3D Viewport

Andreas Jakl, 200930

Click + hold middle mouse button Rotate viewport

Shift + middle mouse button Pan view (move sideways)

Mouse wheel Zoom

Z-key Toggle wireframe or solid mode

NUM5 (with NUM LOCK on) Toggle Ortographic / Perspective

NUM0 (with NUM LOCK on) Camera view (MMB will exit it)

Choose viewport shading (more options than Z)

NUM refers to the number pad – if you pressed the numbers above the normal

keys instead, press 1 to return to the normal view (for viewing layer 1)

Page 31: Java me 08-mobile3d

The 3D Cursor

Andreas Jakl, 200931

Task: place the 3D cursor between the camera and the cube

1. Make sure you are in object mode (press TAB to toggle)

2. Disable the “3d transform manipulator” to make sure you can move the cursor without selecting the cube by accident

3. Hit NUM7 to get to the top view4. Click with LMB between cube and

camera5. Choose different view (NUM1 – front

view or NUM3 – side view)6. Click between cube and camera with

LMB7. Rotate the view to see if it was

positioned correctly

Note: we’re working in a 3D space but only have a 2D screen –therefore you need two views to set all three coordinates of the cursor!

Page 32: Java me 08-mobile3d

Deleting and Adding Objects

Andreas Jakl, 200932

Task: replace the cube with a monkey

1. Set “Object Mode”; switch off “3d transform manipulator” (see previous slide)

2. Move the cursor to the center(Shift+CKEY)

3. RMB on the cube to select it4. DELKEY to delete the object. Confirm in

the prompt window (“Erase Selected”)5. Use the Add menu to add a new object.

Here: Add Mesh Monkey6. Blender automatically switches to edit

mode. Go to object mode (TAB), press CKEY to center the cursor on the screen, ZKEY to toggle solid and wireframe mode. Zoom in (Mouse wheel)

Page 33: Java me 08-mobile3d

Material

Andreas Jakl, 200933

Task: change the material of the monkey

1. Select the monkey object (RMB); set “Object Mode”

2. Press the shading button3. In contrast to the cube, the monkey doesn’t

have a default material. Click the button next to “Add New” and choose “O Material” (same that was originally on the cube)

4. In the “Material” tab, set any color you want for “Col” and “Spe” (specular color, for highlights)

5. Press the button with the small car in the “Links and Pipeline” tab for an automatic material name

6. Set the draw type of the 3D Viewport to “Shaded” for a more accurate preview

Page 34: Java me 08-mobile3d

IDs and m3g Export

Andreas Jakl, 200934

Task: to access the nodes from the m3g file in source code, they need IDs.

1. Set “Object Mode”2. Select the monkey with the RMB3. Add #01 at the end of the object name

to give it the ID 1 in the m3g file4. Do the same for the light (#02) and the

camera (#03)

Task: export the m3g file (requires the m3g exporter plug-in)

1. File Export M3G2. You can safely disable texturing

Page 35: Java me 08-mobile3d

m3g File

Andreas Jakl, 200935

Open the m3g file in the m3g viewer* and choose Display Scene Graph View

Our IDs have been saved in the file

* http://www.mascotcapsule.com/toolkit/m3g/en/index.php

Page 36: Java me 08-mobile3d

Display the 3D sceneSwitching to the Java side

Andreas Jakl, 200936

Page 37: Java me 08-mobile3d

Loading the m3g File

● Create a simple MIDlet and Canvas-class (sample start project is provided)

● Add the monkey.m3g to the root of the .jar

● Init code snippetof the Canvas:

Andreas Jakl, 200937

Object3D[] roots = null;try {

// Load the m3g file// The loader will return all root level objects of the file,// which are not referenced by any other objects.// Always state an absolute path ("/")!roots = Loader.load("/monkey.m3g");

} catch (IOException ex) {// couldn't open the file, or invalid data in the fileex.printStackTrace();

}// Usually the world node is the only and first node.// If loading unknown files, you should check it though.// Save the world node to an instance variablefor (int i = 0; i < roots.length; ++i) {

if (roots[i] instanceof World) {iWorld = (World) roots[i];

}}

Page 38: Java me 08-mobile3d

Game loop in a GameCanvas

Andreas Jakl, 200938

public void run() {// Get the singleton Graphics3D instance that is associated with this midlet.Graphics3D g3d = Graphics3D.getInstance();// Measure the time that has passed since the prev. framelong start, elapsed = 0; int time = 0;while (iIsActive) {

start = System.currentTimeMillis();try {

// Bind the 3D graphics context to the given MIDP Graphics object.g3d.bindTarget(getGraphics());// Update the world [...]iWorld.animate(time); // Animate the world// Render the view from the active camerag3d.render(iWorld);

} finally {// Release the graphics contextg3d.releaseTarget();

}flushGraphics(); // Flush the rendered image to the screen// Give other threads a chance to runThread.sleep(20); // This sample omits try and catchelapsed = System.currentTimeMillis() - start;time += elapsed; // Time that has passed since the last frame

}}

Page 39: Java me 08-mobile3d

MIDP Relationship

Andreas Jakl, 200939

Page 40: Java me 08-mobile3d

The Camera

● The camera was too far away in the Blender scene

● Move it towards the object after loading the world:

Andreas Jakl, 200940

Camera cam = iWorld.getActiveCamera();cam.setTranslation(-5.0f, 5.0f, -4.0f);

Page 41: Java me 08-mobile3d

Node Transformations

● Each node: local coordinate system

● Node transformation: from this node to the parent node (ignored for root)

Translation T

Rotation R

Non-uniform scale S

Generic matrix M

● Composite C = TRSM

Andreas Jakl, 200941

World

Group

Sprite3D Group

Group Mesh

Page 42: Java me 08-mobile3d

Modifying Objects

● Rotate the monkey

Andreas Jakl, 200942

private static final int ID_MONKEY = 1;

// Find searches through all children of this nodeNode monkey = (Node) iWorld.find(ID_MONKEY);switch (getKeyStates()){

case LEFT_PRESSED:// Parameters: angle, x / y / z component of// the rotation axis. Here: all on z axismonkey.postRotate(5.0f, 0.0f, 0.0f, -1.0f);break;

case RIGHT_PRESSED:monkey.postRotate(5.0f, 0.0f, 0.0f, 1.0f);break;

}

Page 43: Java me 08-mobile3d

Objects and MaterialsWhat is actually rendered?

Andreas Jakl, 200943

Page 44: Java me 08-mobile3d

Textures

● Add visual richness

Backgrounds

Surface structure

Sprites for “cheating”(no complex 3D models)

Andreas Jakl, 200944

Character images copyright David Danghttp://www.chi-3d.co.uk/

Sprites in “Doom” by id Software

Page 45: Java me 08-mobile3d

Perspective Correction

● Perspective correction

Expensive, avoid if possible

But still better than a lot more polygons

Nokia: 2% fixed overhead, 20% in the worst case

Andreas Jakl, 200945

Perspective correctionhttp://en.wikipedia.org/wiki/Texture_mapping

Page 46: Java me 08-mobile3d

Light Maps

● Lightning

Use light maps instead of real light

Pre-calculate light for non-textured (colored) objects: vertex coloring

Andreas Jakl, 200946

w/o light maps

light map(unfiltered)

light map(filtered)

with light maps

Images from lecture slides of Stephen Chenney (see sources at the end)

Page 47: Java me 08-mobile3d

Mobile Textures II

● Backgrounds

Use background images, no skybox or real 3D

● Sprites

Much faster than real geometry or textured quads

But too much overhead for particle effects

● Images

Load through Loader class (Image2D) –going through MIDP Image wastes memory

Andreas Jakl, 200947

Sprites in a 3D scenePlayman World Soccer

(Mr.Goodliving)

3D Sprites can be useful for 2D games as well

(rotation!)Tower Bloxx (Digital

Chocolate)

Page 48: Java me 08-mobile3d

AppearanceAppearance

IndexBufferIndexBuffer

Mesh

● How is the object defined? (vertices, material, ...)

Andreas Jakl, 200948

Mesh

VertexBuffer

IndexBuffer

Appearance

Vertex positions, normals, texture coordinates, ...

Defines a submesh(consisting of triangle strips) –reference data from the vertex buffer

1 Appearance per submesh. Defines color, texture, ...

Page 49: Java me 08-mobile3d

Sprite3D

● 2D image with 3D position

● Fast, but functionally restricted alternative to textured geometry

Scaled mode: billboards, trees, ...

Unscaled mode: text labels, icons, ...

Andreas Jakl, 200949

Sprite3D Appearance

Image2D

Contains compositing and fogging attributes

Page 50: Java me 08-mobile3d

AnimationShort overview of

Andreas Jakl, 200950

Page 51: Java me 08-mobile3d

Animations

● Defined in the m3g file

● Automatically played corresponding to the time by animate() method

Andreas Jakl, 200951

Animated Object (Object3D)

AnimationTrack

AnimationTrack

AnimationController

KeyframeSequence

KeyframeSequence

Each track associates a property (e.g. position) with a controler & keyframe data.

Multiple tracks may be blended.

Values of the animated property (keyframes) + the interpolation mode.

Defines time within the animation, speed, weight for blending, ...

Page 52: Java me 08-mobile3d

KeyframeSequence

● Keyframe: time & property value at that time

● Sequence:

Can store multiple keyframes

Several interpolation methods

Can be looping

Andreas Jakl, 200952

KeyframeSequence

Diagram based on Sean Ellis, Superscape

sequence time

pro

per

ty v

alu

e

t

v

Page 53: Java me 08-mobile3d

KeyframeSequence

● Keyframe: time & property value at that time

● Sequence:

Can store multiple keyframes

Several interpolation methods

Can be looping

Andreas Jakl, 200953

KeyframeSequence

Diagram based on Sean Ellis, Superscape

sequence time

pro

per

ty v

alu

e

Page 54: Java me 08-mobile3d

KeyframeSequence

● Keyframe: time & property value at that time

● Sequence:

Can store multiple keyframes

Several interpolation methods

Can be looping

Andreas Jakl, 200954

KeyframeSequence

Diagram based on Sean Ellis, Superscape

sequence time

pro

per

ty v

alu

e

Page 55: Java me 08-mobile3d

KeyframeSequence

● Keyframe: time & property value at that time

● Sequence:

Can store multiple keyframes

Several interpolation methods

Can be looping

Andreas Jakl, 200955

KeyframeSequence

Diagram based on Sean Ellis, Superscape

sequence time

pro

per

ty v

alu

e

t

v

Page 56: Java me 08-mobile3d

AnimationController

● Controls position, speed and weight of an animation sequence

● Usually controls multiple AnimationTracks (properties) at the same time

● Defines mapping from world time to sequence time

Andreas Jakl, 200956

world time

sequence time

AnimationController

t1

0 d

t2

s

Diagram based on Sean Ellis, Superscape

Page 57: Java me 08-mobile3d

Morphing

Andreas Jakl, 200957

Mesh MorphingMesh

Base Target 1eyes closed

Target 2mouth closed

Animate eyesand mouth

independently

Images from Aarnio and Pulli (see sources at the end)

Unfortunately, the animations are not exported to the pdf

Page 58: Java me 08-mobile3d

Skinning

Andreas Jakl, 200958

No skinning Local skinningone bone per vertex

Smooth skinningtwo bones per vertex

Mesh SkinnedMesh

Images from Aarnio and Pulli (see sources at the end)

Unfortunately, the animations are not exported to the pdf

Page 59: Java me 08-mobile3d

Summary

● M3G enables real-time 3D on mobile phones

Works both with software and hardware 3D

Supported by millions of devices

● Mixture of low- and high level

Allows easy creation of scenes

But still gives the developer full control

● Flexible architecture

Based on OpenGL ES features

Defines a convenient file format

Andreas Jakl, 200959

Page 60: Java me 08-mobile3d

Sources

● Tomi Aarnio and Kari Pulli: Advanced Game Development with the Mobile 3D Graphics API. JavaOne Conference, 2004.

● Andrew Davison: Special Topics in Info. Eng.: Introduction to J2ME Programming. Lecture slides, 2007

● Qusay H. Mahmoud: Getting Started With the Mobile 3D Graphics API for J2ME. http://developers.sun.com/mobility/apis/articles/3dgraphics/

● Carol Hamer: Creating a basic M3G file with Blender. http://bittyjava.wordpress.com/2007/01/04/creating-a-basic-m3g-file-with-blender/

● David Millet, Arthur Tombs et al.: Blender 3D: Noob to Pro. http://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro

● Mikael Baros: 3D programming tutorial for mobile devices using M3G (JSR 184). http://developer.sonyericsson.com/site/global/techsupport/tipstrickscode/mobilejava3d/p_java3d_tutorial_part1_compliments_redikod.jsp

● Kari Pulli et al.: Developing Mobile 3D Applications With OpenGL ES and M3G. Eurographics 2006. http://people.csail.mit.edu/kapu/mobile_3D_course/index.html

● Janne Hellsten: Building scalable 3D apps - Tips & tricks for developers. http://www.khronos.org/developers/library/seoul_04_2005/Hybrid_Tips-and-Tricks.ppt

● Stephen Chenney: Computer Game Technology. Lecture slides, 2001. http://www.cs.wisc.edu/graphics/Courses/638-f2001/

Andreas Jakl, 200960

Page 61: Java me 08-mobile3d

Thanks for your attentionThat’s it!

Andreas Jakl, 200961