`
luliangok
  • 浏览: 782669 次
文章分类
社区版块
存档分类
最新评论

Doja平台3D基础示例

 
阅读更多
Doja平台3D基础示例
这个示例在前面示例的基础上进行了改进,实现了doja平台上3D常见的操作,比如偏移,缩放,旋转,光照等效果,不过由于没有使用纹理,光照效果不明显~~。 飘飘白云
import com.nttdocomo.ui.*;
import com.nttdocomo.opt.ui.j3d.*;
//
/**
* @author ppby 2006-01-25
* Doja3D foundation
*/
public class Doja3D extends IApplication {
private Canvas3D canv;
public void start() {
try {
canv = new Canvas3D();
Display.setCurrent(canv);
Thread thread = new Thread(canv);
thread.start();
} catch (IllegalThreadStateException itse) {
itse.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Canvas class
*/
class Canvas3D extends Canvas implements Runnable {
// Command list array
private int[] cmdListFunc_;
private int[] cmdListPrim_;
// 3D affine transformation matrix
private AffineTrans viewT = new AffineTrans();
private AffineTrans affineT = new AffineTrans();
private AffineTrans rotateT = new AffineTrans();
private AffineTrans scaleT = new AffineTrans();
private AffineTrans translateT = new AffineTrans();
private int rotX, rotY; // Variable for rotation
private int nScale; // Variable for scale
private int nTransX,nTransY;//Variable for translation
private Vector3D vLightDirection ;//direction of directing light
private boolean bAmbientLight = false;//enable ambient light flag
private int nLight; //intensity of ambient light
public Canvas3D() {
// Set a camera:(eye position,look at position,up direction)
viewT.lookAt(
new Vector3D(0, 0, 800),
new Vector3D(0, 0, 0),
new Vector3D(0, 4096, 0)
);
vLightDirection = new Vector3D(4096/4, 4096/4, 4096);//direction of Light
// Set a command list
setupCmdListFunc();
setupCmdListPrim();
}
public void run() {
while (true) {
UpdateAffineTrans();
repaint();
try {
Thread.sleep(100); // wait 100ms
} catch (InterruptedException ie) {
}
}
}
public void UpdateAffineTrans()
{
affineT.setIdentity();
/*
* Translate
*/
translateT.setIdentity();
translateT.m03 += nTransX;
translateT.m13 += nTransY;
affineT.mul(translateT);
/*
* Rotate
*/
//rotate y axis
rotateT.setIdentity();
rotateT.setRotateY(rotY);
affineT.mul(rotateT);
//rotate x axis
rotateT.setIdentity();
rotateT.setRotateX(rotX);
affineT.mul(rotateT,affineT);//same as affineT.mul(rotateT);
/*
* Scale:What will happen if scaleT.mXX < 0?~~
*/
scaleT.setIdentity();
scaleT.setElement(0, 0, scaleT.m00 + nScale);
scaleT.setElement(1, 1, scaleT.m11 + nScale);
//The follow two lines have the same effect.
// scaleT.m00 += nScale;
// scaleT.m11 += nScale;
affineT.mul(scaleT,affineT);
}
/**
* Rendering method.
*/
public void paint(Graphics g) {
if (g == null)
return;
try {
g.lock();
// Background Clear
g.setColor(Graphics.getColorOfRGB(0, 0, 0));
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Graphics.getColorOfRGB(150, 150, 120));
Graphics3D g3d = (Graphics3D) g;
//Method 1
g3d.setClipRect3D(0, 0, getWidth(),getHeight());
g3d.setScreenCenter(getWidth()>>1, getHeight()>>1);
g3d.setPerspective(200, 10000, 4096*50/360);
//Method 2:The effect is the same s method 1.
//g3d.executeCommandList(cmdListFunc_); // Excute the command list
//light: The lighting effect is not obvious in this program.
//If i use texture ,it maybe obviously to see lighting effect.
g3d.setDirectionLight(vLightDirection, nLight );
if( bAmbientLight )
g3d.setAmbientLight(nLight);
g3d.enableLight(true);
//set viewport
affineT.mul(viewT,affineT);
g3d.setViewTrans(affineT); // Set view coordinate matrix
setupCmdListPrim();
g3d.executeCommandList(cmdListPrim_); // Excute a command list
//g3d.flush();//It has been done when executeCommandList.
g.setColor(Graphics.getColorOfRGB(0, 255, 0xff));
g.drawString("Rotate:up,down,left,right key.", 5, Font.getDefaultFont().getHeight());
g.drawString("Scale :KEY_1,KEY_3", 5, 5+2*Font.getDefaultFont().getHeight());
g.drawString("Translate :KEY_4,6,8,2", 5, 10+3*Font.getDefaultFont().getHeight());
g.drawString("Light :KEY_0,7,9", 5, 15+4*Font.getDefaultFont().getHeight());
g.unlock(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Key event process.
*/
public void processEvent(int type, int param) {
try {
if (type == Display.KEY_PRESSED_EVENT) {
switch (param) {
//rotate
case Display.KEY_UP:
rotX -= 128;
rotX %= 4096;
break;
case Display.KEY_DOWN:
rotX += 128;
rotX %= 4096;
break;
case Display.KEY_LEFT:
rotY -= 128;
rotY %= 4096;
break;
case Display.KEY_RIGHT:
rotY += 128;
rotY %= 4096;
break;
//scale
case Display.KEY_1:
nScale += 128;
break;
case Display.KEY_3:
nScale -= 128;
break;
//translate
case Display.KEY_4:
nTransX -= 15;
break;
case Display.KEY_6:
nTransX += 15;
break;
case Display.KEY_8:
nTransY -= 15;
break;
case Display.KEY_2:
nTransY += 15;
break;
//light
case Display.KEY_0:
bAmbientLight = !bAmbientLight;
break;
case Display.KEY_7:
nLight -= 512;
nLight %= 4096;
nLight = (nLight<0?-nLight:nLight);
break;
case Display.KEY_9:
nLight += 512;
nLight %= 4096;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Set the command list for methods.
* @return true - success. false - fail.
*/
private boolean setupCmdListFunc() {
try {
int[] cmdListFunc = {
Graphics3D.COMMAND_LIST_VERSION_1,
Graphics3D.COMMAND_CLIP_RECT, 0, 0, getWidth(),
getHeight(), Graphics3D.COMMAND_SCREEN_CENTER,
(getWidth() >> 1), (getHeight() >> 1),
Graphics3D.COMMAND_PERSPECTIVE1, 200, 10000,
4096 * 50 / 360, Graphics3D.COMMAND_END };
cmdListFunc_ = cmdListFunc;
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* Set the command list for primitives.
* @return true - Setting succeeded. false - Setting failed.
*/
private boolean setupCmdListPrim() {
try {
//draw four trangles.
int[] cmdListPrim = {
Graphics3D.COMMAND_LIST_VERSION_1,
// TRIANGLES
(Graphics3D.COMMAND_RENDER_TRIANGLES
| Graphics3D.COLOR_PER_FACE | (4 << 16)),//trangle NUM << 16
//each trangle's three vertexes.vertex:(x,y,z)
0, 120, 0, -120, -120, 120, 120, -120, 120,
0, 120, 0, 120, -120, 120, 120, -120, -120,
0, 120, 0, 120, -120, -120, -120, -120, -120,
0, 120, 0, -120, -120, -120, -120, -120, 120,
//each trangle's color
0xff << 16, //red
0xff << 8, //green
0xff << 4, //blue
0xff << 8 | 0xff << 16, //yellow
// flush
Graphics3D.COMMAND_FLUSH, Graphics3D.COMMAND_END };
cmdListPrim_ = cmdListPrim;
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
分享到:
评论

相关推荐

    Doja SDK 5.1 en

    emufordoja5_1_en_090501 ...基于i-mode 的Java是由Sun Micorsystems专门为消费电子和嵌入式设备开发的Java 平台J2ME CLDC, 以及i-mode扩展库(基于i-mode的Java应用概要)组成,i-mode扩展库包括用户界面和HTTP通讯。

    InputStreamReader_test.rar_doja_doja中文_简体 繁体

    doja中文简体中文与繁体中文转换例子(doja2.5开发环境)

    DoJa-1942:使用 iAppli SDK 1.5 从 J2ME 1942 移植到 DoJa(2005 年 5 月)

    总结 我将我的 J2ME 1942 克隆移植到 i-mode (DoJa) 平台 运行 JAR 的要求 Java 2 运行时环境 DoJa 1.5 模拟器版1.07 可以从 DoJa 开发者网络下载: 如何玩演示 可分发文件位于“dist”文件夹中 箭头(或 8/2、4/6...

    DoJa Trivia Quiz-开源

    适用于支持 DoJa 2.5 的手机的琐事问答游戏。

    Doja Cat New Tab Wallpapers New Tab Theme-crx插件

    在每个新的标签页上包括Yoru最喜欢的Regper Doja Cat的高清图像! 对于RAP和HIP-HOP的粉丝! 请评价我们! 如果您觉得我们不值得五星级评分,请发电子邮件或首先致电我们! 我们希望收到你的来信! support@...

    java XML sax parser (2kb)-开源

    这是一个为所有 Java 设备制作的小型 SAX 解析器。 它可以从非常小的设备(从 Midp 到 DoJa 的典型 java 电话)到企业服务器使用。 现在,它仅支持 SAX 解析器模型。 使用非常简单(请参阅 .java 示例)。 提示是

    JammedUp-开源

    JammedUp(JAM和JAD Update的缩写)是一个自动化的MIDP和DoJa描述符文件同步器。 MIDlet分为JAR可执行文件和描述符文件,这是下载前对JAR文件内容的简短但准确的描述。

Global site tag (gtag.js) - Google Analytics