JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
FovHVHalves.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.math;
29
30/**
31 * Horizontal and vertical field of view (FOV) halves,
32 * allowing a non-centered projection.
33 * <p>
34 * The values might be either in tangent or radians.
35 * </p>
36 */
37public final class FovHVHalves {
38 /** Half horizontal FOV from center to left, either in {@link #inTangents} or radians. */
39 public final float left;
40 /** Half horizontal FOV from center to right, either in {@link #inTangents} or radians. */
41 public final float right;
42 /** Half vertical FOV from center to top, either in {@link #inTangents} or radians. */
43 public final float top;
44 /** Half vertical FOV from center to bottom, either in {@link #inTangents} or radians. */
45 public final float bottom;
46 /** If true, values are in tangent, otherwise radians.*/
47 public final boolean inTangents;
48
49 /**
50 * Constructor for one {@link FovHVHalves} instance.
51 * <p>
52 * It is recommended to pass and store values in tangent
53 * if used for perspective FOV calculations, since it will avoid conversion to tangent later on.
54 * </p>
55 * @param left half horizontal FOV, left side, in tangent or radians
56 * @param right half horizontal FOV, right side, in tangent or radians
57 * @param top half vertical FOV, top side, in tangent or radians
58 * @param bottom half vertical FOV, bottom side, in tangent or radians
59 * @param inTangents if true, values are in tangent, otherwise radians
60 */
61 public FovHVHalves(final float left, final float right, final float top, final float bottom, final boolean inTangents) {
62 this.left = left;
63 this.right = right;
64 this.top = top;
65 this.bottom = bottom;
66 this.inTangents = inTangents;
67 }
68
69 /**
70 * Returns a symmetrical centered {@link FovHVHalves} instance in {@link #inTangents}, using:
71 * <pre>
72 halfHorizFovTan = tan( horizontalFov / 2f );
73 halfVertFovTan = tan( verticalFov / 2f );
74 * </pre>
75 * @param horizontalFov whole horizontal FOV in radians
76 * @param verticalFov whole vertical FOV in radians
77 */
78 public static FovHVHalves byRadians(final float horizontalFov, final float verticalFov) {
79 final float halfHorizFovTan = FloatUtil.tan(horizontalFov/2f);
80 final float halfVertFovTan = FloatUtil.tan(verticalFov/2f);
81 return new FovHVHalves(halfHorizFovTan, halfHorizFovTan, halfVertFovTan, halfVertFovTan, true);
82 }
83
84 /**
85 * Returns a symmetrical centered {@link FovHVHalves} instance in {@link #inTangents}, using:
86 * <pre>
87 top = bottom = tan( verticalFov / 2f );
88 left = right = aspect * top;
89 * </pre>
90 *
91 * @param verticalFov vertical FOV in radians
92 * @param aspect aspect ration width / height
93 */
94 public static FovHVHalves byFovyRadianAndAspect(final float verticalFov, final float aspect) {
95 final float halfVertFovTan = FloatUtil.tan(verticalFov/2f);
96 final float halfHorizFovTan = aspect * halfVertFovTan;
97 return new FovHVHalves(halfHorizFovTan, halfHorizFovTan,
98 halfVertFovTan, halfVertFovTan, true);
99 }
100
101 /**
102 * Returns a custom symmetry {@link FovHVHalves} instance {@link #inTangents}, using:
103 * <pre>
104 left = tan( horizontalFov * horizCenterFromLeft )
105 right = tan( horizontalFov * ( 1f - horizCenterFromLeft ) )
106 top = tan( verticalFov * vertCenterFromTop )
107 bottom = tan( verticalFov * (1f - vertCenterFromTop ) )
108 * </pre>
109 * @param horizontalFov whole horizontal FOV in radians
110 * @param horizCenterFromLeft horizontal center from left in [0..1]
111 * @param verticalFov whole vertical FOV in radians
112 * @param vertCenterFromTop vertical center from top in [0..1]
113 */
114 public static FovHVHalves byRadians(final float horizontalFov, final float horizCenterFromLeft,
115 final float verticalFov, final float vertCenterFromTop) {
116 return new FovHVHalves(FloatUtil.tan(horizontalFov * horizCenterFromLeft),
117 FloatUtil.tan(horizontalFov * ( 1f - horizCenterFromLeft )),
118 FloatUtil.tan(verticalFov * vertCenterFromTop),
119 FloatUtil.tan(verticalFov * (1f - vertCenterFromTop )),
120 true);
121 }
122
123 /**
124 * Returns a custom symmetry {@link FovHVHalves} instance {@link #inTangents},
125 * via computing the <code>horizontalFov</code> using:
126 * <pre>
127 halfVertFovTan = tan( verticalFov / 2f );
128 halfHorizFovTan = aspect * halfVertFovTan;
129 horizontalFov = atan( halfHorizFovTan ) * 2f;
130 return {@link #byRadians(float, float, float, float) byRadians}(horizontalFov, horizCenterFromLeft, verticalFov, vertCenterFromTop)
131 * </pre>
132 * @param verticalFov whole vertical FOV in radians
133 * @param vertCenterFromTop vertical center from top in [0..1]
134 * @param aspect aspect ration width / height
135 * @param horizCenterFromLeft horizontal center from left in [0..1]
136 */
137 public static FovHVHalves byFovyRadianAndAspect(final float verticalFov, final float vertCenterFromTop,
138 final float aspect, final float horizCenterFromLeft) {
139 final float halfVertFovTan = FloatUtil.tan(verticalFov/2f);
140 final float halfHorizFovTan = aspect * halfVertFovTan;
141 final float horizontalFov = FloatUtil.atan(halfHorizFovTan) * 2f;
142 return byRadians(horizontalFov, horizCenterFromLeft, verticalFov, vertCenterFromTop);
143 }
144
145 /**
146 * Returns this instance <i>in tangent</i> values.
147 * <p>
148 * If this instance is {@link #inTangents} already, method returns this instance,
149 * otherwise a newly created instance w/ converted values to tangent.
150 * </p>
151 */
152 public final FovHVHalves toTangents() {
153 if( inTangents ) {
154 return this;
155 } else {
157 }
158 }
159
160 /** Returns the full horizontal FOV, i.e. {@link #left} + {@link #right}, either in {@link #inTangents} or radians. */
161 public final float horzFov() { return left+right; }
162
163 /** Returns the full vertical FOV, i.e. {@link #top} + {@link #bottom}, either in {@link #inTangents} or radians. */
164 public final float vertFov() { return top+bottom; }
165
166 public final String toString() {
167 return "FovHVH["+(inTangents?"tangents":"radians")+": "+left+" l, "+right+" r, "+top+" t, "+bottom+" b]";
168 }
169 public final String toStringInDegrees() {
170 final float f = 180.0f / FloatUtil.PI;
171 final String storedAs = inTangents?"tangents":"radians";
172 if( inTangents ) {
173 final float aleft = FloatUtil.atan(left);
174 final float aright = FloatUtil.atan(right);
175 final float atop = FloatUtil.atan(top);
176 final float abottom = FloatUtil.atan(bottom);
177 return "FovHVH[degrees: "+aleft*f+" l, "+aright*f+" r, "+atop*f+" t, "+abottom*f+" b, stored-as: "+storedAs+"]";
178 } else {
179 return "FovHVH[degrees: "+left*f+" l, "+right*f+" r, "+top*f+" t, "+bottom*f+" b, stored-as: "+storedAs+"]";
180 }
181 }
182}
Basic Float math utility functions.
Definition: FloatUtil.java:83
static float atan(final float a)
static final float PI
The value PI, i.e.
static float tan(final float a)
Horizontal and vertical field of view (FOV) halves, allowing a non-centered projection.
final float left
Half horizontal FOV from center to left, either in inTangents or radians.
static FovHVHalves byFovyRadianAndAspect(final float verticalFov, final float aspect)
Returns a symmetrical centered FovHVHalves instance in inTangents, using:
final String toStringInDegrees()
final float right
Half horizontal FOV from center to right, either in inTangents or radians.
final float horzFov()
Returns the full horizontal FOV, i.e.
final FovHVHalves toTangents()
Returns this instance in tangent values.
final boolean inTangents
If true, values are in tangent, otherwise radians.
final float top
Half vertical FOV from center to top, either in inTangents or radians.
final float vertFov()
Returns the full vertical FOV, i.e.
static FovHVHalves byRadians(final float horizontalFov, final float horizCenterFromLeft, final float verticalFov, final float vertCenterFromTop)
Returns a custom symmetry FovHVHalves instance inTangents, using:
static FovHVHalves byRadians(final float horizontalFov, final float verticalFov)
Returns a symmetrical centered FovHVHalves instance in inTangents, using:
FovHVHalves(final float left, final float right, final float top, final float bottom, final boolean inTangents)
Constructor for one FovHVHalves instance.
final float bottom
Half vertical FOV from center to bottom, either in inTangents or radians.
static FovHVHalves byFovyRadianAndAspect(final float verticalFov, final float vertCenterFromTop, final float aspect, final float horizCenterFromLeft)
Returns a custom symmetry FovHVHalves instance inTangents, via computing the horizontalFov using: