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 #ifndef C_ARCORE_HELLOE_AR_HELLO_AR_APPLICATION_H_ 18 #define C_ARCORE_HELLOE_AR_HELLO_AR_APPLICATION_H_ 19 20 #include "include/core/SkSurface.h" 21 #include "include/gpu/GrBackendSurface.h" 22 #include "include/gpu/GrContext.h" 23 #include "include/gpu/gl/GrGLTypes.h" 24 #include "modules/skottie/include/Skottie.h" 25 #include <GLES2/gl2.h> 26 #include <GLES2/gl2ext.h> 27 #include <android/asset_manager.h> 28 #include <jni.h> 29 #include <memory> 30 #include <set> 31 #include <string> 32 #include <unordered_map> 33 34 #include "arcore_c_api.h" 35 #include "platform_tools/android/apps/arcore/src/main/cpp/background_renderer.h" 36 #include "platform_tools/android/apps/arcore/src/main/cpp/glm.h" 37 #include "platform_tools/android/apps/arcore/src/main/cpp/pending_anchor.h" 38 #include "platform_tools/android/apps/arcore/src/main/cpp/plane_renderer.h" 39 #include "platform_tools/android/apps/arcore/src/main/cpp/point_cloud_renderer.h" 40 #include "platform_tools/android/apps/arcore/src/main/cpp/util.h" 41 42 namespace hello_ar { 43 44 // HelloArApplication handles all application logics. 45 class HelloArApplication { 46 public: 47 // Constructor and deconstructor. 48 HelloArApplication() = default; 49 50 HelloArApplication(AAssetManager *asset_manager); 51 52 ~HelloArApplication(); 53 54 SkMatrix SkiaRenderer(const glm::mat4 &proj, const glm::mat4 &view, const glm::mat4 &model); 55 56 // OnPause is called on the UI thread from the Activity's onPause method. 57 void OnPause(); 58 59 // OnResume is called on the UI thread from the Activity's onResume method. 60 void OnResume(void *env, void *context, void *activity); 61 62 // OnSurfaceCreated is called on the OpenGL thread when GLSurfaceView 63 // is created. 64 void OnSurfaceCreated(); 65 66 // OnDisplayGeometryChanged is called on the OpenGL thread when the 67 // render surface size or display rotation changes. 68 // 69 // @param display_rotation: current display rotation. 70 // @param width: width of the changed surface view. 71 // @param height: height of the changed surface view. 72 void OnDisplayGeometryChanged(int display_rotation, int width, int height); 73 74 void OnObjectRotationChanged(int rotation); 75 76 void OnAction(float value); 77 78 // OnDrawFrame is called on the OpenGL thread to render the next frame. 79 void OnDrawFrame(); 80 81 bool OnTouchedFirst(float x, float y, int drawMode); 82 83 void OnTouchTranslate(float x, float y); 84 85 void OnEditTouched(float x, float y); 86 87 void OnTouchedFinal(int type); 88 89 void RemoveAnchor(ArAnchor* anchor); 90 91 void AddAnchor(ArAnchor* anchor, ArPlane* containingPlane); 92 93 void UpdateMatrixMaps(ArAnchor* anchorKey, glm::mat4 aaMat, glm::mat4 caMat, glm::mat4 snapMat); 94 95 void SetModelMatrices(glm::mat4& aaMat, glm::mat4& caMat, glm::mat4& snapMat, const glm::mat4& planeModel); 96 97 void SetCameraAlignedMatrix(glm::mat4& caMat, glm::vec3 hitPos, glm::mat4& planeModel, const glm::mat4& initRotation); 98 99 // Returns true if any planes have been detected. Used for hiding the 100 // "searching for planes" snackbar. HasDetectedPlanes()101 bool HasDetectedPlanes() const { return plane_count_ > 0; } 102 103 glm::mat4 104 ComputeCameraAlignedMatrix(ArPlane *arPlane, glm::mat4 planeModel, glm::mat4 initRotation, 105 glm::vec4 anchorPos, 106 glm::vec3 cameraPos, glm::vec3 hitPos, 107 float cameraDisplayOutRaw[]); 108 109 private: 110 ArSession *ar_session_ = nullptr; 111 ArFrame *ar_frame_ = nullptr; 112 113 PendingAnchor* pendingAnchor = nullptr; 114 115 //SKIA VARS 116 sk_sp<GrContext> grContext; 117 sk_sp<skottie::Animation> fAnim; 118 SkScalar fAnimT = 0; 119 120 bool install_requested_ = false; 121 int width_ = 1; 122 int height_ = 1; 123 int display_rotation_ = 0; 124 125 int currentObjectRotation = 0; 126 float currentValue = 0; 127 128 std::vector<glm::vec3> begins; 129 std::vector<glm::vec3> ends; 130 131 AAssetManager *const asset_manager_; 132 133 // The anchors at which we are drawing android models 134 std::vector<ArAnchor *> tracked_obj_set_; 135 136 // Stores the randomly-selected color each plane is drawn with 137 std::unordered_map<ArPlane *, glm::vec3> plane_color_map_; 138 139 std::unordered_map<ArAnchor *, SkMatrix44> anchor_skmat4_axis_aligned_map_; 140 std::unordered_map<ArAnchor *, SkMatrix44> anchor_skmat4_camera_aligned_map_; 141 std::unordered_map<ArAnchor *, SkMatrix44> anchor_skmat4_snap_aligned_map_; 142 143 std::unordered_map<ArPlane *, std::vector<ArAnchor*>> plane_anchors_map_; 144 std::unordered_map<ArAnchor *, ArPlane*> anchor_plane_map_; 145 146 // The first plane is always rendered in white, if this is true then a plane 147 // at some point has been found. 148 bool first_plane_has_been_found_ = false; 149 150 PointCloudRenderer point_cloud_renderer_; 151 BackgroundRenderer background_renderer_; 152 PlaneRenderer plane_renderer_; 153 154 int32_t plane_count_ = 0; 155 }; 156 } // namespace hello_ar 157 158 #endif // C_ARCORE_HELLOE_AR_HELLO_AR_APPLICATION_H_ 159