m3gcanvas.java

来自「3D手机游戏开发实例源代码」· Java 代码 · 共 51 行

JAVA
51
字号


import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import javax.microedition.lcdui.game.*;

class M3GCanvas  extends GameCanvas
{
  // Choose another Camera25,26
	//static final int CAMERA_ID = 25;
	 static final int CAMERA_ID = 25;
	private Camera camera = null;	
	Graphics3D g3d = Graphics3D.getInstance();
	World myWorld = null;
  public M3GCanvas(){
    super(false);
    setFullScreenMode(false);

   			
		try {
			// Load the world from the m3g file							
			myWorld = (World) Loader.load("/teapot.m3g")[0];
    	camera = (Camera) myWorld.find(CAMERA_ID);
    	float viewport_width = getWidth();
	    float viewport_height = getHeight();
	    float aspectRatio = (float)viewport_width/(float)viewport_height;
	  	camera.setPerspective(45, aspectRatio,1.0f,2000.0f); 
  		myWorld.setActiveCamera(camera);	

		} catch (Exception e) {
			e.printStackTrace();
		}
		paint();

    }
   public void paint() {
        Graphics g2d = getGraphics();
	       try{
            g3d.bindTarget(g2d); // Binds the given Graphics or mutable Image2D as the rendering target of this Graphics3D
            g3d.clear(null);
            g3d.render(myWorld);
    }finally{
            g3d.releaseTarget();
        }
            flushGraphics();
    }
  }



 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?