1. 程式人生 > >JPCT-AE for Android 3D (一)----------Hello World建立自己的立方體

JPCT-AE for Android 3D (一)----------Hello World建立自己的立方體

CBoxView.java

public class CBoxView extends GLSurfaceView {

	private float xpos = -1;
	private float ypos = -1;
	CBoxRander mRander;
	
	public CBoxView(Context context, AttributeSet attrs) {
		super(context);
		// TODO Auto-generated constructor stub

		// 使用自己實現的 EGLConfigChooser,該實現必須在setRenderer(renderer)之前
		// 如果沒有setEGLConfigChooser方法被呼叫,則預設情況下,檢視將選擇一個與當前android.view.Surface相容至少16位深度緩衝深度EGLConfig。
		setEGLConfigChooser(new EGLConfigChooser() {
			public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
				int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16,
						EGL10.EGL_NONE };
				EGLConfig[] configs = new EGLConfig[1];
				int[] result = new int[1];
				egl.eglChooseConfig(display, attributes, configs, 1, result);
				return configs[0];
			}
		});

		mRander = new CBoxRander( context);
		setRenderer(mRander);
	}
	
	public boolean onTouchEvent(MotionEvent me) {

		if (me.getAction() == MotionEvent.ACTION_DOWN) {
			xpos = me.getX();
			ypos = me.getY();
			
			return true;
		}

		if (me.getAction() == MotionEvent.ACTION_UP) {
			xpos = -1;
			ypos = -1;
			mRander.touchTurn = 0;
			mRander.touchTurnUp = 0;
			
			return true;
		}

		if (me.getAction() == MotionEvent.ACTION_MOVE) {
			float xd = me.getX() - xpos;
			float yd = me.getY() - ypos;

			xpos = me.getX();
			ypos = me.getY();

			mRander.touchTurn = xd / -100f;
			mRander.touchTurnUp = yd / -100f;
			return true;
		}

		try {
			Thread.sleep(15);
		} catch (Exception e) {
			// No need for this...
		}

		return super.onTouchEvent(me);
	}

}
CBoxRander.java
public class CBoxRander implements Renderer {

	public MainActivity mActivity = null;
	private FrameBuffer fb = null;
	private World world = null;
	Camera cam = null;
	// 立方體
	private Object3D cube = null;
	private Object3D yi,shi,zhu,xing,wan,wen;
	// 光照類
	private Light sun = null;
	private RGBColor back = new RGBColor(255, 255, 255);
	// 旋轉
	public float touchTurn = 0;
	public float touchTurnUp = 0;


	public CBoxRander(Context context) {
		mActivity = (MainActivity) context;
	}

	
	@Override
	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
		if (touchTurn != 0) {
			cube.rotateY(touchTurn);
			touchTurn = 0;
		}

		if (touchTurnUp != 0) {
			cube.rotateX(touchTurnUp);
			touchTurnUp = 0;
		}

		fb.clear(back);
		world.renderScene(fb);
		world.draw(fb);
		fb.display();
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int w, int h) {
		// TODO Auto-generated method stub
		if (fb != null) {
			fb.dispose();
		}

		// 建立一個寬度為w,高為h的FrameBuffer
		fb = new FrameBuffer(gl, w, h);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		world = new World();
		world.setAmbientLight(100, 100, 100);

		sun = new Light(world);
		sun.setIntensity(250, 250, 250);

		// 紋理
		Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(mActivity.
getResources().getDrawable(R.drawable.icon)), 64, 64)); TextureManager.getInstance().addTexture("texture", texture); // 立方體 cube = Primitives.getCube(10); cube.calcTextureWrapSpherical(); cube.setTexture("texture"); cube.strip(); cube.build(); world.addObject(cube); // 攝像機 cam = world.getCamera(); cam.moveCamera(Camera.CAMERA_MOVEOUT, 50); cam.lookAt(cube.getTransformedCenter()); SimpleVector sv = new SimpleVector(); sv.set(cube.getTransformedCenter()); sv.y -= 100; sv.z -= 100; sun.setPosition(sv); MemoryHelper.compact(); } }