您的位置:首页 > 移动开发 > Android开发

jpct_ae 安卓创建3d视图效果 jpct的基本使用

2016-09-28 14:07 399 查看
是一款基于OpenGL技术开发的3D图形引擎(PC环境为标准OpenGL,Android为OpenGL ES), 以Java语言为基础的,拥有功能强大的Java 3D解决方案。该引擎与LGame(此为2D游戏引擎)相类似,目前拥有PC(J2SE)以及Android两个开发版本。

这里简单介绍下jpct的基本用法

首先看一下activity里面的代码

 GLSurfaceView mGLSurfaceView;  

 public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

// 这是和一般的opengl一样,定义一个glview  

        mGLSurfaceView = new GLSurfaceView(this); 

  // 这是和一般的opengl一样,定义一个渲染类mGlRenderer  

        mGlRenderer = new GlRenderer();  

        mGLSurfaceView.setRenderer(mGlRenderer);  

        setContentView(mGLSurfaceView);  

}

//这里记得要绑定生命周期
4000

@Override  

    protected void onPause() {  

        // TODO Auto-generated method stub  

        Logger.log("onPause");  

        super.onPause();  

        mGLSurfaceView.onPause();  

    }  

      

      

    @Override  

    protected void onResume() {  

        // TODO Auto-generated method stub  

        Logger.log("onResume");  

        super.onResume();  

        mGLSurfaceView.onResume();  

    }  

//touch事件来控制3d模块的翻转

  @Override  

    public boolean onTouchEvent(MotionEvent me) {  

        // TODO Auto-generated method stub  

  

        // 按键开始    

        if (me.getAction() == MotionEvent.ACTION_DOWN) {    

            // 保存按下的初始x,y位置于xpos,ypos中    

            xpos = me.getX();    

            ypos = me.getY();    

            return true;    

        }    

        // 按键结束    

        if (me.getAction() == MotionEvent.ACTION_UP) {    

            // 设置x,y及旋转角度为初始值    

            xpos = -1;    

            ypos = -1;    

            rotateX = 0;    

            rotateY = 0;    

            return true;    

        }    

    

        if (me.getAction() == MotionEvent.ACTION_MOVE) {    

            // 计算x,y偏移位置及x,y轴上的旋转角度    

            float xd = me.getX() - xpos;    

            float yd = me.getY() - ypos;   

              

            xpos = me.getX();    

            ypos = me.getY();    

              

            rotateX = xd / -100f;    

            rotateY = yd / -100f;    

            return true;    

        }    

    

        // 每Move一下休眠毫秒    

        try {    

            Thread.sleep(15);    

        } catch (Exception e) {    

            // No need for this...    

        }    

        return super.onTouchEvent(me);  

    } 

//GlRenderer 类代码

public class GlRenderer implements Renderer {  

  

        // 停止旗语  

        private boolean stop = false;  

  

        // 停止方法  

        public void setStop() {  

            stop = true;  

        }  

  

        @Override  

        public void onDrawFrame(GL10 gl) {  

            // TODO Auto-generated method stub  

            if (!stop) {  

                // 如果rotateX不为0,向Y轴旋转rotateX角度  

                if (rotateX != 0) {  

                    // 旋转物体的绕Y旋转  

                    cube.rotateY(rotateX);  

                    // 将rotateX置0  

                    rotateX = 0;  

                }  

                  

                if (rotateY != 0) {    

                    // 旋转物体的旋转围绕x由给定角度宽(弧度,逆时针为正值)轴矩阵,应用到对象下一次渲染时。    

                    cube.rotateX(rotateY);    

                    // 将rotateY置0    

                    rotateY = 0;    

                }    

                  

                // 用给定的颜色(backColor)清除FrameBuffer  

                fb.clear(backColor);  

  

                // 变换和灯光所有多边形  

                world.renderScene(fb);  

  

                // 绘制  

                world.draw(fb);  

  

                // 渲染图像显示  

                fb.display();  

            }else {    

                if (fb != null) {    

                    fb.dispose();    

                    fb = null;    

                }    

            }    

        }  

  

        @Override  

        public void onSurfaceChanged(GL10 gl, int width, int height) {  

            // TODO Auto-generated method stub  

            // 如果FrameBuffer不为NULL,释放fb所占资源  

            if (fb != null) {  

                fb.dispose();  

            }  

  

            // 创建一个宽度为w,高为h的FrameBuffer  

            fb = new FrameBuffer(gl, width, height);  

  

            // 如果master为空  

            if (master == null) {  

  

                // 实例化World对象  

                world = new World();  

  

                // 设置了环境光源强度。负:整个场景会变暗;正:将照亮了一切。  

                world.setAmbientLight(20, 20, 20);  

  

                // 在World中创建一个新的光源  

                sun = new Light(world);  

  

                // 创建一个纹理  

                Bitmap image = BitmapFactory.decodeResource(mContext  

                        .getResources(), R.drawable.ic_launcher);  

                Texture texture = new Texture(image);  

  

                // 纹理的名字  

                String textureName = "texture";  

  

                // TextureManager.getInstance()取得一个Texturemanager对象  

                // addTexture(textureName,texture)添加一个纹理  

                TextureManager.getInstance().addTexture(textureName, texture);  

  

                // Object3D对象开始了:-)  

  

                // Primitives提供了一些基本的三维物体,假如你为了测试而生成一些对象或为  

                // 其它目的使用这些类将很明智,因为它即快速又简单,不需要载入和编辑。  

                // 调用public static Object3D getCube(float scale) scale:角度  

                // 返回一个立方体  

                cube = Primitives.getCube(10);  

  

                // 以纹理的方式给对象所有面"包装"上纹理  

                cube.calcTextureWrapSpherical();  

  

                // 给对象设置纹理  

                cube.setTexture(textureName);  

  

                // 除非你想在事后再用PolygonManager修改,否则释放那些不再需要数据的内存  

                cube.strip();  

  

                // 初始化一些基本的对象是几乎所有进一步处理所需的过程。  

                // 如果对象是"准备渲染"(装载,纹理分配,安置,渲染模式设置,  

                // 动画和顶点控制器分配),那么build()必须被调用,  

                cube.build();  

  

                // 将Object3D对象添加到world集合  

                world.addObject(cube);  

  

                // 该Camera代表了Camera/viewer在当前场景的位置和方向,它也包含了当前视野的有关信息  

                // 你应该记住Camera的旋转矩阵实际上是应用在World中的对象的一个旋转矩阵。  

                // 这一点很重要,当选择了Camera的旋转角度,一个Camera(虚拟)围绕w旋转和通过围绕World围绕w旋转、  

                // 将起到相同的效果,因此,考虑到旋转角度,World围绕camera时,camera的视角是静态的。假如你不喜欢  

                // 这种习惯,你可以使用rotateCamera()方法  

                Camera cam = world.getCamera();  

  

                // 以50有速度向后移动Camera(相对于目前的方向)  

                cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);  

  

                // cub.getTransformedCenter()返回对象的中心  

                // cam.lookAt(SimpleVector lookAt))  

                // 旋转这样camera以至于它看起来是在给定的world-space 的位置  

                cam.lookAt(cube.getTransformedCenter());  

  

                // SimpleVector是一个代表三维矢量的基础类,几乎每一个矢量都  

                // 是用SimpleVector或者至少是一个SimpleVector变体构成的(有时由于  

                // 某些原因比如性能可能会用(float x,float y,float z)之类)。  

                SimpleVector simpleVector = new SimpleVector();  

  

                // 将当前SimpleVector的x,y,z值设为给定的SimpleVector(cube.getTransformedCenter())的值  

                simpleVector.set(cube.getTransformedCenter());  

  

                // Y方向上减去100  

                simpleVector.y -= 100;  

  

                // Z方向上减去100  

                simpleVector.z -= 100;  

  

                // 设置光源位置  

                sun.setPosition(simpleVector);  

  

                // 强制GC和finalization工作来试图去释放一些内存,同时将当时的内存写入日志,  

                // 这样可以避免动画不连贯的情况,然而,它仅仅是减少这种情况发生的机率  

                MemoryHelper.compact();  

  

                // 如果master为空,使用日志记录且设master为HelloWorld本身  

                if (master == null) {  

                    // Logger.log("Saving master Activity!");  

                    master = main_activity.this;  

                }  

            }  

  

        }  

  

        public void onSurfaceCreated(GL10 gl, EGLConfig config) {  

            // TODO Auto-generated method stub  

  

        }

@Override
public void onSurfaceCreated(GL10 gl,
javax.microedition.khronos.egl.EGLConfig config) {
// TODO Auto-generated method stub

}  

  

    }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息