📄 micro3dcanvas.java
字号:
import com.mascotcapsule.micro3d.v3.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
public class Micro3DCanvas extends GameCanvas implements Runnable
{
/**
* Rotation value expressed in a format
* where 360 degrees equals 4096 units.
*/
private static final int rotationIncrement = 10;
private boolean isRun = true;
/**
* 3D model
*/
private static Texture robotTexture;
private static Figure robotFigure;
private static ActionTable robotAction;
Graphics3D g3d = new Graphics3D();
/**
* The current frame in the animation
*/
private static int currentFrame = 0;
/**
* There are two different animations that is loaded
* dynamically using the same variable (robotAction).
* robotActionNumber is used to know which one we are currently dealing with.
*/
private static int robotActionNumber = 1;
private Vector3D ROBOT_POSITION = new Vector3D(-50,200,450);
private Vector3D ROBOT_LOOK = new Vector3D(0,-500,-4096);
private Vector3D ROBOT_UP = new Vector3D(0,4096,0);
/**
* Light
*
*/
private Vector3D lightDirVec = new Vector3D(-146,293,439);
private int lightDir = 3730;
private int lightAmbi = 1626;
private Light backgroundLight = new Light(lightDirVec,lightDir,lightAmbi);
// AffineTrans for robot
private AffineTrans robotXTrans;
private AffineTrans robotYTrans;
private AffineTrans robotZTrans;
private AffineTrans robotTrans;
private AffineTrans robotVTrans;
/**
* Initial rotation position (3D model facing the user).
* Note that we rotate the model instead of the camera!
*/
private int rotationX = 100;
private int rotationY = 0;
private FigureLayout backgroundLayout;
private FigureLayout robotLayout;
private Effect3D effect;
public Micro3DCanvas()
{
super(false);
try
{
robotTexture = new Texture("/Texture0.bmp",true);
robotFigure = new Figure("/figure.mbac");
robotFigure.setTexture(robotTexture);
robotAction = new ActionTable("/action.mtra");
}
catch (Exception e)
{
e.printStackTrace();
}
// 3D Init
effect = new Effect3D();
// Set the shading and lights
effect.setShadingType(Effect3D.NORMAL_SHADING);
effect.setLight(backgroundLight);
// Robot Affine Transformation variables --
robotXTrans = new AffineTrans();
robotYTrans = new AffineTrans();
robotZTrans = new AffineTrans();
robotTrans = new AffineTrans();
robotVTrans = new AffineTrans();
// Robot layout
robotLayout = new FigureLayout();
robotLayout.setPerspective(1,4096,512);
robotLayout.setCenter(getWidth()/2, getHeight()/2);
robotXTrans.rotationX(rotationX); // X-axis rotation
robotYTrans.rotationY(rotationY); // Y-axis rotation
robotZTrans.rotationZ(0); // We don't do any z-axis rotation
robotYTrans.mul(robotXTrans);
robotZTrans.mul(robotYTrans);
robotVTrans.setIdentity();
robotVTrans.lookAt(ROBOT_POSITION, ROBOT_LOOK, ROBOT_UP);
robotTrans.mul(robotVTrans, robotZTrans);
robotLayout.setAffineTrans(robotTrans);
Thread t = new Thread(this);
t.start();
}
/**
* Calculates the new position for the 3D models and paints them
* on the 2D display
*/
synchronized void draw(Graphics g)
{
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
try
{
g3d.bind(g);
g3d.renderFigure(robotFigure,0,0,robotLayout,effect);
g3d.flush();
}
catch(Throwable h)
{
h.printStackTrace();
}finally{
g3d.release( g );
}
flushGraphics();
}
private void tick(){
currentFrame += robotAction.getNumFrames(0)/100;
if(currentFrame > robotAction.getNumFrames(0))
{
currentFrame = 0;
}
robotFigure.setPosture(robotAction,0,currentFrame);
}
/**
* Clear all resources
*/
public void clearResource()
{
isRun = false;
if( robotFigure != null )
{
robotFigure.dispose();
robotFigure = null;
}
if( robotTexture != null )
{
robotTexture.dispose();
robotTexture = null;
}
if( robotAction != null )
{
robotAction.dispose();
robotAction = null;
}
}
public void run()
{ Graphics g = getGraphics();
while (isRun)
{
draw(g);
tick();
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -