原文地址鏈接
現在將帶領你使用Mascot Capsule Micro 3D v3進行3D開發,這里有十個簡單的例子將一步一步的向你介紹你必須掌握的基本技術。所有例子都基于同一個核心代碼去展示一個簡單的3D模型。下面是這些例子的基本組織和內容:
例1 簡單的顯示這是3D模型
例2 按下數字鍵‘2’,’
例3 按下數字鍵’
例4 按下”Light”鍵實現在3D場景中添加燈光效果
例5 按下”Perspective”鍵實現3D模型在平行投影和透視投影之間切換
例6 使用手機的方向鍵實現3D模型的旋轉
例7 給3D模型添加動畫效果
例8 添加兩個動畫,使用數字鍵’
例9 顯示多個3D模型
例10 顯示如何在屏幕上繪制一個原始模型
所有的例子可以通過下面的鏈接下載:
下載源代碼
注意:這些程序是使用的Micro3D技術,但并不是很好的編碼實踐。例如,程序并沒有編碼實現處理中斷退出該程序時正常的軟鍵,只得一直按住”back”鍵來退出程序
大多數代碼都很容易理解,并不需要更多的解釋,不過下面還是有些要點需要論述。
例1 簡單的顯示這是3D模型
下面幾行是基本的3D模型和紋理的導入和設置:
figure = new Figure("/example/DemoMIDP/test_model_robo.mbac");
mainTexture = new Texture("/example/DemoMIDP/tex_001.bmp", true);
figure.setTexture(mainTexture);
下面是在Canvas里繪制3D世界
PRivate Graphics3D g3 = new Graphics3D();
protected void paint(Graphics g) {
...
g3.bind(g);
g3.renderFigure(figure, 0, 0, layout, effect);
//Flush to screen
g3.flush();
//Release the Graphics 3D object
g3.release(g);
}
例2 移動模型
AffineTrans類是用來處理所有變換的,例如:移動和旋轉。程序在X和Y軸上移動3D模型只需要改變AffineTrans矩陣的兩個變量。
affineTrans.m03 += moveX;
affineTrans.m13 += moveY;
例3 縮放模型
要實現3D模型的縮放,我們應該先用比例因數創建一個矩陣,然后添加這個矩陣到AffineTrans矩陣中。
AffineTrans scaleTrans = new AffineTrans();
scaleTrans.set(scaleX,0,0,0,0,scaleY,0,0,0,0,scaleZ,0);
// Scaling the model
affineTrans.mul(scaleTrans);
例4 添加燈光
燈光非常容易設置。一個方向向量和一個亮度值就足夠了
private Vector3D dir = new Vector3D(-3511, 731, 878); // Light vector
private final int dirIntensity = 4096; // Light intensity
private final int ambIntensity = 1755; // Ambient light intensity
...
light = new Light(dir,dirIntensity,ambIntensity);
effect = new Effect3D( light, Effect3D.NORMAL_SHADING, true, null);
g3.renderFigure(figure, 0, 0, layout, effect);
...
例5 投影
你可以3D模型上使用透視或平行投影。通過簡單的調用實現兩者間轉換。
// Camera distance
private final static int persNear = 1; // Minimum distance to the camera
private final static int persFar = 4096; // Maximum distance to the camera
private final static int persAngle = 682; // Angle
...
//Setting the projection method
if(persEnabled){
layout.setPerspective(persNear, persFar, persAngle);
}else{
layout.setParallelSize(800, 800);
}
例6 旋轉模型
旋轉3D模型與例3中的縮放模型是用的同樣的技術。你創建一個AffineTrans對象來控制你的旋轉數據,并把它的矩陣添加到模型的主AffineTrans里。
// Rotation value
public final static int SPIN_X_PLUS = 100; // Increase or decrease value of the rotation around X axis
public final static int SPIN_Y_PLUS = 100; // Increase or decrease value of the rotation around Y axis
private static int spinX = 0; // X axis rotation value
private static int spinY = 0; // Y axis rotation value
...
kc = getGameAction(kc);
switch (kc) {
case Canvas.UP: // roll up
setSpinX(-SPIN_X_PLUS);
break;
case Canvas.DOWN: // roll down
setSpinX(SPIN_X_PLUS);
break;
case Canvas.LEFT: // roll left
setSpinY(-SPIN_Y_PLUS);
break;
case Canvas.RIGHT: // roll right
setSpinY(SPIN_Y_PLUS);
break;
default:
break;
}
...
AffineTrans rotTrans = new AffineTrans();
//X roll
rotTrans.setIdentity();
rotTrans.setRotationX(spinX);
affineTrans.mul(rotTrans);
//Y roll
rotTrans.setIdentity();
rotTrans setRotationY(spinY);
affineTrans.mul(rotTrans);
例7 模型中的動畫效果
這個例子為你演示如何導入.mtra文件里的動畫數據并應用于你的3D模型。
action = new ActionTable("/example/DemoMIDP/action_01.mtra");
...
frame += action.getNumFrames(0)/10;
if( frame >= action.getNumFrames(0) ){
frame = 0;
}
figure.setPosture(action, 0, frame);
g3.renderFigure(figure, 0, 0, layout, effect);
例8 模型中的多個動畫效果
使用兩個不同的動畫文件比不比使用一個動畫文件難多少。僅僅是導入兩個文件并在每次3D模型繪制時選擇其中一個。
action[0] = new ActionTable("/example/DemoMIDP/action_01.mtra");
action[1] = new ActionTable("/example/DemoMIDP/action_02.mtra");
...
case Canvas.KEY_NUM1: // action
actNo = 1;
frame = 0;
break;
...
frame += action[actNo].getNumFrames(0)/10;
if( frame >= action[actNo].getNumFrames(0) ){
frame = 0;
actNo = 0;
}
figure.setPosture(action[actNo], 0, frame);
g3.renderFigure(figure, 0, 0, layout, effect);
例9 顯示多個3D模型
使用多個3D模型同在一個模型中使用多個動畫一樣的簡單。從新從一個新的3D模型.mbac文件去創建一個新的figure實例與創建第一個實例的方法相同。
// One Figure created from a mbac file...
figure = new Figure("/example/DemoMIDP/test_model_robo.mbac");
mainTexture = new Texture("/example/DemoMIDP/tex_001.bmp", true);
figure.setTexture(mainTexture);
//... And another Figure created from another mbac file.
figureBg = new Figure("/example/DemoMIDP/test_model_haikei.mbac");
例10 用原型繪制
即使Micro3D v3主要是使用預先建立的模型進行3D建模編程,你也可以直接從原始的數組命令來創建3D圖形。
// Use this array of commands to show a triangle with texture....
static int[] command = {
Graphics3D.COMMAND_LIST_VERSION_1_0,
Graphics3D.PRIMITVE_TRIANGLES
Graphics3D.PDATA_NORMAL_PER_FACE
Graphics3D.PDATA_TEXURE_COORD
Graphics3D.PATTR_LIGHTING
Graphics3D.PATTR_SPHERE_MAP
Graphics3D.PATTR_BLEND_HALF
(1<<16), // Nbr of primitives, in this case just one triangle
0, 0, 0, // The triangle's ccordinates
200, 0, 0,
0, 200, 0,
0, 0, 4096, // The Normal
0,255,255,255, 0, 0, // The coordinates for the texture
Graphics3D.COMMAND_END, };
...
protected void paint(Graphics g) {
...
g3.drawCommandList( mainTexture, 0, 0, layout, effect, command);
...
}
(出處:http://www.49028c.com)
新聞熱點
疑難解答