Presentation is loading. Please wait.

Presentation is loading. Please wait.

240-571 J2ME: M3G/11 Intro to J2ME. Prog. v Objectives: –to continue talking about "M3G Chapter 2. An Animated Model" u explain animation and the floor.

Similar presentations


Presentation on theme: "240-571 J2ME: M3G/11 Intro to J2ME. Prog. v Objectives: –to continue talking about "M3G Chapter 2. An Animated Model" u explain animation and the floor."— Presentation transcript:

1 240-571 J2ME: M3G/11 Intro to J2ME. Prog. v Objectives: –to continue talking about "M3G Chapter 2. An Animated Model" u explain animation and the floor u leave the penguin until next time 240-571, Semester 2, 2006-2007 M3G. Part 2

2 240-571 J2ME: M3G/12 1. M3G Chapter 2. An Animated Model v Make a penguin walk in circles http://fivedots.coe.psu.ac.th/~ad/jg/m3g2/

3 240-571 J2ME: M3G/13 Features v Uses retained mode. v The scene includes a single directional light, a textured floor, a light blue background –or an image background v Mixes M3G rendering and MIDP's drawString() v Penguin model is an animated Mesh –it translates and rotates

4 240-571 J2ME: M3G/14 AnimM3G Class Diagrams MIDlet Canvas CommandListener TimerTask

5 240-571 J2ME: M3G/15 Scene Graph World scene LightBackgroundCamera Mesh floorMesh Group built by the Floor class Mesh model transRotGroup animated built by the AnimModel class scene graph children

6 240-571 J2ME: M3G/16 AnimCanvas Constructor private World scene; // global variable public AnimCanvas(...) { //... other code scene = new World(); buildScene(); //... other code }

7 240-571 J2ME: M3G/17 private void buildScene() // add nodes to the scene graph { addCamera(); addLight(); addBackground(); animModel = new AnimModel(); scene.addChild( animModel.getModelGroup() ); // add the model addFloor(); } // end of buildScene()

8 240-571 J2ME: M3G/18 Adding the Penguin // global for translating and rotating the model private Group transRotGroup; public AnimModel() { // other code... Mesh model = makeModel(); // reposition the model's start position and size model.setTranslation(0.25f, 0.25f, 0.25f); // so at center model.scale(0.5f, 0.5f, 0.5f); // translation/rotation group for the model transRotGroup = new Group(); transRotGroup.addChild(model); // other code... }

9 240-571 J2ME: M3G/19 Changing the Penguin's Position Mesh model = makeModel(); // reposition the model's start position and size model.setTranslation(0.25f, 0.25f, 0.25f); // so at center model.scale(0.5f, 0.5f, 0.5f);

10 240-571 J2ME: M3G/110 2. Updating the Application timer run() { animCanvas.update(); } AnimTimer (in AnimM3G) update() { // increment world time // call animate() on world // call repaint() } AnimCanvas paint() { // render the 3D scene // draw 2D info. string } AnimCanvas call run() every PERIOD ms update the canvas KVM repaint request

11 240-571 J2ME: M3G/111 AnimCanvas // global timing information private int appTime = 0; private int nextTimeToAnimate; public AnimCanvas(AnimM3G top) { // other code... // start the animation nextTimeToAnimate = scene.animate(appTime); } public void update() // called by TimerTask every PERIOD (50) ms { appTime++; if (appTime >= nextTimeToAnimate) { nextTimeToAnimate = scene.animate(appTime) + appTime; repaint(); } }

12 240-571 J2ME: M3G/112 3. Animating the Penguin

13 240-571 J2ME: M3G/113 From time to Update AnimationTrack Animation Controller Keyframe Sequence twtw twtw tsts tsts update property frame t w = world time t s = sequence time t s = speed*(t w – tRef w ) + tRef s

14 240-571 J2ME: M3G/114 A Keyframe Sequence

15 240-571 J2ME: M3G/115 Initializing the Animation private void setUpAnimation() { // creation animation controller AnimationController animController = new AnimationController(); animController.setActiveInterval(0, 1500); // animController.setSpeed(2.0f, 0); // creation translation animation track KeyframeSequence transKS = translateFrames(); AnimationTrack transTrack = new AnimationTrack(transKS, AnimationTrack.TRANSLATION); transTrack.setController(animController); transRotGroup.addAnimationTrack(transTrack); :

16 240-571 J2ME: M3G/116 // creation rotation animation track KeyframeSequence rotKS = rotationFrames(); AnimationTrack rotTrack = new AnimationTrack(rotKS, AnimationTrack.ORIENTATION); rotTrack.setController(animController); transRotGroup.addAnimationTrack(rotTrack); } // end of setUpAnimation()

17 240-571 J2ME: M3G/117 Translation KeyFrames

18 240-571 J2ME: M3G/118 Translation Frame Sequence

19 240-571 J2ME: M3G/119 Code for Sequence private KeyframeSequence translateFrames() { KeyframeSequence ks = new KeyframeSequence(8, 3, KeyframeSequence.SPLINE); ks.setKeyframe(0,0, new float[] { -0.5f,0.0f, 0.0f }); ks.setKeyframe(1,10, new float[] {-0.3536f,0.0f,0.3536f }); ks.setKeyframe(2,20, new float[] { 0.0f, 0.0f, 0.5f }); ks.setKeyframe(3,30, new float[] { 0.3536f,0.0f, 0.3536f }); ks.setKeyframe(4,40, new float[] { 0.5f, 0.0f, 0.0f }); ks.setKeyframe(5,50, new float[] { 0.3536f,0.0f,-0.3536f }); ks.setKeyframe(6,60, new float[] { 0.0f, 0.0f, -0.5f }); ks.setKeyframe(7,70, new float[] {-0.3536f,0.0f,-0.3536f }); :

20 240-571 J2ME: M3G/120 ks.setDuration(80); // one cycle takes 80 seq time units ks.setValidRange(0, 7); ks.setRepeatMode(KeyframeSequence.LOOP); return ks; } // end of translateFrames()

21 240-571 J2ME: M3G/121 Rotation Keyframes

22 240-571 J2ME: M3G/122 Rotation Frame Sequence

23 240-571 J2ME: M3G/123 Code for Sequence private KeyframeSequence rotationFrames() { KeyframeSequence ks = new KeyframeSequence(8,4,KeyframeSequence.SLERP); ks.setKeyframe(0, 0, rotYQuat(0)); ks.setKeyframe(1, 10, rotYQuat(45)); ks.setKeyframe(2, 20, rotYQuat(90)); ks.setKeyframe(3, 30, rotYQuat(135)); ks.setKeyframe(4, 40, rotYQuat(180)); ks.setKeyframe(5, 50, rotYQuat(225)); ks.setKeyframe(6, 60, rotYQuat(270)); ks.setKeyframe(7, 70, rotYQuat(315)); ks.setDuration(80); ks.setValidRange(0, 7); ks.setRepeatMode(KeyframeSequence.LOOP); return ks; } // end of rotationFrames()

24 240-571 J2ME: M3G/124 Axis-Angle Rotation v axis-angle = [Vx, Vy, Vz, angle] V angle

25 240-571 J2ME: M3G/125 To Quaternions v The axis angle [Vx,Vy,Vz, angle] becomes the quaternion: –[ sin(angle/2)*Vx, sin(angle/2)*Vy, sin(angle/2)*Vz, cos(angle/2) ] –called [i, j, k, scalar] u 4 values

26 240-571 J2ME: M3G/126 What is a Quaternion? v It defines a point on a 4D sphere! v Rotation between two quaternions is the same as a constant speed movement between the two points using the shortest distance.

27 240-571 J2ME: M3G/127 Rotation Around the Y-axis private float[] rotYQuat(double angle) /* Calculate the quaternion for a clockwise rotation of angle degress about the y-axis. */ { double radianAngle = Math.toRadians(angle)/2.0; float[] quat = new float[4]; quat[0] = 0.0f; // i coefficient quat[1] = (float) Math.sin(radianAngle); // j coef quat[2] = 0.0f; // k coef quat[3] = (float) Math.cos(radianAngle); // scalar component a return quat; } // end of rotYQuaternion()

28 240-571 J2ME: M3G/128 4. Mesh Details VertexBuffer IndexBuffer Appearance composed of 1 or more VertexBuffer positions VertexArray normals VertexArray colours VertexArray composed of 0 or 1 tex. coords. VertexArray 0 or more Appearance Material PolygonMode CompositingMode composed of Fog Texture2D Image2D 1 or more Mesh

29 240-571 J2ME: M3G/129 Vertex Indicies v Vertex indicies {0, 1, 2, 3, 4, 5} are organised into a triangle strip: Triangles: (0,1,2) (1,2,3) (2,3,4) (3,4,5)

30 240-571 J2ME: M3G/130 5. Adding the Floor private void addFloor() { Image2D floorIm = loadImage("/bigGrid.gif"); // large, so slow to load Floor f = new Floor( floorIm, 8); //8 by 8 sz // Image2D floorIm = loadImage("/grass.gif"); // or try "/floor.png" // Floor f = new Floor( floorIm, 6);// 6 by 6 scene.addChild( f.getFloorMesh() ); }

31 240-571 J2ME: M3G/131 The Floor Grid Image

32 240-571 J2ME: M3G/132 Making the Floor Triangles: (1,2,0) (2,0,3) Triangle strip: {1, 2, 0, 3}

33 240-571 J2ME: M3G/133 Texture Coordinates

34 240-571 J2ME: M3G/134 Code private Mesh floorMesh; public Floor(Image2D floorIm, int size) { VertexBuffer floorVertBuf = makeGeometry(size); int[] indicies = {1,2,0,3}; // one quad int[] stripLens = {4}; IndexBuffer floorIdxBuf = new TriangleStripArray(indicies, stripLens); Appearance floorApp = makeAppearance(floorIm); floorMesh = new Mesh(floorVertBuf, floorIdxBuf, floorApp); } // end of Floor()

35 240-571 J2ME: M3G/135 private VertexBuffer makeGeometry(int size) /* A square centered at the origin on the XZ plane with sides of length size. No normals. */ { // create vertices short pt = (short)(size/2); short npt = (short) (-size/2); // negative pt short[] verts = {npt,0,pt, pt,0,pt, pt,0,npt, npt,0,npt}; VertexArray va = new VertexArray(verts.length/3, 3, 2); va.set(0, verts.length/3, verts); :

36 240-571 J2ME: M3G/136 // create texture coordinates short[] tcs = {0,1, 1,1, 1,0, 0,0}; VertexArray texArray = new VertexArray(tcs.length/2, 2, 2); texArray.set(0, tcs.length/2, tcs); VertexBuffer vb = new VertexBuffer(); vb.setPositions(va, 1.0f, null); // no scale, bias vb.setTexCoords(0, texArray, 1.0f, null); return vb; } // end of makeGeometry()

37 240-571 J2ME: M3G/137 private Appearance makeAppearance(Image2D floorIm) // The appearance is only a single texture; no material. { Appearance app = new Appearance(); if (floorIm != null) { Texture2D tex = new Texture2D(floorIm); tex.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST); tex.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP); app.setTexture(0, tex); } :

38 240-571 J2ME: M3G/138 // add perspective correction, and switch off culling PolygonMode floorPolygonMode = new PolygonMode(); floorPolygonMode.setPerspectiveCorrectionEnable(true); floorPolygonMode.setCulling(PolygonMode.CULL_NONE); app.setPolygonMode(floorPolygonMode); return app; } // end of makeAppearance()


Download ppt "240-571 J2ME: M3G/11 Intro to J2ME. Prog. v Objectives: –to continue talking about "M3G Chapter 2. An Animated Model" u explain animation and the floor."

Similar presentations


Ads by Google