IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  Java technology  >

针对 Java 移动设备的 3D 图形,第 2 部分: M3G 的保留模式

使用 JSR 184 轻松地管理场景图中的 3D 对象

developerWorks


返回原文


/*
 * Sample code for M3G article on IBM developerWorks.
 * http://www.ibm.com/developerworks/
 */

package m3gsamples2;

import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;

/**
 * Animates a 3D model imported in M3G's binary format.
 *
 * @author Claus Hoefele
 */
public class BinaryWorldSample extends Canvas implements Sample, Runnable
{
  /** Name of the M3G file. */
  private static final String M3G_FILE_NAME = "/blender.m3g";
  
  /** User ID to identify the text mesh (1).*/
  private static final int USER_ID_TEXT = 1;
  
  /** Object that represents the 3D world. */
  private World _world;
  
  /** Flag for stopping the Thread.*/
  private boolean _isRunning;
  
  /** Graphics singleton used for rendering. */
  private Graphics3D _graphics3d;

  /**
   * When the sample is shown, it's initialized and the animation
   * thread started.
   */
  public void showNotify()
  {
    init();
    
    Thread thread = new Thread(this);
    _isRunning = true;
    thread.start();
  }

  /**
   * Stops the animation thread.
   */
  public void hideNotify()
  {
    _isRunning = false;
  }

  /**
   * Initializes the sample.
   */
  protected void init()
  {
    // Get the singleton for 3D rendering.
    _graphics3d = Graphics3D.getInstance();

    try
    {
      // Load World from M3G binary file.
      Object3D[] objects = Loader.load(M3G_FILE_NAME);
      _world = (World) objects[0];
      
      // Change the camera's properties to match the current device.
      Camera camera = _world.getActiveCamera();
      float aspect = (float) getWidth() / (float) getHeight();
      camera.setPerspective(60.0f, aspect, 1.0f, 1000.0f);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
  
  /**
   * Drives the animation.
   */
  public void run()
  {
    while(_isRunning)
    {
      // Find the Mesh and rotate it.
      Mesh text = (Mesh) _world.find(USER_ID_TEXT);
      text.postRotate(-2.0f, 0.0f, 0.0f, 1.0f);
      repaint();
      
      try
      {
        Thread.sleep(50);
      }
      catch (Exception e){}
    }
  }

  /**
   * Renders the sample on the screen.
   *
   * @param graphics the graphics object to draw on.
   */
  protected void paint(Graphics graphics)
  {
    _graphics3d.bindTarget(graphics);
    _graphics3d.render(_world);
    _graphics3d.releaseTarget();
  }

  /**
   * Returns the <code>Displayable</code> used to display this sample.
   *
   * @return display
   */
  public Displayable getDisplayable()
  {
    return this;
  }

  /**
   * Returns the display name of this sample.
   *
   * @return name
   */
  public String getName()
  {
    return "Binary World";
  }
}


返回原文


    关于 IBM 隐私条约 联系 IBM 使用条款