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

Doja平台上实现多个纹理

 
阅读更多
Doja平台上实现多个纹理
作者:飘飘白云
这个程序在imode平台上实现了原生模型上使用多个纹理的效果,这一次光照效果比较明显。程序中使用的图片image2.bmp来自j2medev网站。
源程序和资源下载:点击这里
import com.nttdocomo.ui.*;
import com.nttdocomo.opt.ui.j3d.*;
import javax.microedition.io.*;
//
/**
* @author ppby 2006-01-26
* Doja3D texture effect on primitive object
*/
public class DojaTexture 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 {
//quad vertexex array
private int[] quadV =
{
-120,-120, 120, 120, -120, 120, 120, 120, 120, -120, 120, 120, // front
120, -120, -120, -120, -120, -120,-120, 120, -120, 120, 120, -120, // back
-120, -120, -120, -120, -120, 120, -120, 120, 120, -120, 120, -120, // left
120, -120, 120, 120, -120, -120, 120, 120, -120, 120, 120, 120, // right
-120, 120, 120, 120, 120, 120, 120, 120, -120, -120, 120, -120, // top
120, -120, -120, -120, -120, -120,-120, -120, 120, 120, -120, 120, // bottom
};
//normals array for each vertex of quad
private int[] quadN =
{
0, 0, 4096, 0, 0, 4096, 0, 0, 4096, 0, 0, 4096,
0, 0, -4096, 0, 0, -4096, 0, 0, -4096, 0, 0, -4096,
-4096, 0, 0, -4096, 0, 0, -4096, 0, 0, -4096, 0, 0,
4096, 0, 0, 4096, 0, 0, 4096, 0, 0, 4096, 0, 0,
0, 4096, 0, 0, 4096, 0, 0, 4096, 0, 0, 4096, 0,
0, -4096, 0, 0, -4096, 0, 0, -4096, 0, 0, -4096, 0,
};
//texture array for each vertex of quad primitive
// One image containing two textures of 96x96, the whole image is 192x96
// the values given is the pixel values of the image.
private int[] quadT =
{
96, 96, 192, 96, 192, 0, 96, 0,
0, 96, 96, 96, 96, 0, 0, 0,
0, 96, 96, 96, 95, 0, 0, 0,
0, 96, 96, 96, 95, 0, 0, 0,
0, 96, 96, 96, 95, 0, 0, 0,
0, 96, 96, 96, 95, 0, 0, 0,
};
//color array for each vertex of quad primitive
private int[] quadC =
{
0xff << 16, //red
0xff << 8, //green
0xff << 4, //blue
0xff << 8 | 0xff << 16, //yellow
0xff << 16 | 0xff << 4,
0xff << 8 | 0xff << 4,
};
//quad primitive array
private PrimitiveArray quadPrim = null;
Graphics g = null;
// 3D affine transformation matrix
private AffineTrans viewT = new AffineTrans();
private AffineTrans affineT = new AffineTrans();
private AffineTrans rotateT = new AffineTrans();
private int rotX, rotY; // Variable for rotation
private int nAmLight = 2048 ;//ambient light intensity
private Texture quadTexture; // texture to use for the primitive figure.
public Canvas3D() {
g = this.getGraphics();
try {
// Set a camera:(eye position,look at position,up direction)
viewT.lookAt(
new Vector3D(0, 0, 800),
new Vector3D(0, 0, 200),
new Vector3D(0, 4096, 0)
);
//setup quad primitive data
setupPrim();
// load the texture to use for the primitive.
quadTexture = new Texture(Connector.openDataInputStream("resource:///image2.bmp"), false);
} catch (Exception e) {
System.out.println("Init data failed");
e.printStackTrace();
}
}
public void run() {
while (true) {
updateAffineTrans();
render();
try {
Thread.sleep(100); // wait 100ms
} catch (InterruptedException ie) {
System.out.println("Run error~~");
}
}
}
public void setupPrim()
{
quadPrim = new PrimitiveArray(
Graphics3D.PRIMITIVE_QUADS,
Graphics3D.NORMAL_PER_VERTEX
|Graphics3D.TEXTURE_COORD_PER_VERTEX
|Graphics3D.COLOR_NONE //no color for each face
|Graphics3D.ATTR_LIGHT, //enable light
6
);
//set quad primitive data
int[] primVer = quadPrim.getVertexArray();
int[] primTex = quadPrim.getTextureCoordArray();
int[] primNor = quadPrim.getNormalArray();
arrayCopy(primVer,quadV);
arrayCopy(primNor,quadN);
arrayCopy(primTex,quadT);
System.out.println("ver " + primVer.length + " tex " + primTex.length + " Nor " + primNor.length);
}
public void updateAffineTrans()
{
affineT.setIdentity();
//rotate y axis
rotateT.setIdentity();
rotateT.setRotateY(rotY);
affineT.mul(rotateT);
//rotate x axis
rotateT.setIdentity();
rotateT.setRotateX(rotX);
affineT.mul(rotateT,affineT);
}
public void arrayCopy(int[] dist,int[] src)
{
for(int i = 0;i < dist.length;i++)
{
dist[i] = src[i];
}
}
public void render()
{
try {
g.lock();
// Background Clear
g.setColor(Graphics.getColorOfRGB(0, 0, 0));
g.fillRect(0, 0, getWidth(), getHeight());
Graphics3D g3d = (Graphics3D) g;
g3d.setClipRect3D(0, 0, getWidth(),getHeight());
g3d.setScreenScale(4096/2, 4096/2);
g3d.setScreenCenter(getWidth()>>1, getHeight()>>1);
g3d.setPerspective(200, 10000, 4096*50/360);
//set light
g3d.setAmbientLight(nAmLight);
g3d.setDirectionLight(new Vector3D(1,1,0),4096/2);
g3d.enableLight(true);
//set viewport
affineT.mul(viewT,affineT);
g3d.setViewTrans(affineT); // Set view coordinate matrix
//set texture for quad primitive
g3d.setPrimitiveTextureArray(quadTexture);
g3d.setPrimitiveTexture(0);
//render quad
g3d.renderPrimitives(quadPrim, 0);
g3d.flush();
g.setColor(Graphics.getColorOfRGB(0, 255, 0xff));
g.drawString("Rotate:up,down,left,right key.", 5, 5+ Font.getDefaultFont().getHeight());
g.drawString("Light :KEY_7,KEY_9.", 5, 10+2*Font.getDefaultFont().getHeight());
g.unlock(true);
} catch (Exception e) {
System.out.println("Paint() err");
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;
//light
case Display.KEY_7:
nAmLight -= 512;
nAmLight %= 4096;
nAmLight = (nAmLight<0? 1:nAmLight);
break;
case Display.KEY_9:
nAmLight += 512;
nAmLight = (nAmLight>=4096 ? 4095:nAmLight);
nAmLight %= 4096;
break;
}
}
} catch (Exception e) {
System.out.println("Set attribute value err");
e.printStackTrace();
}
}
public void paint(Graphics _g) {
}
}
分享到:
评论

相关推荐

    Doja SDK 5.1 en

    DoJa是NTT DoCoMo用于其i-mode服务的Java应用概要,DoJa运行在J2ME CLDC API上,该API是Java Community Process(JCP)定义的,DoJa扩展库是由NTT DoCoMo制定的,用于通讯和其他输入输出处理、用户界面(GUI)和其他...

    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@...

    JammedUp-开源

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

    java XML sax parser (2kb)-开源

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

Global site tag (gtag.js) - Google Analytics