术语汇编 J2ME 3D技术简介(1)(2)
◆下面是一个最简单的M3G程序,来自Sony-Ericsson的示例代码,它创建一个旋转的金字塔,可以从此处下载完整代码并在WTK2.2中运行。 首先,我们要获得唯一
◆下面是一个最简单的M3G程序,来自Sony-Ericsson的示例代码,它创建一个旋转的金字塔,可以从此处下载完整代码并在WTK2.2中运行。
首先,我们要获得唯一的Graphics3D实例,用于渲染3D场景。Graphics3D是一个Singleton实现,可以在任何地方获得:
- g3d=Graphics3D.getInstance();
然后,在Canvas中渲染:
- publicclassMyCanvasextendsCanvas
- {
- publicvoidpaint(Graphicsg){
- try{
- g3d.bindTarget(g);
- ...updatethescene...
- ...renderthescene...
- }finally{
- g3d.releaseTarget();
- }
- }
接下来创建一个World并设置Camera:
- world=newWorld();
- camera=newCamera();
- world.addChild(camera);
- //Thewidthandheightofthecanvas.
- floatw=getWidth();
- floath=getHeight();
- //Constructsaperspectiveprojectionmatri
- xandsetsthatasthecurrentprojectionmatrix.
- camera.setPerspective(60.0f,w/h,0.1f,50f);
- world.setActiveCamera(camera);
接着,在createPyramid()方法中创建一个Mesh,代表金字塔,并添加到World中:
- privateMeshpyramidMesh;//thepyramidinthescene
- pyramidMesh=createPyramid();//createourpyramid.
- pyramidMesh.setTranslation(0.0f,0.0f,-3.0f);
- //movethepyramid3unitsintothescreen.
- world.addChild(pyramidMesh);//addthepyramidtotheworld
最后,在一个线程中让金字塔绕Y轴旋转起来:
- publicvoidrun(){
- Graphicsg=getGraphics();
- while(true){
- //rotatethepyramid1degreearoundtheY-axis.
- pyramidMesh.postRotate(3.0f,0.0f,1.0f,0.0f);
- draw3D(g);
- flushGraphics();
- }
- }
精彩图集
精彩文章