📄 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<P> * 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<P> * * @return A World object. */ public World getWorld(){ return world; } /** * Determines if a model is visible in the Camera FOV<P> * 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<P> * 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<num;i++){ // Get model to put in scene model = world.getModel(i); // Get coordinates stream of model geom = model.getGeometry(); // Model view culling if (inView(model)){ // Set visibility on for model visibility_mask |= (1 << i); // Transform Model for location and orientation in scene transform(geom, model.pitch-pitch,model.yaw-yaw,model.roll-roll, c.x,c.y,c.z, model.size, offset); // Set surface masks for Surface geometry models if (geom instanceof Surface){ surface_masks[i] = setModelSurfaceMask((Surface)geom,offset); } } else visibility_mask &= ~(1 << i); // Set offset for next model offset += geom.x.length; } } private int setModelSurfaceMask(Surface geom, int m_offset){ int normals_idx = m_offset + geom.getNormalOffset(); int normals_max = m_offset + geom.x.length; int surface_mask = 0; int mask_index = 0; // For each defined surface do for (int i=normals_idx;i< normals_max;i++){ // Calculate dot product between normals and view vector int dot_product = vx*x_tfx[i] + vy*y_tfx[i] + vz*z_tfx[i]; // Set Surface visibility if (dot_product <= 0) surface_mask |= 1 << mask_index; mask_index++; } return(surface_mask); } private void transform(Geometry geom,int rx,int ry,int rz, int tx, int ty, int tz,int scale, int offset){ // Set internal loop boundaries int x_last = geom.x.length + offset; int normal_idx = 0; if (geom instanceof Surface) normal_idx = ((Surface)geom).getNormalOffset(); normal_idx += offset; // Set the cached rotation parameters c.setRotation(rx,ry,rz); // Transform each coordinate for (int i=offset;i < x_last;i++) { // Get the current coordiates from the stream c.x = geom.x[i]; c.y = geom.y[i]; c.z = geom.z[i]; // Rotate the coordinates of the model c.rotate(); // Store the rotation transformation x_tfx[i]=c.x; y_tfx[i]=c.y; z_tfx[i]=c.z; // Translate over (tx,ty,tz); if (i < normal_idx){ x_tfx[i] += tx; y_tfx[i] += ty; z_tfx[i] += tz; } } } /** * Return the approximate distance between the camera and a model<P> * 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(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -