M3G.java
上传用户:haobig99
上传日期:2022-06-15
资源大小:369k
文件大小:6k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
  3.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4.  *
  5.  * This code is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License version 2 only, as
  7.  * published by the Free Software Foundation.  Sun designates this
  8.  * particular file as subject to the "Classpath" exception as provided
  9.  * by Sun in the LICENSE file that accompanied this code.
  10.  *
  11.  * This code is distributed in the hope that it will be useful, but WITHOUT
  12.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14.  * version 2 for more details (a copy is included in the LICENSE file that
  15.  * accompanied this code).
  16.  *
  17.  * You should have received a copy of the GNU General Public License version
  18.  * 2 along with this work; if not, write to the Free Software Foundation,
  19.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22.  * CA 95054 USA or visit www.sun.com if you need additional information or
  23.  * have any questions.
  24.  */
  25. package com.sun.lwuit;
  26. import javax.microedition.m3g.Graphics3D;
  27. import javax.microedition.m3g.Image2D;
  28. /**
  29.  * Support for binding the 3D graphics M3G API (JSR 184), this allows us to integrate
  30.  * 2D UI's with 3D special effects and transitions while keeping the rendering
  31.  * pipelines in sync.
  32.  * <p>This class is a singleton that includes a callback interface which abstracts
  33.  * the separation between the 2D and 3D pipelines. For more on the difference between
  34.  * 2D and 3D rendering pipelines please consult the M3G documentation.
  35.  * 
  36.  * @author Shai Almog
  37.  */
  38. public class M3G {
  39.     private static final M3G INSTANCE = new M3G();
  40.     private static final boolean IS_SUPPORTED;
  41.     static {
  42.         boolean supported = false;
  43.         try {
  44.             Class.forName("javax.microedition.m3g.Graphics3D");
  45.             supported = true;
  46.         } catch (Throwable t) {
  47.         }
  48.         IS_SUPPORTED = supported;
  49.     }
  50.     private M3G() {
  51.     }
  52.     /**
  53.      * Returns the singleton instance of this class
  54.      * 
  55.      * @return the singleton instance of this class
  56.      */
  57.     public static M3G getInstance() {
  58.         return INSTANCE;
  59.     }
  60.     /**
  61.      * Returns true if the M3G (JSR 184) API is supported on this device
  62.      * 
  63.      * @return True if this device supports M3G
  64.      */
  65.     public static boolean isM3GSupported() {
  66.         return IS_SUPPORTED;
  67.     }
  68.     /**
  69.      * Returns the maximum size for a texture according to the underlying graphics engine
  70.      * 
  71.      * @return the size of the largest possible texture on the device
  72.      */
  73.     public int getMaxTextureDimension() {
  74.         return ((Integer)Graphics3D.getProperties().get("maxTextureDimension")).intValue();
  75.     }
  76.     
  77.     /**
  78.      * Helper method returns the closest power of two smaller than X
  79.      * 
  80.      * @param x number
  81.      * @return a power of 2 smaller than X
  82.      */
  83.     public static int closestLowerPowerOf2(int x) {        
  84.         return closestHigherPowerOf2(x) >> 1;
  85.     }
  86.     /**
  87.      * Helper method returns the closest power of two larger than X
  88.      * 
  89.      * @param x number
  90.      * @return a power of 2 smaller than X
  91.      */
  92.     public static int closestHigherPowerOf2(int x) {
  93.         x--;
  94.         x |= x >> 1;
  95.         x |= x >> 2;
  96.         x |= x >> 4;
  97.         x |= x >> 8;
  98.         x |= x >> 16;
  99.         x++;
  100.         return x;
  101.     }
  102.     /**
  103.      * Binds the Graphics3D object to the current graphics context and invokes
  104.      * callback to perform the rendering. This method is responsible for flushing
  105.      * the graphics3D object and buffer to avoid problems related to 2D/3D rendering
  106.      * pipeline collision.
  107.      * 
  108.      * @param g Graphics context to draw on, notice that translate and clipping might be ignored
  109.      * @param depth should depth buffering be used in the graphics context bind method
  110.      * @param arguments arguments to the Graphics3D bind method
  111.      * @param c the callback invoked to perform the actual 3D rendering
  112.      */
  113.     public void renderM3G(Graphics g, boolean depth, int arguments, Callback c) {
  114.         Graphics3D g3d = Graphics3D.getInstance();
  115.         g3d.bindTarget(g.getGraphics(), depth, arguments);
  116.         try {
  117.             c.paintM3G(g3d);
  118.         } finally {
  119.             g3d.releaseTarget();
  120.         }
  121.     }
  122.     /**
  123.      * Converts an image to a new image 2D object, notice that further changes to
  124.      * the would have no effect on the Image2D object.
  125.      * 
  126.      * @param type the type of the image e.g. RGB, ARGB etc.
  127.      * @param img image to convert
  128.      * @return Image2D object useful in the 3D API
  129.      */
  130.     public Image2D createImage2D(int type, Image img) {
  131.         if(img.getImage() == null) {
  132.             Image im = Image.createImage(img.getWidth(), img.getHeight());
  133.             im.getGraphics().drawImage(img, 0, 0);
  134.             img = im;
  135.         }
  136.         return new Image2D(type, img.getImage());
  137.     }
  138.     /**
  139.      * Callback interface that allows rendering of 3D graphics on top/bellow the 
  140.      * current form. This interface is invoked as a result of a renderM3G call
  141.      * and should not make any calls to the 2D pipeline. It is the responsibility
  142.      * of the developer not to open additional threads or try to "abuse" the 2D/3D
  143.      * pipeline separation in any way.
  144.      */
  145.     public static interface Callback {
  146.         /**
  147.          * Invoked as a result of a renderM3G call, receives a bound and ready 
  148.          * Graphics3D object that doesn't need "releaseTarget()" calls. 
  149.          * Make sure to return properly from this method or painting won't continue
  150.          * until you do.
  151.          * 
  152.          * @param g Graphics3D object from the M3G API 
  153.          * @see M3G
  154.          */
  155.         public void paintM3G(Graphics3D g);
  156.     }
  157. }