├── .gitignore ├── LICENSE ├── README.md ├── libraries ├── NOTICE └── arcore_client.aar └── samples ├── README.md └── java_arcore_hello_ar ├── .gitignore ├── NOTICE ├── app ├── .gitignore ├── build.gradle ├── libs │ └── obj-0.2.1.jar ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── assets │ ├── andy.obj │ ├── andy.png │ ├── andy_shadow.obj │ ├── andy_shadow.png │ └── trigrid.png │ ├── java │ └── com │ │ └── google │ │ └── ar │ │ └── core │ │ └── examples │ │ └── java │ │ └── helloar │ │ ├── CameraPermissionHelper.java │ │ ├── HelloArActivity.java │ │ ├── hack │ │ └── HackDeviceInfo.java │ │ ├── package-info.java │ │ ├── rendering │ │ ├── Attachment.java │ │ ├── BackgroundRenderer.java │ │ ├── FrameAttachment.java │ │ ├── ObjectRenderer.java │ │ ├── PlaneAttachment.java │ │ ├── PlaneRenderer.java │ │ ├── PointCloudRenderer.java │ │ ├── ShaderUtil.java │ │ └── package-info.java │ │ └── utils │ │ └── TLog.java │ └── res │ ├── drawable-xxhdpi │ └── ic_launcher.png │ ├── layout │ └── activity_main.xml │ ├── raw │ ├── light_vertex.shader │ ├── object_fragment.shader │ ├── object_vertex.shader │ ├── passthrough_fragment.shader │ ├── plane_fragment.shader │ ├── plane_vertex.shader │ ├── point_cloud_vertex.shader │ ├── screenquad_fragment_oes.shader │ └── screenquad_vertex.shader │ └── values │ ├── strings.xml │ └── styles.xml ├── arcore-preview.apk ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Android Studio configuration. 2 | *.iml 3 | .idea/ 4 | # Gradle configuration. 5 | .gradle/ 6 | build/ 7 | # User configuration. 8 | local.properties 9 | # OS configurations. 10 | .DS_Store 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017, Google Inc. 2 | Licensed under the Apache License, Version 2.0 (the "License"); 3 | you may not use this file except in compliance with the License. 4 | Unless required by applicable law or agreed to in writing, software 5 | distributed under the License is distributed on an "AS IS" BASIS, 6 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 7 | See the License for the specific language governing permissions and 8 | limitations under the License. 9 | Apache License 10 | Version 2.0, January 2004 11 | http://www.apache.org/licenses/ 12 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 13 | 1. Definitions. 14 | "License" shall mean the terms and conditions for use, reproduction, 15 | and distribution as defined by Sections 1 through 9 of this document. 16 | "Licensor" shall mean the copyright owner or entity authorized by 17 | the copyright owner that is granting the License. 18 | "Legal Entity" shall mean the union of the acting entity and all 19 | other entities that control, are controlled by, or are under common 20 | control with that entity. For the purposes of this definition, 21 | "control" means (i) the power, direct or indirect, to cause the 22 | direction or management of such entity, whether by contract or 23 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 24 | outstanding shares, or (iii) beneficial ownership of such entity. 25 | "You" (or "Your") shall mean an individual or Legal Entity 26 | exercising permissions granted by this License. 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | "Work" shall mean the work of authorship, whether in Source or 35 | Object form, made available under the License, as indicated by a 36 | copyright notice that is included in or attached to the work 37 | (an example is provided in the Appendix below). 38 | "Derivative Works" shall mean any work, whether in Source or Object 39 | form, that is based on (or derived from) the Work and for which the 40 | editorial revisions, annotations, elaborations, or other modifications 41 | represent, as a whole, an original work of authorship. For the purposes 42 | of this License, Derivative Works shall not include works that remain 43 | separable from, or merely link (or bind by name) to the interfaces of, 44 | the Work and Derivative Works thereof. 45 | "Contribution" shall mean any work of authorship, including 46 | the original version of the Work and any modifications or additions 47 | to that Work or Derivative Works thereof, that is intentionally 48 | submitted to Licensor for inclusion in the Work by the copyright owner 49 | or by an individual or Legal Entity authorized to submit on behalf of 50 | the copyright owner. For the purposes of this definition, "submitted" 51 | means any form of electronic, verbal, or written communication sent 52 | to the Licensor or its representatives, including but not limited to 53 | communication on electronic mailing lists, source code control systems, 54 | and issue tracking systems that are managed by, or on behalf of, the 55 | Licensor for the purpose of discussing and improving the Work, but 56 | excluding communication that is conspicuously marked or otherwise 57 | designated in writing by the copyright owner as "Not a Contribution." 58 | "Contributor" shall mean Licensor and any individual or Legal Entity 59 | on behalf of whom a Contribution has been received by Licensor and 60 | subsequently incorporated within the Work. 61 | 2. Grant of Copyright License. Subject to the terms and conditions of 62 | this License, each Contributor hereby grants to You a perpetual, 63 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 64 | copyright license to reproduce, prepare Derivative Works of, 65 | publicly display, publicly perform, sublicense, and distribute the 66 | Work and such Derivative Works in Source or Object form. 67 | 3. Grant of Patent License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | (except as stated in this section) patent license to make, have made, 71 | use, offer to sell, sell, import, and otherwise transfer the Work, 72 | where such license applies only to those patent claims licensable 73 | by such Contributor that are necessarily infringed by their 74 | Contribution(s) alone or by combination of their Contribution(s) 75 | with the Work to which such Contribution(s) was submitted. If You 76 | institute patent litigation against any entity (including a 77 | cross-claim or counterclaim in a lawsuit) alleging that the Work 78 | or a Contribution incorporated within the Work constitutes direct 79 | or contributory patent infringement, then any patent licenses 80 | granted to You under this License for that Work shall terminate 81 | as of the date such litigation is filed. 82 | 4. Redistribution. You may reproduce and distribute copies of the 83 | Work or Derivative Works thereof in any medium, with or without 84 | modifications, and in Source or Object form, provided that You 85 | meet the following conditions: 86 | (a) You must give any other recipients of the Work or 87 | Derivative Works a copy of this License; and 88 | (b) You must cause any modified files to carry prominent notices 89 | stating that You changed the files; and 90 | (c) You must retain, in the Source form of any Derivative Works 91 | that You distribute, all copyright, patent, trademark, and 92 | attribution notices from the Source form of the Work, 93 | excluding those notices that do not pertain to any part of 94 | the Derivative Works; and 95 | (d) If the Work includes a "NOTICE" text file as part of its 96 | distribution, then any Derivative Works that You distribute must 97 | include a readable copy of the attribution notices contained 98 | within such NOTICE file, excluding those notices that do not 99 | pertain to any part of the Derivative Works, in at least one 100 | of the following places: within a NOTICE text file distributed 101 | as part of the Derivative Works; within the Source form or 102 | documentation, if provided along with the Derivative Works; or, 103 | within a display generated by the Derivative Works, if and 104 | wherever such third-party notices normally appear. The contents 105 | of the NOTICE file are for informational purposes only and 106 | do not modify the License. You may add Your own attribution 107 | notices within Derivative Works that You distribute, alongside 108 | or as an addendum to the NOTICE text from the Work, provided 109 | that such additional attribution notices cannot be construed 110 | as modifying the License. 111 | You may add Your own copyright statement to Your modifications and 112 | may provide additional or different license terms and conditions 113 | for use, reproduction, or distribution of Your modifications, or 114 | for any such Derivative Works as a whole, provided Your use, 115 | reproduction, and distribution of the Work otherwise complies with 116 | the conditions stated in this License. 117 | 5. Submission of Contributions. Unless You explicitly state otherwise, 118 | any Contribution intentionally submitted for inclusion in the Work 119 | by You to the Licensor shall be under the terms and conditions of 120 | this License, without any additional terms or conditions. 121 | Notwithstanding the above, nothing herein shall supersede or modify 122 | the terms of any separate license agreement you may have executed 123 | with Licensor regarding such Contributions. 124 | 6. Trademarks. This License does not grant permission to use the trade 125 | names, trademarks, service marks, or product names of the Licensor, 126 | except as required for reasonable and customary use in describing the 127 | origin of the Work and reproducing the content of the NOTICE file. 128 | 7. Disclaimer of Warranty. Unless required by applicable law or 129 | agreed to in writing, Licensor provides the Work (and each 130 | Contributor provides its Contributions) on an "AS IS" BASIS, 131 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 132 | implied, including, without limitation, any warranties or conditions 133 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 134 | PARTICULAR PURPOSE. You are solely responsible for determining the 135 | appropriateness of using or redistributing the Work and assume any 136 | risks associated with Your exercise of permissions under this License. 137 | 8. Limitation of Liability. In no event and under no legal theory, 138 | whether in tort (including negligence), contract, or otherwise, 139 | unless required by applicable law (such as deliberate and grossly 140 | negligent acts) or agreed to in writing, shall any Contributor be 141 | liable to You for damages, including any direct, indirect, special, 142 | incidental, or consequential damages of any character arising as a 143 | result of this License or out of the use or inability to use the 144 | Work (including but not limited to damages for loss of goodwill, 145 | work stoppage, computer failure or malfunction, or any and all 146 | other commercial damages or losses), even if such Contributor 147 | has been advised of the possibility of such damages. 148 | 9. Accepting Warranty or Additional Liability. While redistributing 149 | the Work or Derivative Works thereof, You may choose to offer, 150 | and charge a fee for, acceptance of support, warranty, indemnity, 151 | or other liability obligations and/or rights consistent with this 152 | License. However, in accepting such obligations, You may act only 153 | on Your own behalf and on Your sole responsibility, not on behalf 154 | of any other Contributor, and only if You agree to indemnify, 155 | defend, and hold each Contributor harmless for any liability 156 | incurred by, or claims asserted against, such Contributor by reason 157 | of your accepting any such warranty or additional liability. 158 | END OF TERMS AND CONDITIONS 159 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Google ARCore, support Pixel、S8、Nexus xx 3 | 4 | =======================================start======================================== 5 | 6 | 官方声明支持的设备只有Pixel和S8,设备检查是在arcore_client.aar中的SupportedDevices.class, 7 | 是根据设备的Build.FINGERPRINT判断的,所做的是反射修改掉这个值,绕过检查即可。 8 | 9 | 验证Nexus 6可以使用,7.0以上机型都可以试一下 10 | 11 | =======================================end========================================== 12 | 13 | Google AR SDK 14 | ===================== 15 | Copyright (c) 2017 Google Inc. All rights reserved. 16 | 17 | [https://developers.google.com/ar/develop/java/getting-started](https://developers.google.com/ar/develop/java/getting-started) 18 | 19 | Please note, we do not accept pull requests. 20 | -------------------------------------------------------------------------------- /libraries/NOTICE: -------------------------------------------------------------------------------- 1 | ==================== 2 | Open Source Licenses 3 | ==================== 4 | 5 | arcore_client.aar may use portions of the following libraries subject to the 6 | accompanying licenses: 7 | 8 | *** 9 | vecmath 10 | *** 11 | 12 | Copyright (C) 1997,1998,1999 13 | Kenji Hiranabe, Eiwa System Management, Inc. 14 | 15 | This program is free software. 16 | Implemented by Kenji Hiranabe(hiranabe@esm.co.jp), 17 | conforming to the Java(TM) 3D API specification by Sun Microsystems. 18 | 19 | Permission to use, copy, modify, distribute and sell this software 20 | and its documentation for any purpose is hereby granted without fee, 21 | provided that the above copyright notice appear in all copies and 22 | that both that copyright notice and this permission notice appear 23 | in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc. 24 | makes no representations about the suitability of this software for any 25 | purpose. It is provided "AS IS" with NO WARRANTY. 26 | -------------------------------------------------------------------------------- /libraries/arcore_client.aar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jason0539/arcore-android-sdk/0398b60da0aa7fb23fb4e969e76e81cd4360cb8c/libraries/arcore_client.aar -------------------------------------------------------------------------------- /samples/README.md: -------------------------------------------------------------------------------- 1 | Google AR SDK 2 | ===================== 3 | Copyright (c) 2017 Google Inc. All rights reserved. 4 | 5 | [https://developers.google.com/ar/develop/java/getting-started](https://developers.google.com/ar/develop/java/getting-started) 6 | 7 | Please note, we do not accept pull requests. 8 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | local.properties 3 | .idea 4 | *.iml 5 | .DS_Store 6 | build 7 | captures 8 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/NOTICE: -------------------------------------------------------------------------------- 1 | ==================== 2 | Open Source Licenses 3 | ==================== 4 | hello_ar may use portions of the following libraries subject to the accompanying 5 | licenses: 6 | *** 7 | JavaGL Obj 8 | *** 9 | Copyright (c) 2008-2015 Marco Hutter - http://www.javagl.de 10 | Permission is hereby granted, free of charge, to any person 11 | obtaining a copy of this software and associated documentation 12 | files (the "Software"), to deal in the Software without 13 | restriction, including without limitation the rights to use, 14 | copy, modify, merge, publish, distribute, sublicense, and/or sell 15 | copies of the Software, and to permit persons to whom the 16 | Software is furnished to do so, subject to the following 17 | conditions: 18 | The above copyright notice and this permission notice shall be 19 | included in all copies or substantial portions of the Software. 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | OTHER DEALINGS IN THE SOFTWARE. 28 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.0" 6 | 7 | defaultConfig { 8 | applicationId "com.google.ar.core.examples.java.helloar" 9 | minSdkVersion 24 10 | targetSdkVersion 25 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | 20 | lintOptions { 21 | abortOnError false 22 | } 23 | } 24 | } 25 | 26 | repositories { 27 | flatDir { 28 | dirs './libs', '../../../libraries' 29 | } 30 | } 31 | 32 | dependencies { 33 | compile (name: 'arcore_client', ext: 'aar') 34 | compile (name: 'obj-0.2.1', ext: 'jar') 35 | compile 'com.android.support:appcompat-v7:25.0.0' 36 | compile 'com.android.support:design:25.0.0' 37 | } 38 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/libs/obj-0.2.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jason0539/arcore-android-sdk/0398b60da0aa7fb23fb4e969e76e81cd4360cb8c/samples/java_arcore_hello_ar/app/libs/obj-0.2.1.jar -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/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 /opt/android-sdk/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 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 21 | 22 | 25 | 26 | 27 | 28 | 33 | 34 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/assets/andy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jason0539/arcore-android-sdk/0398b60da0aa7fb23fb4e969e76e81cd4360cb8c/samples/java_arcore_hello_ar/app/src/main/assets/andy.png -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/assets/andy_shadow.obj: -------------------------------------------------------------------------------- 1 | # This file uses centimeters as units for non-parametric coordinates. 2 | 3 | g default 4 | v -0.100000 -0.000000 0.100000 5 | v 0.100000 -0.000000 0.100000 6 | v -0.100000 0.000000 -0.100000 7 | v 0.100000 0.000000 -0.100000 8 | vt 0.000000 0.000000 9 | vt 1.000000 0.000000 10 | vt 0.000000 1.000000 11 | vt 1.000000 1.000000 12 | vn 0.000000 1.000000 0.000000 13 | vn 0.000000 1.000000 0.000000 14 | vn 0.000000 1.000000 0.000000 15 | vn 0.000000 1.000000 0.000000 16 | s off 17 | g AndyBlobShadow_GEO 18 | f 4/4/1 3/3/2 1/1/3 2/2/4 19 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/assets/andy_shadow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jason0539/arcore-android-sdk/0398b60da0aa7fb23fb4e969e76e81cd4360cb8c/samples/java_arcore_hello_ar/app/src/main/assets/andy_shadow.png -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/assets/trigrid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jason0539/arcore-android-sdk/0398b60da0aa7fb23fb4e969e76e81cd4360cb8c/samples/java_arcore_hello_ar/app/src/main/assets/trigrid.png -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/CameraPermissionHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | package com.google.ar.core.examples.java.helloar; 16 | 17 | import android.Manifest; 18 | import android.app.Activity; 19 | import android.content.pm.PackageManager; 20 | import android.support.v4.app.ActivityCompat; 21 | import android.support.v4.content.ContextCompat; 22 | 23 | /** 24 | * Helper to ask camera permission. 25 | */ 26 | public class CameraPermissionHelper { 27 | private static final String CAMERA_PERMISSION = Manifest.permission.CAMERA; 28 | private static final int CAMERA_PERMISSION_CODE = 0; 29 | 30 | /** 31 | * Check to see we have the necessary permissions for this app. 32 | */ 33 | public static boolean hasCameraPermission(Activity activity) { 34 | return ContextCompat.checkSelfPermission(activity, CAMERA_PERMISSION) == 35 | PackageManager.PERMISSION_GRANTED; 36 | } 37 | 38 | /** 39 | * Check to see we have the necessary permissions for this app, and ask for them if we don't. 40 | */ 41 | public static void requestCameraPermission(Activity activity) { 42 | ActivityCompat.requestPermissions(activity, new String[]{CAMERA_PERMISSION}, 43 | CAMERA_PERMISSION_CODE); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/HelloArActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.google.ar.core.examples.java.helloar; 18 | 19 | import android.opengl.GLES20; 20 | import android.opengl.GLSurfaceView; 21 | import android.os.Bundle; 22 | import android.support.design.widget.Snackbar; 23 | import android.support.v7.app.AppCompatActivity; 24 | import android.util.Log; 25 | import android.view.GestureDetector; 26 | import android.view.MotionEvent; 27 | import android.view.View; 28 | import android.view.WindowManager; 29 | import android.widget.Toast; 30 | 31 | import com.google.ar.core.Config; 32 | import com.google.ar.core.Frame; 33 | import com.google.ar.core.Frame.TrackingState; 34 | import com.google.ar.core.HitResult; 35 | import com.google.ar.core.Plane; 36 | import com.google.ar.core.PlaneHitResult; 37 | import com.google.ar.core.Session; 38 | import com.google.ar.core.examples.java.helloar.hack.HackDeviceInfo; 39 | import com.google.ar.core.examples.java.helloar.rendering.Attachment; 40 | import com.google.ar.core.examples.java.helloar.rendering.BackgroundRenderer; 41 | import com.google.ar.core.examples.java.helloar.rendering.FrameAttachment; 42 | import com.google.ar.core.examples.java.helloar.rendering.ObjectRenderer; 43 | import com.google.ar.core.examples.java.helloar.rendering.ObjectRenderer.BlendMode; 44 | import com.google.ar.core.examples.java.helloar.rendering.PlaneAttachment; 45 | import com.google.ar.core.examples.java.helloar.rendering.PlaneRenderer; 46 | import com.google.ar.core.examples.java.helloar.rendering.PointCloudRenderer; 47 | import com.google.ar.core.examples.java.helloar.utils.TLog; 48 | 49 | import java.io.IOException; 50 | import java.util.ArrayList; 51 | import java.util.Arrays; 52 | import java.util.concurrent.ArrayBlockingQueue; 53 | 54 | import javax.microedition.khronos.egl.EGLConfig; 55 | import javax.microedition.khronos.opengles.GL10; 56 | 57 | /** 58 | * This is a simple example that shows how to create an augmented reality (AR) application using 59 | * the ARCore API. The application will display any detected planes and will allow the user to 60 | * tap on a plane to place a 3d model of the Android robot. 61 | */ 62 | public class HelloArActivity extends AppCompatActivity implements GLSurfaceView.Renderer { 63 | private static final String TAG = HelloArActivity.class.getSimpleName(); 64 | 65 | // Rendering. The Renderers are created here, and initialized when the GL surface is created. 66 | private GLSurfaceView mSurfaceView; 67 | 68 | private Config mDefaultConfig; 69 | private Session mSession; 70 | private BackgroundRenderer mBackgroundRenderer = new BackgroundRenderer(); 71 | private GestureDetector mGestureDetector; 72 | private Snackbar mLoadingMessageSnackbar = null; 73 | 74 | private ObjectRenderer mVirtualObject = new ObjectRenderer(); 75 | private ObjectRenderer mVirtualObjectShadow = new ObjectRenderer(); 76 | private PlaneRenderer mPlaneRenderer = new PlaneRenderer(); 77 | private PointCloudRenderer mPointCloud = new PointCloudRenderer(); 78 | 79 | // Temporary matrix allocated here to reduce number of allocations for each frame. 80 | private final float[] mAnchorMatrix = new float[16]; 81 | 82 | // Tap handling and UI. 83 | private ArrayBlockingQueue mQueuedSingleTaps = new ArrayBlockingQueue<>(16); 84 | private ArrayList mTouches = new ArrayList<>(); 85 | 86 | @Override 87 | protected void onCreate(Bundle savedInstanceState) { 88 | super.onCreate(savedInstanceState); 89 | HackDeviceInfo.hackBuildFingerprint(); 90 | setContentView(R.layout.activity_main); 91 | mSurfaceView = (GLSurfaceView) findViewById(R.id.surfaceview); 92 | 93 | mSession = new Session(/*context=*/this); 94 | 95 | // Create default config, check is supported, create session from that config. 96 | mDefaultConfig = Config.createDefaultConfig(); 97 | if (!mSession.isSupported(mDefaultConfig)) { 98 | Toast.makeText(this, "This device does not support AR", Toast.LENGTH_LONG).show(); 99 | finish(); 100 | return; 101 | } 102 | 103 | // Set up tap listener. 104 | mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() { 105 | @Override 106 | public boolean onSingleTapUp(MotionEvent e) { 107 | onSingleTap(e); 108 | return true; 109 | } 110 | 111 | @Override 112 | public boolean onDown(MotionEvent e) { 113 | return true; 114 | } 115 | }); 116 | 117 | mSurfaceView.setOnTouchListener(new View.OnTouchListener() { 118 | @Override 119 | public boolean onTouch(View v, MotionEvent event) { 120 | return mGestureDetector.onTouchEvent(event); 121 | } 122 | }); 123 | 124 | // Set up renderer. 125 | mSurfaceView.setPreserveEGLContextOnPause(true); 126 | mSurfaceView.setEGLContextClientVersion(2); 127 | mSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); // Alpha used for plane blending. 128 | mSurfaceView.setRenderer(this); 129 | mSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); 130 | } 131 | 132 | @Override 133 | protected void onResume() { 134 | super.onResume(); 135 | 136 | // ARCore requires camera permissions to operate. If we did not yet obtain runtime 137 | // permission on Android M and above, now is a good time to ask the user for it. 138 | if (CameraPermissionHelper.hasCameraPermission(this)) { 139 | showLoadingMessage(); 140 | // Note that order matters - see the note in onPause(), the reverse applies here. 141 | mSession.resume(mDefaultConfig); 142 | mSurfaceView.onResume(); 143 | } else { 144 | CameraPermissionHelper.requestCameraPermission(this); 145 | } 146 | } 147 | 148 | @Override 149 | public void onPause() { 150 | super.onPause(); 151 | // Note that the order matters - GLSurfaceView is paused first so that it does not try 152 | // to query the session. If Session is paused before GLSurfaceView, GLSurfaceView may 153 | // still call mSession.update() and get a SessionPausedException. 154 | mSurfaceView.onPause(); 155 | mSession.pause(); 156 | } 157 | 158 | @Override 159 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) { 160 | if (!CameraPermissionHelper.hasCameraPermission(this)) { 161 | Toast.makeText(this, 162 | "Camera permission is needed to run this application", Toast.LENGTH_LONG).show(); 163 | finish(); 164 | } 165 | } 166 | 167 | @Override 168 | public void onWindowFocusChanged(boolean hasFocus) { 169 | super.onWindowFocusChanged(hasFocus); 170 | if (hasFocus) { 171 | // Standard Android full-screen functionality. 172 | getWindow().getDecorView().setSystemUiVisibility( 173 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE 174 | | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 175 | | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 176 | | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 177 | | View.SYSTEM_UI_FLAG_FULLSCREEN 178 | | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); 179 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 180 | } 181 | } 182 | 183 | private void onSingleTap(MotionEvent e) { 184 | // Queue tap if there is space. Tap is lost if queue is full. 185 | mQueuedSingleTaps.offer(e); 186 | } 187 | 188 | @Override 189 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 190 | GLES20.glClearColor(0.1f, 0.1f, 0.1f, 1.0f); 191 | 192 | // Create the texture and pass it to ARCore session to be filled during update(). 193 | mBackgroundRenderer.createOnGlThread(/*context=*/this); 194 | mSession.setCameraTextureName(mBackgroundRenderer.getTextureId()); 195 | 196 | // Prepare the other rendering objects. 197 | try { 198 | mVirtualObject.createOnGlThread(/*context=*/this, "andy.obj", "andy.png"); 199 | mVirtualObject.setMaterialProperties(0.0f, 3.5f, 1.0f, 6.0f); 200 | 201 | mVirtualObjectShadow.createOnGlThread(/*context=*/this, 202 | "andy_shadow.obj", "andy_shadow.png"); 203 | mVirtualObjectShadow.setBlendMode(BlendMode.Shadow); 204 | mVirtualObjectShadow.setMaterialProperties(1.0f, 0.0f, 0.0f, 1.0f); 205 | } catch (IOException e) { 206 | Log.e(TAG, "Failed to read obj file"); 207 | } 208 | try { 209 | mPlaneRenderer.createOnGlThread(/*context=*/this, "trigrid.png"); 210 | } catch (IOException e) { 211 | Log.e(TAG, "Failed to read plane texture"); 212 | } 213 | mPointCloud.createOnGlThread(/*context=*/this); 214 | } 215 | 216 | @Override 217 | public void onSurfaceChanged(GL10 gl, int width, int height) { 218 | GLES20.glViewport(0, 0, width, height); 219 | // Notify ARCore session that the view size changed so that the perspective matrix and 220 | // the video background can be properly adjusted. 221 | mSession.setDisplayGeometry(width, height); 222 | } 223 | 224 | @Override 225 | public void onDrawFrame(GL10 gl) { 226 | // Clear screen to notify driver it should not load any pixels from previous frame. 227 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 228 | 229 | try { 230 | // Obtain the current frame from ARSession. When the configuration is set to 231 | // UpdateMode.BLOCKING (it is by default), this will throttle the rendering to the 232 | // camera framerate. 233 | Frame frame = mSession.update(); 234 | 235 | // Handle taps. Handling only one tap per frame, as taps are usually low frequency 236 | // compared to frame rate. 237 | MotionEvent tap = mQueuedSingleTaps.poll(); 238 | if (tap != null && frame.getTrackingState() == TrackingState.TRACKING) { 239 | for (HitResult hit : frame.hitTest(tap)) { 240 | // Check if any plane was hit, and if it was hit inside the plane polygon. 241 | if (hit instanceof PlaneHitResult && ((PlaneHitResult) hit).isHitInPolygon()) { 242 | // Cap the number of objects created. This avoids overloading both the 243 | // rendering system and ARCore. 244 | if (mTouches.size() >= 16) { 245 | mSession.removeAnchors(Arrays.asList(mTouches.get(0).getAnchor())); 246 | mTouches.remove(0); 247 | } 248 | // Adding an Anchor tells ARCore that it should track this position in 249 | // space. This anchor will be used in PlaneAttachment to place the 3d model 250 | // in the correct position relative both to the world and to the plane. 251 | mTouches.add(new PlaneAttachment( 252 | ((PlaneHitResult) hit).getPlane(), 253 | mSession.addAnchor(hit.getHitPose()))); 254 | 255 | // Hits are sorted by depth. Consider only closest hit on a plane. 256 | TLog.d("HelloArActivity->onDrawFrame 检测到地面碰撞"); 257 | break; 258 | }else { 259 | mTouches.add(new FrameAttachment(mSession.addAnchor(frame.getPose()))); 260 | TLog.d("HelloArActivity->onDrawFrame 触摸到空间碰撞"); 261 | break; 262 | } 263 | } 264 | } 265 | 266 | // Draw background. 267 | mBackgroundRenderer.draw(frame); 268 | 269 | // If not tracking, don't draw 3d objects. 270 | if (frame.getTrackingState() == TrackingState.NOT_TRACKING) { 271 | return; 272 | } 273 | 274 | // Get projection matrix. 275 | float[] projmtx = new float[16]; 276 | mSession.getProjectionMatrix(projmtx, 0, 0.1f, 100.0f); 277 | 278 | // Get camera matrix and draw. 279 | float[] viewmtx = new float[16]; 280 | frame.getViewMatrix(viewmtx, 0); 281 | 282 | // Compute lighting from average intensity of the image. 283 | final float lightIntensity = frame.getLightEstimate().getPixelIntensity(); 284 | 285 | // Visualize tracked points. 286 | mPointCloud.update(frame.getPointCloud()); 287 | mPointCloud.draw(frame.getPointCloudPose(), viewmtx, projmtx); 288 | 289 | // Check if we detected at least one plane. If so, hide the loading message. 290 | if (mLoadingMessageSnackbar != null) { 291 | for (Plane plane : mSession.getAllPlanes()) { 292 | if (plane.getType() == com.google.ar.core.Plane.Type.HORIZONTAL_UPWARD_FACING && 293 | plane.getTrackingState() == Plane.TrackingState.TRACKING) { 294 | hideLoadingMessage(); 295 | break; 296 | } 297 | } 298 | } 299 | 300 | // Visualize planes. 301 | mPlaneRenderer.drawPlanes(mSession.getAllPlanes(), frame.getPose(), projmtx); 302 | 303 | // Visualize anchors created by touch. 304 | float scaleFactor = 1.0f; 305 | for (Attachment planeAttachment : mTouches) { 306 | if (!planeAttachment.isTracking()) { 307 | continue; 308 | } 309 | // Get the current combined pose of an Anchor and Plane in world space. The Anchor 310 | // and Plane poses are updated during calls to session.update() as ARCore refines 311 | // its estimate of the world. 312 | planeAttachment.getPose().toMatrix(mAnchorMatrix, 0); 313 | 314 | // Update and draw the model and its shadow. 315 | mVirtualObject.updateModelMatrix(mAnchorMatrix, scaleFactor); 316 | mVirtualObjectShadow.updateModelMatrix(mAnchorMatrix, scaleFactor); 317 | mVirtualObject.draw(viewmtx, projmtx, lightIntensity); 318 | mVirtualObjectShadow.draw(viewmtx, projmtx, lightIntensity); 319 | } 320 | 321 | } catch (Throwable t) { 322 | // Avoid crashing the application due to unhandled exceptions. 323 | Log.e(TAG, "Exception on the OpenGL thread", t); 324 | } 325 | } 326 | 327 | private void showLoadingMessage() { 328 | runOnUiThread(new Runnable() { 329 | @Override 330 | public void run() { 331 | mLoadingMessageSnackbar = Snackbar.make( 332 | HelloArActivity.this.findViewById(android.R.id.content), 333 | "Searching for surfaces...", Snackbar.LENGTH_INDEFINITE); 334 | mLoadingMessageSnackbar.getView().setBackgroundColor(0xbf323232); 335 | mLoadingMessageSnackbar.show(); 336 | } 337 | }); 338 | } 339 | 340 | private void hideLoadingMessage() { 341 | runOnUiThread(new Runnable() { 342 | @Override 343 | public void run() { 344 | mLoadingMessageSnackbar.dismiss(); 345 | mLoadingMessageSnackbar = null; 346 | } 347 | }); 348 | } 349 | } 350 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/hack/HackDeviceInfo.java: -------------------------------------------------------------------------------- 1 | package com.google.ar.core.examples.java.helloar.hack; 2 | 3 | import android.os.Build; 4 | 5 | import com.google.ar.core.examples.java.helloar.utils.TLog; 6 | 7 | import java.lang.reflect.Field; 8 | 9 | /** 10 | * Created by liuzhenhui on 2017/8/31. 11 | */ 12 | 13 | public class HackDeviceInfo { 14 | public static final void hackBuildFingerprint() { 15 | TLog.d("HackDeviceInfo->hackBuildFingerprint 修改前" + Build.FINGERPRINT); 16 | try { 17 | Class buildClass = Class.forName("android.os.Build"); 18 | 19 | //获取Build.Fingerprint字段 20 | Field fingerprintField = buildClass.getDeclaredField("FINGERPRINT"); 21 | fingerprintField.setAccessible(true); 22 | Object fingerprint = fingerprintField.get(null); 23 | TLog.d("HackDeviceInfo->hackBuildFingerprint 反射拿到的值" + fingerprint); 24 | 25 | String hackFingerprint = (String) fingerprint; 26 | hackFingerprint += "sailfish:7"; 27 | TLog.d("HackDeviceInfo->hackBuildFingerprint 修改后的值" + hackFingerprint); 28 | fingerprintField.set(buildClass, hackFingerprint); 29 | } catch (Exception e) { 30 | TLog.d("HackDeviceInfo->hackBuildFingerprint " + e.getMessage()); 31 | e.printStackTrace(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * A sample showing how to build an Augmented Reality app using ARCore. 18 | */ 19 | package com.google.ar.core.examples.java.helloar; 20 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/rendering/Attachment.java: -------------------------------------------------------------------------------- 1 | package com.google.ar.core.examples.java.helloar.rendering; 2 | 3 | import com.google.ar.core.Anchor; 4 | import com.google.ar.core.Pose; 5 | 6 | /** 7 | * Created by liuzhenhui on 2017/9/1. 8 | */ 9 | 10 | public interface Attachment { 11 | boolean isTracking(); 12 | 13 | Pose getPose(); 14 | 15 | Anchor getAnchor(); 16 | } 17 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/rendering/BackgroundRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | package com.google.ar.core.examples.java.helloar.rendering; 16 | 17 | import com.google.ar.core.Frame; 18 | import com.google.ar.core.Session; 19 | import com.google.ar.core.examples.java.helloar.R; 20 | 21 | import android.content.Context; 22 | import android.opengl.GLES11Ext; 23 | import android.opengl.GLES20; 24 | import android.opengl.GLSurfaceView; 25 | 26 | import java.nio.ByteBuffer; 27 | import java.nio.ByteOrder; 28 | import java.nio.FloatBuffer; 29 | import javax.microedition.khronos.egl.EGLConfig; 30 | import javax.microedition.khronos.opengles.GL10; 31 | 32 | /** 33 | * This class renders the AR background from camera feed. It creates and hosts the texture 34 | * given to ARCore to be filled with the camera image. 35 | */ 36 | public class BackgroundRenderer { 37 | private static final String TAG = BackgroundRenderer.class.getSimpleName(); 38 | 39 | private static final int COORDS_PER_VERTEX = 3; 40 | private static final int TEXCOORDS_PER_VERTEX = 2; 41 | private static final int FLOAT_SIZE = 4; 42 | 43 | private FloatBuffer mQuadVertices; 44 | private FloatBuffer mQuadTexCoord; 45 | private FloatBuffer mQuadTexCoordTransformed; 46 | 47 | private int mQuadProgram; 48 | 49 | private int mQuadPositionParam; 50 | private int mQuadTexCoordParam; 51 | private int mTextureId = -1; 52 | private int mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES; 53 | public BackgroundRenderer() { 54 | } 55 | 56 | public int getTextureId() { 57 | return mTextureId; 58 | } 59 | 60 | /** 61 | * Allocates and initializes OpenGL resources needed by the background renderer. Must be 62 | * called on the OpenGL thread, typically in 63 | * {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}. 64 | * 65 | * @param context Needed to access shader source. 66 | */ 67 | public void createOnGlThread(Context context) { 68 | // Generate the background texture. 69 | int textures[] = new int[1]; 70 | GLES20.glGenTextures(1, textures, 0); 71 | mTextureId = textures[0]; 72 | GLES20.glBindTexture(mTextureTarget, mTextureId); 73 | GLES20.glTexParameteri(mTextureTarget, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); 74 | GLES20.glTexParameteri(mTextureTarget, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); 75 | GLES20.glTexParameteri(mTextureTarget, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); 76 | GLES20.glTexParameteri(mTextureTarget, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); 77 | 78 | int numVertices = 4; 79 | if (numVertices != QUAD_COORDS.length / COORDS_PER_VERTEX) { 80 | throw new RuntimeException("Unexpected number of vertices in BackgroundRenderer."); 81 | } 82 | 83 | ByteBuffer bbVertices = ByteBuffer.allocateDirect(QUAD_COORDS.length * FLOAT_SIZE); 84 | bbVertices.order(ByteOrder.nativeOrder()); 85 | mQuadVertices = bbVertices.asFloatBuffer(); 86 | mQuadVertices.put(QUAD_COORDS); 87 | mQuadVertices.position(0); 88 | 89 | ByteBuffer bbTexCoords = ByteBuffer.allocateDirect( 90 | numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); 91 | bbTexCoords.order(ByteOrder.nativeOrder()); 92 | mQuadTexCoord = bbTexCoords.asFloatBuffer(); 93 | mQuadTexCoord.put(QUAD_TEXCOORDS); 94 | mQuadTexCoord.position(0); 95 | 96 | ByteBuffer bbTexCoordsTransformed = ByteBuffer.allocateDirect( 97 | numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE); 98 | bbTexCoordsTransformed.order(ByteOrder.nativeOrder()); 99 | mQuadTexCoordTransformed = bbTexCoordsTransformed.asFloatBuffer(); 100 | 101 | int vertexShader = ShaderUtil.loadGLShader(TAG, context, 102 | GLES20.GL_VERTEX_SHADER, R.raw.screenquad_vertex); 103 | int fragmentShader = ShaderUtil.loadGLShader(TAG, context, 104 | GLES20.GL_FRAGMENT_SHADER, R.raw.screenquad_fragment_oes); 105 | 106 | mQuadProgram = GLES20.glCreateProgram(); 107 | GLES20.glAttachShader(mQuadProgram, vertexShader); 108 | GLES20.glAttachShader(mQuadProgram, fragmentShader); 109 | GLES20.glLinkProgram(mQuadProgram); 110 | GLES20.glUseProgram(mQuadProgram); 111 | 112 | ShaderUtil.checkGLError(TAG, "Program creation"); 113 | 114 | mQuadPositionParam = GLES20.glGetAttribLocation(mQuadProgram, "a_Position"); 115 | mQuadTexCoordParam = GLES20.glGetAttribLocation(mQuadProgram, "a_TexCoord"); 116 | 117 | ShaderUtil.checkGLError(TAG, "Program parameters"); 118 | } 119 | 120 | /** 121 | * Draws the AR background image. The image will be drawn such that virtual content rendered 122 | * with the matrices provided by {@link Frame#getViewMatrix(float[], int)} and 123 | * {@link Session#getProjectionMatrix(float[], int, float, float)} will accurately follow 124 | * static physical objects. This must be called before drawing virtual content. 125 | * 126 | * @param frame The last {@code Frame} returned by {@link Session#update()}. 127 | */ 128 | public void draw(Frame frame) { 129 | // If display rotation changed (also includes view size change), we need to re-query the uv 130 | // coordinates for the screen rect, as they may have changed as well. 131 | if (frame.isDisplayRotationChanged()) { 132 | frame.transformDisplayUvCoords(mQuadTexCoord, mQuadTexCoordTransformed); 133 | } 134 | 135 | // No need to test or write depth, the screen quad has arbitrary depth, and is expected 136 | // to be drawn first. 137 | GLES20.glDisable(GLES20.GL_DEPTH_TEST); 138 | GLES20.glDepthMask(false); 139 | 140 | GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureId); 141 | 142 | GLES20.glUseProgram(mQuadProgram); 143 | 144 | // Set the vertex positions. 145 | GLES20.glVertexAttribPointer( 146 | mQuadPositionParam, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, mQuadVertices); 147 | 148 | // Set the texture coordinates. 149 | GLES20.glVertexAttribPointer(mQuadTexCoordParam, TEXCOORDS_PER_VERTEX, 150 | GLES20.GL_FLOAT, false, 0, mQuadTexCoordTransformed); 151 | 152 | // Enable vertex arrays 153 | GLES20.glEnableVertexAttribArray(mQuadPositionParam); 154 | GLES20.glEnableVertexAttribArray(mQuadTexCoordParam); 155 | 156 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 157 | 158 | // Disable vertex arrays 159 | GLES20.glDisableVertexAttribArray(mQuadPositionParam); 160 | GLES20.glDisableVertexAttribArray(mQuadTexCoordParam); 161 | 162 | // Restore the depth state for further drawing. 163 | GLES20.glDepthMask(true); 164 | GLES20.glEnable(GLES20.GL_DEPTH_TEST); 165 | 166 | ShaderUtil.checkGLError(TAG, "Draw"); 167 | } 168 | 169 | public static final float[] QUAD_COORDS = new float[]{ 170 | -1.0f, -1.0f, 0.0f, 171 | -1.0f, +1.0f, 0.0f, 172 | +1.0f, -1.0f, 0.0f, 173 | +1.0f, +1.0f, 0.0f, 174 | }; 175 | 176 | public static final float[] QUAD_TEXCOORDS = new float[]{ 177 | 0.0f, 1.0f, 178 | 0.0f, 0.0f, 179 | 1.0f, 1.0f, 180 | 1.0f, 0.0f, 181 | }; 182 | } 183 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/rendering/FrameAttachment.java: -------------------------------------------------------------------------------- 1 | package com.google.ar.core.examples.java.helloar.rendering; 2 | 3 | import com.google.ar.core.Anchor; 4 | import com.google.ar.core.Pose; 5 | 6 | /** 7 | * Created by liuzhenhui on 2017/9/1. 8 | */ 9 | 10 | public class FrameAttachment implements Attachment { 11 | private final Anchor mAnchor; 12 | 13 | // Allocate temporary storage to avoid multiple allocations per frame. 14 | private final float[] mPoseTranslation = new float[3]; 15 | private final float[] mPoseRotation = new float[4]; 16 | 17 | public FrameAttachment(Anchor anchor) { 18 | mAnchor = anchor; 19 | } 20 | 21 | public boolean isTracking() { 22 | return /*true if*/ 23 | mAnchor.getTrackingState() == Anchor.TrackingState.TRACKING; 24 | } 25 | 26 | public Pose getPose() { 27 | Pose pose = mAnchor.getPose(); 28 | pose.getTranslation(mPoseTranslation, 0); 29 | pose.getRotationQuaternion(mPoseRotation, 0); 30 | mPoseTranslation[1] = mAnchor.getPose().ty(); 31 | return new Pose(mPoseTranslation, mPoseRotation); 32 | } 33 | 34 | public Anchor getAnchor() { 35 | return mAnchor; 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/rendering/ObjectRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | package com.google.ar.core.examples.java.helloar.rendering; 16 | 17 | import com.google.ar.core.examples.java.helloar.R; 18 | 19 | import android.content.Context; 20 | import android.graphics.Bitmap; 21 | import android.graphics.BitmapFactory; 22 | import android.opengl.GLES20; 23 | import android.opengl.GLUtils; 24 | import android.opengl.Matrix; 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | import java.nio.ByteBuffer; 28 | import java.nio.ByteOrder; 29 | import java.nio.FloatBuffer; 30 | import java.nio.IntBuffer; 31 | import java.nio.ShortBuffer; 32 | import de.javagl.Obj; 33 | import de.javagl.ObjData; 34 | import de.javagl.ObjReader; 35 | import de.javagl.ObjUtils; 36 | 37 | /** 38 | * Renders an object loaded from an OBJ file in OpenGL. 39 | */ 40 | public class ObjectRenderer { 41 | private static final String TAG = ObjectRenderer.class.getSimpleName(); 42 | 43 | /** 44 | * Blend mode. 45 | * 46 | * @see #setBlendMode(BlendMode) 47 | */ 48 | public enum BlendMode { 49 | /** Multiplies the destination color by the source alpha. */ 50 | Shadow, 51 | /** Normal alpha blending. */ 52 | Grid 53 | }; 54 | 55 | private static final int COORDS_PER_VERTEX = 3; 56 | 57 | // Note: the last component must be zero to avoid applying the translational part of the matrix. 58 | private static final float[] LIGHT_DIRECTION = new float[] { 0.0f, 1.0f, 0.0f, 0.0f }; 59 | private float[] mViewLightDirection = new float[4]; 60 | 61 | // Object vertex buffer variables. 62 | private int mVertexBufferId; 63 | private int mVerticesBaseAddress; 64 | private int mTexCoordsBaseAddress; 65 | private int mNormalsBaseAddress; 66 | private int mIndexBufferId; 67 | private int mIndexCount; 68 | 69 | private int mProgram; 70 | private int[] mTextures = new int[1]; 71 | 72 | // Shader location: model view projection matrix. 73 | private int mModelViewUniform; 74 | private int mModelViewProjectionUniform; 75 | 76 | // Shader location: object attributes. 77 | private int mPositionAttribute; 78 | private int mNormalAttribute; 79 | private int mTexCoordAttribute; 80 | 81 | // Shader location: texture sampler. 82 | private int mTextureUniform; 83 | 84 | // Shader location: environment properties. 85 | private int mLightingParametersUniform; 86 | 87 | // Shader location: material properties. 88 | private int mMaterialParametersUniform; 89 | 90 | private BlendMode mBlendMode = null; 91 | 92 | // Temporary matrices allocated here to reduce number of allocations for each frame. 93 | private float[] mModelMatrix = new float[16]; 94 | private float[] mModelViewMatrix = new float[16]; 95 | private float[] mModelViewProjectionMatrix = new float[16]; 96 | 97 | // Set some default material properties to use for lighting. 98 | private float mAmbient = 0.3f; 99 | private float mDiffuse = 1.0f; 100 | private float mSpecular = 1.0f; 101 | private float mSpecularPower = 6.0f; 102 | 103 | public ObjectRenderer() { 104 | } 105 | 106 | /** 107 | * Creates and initializes OpenGL resources needed for rendering the model. 108 | * 109 | * @param context Context for loading the shader and below-named model and texture assets. 110 | * @param objAssetName Name of the OBJ file containing the model geometry. 111 | * @param diffuseTextureAssetName Name of the PNG file containing the diffuse texture map. 112 | */ 113 | public void createOnGlThread(Context context, String objAssetName, 114 | String diffuseTextureAssetName) throws IOException { 115 | // Read the texture. 116 | Bitmap textureBitmap = BitmapFactory.decodeStream( 117 | context.getAssets().open(diffuseTextureAssetName)); 118 | 119 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 120 | GLES20.glGenTextures(mTextures.length, mTextures, 0); 121 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]); 122 | 123 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, 124 | GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR); 125 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, 126 | GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); 127 | GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, textureBitmap, 0); 128 | GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D); 129 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); 130 | 131 | textureBitmap.recycle(); 132 | 133 | ShaderUtil.checkGLError(TAG, "Texture loading"); 134 | 135 | // Read the obj file. 136 | InputStream objInputStream = context.getAssets().open(objAssetName); 137 | Obj obj = ObjReader.read(objInputStream); 138 | 139 | // Prepare the Obj so that its structure is suitable for 140 | // rendering with OpenGL: 141 | // 1. Triangulate it 142 | // 2. Make sure that texture coordinates are not ambiguous 143 | // 3. Make sure that normals are not ambiguous 144 | // 4. Convert it to single-indexed data 145 | obj = ObjUtils.convertToRenderable(obj); 146 | 147 | // OpenGL does not use Java arrays. ByteBuffers are used instead to provide data in a format 148 | // that OpenGL understands. 149 | 150 | // Obtain the data from the OBJ, as direct buffers: 151 | IntBuffer wideIndices = ObjData.getFaceVertexIndices(obj, 3); 152 | FloatBuffer vertices = ObjData.getVertices(obj); 153 | FloatBuffer texCoords = ObjData.getTexCoords(obj, 2); 154 | FloatBuffer normals = ObjData.getNormals(obj); 155 | 156 | // Convert int indices to shorts for GL ES 2.0 compatibility 157 | ShortBuffer indices = ByteBuffer.allocateDirect(2 * wideIndices.limit()) 158 | .order(ByteOrder.nativeOrder()).asShortBuffer(); 159 | while (wideIndices.hasRemaining()) { 160 | indices.put((short) wideIndices.get()); 161 | } 162 | indices.rewind(); 163 | 164 | int[] buffers = new int[2]; 165 | GLES20.glGenBuffers(2, buffers, 0); 166 | mVertexBufferId = buffers[0]; 167 | mIndexBufferId = buffers[1]; 168 | 169 | // Load vertex buffer 170 | mVerticesBaseAddress = 0; 171 | mTexCoordsBaseAddress = mVerticesBaseAddress + 4 * vertices.limit(); 172 | mNormalsBaseAddress = mTexCoordsBaseAddress + 4 * texCoords.limit(); 173 | final int totalBytes = mNormalsBaseAddress + 4 * normals.limit(); 174 | 175 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVertexBufferId); 176 | GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, totalBytes, null, GLES20.GL_STATIC_DRAW); 177 | GLES20.glBufferSubData( 178 | GLES20.GL_ARRAY_BUFFER, mVerticesBaseAddress, 4 * vertices.limit(), vertices); 179 | GLES20.glBufferSubData( 180 | GLES20.GL_ARRAY_BUFFER, mTexCoordsBaseAddress, 4 * texCoords.limit(), texCoords); 181 | GLES20.glBufferSubData( 182 | GLES20.GL_ARRAY_BUFFER, mNormalsBaseAddress, 4 * normals.limit(), normals); 183 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); 184 | 185 | // Load index buffer 186 | GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId); 187 | mIndexCount = indices.limit(); 188 | GLES20.glBufferData( 189 | GLES20.GL_ELEMENT_ARRAY_BUFFER, 2 * mIndexCount, indices, GLES20.GL_STATIC_DRAW); 190 | GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0); 191 | 192 | ShaderUtil.checkGLError(TAG, "OBJ buffer load"); 193 | 194 | final int vertexShader = ShaderUtil.loadGLShader(TAG, context, 195 | GLES20.GL_VERTEX_SHADER, R.raw.object_vertex); 196 | final int fragmentShader = ShaderUtil.loadGLShader(TAG, context, 197 | GLES20.GL_FRAGMENT_SHADER, R.raw.object_fragment); 198 | 199 | mProgram = GLES20.glCreateProgram(); 200 | GLES20.glAttachShader(mProgram, vertexShader); 201 | GLES20.glAttachShader(mProgram, fragmentShader); 202 | GLES20.glLinkProgram(mProgram); 203 | GLES20.glUseProgram(mProgram); 204 | 205 | ShaderUtil.checkGLError(TAG, "Program creation"); 206 | 207 | mModelViewUniform = GLES20.glGetUniformLocation(mProgram, "u_ModelView"); 208 | mModelViewProjectionUniform = 209 | GLES20.glGetUniformLocation(mProgram, "u_ModelViewProjection"); 210 | 211 | mPositionAttribute = GLES20.glGetAttribLocation(mProgram, "a_Position"); 212 | mNormalAttribute = GLES20.glGetAttribLocation(mProgram, "a_Normal"); 213 | mTexCoordAttribute = GLES20.glGetAttribLocation(mProgram, "a_TexCoord"); 214 | 215 | mTextureUniform = GLES20.glGetUniformLocation(mProgram, "u_Texture"); 216 | 217 | mLightingParametersUniform = GLES20.glGetUniformLocation(mProgram, "u_LightingParameters"); 218 | mMaterialParametersUniform = GLES20.glGetUniformLocation(mProgram, "u_MaterialParameters"); 219 | 220 | ShaderUtil.checkGLError(TAG, "Program parameters"); 221 | 222 | Matrix.setIdentityM(mModelMatrix, 0); 223 | } 224 | 225 | /** 226 | * Selects the blending mode for rendering. 227 | * 228 | * @param blendMode The blending mode. Null indicates no blending (opaque rendering). 229 | */ 230 | public void setBlendMode(BlendMode blendMode) { 231 | mBlendMode = blendMode; 232 | } 233 | 234 | /** 235 | * Updates the object model matrix and applies scaling. 236 | * 237 | * @param modelMatrix A 4x4 model-to-world transformation matrix, stored in column-major order. 238 | * @param scaleFactor A separate scaling factor to apply before the {@code modelMatrix}. 239 | * @see android.opengl.Matrix 240 | */ 241 | public void updateModelMatrix(float[] modelMatrix, float scaleFactor) { 242 | float[] scaleMatrix = new float[16]; 243 | Matrix.setIdentityM(scaleMatrix, 0); 244 | scaleMatrix[0] = scaleFactor; 245 | scaleMatrix[5] = scaleFactor; 246 | scaleMatrix[10] = scaleFactor; 247 | Matrix.multiplyMM(mModelMatrix, 0, modelMatrix, 0, scaleMatrix, 0); 248 | } 249 | 250 | /** 251 | * Sets the surface characteristics of the rendered model. 252 | * 253 | * @param ambient Intensity of non-directional surface illumination. 254 | * @param diffuse Diffuse (matte) surface reflectivity. 255 | * @param specular Specular (shiny) surface reflectivity. 256 | * @param specularPower Surface shininess. Larger values result in a smaller, sharper 257 | * specular highlight. 258 | */ 259 | public void setMaterialProperties( 260 | float ambient, float diffuse, float specular, float specularPower) { 261 | mAmbient = ambient; 262 | mDiffuse = diffuse; 263 | mSpecular = specular; 264 | mSpecularPower = specularPower; 265 | } 266 | 267 | /** 268 | * Draws the model. 269 | * 270 | * @param cameraView A 4x4 view matrix, in column-major order. 271 | * @param cameraPerspective A 4x4 projection matrix, in column-major order. 272 | * @param lightIntensity Illumination intensity. Combined with diffuse and specular material 273 | * properties. 274 | * @see #setBlendMode(BlendMode) 275 | * @see #updateModelMatrix(float[], float) 276 | * @see #setMaterialProperties(float, float, float, float) 277 | * @see android.opengl.Matrix 278 | */ 279 | public void draw(float[] cameraView, float[] cameraPerspective, float lightIntensity) { 280 | 281 | ShaderUtil.checkGLError(TAG, "Before draw"); 282 | 283 | // Build the ModelView and ModelViewProjection matrices 284 | // for calculating object position and light. 285 | Matrix.multiplyMM(mModelViewMatrix, 0, cameraView, 0, mModelMatrix, 0); 286 | Matrix.multiplyMM(mModelViewProjectionMatrix, 0, cameraPerspective, 0, mModelViewMatrix, 0); 287 | 288 | GLES20.glUseProgram(mProgram); 289 | 290 | // Set the lighting environment properties. 291 | Matrix.multiplyMV(mViewLightDirection, 0, mModelViewMatrix, 0, LIGHT_DIRECTION, 0); 292 | normalizeVec3(mViewLightDirection); 293 | GLES20.glUniform4f(mLightingParametersUniform, 294 | mViewLightDirection[0], mViewLightDirection[1], mViewLightDirection[2], lightIntensity); 295 | 296 | // Set the object material properties. 297 | GLES20.glUniform4f(mMaterialParametersUniform, mAmbient, mDiffuse, mSpecular, 298 | mSpecularPower); 299 | 300 | // Attach the object texture. 301 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 302 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]); 303 | GLES20.glUniform1i(mTextureUniform, 0); 304 | 305 | // Set the vertex attributes. 306 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVertexBufferId); 307 | 308 | GLES20.glVertexAttribPointer( 309 | mPositionAttribute, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, mVerticesBaseAddress); 310 | GLES20.glVertexAttribPointer( 311 | mNormalAttribute, 3, GLES20.GL_FLOAT, false, 0, mNormalsBaseAddress); 312 | GLES20.glVertexAttribPointer( 313 | mTexCoordAttribute, 2, GLES20.GL_FLOAT, false, 0, mTexCoordsBaseAddress); 314 | 315 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); 316 | 317 | // Set the ModelViewProjection matrix in the shader. 318 | GLES20.glUniformMatrix4fv( 319 | mModelViewUniform, 1, false, mModelViewMatrix, 0); 320 | GLES20.glUniformMatrix4fv( 321 | mModelViewProjectionUniform, 1, false, mModelViewProjectionMatrix, 0); 322 | 323 | // Enable vertex arrays 324 | GLES20.glEnableVertexAttribArray(mPositionAttribute); 325 | GLES20.glEnableVertexAttribArray(mNormalAttribute); 326 | GLES20.glEnableVertexAttribArray(mTexCoordAttribute); 327 | 328 | if (mBlendMode != null) { 329 | GLES20.glDepthMask(false); 330 | GLES20.glEnable(GLES20.GL_BLEND); 331 | switch (mBlendMode) { 332 | case Shadow: 333 | // Multiplicative blending function for Shadow. 334 | GLES20.glBlendFunc(GLES20.GL_ZERO, GLES20.GL_ONE_MINUS_SRC_ALPHA); 335 | break; 336 | case Grid: 337 | // Grid, additive blending function. 338 | GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); 339 | break; 340 | } 341 | } 342 | 343 | GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId); 344 | GLES20.glDrawElements(GLES20.GL_TRIANGLES, mIndexCount, GLES20.GL_UNSIGNED_SHORT, 0); 345 | GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0); 346 | 347 | if (mBlendMode != null) { 348 | GLES20.glDisable(GLES20.GL_BLEND); 349 | GLES20.glDepthMask(true); 350 | } 351 | 352 | // Disable vertex arrays 353 | GLES20.glDisableVertexAttribArray(mPositionAttribute); 354 | GLES20.glDisableVertexAttribArray(mNormalAttribute); 355 | GLES20.glDisableVertexAttribArray(mTexCoordAttribute); 356 | 357 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); 358 | 359 | ShaderUtil.checkGLError(TAG, "After draw"); 360 | } 361 | 362 | public static void normalizeVec3(float[] v) { 363 | float reciprocalLength = 1.0f / (float) Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); 364 | v[0] *= reciprocalLength; 365 | v[1] *= reciprocalLength; 366 | v[2] *= reciprocalLength; 367 | } 368 | } 369 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/rendering/PlaneAttachment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | package com.google.ar.core.examples.java.helloar.rendering; 16 | 17 | import com.google.ar.core.Anchor; 18 | import com.google.ar.core.Plane; 19 | import com.google.ar.core.Pose; 20 | 21 | /** 22 | * This class tracks the attachment of object's Anchor to a Plane. It will construct a pose 23 | * that will stay on the plane (in Y direction), while still properly tracking the XZ changes 24 | * from the anchor updates. 25 | */ 26 | public class PlaneAttachment implements Attachment{ 27 | private final Plane mPlane; 28 | private final Anchor mAnchor; 29 | 30 | // Allocate temporary storage to avoid multiple allocations per frame. 31 | private final float[] mPoseTranslation = new float[3]; 32 | private final float[] mPoseRotation = new float[4]; 33 | 34 | public PlaneAttachment(Plane plane, Anchor anchor) { 35 | mPlane = plane; 36 | mAnchor = anchor; 37 | } 38 | 39 | public boolean isTracking() { 40 | return /*true if*/ 41 | mPlane.getTrackingState() == Plane.TrackingState.TRACKING && 42 | mAnchor.getTrackingState() == Anchor.TrackingState.TRACKING; 43 | } 44 | 45 | public Pose getPose() { 46 | Pose pose = mAnchor.getPose(); 47 | pose.getTranslation(mPoseTranslation, 0); 48 | pose.getRotationQuaternion(mPoseRotation, 0); 49 | mPoseTranslation[1] = mPlane.getCenterPose().ty(); 50 | return new Pose(mPoseTranslation, mPoseRotation); 51 | } 52 | 53 | public Anchor getAnchor() { 54 | return mAnchor; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/rendering/PlaneRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | package com.google.ar.core.examples.java.helloar.rendering; 16 | 17 | import com.google.ar.core.Frame; 18 | import com.google.ar.core.Plane; 19 | import com.google.ar.core.Pose; 20 | import com.google.ar.core.Session; 21 | import com.google.ar.core.examples.java.helloar.R; 22 | 23 | import android.content.Context; 24 | import android.graphics.Bitmap; 25 | import android.graphics.BitmapFactory; 26 | import android.opengl.GLES20; 27 | import android.opengl.GLSurfaceView; 28 | import android.opengl.GLUtils; 29 | import android.opengl.Matrix; 30 | 31 | import java.io.IOException; 32 | import java.nio.ByteBuffer; 33 | import java.nio.ByteOrder; 34 | import java.nio.FloatBuffer; 35 | import java.nio.ShortBuffer; 36 | import java.util.ArrayList; 37 | import java.util.Collection; 38 | import java.util.Collections; 39 | import java.util.Comparator; 40 | import java.util.HashMap; 41 | import java.util.List; 42 | import java.util.Map; 43 | import javax.microedition.khronos.egl.EGLConfig; 44 | import javax.microedition.khronos.opengles.GL10; 45 | 46 | /** 47 | * Renders the detected AR planes. 48 | */ 49 | public class PlaneRenderer { 50 | private static final String TAG = PlaneRenderer.class.getSimpleName(); 51 | 52 | private static final int BYTES_PER_FLOAT = Float.SIZE / 8; 53 | private static final int BYTES_PER_SHORT = Short.SIZE / 8; 54 | private static final int COORDS_PER_VERTEX = 3; // x, z, alpha 55 | 56 | private static final int VERTS_PER_BOUNDARY_VERT = 2; 57 | private static final int INDICES_PER_BOUNDARY_VERT = 3; 58 | private static final int INITIAL_BUFFER_BOUNDARY_VERTS = 64; 59 | 60 | private static final int INITIAL_VERTEX_BUFFER_SIZE_BYTES = 61 | BYTES_PER_FLOAT * COORDS_PER_VERTEX * VERTS_PER_BOUNDARY_VERT * 62 | INITIAL_BUFFER_BOUNDARY_VERTS; 63 | 64 | private static final int INITIAL_INDEX_BUFFER_SIZE_BYTES = 65 | BYTES_PER_SHORT * INDICES_PER_BOUNDARY_VERT * INDICES_PER_BOUNDARY_VERT * 66 | INITIAL_BUFFER_BOUNDARY_VERTS; 67 | 68 | private static final float FADE_RADIUS_M = 0.25f; 69 | private static final float DOTS_PER_METER = 10.0f; 70 | private static final float EQUILATERAL_TRIANGLE_SCALE = (float) (1 / Math.sqrt(3)); 71 | 72 | // Using the "signed distance field" approach to render sharp lines and circles. 73 | // {dotThreshold, lineThreshold, lineFadeSpeed, occlusionScale} 74 | // dotThreshold/lineThreshold: red/green intensity above which dots/lines are present 75 | // lineFadeShrink: lines will fade in between alpha = 1-(1/lineFadeShrink) and 1.0 76 | // occlusionShrink: occluded planes will fade out between alpha = 0 and 1/occlusionShrink 77 | private static final float[] GRID_CONTROL = {0.2f, 0.4f, 2.0f, 1.5f}; 78 | 79 | private int mPlaneProgram; 80 | private int[] mTextures = new int[1]; 81 | 82 | private int mPlaneXZPositionAlphaAttribute; 83 | 84 | private int mPlaneModelUniform; 85 | private int mPlaneModelViewProjectionUniform; 86 | private int mTextureUniform; 87 | private int mLineColorUniform; 88 | private int mDotColorUniform; 89 | private int mGridControlUniform; 90 | private int mPlaneUvMatrixUniform; 91 | 92 | private FloatBuffer mVertexBuffer = ByteBuffer.allocateDirect(INITIAL_VERTEX_BUFFER_SIZE_BYTES) 93 | .order(ByteOrder.nativeOrder()).asFloatBuffer(); 94 | private ShortBuffer mIndexBuffer = ByteBuffer.allocateDirect(INITIAL_INDEX_BUFFER_SIZE_BYTES) 95 | .order(ByteOrder.nativeOrder()).asShortBuffer(); 96 | 97 | // Temporary lists/matrices allocated here to reduce number of allocations for each frame. 98 | private float[] mModelMatrix = new float[16]; 99 | private float[] mModelViewMatrix = new float[16]; 100 | private float[] mModelViewProjectionMatrix = new float[16]; 101 | private float[] mPlaneColor = new float[4]; 102 | private float[] mPlaneAngleUvMatrix = new float[4]; // 2x2 rotation matrix applied to uv coords. 103 | 104 | private Map mPlaneIndexMap = new HashMap<>(); 105 | 106 | public PlaneRenderer() { 107 | } 108 | 109 | /** 110 | * Allocates and initializes OpenGL resources needed by the plane renderer. Must be 111 | * called on the OpenGL thread, typically in 112 | * {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}. 113 | * 114 | * @param context Needed to access shader source and texture PNG. 115 | * @param gridDistanceTextureName Name of the PNG file containing the grid texture. 116 | */ 117 | public void createOnGlThread(Context context, String gridDistanceTextureName) 118 | throws IOException { 119 | int vertexShader = ShaderUtil.loadGLShader(TAG, context, 120 | GLES20.GL_VERTEX_SHADER, R.raw.plane_vertex); 121 | int passthroughShader = ShaderUtil.loadGLShader(TAG, context, 122 | GLES20.GL_FRAGMENT_SHADER, R.raw.plane_fragment); 123 | 124 | mPlaneProgram = GLES20.glCreateProgram(); 125 | GLES20.glAttachShader(mPlaneProgram, vertexShader); 126 | GLES20.glAttachShader(mPlaneProgram, passthroughShader); 127 | GLES20.glLinkProgram(mPlaneProgram); 128 | GLES20.glUseProgram(mPlaneProgram); 129 | 130 | ShaderUtil.checkGLError(TAG, "Program creation"); 131 | 132 | // Read the texture. 133 | Bitmap textureBitmap = BitmapFactory.decodeStream( 134 | context.getAssets().open(gridDistanceTextureName)); 135 | 136 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 137 | GLES20.glGenTextures(mTextures.length, mTextures, 0); 138 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]); 139 | 140 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, 141 | GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR); 142 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, 143 | GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); 144 | GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, textureBitmap, 0); 145 | GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D); 146 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); 147 | 148 | ShaderUtil.checkGLError(TAG, "Texture loading"); 149 | 150 | mPlaneXZPositionAlphaAttribute = GLES20.glGetAttribLocation(mPlaneProgram, 151 | "a_XZPositionAlpha"); 152 | 153 | mPlaneModelUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_Model"); 154 | mPlaneModelViewProjectionUniform = 155 | GLES20.glGetUniformLocation(mPlaneProgram, "u_ModelViewProjection"); 156 | mTextureUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_Texture"); 157 | mLineColorUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_lineColor"); 158 | mDotColorUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_dotColor"); 159 | mGridControlUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_gridControl"); 160 | mPlaneUvMatrixUniform = GLES20.glGetUniformLocation(mPlaneProgram, "u_PlaneUvMatrix"); 161 | 162 | ShaderUtil.checkGLError(TAG, "Program parameters"); 163 | } 164 | 165 | /** 166 | * Updates the plane model transform matrix and extents. 167 | */ 168 | private void updatePlaneParameters(float[] planeMatrix, float extentX, float extentZ, 169 | FloatBuffer boundary) { 170 | System.arraycopy(planeMatrix, 0, mModelMatrix, 0, 16); 171 | if (boundary == null) { 172 | mVertexBuffer.limit(0); 173 | mIndexBuffer.limit(0); 174 | return; 175 | } 176 | 177 | // Generate a new set of vertices and a corresponding triangle strip index set so that 178 | // the plane boundary polygon has a fading edge. This is done by making a copy of the 179 | // boundary polygon vertices and scaling it down around center to push it inwards. Then 180 | // the index buffer is setup accordingly. 181 | boundary.rewind(); 182 | int boundaryVertices = boundary.limit() / 2; 183 | int numVertices; 184 | int numIndices; 185 | 186 | numVertices = boundaryVertices * VERTS_PER_BOUNDARY_VERT; 187 | // drawn as GL_TRIANGLE_STRIP with 3n-2 triangles (n-2 for fill, 2n for perimeter). 188 | numIndices = boundaryVertices * INDICES_PER_BOUNDARY_VERT; 189 | 190 | if (mVertexBuffer.capacity() < numVertices * COORDS_PER_VERTEX) { 191 | int size = mVertexBuffer.capacity(); 192 | while (size < numVertices * COORDS_PER_VERTEX) { 193 | size *= 2; 194 | } 195 | mVertexBuffer = ByteBuffer.allocateDirect(BYTES_PER_FLOAT * size) 196 | .order(ByteOrder.nativeOrder()).asFloatBuffer(); 197 | } 198 | mVertexBuffer.rewind(); 199 | mVertexBuffer.limit(numVertices * COORDS_PER_VERTEX); 200 | 201 | 202 | if (mIndexBuffer.capacity() < numIndices) { 203 | int size = mIndexBuffer.capacity(); 204 | while (size < numIndices) { 205 | size *= 2; 206 | } 207 | mIndexBuffer = ByteBuffer.allocateDirect(BYTES_PER_SHORT * size) 208 | .order(ByteOrder.nativeOrder()).asShortBuffer(); 209 | } 210 | mIndexBuffer.rewind(); 211 | mIndexBuffer.limit(numIndices); 212 | 213 | // Note: when either dimension of the bounding box is smaller than 2*FADE_RADIUS_M we 214 | // generate a bunch of 0-area triangles. These don't get rendered though so it works 215 | // out ok. 216 | float xScale = Math.max((extentX - 2 * FADE_RADIUS_M) / extentX, 0.0f); 217 | float zScale = Math.max((extentZ - 2 * FADE_RADIUS_M) / extentZ, 0.0f); 218 | 219 | while (boundary.hasRemaining()) { 220 | float x = boundary.get(); 221 | float z = boundary.get(); 222 | mVertexBuffer.put(x); 223 | mVertexBuffer.put(z); 224 | mVertexBuffer.put(0.0f); 225 | mVertexBuffer.put(x * xScale); 226 | mVertexBuffer.put(z * zScale); 227 | mVertexBuffer.put(1.0f); 228 | } 229 | 230 | // step 1, perimeter 231 | mIndexBuffer.put((short) ((boundaryVertices - 1) * 2)); 232 | for (int i = 0; i < boundaryVertices; ++i) { 233 | mIndexBuffer.put((short) (i * 2)); 234 | mIndexBuffer.put((short) (i * 2 + 1)); 235 | } 236 | mIndexBuffer.put((short) 1); 237 | // This leaves us on the interior edge of the perimeter between the inset vertices 238 | // for boundary verts n-1 and 0. 239 | 240 | // step 2, interior: 241 | for (int i = 1; i < boundaryVertices / 2; ++i) { 242 | mIndexBuffer.put((short) ((boundaryVertices - 1 - i) * 2 + 1)); 243 | mIndexBuffer.put((short) (i * 2 + 1)); 244 | } 245 | if (boundaryVertices % 2 != 0) { 246 | mIndexBuffer.put((short) ((boundaryVertices / 2) * 2 + 1)); 247 | } 248 | } 249 | 250 | private void draw(float[] cameraView, float[] cameraPerspective) { 251 | // Build the ModelView and ModelViewProjection matrices 252 | // for calculating cube position and light. 253 | Matrix.multiplyMM(mModelViewMatrix, 0, cameraView, 0, mModelMatrix, 0); 254 | Matrix.multiplyMM(mModelViewProjectionMatrix, 0, cameraPerspective, 0, mModelViewMatrix, 0); 255 | 256 | // Set the position of the plane 257 | mVertexBuffer.rewind(); 258 | GLES20.glVertexAttribPointer( 259 | mPlaneXZPositionAlphaAttribute, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 260 | BYTES_PER_FLOAT * COORDS_PER_VERTEX, mVertexBuffer); 261 | 262 | // Set the Model and ModelViewProjection matrices in the shader. 263 | GLES20.glUniformMatrix4fv(mPlaneModelUniform, 1, false, mModelMatrix, 0); 264 | GLES20.glUniformMatrix4fv( 265 | mPlaneModelViewProjectionUniform, 1, false, mModelViewProjectionMatrix, 0); 266 | 267 | mIndexBuffer.rewind(); 268 | GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP, mIndexBuffer.limit(), 269 | GLES20.GL_UNSIGNED_SHORT, mIndexBuffer); 270 | ShaderUtil.checkGLError(TAG, "Drawing plane"); 271 | } 272 | 273 | static class SortablePlane { 274 | final float mDistance; 275 | final Plane mPlane; 276 | public SortablePlane(float distance, Plane plane) { 277 | this.mDistance = distance; 278 | this.mPlane = plane; 279 | } 280 | } 281 | 282 | /** 283 | * Draws the collection of tracked planes, with closer planes hiding more distant ones. 284 | * 285 | * @param allPlanes The collection of planes to draw. 286 | * @param cameraPose The pose of the camera, as returned by {@link Frame#getPose()} 287 | * @param cameraPerspective The projection matrix, as returned by 288 | * {@link Session#getProjectionMatrix(float[], int, float, float)} 289 | */ 290 | public void drawPlanes(Collection allPlanes, Pose cameraPose, 291 | float[] cameraPerspective) { 292 | // Planes must be sorted by distance from camera so that we draw closer planes first, and 293 | // they occlude the farther planes. 294 | List sortedPlanes = new ArrayList<>(); 295 | float[] normal = new float[3]; 296 | float cameraX = cameraPose.tx(); 297 | float cameraY = cameraPose.ty(); 298 | float cameraZ = cameraPose.tz(); 299 | for (Plane plane : allPlanes) { 300 | if (plane.getType() != com.google.ar.core.Plane.Type.HORIZONTAL_UPWARD_FACING || 301 | plane.getTrackingState() != Plane.TrackingState.TRACKING) { 302 | continue; 303 | } 304 | 305 | Pose center = plane.getCenterPose(); 306 | // Get transformed Y axis of plane's coordinate system. 307 | center.getTransformedAxis(1, 1.0f, normal, 0); 308 | // Compute dot product of plane's normal with vector from camera to plane center. 309 | float distance = (cameraX - center.tx()) * normal[0] + 310 | (cameraY - center.ty()) * normal[1] + (cameraZ - center.tz()) * normal[2]; 311 | if (distance < 0) { // Plane is back-facing. 312 | continue; 313 | } 314 | sortedPlanes.add(new SortablePlane(distance, plane)); 315 | } 316 | Collections.sort(sortedPlanes, new Comparator() { 317 | @Override 318 | public int compare(SortablePlane a, SortablePlane b) { 319 | return Float.compare(a.mDistance, b.mDistance); 320 | } 321 | }); 322 | 323 | 324 | float cameraView[] = new float[16]; 325 | cameraPose.inverse().toMatrix(cameraView, 0); 326 | 327 | // Planes are drawn with additive blending, masked by the alpha channel for occlusion. 328 | 329 | // Start by clearing the alpha channel of the color buffer to 1.0. 330 | GLES20.glClearColor(1, 1, 1, 1); 331 | GLES20.glColorMask(false, false, false, true); 332 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); 333 | GLES20.glColorMask(true, true, true, true); 334 | 335 | // Disable depth write. 336 | GLES20.glDepthMask(false); 337 | 338 | // Additive blending, masked by alpha chanel, clearing alpha channel. 339 | GLES20.glEnable(GLES20.GL_BLEND); 340 | GLES20.glBlendFuncSeparate( 341 | GLES20.GL_DST_ALPHA, GLES20.GL_ONE, // RGB (src, dest) 342 | GLES20.GL_ZERO, GLES20.GL_ONE_MINUS_SRC_ALPHA); // ALPHA (src, dest) 343 | 344 | // Set up the shader. 345 | GLES20.glUseProgram(mPlaneProgram); 346 | 347 | // Attach the texture. 348 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 349 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]); 350 | GLES20.glUniform1i(mTextureUniform, 0); 351 | 352 | // Shared fragment uniforms. 353 | GLES20.glUniform4fv(mGridControlUniform, 1, GRID_CONTROL, 0); 354 | 355 | // Enable vertex arrays 356 | GLES20.glEnableVertexAttribArray(mPlaneXZPositionAlphaAttribute); 357 | 358 | ShaderUtil.checkGLError(TAG, "Setting up to draw planes"); 359 | 360 | for (SortablePlane sortedPlane : sortedPlanes) { 361 | Plane plane = sortedPlane.mPlane; 362 | float[] planeMatrix = new float[16]; 363 | plane.getCenterPose().toMatrix(planeMatrix, 0); 364 | 365 | updatePlaneParameters(planeMatrix, plane.getExtentX(), 366 | plane.getExtentZ(), plane.getPlanePolygon()); 367 | 368 | // Get plane index. Keep a map to assign same indices to same planes. 369 | Integer planeIndex = mPlaneIndexMap.get(plane); 370 | if (planeIndex == null) { 371 | planeIndex = Integer.valueOf(mPlaneIndexMap.size()); 372 | mPlaneIndexMap.put(plane, planeIndex); 373 | } 374 | 375 | // Set plane color. Computed deterministically from the Plane index. 376 | int colorIndex = planeIndex % PLANE_COLORS_RGBA.length; 377 | colorRgbaToFloat(mPlaneColor, PLANE_COLORS_RGBA[colorIndex]); 378 | GLES20.glUniform4fv(mLineColorUniform, 1, mPlaneColor, 0); 379 | GLES20.glUniform4fv(mDotColorUniform, 1, mPlaneColor, 0); 380 | 381 | // Each plane will have its own angle offset from others, to make them easier to 382 | // distinguish. Compute a 2x2 rotation matrix from the angle. 383 | float angleRadians = planeIndex * 0.144f; 384 | float uScale = DOTS_PER_METER; 385 | float vScale = DOTS_PER_METER * EQUILATERAL_TRIANGLE_SCALE; 386 | mPlaneAngleUvMatrix[0] = +(float) Math.cos(angleRadians) * uScale; 387 | mPlaneAngleUvMatrix[1] = -(float) Math.sin(angleRadians) * uScale; 388 | mPlaneAngleUvMatrix[2] = +(float) Math.sin(angleRadians) * vScale; 389 | mPlaneAngleUvMatrix[3] = +(float) Math.cos(angleRadians) * vScale; 390 | GLES20.glUniformMatrix2fv(mPlaneUvMatrixUniform, 1, false, mPlaneAngleUvMatrix, 0); 391 | 392 | draw(cameraView, cameraPerspective); 393 | } 394 | 395 | // Clean up the state we set 396 | GLES20.glDisableVertexAttribArray(mPlaneXZPositionAlphaAttribute); 397 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); 398 | GLES20.glDisable(GLES20.GL_BLEND); 399 | GLES20.glDepthMask(true); 400 | 401 | ShaderUtil.checkGLError(TAG, "Cleaning up after drawing planes"); 402 | } 403 | 404 | private static void colorRgbaToFloat(float[] planeColor, int colorRgba) { 405 | planeColor[0] = ((float) ((colorRgba >> 24) & 0xff)) / 255.0f; 406 | planeColor[1] = ((float) ((colorRgba >> 16) & 0xff)) / 255.0f; 407 | planeColor[2] = ((float) ((colorRgba >> 8) & 0xff)) / 255.0f; 408 | planeColor[3] = ((float) ((colorRgba >> 0) & 0xff)) / 255.0f; 409 | } 410 | 411 | private static final int[] PLANE_COLORS_RGBA = { 412 | 0xFFFFFFFF, 413 | 0xF44336FF, 414 | 0xE91E63FF, 415 | 0x9C27B0FF, 416 | 0x673AB7FF, 417 | 0x3F51B5FF, 418 | 0x2196F3FF, 419 | 0x03A9F4FF, 420 | 0x00BCD4FF, 421 | 0x009688FF, 422 | 0x4CAF50FF, 423 | 0x8BC34AFF, 424 | 0xCDDC39FF, 425 | 0xFFEB3BFF, 426 | 0xFFC107FF, 427 | 0xFF9800FF, 428 | }; 429 | } 430 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/rendering/PointCloudRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | package com.google.ar.core.examples.java.helloar.rendering; 16 | 17 | import com.google.ar.core.Frame; 18 | import com.google.ar.core.PointCloud; 19 | import com.google.ar.core.Pose; 20 | import com.google.ar.core.Session; 21 | import com.google.ar.core.examples.java.helloar.R; 22 | 23 | import android.content.Context; 24 | import android.opengl.GLES20; 25 | import android.opengl.GLSurfaceView; 26 | import android.opengl.Matrix; 27 | import javax.microedition.khronos.egl.EGLConfig; 28 | import javax.microedition.khronos.opengles.GL10; 29 | 30 | /** 31 | * Renders a point cloud. 32 | */ 33 | public class PointCloudRenderer { 34 | private static final String TAG = PointCloud.class.getSimpleName(); 35 | 36 | private static final int BYTES_PER_FLOAT = Float.SIZE / 8; 37 | private static final int FLOATS_PER_POINT = 4; // X,Y,Z,confidence. 38 | private static final int BYTES_PER_POINT = BYTES_PER_FLOAT * FLOATS_PER_POINT; 39 | private static final int INITIAL_BUFFER_POINTS = 1000; 40 | 41 | private int mVbo; 42 | private int mVboSize; 43 | 44 | private int mProgramName; 45 | private int mPositionAttribute; 46 | private int mModelViewProjectionUniform; 47 | private int mColorUniform; 48 | private int mPointSizeUniform; 49 | 50 | private int mNumPoints = 0; 51 | 52 | // Keep track of the last point cloud rendered to avoid updating the VBO if point cloud 53 | // was not changed. 54 | private PointCloud mLastPointCloud = null; 55 | 56 | public PointCloudRenderer() { 57 | } 58 | 59 | /** 60 | * Allocates and initializes OpenGL resources needed by the plane renderer. Must be 61 | * called on the OpenGL thread, typically in 62 | * {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}. 63 | * 64 | * @param context Needed to access shader source. 65 | */ 66 | public void createOnGlThread(Context context) { 67 | ShaderUtil.checkGLError(TAG, "before create"); 68 | 69 | int buffers[] = new int[1]; 70 | GLES20.glGenBuffers(1, buffers, 0); 71 | mVbo = buffers[0]; 72 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVbo); 73 | 74 | mVboSize = INITIAL_BUFFER_POINTS * BYTES_PER_POINT; 75 | GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mVboSize, null, GLES20.GL_DYNAMIC_DRAW); 76 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); 77 | 78 | ShaderUtil.checkGLError(TAG, "buffer alloc"); 79 | 80 | int vertexShader = ShaderUtil.loadGLShader(TAG, context, 81 | GLES20.GL_VERTEX_SHADER, R.raw.point_cloud_vertex); 82 | int passthroughShader = ShaderUtil.loadGLShader(TAG, context, 83 | GLES20.GL_FRAGMENT_SHADER, R.raw.passthrough_fragment); 84 | 85 | mProgramName = GLES20.glCreateProgram(); 86 | GLES20.glAttachShader(mProgramName, vertexShader); 87 | GLES20.glAttachShader(mProgramName, passthroughShader); 88 | GLES20.glLinkProgram(mProgramName); 89 | GLES20.glUseProgram(mProgramName); 90 | 91 | ShaderUtil.checkGLError(TAG, "program"); 92 | 93 | mPositionAttribute = GLES20.glGetAttribLocation(mProgramName, "a_Position"); 94 | mColorUniform = GLES20.glGetUniformLocation(mProgramName, "u_Color"); 95 | mModelViewProjectionUniform = GLES20.glGetUniformLocation( 96 | mProgramName, "u_ModelViewProjection"); 97 | mPointSizeUniform = GLES20.glGetUniformLocation(mProgramName, "u_PointSize"); 98 | 99 | ShaderUtil.checkGLError(TAG, "program params"); 100 | } 101 | 102 | /** 103 | * Updates the OpenGL buffer contents to the provided point. Repeated calls with the same 104 | * point cloud will be ignored. 105 | */ 106 | public void update(PointCloud cloud) { 107 | if (mLastPointCloud == cloud) { 108 | // Redundant call. 109 | return; 110 | } 111 | 112 | ShaderUtil.checkGLError(TAG, "before update"); 113 | 114 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVbo); 115 | mLastPointCloud = cloud; 116 | 117 | // If the VBO is not large enough to fit the new point cloud, resize it. 118 | mNumPoints = mLastPointCloud.getPoints().remaining() / FLOATS_PER_POINT; 119 | if (mNumPoints * BYTES_PER_POINT > mVboSize) { 120 | while (mNumPoints * BYTES_PER_POINT > mVboSize) { 121 | mVboSize *= 2; 122 | } 123 | GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, mVboSize, null, GLES20.GL_DYNAMIC_DRAW); 124 | } 125 | GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, mNumPoints * BYTES_PER_POINT, 126 | mLastPointCloud.getPoints()); 127 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); 128 | 129 | ShaderUtil.checkGLError(TAG, "after update"); 130 | } 131 | 132 | /** 133 | * Renders the point cloud. 134 | * 135 | * @param pose the current point cloud pose, from {@link Frame#getPointCloudPose()}. 136 | * @param cameraView the camera view matrix for this frame, typically from 137 | * {@link Frame#getViewMatrix(float[], int)}. 138 | * @param cameraPerspective the camera projection matrix for this frame, typically from 139 | * {@link Session#getProjectionMatrix(float[], int, float, float)}. 140 | */ 141 | public void draw(Pose pose, float[] cameraView, float[] cameraPerspective) { 142 | float[] modelMatrix = new float[16]; 143 | pose.toMatrix(modelMatrix, 0); 144 | 145 | float[] modelView = new float[16]; 146 | float[] modelViewProjection = new float[16]; 147 | Matrix.multiplyMM(modelView, 0, cameraView, 0, modelMatrix, 0); 148 | Matrix.multiplyMM(modelViewProjection, 0, cameraPerspective, 0, modelView, 0); 149 | 150 | ShaderUtil.checkGLError(TAG, "Before draw"); 151 | 152 | GLES20.glUseProgram(mProgramName); 153 | GLES20.glEnableVertexAttribArray(mPositionAttribute); 154 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, mVbo); 155 | GLES20.glVertexAttribPointer( 156 | mPositionAttribute, 4, GLES20.GL_FLOAT, false, BYTES_PER_POINT, 0); 157 | GLES20.glUniform4f(mColorUniform, 31.0f / 255.0f, 188.0f / 255.0f, 210.0f / 255.0f, 1.0f); 158 | GLES20.glUniformMatrix4fv(mModelViewProjectionUniform, 1, false, modelViewProjection, 0); 159 | GLES20.glUniform1f(mPointSizeUniform, 5.0f); 160 | 161 | GLES20.glDrawArrays(GLES20.GL_POINTS, 0, mNumPoints); 162 | GLES20.glDisableVertexAttribArray(mPositionAttribute); 163 | GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); 164 | 165 | ShaderUtil.checkGLError(TAG, "Draw"); 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/rendering/ShaderUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | package com.google.ar.core.examples.java.helloar.rendering; 16 | 17 | import android.content.Context; 18 | import android.opengl.GLES20; 19 | import android.util.Log; 20 | 21 | import java.io.BufferedReader; 22 | import java.io.IOException; 23 | import java.io.InputStream; 24 | import java.io.InputStreamReader; 25 | 26 | /** 27 | * Shader helper functions. 28 | */ 29 | public class ShaderUtil { 30 | /** 31 | * Converts a raw text file, saved as a resource, into an OpenGL ES shader. 32 | * 33 | * @param type The type of shader we will be creating. 34 | * @param resId The resource ID of the raw text file about to be turned into a shader. 35 | * @return The shader object handler. 36 | */ 37 | public static int loadGLShader(String tag, Context context, int type, int resId) { 38 | String code = readRawTextFile(context, resId); 39 | int shader = GLES20.glCreateShader(type); 40 | GLES20.glShaderSource(shader, code); 41 | GLES20.glCompileShader(shader); 42 | 43 | // Get the compilation status. 44 | final int[] compileStatus = new int[1]; 45 | GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0); 46 | 47 | // If the compilation failed, delete the shader. 48 | if (compileStatus[0] == 0) { 49 | Log.e(tag, "Error compiling shader: " + GLES20.glGetShaderInfoLog(shader)); 50 | GLES20.glDeleteShader(shader); 51 | shader = 0; 52 | } 53 | 54 | if (shader == 0) { 55 | throw new RuntimeException("Error creating shader."); 56 | } 57 | 58 | return shader; 59 | } 60 | 61 | /** 62 | * Checks if we've had an error inside of OpenGL ES, and if so what that error is. 63 | * 64 | * @param label Label to report in case of error. 65 | * @throws RuntimeException If an OpenGL error is detected. 66 | */ 67 | public static void checkGLError(String tag, String label) { 68 | int error; 69 | while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) { 70 | Log.e(tag, label + ": glError " + error); 71 | throw new RuntimeException(label + ": glError " + error); 72 | } 73 | } 74 | 75 | /** 76 | * Converts a raw text file into a string. 77 | * 78 | * @param resId The resource ID of the raw text file about to be turned into a shader. 79 | * @return The context of the text file, or null in case of error. 80 | */ 81 | private static String readRawTextFile(Context context, int resId) { 82 | InputStream inputStream = context.getResources().openRawResource(resId); 83 | try { 84 | BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 85 | StringBuilder sb = new StringBuilder(); 86 | String line; 87 | while ((line = reader.readLine()) != null) { 88 | sb.append(line).append("\n"); 89 | } 90 | reader.close(); 91 | return sb.toString(); 92 | } catch (IOException e) { 93 | e.printStackTrace(); 94 | } 95 | return null; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/rendering/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This package contains classes that do the rendering for this example. 18 | */ 19 | package com.google.ar.core.examples.java.helloar.rendering; 20 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/utils/TLog.java: -------------------------------------------------------------------------------- 1 | package com.google.ar.core.examples.java.helloar.utils; 2 | 3 | import android.util.Log; 4 | 5 | /** 6 | * Created by liuzhenhui on 2017/8/31. 7 | */ 8 | 9 | public class TLog { 10 | private static final String TAG = "lzh"; 11 | 12 | private static boolean isDebug() { 13 | return true; 14 | } 15 | 16 | public static final void d(String msg) { 17 | if (isDebug()) { 18 | Log.d(TAG, TAG + "-->" + msg); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jason0539/arcore-android-sdk/0398b60da0aa7fb23fb4e969e76e81cd4360cb8c/samples/java_arcore_hello_ar/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 16 | 21 | 22 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/raw/light_vertex.shader: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | uniform mat4 u_Model; 17 | uniform mat4 u_ModelViewProjection; 18 | uniform mat4 u_ModelView; 19 | uniform vec3 u_LightPos; 20 | 21 | attribute vec4 a_Position; 22 | attribute vec4 a_Color; 23 | attribute vec3 a_Normal; 24 | 25 | varying vec4 v_Color; 26 | varying vec3 v_Grid; 27 | 28 | void main() { 29 | v_Grid = vec3(u_Model * a_Position); 30 | 31 | vec3 modelViewVertex = vec3(u_ModelView * a_Position); 32 | vec3 modelViewNormal = vec3(u_ModelView * vec4(a_Normal, 0.0)); 33 | 34 | float distance = length(u_LightPos - modelViewVertex); 35 | vec3 lightVector = normalize(u_LightPos - modelViewVertex); 36 | float diffuse = max(dot(modelViewNormal, lightVector), 0.5); 37 | 38 | diffuse = diffuse * (1.0 / (1.0 + (0.00001 * distance * distance))); 39 | v_Color = vec4(a_Color.rgb * diffuse, a_Color.a); 40 | gl_Position = u_ModelViewProjection * a_Position; 41 | } -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/raw/object_fragment.shader: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | precision mediump float; 17 | 18 | uniform sampler2D u_Texture; 19 | 20 | uniform vec4 u_LightingParameters; 21 | uniform vec4 u_MaterialParameters; 22 | 23 | varying vec3 v_ViewPosition; 24 | varying vec3 v_ViewNormal; 25 | varying vec2 v_TexCoord; 26 | 27 | void main() { 28 | // We support approximate sRGB gamma. 29 | const float kGamma = 0.4545454; 30 | const float kInverseGamma = 2.2; 31 | 32 | // Unpack lighting and material parameters for better naming. 33 | vec3 viewLightDirection = u_LightingParameters.xyz; 34 | float lightIntensity = u_LightingParameters.w; 35 | 36 | float materialAmbient = u_MaterialParameters.x; 37 | float materialDiffuse = u_MaterialParameters.y; 38 | float materialSpecular = u_MaterialParameters.z; 39 | float materialSpecularPower = u_MaterialParameters.w; 40 | 41 | // Normalize varying parameters, because they are linearly interpolated in the vertex shader. 42 | vec3 viewFragmentDirection = normalize(v_ViewPosition); 43 | vec3 viewNormal = normalize(v_ViewNormal); 44 | 45 | // Apply inverse SRGB gamma to the texture before making lighting calculations. 46 | // Flip the y-texture coordinate to address the texture from top-left. 47 | vec4 objectColor = texture2D(u_Texture, vec2(v_TexCoord.x, 1.0 - v_TexCoord.y)); 48 | objectColor.rgb = pow(objectColor.rgb, vec3(kInverseGamma)); 49 | 50 | // Ambient light is unaffected by the light intensity. 51 | float ambient = materialAmbient; 52 | 53 | // Approximate a hemisphere light (not a harsh directional light). 54 | float diffuse = lightIntensity * materialDiffuse * 55 | 0.5 * (dot(viewNormal, viewLightDirection) + 1.0); 56 | 57 | // Compute specular light. 58 | vec3 reflectedLightDirection = reflect(viewLightDirection, viewNormal); 59 | float specularStrength = max(0.0, dot(viewFragmentDirection, reflectedLightDirection)); 60 | float specular = lightIntensity * materialSpecular * 61 | pow(specularStrength, materialSpecularPower); 62 | 63 | // Apply SRGB gamma before writing the fragment color. 64 | gl_FragColor.a = objectColor.a; 65 | gl_FragColor.rgb = pow(objectColor.rgb * (ambient + diffuse) + specular, vec3(kGamma)); 66 | } 67 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/raw/object_vertex.shader: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | uniform mat4 u_ModelView; 17 | uniform mat4 u_ModelViewProjection; 18 | 19 | attribute vec4 a_Position; 20 | attribute vec3 a_Normal; 21 | attribute vec2 a_TexCoord; 22 | 23 | varying vec3 v_ViewPosition; 24 | varying vec3 v_ViewNormal; 25 | varying vec2 v_TexCoord; 26 | 27 | void main() { 28 | v_ViewPosition = (u_ModelView * a_Position).xyz; 29 | v_ViewNormal = (u_ModelView * vec4(a_Normal, 0.0)).xyz; 30 | v_TexCoord = a_TexCoord; 31 | gl_Position = u_ModelViewProjection * a_Position; 32 | } 33 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/raw/passthrough_fragment.shader: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | precision mediump float; 17 | varying vec4 v_Color; 18 | 19 | void main() { 20 | gl_FragColor = v_Color; 21 | } -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/raw/plane_fragment.shader: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | precision highp float; 17 | uniform sampler2D u_Texture; 18 | uniform vec4 u_dotColor; 19 | uniform vec4 u_lineColor; 20 | uniform vec4 u_gridControl; // dotThreshold, lineThreshold, lineFadeShrink, occlusionShrink 21 | varying vec3 v_TexCoordAlpha; 22 | 23 | void main() { 24 | vec4 control = texture2D(u_Texture, v_TexCoordAlpha.xy); 25 | float dotScale = v_TexCoordAlpha.z; 26 | float lineFade = max(0.0, u_gridControl.z * v_TexCoordAlpha.z - (u_gridControl.z - 1.0)); 27 | vec3 color = (control.r * dotScale > u_gridControl.x) ? u_dotColor.rgb 28 | : (control.g > u_gridControl.y) ? u_lineColor.rgb * lineFade 29 | : (u_lineColor.rgb * 0.25 * lineFade) ; 30 | gl_FragColor = vec4(color, v_TexCoordAlpha.z * u_gridControl.w); 31 | } 32 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/raw/plane_vertex.shader: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | uniform mat4 u_Model; 17 | uniform mat4 u_ModelViewProjection; 18 | uniform mat2 u_PlaneUvMatrix; 19 | 20 | attribute vec3 a_XZPositionAlpha; // (x, z, alpha) 21 | 22 | varying vec3 v_TexCoordAlpha; 23 | 24 | void main() { 25 | vec4 position = vec4(a_XZPositionAlpha.x, 0.0, a_XZPositionAlpha.y, 1.0); 26 | v_TexCoordAlpha = vec3(u_PlaneUvMatrix * (u_Model * position).xz, a_XZPositionAlpha.z); 27 | gl_Position = u_ModelViewProjection * position; 28 | } -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/raw/point_cloud_vertex.shader: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | uniform mat4 u_ModelViewProjection; 17 | uniform vec4 u_Color; 18 | uniform float u_PointSize; 19 | 20 | attribute vec4 a_Position; 21 | 22 | varying vec4 v_Color; 23 | 24 | void main() { 25 | v_Color = u_Color; 26 | gl_Position = u_ModelViewProjection * vec4(a_Position.xyz, 1.0); 27 | gl_PointSize = u_PointSize; 28 | } 29 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/raw/screenquad_fragment_oes.shader: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #extension GL_OES_EGL_image_external : require 16 | 17 | precision mediump float; 18 | varying vec2 v_TexCoord; 19 | uniform samplerExternalOES sTexture; 20 | 21 | 22 | void main() { 23 | gl_FragColor = texture2D(sTexture, v_TexCoord); 24 | } -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/raw/screenquad_vertex.shader: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Google Inc. All Rights Reserved. 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | attribute vec4 a_Position; 17 | attribute vec2 a_TexCoord; 18 | 19 | varying vec2 v_TexCoord; 20 | 21 | void main() { 22 | gl_Position = a_Position; 23 | v_TexCoord = a_TexCoord; 24 | } -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | HelloAR Java 19 | 20 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 22 | 29 | 30 | 31 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/arcore-preview.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jason0539/arcore-android-sdk/0398b60da0aa7fb23fb4e969e76e81cd4360cb8c/samples/java_arcore_hello_ar/arcore-preview.apk -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | buildscript { 3 | repositories { 4 | jcenter() 5 | } 6 | dependencies { 7 | classpath 'com.android.tools.build:gradle:2.3.3' 8 | 9 | // NOTE: Do not place your application dependencies here; they belong 10 | // in the individual module build.gradle files 11 | } 12 | } 13 | 14 | allprojects { 15 | repositories { 16 | mavenLocal() 17 | jcenter() 18 | maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/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 19 | 20 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jason0539/arcore-android-sdk/0398b60da0aa7fb23fb4e969e76e81cd4360cb8c/samples/java_arcore_hello_ar/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Oct 26 08:02:00 ART 2015 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-3.3-all.zip 7 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/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 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/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 | -------------------------------------------------------------------------------- /samples/java_arcore_hello_ar/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------