📄 demobase.java
字号:
/**
*
* COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2004.
*
* The software is the copyrighted work of Sony Ericsson Mobile Communications AB.
* The use of the software is subject to the terms of the end-user license agreement which
* accompanies or is included with the software. The software is provided "as is" and Sony Ericsson
* specifically disclaim any warranty or condition whatsoever regarding merchantability or fitness for
* a specific purpose, title or non-infringement. No warranty of any kind is made in relation to the condition,
* suitability, availability, accuracy, reliability, merchantability and/or non-infringement of the software provided herein
*
*/
package com.sonyericsson.javatest.mobile3d;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Background;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.IndexBuffer;
import javax.microedition.m3g.Light;
import javax.microedition.m3g.Material;
import javax.microedition.m3g.Transform;
import javax.microedition.m3g.TriangleStripArray;
import javax.microedition.m3g.VertexArray;
import javax.microedition.m3g.VertexBuffer;
/**
*
* Base class for demo.
*/
public abstract class DemoBase extends Canvas {
private long animDelay = 1;
private float iAngle = 30.0f;
protected Appearance iAppearance; // material, texture, compositing, ...
private Background iBackground = new Background();
private Camera iCamera;
private Graphics3D iG3D;
private IndexBuffer iIb; // indices to iVB, forming triangle strips
protected Image iImage;
private Light iLight;
protected Material iMaterial = new Material();
private Timer iTimer;
private Transform iTransform = new Transform();
private VertexBuffer iVb; // positions, normals, colors, texcoords
private String labelFps = "FPS/Avg";
private boolean showLabelFlag = true;
private float turnAngle = 2.0f;
private boolean backgroundMusic = false;
FrameCounter fCounter;
private int bgColor = 0x335533; //green background
private float lightIntensity = 1.25f;
private float xTurnAngle = 1.0f;
private float yTurnAngle = 5.0f;
private float zTurnAngle = 5.0f;
class KeyListener implements CommandListener {
public void commandAction(Command c, Displayable d) {
if (c.getCommandType() == Command.EXIT) {
// exit the MIDlet
Mobile3D.quitApp();
} else if (c.getCommandType() == Command.OK) {
//nothing!
} else {
System.err.println("Unknown command!");
}
}
}
/**
* Our timer task for providing animation.
*/
class MyTimerTask extends TimerTask {
synchronized public void run() {
repaint();
}
}
public static void displayMenu() {
Menu lm = new Menu();
}
/**
* Construct the Test.
*/
public DemoBase() {
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
fCounter = new FrameCounter();
startTimer();
}
/**
* Paint the scene.
*/
public void paint(Graphics g) {
iBackground.setColor(bgColor); // set the background color
// Bind the Graphics of this Canvas to our Graphics3D. The
// viewport is automatically set to cover the entire clipping
// rectangle of the Graphics object. The parameters indicate
// that z-buffering, dithering and true color rendering are
// enabled, but antialiasing is disabled.
iG3D.bindTarget(g, true, Graphics3D.DITHER | Graphics3D.TRUE_COLOR);
// clear the color and depth buffers
iG3D.clear(iBackground);
// set up the camera in the desired position
Transform transform = new Transform();
transform.postTranslate(0.0f, 0.0f, 40.0f);
iG3D.setCamera(iCamera, transform);
// set up a "headlight": a directional light shining
// from the direction of the camera
iG3D.resetLights();
iG3D.addLight(iLight, transform);
// update our transform (this will give us a rotating cube)
iAngle += turnAngle;
iTransform.setIdentity();
iTransform.postRotate(iAngle, // rotate 1 degree per frame
xTurnAngle, yTurnAngle, zTurnAngle); // rotate around this axis
// Render our cube. We provide the vertex and index buffers
// to specify the geometry; the appearance so we know what
// material and texture to use; and the transform to tell
// where to render the object
iG3D.render(iVb, iIb, iAppearance, iTransform);
// flush
iG3D.releaseTarget();
//update framerate counter
try {
fCounter.update();
} catch (Exception e) {
//System.out.println("counter update error");
}
// Pass Fps value to main component
Mobile3D.instance.setAverageFpsString(fCounter.getAverageFpsString());
//draw fps counter
try {
fCounter.drawFps(g);
} catch (Exception e) {
System.out.println("counter paint error");
}
// paint the canvas
repaint();
}
/**
* Component initialization.
*/
private void init() throws Exception {
setFullScreenMode(true);
// get the singleton Graphics3D instance
iG3D = Graphics3D.getInstance();
// create a camera
iCamera = new Camera();
iCamera.setPerspective(60.0f, // field of view
(float) getWidth() / (float) getHeight(), // aspectRatio
1.0f, // near clipping plane
1000.0f); // far clipping plane
// create a light
iLight = new Light();
iLight.setColor(0xffffff); // white light
iLight.setIntensity(lightIntensity); // overbright
// init some arrays for our object (cube)
// Each line in this array declaration represents a triangle strip for
// one side of a cube. The only primitive we can draw with is the
// triangle strip so if we want to make a cube with hard edges we
// need to construct one triangle strip per face of the cube.
short[] vert = { 10, 10, 10, -10, 10, 10, 10, -10, 10, -10, -10, 10, // front
-10, 10, -10, 10, 10, -10, -10, -10, -10, 10, -10, -10, // back
-10, 10, 10, -10, 10, -10, -10, -10, 10, -10, -10, -10, // left
10, 10, -10, 10, 10, 10, 10, -10, -10, 10, -10, 10, // right
10, 10, -10, -10, 10, -10, 10, 10, 10, -10, 10, 10, // top
10, -10, 10, -10, -10, 10, 10, -10, -10, -10, -10, -10 }; // bottom
// create a VertexArray to hold the vertices for the object
VertexArray vertArray = new VertexArray(vert.length / 3, 3, 2);
vertArray.set(0, vert.length / 3, vert);
// The per-vertex normals for the cube; these match with the vertices
// above. Each normal is perpendicular to the surface of the object at
// the corresponding vertex.
byte[] norm = { 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, -127, 0, 0, -127, 0, 0, -127, 0, 0, -127,
-127, 0, 0, -127, 0, 0, -127, 0, 0, -127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 0, 127, 0,
0, 127, 0, 0, 127, 0, 0, 127, 0, 0, -127, 0, 0, -127, 0, 0, -127, 0, 0, -127, 0 };
// create a vertex array for the normals of the object
VertexArray normArray = new VertexArray(norm.length / 3, 3, 1);
normArray.set(0, norm.length / 3, norm);
// per vertex texture coordinates
short[] tex = { 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1 };
// create a vertex array for the texture coordinates of the object
VertexArray texArray = new VertexArray(tex.length / 2, 2, 2);
texArray.set(0, tex.length / 2, tex);
// the length of each triangle strip
int[] stripLen = { 4, 4, 4, 4, 4, 4 };
// create the VertexBuffer for our object
VertexBuffer vb = iVb = new VertexBuffer();
vb.setPositions(vertArray, 1.0f, null); // unit scale, zero bias
vb.setNormals(normArray);
vb.setTexCoords(0, texArray, 1.0f, null); // unit scale, zero bias
// create the index buffer for our object (this tells how to
// create triangle strips from the contents of the vertex buffer).
iIb = new TriangleStripArray(0, stripLen);
applyAppearance(); //apply appearance to object
iBackground.setColor(bgColor); // set the background color
}
/**
* Apply appearance for object
*
*/
protected void applyAppearance() {
}
/**
* This method is called when app is removed from view
*/
protected synchronized void hideNotify() {
//Save time for FPS accuracy
fCounter.suspend();
iTimer.cancel();
}
/**
* Handle key release event
*/
protected void keyReleased(int keyCode) {
switch (keyCode) {
case KEY_POUND:
fCounter.setShowLabelFlag(!fCounter.isShowLabelFlag());
return;
case KEY_NUM1:
turnAngle = turnAngle + 1;
return;
case KEY_NUM2:
yTurnAngle = yTurnAngle + 1;
return;
case KEY_NUM8:
yTurnAngle = yTurnAngle - 1;
return;
case KEY_NUM6:
xTurnAngle = xTurnAngle + 1;
return;
case KEY_NUM4:
xTurnAngle = xTurnAngle - 1;
return;
case KEY_STAR:
Random r =new Random();
bgColor = r.nextInt();
return;
default:
displayMenu();
}
}
/**
* Handle the application going from hidden state to normal;
*/
protected synchronized void showNotify() {
// make FPS counter think that it's a continious time
fCounter.restore();
startTimer();
}
/**
* Instantiate and start the timer
*
*/
protected void startTimer() {
iTimer = new Timer();
iTimer.schedule(new MyTimerTask(), 0, animDelay);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -