精通Micro3D v3基础技术

作者:numenzq 文章来源:http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=1097

现在将带领你使用Mascot Capsule Micro 3D v3进行3D开发,这里有十个简单的例子将一步一步的向你介绍你必须掌握的基本技术。所有例子都基于同一个核心代码去展示一个简单的3D模型。下面是这些例子的基本组织和内容:

 

1 简单的显示这是3D模型

2 按下数字键‘2’’ 8’ ’ 4’ ’ 6’ )实现3D模型上下左右移动

3 按下数字键’ 7’ ’ 9’ 实现3D模型的缩放效果(注意:缩放模型不是让该模型在Z轴上移动)

4 按下”Light”键实现在3D场景中添加灯光效果

5 按下”Perspective”键实现3D模型在平行投影和透视投影之间切换

6 使用手机的方向键实现3D模型的旋转

7 3D模型添加动画效果

8 添加两个动画,使用数字键’ 1’ 进行切换

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类是用来处理所有变换的,例如:移动和旋转。程序在XY轴上移动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);
...
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值