Visibility.java revision 076357b8567458d4b6dfdcf839ef751634cd2bfb
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.opengl; 18 19/** 20 * {@hide} 21 * A collection of utility methods for computing the visibility of triangle 22 * meshes. 23 * 24 */ 25public class Visibility { 26 /** 27 * Test whether a given triangle mesh is visible on the screen. The mesh 28 * is specified as an indexed triangle list. 29 * 30 * @param ws the world space to screen space transform matrix, as an OpenGL 31 * column matrix. 32 * @param wsOffset an index into the ws array where the data starts. 33 * @param positions the vertex positions (x, y, z). 34 * @param positionsOffset the index in the positions array where the data 35 * starts. 36 * @param indices the indices of the triangle list. The indices are 37 * expressed as chars because they are unsigned 16-bit values. 38 * @param indicesOffset the index in the indices array where the index data 39 * starts. 40 * @param indexCount the number of indices in use. Typically a multiple of 41 * three. If not a multiple of three, the remaining one or two indices will 42 * be ignored. 43 * @return 2 if all of the mesh is visible, 1 if some part of the mesh is 44 * visible, 0 if no part is visible. 45 * 46 * @throws IllegalArgumentException if ws is null, wsOffset < 0, 47 * positions is null, positionsOffset < 0, indices is null, 48 * indicesOffset < 0, indicesOffset > indices.length - indexCount 49 */ 50 public static native int visibilityTest(float[] ws, int wsOffset, 51 float[] positions, int positionsOffset, char[] indices, 52 int indicesOffset, int indexCount); 53 54 /** 55 * Given an OpenGL ES ModelView-Projection matrix (which implicitly 56 * describes a frustum) and a list of spheres, determine which spheres 57 * intersect the frustum. 58 * <p> 59 * A ModelView-Projection matrix can be computed by multiplying the 60 * a Projection matrix by the a ModelView matrix (in that order.). There 61 * are several possible ways to obtain the current ModelView and 62 * Projection matrices. The most generally applicable way is to keep 63 * track of the current matrices in application code. If that is not 64 * convenient, there are two optional OpenGL ES extensions which may 65 * be used to read the current matrices from OpenGL ES: 66 * <ul> 67 * <li>GL10Ext.glQueryMatrixxOES 68 * <li>GL11.GL_MODELVIEW_MATRIX_FLOAT_AS_INT_BITS_OES and 69 * GL_PROJECTION_MATRIX_FLOAT_AS_INT_BITS_OES 70 * </ul> 71 * The problem with reading back the matrices is that your application 72 * will only work with devices that support the extension(s) that 73 * it uses. 74 * <p> 75 * A frustum is a six-sided truncated pyramid that defines the portion of 76 * world space that is visible in the view. 77 * <p> 78 * Spheres are described as four floating point values: x, y, z, and r, in 79 * world-space coordinates. R is the radius of the sphere. 80 * <p> 81 * @param mvp a float array containing the mode-view-projection matrix 82 * @param mvpOffset The offset of the mvp data within the mvp array. 83 * @param spheres a float array containing the sphere data. 84 * @param spheresOffset an offset into the sphere array where the sphere 85 * data starts 86 * @param spheresCount the number of spheres to cull. 87 * @param results an integer array containing the indices of the spheres 88 * that are either contained entirely within or intersect the frustum. 89 * @param resultsOffset an offset into the results array where the results 90 * start. 91 * @param resultsCapacity the number of array elements available for storing 92 * results. 93 * @return the number of spheres that intersected the frustum. Can be 94 * larger than resultsCapacity, in which case only the first resultsCapacity 95 * results are written into the results array. 96 * 97 * @throws IllegalArgumentException if mvp is null, mvpOffset < 0, 98 * mvpOffset > mvp.length - 16, spheres is null, spheresOffset < 0, 99 * spheresOffset > spheres.length - sphereCount, 100 * results is null, resultsOffset < 0, resultsOffset > results.length - 101 * resultsCapacity. 102 */ 103 public static native int frustumCullSpheres(float[] mvp, int mvpOffset, 104 float[] spheres, int spheresOffset, int spheresCount, 105 int[] results, int resultsOffset, int resultsCapacity); 106 107 /** 108 * Compute a bounding sphere for a set of points. It is approximately the 109 * minimal bounding sphere of an axis-aligned box that bounds the points. 110 * 111 * @param positions positions in x, y, z triples 112 * @param positionsOffset offset into positions array 113 * @param positionsCount number of position triples to process 114 * @param sphere array containing the output as (x, y, z, r) 115 * @param sphereOffset offset where the sphere data will be written 116 * 117 * @throws IllegalArgumentException if positions is null, 118 * positionsOffset < 0, positionsOffset > positions.length - positionsCount, 119 * sphere is null, sphereOffset < 0, sphereOffset > sphere.length - 4. 120 */ 121 public static native void computeBoundingSphere(float[] positions, 122 int positionsOffset, int positionsCount, float[] sphere, 123 int sphereOffset); 124} 125