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_UTIL_H_ 18 #define C_ARCORE_HELLOE_AR_UTIL_H_ 19 20 #include <GLES2/gl2.h> 21 #include <GLES2/gl2ext.h> 22 #include <android/asset_manager.h> 23 #include <android/log.h> 24 #include <errno.h> 25 #include <jni.h> 26 #include <cstdint> 27 #include <cstdlib> 28 #include <vector> 29 #include <SkMatrix44.h> 30 31 #include "arcore_c_api.h" 32 #include "glm.h" 33 34 #ifndef LOGI 35 #define LOGI(...) \ 36 __android_log_print(ANDROID_LOG_INFO, "hello_ar_example_c", __VA_ARGS__) 37 #endif // LOGI 38 39 #ifndef LOGE 40 #define LOGE(...) \ 41 __android_log_print(ANDROID_LOG_ERROR, "hello_ar_example_c", __VA_ARGS__) 42 #endif // LOGE 43 44 #ifndef CHECK 45 #define CHECK(condition) \ 46 if (!(condition)) { \ 47 LOGE("*** CHECK FAILED at %s:%d: %s", __FILE__, __LINE__, #condition); \ 48 abort(); \ 49 } 50 #endif // CHECK 51 52 namespace hello_ar { 53 // Utilities 54 namespace util { 55 56 // Provides a scoped allocated instance of Anchor. 57 // Can be treated as an ArAnchor*. 58 class ScopedArPose { 59 public: 60 explicit ScopedArPose(const ArSession *session) { 61 ArPose_create(session, nullptr, &pose_); 62 } 63 64 ~ScopedArPose() { ArPose_destroy(pose_); } 65 66 ArPose *GetArPose() { return pose_; } 67 68 // Delete copy constructors. 69 ScopedArPose(const ScopedArPose &) = delete; 70 71 void operator=(const ScopedArPose &) = delete; 72 73 private: 74 ArPose *pose_; 75 }; 76 77 /* GL Utils */ 78 // Check GL error, and abort if an error is encountered. 79 // 80 // @param operation, the name of the GL function call. 81 void CheckGlError(const char *operation); 82 83 // Create a shader program ID. 84 // 85 // @param vertex_source, the vertex shader source. 86 // @param fragment_source, the fragment shader source. 87 // @return 88 GLuint CreateProgram(const char *vertex_source, const char *fragment_source); 89 90 // Load png file from assets folder and then assign it to the OpenGL target. 91 // This method must be called from the renderer thread since it will result in 92 // OpenGL calls to assign the image to the texture target. 93 // 94 // @param target, openGL texture target to load the image into. 95 // @param path, path to the file, relative to the assets folder. 96 // @return true if png is loaded correctly, otherwise false. 97 bool LoadPngFromAssetManager(int target, const std::string &path); 98 99 100 /* ARCore utils */ 101 void GetTransformMatrixFromPose(ArSession *ar_session, const ArPose *ar_pose, glm::mat4 *out_model_mat); 102 103 // Get the plane's normal from center pose. 104 glm::vec3 GetPlaneNormal(const ArSession *ar_session, const ArPose &plane_pose); 105 106 // Calculate the normal distance to plane from cameraPose, the given planePose 107 // should have y axis parallel to plane's normal, for example plane's center 108 // pose or hit test pose. 109 float CalculateDistanceToPlane(const ArSession *ar_session, const ArPose &plane_pose, const ArPose &camera_pose); 110 111 // Outputs the camera rotation using display orientation 112 glm::mat4 GetCameraRotationMatrix(float cameraOutRaw[]); 113 114 // Computes camera position and orientation (using GetCameraRotationMatrix) 115 void GetCameraInfo(ArSession* arSession, ArFrame* arFrame, glm::vec3& cameraPos, glm::mat4& cameraRotation); 116 117 /* Matrix conversion */ 118 SkMatrix44 GlmMatToSkMat(const glm::mat4 m); 119 glm::mat4 SkMatToGlmMat(const SkMatrix44 m); 120 121 /* Logging utils */ 122 //Row major output 123 void Log4x4Matrix(float raw_matrix[16]); 124 125 //Column major output 126 void LogGlmMat(glm::mat4 m, char *type); 127 void LogSkMat44(SkMatrix44 m, char *type); 128 void LogSkMat(SkMatrix m, char *type); 129 void LogOrientation(float rotationDirection, float angleRad, char *type); 130 131 /* Vector ops */ 132 float Dot(glm::vec3 u, glm::vec3 v); 133 float Magnitude(glm::vec3 u); 134 float AngleRad(glm::vec3 u, glm::vec3 v); 135 glm::vec3 ProjectOntoPlane(glm::vec3 in, glm::vec3 normal); 136 } // namespace util 137 } // namespace hello_ar 138 139 #endif // C_ARCORE_HELLOE_AR_UTIL_H_ 140