├── .gitignore ├── AUTHORS.md ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── java6-android-glutils ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── org │ └── typhonrt │ └── android │ └── java6 │ └── opengl │ └── utils │ ├── AndroidGLES20Util.java │ ├── AndroidGLES30Util.java │ ├── AndroidGLES31Util.java │ ├── AndroidGLESUtil.java │ ├── FBOUtil.java │ ├── GLBuffer.java │ ├── GLBufferUtil.java │ └── GLSurfaceView2.java ├── java6-glutils-commons ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── org │ └── typhonrt │ └── commons │ └── java6 │ └── opengl │ └── utils │ ├── FBOData.java │ ├── IGLVersion.java │ ├── XeGLES2.java │ ├── XeGLES3.java │ └── XeGLUnknown.java ├── java6-math ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── org │ └── typhonrt │ └── java6 │ ├── math │ └── MathUtil.java │ └── vecmath │ ├── Matrix3f.java │ ├── Matrix4f.java │ ├── Quat4f.java │ ├── Tuple3f.java │ ├── Tuple4f.java │ ├── VecmathStrings.java │ ├── Vector3f.java │ └── Vector4f.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | ## Gradle 2 | .gradle 3 | /build 4 | 5 | ## Android Studio and Intellij and Android in general 6 | .idea 7 | *.ipr 8 | *.iws 9 | *.iml 10 | local.properties 11 | 12 | ## OS Specific 13 | .DS_Store -------------------------------------------------------------------------------- /AUTHORS.md: -------------------------------------------------------------------------------- 1 | ####Corporate Copyright Statements 2 | ----- 3 | 4 | Copyright (c) 2014-present TyphonRT Inc. [@typhonrt](https://github.com/typhonrt) 5 | 6 | ####Contributors 7 | ----- 8 | 9 | Michael Leahy [@typhonrt](https://github.com/typhonrt) 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Logo](http://i.imgur.com/DlDe2mP.png "Logo") 2 | ============================ 3 | 4 | Latest Updates 5 | ======== 6 | (4.10.16): Update for Android Studio 2.x+ / moved repos to [modern-java-graphics](https://github.com/modern-java-graphics) organization. Moved Google Groups forum to modern-java-graphics. Lowered API level to 19 / Kitkat. Changed licenses for [java6-android-glframework](https://github.com/modern-java-graphics/java6-android-glframework) and [java6-android-demoframework](https://github.com/modern-java-graphics/java6-android-demoframework) to `MPL v2.0`. 7 | 8 | (1.1.15): I finally had a chance to do some refactoring this afternoon, so 9 | check out this post for more information. 10 | 11 | A Google web forum is available to discuss anything related to this modern OpenGL utility framework. If you have questions or comments about modern OpenGL with Java for Android check out the web forum and post your question. 12 | 13 | This repo provides a concise framework and helper utilities to leverage modern OpenGL/ES in Java for Android. The purpose is to inform developers, but also provide a testbed for cross-device testing between various mobile GPUs. This repo only contains utility code which can be used independently of the supplied demo code found in this repo java6-android-gldemos. The framework code provides concise utility code that makes working with OpenGL with Java easier and updates several utility classes like GLSurfaceView which has been "long in the tooth" in regard to the Android SDK utility code for OpenGL. 14 | 15 | Device requirements: Android 4.4+ / OpenGL ES 3.0+ 16 | 17 | IDE requirements: Android Studio 2.x+ / build tools: 23.0.3 18 | 19 | This framework requires the latest Android Studio 2.x+ developer setup. To install Android Studio please download it here 20 | 21 | For full source code installation instructions refer to the wiki / install guide. In time an expanded wiki will illuminate the full power of modern OpenGL with Java for Android. Please stay tuned as this effort is taken on by Michael Leahy, the author, in his spare time. 22 | 23 | If you found these demos useful consider checking out the TyphonRT Video Suite which is his next-gen photo / video engine technology for Android using OpenGL ES 3.0+ and hardware accelerated media encoding / decoding. Click the image below to check it out: 24 | TyphonRT Video Suite 26 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.0.0' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/modern-java-graphics/java6-android-glframework/1327362c33b8a38915f05de8b4b281e2085986c3/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /java6-android-glutils/.gitignore: -------------------------------------------------------------------------------- 1 | ## Gradle 2 | .gradle/ 3 | build/ 4 | 5 | ## Android Studio and Intellij and Android in general 6 | .idea/ 7 | *.ipr 8 | *.iws 9 | *.iml 10 | local.properties 11 | 12 | ## OS Specific 13 | .DS_Store -------------------------------------------------------------------------------- /java6-android-glutils/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion '23.0.3' 6 | 7 | defaultConfig { 8 | minSdkVersion 19 9 | targetSdkVersion 21 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(dir: 'libs', include: ['*.jar']) 23 | compile project(':java6-glutils-commons') 24 | compile project(':java6-math') 25 | } 26 | -------------------------------------------------------------------------------- /java6-android-glutils/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Volumes/Data/program/SDK/android-sdk-macosx/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /java6-android-glutils/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /java6-android-glutils/src/main/java/org/typhonrt/android/java6/opengl/utils/AndroidGLES20Util.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.android.java6.opengl.utils; 9 | 10 | import android.content.res.Resources; 11 | import android.graphics.Bitmap; 12 | import android.graphics.BitmapFactory; 13 | import android.graphics.Matrix; 14 | import android.opengl.GLUtils; 15 | import android.util.Log; 16 | 17 | import java.io.BufferedReader; 18 | import java.io.IOException; 19 | import java.io.InputStreamReader; 20 | 21 | import static android.opengl.GLES20.*; 22 | 23 | public class AndroidGLES20Util extends AndroidGLESUtil 24 | { 25 | protected AndroidGLES20Util() {} 26 | 27 | private static final String s_LOG_TAG = "AndroidGLES20Util"; 28 | 29 | private static final ThreadLocal s_BUILD_PROGRAM_STATUS = new ThreadLocal(); 30 | private static final ThreadLocal s_BUILD_SHADER_STATUS = new ThreadLocal(); 31 | private static final ThreadLocal s_CREATE_BUFFER_ID = new ThreadLocal(); 32 | private static final ThreadLocal s_CREATE_FRAMEBUFFER_ID = new ThreadLocal(); 33 | private static final ThreadLocal s_CREATE_TEXTURE_ID = new ThreadLocal(); 34 | private static final ThreadLocal s_CURRENT_FRAMEBUFFER_ID = new ThreadLocal(); 35 | private static final ThreadLocal s_DELETE_BUFFER_ID = new ThreadLocal(); 36 | private static final ThreadLocal s_DELETE_FRAMEBUFFER_ID = new ThreadLocal(); 37 | private static final ThreadLocal s_DELETE_TEXTURE_ID = new ThreadLocal(); 38 | private static final ThreadLocal s_LOAD_TEXTURE_ID = new ThreadLocal(); 39 | private static final ThreadLocal s_GET_INTEGERV_ID = new ThreadLocal(); 40 | private static final ThreadLocal s_GET_PROGRAM_ID = new ThreadLocal(); 41 | 42 | public static int buildProgramFromAssets(Resources resources, String shaderFileName, int shaderType) 43 | { 44 | String shaderSource = loadFromAssets(resources, shaderFileName); 45 | 46 | return buildProgram(shaderSource, shaderType); 47 | } 48 | 49 | public static int buildProgramFromAssets(Resources resources, String shaderFileName, int shaderType, 50 | String shaderHeader) 51 | { 52 | String shaderSource = loadFromAssets(resources, shaderFileName); 53 | 54 | return buildProgram(shaderHeader + shaderSource, shaderType); 55 | } 56 | 57 | public static int buildProgramFromAssets(Resources resources, String vertexFileName, String fragmentFileName) 58 | { 59 | String vertexSource = loadFromAssets(resources, vertexFileName); 60 | String fragmentSource = loadFromAssets(resources, fragmentFileName); 61 | 62 | return buildProgram(vertexSource, fragmentSource); 63 | } 64 | 65 | public static int buildProgram(String shaderSource, int shaderType) 66 | { 67 | int computeShader = buildShader(shaderSource, shaderType); 68 | if (computeShader == 0) return 0; 69 | 70 | int program = glCreateProgram(); 71 | glAttachShader(program, computeShader); 72 | 73 | glLinkProgram(program); 74 | 75 | int[] status = s_BUILD_PROGRAM_STATUS.get(); 76 | if (status == null) 77 | { 78 | status = new int[1]; 79 | s_BUILD_PROGRAM_STATUS.set(status); 80 | } 81 | 82 | glGetProgramiv(program, GL_LINK_STATUS, status, 0); 83 | if (status[0] != GL_TRUE) 84 | { 85 | String error = glGetProgramInfoLog(program); 86 | Log.d(s_LOG_TAG, "Error while linking program:\n" + error); 87 | glDeleteShader(computeShader); 88 | glDeleteProgram(program); 89 | return 0; 90 | } 91 | 92 | return program; 93 | } 94 | 95 | public static int buildProgram(String vertexSource, String fragmentSource) 96 | { 97 | int vertexShader = buildShader(vertexSource, GL_VERTEX_SHADER); 98 | if (vertexShader == 0) return 0; 99 | 100 | int fragmentShader = buildShader(fragmentSource, GL_FRAGMENT_SHADER); 101 | if (fragmentShader == 0) return 0; 102 | 103 | int program = glCreateProgram(); 104 | glAttachShader(program, vertexShader); 105 | glAttachShader(program, fragmentShader); 106 | 107 | glLinkProgram(program); 108 | 109 | int[] status = s_BUILD_PROGRAM_STATUS.get(); 110 | if (status == null) 111 | { 112 | status = new int[1]; 113 | s_BUILD_PROGRAM_STATUS.set(status); 114 | } 115 | 116 | glGetProgramiv(program, GL_LINK_STATUS, status, 0); 117 | if (status[0] != GL_TRUE) 118 | { 119 | String error = glGetProgramInfoLog(program); 120 | Log.d(s_LOG_TAG, "Error while linking program:\n" +error); 121 | glDeleteShader(vertexShader); 122 | glDeleteShader(fragmentShader); 123 | glDeleteProgram(program); 124 | return 0; 125 | } 126 | 127 | return program; 128 | } 129 | 130 | public static int buildShader(String source, int type) 131 | { 132 | int shader = glCreateShader(type); 133 | 134 | glShaderSource(shader, source); 135 | 136 | glCompileShader(shader); 137 | 138 | int[] status = s_BUILD_SHADER_STATUS.get(); 139 | if (status == null) 140 | { 141 | status = new int[1]; 142 | s_BUILD_SHADER_STATUS.set(status); 143 | } 144 | 145 | glGetShaderiv(shader, GL_COMPILE_STATUS, status, 0); 146 | if (status[0] != GL_TRUE) 147 | { 148 | String error = glGetShaderInfoLog(shader); 149 | Log.d(s_LOG_TAG, "Error while compiling shader:\n" +error); 150 | glDeleteShader(shader); 151 | return 0; 152 | } 153 | 154 | return shader; 155 | } 156 | 157 | public static int checkGlError() 158 | { 159 | return checkGlError(false); 160 | } 161 | 162 | public static int checkGlError(boolean throwException) 163 | { 164 | int error = glGetError(); 165 | if (error != GL_NO_ERROR) 166 | { 167 | Log.d(s_LOG_TAG, "GL error = 0x" +Integer.toHexString(error)); 168 | if (throwException) 169 | { 170 | throw new RuntimeException("GL ERROR = 0x" + Integer.toHexString(error)); 171 | } 172 | } 173 | 174 | return error; 175 | } 176 | 177 | /** 178 | * Create a new texture and set it up 179 | */ 180 | public static int createTexture(int internalFormat, int width, int height) 181 | { 182 | int[] texture = s_CREATE_TEXTURE_ID.get(); 183 | if (texture == null) 184 | { 185 | texture = new int[1]; 186 | s_CREATE_TEXTURE_ID.set(texture); 187 | } 188 | 189 | glGenTextures(1, texture, 0); 190 | glBindTexture(GL_TEXTURE_2D, texture[0]); 191 | 192 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 193 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 194 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 195 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 196 | 197 | glBindTexture(GL_TEXTURE_2D, 0); 198 | 199 | return texture[0]; 200 | } 201 | 202 | public static boolean checkFrameBufferStatus() 203 | { 204 | int status = glCheckFramebufferStatus(GL_FRAMEBUFFER); 205 | 206 | return status == GL_NO_ERROR || status == GL_FRAMEBUFFER_COMPLETE; 207 | } 208 | 209 | public static int createBuffer() 210 | { 211 | int[] buffers = s_CREATE_BUFFER_ID.get(); 212 | if (buffers == null) 213 | { 214 | buffers = new int[1]; 215 | s_CREATE_BUFFER_ID.set(buffers); 216 | } 217 | 218 | glGenBuffers(1, buffers, 0); 219 | return buffers[0]; 220 | } 221 | 222 | public static int createFrameBuffer() 223 | { 224 | int[] framebuffer = s_CREATE_FRAMEBUFFER_ID.get(); 225 | if (framebuffer == null) 226 | { 227 | framebuffer = new int[1]; 228 | s_CREATE_FRAMEBUFFER_ID.set(framebuffer); 229 | } 230 | 231 | glGenFramebuffers(1, framebuffer, 0); 232 | 233 | return framebuffer[0]; 234 | } 235 | 236 | public static void deleteBuffer(int bufferID) 237 | { 238 | int[] buffer = s_DELETE_BUFFER_ID.get(); 239 | if (buffer == null) 240 | { 241 | buffer = new int[1]; 242 | s_DELETE_BUFFER_ID.set(buffer); 243 | } 244 | 245 | buffer[0] = bufferID; 246 | 247 | glDeleteBuffers(1, buffer, 0); 248 | } 249 | 250 | public static void deleteFrameBuffer(int frameBufferID) 251 | { 252 | int[] frameBuffer = s_DELETE_FRAMEBUFFER_ID.get(); 253 | if (frameBuffer == null) 254 | { 255 | frameBuffer = new int[1]; 256 | s_DELETE_FRAMEBUFFER_ID.set(frameBuffer); 257 | } 258 | 259 | frameBuffer[0] = frameBufferID; 260 | 261 | glDeleteFramebuffers(1, frameBuffer, 0); 262 | } 263 | 264 | /** 265 | * Delete the texture by pointer reference 266 | */ 267 | public static void deleteTexture(int textureID) 268 | { 269 | int[] texture = s_DELETE_TEXTURE_ID.get(); 270 | if (texture == null) 271 | { 272 | texture = new int[1]; 273 | s_DELETE_TEXTURE_ID.set(texture); 274 | } 275 | 276 | texture[0] = textureID; 277 | 278 | glDeleteTextures(1, texture, 0); 279 | } 280 | 281 | public static int getCurrentFrameBufferTarget() 282 | { 283 | int[] frameBuffer = s_CURRENT_FRAMEBUFFER_ID.get(); 284 | if (frameBuffer == null) 285 | { 286 | frameBuffer = new int[1]; 287 | s_CURRENT_FRAMEBUFFER_ID.set(frameBuffer); 288 | } 289 | 290 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, frameBuffer, 0); 291 | 292 | return frameBuffer[0]; 293 | } 294 | 295 | public static int getIntegerv(int pName) 296 | { 297 | int value[] = s_GET_INTEGERV_ID.get(); 298 | if (value == null) 299 | { 300 | value = new int[1]; 301 | s_GET_INTEGERV_ID.set(value); 302 | } 303 | 304 | glGetIntegerv(pName, value, 0); 305 | 306 | return value[0]; 307 | } 308 | 309 | public static int getProgramiv(int programID, int pName) 310 | { 311 | int program[] = s_GET_PROGRAM_ID.get(); 312 | if (program == null) 313 | { 314 | program = new int[1]; 315 | s_GET_PROGRAM_ID.set(program); 316 | } 317 | 318 | glGetProgramiv(programID, pName, program, 0); 319 | 320 | return program[0]; 321 | } 322 | 323 | private static String loadFromAssets(Resources resources, String fileName) 324 | { 325 | BufferedReader reader = null; 326 | StringBuilder sb = new StringBuilder(); 327 | 328 | try 329 | { 330 | reader = new BufferedReader(new InputStreamReader(resources.getAssets().open(fileName), "UTF-8")); 331 | 332 | // do reading, usually loop until end of file reading 333 | String mLine = reader.readLine(); 334 | while (mLine != null) 335 | { 336 | sb.append(mLine).append('\n'); 337 | mLine = reader.readLine(); 338 | } 339 | } 340 | catch (IOException e) 341 | { 342 | //log the exception 343 | } 344 | finally 345 | { 346 | if (reader != null) 347 | { 348 | try 349 | { 350 | reader.close(); 351 | } 352 | catch (IOException e) 353 | { 354 | //log the exception 355 | } 356 | } 357 | } 358 | 359 | return sb.toString(); 360 | } 361 | 362 | public static int loadTexture(Resources resources, int resource, int internalFormat, boolean flip) 363 | { 364 | int[] textures = s_LOAD_TEXTURE_ID.get(); 365 | if (textures == null) 366 | { 367 | textures = new int[1]; 368 | s_LOAD_TEXTURE_ID.set(textures); 369 | } 370 | 371 | glActiveTexture(GL_TEXTURE0); 372 | glGenTextures(1, textures, 0); 373 | 374 | int texture = textures[0]; 375 | glBindTexture(GL_TEXTURE_2D, texture); 376 | 377 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 378 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 379 | 380 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 381 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 382 | 383 | Bitmap bitmap = BitmapFactory.decodeResource(resources, resource); 384 | 385 | final int width = bitmap.getWidth(); 386 | final int height = bitmap.getHeight(); 387 | 388 | if (flip) 389 | { 390 | Matrix matrix = new Matrix(); 391 | matrix.setScale(1, -1); 392 | matrix.postTranslate(0, height); 393 | Bitmap flipBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 394 | 395 | bitmap.recycle(); 396 | bitmap = flipBitmap; 397 | } 398 | 399 | GLUtils.texImage2D(GL_TEXTURE_2D, 0, internalFormat, bitmap, GL_UNSIGNED_BYTE, 0); 400 | 401 | bitmap.recycle(); 402 | 403 | glBindTexture(GL_TEXTURE_2D, 0); 404 | 405 | return texture; 406 | } 407 | } -------------------------------------------------------------------------------- /java6-android-glutils/src/main/java/org/typhonrt/android/java6/opengl/utils/AndroidGLES30Util.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.android.java6.opengl.utils; 9 | 10 | import android.content.res.Resources; 11 | import android.graphics.Bitmap; 12 | import android.graphics.BitmapFactory; 13 | import android.graphics.Matrix; 14 | import android.opengl.GLUtils; 15 | 16 | import static android.opengl.GLES30.*; 17 | 18 | public class AndroidGLES30Util extends AndroidGLES20Util 19 | { 20 | protected AndroidGLES30Util() {} 21 | 22 | private static final ThreadLocal s_CREATE_TEXTURE_ID = new ThreadLocal(); 23 | private static final ThreadLocal s_CREATE_SAMPLER_ID = new ThreadLocal(); 24 | private static final ThreadLocal s_CREATE_VERTEX_ARRAY_ID = new ThreadLocal(); 25 | private static final ThreadLocal s_GET_INTERGER_IV = new ThreadLocal(); 26 | private static final ThreadLocal s_LOAD_TEXTURE_ID = new ThreadLocal(); 27 | 28 | /** 29 | * Create a new texture and set it up. The main difference with AndroidGLES30Util.createTexture is that it also 30 | * specifically sets the internal format with glTexStorage2D which is necessary to use this texture with compute 31 | * shaders. 32 | */ 33 | public static int createTexture(int internalFormat, int width, int height) 34 | { 35 | int texture[] = s_CREATE_TEXTURE_ID.get(); 36 | if (texture == null) 37 | { 38 | texture = new int[1]; 39 | s_CREATE_TEXTURE_ID.set(texture); 40 | } 41 | 42 | glGenTextures(1, texture, 0); 43 | glBindTexture(GL_TEXTURE_2D, texture[0]); 44 | 45 | glTexStorage2D(GL_TEXTURE_2D, 1, internalFormat, width, height); 46 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 47 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 48 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 49 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 50 | 51 | glBindTexture(GL_TEXTURE_2D, 0); 52 | 53 | return texture[0]; 54 | } 55 | 56 | public static int createSampler() 57 | { 58 | int sampler[] = s_CREATE_SAMPLER_ID.get(); 59 | if (sampler == null) 60 | { 61 | sampler = new int[1]; 62 | s_CREATE_SAMPLER_ID.set(sampler); 63 | } 64 | 65 | glGenSamplers(1, sampler, 0); 66 | return sampler[0]; 67 | } 68 | 69 | public static int createVertexArray() 70 | { 71 | int vao[] = s_CREATE_VERTEX_ARRAY_ID.get(); 72 | if (vao == null) 73 | { 74 | vao = new int[1]; 75 | s_CREATE_VERTEX_ARRAY_ID.set(vao); 76 | } 77 | 78 | glGenVertexArrays(1, vao, 0); 79 | return vao[0]; 80 | } 81 | 82 | public static int getGetIntegeri_v(int target, int index) 83 | { 84 | int value[] = s_GET_INTERGER_IV.get(); 85 | if (value == null) 86 | { 87 | value = new int[1]; 88 | s_GET_INTERGER_IV.set(value); 89 | } 90 | 91 | glGetIntegeri_v(target, index, value, 0); 92 | 93 | return value[0]; 94 | } 95 | 96 | /** 97 | * Loads a texture from an Android drawable resource performing any flipping as given. The main difference with 98 | * AndroidGLES30Util.loadTexture is that it also specifically sets the internal format with glTexStorage2D which 99 | * is necessary to use this texture with compute shaders. 100 | * 101 | * @param resources 102 | * @param resource 103 | * @param flip 104 | * 105 | * @return 106 | */ 107 | public static int loadTexture(Resources resources, int resource, int internalFormat, boolean flip) 108 | { 109 | int[] textures = s_LOAD_TEXTURE_ID.get(); 110 | if (textures == null) 111 | { 112 | textures = new int[1]; 113 | s_LOAD_TEXTURE_ID.set(textures); 114 | } 115 | 116 | glActiveTexture(GL_TEXTURE0); 117 | glGenTextures(1, textures, 0); 118 | 119 | int texture = textures[0]; 120 | glBindTexture(GL_TEXTURE_2D, texture); 121 | 122 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 123 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 124 | 125 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 126 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 127 | 128 | Bitmap bitmap = BitmapFactory.decodeResource(resources, resource); 129 | 130 | final int width = bitmap.getWidth(); 131 | final int height = bitmap.getHeight(); 132 | 133 | if (flip) 134 | { 135 | Matrix matrix = new Matrix(); 136 | matrix.setScale(1, -1); 137 | matrix.postTranslate(0, height); 138 | Bitmap flipBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 139 | 140 | bitmap.recycle(); 141 | bitmap = flipBitmap; 142 | } 143 | 144 | // It turns out we can still use the convenience method "GLUtils.texSubImage2D" below, but for compute shaders 145 | // it is important to make an explicit call to glTexStorage2D with an internal format of GL_RGBA8. 146 | glTexStorage2D(GL_TEXTURE_2D, 1, internalFormat, width, height); 147 | checkGlError(); 148 | 149 | GLUtils.texSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap); 150 | checkGlError(); 151 | 152 | bitmap.recycle(); 153 | 154 | glBindTexture(GL_TEXTURE_2D, 0); 155 | 156 | return texture; 157 | } 158 | } -------------------------------------------------------------------------------- /java6-android-glutils/src/main/java/org/typhonrt/android/java6/opengl/utils/AndroidGLES31Util.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.android.java6.opengl.utils; 9 | 10 | import org.typhonrt.java6.math.MathUtil; 11 | 12 | import static android.opengl.GLES31.*; 13 | 14 | public class AndroidGLES31Util extends AndroidGLES30Util 15 | { 16 | protected AndroidGLES31Util() {} 17 | 18 | public static final String s_STR_AEP_EXT = "ANDROID_extension_pack_es31a"; 19 | 20 | private static final ThreadLocal s_CREATE_PROGRAM_PIPELINE_ID = new ThreadLocal(); 21 | private static final ThreadLocal s_GET_PROGRAM_PIPELINE_ID = new ThreadLocal(); 22 | 23 | /** 24 | * 25 | */ 26 | public static int createProgramPipeline() 27 | { 28 | int pipeline[] = s_CREATE_PROGRAM_PIPELINE_ID.get(); 29 | if (pipeline == null) 30 | { 31 | pipeline = new int[1]; 32 | s_CREATE_PROGRAM_PIPELINE_ID.set(pipeline); 33 | } 34 | 35 | glGenProgramPipelines(1, pipeline, 0); 36 | 37 | return pipeline[0]; 38 | } 39 | 40 | /** 41 | * Returns the max equal power workgroup size that is equal or below GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS. This 42 | * value can be used to dynamically set the proper layout sizes for compute shaders given the supported hardware 43 | * configuration. 44 | * 45 | * @param dimensions specifies the dimensions to calculate [1-3] 46 | * @return int 47 | */ 48 | public static int getMaxComputePowerWorkGroupSize(int dimensions) 49 | { 50 | if (dimensions < 1 || dimensions > 3) 51 | { 52 | throw new IllegalArgumentException("Error: getMaxComputePowerWorkGroupSize requires dimension [1-3]."); 53 | } 54 | 55 | int maxInv = getIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS); 56 | 57 | int power = 4; 58 | int nextPower = 4; 59 | int total = power; 60 | 61 | while (total <= maxInv) 62 | { 63 | power = nextPower; 64 | nextPower = MathUtil.nextPow2(power + 1); 65 | total = (int)Math.pow(nextPower, dimensions); 66 | } 67 | 68 | return power; 69 | } 70 | 71 | public static int getProgramPipelineiv(int programID, int pName) 72 | { 73 | int pipeline[] = s_GET_PROGRAM_PIPELINE_ID.get(); 74 | if (pipeline == null) 75 | { 76 | pipeline = new int[1]; 77 | s_GET_PROGRAM_PIPELINE_ID.set(pipeline); 78 | } 79 | 80 | glGetProgramPipelineiv(programID, pName, pipeline, 0); 81 | 82 | return pipeline[0]; 83 | } 84 | } -------------------------------------------------------------------------------- /java6-android-glutils/src/main/java/org/typhonrt/android/java6/opengl/utils/AndroidGLESUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.android.java6.opengl.utils; 9 | 10 | import android.app.ActivityManager; 11 | import android.content.Context; 12 | import android.content.pm.ConfigurationInfo; 13 | import android.opengl.GLES20; 14 | 15 | import org.typhonrt.commons.java6.opengl.utils.IGLVersion; 16 | import org.typhonrt.commons.java6.opengl.utils.XeGLES2; 17 | import org.typhonrt.commons.java6.opengl.utils.XeGLES3; 18 | import org.typhonrt.commons.java6.opengl.utils.XeGLUnknown; 19 | 20 | /** 21 | * AndroidGLESUtil 22 | * 23 | * Note: Ideally the methods below should be implemented via composition so that version checks can be upgraded 24 | * dynamically. The TyphonRT middleware enables this pattern. 25 | */ 26 | public class AndroidGLESUtil 27 | { 28 | protected AndroidGLESUtil() {} 29 | 30 | public static IGLVersion getGLVersion(Context context) 31 | { 32 | IGLVersion glVersion = XeGLUnknown.GL_UNKNOWN; 33 | 34 | final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 35 | final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo(); 36 | 37 | String glesVersionString = configurationInfo.getGlEsVersion(); 38 | 39 | // This is messy... 40 | if (XeGLES2.GLES2_0.versionString.equals(glesVersionString)) 41 | { 42 | glVersion = XeGLES2.GLES2_0; 43 | } 44 | else if (XeGLES3.GLES3_0.versionString.equals(glesVersionString)) 45 | { 46 | glVersion = XeGLES3.GLES3_0; 47 | } 48 | else if (XeGLES3.GLES3_1.versionString.equals(glesVersionString)) 49 | { 50 | glVersion = XeGLES3.GLES3_1; 51 | } 52 | 53 | return glVersion; 54 | } 55 | 56 | /** 57 | * Returns appropriate IGLVersion for major / minor version numbers. 58 | * 59 | * @param majorVersion 60 | * @param minorVersion 61 | * 62 | * @return IGLVersion 63 | */ 64 | public static IGLVersion getGLVersion(int majorVersion, int minorVersion) 65 | { 66 | IGLVersion glVersion = XeGLUnknown.GL_UNKNOWN; 67 | 68 | switch (majorVersion) 69 | { 70 | case 2: 71 | glVersion = XeGLES2.GLES2_0; 72 | break; 73 | 74 | case 3: 75 | if (minorVersion == 0) 76 | { 77 | glVersion = XeGLES3.GLES3_0; 78 | } 79 | else if (minorVersion == 1) 80 | { 81 | glVersion = XeGLES3.GLES3_1; 82 | } 83 | break; 84 | } 85 | 86 | return glVersion; 87 | } 88 | 89 | /** 90 | * Checks the GL_EXTENSIONS String for the given "extension". This method must only be called when a valid context 91 | * and surface is valid. 92 | * 93 | * @param extension 94 | * @return boolean 95 | */ 96 | public static boolean hasExtension(String extension) 97 | { 98 | String extensions = GLES20.glGetString(GLES20.GL_EXTENSIONS); 99 | 100 | boolean foundExtension = false; 101 | 102 | if (extensions != null) 103 | { 104 | foundExtension = extensions.toLowerCase().contains(extension.toLowerCase()); 105 | } 106 | 107 | return foundExtension; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /java6-android-glutils/src/main/java/org/typhonrt/android/java6/opengl/utils/FBOUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.android.java6.opengl.utils; 9 | 10 | import org.typhonrt.commons.java6.opengl.utils.FBOData; 11 | 12 | import static android.opengl.GLES20.*; 13 | 14 | /** 15 | * FBOUtil 16 | */ 17 | public class FBOUtil 18 | { 19 | public static FBOData initialize(FBOData fbo) 20 | { 21 | return initialize(fbo, fbo.width, fbo.height); 22 | } 23 | 24 | public static FBOData initialize(FBOData fbo, int width, int height) 25 | { 26 | return initialize(fbo, width, height, GL_RGBA); 27 | } 28 | 29 | public static FBOData initialize(FBOData fbo, int width, int height, int format) 30 | { 31 | fbo.width = width; 32 | fbo.height = height; 33 | 34 | fbo.bufferID = AndroidGLES20Util.createFrameBuffer(); 35 | 36 | glBindFramebuffer(GL_FRAMEBUFFER, fbo.bufferID); 37 | AndroidGLES20Util.checkGlError(); 38 | 39 | fbo.textureID = AndroidGLES20Util.createTexture(format, width, height); 40 | 41 | glBindTexture(GL_TEXTURE_2D, fbo.textureID); 42 | 43 | glTexImage2D(GL_TEXTURE_2D, 0, format, fbo.width, fbo.height, 0, format, GL_UNSIGNED_BYTE, null); 44 | 45 | AndroidGLES20Util.checkGlError(); 46 | 47 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo.textureID, 0); 48 | 49 | glBindFramebuffer(GL_FRAMEBUFFER, 0); 50 | 51 | glBindTexture(GL_TEXTURE_2D, 0); 52 | 53 | return fbo; 54 | } 55 | 56 | public static void makeCurrentRenderTarget(FBOData fbo) 57 | { 58 | glBindFramebuffer(GL_FRAMEBUFFER, fbo.bufferID); 59 | } 60 | 61 | public static void makeCurrentAndClear(FBOData fbo) 62 | { 63 | glBindFramebuffer(GL_FRAMEBUFFER, fbo.bufferID); 64 | 65 | glClearColor(0, 0, 0, 0); 66 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 67 | } 68 | 69 | public static boolean isValid(FBOData fbo) 70 | { 71 | return fbo.textureID != 0 && fbo.bufferID != 0; 72 | } 73 | 74 | public static void release(FBOData fbo) 75 | { 76 | if (fbo.textureID != 0) 77 | { 78 | AndroidGLES20Util.deleteTexture(fbo.textureID); 79 | } 80 | 81 | if (fbo.bufferID != 0) 82 | { 83 | AndroidGLES20Util.deleteFrameBuffer(fbo.bufferID); 84 | } 85 | 86 | fbo.textureID = fbo.bufferID = 0; 87 | } 88 | 89 | public static void releaseAsRenderTarget() 90 | { 91 | glBindFramebuffer(GL_FRAMEBUFFER, 0); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /java6-android-glutils/src/main/java/org/typhonrt/android/java6/opengl/utils/GLBuffer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.android.java6.opengl.utils; 9 | 10 | import java.nio.ByteBuffer; 11 | import java.nio.ByteOrder; 12 | 13 | import static android.opengl.GLES30.*; 14 | 15 | /** 16 | * GLBuffer 17 | * 18 | * target - type of buffer; IE GL_ARRAY_BUFFER, GL_SHADER_STORAGE_BUFFER, etc. 19 | * size - size in unit blocks of data 20 | * stride - size of bytes per unit block of data 21 | */ 22 | public final class GLBuffer 23 | { 24 | private int targetType; 25 | 26 | private int size; 27 | private int stride; 28 | 29 | private int bufferID; 30 | 31 | public GLBuffer(int targetType, int size, int stride) 32 | { 33 | this.targetType = targetType; 34 | this.size = size; 35 | this.stride = stride; 36 | 37 | bufferID = AndroidGLES20Util.createBuffer(); 38 | 39 | bind(); 40 | 41 | glBufferData(targetType, size * stride, null, GL_STATIC_DRAW); 42 | 43 | unbind(); 44 | } 45 | 46 | public GLBuffer bind() 47 | { 48 | glBindBuffer(targetType, bufferID); 49 | 50 | return this; 51 | } 52 | 53 | public void dispose() 54 | { 55 | AndroidGLES20Util.deleteBuffer(bufferID); 56 | } 57 | 58 | public GLBuffer unbind() 59 | { 60 | glBindBuffer(targetType, 0); 61 | 62 | return this; 63 | } 64 | 65 | // GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT 66 | public ByteBuffer map(int accessBitfield) 67 | { 68 | bind(); 69 | 70 | ByteBuffer b = (ByteBuffer)glMapBufferRange(targetType, 0, size * stride, accessBitfield); 71 | b.order(ByteOrder.nativeOrder()); 72 | 73 | return b; 74 | } 75 | 76 | public GLBuffer unmap() 77 | { 78 | bind(); 79 | glUnmapBuffer(targetType); 80 | 81 | return this; 82 | } 83 | 84 | public int getBufferID() 85 | { 86 | return bufferID; 87 | } 88 | 89 | public int getSize() 90 | { 91 | return size; 92 | } 93 | 94 | public int getStride() { return stride; } 95 | 96 | private static final String s_STR_COLON = ": "; 97 | 98 | public void debugPrint() 99 | { 100 | ByteBuffer buffer = map(GL_MAP_READ_BIT); 101 | 102 | for (int cntr = buffer.position(), length = buffer.remaining(); cntr < length; cntr++) 103 | { 104 | System.err.println(cntr +s_STR_COLON +buffer.get(cntr)); 105 | } 106 | 107 | unmap(); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /java6-android-glutils/src/main/java/org/typhonrt/android/java6/opengl/utils/GLBufferUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.android.java6.opengl.utils; 9 | 10 | import org.typhonrt.java6.math.MathUtil; 11 | 12 | import java.nio.ByteBuffer; 13 | 14 | import static android.opengl.GLES30.*; 15 | 16 | /** 17 | * GLBufferUtil 18 | */ 19 | public final class GLBufferUtil 20 | { 21 | private GLBufferUtil() {} 22 | 23 | public static final int s_QUAD_BUFFER_STRIDE = 5 * MathUtil.s_FLOAT_SIZE_BYTES; // x, y, z, u, v; buffer stride in bytes 24 | public static final int s_QUAD_UV_OFFSET = 3 * MathUtil.s_FLOAT_SIZE_BYTES; // offset to u/v in bytes 25 | 26 | public static GLBuffer createQuadVertexUVBuffer(float aspectRatio) 27 | { 28 | GLBuffer buffer = new GLBuffer(GL_ARRAY_BUFFER, 4, s_QUAD_BUFFER_STRIDE); 29 | 30 | ByteBuffer b = buffer.map(GL_MAP_WRITE_BIT); 31 | 32 | b.putFloat(aspectRatio).putFloat(-1f).putFloat(0f).putFloat(1f).putFloat(0f); 33 | b.putFloat(-aspectRatio).putFloat(-1f).putFloat(0f).putFloat(0f).putFloat(0f); 34 | b.putFloat(aspectRatio).putFloat(1f).putFloat(0f).putFloat(1f).putFloat(1f); 35 | b.putFloat(-aspectRatio).putFloat(1f).putFloat(0f).putFloat(0f).putFloat(1f); 36 | 37 | b.flip(); 38 | 39 | buffer.unmap(); 40 | 41 | return buffer; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /java6-glutils-commons/.gitignore: -------------------------------------------------------------------------------- 1 | ## Gradle 2 | .gradle/ 3 | build/ 4 | 5 | ## Android Studio and Intellij and Android in general 6 | .idea/ 7 | *.ipr 8 | *.iws 9 | *.iml 10 | local.properties 11 | 12 | ## OS Specific 13 | .DS_Store -------------------------------------------------------------------------------- /java6-glutils-commons/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | dependencies { 4 | compile fileTree(dir: 'libs', include: ['*.jar']) 5 | } -------------------------------------------------------------------------------- /java6-glutils-commons/src/main/java/org/typhonrt/commons/java6/opengl/utils/FBOData.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.commons.java6.opengl.utils; 9 | 10 | /** 11 | * FBOData 12 | */ 13 | public class FBOData 14 | { 15 | public int textureID; 16 | public int bufferID; 17 | 18 | public int width; 19 | public int height; 20 | } 21 | -------------------------------------------------------------------------------- /java6-glutils-commons/src/main/java/org/typhonrt/commons/java6/opengl/utils/IGLVersion.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.commons.java6.opengl.utils; 9 | 10 | /** 11 | * IGLVersion 12 | */ 13 | public interface IGLVersion 14 | { 15 | public int getMajorVersion(); 16 | 17 | public int getMinorVersion(); 18 | 19 | public String getVersionString(); 20 | 21 | public boolean lessThan(IGLVersion glVersion); 22 | } 23 | -------------------------------------------------------------------------------- /java6-glutils-commons/src/main/java/org/typhonrt/commons/java6/opengl/utils/XeGLES2.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.commons.java6.opengl.utils; 9 | 10 | /** 11 | * XeGLES2 -- Provides an extensible enum implementation for OpenGL ES 2.0 version info. 12 | */ 13 | public enum XeGLES2 implements IGLVersion 14 | { 15 | GLES2_0(2, 0, "2.0"); 16 | 17 | public final int majorVersion, minorVersion; 18 | 19 | public final String versionString; 20 | 21 | private XeGLES2(int majorVersion, int minorVersion, String versionString) 22 | { 23 | this.majorVersion = majorVersion; 24 | this.minorVersion = minorVersion; 25 | this.versionString = versionString; 26 | } 27 | 28 | @Override 29 | public int getMajorVersion() 30 | { 31 | return majorVersion; 32 | } 33 | 34 | @Override 35 | public int getMinorVersion() 36 | { 37 | return minorVersion; 38 | } 39 | 40 | @Override 41 | public String getVersionString() 42 | { 43 | return versionString; 44 | } 45 | 46 | @Override 47 | public boolean lessThan(IGLVersion glVersion) 48 | { 49 | int thatGLMajorVersion = glVersion.getMajorVersion(); 50 | int thatGLMinorVersion = glVersion.getMinorVersion(); 51 | 52 | return majorVersion < thatGLMajorVersion || 53 | (majorVersion == thatGLMajorVersion && minorVersion < thatGLMinorVersion); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /java6-glutils-commons/src/main/java/org/typhonrt/commons/java6/opengl/utils/XeGLES3.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.commons.java6.opengl.utils; 9 | 10 | /** 11 | * XeGLES3 -- Provides an extensible enum implementation for OpenGL ES 3.x version info. 12 | */ 13 | public enum XeGLES3 implements IGLVersion 14 | { 15 | GLES3_0(3, 0, "3.0"), 16 | GLES3_1(3, 1, "3.1"), 17 | GLES3_2(3, 2, "3.2"); 18 | 19 | public final int majorVersion, minorVersion; 20 | 21 | public final String versionString; 22 | 23 | private XeGLES3(int majorVersion, int minorVersion, String versionString) 24 | { 25 | this.majorVersion = majorVersion; 26 | this.minorVersion = minorVersion; 27 | this.versionString = versionString; 28 | } 29 | 30 | @Override 31 | public int getMajorVersion() 32 | { 33 | return majorVersion; 34 | } 35 | 36 | @Override 37 | public int getMinorVersion() 38 | { 39 | return minorVersion; 40 | } 41 | 42 | @Override 43 | public String getVersionString() 44 | { 45 | return versionString; 46 | } 47 | 48 | @Override 49 | public boolean lessThan(IGLVersion glVersion) 50 | { 51 | int thatGLMajorVersion = glVersion.getMajorVersion(); 52 | int thatGLMinorVersion = glVersion.getMinorVersion(); 53 | 54 | return majorVersion < thatGLMajorVersion || 55 | (majorVersion == thatGLMajorVersion && minorVersion < thatGLMinorVersion); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /java6-glutils-commons/src/main/java/org/typhonrt/commons/java6/opengl/utils/XeGLUnknown.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.commons.java6.opengl.utils; 9 | 10 | /** 11 | * XeGLUnknown -- Provides an extensible enum implementation for unknown OpenGL ES version info. 12 | */ 13 | public enum XeGLUnknown implements IGLVersion 14 | { 15 | GL_UNKNOWN(0, 0, "Unknown"); 16 | 17 | public final int majorVersion, minorVersion; 18 | 19 | public final String versionString; 20 | 21 | private XeGLUnknown(int majorVersion, int minorVersion, String versionString) 22 | { 23 | this.majorVersion = majorVersion; 24 | this.minorVersion = minorVersion; 25 | this.versionString = versionString; 26 | } 27 | 28 | @Override 29 | public int getMajorVersion() 30 | { 31 | return majorVersion; 32 | } 33 | 34 | @Override 35 | public int getMinorVersion() 36 | { 37 | return minorVersion; 38 | } 39 | 40 | @Override 41 | public String getVersionString() 42 | { 43 | return versionString; 44 | } 45 | 46 | @Override 47 | public boolean lessThan(IGLVersion glVersion) 48 | { 49 | int thatGLMajorVersion = glVersion.getMajorVersion(); 50 | int thatGLMinorVersion = glVersion.getMinorVersion(); 51 | 52 | return majorVersion < thatGLMajorVersion || 53 | (majorVersion == thatGLMajorVersion && minorVersion < thatGLMinorVersion); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /java6-math/.gitignore: -------------------------------------------------------------------------------- 1 | ## Gradle 2 | .gradle/ 3 | build/ 4 | 5 | ## Android Studio and Intellij and Android in general 6 | .idea/ 7 | *.ipr 8 | *.iws 9 | *.iml 10 | local.properties 11 | 12 | ## OS Specific 13 | .DS_Store -------------------------------------------------------------------------------- /java6-math/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | dependencies { 4 | compile fileTree(dir: 'libs', include: ['*.jar']) 5 | } -------------------------------------------------------------------------------- /java6-math/src/main/java/org/typhonrt/java6/math/MathUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.java6.math; 9 | 10 | /** 11 | * MathUtil 12 | */ 13 | public final class MathUtil 14 | { 15 | public static final int s_FLOAT_SIZE_BYTES = Float.SIZE / 8; 16 | public static final int s_INTEGER_SIZE_BYTES = Integer.SIZE / 8; 17 | public static final int s_LONG_SIZE_BYTES = Long.SIZE / 8; 18 | 19 | public static final int s_VEC4F_SIZE_BYTES = s_FLOAT_SIZE_BYTES * 4; 20 | 21 | private static final int s_RAND_MAX = 64000; 22 | 23 | private MathUtil() 24 | { 25 | } 26 | 27 | public static byte randByte() 28 | { 29 | return (byte) ((int) (Math.random() * s_RAND_MAX) & 0xff); 30 | } 31 | 32 | public static float frand() 33 | { 34 | return (float) Math.random(); 35 | } 36 | 37 | public static float sfrand() 38 | { 39 | return frand() * 2.0f - 1.0f; 40 | } 41 | 42 | public static int nextPow2(int val) 43 | { 44 | val--; 45 | for (int i = 1; i < Integer.SIZE; i <<= 1) 46 | { 47 | val = val | val >> i; 48 | } 49 | return val + 1; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /java6-math/src/main/java/org/typhonrt/java6/vecmath/Matrix3f.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.java6.vecmath; 9 | 10 | /** 11 | * A single precision floating point 3 by 3 matrix. 12 | * 13 | * @author Michael Leahy 14 | */ 15 | public class Matrix3f 16 | { 17 | /** 18 | * The first element of the first row. 19 | */ 20 | public float m00; 21 | 22 | /** 23 | * The second element of the first row. 24 | */ 25 | public float m01; 26 | 27 | /** 28 | * third element of the first row. 29 | */ 30 | public float m02; 31 | 32 | /** 33 | * The first element of the second row. 34 | */ 35 | public float m10; 36 | 37 | /** 38 | * The second element of the second row. 39 | */ 40 | public float m11; 41 | 42 | /** 43 | * The third element of the second row. 44 | */ 45 | public float m12; 46 | 47 | /** 48 | * The first element of the third row. 49 | */ 50 | public float m20; 51 | 52 | /** 53 | * The second element of the third row. 54 | */ 55 | public float m21; 56 | 57 | /** 58 | * The third element of the third row. 59 | */ 60 | public float m22; 61 | 62 | /** 63 | * Constructs a new matrix with the same values as the Matrix3f parameter. 64 | * 65 | * @param m1 The source matrix. 66 | */ 67 | public Matrix3f(Matrix3f m1) 68 | { 69 | m00 = m1.m00; 70 | m01 = m1.m01; 71 | m02 = m1.m02; 72 | m10 = m1.m10; 73 | m11 = m1.m11; 74 | m12 = m1.m12; 75 | m20 = m1.m20; 76 | m21 = m1.m21; 77 | m22 = m1.m22; 78 | } 79 | 80 | /** 81 | * Constructs and initializes a Matrix3f to all zeros. 82 | */ 83 | public Matrix3f() 84 | { 85 | setZero(); 86 | } 87 | 88 | /** 89 | * Returns a string that contains the values of this Matrix3f. 90 | * 91 | * @return the String representation 92 | */ 93 | @Override 94 | public String toString() 95 | { 96 | StringBuilder sb = new StringBuilder(); 97 | 98 | sb.append(VecmathStrings.s_STR_OPEN).append(VecmathStrings.s_STR_LINE_SEPARATOR).append(VecmathStrings.s_STR_OPEN_INDENT).append(m00).append('\t').append( 99 | m01).append('\t').append(m02).append(VecmathStrings.s_STR_CLOSE).append(VecmathStrings.s_STR_LINE_SEPARATOR); 100 | 101 | sb.append(VecmathStrings.s_STR_OPEN_INDENT).append(m10).append('\t').append(m11).append('\t').append(m22).append( 102 | VecmathStrings.s_STR_CLOSE).append(VecmathStrings.s_STR_LINE_SEPARATOR); 103 | 104 | sb.append(VecmathStrings.s_STR_OPEN_INDENT).append(m20).append('\t').append(m21).append('\t').append(m22).append( 105 | VecmathStrings.s_STR_DOUBLE_CLOSE); 106 | 107 | return sb.toString(); 108 | } 109 | 110 | /** 111 | * Sets this Matrix3f to identity. 112 | */ 113 | public final void setIdentity() 114 | { 115 | m00 = 1.0f; 116 | m01 = 0.0f; 117 | m02 = 0.0f; 118 | m10 = 0.0f; 119 | m11 = 1.0f; 120 | m12 = 0.0f; 121 | m20 = 0.0f; 122 | m21 = 0.0f; 123 | m22 = 1.0f; 124 | } 125 | 126 | /** 127 | * Sets the scale component of the current matrix by factoring out the 128 | * current scale (by doing an SVD) from the rotational component and 129 | * multiplying by the new scale. 130 | * 131 | * @param scale the new scale amount 132 | */ 133 | public final void setScale(float scale) 134 | { 135 | SVD(this); 136 | mul(scale); 137 | } 138 | 139 | /** 140 | * Sets the specified element of this matrix3d to the value provided. 141 | * 142 | * @param row the row number to be modified (zero indexed) 143 | * @param column the column number to be modified (zero indexed) 144 | * @param value the new value 145 | */ 146 | public final void setElement(int row, int column, float value) 147 | { 148 | if (row == 0) 149 | { 150 | if (column == 0) 151 | { 152 | m00 = value; 153 | } 154 | else if (column == 1) 155 | { 156 | m01 = value; 157 | } 158 | else if (column == 2) 159 | { 160 | m02 = value; 161 | } 162 | else 163 | { 164 | throw new ArrayIndexOutOfBoundsException("column must be 0 to 2 and is " + column); 165 | } 166 | } 167 | else if (row == 1) 168 | { 169 | if (column == 0) 170 | { 171 | m10 = value; 172 | } 173 | else if (column == 1) 174 | { 175 | m11 = value; 176 | } 177 | else if (column == 2) 178 | { 179 | m12 = value; 180 | } 181 | else 182 | { 183 | throw new ArrayIndexOutOfBoundsException("column must be 0 to 2 and is " + column); 184 | } 185 | } 186 | else if (row == 2) 187 | { 188 | if (column == 0) 189 | { 190 | m20 = value; 191 | } 192 | else if (column == 1) 193 | { 194 | m21 = value; 195 | } 196 | else if (column == 2) 197 | { 198 | m22 = value; 199 | } 200 | else 201 | { 202 | throw new ArrayIndexOutOfBoundsException("column must be 0 to 2 and is " + column); 203 | } 204 | } 205 | else 206 | { 207 | throw new ArrayIndexOutOfBoundsException("row must be 0 to 2 and is " + row); 208 | } 209 | } 210 | 211 | /** 212 | * Retrieves the value at the specified row and column of this matrix. 213 | * 214 | * @param row the row number to be retrieved (zero indexed) 215 | * @param column the column number to be retrieved (zero indexed) 216 | * @return the value at the indexed element 217 | */ 218 | public final float getElement(int row, int column) 219 | { 220 | if (row == 0) 221 | { 222 | if (column == 0) 223 | { 224 | return m00; 225 | } 226 | else if (column == 1) 227 | { 228 | return m01; 229 | } 230 | else if (column == 2) 231 | { 232 | return m02; 233 | } 234 | else 235 | { 236 | throw new ArrayIndexOutOfBoundsException("column must be 0 to 2 and is " + column); 237 | } 238 | } 239 | else if (row == 1) 240 | { 241 | if (column == 0) 242 | { 243 | return m10; 244 | } 245 | else if (column == 1) 246 | { 247 | return m11; 248 | } 249 | else if (column == 2) 250 | { 251 | return m12; 252 | } 253 | else 254 | { 255 | throw new ArrayIndexOutOfBoundsException("column must be 0 to 2 and is " + column); 256 | } 257 | } 258 | else if (row == 2) 259 | { 260 | if (column == 0) 261 | { 262 | return m20; 263 | } 264 | else if (column == 1) 265 | { 266 | return m21; 267 | } 268 | else if (column == 2) 269 | { 270 | return m22; 271 | } 272 | else 273 | { 274 | throw new ArrayIndexOutOfBoundsException("column must be 0 to 2 and is " + column); 275 | } 276 | } 277 | else 278 | { 279 | throw new ArrayIndexOutOfBoundsException("row must be 0 to 2 and is " + row); 280 | } 281 | } 282 | 283 | 284 | /** 285 | * Sets the specified row of this matrix3d to the three values provided. 286 | * 287 | * @param row the row number to be modified (zero indexed) 288 | * @param x the first column element 289 | * @param y the second column element 290 | * @param z the third column element 291 | */ 292 | public final void setRow(int row, float x, float y, float z) 293 | { 294 | switch (row) 295 | { 296 | case 0: 297 | m00 = x; 298 | m01 = y; 299 | m02 = z; 300 | break; 301 | 302 | case 1: 303 | m10 = x; 304 | m11 = y; 305 | m12 = z; 306 | break; 307 | 308 | case 2: 309 | m20 = x; 310 | m21 = y; 311 | m22 = z; 312 | break; 313 | 314 | default: 315 | throw new ArrayIndexOutOfBoundsException("row must be 0 to 2 and is " + row); 316 | } 317 | } 318 | 319 | /** 320 | * Sets the specified row of this matrix3d to the Vector provided. 321 | * 322 | * @param row the row number to be modified (zero indexed) 323 | * @param v the replacement row 324 | */ 325 | public final void setRow(int row, Vector3f v) 326 | { 327 | switch (row) 328 | { 329 | case 0: 330 | m00 = v.x; 331 | m01 = v.y; 332 | m02 = v.z; 333 | break; 334 | 335 | case 1: 336 | m10 = v.x; 337 | m11 = v.y; 338 | m12 = v.z; 339 | break; 340 | 341 | case 2: 342 | m20 = v.x; 343 | m21 = v.y; 344 | m22 = v.z; 345 | break; 346 | 347 | default: 348 | throw new ArrayIndexOutOfBoundsException("row must be 0 to 2 and is " + row); 349 | } 350 | } 351 | 352 | /** 353 | * Copies the matrix values in the specified row into the 354 | * array parameter. 355 | * 356 | * @param row the matrix row 357 | * @param v The array into which the matrix row values will be copied 358 | */ 359 | public final void getRow(int row, float v[]) 360 | { 361 | switch (row) 362 | { 363 | case 0: 364 | v[0] = m00; 365 | v[1] = m01; 366 | v[2] = m02; 367 | break; 368 | 369 | case 1: 370 | v[0] = m10; 371 | v[1] = m11; 372 | v[2] = m12; 373 | break; 374 | 375 | case 2: 376 | v[0] = m20; 377 | v[1] = m21; 378 | v[2] = m22; 379 | break; 380 | 381 | default: 382 | throw new ArrayIndexOutOfBoundsException("row must be 0 to 2 and is " + row); 383 | } 384 | } 385 | 386 | /** 387 | * Copies the matrix values in the specified row into the 388 | * vector parameter. 389 | * 390 | * @param row the matrix row 391 | * @param v The vector into which the matrix row values will be copied 392 | */ 393 | public final void getRow(int row, Vector3f v) 394 | { 395 | switch (row) 396 | { 397 | case 0: 398 | v.x = m00; 399 | v.y = m01; 400 | v.z = m02; 401 | break; 402 | 403 | case 1: 404 | v.x = m10; 405 | v.y = m11; 406 | v.z = m12; 407 | break; 408 | 409 | case 2: 410 | v.x = m20; 411 | v.y = m21; 412 | v.z = m22; 413 | break; 414 | 415 | default: 416 | throw new ArrayIndexOutOfBoundsException("row must be 0 to 2 and is " + row); 417 | } 418 | } 419 | 420 | /** 421 | * Sets the specified row of this matrix3d to the four values provided. 422 | * 423 | * @param row the row number to be modified (zero indexed) 424 | * @param v the replacement row 425 | */ 426 | public final void setRow(int row, float v[]) 427 | { 428 | switch (row) 429 | { 430 | case 0: 431 | m00 = v[0]; 432 | m01 = v[1]; 433 | m02 = v[2]; 434 | break; 435 | 436 | case 1: 437 | m10 = v[0]; 438 | m11 = v[1]; 439 | m12 = v[2]; 440 | break; 441 | 442 | case 2: 443 | m20 = v[0]; 444 | m21 = v[1]; 445 | m22 = v[2]; 446 | break; 447 | 448 | default: 449 | throw new ArrayIndexOutOfBoundsException("row must be 0 to 2 and is " + row); 450 | } 451 | } 452 | 453 | /** 454 | * Sets the specified column of this matrix3d to the three values provided. 455 | * 456 | * @param column the column number to be modified (zero indexed) 457 | * @param x the first row element 458 | * @param y the second row element 459 | * @param z the third row element 460 | */ 461 | public final void setColumn(int column, float x, float y, float z) 462 | { 463 | switch (column) 464 | { 465 | case 0: 466 | m00 = x; 467 | m10 = y; 468 | m20 = z; 469 | break; 470 | 471 | case 1: 472 | m01 = x; 473 | m11 = y; 474 | m21 = z; 475 | break; 476 | 477 | case 2: 478 | m02 = x; 479 | m12 = y; 480 | m22 = z; 481 | break; 482 | 483 | default: 484 | throw new ArrayIndexOutOfBoundsException("column must be 0 to 2 and is " + column); 485 | } 486 | } 487 | 488 | /** 489 | * Sets the specified column of this matrix3d to the vector provided. 490 | * 491 | * @param column the column number to be modified (zero indexed) 492 | * @param v the replacement column 493 | */ 494 | public final void setColumn(int column, Vector3f v) 495 | { 496 | switch (column) 497 | { 498 | case 0: 499 | m00 = v.x; 500 | m10 = v.y; 501 | m20 = v.z; 502 | break; 503 | 504 | case 1: 505 | m01 = v.x; 506 | m11 = v.y; 507 | m21 = v.z; 508 | break; 509 | 510 | case 2: 511 | m02 = v.x; 512 | m12 = v.y; 513 | m22 = v.z; 514 | break; 515 | 516 | default: 517 | throw new ArrayIndexOutOfBoundsException("column must be 0 to 2 and is " + column); 518 | } 519 | } 520 | 521 | /** 522 | * Sets the specified column of this matrix3d to the four values provided. 523 | * 524 | * @param column the column number to be modified (zero indexed) 525 | * @param v the replacement column 526 | */ 527 | public final void setColumn(int column, float v[]) 528 | { 529 | switch (column) 530 | { 531 | case 0: 532 | m00 = v[0]; 533 | m10 = v[1]; 534 | m20 = v[2]; 535 | break; 536 | 537 | case 1: 538 | m01 = v[0]; 539 | m11 = v[1]; 540 | m21 = v[2]; 541 | break; 542 | 543 | case 2: 544 | m02 = v[0]; 545 | m12 = v[1]; 546 | m22 = v[2]; 547 | break; 548 | 549 | default: 550 | throw new ArrayIndexOutOfBoundsException("column must be 0 to 2 and is " + column); 551 | } 552 | } 553 | 554 | /** 555 | * Copies the matrix values in the specified column into the vector 556 | * parameter. 557 | * 558 | * @param column the matrix column 559 | * @param v The vector into which the matrix row values will be copied 560 | */ 561 | public final void getColumn(int column, Vector3f v) 562 | { 563 | switch (column) 564 | { 565 | case 0: 566 | v.x = m00; 567 | v.y = m10; 568 | v.z = m20; 569 | break; 570 | 571 | case 1: 572 | v.x = m01; 573 | v.y = m11; 574 | v.z = m21; 575 | break; 576 | 577 | case 2: 578 | v.x = m02; 579 | v.y = m12; 580 | v.z = m22; 581 | break; 582 | 583 | default: 584 | throw new ArrayIndexOutOfBoundsException("column must be 0 to 2 and is " + column); 585 | } 586 | } 587 | 588 | /** 589 | * Copies the matrix values in the specified column into the array 590 | * parameter. 591 | * 592 | * @param column the matrix column 593 | * @param v The array into which the matrix row values will be copied 594 | */ 595 | public final void getColumn(int column, float v[]) 596 | { 597 | switch (column) 598 | { 599 | case 0: 600 | v[0] = m00; 601 | v[1] = m10; 602 | v[2] = m20; 603 | break; 604 | 605 | case 1: 606 | v[0] = m01; 607 | v[1] = m11; 608 | v[2] = m21; 609 | break; 610 | 611 | case 2: 612 | v[0] = m02; 613 | v[1] = m12; 614 | v[2] = m22; 615 | break; 616 | 617 | default: 618 | throw new ArrayIndexOutOfBoundsException("column must be 0 to 2 and is " + column); 619 | } 620 | } 621 | 622 | 623 | /** 624 | * Performs an SVD normalization of this matrix to calculate and return the 625 | * uniform scale factor. This matrix is not modified. 626 | * 627 | * @return the scale factor of this matrix 628 | */ 629 | public final float getScale() 630 | { 631 | return SVD(null); 632 | } 633 | 634 | 635 | /** 636 | * Adds a scalar to each component of this matrix. 637 | * 638 | * @param scalar The scalar adder. 639 | */ 640 | public final void add(float scalar) 641 | { 642 | m00 += scalar; 643 | m01 += scalar; 644 | m02 += scalar; 645 | m10 += scalar; 646 | m11 += scalar; 647 | m12 += scalar; 648 | m20 += scalar; 649 | m21 += scalar; 650 | m22 += scalar; 651 | } 652 | 653 | /** 654 | * Adds a scalar to each component of the matrix m1 and places 655 | * the result into this. Matrix m1 is not modified. 656 | * 657 | * @param scalar The scalar adder. 658 | * @param m1 The original matrix values. 659 | */ 660 | public final void add(float scalar, Matrix3f m1) 661 | { 662 | set(m1); 663 | add(scalar); 664 | } 665 | 666 | 667 | /** 668 | * Sets the value of this matrix to the matrix sum of matrices m1 and m2. 669 | * 670 | * @param m1 the first matrix 671 | * @param m2 the second matrix 672 | */ 673 | public final void add(Matrix3f m1, Matrix3f m2) 674 | { 675 | // note this is alias safe. 676 | set( 677 | m1.m00 + m2.m00, 678 | m1.m01 + m2.m01, 679 | m1.m02 + m2.m02, 680 | m1.m10 + m2.m10, 681 | m1.m11 + m2.m11, 682 | m1.m12 + m2.m12, 683 | m1.m20 + m2.m20, 684 | m1.m21 + m2.m21, 685 | m1.m22 + m2.m22 686 | ); 687 | } 688 | 689 | /** 690 | * Sets the value of this matrix to sum of itself and matrix m1. 691 | * 692 | * @param m1 the other matrix 693 | */ 694 | public final void add(Matrix3f m1) 695 | { 696 | m00 += m1.m00; 697 | m01 += m1.m01; 698 | m02 += m1.m02; 699 | m10 += m1.m10; 700 | m11 += m1.m11; 701 | m12 += m1.m12; 702 | m20 += m1.m20; 703 | m21 += m1.m21; 704 | m22 += m1.m22; 705 | } 706 | 707 | /** 708 | * Sets the value of this matrix to the matrix difference 709 | * of matrices m1 and m2. 710 | * 711 | * @param m1 the first matrix 712 | * @param m2 the second matrix 713 | */ 714 | public final void sub(Matrix3f m1, Matrix3f m2) 715 | { 716 | set( 717 | m1.m00 - m2.m00, 718 | m1.m01 - m2.m01, 719 | m1.m02 - m2.m02, 720 | m1.m10 - m2.m10, 721 | m1.m11 - m2.m11, 722 | m1.m12 - m2.m12, 723 | m1.m20 - m2.m20, 724 | m1.m21 - m2.m21, 725 | m1.m22 - m2.m22 726 | ); 727 | } 728 | 729 | /** 730 | * Sets the value of this matrix to the matrix difference of itself 731 | * and matrix m1 (this = this - m1). 732 | * 733 | * @param m1 the other matrix 734 | */ 735 | public final void sub(Matrix3f m1) 736 | { 737 | m00 -= m1.m00; 738 | m01 -= m1.m01; 739 | m02 -= m1.m02; 740 | m10 -= m1.m10; 741 | m11 -= m1.m11; 742 | m12 -= m1.m12; 743 | m20 -= m1.m20; 744 | m21 -= m1.m21; 745 | m22 -= m1.m22; 746 | } 747 | 748 | /** 749 | * Sets the value of this matrix to its transpose. 750 | */ 751 | public final void transpose() 752 | { 753 | float tmp = m01; 754 | m01 = m10; 755 | m10 = tmp; 756 | 757 | tmp = m02; 758 | m02 = m20; 759 | m20 = tmp; 760 | 761 | tmp = m12; 762 | m12 = m21; 763 | m21 = tmp; 764 | 765 | } 766 | 767 | /** 768 | * Sets the value of this matrix to the transpose of the argument matrix 769 | * 770 | * @param m1 the matrix to be transposed 771 | */ 772 | public final void transpose(Matrix3f m1) 773 | { 774 | // alias-safe 775 | set(m1); 776 | transpose(); 777 | } 778 | 779 | /** 780 | * Sets the value of this matrix to the matrix conversion of the 781 | * (single precision) quaternion argument. 782 | * 783 | * @param q1 the quaternion to be converted 784 | */ 785 | public final void set(Quat4f q1) 786 | { 787 | setFromQuat(q1.x, q1.y, q1.z, q1.w); 788 | } 789 | 790 | /** 791 | * Sets the value of this matrix to the double value of the Matrix3f 792 | * argument. 793 | * 794 | * @param m1 the matrix3f 795 | */ 796 | public final void set(Matrix3f m1) 797 | { 798 | m00 = m1.m00; 799 | m01 = m1.m01; 800 | m02 = m1.m02; 801 | m10 = m1.m10; 802 | m11 = m1.m11; 803 | m12 = m1.m12; 804 | m20 = m1.m20; 805 | m21 = m1.m21; 806 | m22 = m1.m22; 807 | } 808 | 809 | /** 810 | * Sets the values in this Matrix3f equal to the row-major array parameter 811 | * (ie, the first four elements of the array will be copied into the first 812 | * row of this matrix, etc.). 813 | */ 814 | public final void set(float m[]) 815 | { 816 | m00 = m[0]; 817 | m01 = m[1]; 818 | m02 = m[2]; 819 | m10 = m[3]; 820 | m11 = m[4]; 821 | m12 = m[5]; 822 | m20 = m[6]; 823 | m21 = m[7]; 824 | m22 = m[8]; 825 | } 826 | 827 | /** 828 | * Sets the value of this matrix to the matrix inverse 829 | * of the passed matrix m1. 830 | * 831 | * @param m1 the matrix to be inverted 832 | */ 833 | public final void invert(Matrix3f m1) 834 | { 835 | set(m1); 836 | invert(); 837 | } 838 | 839 | /** 840 | * Sets the value of this matrix to its inverse. 841 | */ 842 | public final void invert() 843 | { 844 | double s = determinant(); 845 | if (s == 0.0) 846 | return; 847 | s = 1 / s; 848 | // alias-safe way. 849 | set( 850 | m11 * m22 - m12 * m21, m02 * m21 - m01 * m22, m01 * m12 - m02 * m11, 851 | m12 * m20 - m10 * m22, m00 * m22 - m02 * m20, m02 * m10 - m00 * m12, 852 | m10 * m21 - m11 * m20, m01 * m20 - m00 * m21, m00 * m11 - m01 * m10 853 | ); 854 | mul((float) s); 855 | } 856 | 857 | /** 858 | * Computes the determinant of this matrix. 859 | * 860 | * @return the determinant of the matrix 861 | */ 862 | public final float determinant() 863 | { 864 | // less *,+,- calculation than expanded expression. 865 | return m00 * (m11 * m22 - m21 * m12) 866 | - m01 * (m10 * m22 - m20 * m12) 867 | + m02 * (m10 * m21 - m20 * m11); 868 | } 869 | 870 | /** 871 | * Sets the value of this matrix to a scale matrix with the 872 | * passed scale amount. 873 | * 874 | * @param scale the scale factor for the matrix 875 | */ 876 | public final void set(float scale) 877 | { 878 | m00 = scale; 879 | m01 = 0.0f; 880 | m02 = 0.0f; 881 | m10 = 0.0f; 882 | m11 = scale; 883 | m12 = 0.0f; 884 | m20 = 0.0f; 885 | m21 = 0.0f; 886 | m22 = scale; 887 | } 888 | 889 | 890 | /** 891 | * Sets the value of this matrix to a rotation matrix about the x axis 892 | * by the passed angle. 893 | * 894 | * @param angle the angle to rotate about the X axis in radians 895 | */ 896 | public final void rotX(float angle) 897 | { 898 | double c = Math.cos(angle); 899 | double s = Math.sin(angle); 900 | m00 = 1.0f; 901 | m01 = 0.0f; 902 | m02 = 0.0f; 903 | m10 = 0.0f; 904 | m11 = (float) c; 905 | m12 = (float) -s; 906 | m20 = 0.0f; 907 | m21 = (float) s; 908 | m22 = (float) c; 909 | } 910 | 911 | /** 912 | * Sets the value of this matrix to a rotation matrix about the y axis 913 | * by the passed angle. 914 | * 915 | * @param angle the angle to rotate about the Y axis in radians 916 | */ 917 | public final void rotY(float angle) 918 | { 919 | double c = Math.cos(angle); 920 | double s = Math.sin(angle); 921 | m00 = (float) c; 922 | m01 = 0.0f; 923 | m02 = (float) s; 924 | m10 = 0.0f; 925 | m11 = 1.0f; 926 | m12 = 0.0f; 927 | m20 = (float) -s; 928 | m21 = 0.0f; 929 | m22 = (float) c; 930 | } 931 | 932 | /** 933 | * Sets the value of this matrix to a rotation matrix about the z axis 934 | * by the passed angle. 935 | * 936 | * @param angle the angle to rotate about the Z axis in radians 937 | */ 938 | public final void rotZ(float angle) 939 | { 940 | double c = Math.cos(angle); 941 | double s = Math.sin(angle); 942 | m00 = (float) c; 943 | m01 = (float) -s; 944 | m02 = 0.0f; 945 | m10 = (float) s; 946 | m11 = (float) c; 947 | m12 = 0.0f; 948 | m20 = 0.0f; 949 | m21 = 0.0f; 950 | m22 = 1.0f; 951 | } 952 | 953 | /** 954 | * Multiplies each element of this matrix by a scalar. 955 | * 956 | * @param scalar The scalar multiplier. 957 | */ 958 | public final void mul(float scalar) 959 | { 960 | m00 *= scalar; 961 | m01 *= scalar; 962 | m02 *= scalar; 963 | m10 *= scalar; 964 | m11 *= scalar; 965 | m12 *= scalar; 966 | m20 *= scalar; 967 | m21 *= scalar; 968 | m22 *= scalar; 969 | } 970 | 971 | /** 972 | * Multiplies each element of matrix m1 by a scalar and places the result 973 | * into this. Matrix m1 is not modified. 974 | * 975 | * @param scalar The scalar multiplier. 976 | * @param m1 The original matrix. 977 | */ 978 | public final void mul(float scalar, Matrix3f m1) 979 | { 980 | set(m1); 981 | mul(scalar); 982 | } 983 | 984 | /** 985 | * Sets the value of this matrix to the result of multiplying itself 986 | * with matrix m1. 987 | * 988 | * @param m1 the other matrix 989 | */ 990 | public final void mul(Matrix3f m1) 991 | { 992 | mul(this, m1); 993 | } 994 | 995 | /** 996 | * Sets the value of this matrix to the result of multiplying 997 | * the two argument matrices together. 998 | * 999 | * @param m1 the first matrix 1000 | * @param m2 the second matrix 1001 | */ 1002 | public final void mul(Matrix3f m1, Matrix3f m2) 1003 | { 1004 | // alias-safe way. 1005 | set( 1006 | m1.m00 * m2.m00 + m1.m01 * m2.m10 + m1.m02 * m2.m20, 1007 | m1.m00 * m2.m01 + m1.m01 * m2.m11 + m1.m02 * m2.m21, 1008 | m1.m00 * m2.m02 + m1.m01 * m2.m12 + m1.m02 * m2.m22, 1009 | 1010 | m1.m10 * m2.m00 + m1.m11 * m2.m10 + m1.m12 * m2.m20, 1011 | m1.m10 * m2.m01 + m1.m11 * m2.m11 + m1.m12 * m2.m21, 1012 | m1.m10 * m2.m02 + m1.m11 * m2.m12 + m1.m12 * m2.m22, 1013 | 1014 | m1.m20 * m2.m00 + m1.m21 * m2.m10 + m1.m22 * m2.m20, 1015 | m1.m20 * m2.m01 + m1.m21 * m2.m11 + m1.m22 * m2.m21, 1016 | m1.m20 * m2.m02 + m1.m21 * m2.m12 + m1.m22 * m2.m22 1017 | ); 1018 | } 1019 | 1020 | /** 1021 | * Multiplies this matrix by matrix m1, does an SVD normalization of the 1022 | * result, and places the result back into this matrix this = 1023 | * SVDnorm(this*m1). 1024 | * 1025 | * @param m1 the matrix on the right hand side of the multiplication 1026 | */ 1027 | public final void mulNormalize(Matrix3f m1) 1028 | { 1029 | mul(m1); 1030 | SVD(this); 1031 | } 1032 | 1033 | 1034 | /** 1035 | * Multiplies matrix m1 by matrix m2, does an SVD normalization of the 1036 | * result, and places the result into this matrix this = SVDnorm(m1*m2). 1037 | * 1038 | * @param m1 the matrix on the left hand side of the multiplication 1039 | * @param m2 the matrix on the right hand side of the multiplication 1040 | */ 1041 | public final void mulNormalize(Matrix3f m1, Matrix3f m2) 1042 | { 1043 | mul(m1, m2); 1044 | SVD(this); 1045 | } 1046 | 1047 | /** 1048 | * Multiplies the transpose of matrix m1 times the transpose of matrix m2, 1049 | * and places the result into this. 1050 | * 1051 | * @param m1 The matrix on the left hand side of the multiplication 1052 | * @param m2 The matrix on the right hand side of the multiplication 1053 | */ 1054 | public final void mulTransposeBoth(Matrix3f m1, Matrix3f m2) 1055 | { 1056 | mul(m2, m1); 1057 | transpose(); 1058 | } 1059 | 1060 | /** 1061 | * Multiplies matrix m1 times the transpose of matrix m2, and places the 1062 | * result into this. 1063 | * 1064 | * @param m1 The matrix on the left hand side of the multiplication 1065 | * @param m2 The matrix on the right hand side of the multiplication 1066 | */ 1067 | public final void mulTransposeRight(Matrix3f m1, Matrix3f m2) 1068 | { 1069 | // alias-safe way. 1070 | set( 1071 | m1.m00 * m2.m00 + m1.m01 * m2.m01 + m1.m02 * m2.m02, 1072 | m1.m00 * m2.m10 + m1.m01 * m2.m11 + m1.m02 * m2.m12, 1073 | m1.m00 * m2.m20 + m1.m01 * m2.m21 + m1.m02 * m2.m22, 1074 | 1075 | m1.m10 * m2.m00 + m1.m11 * m2.m01 + m1.m12 * m2.m02, 1076 | m1.m10 * m2.m10 + m1.m11 * m2.m11 + m1.m12 * m2.m12, 1077 | m1.m10 * m2.m20 + m1.m11 * m2.m21 + m1.m12 * m2.m22, 1078 | 1079 | m1.m20 * m2.m00 + m1.m21 * m2.m01 + m1.m22 * m2.m02, 1080 | m1.m20 * m2.m10 + m1.m21 * m2.m11 + m1.m22 * m2.m12, 1081 | m1.m20 * m2.m20 + m1.m21 * m2.m21 + m1.m22 * m2.m22 1082 | ); 1083 | } 1084 | 1085 | 1086 | /** 1087 | * Multiplies the transpose of matrix m1 times matrix m2, and places the 1088 | * result into this. 1089 | * 1090 | * @param m1 The matrix on the left hand side of the multiplication 1091 | * @param m2 The matrix on the right hand side of the multiplication 1092 | */ 1093 | public final void mulTransposeLeft(Matrix3f m1, Matrix3f m2) 1094 | { 1095 | // alias-safe way. 1096 | set( 1097 | m1.m00 * m2.m00 + m1.m10 * m2.m10 + m1.m20 * m2.m20, 1098 | m1.m00 * m2.m01 + m1.m10 * m2.m11 + m1.m20 * m2.m21, 1099 | m1.m00 * m2.m02 + m1.m10 * m2.m12 + m1.m20 * m2.m22, 1100 | 1101 | m1.m01 * m2.m00 + m1.m11 * m2.m10 + m1.m21 * m2.m20, 1102 | m1.m01 * m2.m01 + m1.m11 * m2.m11 + m1.m21 * m2.m21, 1103 | m1.m01 * m2.m02 + m1.m11 * m2.m12 + m1.m21 * m2.m22, 1104 | 1105 | m1.m02 * m2.m00 + m1.m12 * m2.m10 + m1.m22 * m2.m20, 1106 | m1.m02 * m2.m01 + m1.m12 * m2.m11 + m1.m22 * m2.m21, 1107 | m1.m02 * m2.m02 + m1.m12 * m2.m12 + m1.m22 * m2.m22 1108 | ); 1109 | } 1110 | 1111 | /** 1112 | * Performs singular value decomposition normalization of this matrix. 1113 | */ 1114 | public final void normalize() 1115 | { 1116 | SVD(this); 1117 | } 1118 | 1119 | /** 1120 | * Perform singular value decomposition normalization of matrix m1 and 1121 | * place the normalized values into this. 1122 | * 1123 | * @param m1 Provides the matrix values to be normalized 1124 | */ 1125 | public final void normalize(Matrix3f m1) 1126 | { 1127 | set(m1); 1128 | SVD(this); 1129 | } 1130 | 1131 | /** 1132 | * Perform cross product normalization of this matrix. 1133 | */ 1134 | public final void normalizeCP() 1135 | { 1136 | // domain error may occur 1137 | mul((float) Math.pow(determinant(), -1.0 / 3.0)); 1138 | } 1139 | 1140 | /** 1141 | * Perform cross product normalization of matrix m1 and place the 1142 | * normalized values into this. 1143 | * 1144 | * @param m1 Provides the matrix values to be normalized 1145 | */ 1146 | public final void normalizeCP(Matrix3f m1) 1147 | { 1148 | set(m1); 1149 | normalizeCP(); 1150 | } 1151 | 1152 | 1153 | /** 1154 | * Returns true if all of the data members of Matrix3f m1 are 1155 | * equal to the corresponding data members in this Matrix3f. 1156 | * 1157 | * @param m1 The matrix with which the comparison is made. 1158 | * @return true or false 1159 | */ 1160 | public boolean equals(Matrix3f m1) 1161 | { 1162 | return m1 != null 1163 | && m00 == m1.m00 1164 | && m01 == m1.m01 1165 | && m02 == m1.m02 1166 | && m10 == m1.m10 1167 | && m11 == m1.m11 1168 | && m12 == m1.m12 1169 | && m20 == m1.m20 1170 | && m21 == m1.m21 1171 | && m22 == m1.m22; 1172 | } 1173 | 1174 | /** 1175 | * Returns true if the Object o1 is of type Matrix3f and all of the data 1176 | * members of t1 are equal to the corresponding data members in this 1177 | * Matrix3f. 1178 | * 1179 | * @param o1 the object with which the comparison is made. 1180 | */ 1181 | @Override 1182 | public boolean equals(Object o1) 1183 | { 1184 | return o1 != null && (o1 instanceof Matrix3f) && equals((Matrix3f) o1); 1185 | } 1186 | 1187 | /** 1188 | * Returns true if the L-infinite distance between this matrix and matrix 1189 | * m1 is less than or equal to the epsilon parameter, otherwise returns 1190 | * false. The L-infinite distance is equal to MAX[i=0,1,2,3 ; j=0,1,2,3 ; 1191 | * abs(this.m(i,j) - m1.m(i,j)] 1192 | * 1193 | * @param m1 The matrix to be compared to this matrix 1194 | * @param epsilon the threshold value 1195 | */ 1196 | public boolean epsilonEquals(Matrix3f m1, double epsilon) 1197 | { 1198 | return Math.abs(m00 - m1.m00) <= epsilon 1199 | && Math.abs(m01 - m1.m01) <= epsilon 1200 | && Math.abs(m02 - m1.m02) <= epsilon 1201 | 1202 | && Math.abs(m10 - m1.m10) <= epsilon 1203 | && Math.abs(m11 - m1.m11) <= epsilon 1204 | && Math.abs(m12 - m1.m12) <= epsilon 1205 | 1206 | && Math.abs(m20 - m1.m20) <= epsilon 1207 | && Math.abs(m21 - m1.m21) <= epsilon 1208 | && Math.abs(m22 - m1.m22) <= epsilon; 1209 | } 1210 | 1211 | /** 1212 | * Returns a hash number based on the data values in this 1213 | * object. Two different Matrix3f objects with identical data values 1214 | * (ie, returns true for equals(Matrix3f) ) will return the same hash 1215 | * number. Two objects with different data members may return the 1216 | * same hash value, although this is not likely. 1217 | * 1218 | * @return the integer hash value 1219 | */ 1220 | @Override 1221 | public int hashCode() 1222 | { 1223 | return Float.floatToIntBits(m00) ^ 1224 | Float.floatToIntBits(m01) ^ 1225 | Float.floatToIntBits(m02) ^ 1226 | Float.floatToIntBits(m10) ^ 1227 | Float.floatToIntBits(m11) ^ 1228 | Float.floatToIntBits(m12) ^ 1229 | Float.floatToIntBits(m20) ^ 1230 | Float.floatToIntBits(m21) ^ 1231 | Float.floatToIntBits(m22); 1232 | } 1233 | 1234 | /** 1235 | * Sets this matrix to all zeros. 1236 | */ 1237 | public final void setZero() 1238 | { 1239 | m00 = 0.0f; 1240 | m01 = 0.0f; 1241 | m02 = 0.0f; 1242 | m10 = 0.0f; 1243 | m11 = 0.0f; 1244 | m12 = 0.0f; 1245 | m20 = 0.0f; 1246 | m21 = 0.0f; 1247 | m22 = 0.0f; 1248 | } 1249 | 1250 | /** 1251 | * Negates the value of this matrix: this = -this. 1252 | */ 1253 | public final void negate() 1254 | { 1255 | m00 = -m00; 1256 | m01 = -m01; 1257 | m02 = -m02; 1258 | m10 = -m10; 1259 | m11 = -m11; 1260 | m12 = -m12; 1261 | m20 = -m20; 1262 | m21 = -m21; 1263 | m22 = -m22; 1264 | } 1265 | 1266 | /** 1267 | * Sets the value of this matrix equal to the negation of of the Matrix3f 1268 | * parameter. 1269 | * 1270 | * @param m1 The source matrix 1271 | */ 1272 | public final void negate(Matrix3f m1) 1273 | { 1274 | set(m1); 1275 | negate(); 1276 | } 1277 | 1278 | /** 1279 | * Transforms the input tuple with this Matrix3f and places the result also into input. 1280 | * 1281 | * @param inputOutput the input tuple to be transformed; result is stored in input. 1282 | */ 1283 | public final void transform(Tuple3f inputOutput) 1284 | { 1285 | inputOutput.set( 1286 | m00 * inputOutput.x + m01 * inputOutput.y + m02 * inputOutput.z, 1287 | m10 * inputOutput.x + m11 * inputOutput.y + m12 * inputOutput.z, 1288 | m20 * inputOutput.x + m21 * inputOutput.y + m22 * inputOutput.z 1289 | ); 1290 | } 1291 | 1292 | /** 1293 | * Transforms the input tuple with this Matrix3f and places the result into output. Input must not be the same 1294 | * object as output. 1295 | * 1296 | * @param input the input tuple to be transformed. 1297 | * @param output the transformed tuple 1298 | */ 1299 | public final void transform(Tuple3f input, Tuple3f output) 1300 | { 1301 | output.x = m00 * input.x + m01 * input.y + m02 * input.z; 1302 | output.y = m10 * input.x + m11 * input.y + m12 * input.z; 1303 | output.z = m20 * input.x + m21 * input.y + m22 * input.z; 1304 | } 1305 | 1306 | /** 1307 | * Sets 9 values 1308 | */ 1309 | private void set(float m00, float m01, float m02, 1310 | float m10, float m11, float m12, 1311 | float m20, float m21, float m22) 1312 | { 1313 | this.m00 = m00; 1314 | this.m01 = m01; 1315 | this.m02 = m02; 1316 | this.m10 = m10; 1317 | this.m11 = m11; 1318 | this.m12 = m12; 1319 | this.m20 = m20; 1320 | this.m21 = m21; 1321 | this.m22 = m22; 1322 | } 1323 | 1324 | /** 1325 | * Performs SVD on this matrix and gets scale and rotation. 1326 | * Rotation is placed into rot. 1327 | * 1328 | * @param rot the rotation factor. 1329 | * @return scale factor 1330 | */ 1331 | private float SVD(Matrix3f rot) 1332 | { 1333 | // this is a simple svd. 1334 | // Not complete but fast and reasonable. 1335 | 1336 | /* 1337 | * SVD scale factors(squared) are the 3 roots of 1338 | * 1339 | * | xI - M*MT | = 0. 1340 | * 1341 | * This will be expanded as follows 1342 | * 1343 | * x^3 - A x^2 + B x - C = 0 1344 | * 1345 | * where A, B, C can be denoted by 3 roots x0, x1, x2. 1346 | * 1347 | * A = (x0+x1+x2), B = (x0x1+x1x2+x2x0), C = x0x1x2. 1348 | * 1349 | * An average of x0,x1,x2 is needed here. C^(1/3) is a cross product 1350 | * normalization factor. 1351 | * So here, I use A/3. Note that x should be sqrt'ed for the 1352 | * actual factor. 1353 | */ 1354 | 1355 | float s = (float) Math.sqrt( 1356 | ( 1357 | m00 * m00 + m10 * m10 + m20 * m20 + 1358 | m01 * m01 + m11 * m11 + m21 * m21 + 1359 | m02 * m02 + m12 * m12 + m22 * m22 1360 | ) / 3.0 1361 | ); 1362 | 1363 | // zero-div may occur. 1364 | float t = (s == 0.0f ? 0.0f : 1.0f / s); 1365 | 1366 | if (rot != null) 1367 | { 1368 | if (rot != this) 1369 | rot.set(this); 1370 | rot.mul(t); 1371 | } 1372 | 1373 | return s; 1374 | } 1375 | 1376 | private void setFromQuat(float x, float y, float z, float w) 1377 | { 1378 | float n = x * x + y * y + z * z + w * w; 1379 | float s = (n > 0.0f) ? (2.0f / n) : 0.0f; 1380 | 1381 | float xs = x * s, ys = y * s, zs = z * s; 1382 | float wx = w * xs, wy = w * ys, wz = w * zs; 1383 | float xx = x * xs, xy = x * ys, xz = x * zs; 1384 | float yy = y * ys, yz = y * zs, zz = z * zs; 1385 | 1386 | m00 = 1.0f - (yy + zz); 1387 | m01 = xy - wz; 1388 | m02 = xz + wy; 1389 | m10 = xy + wz; 1390 | m11 = 1.0f - (xx + zz); 1391 | m12 = yz - wx; 1392 | m20 = xz - wy; 1393 | m21 = yz + wx; 1394 | m22 = 1.0f - (xx + yy); 1395 | } 1396 | 1397 | private void setFromAxisAngle(float x, float y, float z, float angle) 1398 | { 1399 | // Taken from Rick's which is taken from Wertz. pg. 412 1400 | // Bug Fixed and changed into right-handed by hiranabe 1401 | float n = (float)Math.sqrt(x * x + y * y + z * z); 1402 | // zero-div may occur 1403 | n = 1 / n; 1404 | x *= n; 1405 | y *= n; 1406 | z *= n; 1407 | float c = (float)Math.cos(angle); 1408 | float s = (float)Math.sin(angle); 1409 | float omc = 1.0f - c; 1410 | m00 = c + x * x * omc; 1411 | m11 = c + y * y * omc; 1412 | m22 = c + z * z * omc; 1413 | 1414 | float tmp1 = x * y * omc; 1415 | float tmp2 = z * s; 1416 | m01 = tmp1 - tmp2; 1417 | m10 = tmp1 + tmp2; 1418 | 1419 | tmp1 = x * z * omc; 1420 | tmp2 = y * s; 1421 | m02 = tmp1 + tmp2; 1422 | m20 = tmp1 - tmp2; 1423 | 1424 | tmp1 = y * z * omc; 1425 | tmp2 = x * s; 1426 | m12 = tmp1 - tmp2; 1427 | m21 = tmp1 + tmp2; 1428 | } 1429 | } 1430 | -------------------------------------------------------------------------------- /java6-math/src/main/java/org/typhonrt/java6/vecmath/Quat4f.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.java6.vecmath; 9 | 10 | /** 11 | * A 4 element quaternion represented by single precision floating point x,y,z,w coordinates. 12 | * 13 | * @author Michael Leahy 14 | */ 15 | public class Quat4f extends Tuple4f 16 | { 17 | /** 18 | * Constructs and initializes a Quat4f from the specified xyzw coordinates. 19 | * 20 | * @param x the x coordinate 21 | * @param y the y coordinate 22 | * @param z the z coordinate 23 | * @param w the w scalar component 24 | */ 25 | public Quat4f(float x, float y, float z, float w) 26 | { 27 | super(x, y, z, w); 28 | } 29 | 30 | /** 31 | * Constructs and initializes a Quat4f from the specified Quat4f. 32 | * 33 | * @param q1 the Quat4f containing the initialization x y z w data 34 | */ 35 | public Quat4f(Quat4f q1) 36 | { 37 | super(q1); 38 | } 39 | 40 | /** 41 | * Constructs and initializes a Quat4f from the specified Tuple4f. 42 | * 43 | * @param t1 the Tuple4f containing the initialization x y z w data 44 | */ 45 | public Quat4f(Tuple4f t1) 46 | { 47 | super(t1); 48 | } 49 | 50 | /** 51 | * Constructs and initializes a Quat4f to (0,0,0,0). 52 | */ 53 | public Quat4f() {} 54 | 55 | /** 56 | * Sets the value of this quaternion to the conjugate of quaternion q1. 57 | * 58 | * @param q1 the source vector 59 | */ 60 | public final void conjugate(Quat4f q1) 61 | { 62 | x = -q1.x; 63 | y = -q1.y; 64 | z = -q1.z; 65 | w = q1.w; 66 | } 67 | 68 | /** 69 | * Negate the value of of each of this quaternion's x,y,z coordinates 70 | * in place. 71 | */ 72 | public final void conjugate() 73 | { 74 | x = -x; 75 | y = -y; 76 | z = -z; 77 | } 78 | 79 | /** 80 | * Sets the value of this quaternion to the quaternion product of 81 | * quaternions q1 and q2 (this = q1 * q2). 82 | * Note that this is safe for aliasing (e.g. this can be q1 or q2). 83 | * 84 | * @param q1 the first quaternion 85 | * @param q2 the second quaternion 86 | */ 87 | public final void mul(Quat4f q1, Quat4f q2) 88 | { 89 | // store on stack for aliasing-safty 90 | set( 91 | q1.x * q2.w + q1.w * q2.x + q1.y * q2.z - q1.z * q2.y, 92 | q1.y * q2.w + q1.w * q2.y + q1.z * q2.x - q1.x * q2.z, 93 | q1.z * q2.w + q1.w * q2.z + q1.x * q2.y - q1.y * q2.x, 94 | q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z 95 | ); 96 | } 97 | 98 | /** 99 | * Sets the value of this quaternion to the quaternion product of 100 | * itself and q1 (this = this * q1). 101 | * 102 | * @param q1 the other quaternion 103 | */ 104 | public final void mul(Quat4f q1) 105 | { 106 | // store on stack for aliasing-safty 107 | set( 108 | x * q1.w + w * q1.x + y * q1.z - z * q1.y, 109 | y * q1.w + w * q1.y + z * q1.x - x * q1.z, 110 | z * q1.w + w * q1.z + x * q1.y - y * q1.x, 111 | w * q1.w - x * q1.x - y * q1.y - z * q1.z 112 | ); 113 | } 114 | 115 | /** 116 | * Multiplies quaternion q1 by the inverse of quaternion q2 and places 117 | * the value into this quaternion. The value of both argument quaternions 118 | * is preservered (this = q1 * q2^-1). 119 | * 120 | * @param q1 the left quaternion 121 | * @param q2 the right quaternion 122 | */ 123 | public final void mulInverse(Quat4f q1, Quat4f q2) 124 | { 125 | double n = norm(); 126 | // zero-div may occur. 127 | n = (n == 0.0 ? n : 1 / n); 128 | // store on stack once for aliasing-safty 129 | set( 130 | (float) ((q1.x * q2.w - q1.w * q2.x - q1.y * q2.z + q1.z * q2.y) * n), 131 | (float) ((q1.y * q2.w - q1.w * q2.y - q1.z * q2.x + q1.x * q2.z) * n), 132 | (float) ((q1.z * q2.w - q1.w * q2.z - q1.x * q2.y + q1.y * q2.x) * n), 133 | (float) ((q1.w * q2.w + q1.x * q2.x + q1.y * q2.y + q1.z * q2.z) * n) 134 | ); 135 | } 136 | 137 | /** 138 | * Multiplies this quaternion by the inverse of quaternion q1 and places 139 | * the value into this quaternion. The value of the argument quaternion 140 | * is preserved (this = this * q^-1). 141 | * 142 | * @param q1 the other quaternion 143 | */ 144 | public final void mulInverse(Quat4f q1) 145 | { 146 | double n = norm(); 147 | // zero-div may occur. 148 | n = (n == 0.0 ? n : 1 / n); 149 | // store on stack once for aliasing-safty 150 | set( 151 | (float) ((x * q1.w - w * q1.x - y * q1.z + z * q1.y) * n), 152 | (float) ((y * q1.w - w * q1.y - z * q1.x + x * q1.z) * n), 153 | (float) ((z * q1.w - w * q1.z - x * q1.y + y * q1.x) * n), 154 | (float) ((w * q1.w + x * q1.x + y * q1.y + z * q1.z) * n) 155 | ); 156 | } 157 | 158 | 159 | // helper 160 | private float norm() 161 | { 162 | return x * x + y * y + z * z + w * w; 163 | } 164 | 165 | /** 166 | * Sets the value of this quaternion to quaternion inverse of quaternion q1. 167 | * 168 | * @param q1 the quaternion to be inverted 169 | */ 170 | public final void inverse(Quat4f q1) 171 | { 172 | double n = q1.norm(); 173 | // zero-div may occur. 174 | x = (float) (-q1.x / n); 175 | y = (float) (-q1.y / n); 176 | z = (float) (-q1.z / n); 177 | w = (float) (q1.w / n); 178 | } 179 | 180 | /** 181 | * Sets the value of this quaternion to the quaternion inverse of itself. 182 | */ 183 | public final void inverse() 184 | { 185 | double n = norm(); 186 | // zero-div may occur. 187 | x = (float) (-x / n); 188 | y = (float) (-y / n); 189 | z = (float) (-z / n); 190 | w = (float) (w / n); 191 | } 192 | 193 | /** 194 | * Sets the value of this quaternion to the normalized value 195 | * of quaternion q1. 196 | * 197 | * @param q1 the quaternion to be normalized. 198 | */ 199 | public final void normalize(Quat4f q1) 200 | { 201 | double n = Math.sqrt(q1.norm()); 202 | // zero-div may occur. 203 | x = (float) (q1.x / n); 204 | y = (float) (q1.y / n); 205 | z = (float) (q1.z / n); 206 | w = (float) (q1.w / n); 207 | } 208 | 209 | /** 210 | * Sets the value of this quaternion to the rotational component of 211 | * the passed matrix. 212 | * 213 | * @param m1 the matrix4f 214 | */ 215 | public final void set(Matrix4f m1) 216 | { 217 | setFromMat( 218 | m1.m00, m1.m01, m1.m02, 219 | m1.m10, m1.m11, m1.m12, 220 | m1.m20, m1.m21, m1.m22 221 | ); 222 | } 223 | 224 | /** 225 | * Sets the value of this quaternion to the rotational component of 226 | * the passed matrix. 227 | * 228 | * @param m1 the matrix3f 229 | */ 230 | public final void set(Matrix3f m1) 231 | { 232 | setFromMat( 233 | m1.m00, m1.m01, m1.m02, 234 | m1.m10, m1.m11, m1.m12, 235 | m1.m20, m1.m21, m1.m22 236 | ); 237 | } 238 | 239 | public final void set(Quat4f q1) 240 | { 241 | x = q1.x; 242 | y = q1.y; 243 | z = q1.z; 244 | w = q1.w; 245 | } 246 | 247 | /** 248 | * Performs a great circle interpolation between this quaternion and the 249 | * quaternion parameter and places the result into this quaternion. 250 | * 251 | * @param q1 the other quaternion 252 | * @param alpha the alpha interpolation parameter 253 | */ 254 | public final void interpolate(Quat4f q1, double alpha) 255 | { 256 | // From Hoggar. 257 | normalize(); 258 | double n1 = Math.sqrt(q1.norm()); 259 | // zero-div may occur. 260 | double x1 = q1.x / n1; 261 | double y1 = q1.y / n1; 262 | double z1 = q1.z / n1; 263 | double w1 = q1.w / n1; 264 | 265 | // t is cosine (dot product) 266 | double t = x * x1 + y * y1 + z * z1 + w * w1; 267 | 268 | // same quaternion (avoid domain error) 269 | if (1.0 <= Math.abs(t)) 270 | return; 271 | 272 | // t is now theta 273 | t = Math.acos(t); 274 | 275 | double sin_t = Math.sin(t); 276 | 277 | // same quaternion (avoid zero-div) 278 | if (sin_t == 0.0) 279 | return; 280 | 281 | double s = Math.sin((1.0 - alpha) * t) / sin_t; 282 | t = Math.sin(alpha * t) / sin_t; 283 | 284 | // setComponent values 285 | x = (float) (s * x + t * x1); 286 | y = (float) (s * y + t * y1); 287 | z = (float) (s * z + t * z1); 288 | w = (float) (s * w + t * w1); 289 | } 290 | 291 | /** 292 | * Performs a great circle interpolation between quaternion q1 and 293 | * quaternion q2 and places the result into this quaternion. 294 | * 295 | * @param q1 the first quaternion 296 | * @param q2 the second quaternion 297 | * @param alpha the alpha interpolation parameter 298 | */ 299 | public final void interpolate(Quat4f q1, Quat4f q2, double alpha) 300 | { 301 | set(q1); 302 | interpolate(q2, alpha); 303 | } 304 | 305 | // helper method 306 | private void setFromMat(double m00, double m01, double m02, 307 | double m10, double m11, double m12, 308 | double m20, double m21, double m22) 309 | { 310 | // From Ken Shoemake 311 | // (ftp://ftp.cis.upenn.edu/pub/graphics/shoemake) 312 | 313 | double s; 314 | double tr = m00 + m11 + m22; 315 | if (tr >= 0.0) 316 | { 317 | s = Math.sqrt(tr + 1.0); 318 | w = (float) (s * 0.5); 319 | s = 0.5 / s; 320 | x = (float) ((m21 - m12) * s); 321 | y = (float) ((m02 - m20) * s); 322 | z = (float) ((m10 - m01) * s); 323 | } 324 | else 325 | { 326 | double max = Math.max(Math.max(m00, m11), m22); 327 | if (max == m00) 328 | { 329 | s = Math.sqrt(m00 - (m11 + m22) + 1.0); 330 | x = (float) (s * 0.5); 331 | s = 0.5 / s; 332 | y = (float) ((m01 + m10) * s); 333 | z = (float) ((m20 + m02) * s); 334 | w = (float) ((m21 - m12) * s); 335 | } 336 | else if (max == m11) 337 | { 338 | s = Math.sqrt(m11 - (m22 + m00) + 1.0); 339 | y = (float) (s * 0.5); 340 | s = 0.5 / s; 341 | z = (float) ((m12 + m21) * s); 342 | x = (float) ((m01 + m10) * s); 343 | w = (float) ((m02 - m20) * s); 344 | } 345 | else 346 | { 347 | s = Math.sqrt(m22 - (m00 + m11) + 1.0); 348 | z = (float) (s * 0.5); 349 | s = 0.5 / s; 350 | x = (float) ((m20 + m02) * s); 351 | y = (float) ((m12 + m21) * s); 352 | w = (float) ((m10 - m01) * s); 353 | } 354 | } 355 | } 356 | } 357 | -------------------------------------------------------------------------------- /java6-math/src/main/java/org/typhonrt/java6/vecmath/Tuple3f.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.java6.vecmath; 9 | 10 | /** 11 | * A generic 3 element tuple that is represented by single precision floating point x,y and z coordinates. 12 | * 13 | * @author Michael Leahy 14 | */ 15 | public class Tuple3f 16 | { 17 | /** 18 | * The x coordinate. 19 | */ 20 | public float x; 21 | 22 | /** 23 | * The y coordinate. 24 | */ 25 | public float y; 26 | 27 | /** 28 | * The z coordinate. 29 | */ 30 | public float z; 31 | 32 | /** 33 | * Constructs and initializes a Tuple3f from the specified xyz coordinates. 34 | * 35 | * @param x the x coordinate 36 | * @param y the y coordinate 37 | * @param z the z coordinate 38 | */ 39 | public Tuple3f(float x, float y, float z) 40 | { 41 | this.x = x; 42 | this.y = y; 43 | this.z = z; 44 | } 45 | 46 | /** 47 | * Constructs and initializes a Tuple3f from the specified array. 48 | * 49 | * @param t the array of length 3 containing xyz in order 50 | */ 51 | public Tuple3f(float t[]) 52 | { 53 | // ArrayIndexOutOfBounds is thrown if t.length < 3 54 | this.x = t[0]; 55 | this.y = t[1]; 56 | this.z = t[2]; 57 | } 58 | 59 | /** 60 | * Constructs and initializes a Tuple3f from the specified Tuple3f. 61 | * 62 | * @param t1 the Tuple3f containing the initialization x y z data 63 | */ 64 | public Tuple3f(Tuple3f t1) 65 | { 66 | x = t1.x; 67 | y = t1.y; 68 | z = t1.z; 69 | } 70 | 71 | /** 72 | * Constructs and initializes a Tuple3f to (0,0,0). 73 | */ 74 | public Tuple3f() 75 | { 76 | x = 0.0f; 77 | y = 0.0f; 78 | z = 0.0f; 79 | } 80 | 81 | /** 82 | * Sets the value of this tuple to the specified xyz coordinates. 83 | * 84 | * @param x the x coordinate 85 | * @param y the y coordinate 86 | * @param z the z coordinate 87 | */ 88 | public final void set(float x, float y, float z) 89 | { 90 | this.x = x; 91 | this.y = y; 92 | this.z = z; 93 | } 94 | 95 | /** 96 | * Sets the value of this tuple from the 3 values specified in the array. 97 | * 98 | * @param t the array of length 3 containing xyz in order 99 | */ 100 | public final void set(float t[]) 101 | { 102 | // ArrayIndexOutOfBounds is thrown if t.length < 3 103 | x = t[0]; 104 | y = t[1]; 105 | z = t[2]; 106 | } 107 | 108 | /** 109 | * Sets the value of this tuple from the 3 values specified in the array. 110 | * 111 | * @param t the array of length 3 containing xyz in order 112 | */ 113 | public final void set(float t[], int index) 114 | { 115 | // ArrayIndexOutOfBounds is thrown if t.length < index + 2 116 | x = t[index]; 117 | y = t[index + 1]; 118 | z = t[index + 2]; 119 | } 120 | 121 | /** 122 | * Sets the value of this tuple to the value of the Tuple3f argument. 123 | * 124 | * @param t1 the tuple to be copied 125 | */ 126 | public final void set(Tuple3f t1) 127 | { 128 | x = t1.x; 129 | y = t1.y; 130 | z = t1.z; 131 | } 132 | 133 | /** 134 | * Copies the value of the elements of this tuple into the array t[]. 135 | * 136 | * @param t the array that will contain the values of the vector 137 | */ 138 | public final void get(float t[]) 139 | { 140 | // ArrayIndexOutOfBounds is thrown if t.length < 3 141 | t[0] = x; 142 | t[1] = y; 143 | t[2] = z; 144 | } 145 | 146 | /** 147 | * Copies the value of the elements of this tuple into the array t[]. 148 | * 149 | * @param t the array that will contain the values of the vector 150 | */ 151 | public final void get(float t[], int index) 152 | { 153 | // ArrayIndexOutOfBounds is thrown if t.length < index + 2 154 | t[index] = x; 155 | t[index + 1] = y; 156 | t[index + 2] = z; 157 | } 158 | 159 | /** 160 | * Gets the value of this tuple and copies the values into the Tuple3f. 161 | * 162 | * @param t Tuple3f object into which that values of this object are copied 163 | */ 164 | public final void get(Tuple3f t) 165 | { 166 | t.x = x; 167 | t.y = y; 168 | t.z = z; 169 | } 170 | 171 | /** 172 | * Sets the value of this tuple to the vector sum of tuples t1 and t2. 173 | * 174 | * @param t1 the first tuple 175 | * @param t2 the second tuple 176 | */ 177 | public final void add(Tuple3f t1, Tuple3f t2) 178 | { 179 | x = t1.x + t2.x; 180 | y = t1.y + t2.y; 181 | z = t1.z + t2.z; 182 | } 183 | 184 | /** 185 | * Sets the value of this tuple to the vector sum of itself and tuple t1. 186 | * 187 | * @param t1 the other tuple 188 | */ 189 | public final void add(Tuple3f t1) 190 | { 191 | x += t1.x; 192 | y += t1.y; 193 | z += t1.z; 194 | } 195 | 196 | 197 | /** 198 | * Sets the value of this tuple to the vector difference of tuple t1 and t2 (this = t1 - t2). 199 | * 200 | * @param t1 the first tuple 201 | * @param t2 the second tuple 202 | */ 203 | public final void sub(Tuple3f t1, Tuple3f t2) 204 | { 205 | x = t1.x - t2.x; 206 | y = t1.y - t2.y; 207 | z = t1.z - t2.z; 208 | } 209 | 210 | /** 211 | * Sets the value of this tuple to the vector difference of itself and tuple t1 (this = this - t1). 212 | * 213 | * @param t1 the other tuple 214 | */ 215 | public final void sub(Tuple3f t1) 216 | { 217 | x -= t1.x; 218 | y -= t1.y; 219 | z -= t1.z; 220 | } 221 | 222 | /** 223 | * Calculates the midpoint between this tuple and another. 224 | * (e.g. x from x value, y from y, etc.) 225 | * 226 | * @param t The other tuple to subtract 227 | */ 228 | public final void mid(Tuple3f t) 229 | { 230 | x = (x + t.x) / 2; 231 | y = (y + t.y) / 2; 232 | z = (z + t.z) / 2; 233 | } 234 | 235 | /** 236 | * Sets the value of this tuple to the negation of tuple t1. 237 | * 238 | * @param t1 the source vector 239 | */ 240 | public final void negate(Tuple3f t1) 241 | { 242 | x = -t1.x; 243 | y = -t1.y; 244 | z = -t1.z; 245 | } 246 | 247 | /** 248 | * Negates the value of this vector in place. 249 | */ 250 | public final void negate() 251 | { 252 | x = -x; 253 | y = -y; 254 | z = -z; 255 | } 256 | 257 | /** 258 | * Multiplies each of the x,y,z components of the Tuple4f parameter 259 | * by 1/w and places the projected values into this point. 260 | * 261 | * @param p1 the source Tuple4f, which is not modified 262 | */ 263 | public final void project(Tuple4f p1) 264 | { 265 | // zero div may occur. 266 | x = p1.x / p1.w; 267 | y = p1.y / p1.w; 268 | z = p1.z / p1.w; 269 | } 270 | 271 | /** 272 | * Sets the value of this tuple to the scalar multiplication of tuple t1. 273 | * 274 | * @param s the scalar value 275 | * @param t1 the source tuple 276 | */ 277 | public final void scale(float s, Tuple3f t1) 278 | { 279 | x = s * t1.x; 280 | y = s * t1.y; 281 | z = s * t1.z; 282 | } 283 | 284 | /** 285 | * Sets the value of this tuple to the scalar multiplication of itself. 286 | * 287 | * @param s the scalar value 288 | */ 289 | public final void scale(float s) 290 | { 291 | x *= s; 292 | y *= s; 293 | z *= s; 294 | } 295 | 296 | /** 297 | * Sets the value of this tuple to the scalar multiplication of itself. 298 | * 299 | * @param scaleX the scalar value 300 | * @param scaleY the scalar value 301 | * @param scaleZ the scalar value 302 | */ 303 | public final void scale(float scaleX, float scaleY, float scaleZ) 304 | { 305 | x *= scaleX; 306 | y *= scaleY; 307 | z *= scaleZ; 308 | } 309 | 310 | /** 311 | * Sets the value of this tuple to the scalar multiplication of tuple t1 and then 312 | * adds tuple t2 (this = s*t1 + t2). 313 | * 314 | * @param s the scalar value 315 | * @param t1 the tuple to be multipled 316 | * @param t2 the tuple to be added 317 | */ 318 | public final void scaleAdd(float s, Tuple3f t1, Tuple3f t2) 319 | { 320 | x = s * t1.x + t2.x; 321 | y = s * t1.y + t2.y; 322 | z = s * t1.z + t2.z; 323 | } 324 | 325 | /** 326 | * Sets the value of this tuple to the scalar multiplication of itself and then 327 | * adds tuple t1 (this = s*this + t1). 328 | * 329 | * @param s the scalar value 330 | * @param t1 the tuple to be added 331 | */ 332 | public final void scaleAdd(float s, Tuple3f t1) 333 | { 334 | x = s * x + t1.x; 335 | y = s * y + t1.y; 336 | z = s * z + t1.z; 337 | } 338 | 339 | /** 340 | * Returns a hash number based on the data values in this object. 341 | * Two different Tuple3f objects with identical data values 342 | * (ie, returns true for equals(Tuple3f) ) will return the same hash number. 343 | * Two vectors with different data members may return the same hash value, 344 | * although this is not likely. 345 | */ 346 | @Override 347 | public int hashCode() 348 | { 349 | int xbits = Float.floatToIntBits(x); 350 | int ybits = Float.floatToIntBits(y); 351 | int zbits = Float.floatToIntBits(z); 352 | return xbits ^ ybits ^ zbits; 353 | } 354 | 355 | /** 356 | * Returns true if all of the data members of Tuple3f t1 are equal to the corresponding 357 | * data members in this 358 | * 359 | * @param t1 the vector with which the comparison is made. 360 | */ 361 | public boolean equals(Tuple3f t1) 362 | { 363 | return t1 != null && x == t1.x && y == t1.y && z == t1.z; 364 | } 365 | 366 | /** 367 | * Returns true if the L-infinite distance between this tuple and tuple t1 is 368 | * less than or equal to the epsilon parameter, otherwise returns false. The L-infinite 369 | * distance is equal to MAX[abs(x1-x2), abs(y1-y2)]. 370 | * 371 | * @param t1 the tuple to be compared to this tuple 372 | * @param epsilon the threshold value 373 | */ 374 | public boolean epsilonEquals(Tuple3f t1, float epsilon) 375 | { 376 | return (Math.abs(t1.x - this.x) <= epsilon) && 377 | (Math.abs(t1.y - this.y) <= epsilon) && 378 | (Math.abs(t1.z - this.z) <= epsilon); 379 | } 380 | 381 | /** 382 | * Returns a string that contains the values of this Tuple3f. The form is (x,y,z). 383 | * 384 | * @return the String representation 385 | */ 386 | @Override 387 | public String toString() 388 | { 389 | StringBuilder sb = new StringBuilder(VecmathStrings.s_STR_OPEN_X); 390 | sb.append(x).append(VecmathStrings.s_STR_Y).append(y).append(VecmathStrings.s_STR_Z).append(z).append(VecmathStrings.s_STR_CLOSE); 391 | return sb.toString(); 392 | } 393 | 394 | /** 395 | * Clamps the tuple parameter to the range [low, high] and places the values 396 | * into this tuple. 397 | * 398 | * @param min the lowest value in the tuple after clamping 399 | * @param max the highest value in the tuple after clamping 400 | * @param t the source tuple, which will not be modified 401 | */ 402 | public final void clamp(float min, float max, Tuple3f t) 403 | { 404 | set(t); 405 | clamp(min, max); 406 | } 407 | 408 | /** 409 | * Clamps the minimum value of the tuple parameter to the min parameter 410 | * and places the values into this tuple. 411 | * 412 | * @param min the lowest value in the tuple after clamping 413 | * @param t the source tuple, which will not be modified 414 | */ 415 | public final void clampMin(float min, Tuple3f t) 416 | { 417 | set(t); 418 | clampMin(min); 419 | } 420 | 421 | /** 422 | * Clamps the maximum value of the tuple parameter to the max parameter and 423 | * places the values into this tuple. 424 | * 425 | * @param max the highest value in the tuple after clamping 426 | * @param t the source tuple, which will not be modified 427 | */ 428 | public final void clampMax(float max, Tuple3f t) 429 | { 430 | set(t); 431 | clampMax(max); 432 | } 433 | 434 | 435 | /** 436 | * Sets each component of the tuple parameter to its absolute value and 437 | * places the modified values into this tuple. 438 | * 439 | * @param t the source tuple, which will not be modified 440 | */ 441 | public final void absolute(Tuple3f t) 442 | { 443 | set(t); 444 | absolute(); 445 | } 446 | 447 | /** 448 | * Clamps this tuple to the range [low, high]. 449 | * 450 | * @param min the lowest value in this tuple after clamping 451 | * @param max the highest value in this tuple after clamping 452 | */ 453 | public final void clamp(float min, float max) 454 | { 455 | clampMin(min); 456 | clampMax(max); 457 | } 458 | 459 | /** 460 | * Clamps the minimum value of this tuple to the min parameter. 461 | * 462 | * @param min the lowest value in this tuple after clamping 463 | */ 464 | public final void clampMin(float min) 465 | { 466 | if (x < min) 467 | x = min; 468 | if (y < min) 469 | y = min; 470 | if (z < min) 471 | z = min; 472 | } 473 | 474 | /** 475 | * Clamps the maximum value of this tuple to the max parameter. 476 | * 477 | * @param max the highest value in the tuple after clamping 478 | */ 479 | public final void clampMax(float max) 480 | { 481 | if (x > max) 482 | x = max; 483 | if (y > max) 484 | y = max; 485 | if (z > max) 486 | z = max; 487 | } 488 | 489 | /** 490 | * Sets each component of this tuple to its absolute value. 491 | */ 492 | public final void absolute() 493 | { 494 | if (x < 0.0) 495 | x = -x; 496 | if (y < 0.0) 497 | y = -y; 498 | if (z < 0.0) 499 | z = -z; 500 | } 501 | 502 | /** 503 | * Linearly interpolates between tuples t1 and t2 and places the 504 | * result into this tuple: this = (1-alpha)*t1 + alpha*t2. 505 | * 506 | * @param t1 the first tuple 507 | * @param t2 the second tuple 508 | * @param alpha the alpha interpolation parameter 509 | */ 510 | public final void interpolate(Tuple3f t1, Tuple3f t2, float alpha) 511 | { 512 | set(t1); 513 | interpolate(t2, alpha); 514 | } 515 | 516 | 517 | /** 518 | * Linearly interpolates between this tuple and tuple t1 and places the 519 | * result into this tuple: this = (1-alpha)*this + alpha*t1. 520 | * 521 | * @param t1 the first tuple 522 | * @param alpha the alpha interpolation parameter 523 | */ 524 | public final void interpolate(Tuple3f t1, float alpha) 525 | { 526 | float beta = 1 - alpha; 527 | x = beta * x + alpha * t1.x; 528 | y = beta * y + alpha * t1.y; 529 | z = beta * z + alpha * t1.z; 530 | } 531 | 532 | /** 533 | * Returns if this Tuple is normalized by checking for length being 1 534 | * @return 535 | */ 536 | public final boolean isNormal() 537 | { 538 | // return Math.abs(length - 1.0F) < 0.001F; 539 | return Math.abs((float) Math.sqrt(x * x + y * y + z * z) - 1.0F) < 0.001F; 540 | } 541 | 542 | /** 543 | * Returns if the x, y, z components are 1 544 | * 545 | * @return boolean 546 | */ 547 | public final boolean isOne() 548 | { 549 | return x == 1 && y == 1 && z == 1; 550 | } 551 | 552 | /** 553 | * Returns if the x, y, z components are within unity magnitudes 554 | * 555 | * @return boolean 556 | */ 557 | public final boolean isUnity() 558 | { 559 | return (x >= -1 && x <= 1) && (y >= -1 && y <= 1) && (z >= -1 && z <= 1); 560 | } 561 | 562 | /** 563 | * Returns if the x, y, z components are 0 564 | * 565 | * @return boolean 566 | */ 567 | public final boolean isZero() 568 | { 569 | return x == 0 && y == 0 && z == 0; 570 | } 571 | 572 | /** 573 | * Computes the square of the distance between this tuple and tuple t1. 574 | * 575 | * @param t1 the other tuple 576 | * @return the square of distance between these two tuples as a float 577 | */ 578 | public final float distanceSquaredF(Tuple3f t1) 579 | { 580 | float dx = x - t1.x; 581 | float dy = y - t1.y; 582 | float dz = z - t1.z; 583 | return dx * dx + dy * dy + dz * dz; 584 | } 585 | 586 | /** 587 | * Returns the distance between this tuple and tuple t1. 588 | * 589 | * @param t1 the other tuple 590 | * @return the distance between these two tuples as a float 591 | */ 592 | public final float distanceF(Tuple3f t1) 593 | { 594 | float dx = x - t1.x; 595 | float dy = y - t1.y; 596 | float dz = z - t1.z; 597 | return (float)Math.sqrt(dx * dx + dy * dy + dz * dz); 598 | } 599 | 600 | /** 601 | * Computes the L-1 (Manhattan) distance between this tuple and tuple t1. 602 | * The L-1 distance is equal to abs(x1-x2) + abs(y1-y2). 603 | * 604 | * @param t1 the other tuple 605 | */ 606 | public final float distanceL1F(Tuple3f t1) 607 | { 608 | return Math.abs(x - t1.x) + Math.abs(y - t1.y) + Math.abs(z - t1.z); 609 | } 610 | 611 | /** 612 | * Computes the L-infinite distance between this tuple and tuple t1. 613 | * The L-infinite distance is equal to MAX[abs(x1-x2), abs(y1-y2)]. 614 | * 615 | * @param t1 the other tuple 616 | */ 617 | public final float distanceLinfF(Tuple3f t1) 618 | { 619 | return Math.max(Math.max(Math.abs(x - t1.x), Math.abs(y - t1.y)), Math.abs(z - t1.z)); 620 | } 621 | 622 | /** 623 | * Computes the square of the distance between this tuple and tuple t1. 624 | * 625 | * @param t1 the other tuple 626 | * @return the square of distance between these two tuples as a float 627 | */ 628 | public final double distanceSquaredD(Tuple3f t1) 629 | { 630 | float dx = x - t1.x; 631 | float dy = y - t1.y; 632 | float dz = z - t1.z; 633 | return dx * dx + dy * dy + dz * dz; 634 | } 635 | 636 | /** 637 | * Returns the distance between this tuple and tuple t1. 638 | * 639 | * @param t1 the other tuple 640 | * @return the distance between these two tuples as a float 641 | */ 642 | public final double distanceD(Tuple3f t1) 643 | { 644 | float dx = x - t1.x; 645 | float dy = y - t1.y; 646 | float dz = z - t1.z; 647 | return Math.sqrt(dx * dx + dy * dy + dz * dz); 648 | } 649 | 650 | /** 651 | * Computes the L-1 (Manhattan) distance between this tuple and tuple t1. 652 | * The L-1 distance is equal to abs(x1-x2) + abs(y1-y2). 653 | * 654 | * @param t1 the other tuple 655 | */ 656 | public final double distanceL1D(Tuple3f t1) 657 | { 658 | return Math.abs(x - t1.x) + Math.abs(y - t1.y) + Math.abs(z - t1.z); 659 | } 660 | 661 | /** 662 | * Computes the L-infinite distance between this tuple and tuple t1. 663 | * The L-infinite distance is equal to MAX[abs(x1-x2), abs(y1-y2)]. 664 | * 665 | * @param t1 the other tuple 666 | */ 667 | public final double distanceLinfD(Tuple3f t1) 668 | { 669 | return Math.max(Math.max(Math.abs(x - t1.x), Math.abs(y - t1.y)), Math.abs(z - t1.z)); 670 | } 671 | 672 | /** 673 | * Returns the squared length of this vector. 674 | * 675 | * @return the squared length of this vector 676 | */ 677 | public final float lengthSquared() 678 | { 679 | return x * x + y * y + z * z; 680 | } 681 | 682 | /** 683 | * Returns the length of this vector. 684 | * 685 | * @return the length of this vector 686 | */ 687 | public final float length() 688 | { 689 | return (float) Math.sqrt(x * x + y * y + z * z); 690 | } 691 | 692 | /** 693 | * Sets this vector to be the vector cross product of vectors v1 and v2. 694 | * 695 | * @param v1 the first vector 696 | * @param v2 the second vector 697 | */ 698 | public final void cross(Vector3f v1, Vector3f v2) 699 | { 700 | set( 701 | v1.y * v2.z - v1.z * v2.y, 702 | v1.z * v2.x - v1.x * v2.z, 703 | v1.x * v2.y - v1.y * v2.x 704 | ); 705 | } 706 | 707 | /** 708 | * Computes the dot product of the this vector and vector v1. 709 | * 710 | * @param v1 the other vector 711 | */ 712 | public final float dot(Vector3f v1) 713 | { 714 | return x * v1.x + y * v1.y + z * v1.z; 715 | } 716 | 717 | /** 718 | * Sets the value of this vector to the normalization of vector v1. 719 | * 720 | * @param v1 the un-normalized vector 721 | */ 722 | public final void normalize(Vector3f v1) 723 | { 724 | set(v1); 725 | normalize(); 726 | } 727 | 728 | /** 729 | * Normalizes this vector in place. 730 | */ 731 | public final void normalize() 732 | { 733 | float d = (float)Math.sqrt(x * x + y * y + z * z); 734 | 735 | // zero-div may occur. 736 | x /= d; 737 | y /= d; 738 | z /= d; 739 | } 740 | 741 | /** 742 | * Returns the angle in radians between this vector and 743 | * the vector parameter; the return value is constrained to the 744 | * range [0,PI]. 745 | * 746 | * @param v1 the other vector 747 | * @return the angle in radians in the range [0,PI] 748 | */ 749 | public final float angle(Vector3f v1) 750 | { 751 | // return (double)Math.acos(dot(v1)/v1.length()/v.length()); 752 | // Numerically, near 0 and PI are very bad condition for acos. 753 | // In 3-space, |atan2(sin,cos)| is much stable. 754 | 755 | float xx = y * v1.z - z * v1.y; 756 | float yy = z * v1.x - x * v1.z; 757 | float zz = x * v1.y - y * v1.x; 758 | float cross = (float)Math.sqrt(xx * xx + yy * yy + zz * zz); 759 | 760 | return (float) Math.abs(Math.atan2(cross, dot(v1))); 761 | } 762 | } 763 | -------------------------------------------------------------------------------- /java6-math/src/main/java/org/typhonrt/java6/vecmath/Tuple4f.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.java6.vecmath; 9 | 10 | /** 11 | * A generic 4 element tuple that is represented by single precision floating point x,y,z and w coordinates. 12 | * 13 | * @author Michael Leahy 14 | */ 15 | public class Tuple4f 16 | { 17 | /** 18 | * The x coordinate. 19 | */ 20 | public float x; 21 | 22 | /** 23 | * The y coordinate. 24 | */ 25 | public float y; 26 | 27 | /** 28 | * The z coordinate. 29 | */ 30 | public float z; 31 | 32 | /** 33 | * The w coordinate. 34 | */ 35 | public float w; 36 | 37 | /** 38 | * Constructs and initializes a Tuple4f from the specified xyzw coordinates. 39 | * 40 | * @param x the x coordinate 41 | * @param y the y coordinate 42 | * @param z the z coordinate 43 | * @param w the w coordinate 44 | */ 45 | public Tuple4f(float x, float y, float z, float w) 46 | { 47 | set(x, y, z, w); 48 | } 49 | 50 | /** 51 | * Constructs and initializes a Tuple4f from the specified array. 52 | * 53 | * @param t the array of length 4 containing xyzw in order 54 | */ 55 | public Tuple4f(float t[]) 56 | { 57 | set(t); 58 | } 59 | 60 | /** 61 | * Constructs and initializes a Tuple4f from the specified Tuple4f. 62 | * 63 | * @param t1 the Tuple4f containing the initialization x y z w data 64 | */ 65 | public Tuple4f(Tuple4f t1) 66 | { 67 | set(t1); 68 | } 69 | 70 | /** 71 | * Constructs and initializes a Tuple4f to (0,0,0,0). 72 | */ 73 | public Tuple4f() 74 | { 75 | x = 0.0f; 76 | y = 0.0f; 77 | z = 0.0f; 78 | w = 0.0f; 79 | } 80 | 81 | /** 82 | * Sets the value of this tuple to the specified xyzw coordinates. 83 | * 84 | * @param x the x coordinate 85 | * @param y the y coordinate 86 | * @param z the z coordinate 87 | * @param w the w coordinate 88 | */ 89 | public final Tuple4f set(float x, float y, float z, float w) 90 | { 91 | this.x = x; 92 | this.y = y; 93 | this.z = z; 94 | this.w = w; 95 | return this; 96 | } 97 | 98 | /** 99 | * Sets the value of this tuple from the 4 values specified in the array. 100 | * 101 | * @param t the array of length 4 containing xyzw in order 102 | */ 103 | public final Tuple4f set(float t[]) 104 | { 105 | // ArrayIndexOutOfBounds is thrown if t.length < 4 106 | x = t[0]; 107 | y = t[1]; 108 | z = t[2]; 109 | w = t[3]; 110 | return this; 111 | } 112 | 113 | /** 114 | * Sets the value of this tuple from the 4 values specified in the array. 115 | * 116 | * @param t the array of length 4 containing xyzw in order 117 | */ 118 | public final Tuple4f set(float t[], int index) 119 | { 120 | // ArrayIndexOutOfBounds is thrown if t.length < index + 3 121 | x = t[index]; 122 | y = t[index + 1]; 123 | z = t[index + 2]; 124 | w = t[index + 3]; 125 | return this; 126 | } 127 | 128 | /** 129 | * Sets the value of this tuple to the value of tuple t1. 130 | * 131 | * @param t1 the tuple to be copied 132 | */ 133 | public final Tuple4f set(Tuple4f t1) 134 | { 135 | x = t1.x; 136 | y = t1.y; 137 | z = t1.z; 138 | w = t1.w; 139 | return this; 140 | } 141 | 142 | /** 143 | * Sets the x,y,z components of this point to the corresponding 144 | * components of tuple t1. The w component of this point is setComponent to 0. 145 | * 146 | * @param t1 the tuple to be copied 147 | * @since Java3D 1.2 148 | */ 149 | public final Tuple4f set(Tuple3f t1) 150 | { 151 | x = t1.x; 152 | y = t1.y; 153 | z = t1.z; 154 | w = 0.0f; 155 | return this; 156 | } 157 | 158 | /** 159 | * Copies the value of the elements of this tuple into the array t[]. 160 | * 161 | * @param t the array that will contain the values of the vector 162 | */ 163 | public final Tuple4f get(float t[]) 164 | { 165 | // ArrayIndexOutOfBounds is thrown if t.length < 4 166 | t[0] = x; 167 | t[1] = y; 168 | t[2] = z; 169 | t[3] = w; 170 | return this; 171 | } 172 | 173 | /** 174 | * Copies the value of the elements of this tuple into the array t[]. 175 | * 176 | * @param t the array that will contain the values of the vector 177 | */ 178 | public final Tuple4f get(float t[], int index) 179 | { 180 | // ArrayIndexOutOfBounds is thrown if t.length < index + 3 181 | t[index] = x; 182 | t[index + 1] = y; 183 | t[index + 2] = z; 184 | t[index + 3] = w; 185 | return this; 186 | } 187 | 188 | /** 189 | * Gets the value of this tuple and copies the values into the Tuple4f. 190 | * 191 | * @param t Tuple4f object into which that values of this object are copied 192 | */ 193 | public final Tuple4f get(Tuple4f t) 194 | { 195 | t.x = x; 196 | t.y = y; 197 | t.z = z; 198 | t.w = w; 199 | return this; 200 | } 201 | 202 | /** 203 | * Sets the value of this tuple to the vector sum of tuples t1 and t2. 204 | * 205 | * @param t1 the first tuple 206 | * @param t2 the second tuple 207 | */ 208 | public final Tuple4f add(Tuple4f t1, Tuple4f t2) 209 | { 210 | x = t1.x + t2.x; 211 | y = t1.y + t2.y; 212 | z = t1.z + t2.z; 213 | w = t1.w + t2.w; 214 | return this; 215 | } 216 | 217 | /** 218 | * Sets the value of this tuple to the vector sum of itself and tuple t1. 219 | * 220 | * @param t1 the other tuple 221 | */ 222 | public final Tuple4f add(Tuple4f t1) 223 | { 224 | x += t1.x; 225 | y += t1.y; 226 | z += t1.z; 227 | w += t1.w; 228 | return this; 229 | } 230 | 231 | 232 | /** 233 | * Sets the value of this tuple to the vector difference of tuple t1 and t2 (this = t1 - t2). 234 | * 235 | * @param t1 the first tuple 236 | * @param t2 the second tuple 237 | */ 238 | public final Tuple4f sub(Tuple4f t1, Tuple4f t2) 239 | { 240 | x = t1.x - t2.x; 241 | y = t1.y - t2.y; 242 | z = t1.z - t2.z; 243 | w = t1.w - t2.w; 244 | return this; 245 | } 246 | 247 | /** 248 | * Sets the value of this tuple to the vector difference of itself and tuple t1 (this = this - t1). 249 | * 250 | * @param t1 the other tuple 251 | */ 252 | public final Tuple4f sub(Tuple4f t1) 253 | { 254 | x -= t1.x; 255 | y -= t1.y; 256 | z -= t1.z; 257 | w -= t1.w; 258 | return this; 259 | } 260 | 261 | /** 262 | * Calculates the midpoint between this tuple and another. 263 | * (e.g. x from x value, y from y, etc.) 264 | * 265 | * @param t The other tuple to subtract 266 | */ 267 | public final Tuple4f mid(Tuple4f t) 268 | { 269 | x = (x + t.x) / 2; 270 | y = (y + t.y) / 2; 271 | z = (z + t.z) / 2; 272 | w = (w + t.w) / 2; 273 | return this; 274 | } 275 | 276 | /** 277 | * Sets the value of this tuple to the negation of tuple t1. 278 | * 279 | * @param t1 the source vector 280 | */ 281 | public final Tuple4f negate(Tuple4f t1) 282 | { 283 | x = -t1.x; 284 | y = -t1.y; 285 | z = -t1.z; 286 | w = -t1.w; 287 | return this; 288 | } 289 | 290 | /** 291 | * Negates the value of this vector in place. 292 | */ 293 | public final Tuple4f negate() 294 | { 295 | x = -x; 296 | y = -y; 297 | z = -z; 298 | w = -w; 299 | return this; 300 | } 301 | 302 | /** 303 | * Normalizes this vector in place. 304 | */ 305 | public final void normalize() 306 | { 307 | double d = Math.sqrt(x * x + y * y + z * z + w * w); 308 | 309 | // zero-div may occur. 310 | x /= d; 311 | y /= d; 312 | z /= d; 313 | w /= d; 314 | } 315 | 316 | /** 317 | * Multiplies each of the x,y,z components of the Tuple4f parameter by 1/w, 318 | * places the projected values into this point, and places a 1 as the w 319 | * parameter of this point. 320 | * 321 | * @param p1 the source Tuple4f, which is not modified 322 | */ 323 | public final void project(Tuple4f p1) 324 | { 325 | // zero div may occur. 326 | x = p1.x / p1.w; 327 | y = p1.y / p1.w; 328 | z = p1.z / p1.w; 329 | w = 1.0f; 330 | } 331 | 332 | /** 333 | * Sets the value of this tuple to the scalar multiplication of tuple t1. 334 | * 335 | * @param s the scalar value 336 | * @param t1 the source tuple 337 | */ 338 | public final Tuple4f scale(float s, Tuple4f t1) 339 | { 340 | x = s * t1.x; 341 | y = s * t1.y; 342 | z = s * t1.z; 343 | w = s * t1.w; 344 | return this; 345 | } 346 | 347 | /** 348 | * Sets the value of this tuple to the scalar multiplication of itself. 349 | * 350 | * @param s the scalar value 351 | */ 352 | public final Tuple4f scale(float s) 353 | { 354 | x *= s; 355 | y *= s; 356 | z *= s; 357 | w *= s; 358 | return this; 359 | } 360 | 361 | /** 362 | * Sets the value of this tuple to the scalar multiplication of tuple t1 and then 363 | * adds tuple t2 (this = s*t1 + t2). 364 | * 365 | * @param s the scalar value 366 | * @param t1 the tuple to be multipled 367 | * @param t2 the tuple to be added 368 | */ 369 | public final Tuple4f scaleAdd(float s, Tuple4f t1, Tuple4f t2) 370 | { 371 | x = s * t1.x + t2.x; 372 | y = s * t1.y + t2.y; 373 | z = s * t1.z + t2.z; 374 | w = s * t1.w + t2.w; 375 | return this; 376 | } 377 | 378 | /** 379 | * Sets the value of this tuple to the scalar multiplication of itself and then 380 | * adds tuple t1 (this = s*this + t1). 381 | * 382 | * @param s the scalar value 383 | * @param t1 the tuple to be added 384 | */ 385 | public final Tuple4f scaleAdd(float s, Tuple4f t1) 386 | { 387 | x = s * x + t1.x; 388 | y = s * y + t1.y; 389 | z = s * z + t1.z; 390 | w = s * z + t1.w; 391 | return this; 392 | } 393 | 394 | /** 395 | * Returns a hash number based on the data values in this object. 396 | * Two different Tuple4f objects with identical data values 397 | * (ie, returns true for equals(Tuple4f) ) will return the same hash number. 398 | * Two vectors with different data members may return the same hash value, 399 | * although this is not likely. 400 | */ 401 | @Override 402 | public int hashCode() 403 | { 404 | return Float.floatToIntBits(x) ^ 405 | Float.floatToIntBits(y) ^ 406 | Float.floatToIntBits(z) ^ 407 | Float.floatToIntBits(w); 408 | } 409 | 410 | /** 411 | * Returns true if all of the data members of Tuple4f t1 are equal to the corresponding 412 | * data members in this 413 | * 414 | * @param t1 the vector with which the comparison is made. 415 | */ 416 | public boolean equals(Tuple4f t1) 417 | { 418 | return t1 != null && x == t1.x && y == t1.y && z == t1.z && w == t1.w; 419 | } 420 | 421 | /** 422 | * Returns true if the L-infinite distance between this tuple and tuple t1 is 423 | * less than or equal to the epsilon parameter, otherwise returns false. The L-infinite 424 | * distance is equal to MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2), abs(w1-w2)]. 425 | * 426 | * @param t1 the tuple to be compared to this tuple 427 | * @param epsilon the threshold value 428 | */ 429 | public boolean epsilonEquals(Tuple4f t1, float epsilon) 430 | { 431 | return (Math.abs(t1.x - this.x) <= epsilon) && 432 | (Math.abs(t1.y - this.y) <= epsilon) && 433 | (Math.abs(t1.z - this.z) <= epsilon) && 434 | (Math.abs(t1.w - this.w) <= epsilon); 435 | } 436 | 437 | /** 438 | * Returns a string that contains the values of this Tuple4f. The form is (x,y,z,w). 439 | * 440 | * @return the String representation 441 | */ 442 | @Override 443 | public String toString() 444 | { 445 | StringBuilder sb = new StringBuilder(VecmathStrings.s_STR_OPEN_X); 446 | sb.append(x).append(VecmathStrings.s_STR_Y).append(y).append(VecmathStrings.s_STR_Z).append(z).append(VecmathStrings.s_STR_W).append(w).append(VecmathStrings.s_STR_CLOSE); 447 | return sb.toString(); 448 | } 449 | 450 | /** 451 | * Clamps the tuple parameter to the range [low, high] and places the values 452 | * into this tuple. 453 | * 454 | * @param min the lowest value in the tuple after clamping 455 | * @param max the highest value in the tuple after clamping 456 | * @param t the source tuple, which will not be modified 457 | */ 458 | public final Tuple4f clamp(float min, float max, Tuple4f t) 459 | { 460 | set(t); 461 | clamp(min, max); 462 | return this; 463 | } 464 | 465 | /** 466 | * Clamps the minimum value of the tuple parameter to the min parameter 467 | * and places the values into this tuple. 468 | * 469 | * @param min the lowest value in the tuple after clamping 470 | * @param t the source tuple, which will not be modified 471 | */ 472 | public final Tuple4f clampMin(float min, Tuple4f t) 473 | { 474 | set(t); 475 | clampMin(min); 476 | return this; 477 | } 478 | 479 | /** 480 | * Clamps the maximum value of the tuple parameter to the max parameter and 481 | * places the values into this tuple. 482 | * 483 | * @param max the highest value in the tuple after clamping 484 | * @param t the source tuple, which will not be modified 485 | */ 486 | public final Tuple4f clampMax(float max, Tuple4f t) 487 | { 488 | set(t); 489 | clampMax(max); 490 | return this; 491 | } 492 | 493 | 494 | /** 495 | * Sets each component of the tuple parameter to its absolute value and 496 | * places the modified values into this tuple. 497 | * 498 | * @param t the source tuple, which will not be modified 499 | */ 500 | public final Tuple4f absolute(Tuple4f t) 501 | { 502 | set(t); 503 | absolute(); 504 | return this; 505 | } 506 | 507 | /** 508 | * Clamps this tuple to the range [low, high]. 509 | * 510 | * @param min the lowest value in this tuple after clamping 511 | * @param max the highest value in this tuple after clamping 512 | */ 513 | public final Tuple4f clamp(float min, float max) 514 | { 515 | clampMin(min); 516 | clampMax(max); 517 | return this; 518 | } 519 | 520 | /** 521 | * Clamps the minimum value of this tuple to the min parameter. 522 | * 523 | * @param min the lowest value in this tuple after clamping 524 | */ 525 | public final Tuple4f clampMin(float min) 526 | { 527 | if (x < min) 528 | x = min; 529 | if (y < min) 530 | y = min; 531 | if (z < min) 532 | z = min; 533 | if (w < min) 534 | w = min; 535 | return this; 536 | } 537 | 538 | /** 539 | * Clamps the maximum value of this tuple to the max parameter. 540 | * 541 | * @param max the highest value in the tuple after clamping 542 | */ 543 | public final Tuple4f clampMax(float max) 544 | { 545 | if (x > max) 546 | x = max; 547 | if (y > max) 548 | y = max; 549 | if (z > max) 550 | z = max; 551 | if (w > max) 552 | w = max; 553 | return this; 554 | } 555 | 556 | /** 557 | * Sets each component of this tuple to its absolute value. 558 | */ 559 | public final Tuple4f absolute() 560 | { 561 | if (x < 0.0) 562 | x = -x; 563 | if (y < 0.0) 564 | y = -y; 565 | if (z < 0.0) 566 | z = -z; 567 | if (w < 0.0) 568 | w = -w; 569 | return this; 570 | } 571 | 572 | /** 573 | * Linearly interpolates between tuples t1 and t2 and places the 574 | * result into this tuple: this = (1-alpha)*t1 + alpha*t2. 575 | * 576 | * @param t1 the first tuple 577 | * @param t2 the second tuple 578 | * @param alpha the alpha interpolation parameter 579 | */ 580 | public final Tuple4f interpolate(Tuple4f t1, Tuple4f t2, float alpha) 581 | { 582 | set(t1); 583 | interpolate(t2, alpha); 584 | return this; 585 | } 586 | 587 | 588 | /** 589 | * Linearly interpolates between this tuple and tuple t1 and places the 590 | * result into this tuple: this = (1-alpha)*this + alpha*t1. 591 | * 592 | * @param t1 the first tuple 593 | * @param alpha the alpha interpolation parameter 594 | */ 595 | public final Tuple4f interpolate(Tuple4f t1, float alpha) 596 | { 597 | float beta = 1 - alpha; 598 | x = beta * x + alpha * t1.x; 599 | y = beta * y + alpha * t1.y; 600 | z = beta * z + alpha * t1.z; 601 | w = beta * w + alpha * t1.w; 602 | return this; 603 | } 604 | 605 | /** 606 | * Returns if this Tuple is normalized by checking for length being 1 607 | * @return 608 | */ 609 | public final boolean isNormal() 610 | { 611 | // return Math.abs(length - 1.0F) < 0.001F; 612 | return Math.abs((float) Math.sqrt(x * x + y * y + z * z + w * w) - 1.0F) < 0.001F; 613 | } 614 | 615 | /** 616 | * Returns if the x, y, z, w components are 1 617 | * 618 | * @return boolean 619 | */ 620 | public final boolean isOne() 621 | { 622 | return x == 1 && y == 1 && z == 1 && w == 1; 623 | } 624 | 625 | /** 626 | * Returns if the x, y, z, w components are within unity magnitudes 627 | * 628 | * @return boolean 629 | */ 630 | public final boolean isUnity() 631 | { 632 | return (x >= -1 && x <= 1) && (y >= -1 && y <= 1) && (z >= -1 && z <= 1) && (w >= -1 && w <= 1); 633 | } 634 | 635 | /** 636 | * Returns if the x, y, z, w components are 0 637 | * 638 | * @return boolean 639 | */ 640 | public final boolean isZero() 641 | { 642 | return x == 0 && y == 0 && z == 0 && w == 0; 643 | } 644 | 645 | /** 646 | * Computes the square of the distance between this tuple and tuple t1. 647 | * 648 | * @param t1 the other tuple 649 | * @return the square of distance between this tuple and t1 650 | */ 651 | public final float distanceSquaredF(Tuple4f t1) 652 | { 653 | float dx = x - t1.x; 654 | float dy = y - t1.y; 655 | float dz = z - t1.z; 656 | float dw = w - t1.w; 657 | return dx * dx + dy * dy + dz * dz + dw * dw; 658 | } 659 | 660 | /** 661 | * Returns the distance between this tuple and tuple t1. 662 | * 663 | * @param t1 the other tuple 664 | * @return the distance between this tuple and tuple t1. 665 | */ 666 | public final float distanceF(Tuple4f t1) 667 | { 668 | float dx = x - t1.x; 669 | float dy = y - t1.y; 670 | float dz = z - t1.z; 671 | float dw = w - t1.w; 672 | return (float)Math.sqrt(dx * dx + dy * dy + dz * dz + dw * dw); 673 | } 674 | 675 | /** 676 | * Computes the L-1 (Manhattan) distance between this tuple and tuple t1. 677 | * The L-1 distance is equal to abs(x1-x2) + abs(y1-y2) 678 | * + abs(z1-z2) + abs(w1-w2). 679 | * 680 | * @param t1 the other tuple 681 | * @return L-1 distance 682 | */ 683 | public final float distanceL1F(Tuple4f t1) 684 | { 685 | return Math.abs(x - t1.x) + Math.abs(y - t1.y) + Math.abs(z - t1.z) + Math.abs(w - t1.w); 686 | } 687 | 688 | /** 689 | * Computes the L-infinite distance between this tuple and tuple t1. 690 | * The L-infinite distance is equal to MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2), abs(w1-w2)]. 691 | * 692 | * @param t1 the other tuple 693 | * @return L-infinite distance 694 | */ 695 | public final float distanceLinfF(Tuple4f t1) 696 | { 697 | return Math.max(Math.max(Math.abs(x - t1.x), Math.abs(y - t1.y)), 698 | Math.max(Math.abs(z - t1.z), Math.abs(w - t1.w))); 699 | } 700 | 701 | /** 702 | * Computes the square of the distance between this tuple and tuple t1. 703 | * 704 | * @param t1 the other tuple 705 | * @return the square of distance between this tuple and t1 706 | */ 707 | public final double distanceSquaredD(Tuple4f t1) 708 | { 709 | float dx = x - t1.x; 710 | float dy = y - t1.y; 711 | float dz = z - t1.z; 712 | float dw = w - t1.w; 713 | return dx * dx + dy * dy + dz * dz + dw * dw; 714 | } 715 | 716 | /** 717 | * Returns the distance between this tuple and tuple t1. 718 | * 719 | * @param t1 the other tuple 720 | * @return the distance between this tuple and tuple t1. 721 | */ 722 | public final double distanceD(Tuple4f t1) 723 | { 724 | float dx = x - t1.x; 725 | float dy = y - t1.y; 726 | float dz = z - t1.z; 727 | float dw = w - t1.w; 728 | return Math.sqrt(dx * dx + dy * dy + dz * dz + dw * dw); 729 | } 730 | 731 | /** 732 | * Computes the L-1 (Manhattan) distance between this tuple and tuple t1. 733 | * The L-1 distance is equal to abs(x1-x2) + abs(y1-y2) 734 | * + abs(z1-z2) + abs(w1-w2). 735 | * 736 | * @param t1 the other tuple 737 | * @return L-1 distance 738 | */ 739 | public final double distanceL1D(Tuple4f t1) 740 | { 741 | return Math.abs(x - t1.x) + Math.abs(y - t1.y) 742 | + Math.abs(z - t1.z) + Math.abs(w - t1.w); 743 | } 744 | 745 | /** 746 | * Computes the L-infinite distance between this tuple and tuple t1. 747 | * The L-infinite distance is equal to MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2), abs(w1-w2)]. 748 | * 749 | * @param t1 the other tuple 750 | * @return L-infinite distance 751 | */ 752 | public final double distanceLinfD(Tuple4f t1) 753 | { 754 | return Math.max(Math.max(Math.abs(x - t1.x), Math.abs(y - t1.y)), 755 | Math.max(Math.abs(z - t1.z), Math.abs(w - t1.w))); 756 | } 757 | } 758 | -------------------------------------------------------------------------------- /java6-math/src/main/java/org/typhonrt/java6/vecmath/VecmathStrings.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.java6.vecmath; 9 | 10 | class VecmathStrings 11 | { 12 | private VecmathStrings() {} 13 | 14 | public static final String s_STR_ORIGIN = "VECMATHSTRINGS_ORIGIN"; 15 | public static final String s_STR_VIEWING = "VECMATHSTRINGS_VIEWING"; 16 | 17 | public static final String s_STR_LINE_SEPARATOR = System.getProperty("line.separator"); 18 | public static final String s_STR_COMMA = System.getProperty(", "); 19 | 20 | public static final String s_STR_OPEN = "["; 21 | public static final String s_STR_OPEN_INDENT = " ["; 22 | public static final String s_STR_OPEN_X = "[x="; 23 | public static final String s_STR_Y = " y="; 24 | public static final String s_STR_Z = " z="; 25 | public static final String s_STR_W = " w="; 26 | public static final String s_STR_ANGLE = " angle="; 27 | public static final String s_STR_CLOSE = "]"; 28 | public static final String s_STR_DOUBLE_CLOSE = "] ]"; 29 | } -------------------------------------------------------------------------------- /java6-math/src/main/java/org/typhonrt/java6/vecmath/Vector3f.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.java6.vecmath; 9 | 10 | /** 11 | * A 3 element vector that is represented by single precision floating point x,y,z coordinates. If this value 12 | * represents a normal, then it should be normalized. 13 | * 14 | * @author Michael Leahy 15 | */ 16 | public class Vector3f extends Tuple3f 17 | { 18 | public static final Vector3f s_X = new Vector3f(1.0f, 0.0f, 0.0f); 19 | public static final Vector3f s_Y = new Vector3f(0.0f, 1.0f, 0.0f); 20 | public static final Vector3f s_Z = new Vector3f(0.0f, 0.0f, 1.0f); 21 | 22 | /** 23 | * Constructs and initializes a Vector3f from the specified xyz coordinates. 24 | * 25 | * @param x the x coordinate 26 | * @param y the y coordinate 27 | * @param z the z coordinate 28 | */ 29 | public Vector3f(float x, float y, float z) 30 | { 31 | super(x, y, z); 32 | } 33 | 34 | /** 35 | * Constructs and initializes a Vector3f from the specified array of length 3. 36 | * 37 | * @param v the array of length 3 containing xyz in order 38 | */ 39 | public Vector3f(float v[]) 40 | { 41 | super(v); 42 | } 43 | 44 | /** 45 | * Constructs and initializes a Vector3f to (0,0,0). 46 | */ 47 | public Vector3f() {} 48 | 49 | /** 50 | * Constructs and initializes a Vector3f from the specified Tuple3f. 51 | * 52 | * @param t1 the Tuple3f containing the initialization x y z data 53 | */ 54 | public Vector3f(Tuple3f t1) 55 | { 56 | super(t1); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /java6-math/src/main/java/org/typhonrt/java6/vecmath/Vector4f.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 Michael Leahy / TyphonRT, Inc. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package org.typhonrt.java6.vecmath; 9 | 10 | /** 11 | * A 4 element vector that is represented by single precision floating point x,y,z,w coordinates. 12 | * 13 | * @author Michael Leahy 14 | */ 15 | public class Vector4f extends Tuple4f 16 | { 17 | /** 18 | * Constructs and initializes a Vector4f from the specified xyzw coordinates. 19 | * 20 | * @param x the x coordinate 21 | * @param y the y coordinate 22 | * @param z the z coordinate 23 | * @param w the w coordinate 24 | */ 25 | public Vector4f(float x, float y, float z, float w) 26 | { 27 | super(x, y, z, w); 28 | } 29 | 30 | /** 31 | * Constructs and initializes a Vector4f from the specified array of length 4. 32 | * 33 | * @param v the array of length 4 containing xyzw in order 34 | */ 35 | public Vector4f(float v[]) 36 | { 37 | super(v); 38 | } 39 | 40 | /** 41 | * Constructs and initializes a Vector4f to (0,0,0,0). 42 | */ 43 | public Vector4f() {} 44 | 45 | /** 46 | * Constructs and initializes a Vector4f from the specified Tuple4f. 47 | * 48 | * @param t1 the Tuple4f containing the initialization x y z w data 49 | */ 50 | public Vector4f(Tuple4f t1) 51 | { 52 | super(t1); 53 | } 54 | 55 | /** 56 | * Constructs and initializes a Vector4f from the specified Tuple3f. 57 | * The x,y,z components of this point are setComponent to the corresponding 58 | * components 59 | * of tuple t1. The w component of this point is setComponent to 0. 60 | * 61 | * @param t1 the tuple to be copied 62 | * @since Java3D 1.2 63 | */ 64 | public Vector4f(Tuple3f t1) 65 | { 66 | super(t1.x, t1.y, t1.z, 0); 67 | } 68 | 69 | /** 70 | * Returns the (4-space) angle in radians between this vector and 71 | * the vector parameter; the return value is constrained to the 72 | * range [0,PI]. 73 | * 74 | * @param v1 the other vector 75 | * @return the angle in radians in the range [0,PI] 76 | */ 77 | public final float angle(Vector4f v1) 78 | { 79 | // numerically, domain error may occur 80 | return (float) Math.acos((x * v1.x + y * v1.y + z * v1.z + w * v1.w) / 81 | Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z + v1.w * v1.w) / Math.sqrt(x * x + y * y + z * z + w * w)); 82 | /* 83 | // zero div may occur. 84 | double d = x * v1.x + y * v1.y + z * v1.z + w * v1.w; // Dot product 85 | double v1_length = Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z + v1.w * v1.w); // Length v1 86 | double v_length = Math.sqrt(x * x + y * y + z * z + w * w); // Length 87 | 88 | // numerically, domain error may occur 89 | return (float)Math.acos(d / v1_length / v_length); 90 | */ 91 | } 92 | 93 | /** 94 | * Computes the dot product of the this vector and vector v1. 95 | * 96 | * @param v1 the other vector 97 | * @return the dot product of this vector and v1 98 | */ 99 | public final float dot(Vector3f v1) 100 | { 101 | return x * v1.x + y * v1.y + z * v1.z; 102 | } 103 | 104 | /** 105 | * Computes the dot product of the this vector and vector v1. 106 | * 107 | * @param v1 the other vector 108 | * @return the dot product of this vector and v1 109 | */ 110 | public final float dot(Vector4f v1) 111 | { 112 | return x * v1.x + y * v1.y + z * v1.z + w * v1.w; 113 | } 114 | 115 | /** 116 | * Returns the squared length of this vector. 117 | * 118 | * @return the squared length of this vectoras a float 119 | */ 120 | public final float lengthSquared() 121 | { 122 | return x * x + y * y + z * z + w * w; 123 | } 124 | 125 | /** 126 | * Returns the length of this vector. 127 | * 128 | * @return the length of this vector as a float 129 | */ 130 | public final float length() 131 | { 132 | return (float) Math.sqrt(x * x + y * y + z * z + w * w); 133 | } 134 | 135 | /** 136 | * Sets the value of this vector to the normalization of vector v1. 137 | * 138 | * @param v1 the un-normalized vector 139 | */ 140 | public final void normalize(Vector4f v1) 141 | { 142 | set(v1); 143 | normalize(); 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':java6-android-glutils', ':java6-math', ':java6-glutils-commons' 2 | --------------------------------------------------------------------------------