M3GCanvas.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:7k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. import javax.microedition.lcdui.game.*;
  3. import javax.microedition.m3g.*;
  4. class M3GCanvas extends GameCanvas implements Runnable{
  5.   
  6.     private Graphics3D g3d; // Graphics object used to render the world.
  7.     private World world; // This world contains the camera and the pyramidMesh.
  8.     private Camera camera; // the camera in the scene
  9.     private Mesh pyramidMesh; // the pyramid in the scene
  10.     
  11.     public M3GCanvas(){
  12.         super(false);
  13.         
  14.         setFullScreenMode(true);
  15.         
  16.         g3d = Graphics3D.getInstance();
  17.         world = new World();
  18.         camera = new Camera();
  19.         world.addChild(camera); // add the camera to the world.
  20.         float w = getWidth();
  21.         float h = getHeight();
  22.         // Constructs a perspective projection matrix and sets that as the current projection matrix.
  23.         camera.setPerspective(60.0f, w / h, 0.1f, 50f);
  24.         
  25.         pyramidMesh = createpyramid(); // create our pyramid.
  26.         pyramidMesh.setTranslation(0.0f, 0.0f, -3.2f); // move the pyramid 3 units into the screen.
  27.         world.addChild(pyramidMesh); // add the pyramid to the world
  28.       //pyramidMesh.translate(0.0f, 0.0f, -3.2f);
  29.         world.setActiveCamera(camera);
  30.         
  31.         Thread t = new Thread(this);
  32.         t.start();
  33.     }
  34.     public void draw3D(Graphics g){
  35.         try{
  36.             g3d.bindTarget(g); // Binds the given Graphics or mutable Image2D as the rendering target of this Graphics3D
  37.             g3d.render(world); // Render the world
  38.         }finally{
  39.             g3d.releaseTarget();
  40.         }
  41.     }
  42.     public void run() {
  43.         Graphics g = getGraphics();
  44.         while(true){
  45.             // rotate the pyramid 1 degree around the Y-axis.
  46.             pyramidMesh.postRotate(3.0f, 0.0f, 1.0f, 0.0f);
  47.             draw3D(g);
  48.             flushGraphics();
  49.         }
  50.     }
  51.     // this method creates a colored pyramid.
  52.     private Mesh createpyramid(){
  53.         
  54.         // The vertices used by the pyramid. x, y, z
  55.         short []POINTS = new short[] {-1, -1, 1, // point 1
  56.                                       1, -1, 1, // point 2
  57.                                       1, -1, -1, // point 3
  58.                                      -1, -1, -1, // point 4
  59.                                      0, 1, 0}; // point 5, top
  60.                                                             
  61.                                                             
  62.         // The points sequence.
  63.         int []INDICES = new int[] {
  64.         0, 1, 4, 
  65.         1, 2, 4, 
  66.         2, 3, 4, 
  67.         3, 0, 4, 
  68.         2, 1, 0, 
  69.         2, 0, 3};
  70.         byte []COLORS = new byte[] {127, 0, 0, //R
  71.                                     0, 127, 0,  //G
  72.                                     0, 0, 127, //B
  73.                                    127, 0, 127, //B
  74.                                    0, 127, 127};//B
  75.                                                         
  76.         // The length of each sequence in the indices array.
  77.         int []LENGTH = new int[] {3, 3, 3, 3, 3, 3}; // the pyramid is built by six triangles
  78.         
  79.         VertexArray POSITION_ARRAY, COLOR_ARRAY;
  80.         IndexBuffer INDEX_BUFFER;
  81.         
  82.         // Create a VertexArray to be used by the VertexBuffer
  83.         POSITION_ARRAY = new VertexArray(POINTS.length / 3, 3, 2);
  84.         POSITION_ARRAY.set(0, POINTS.length / 3, POINTS);
  85.         COLOR_ARRAY = new VertexArray(COLORS.length / 3, 3, 1);
  86.         COLOR_ARRAY.set(0, COLORS.length / 3, COLORS);
  87.         INDEX_BUFFER = new TriangleStripArray(INDICES, LENGTH);
  88.         
  89.         // VertexBuffer holds references to VertexArrays that contain the positions, colors, normals, 
  90.         // and texture coordinates for a set of vertices
  91.         VertexBuffer vertexBuffer = new VertexBuffer();
  92.         vertexBuffer.setPositions(POSITION_ARRAY, 1.0f, null);
  93.         vertexBuffer.setColors(COLOR_ARRAY);
  94.         System.out.println(vertexBuffer.getVertexCount()); 
  95.         
  96.        /* System.out.println(vertexBuffer.getDefaultColor()); 
  97.         vertexBuffer.setDefaultColor(0xFFFFFF);
  98.         System.out.println(vertexBuffer.getDefaultColor());
  99.         vertexBuffer.setDefaultColor(0x00FF0000);*/
  100.         //vertexBuffer.setDefaultColor(0xFF0000);
  101.         
  102.         
  103.         // Create the 3D object defined as a polygonal surface
  104.         Mesh mesh = new Mesh(vertexBuffer, INDEX_BUFFER, null);
  105.         
  106.         Appearance appearance = new Appearance(); // A set of component objects that define the rendering attributes of a Mesh
  107.         PolygonMode polygonMode = new PolygonMode(); // An Appearance component encapsulating polygon-level attributes
  108.         polygonMode.setPerspectiveCorrectionEnable(true);
  109.         polygonMode.setCulling(PolygonMode.CULL_NONE); // By using CULL_NONE all faces of the pyramid will be shown.
  110.         polygonMode.setShading(PolygonMode.SHADE_SMOOTH); // use a smooth shading of the colors on the pyramid.
  111.        // polygonMode.setPerspectiveCorrectionEnable(true);
  112.         polygonMode.setTwoSidedLightingEnable(false);
  113.         polygonMode.setLocalCameraLightingEnable(true);
  114.         appearance.setPolygonMode(polygonMode);
  115.         
  116.         /*Fog fog = new Fog();
  117.         fog.setMode(Fog.EXPONENTIAL);
  118.         fog.setDensity(0.2f);
  119.         System.out.println(fog.getColor());
  120.         fog.setColor(0xB7B7B7);
  121.         appearance.setFog(fog);*/
  122.          Appearance appearance1 = new Appearance();
  123.          PolygonMode polygonMode1 = new PolygonMode(); // An Appearance component encapsulating polygon-level attributes
  124.         polygonMode1.setPerspectiveCorrectionEnable(true);
  125.         polygonMode1.setCulling(PolygonMode.CULL_NONE); // By using CULL_NONE all faces of the pyramid will be shown.
  126.         polygonMode1.setShading(PolygonMode.SHADE_FLAT); // use a smooth shading of the colors on the pyramid.
  127.        // polygonMode.setPerspectiveCorrectionEnable(true);
  128.         polygonMode1.setTwoSidedLightingEnable(false);
  129.         polygonMode1.setLocalCameraLightingEnable(true);
  130.  
  131.         appearance1.setPolygonMode(polygonMode1);
  132.       /*  Fog fog = new Fog();
  133.         fog.setMode(Fog.LINEAR);
  134.         //fog.setDensity(0.2f);
  135.         fog.setLinear(0.8f,2f);
  136.         //fog.setColor(0xFFFF00);
  137.         fog.setColor(0xB7B7B7);
  138.         appearance.setFog(fog);*/
  139.  
  140.        mesh.setAppearance(0, appearance1);//hou jia xian xian 
  141.        mesh.setAppearance(0, appearance);
  142.         
  143.         appearance1.setLayer(2);
  144.         appearance.setLayer(1);
  145.         System.out.println(mesh.getSubmeshCount()); 
  146.         System.out.println(appearance1.getLayer());
  147.         System.out.println(appearance.getLayer());
  148.         // Set the appearance to the 3D object
  149.         return mesh;
  150.     }    
  151. }