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

了解JPCT-AE

      JPCT是一款基于OpenGL技术开发的3D图形引擎(PC环境为标准OpenGL,Android为OpenGL ES), 以Java语言为基础的,拥有功能强大的Java 3D解决方案。该引擎与LGame(此为2D游戏引擎)相类似,目前拥有PC(J2SE)以及Android两个开发版本。jPCT-AE 是 jPCT 在 Android 平台上的移植版本。
      网址:http://www.jpct.net/jpct-ae/ 

Hello World

在JPCT-AE安装包中存在已经存在了两个绘制立方体的例子(非别使用了固定流水线和可编程流水线)。但例子中存在部分没有必要的代码,这里我们来创建自己的立方体。
activity_main.xml:


      
    

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();
	}	
}


PS:小弟才开始学JAVA代码风格不好,结构不够优化的地方还请你们包涵了。

你可能感兴趣的:(Android)