www.pudn.com > j3dme-0.3.0.rar > Camera.java


/*
 * J3DME Fast 3D software rendering for small devices
 * Copyright (C) 2001 Onno Hommes
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package net.jscience.j3dme;

/**
 * This class allows you to define camera objects in the
 * virtual world. You can define multiple cameras and place them
 * into the same world. A camera must be linked to the view port
 * in order to have the renderer display what its visible in the world.
 * The Viewport class will provide the details for the connection.
 */
public class Camera{

   /**
    * The x component of the camera location in the world
    */
   public int x;

   /**
    * The y component of the camera location in the world
    */
   public int y;
   /**
    * The z component of the camera location in the world
    */
   public int z;

   /**
    * The pitch orientation of the camera
    */
   public int pitch;
   /**
    * The yaw orientation of the camera
    */

   public int yaw;
   /**
    * The roll orientation of the camera
    */

   public int roll;

   /**
    * The field of view sets how many pixels (left to right)
    * are visible
    */
   public int fieldOfView;

   private World world;
   protected int[] x_tfx;
   protected int[] y_tfx;
   protected int[] z_tfx;
   private Coordinate c = new Coordinate();
   private int vx,vy,vz;
   protected int visibility_mask;
   protected int[] surface_masks;

   /**
    * Constructs a Camera object

* Construct a camera in the provided world with a field of view of * fov. The field of view is expressed in the number of pixels that * are visible on the view plane. * * @param w The virtual world seen by the camera * @param fov The field of view in pixels * @return A Camera object in world w with a fov. */ public Camera(World w, int fov){ // Init Camera location x = 0;y = 0;z = 0; // Init Camera Orientation pitch = 0;yaw = 0;roll = 0; // Link Camera to virtual world world = w; // Set Field Of View fieldOfView = fov << 8; // Set maxium coordinates x_tfx = new int[128]; y_tfx = new int[128]; z_tfx = new int[128]; // Align camera model and surface visibility with the World visibility_mask = 0; surface_masks= new int[32]; } /** * Returns the world visible through the camera

* * @return A World object. */ public World getWorld(){ return world; } /** * Determines if a model is visible in the Camera FOV

* Calculates if the provided model is visible in the * field of view (FOV) of the camera. The method returns * true if the model is visible and false if the model * is not in view or to small to be seen. * * @return Is model visible or not. */ public boolean inView(Model model){ c.x = model.x - x; c.y = model.y - y; c.z = model.z - z; c.setRotation(pitch,yaw,roll); c.rotate(); int scale = fieldOfView / c.getDistance(); if (scale == 0) return false; // Too small Geometry geometry = model.getGeometry(); int radius = geometry.getBoundingRadius(); if ((c.z + radius) < 0) return false; // Behind the camera if (((Math.max(Math.abs(c.x), Math.abs(c.y))-radius)*scale) > fieldOfView >> 1) return false; // out of view // Record the view vector vx = c.x; vy = c.y; vz = c.z; return true; // In view } /** * Returns the visible world coordinate transformations

* This method captures the scene as visible through the current * camera object. The camera will return a transformation matrix * with all the visible model coordinates. * * @return The transformation matrix in a int array. */ public void captureScene(){ Model model; Geometry geom; int offset = 0; int num = world.getNumberOfModels(); // Process Models in world for(int i= 0;i * This method uses a quick perceived distance instead of the actual * mathematcial distance. * * @param model The model to determine approx. distance to. * @return The approximate model distance from camera */ public int getModelDistance(Model m){ c.x = x - m.x; c.y = y - m.y; c.z = z - m.z; return c.getDistance(); } }