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