扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:朱先忠 编译 来源:天极 2007年9月4日
关键字:
在本页阅读全文(共2页)
列表2. MyCanvas类
import javax.microedition.lcdui.*; import javax.microedition.m3g.*; public class MyCanvas extends Canvas { private Graphics3D graphics3d; private Camera camera; private Light light; private float angle = 0.0f; private Transform transform = new Transform(); private Background background = new Background(); private VertexBuffer vbuffer; private IndexBuffer indexbuffer; private Appearance appearance; private Material material = new Material(); private Image image; public MyCanvas() { // 创建Displayable对象以探听命令事件 setCommandListener(new CommandListener() { public void commandAction(Command c, Displayable d) { if (c.getCommandType() == Command.EXIT) { MIDletMain.quitApp();}} }); try { init();} catch(Exception e) { e.printStackTrace();} } /** * 组件的初始化 */ private void init() throws Exception { addCommand(new Command("Exit", Command.EXIT, 1)); graphics3d = Graphics3D.getInstance(); camera = new Camera(); camera.setPerspective( 60.0f,(float)getWidth()/ (float)getHeight(), 1.0f, 1000.0f ); light = new Light(); light.setColor(0xffffff); light.setIntensity(1.25f); short[] vert = { 5, 5, 5, -5, 5, 5, 5,-5, 5, -5,-5, 5, -5, 5,-5, 5, 5,-5, -5,-5,-5, 5,-5,-5, -5, 5, 5, -5, 5,-5, -5,-5, 5, -5,-5,-5, 5, 5,-5, 5, 5, 5, 5,-5,-5, 5,-5, 5, 5, 5,-5, -5, 5,-5, 5, 5, 5, -5, 5, 5, 5,-5, 5, -5,-5, 5, 5,-5,-5, -5,-5,-5 }; VertexArray vertArray = new VertexArray(vert.length / 3, 3, 2); vertArray.set(0, vert.length/3, vert); //立方体的各个结点法线 byte[] norm = { 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 normArray = new VertexArray(norm.length / 3, 3, 1); normArray.set(0, norm.length/3, norm); //各个结点的纹理坐标 short[] tex = { 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1 }; VertexArray texArray = new VertexArray(tex.length / 2, 2, 2); texArray.set(0, tex.length/2, tex); int[] stripLen = { 4, 4, 4, 4, 4, 4 }; // 对象的VertexBuffer VertexBuffer vb = vbuffer = new VertexBuffer(); vb.setPositions(vertArray, 1.0f, null); vb.setNormals(normArray); vb.setTexCoords(0, texArray, 1.0f, null); indexbuffer = new TriangleStripArray( 0, stripLen ); //纹理图像 image = Image.createImage( "/pic1.png" ); Image2D image2D = new Image2D( Image2D.RGB, image ); Texture2D texture = new Texture2D( image2D ); texture.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST); texture.setWrapping(Texture2D.WRAP_CLAMP,Texture2D.WRAP_CLAMP); texture.setBlending(Texture2D.FUNC_MODULATE); // 创建外观(Appearance)对象 appearance = new Appearance(); appearance.setTexture(0, texture); appearance.setMaterial(material); material.setColor(Material.DIFFUSE, 0xFFFFFFFF); material.setColor(Material.SPECULAR, 0xFFFFFFFF); material.setShininess(100.0f); background.setColor(0xffffcc); } protected void paint(Graphics g) { graphics3d.bindTarget(g, true, Graphics3D.DITHER | Graphics3D.TRUE_COLOR); graphics3d.clear(background); //设置照相机 Transform transform = new Transform(); transform.postTranslate(0.0f, 0.0f, 30.0f); graphics3d.setCamera(camera, transform); //设置灯光 graphics3d.resetLights(); graphics3d.addLight(light, transform); //设置旋转 angle += 1.0f; transform.setIdentity(); transform.postRotate(angle, 1.0f, 1.0f, 1.0f); graphics3d.render(vbuffer, indexbuffer, appearance, transform); graphics3d.releaseTarget(); } } |
图1 正在一个模拟器中运行的应用程序
小结
JSR 184对于可以运行MIDP 2.0的设备来说,是一个节省时间和空间的可选的软件开发包。它允许开发者使用两种图形方式-保留模式和立即模式-来产生3D图形。本文集中讲述了立即模式,并给出一个例子程序来说明怎样使用3D API。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者