手机上J2ME的3D编程-简单创建3D立方体(1)

来源:互联网 发布:voip网络电话交换机 编辑:程序博客网 时间:2024/05/17 04:01

参考文档:JSR-184

工具:Eclipse3.0+Eclipseme

APIMobile 3D Graphics API

模拟器:Sony Ericsson

分析:

在现实生活中,我们所看到的是一个3维系统。透过双眼,可以看到的是一个丰富多彩的世界。我们所用的照相机,能够拍出很多漂亮的图片。拍摄每张图片时,可以根据拍摄距离的远近可以调整所拍摄画面上的内容。

 

 

在虚拟的世界里,可以模拟真实世界。欲将虚拟世界展示出来,就要利用一个虚拟的照相机,将虚拟的世界拍摄出来。

 

 

JSR-184中,虚拟的世界是由World类构造的。虚拟的照相机是Camera类。通过Camera的位置或视角的改面,将虚拟世界显示到手机屏幕上。Camera默认是朝向Z轴的负方向。在虚拟世界中,各个物体都是由Mesh类创建的。不同的Mesh属性对应不同的物体。

 

 

构造物体时,先要构造物体的骨架(即形状),然后用一种材料蒙到骨架上。物体的形状,是由一组点和每个点的法向量决定的。材料则是图片。

 

 

下面按照与上面相反的顺序再一个虚拟的世界中构建一个立方体。步骤如下:

1 构造立方体

构造一个立方体需要给出立方体的各个顶点,然后规定各个顶点的法向量,最后将图片帖到由顶点和各点法向量构成的架子上。

 

 

各个顶点的给出,是和法向量配合的。由于立方体是由6个矩形组成的,所以就要确定这6个矩形。因为由一个点和一个法向量就能够确定一个面,那么4个顶点和4个法向量就确定了4个面。如果这4个面重叠,4个顶点就构成了一个矩形的区域。用这个方法确定6个矩形,再用这6个举行组成一个立方体。

 

 

将图片帖到立方体是,就是将立方体上的顶点和图片上的点进行对应。两者可以用不同的单位。

下面构建一个立方体

给出立方体的顶点们:

              short x = 20;

              short y = 20;

              short z = 20;

              short fx = (short) -x;

              short fy = (short) -y;

              short fz = (short) -z;

             

              short[] vert = {x,y,z, fx,y,z, x,fy,z, fx,fy,z,        //D

                                          fx,y,fz, x,y,fz, fx,fy,fz, x,fy,fz, //C

                                          fx,y,z, fx,y,fz, fx,fy,z, fx,fy,fz, //B

                                          x,y,fz, x,y,z, x,fy,fz, x,fy,z,          //F

                                          x,y,fz, fx,y,fz, x,y,z, fx,y,z,          //A

                                          x,fy,z, fx,fy,z, x,fy,fz, fx,fy,fz};   //E

             

              VertexArray vertArray = new VertexArray(vert.length/3, 3, 2);

              vertArray.set(0, vert.length/3, vert);

             

给出立方体顶点们的法向量:

        byte[] norm = { 0,0,127, 0,0,127, 0,0,127, 0,0,127,

                                    0,0,-127, 0,0,-127, 0,0,-127, 0,0,-127,

                                          -127,0,0, -127,0,0, -127,0,0, -127,0,0,

                                          127,0,0, 127,0,0, 127,0,0, 127,0,0,

                                          0,127,0, 0,127,0, 0,127,0, 0,127,0,

                                          0,-127,0, 0,-127,0, 0,-127,0, 0,-127,0};

 

 

        VertexArray normArray = new VertexArray(norm.length / 3, 3, 1);

        normArray.set(0, norm.length / 3, norm);

 

 

给出顶点们对应图片上的点(verttex数组是一一对应的):

        short[] tex = { 1, 0, 0, 0, 1, 1, 0, 1,

                                    1, 0, 0, 0, 1, 1, 0, 1,

                                          1, 0, 0, 0, 1, 1, 0, 1,

                                          1, 0, 0, 0, 1, 1, 0, 1,

                                          1, 0, 0, 0, 1, 1, 0, 1,

                                          1, 0, 0, 0, 1, 1, 0, 1 };

 

 

        VertexArray texArray = new VertexArray(tex.length / 2, 2, 2);

        texArray.set(0, tex.length / 2, tex);

 

 

根据上面给出的数组,组成立方体:

        // create the VertexBuffer for our object

        VertexBuffer vb = boxVB = new VertexBuffer();

        vb.setPositions(vertArray, 1.0f, null);

        vb.setNormals(normArray);

        vb.setTexCoords(0, texArray, 1.0f, null);

 

 

 

 

2 将立方体实例化

 

 

// the length of each triangle strip

        int[] stripLen = { 4, 4, 4, 4, 4, 4 };

 

 

        // create the index buffer for our object (this tells how to

        // create triangle strips from the contents of the vertex buffer).

        TriangleStripArray tsa = new TriangleStripArray(0, stripLen);

 

 

        //创建多边形模式。

        PolygonMode pm = new PolygonMode();

        pm.setShading(PolygonMode.SHADE_SMOOTH);

        pm.setCulling(PolygonMode.CULL_NONE);

 

 

              //生成外貌。

        Appearance app = new Appearance();

        app.setPolygonMode(pm);

 

 

Image texImg= null;

        try{

            texImg = Image.createImage(this.imageUrl);

        }catch(Exception e){

               String strErr="Load Image (path=";

               strErr += this.imageUrl;

               strErr += ") Fail!";

            System.err.println(strErr);

        }

      

       

        Texture2D texture=null;

        try{

                     // create texture from loaded image.

            texture = new Texture2D(new Image2D(Image2D.RGB, texImg));           

        }catch(Exception e){

               e.printStackTrace();

               String strErr = "Failed to create texture Use Format --- Image2D.RGB";

            System.out.println(strErr);

        }

       

        try{

                     // repeat texture on surface

            texture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_REPEAT);

           

        }catch(Exception e){

               e.printStackTrace();

            System.out.println("Failed to create texture3");

        }

       

        try{

                     // Blend mode to use.

            texture.setBlending(Texture2D.FUNC_DECAL);

        }catch(Exception e){

            System.out.println("Failed to create texture4");

        }

       

        try{

                     // Use nearest for performance, linear for quality

            texture.setFiltering(Texture2D.FILTER_NEAREST,Texture2D.FILTER_NEAREST);

        }catch(Exception e){

            System.out.println("Failed to create texture5");

        }

       

        try{

                     app.setTexture(0, texture);

        }catch(Exception e){

            System.out.println("Failed to create texture6");

        }

 

 

              //创建网格。

        mesh = new Mesh(vb, tsa, app);

        mesh.setAppearance(0, app);

 

 

 

 

3 创建World

//create a camera

        camera = new Camera();

        camera.setPerspective(60.0f, // field of view

                (float) getWidth() / (float) getHeight(), // aspectRatio

                1.0f, // near clipping plane

                1000.0f); // far clipping plane

        camera.setTranslation(0,0, 50);

 

 

        world = new World();

        world.addChild(camera);

       

              world.addChild(mesh);

        world.setActiveCamera(camera);

 

 

 

 

4 绘制World

//private Graphics3D g3d = null;

 

 

protected void paint(Graphics g) {

           // TODO Auto-generated method stub

           Draw(g);

    }

private void Draw(Graphics g){

              try{

            g3d.bindTarget(g);

            g3d.render(world);

        }catch(Exception e){

           

        }finally{

            g3d.releaseTarget();

        }

       }

 

 

 

 

5 旋转立方体

 

 

mesh.postRotate(1.0f, 1.0f, 0f, 0f);

意识是以向量为(1,0,0)的方向(就是x轴)为轴将立方体旋转1度。

 

 

 

 

至此,就是建造一个虚拟的世界,并放置一个立方体的步骤。上面的代码是从一个完整的程序中截取的,有的地方可能不方便理解。后面将附上完整的代码。

 

 

原创粉丝点击