龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

术语汇编 J2ME 3D技术简介(1)(2)

时间:2013-03-06 14:58来源:未知 作者:admin 点击:
分享到:
◆下面是一个最简单的M3G程序,来自Sony-Ericsson的示例代码,它创建一个旋转的金字塔,可以从此处下载完整代码并在WTK2.2中运行。 首先,我们要获得唯一

◆下面是一个最简单的M3G程序,来自Sony-Ericsson的示例代码,它创建一个旋转的金字塔,可以从此处下载完整代码并在WTK2.2中运行。

首先,我们要获得唯一的Graphics3D实例,用于渲染3D场景。Graphics3D是一个Singleton实现,可以在任何地方获得:

  1. g3d=Graphics3D.getInstance();  
  2.  

然后,在Canvas中渲染:

  1. publicclassMyCanvasextendsCanvas  
  2. {  
  3. publicvoidpaint(Graphicsg){  
  4. try{  
  5. g3d.bindTarget(g);  
  6. ...updatethescene...  
  7. ...renderthescene...  
  8. }finally{  
  9. g3d.releaseTarget();  
  10. }  
  11. }  
  12.  

接下来创建一个World并设置Camera:

  1. world=newWorld();  
  2. camera=newCamera();  
  3. world.addChild(camera);  
  4. //Thewidthandheightofthecanvas.  
  5. floatw=getWidth();  
  6. floath=getHeight();  
  7. //Constructsaperspectiveprojectionmatri
  8. xandsetsthatasthecurrentprojectionmatrix.  
  9. camera.setPerspective(60.0f,w/h,0.1f,50f);  
  10. world.setActiveCamera(camera);  
  11.  

接着,在createPyramid()方法中创建一个Mesh,代表金字塔,并添加到World中:

  1. privateMeshpyramidMesh;//thepyramidinthescene  
  2.  
  3. pyramidMesh=createPyramid();//createourpyramid.  
  4. pyramidMesh.setTranslation(0.0f,0.0f,-3.0f);  
  5.  
  6. //movethepyramid3unitsintothescreen.  
  7. world.addChild(pyramidMesh);//addthepyramidtotheworld  

最后,在一个线程中让金字塔绕Y轴旋转起来: 

  1. publicvoidrun(){  
  2. Graphicsg=getGraphics();  
  3. while(true){  
  4. //rotatethepyramid1degreearoundtheY-axis.  
  5. pyramidMesh.postRotate(3.0f,0.0f,1.0f,0.0f);  
  6. draw3D(g);  
  7. flushGraphics();  
  8. }  

精彩图集

赞助商链接