j2me 3D 为什么不能显示出旋转的正方体啊??谢谢

lt136022740 2010-10-21 05:52:46
import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Background;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Image2D;
import javax.microedition.m3g.IndexBuffer;
import javax.microedition.m3g.Light;
import javax.microedition.m3g.Material;
import javax.microedition.m3g.PolygonMode;
import javax.microedition.m3g.Texture2D;
import javax.microedition.m3g.Transform;
import javax.microedition.m3g.TriangleStripArray;
import javax.microedition.m3g.VertexArray;
import javax.microedition.m3g.VertexBuffer;


public class Cube extends Canvas {

private Graphics3D g3d;
private Camera camera;
private Light light1;
private Transform tf;
private Image2D iImage2D;
private Image2D bkg;
private Appearance app;
private Image image,image1;
private Background bg;
private VertexBuffer vb;
private IndexBuffer indexBuffer;
private Material material;
private Texture2D texture;
public Cube() {
g3d = Graphics3D.getInstance();
camera = new Camera();
material = new Material();
bg = new Background();
camera.setPerspective(80.0f,(float)getWidth()/(float)getHeight(),1.0f,1000.0f);
light1 = new Light();
light1.setColor(0x00ff00);
light1.setIntensity(1.25f);
short[] position = {
10,10,10, -10,10,10, 10,-10,10, -10,-10,10,
-10,10,-10 , 10,10,-10, -10,-10,-10, 10,-10,-10,
-10,10,10 , -10,10,-10, -10,-10,10, -10,-10,-10,

10,10,-10, 10,10,10, 10,-10,-10, 10,-10,10,
10,10,-10, -10,10,-10, 10,10,10, -10,10,10,
10,-10,10, -10,-10,10, 10,-10,-10, -10,-10,-10,
};
VertexArray vertexArray = new VertexArray(position.length/3,3,2);
vertexArray.set(0,position.length/3,position);
byte[] normal = {
0,0,127, 0,0,127, 0,0,127, 0,0,127,
0,0,-127, 0,0,-127, 0,0,-127, 0,0,-127,
-127,0,0, -127,0,0, -127,0,0, -127,0,0,
127,0,0, 127,0,0, 127,0,0, 127,0,0,
0,127,0, 0,127,0, 0,127,0, 0,127,0,
0,-127,0, 0,-127,0, 0,-127,0, 0,-127,0,
};
VertexArray normalArray = new VertexArray(normal.length/3,3,1);
normalArray.set(0,normal.length/3,normal);
short tex[] = {96,32, 64,32, 96,64, 64,64,
64,32, 32,32, 64,64, 32,64,
64,0, 32,0, 64,32, 32,32,
32,0, 0,0, 32,32, 0,32,
32,32, 0,32, 32,64, 0,64,
96,0, 64,0, 96,32, 64,42,
};
VertexArray texArray = new VertexArray(tex.length/2,2,2);
texArray.set(0,tex.length/2,tex);
int []stripLen = {4,4,4,4,4,4};
vb = new VertexBuffer();
vb.setPositions(vertexArray,1.0f,null);
vb.setNormals(normalArray);
vb.setTexCoords(0, texArray, (1.0f/128.0f),null);
indexBuffer = new TriangleStripArray(0,stripLen);
try {
image = Image.createImage("/05.png");
} catch (IOException e1) {
e1.printStackTrace();
}
iImage2D = new Image2D(Image2D.RGB,image);
texture = new Texture2D(iImage2D);
texture.setFiltering(Texture2D.FILTER_NEAREST,Texture2D.FILTER_NEAREST);
texture.setWrapping(Texture2D.WRAP_CLAMP,Texture2D.WRAP_CLAMP);
texture.setBlending(Texture2D.FUNC_MODULATE);
try {
image1 = Image.createImage("/05.png");
} catch (IOException e) {
e.printStackTrace();
}
bkg = new Image2D(Image2D.RGB,image1);
app = new Appearance();
app.setTexture(0, texture);
app.setMaterial(material);
material.setVertexColorTrackingEnable(true);
material.setColor(Material.SPECULAR,0xFFFFFFFF);
material.setShininess(100.0f);
PolygonMode p = new PolygonMode();
p.setPerspectiveCorrectionEnable(true);
p.setCulling(PolygonMode.CULL_BACK);
p.setShading(PolygonMode.SHADE_SMOOTH);
p.setTwoSidedLightingEnable(true);
p.setLocalCameraLightingEnable(false);
app.setPolygonMode(p);
bg.setImage(bkg);
tf = new Transform();
tf.postTranslate(0.0f,0.0f,50.0f);
g3d.setCamera(camera,tf);
g3d.resetLights();
g3d.addLight(light1, tf);
}

public void run(){
while(true){
repaint();
this.serviceRepaints();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

protected void paint(Graphics g) {
g3d.bindTarget(g,true,Graphics3D.DITHER|Graphics3D.TRUE_COLOR);
g3d.clear(bg);
tf.postRotate(3.0f,1.0f,1.0f,1.0f);
g3d.render(vb,indexBuffer,app,tf);
}

}
...全文
67 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zeng5261435 2010-10-28
  • 打赏
  • 举报
回复
有什么错误提示么 或者只显示什么

13,097

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧