JPCT-AE for Android(安卓)3D (一)----------Hello World创建自己的立方体
16lz
2021-12-04
了解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;}@Overridepublic void onDrawFrame(GL10 gl) {// TODO Auto-generated method stubif (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();}@Overridepublic void onSurfaceChanged(GL10 gl, int w, int h) {// TODO Auto-generated method stubif (fb != null) {fb.dispose();}// 创建一个宽度为w,高为h的FrameBufferfb = new FrameBuffer(gl, w, h);}@Overridepublic void onSurfaceCreated(GL10 gl, EGLConfig config) {// TODO Auto-generated method stubworld = 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应用开发
- activity
- Android(安卓)Studio 学习之 Android(安卓)SDK快速更新
- 【开源项目3】Android快速开源框架--afinal
- Android(安卓)Support v4、v7、v13的区别和应用场景
- Android热修复技术链接收集
- android TextView中UrlSpan与文本中的超链接冲突问题
- 【Arcgis for android】保存地图截图到sd卡
- Activity-Spinner使用