当前位置: 首页 > 工具软件 > jPCT > 使用案例 >

android jpct ae教程,JPCT-AE for Android 3D (一)----------Hello World创建自己的立方体

蒙华翰
2023-12-01

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

}

}

 类似资料: