aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/apps/arcore/src/main/cpp/hello_ar_application.h
blob: 2ebec7ed65045715f5459656de4b78e18208aa6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef C_ARCORE_HELLOE_AR_HELLO_AR_APPLICATION_H_
#define C_ARCORE_HELLOE_AR_HELLO_AR_APPLICATION_H_

#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <android/asset_manager.h>
#include <jni.h>
#include <memory>
#include <set>
#include <string>
#include <unordered_map>
#include <GrContext.h>
#include <gl/GrGLTypes.h>
#include <GrBackendSurface.h>
#include <SkSurface.h>
#include <Skottie.h>

#include "arcore_c_api.h"
#include "background_renderer.h"
#include "glm.h"
#include "plane_renderer.h"
#include "point_cloud_renderer.h"
#include "util.h"
#include "pending_anchor.h"

namespace hello_ar {

// HelloArApplication handles all application logics.
    class HelloArApplication {
    public:
        // Constructor and deconstructor.
        HelloArApplication() = default;

        HelloArApplication(AAssetManager *asset_manager);

        ~HelloArApplication();

        SkMatrix SkiaRenderer(const glm::mat4 &proj, const glm::mat4 &view, const glm::mat4 &model);

        // OnPause is called on the UI thread from the Activity's onPause method.
        void OnPause();

        // OnResume is called on the UI thread from the Activity's onResume method.
        void OnResume(void *env, void *context, void *activity);

        // OnSurfaceCreated is called on the OpenGL thread when GLSurfaceView
        // is created.
        void OnSurfaceCreated();

        // OnDisplayGeometryChanged is called on the OpenGL thread when the
        // render surface size or display rotation changes.
        //
        // @param display_rotation: current display rotation.
        // @param width: width of the changed surface view.
        // @param height: height of the changed surface view.
        void OnDisplayGeometryChanged(int display_rotation, int width, int height);

        void OnObjectRotationChanged(int rotation);

        void OnAction(float value);

        // OnDrawFrame is called on the OpenGL thread to render the next frame.
        void OnDrawFrame();

        bool OnTouchedFirst(float x, float y, int drawMode);

        void OnTouchTranslate(float x, float y);

        void OnEditTouched(float x, float y);

        void OnTouchedFinal(int type);

        void RemoveAnchor(ArAnchor* anchor);

        void AddAnchor(ArAnchor* anchor, ArPlane* containingPlane);

        void UpdateMatrixMaps(ArAnchor* anchorKey, glm::mat4 aaMat, glm::mat4 caMat, glm::mat4 snapMat);

        void SetModelMatrices(glm::mat4& aaMat, glm::mat4& caMat, glm::mat4& snapMat, const glm::mat4& planeModel);

        void SetCameraAlignedMatrix(glm::mat4& caMat, glm::vec3 hitPos, glm::mat4& planeModel, const glm::mat4& initRotation);

        // Returns true if any planes have been detected.  Used for hiding the
        // "searching for planes" snackbar.
        bool HasDetectedPlanes() const { return plane_count_ > 0; }

        glm::mat4
        ComputeCameraAlignedMatrix(ArPlane *arPlane, glm::mat4 planeModel, glm::mat4 initRotation,
                                   glm::vec4 anchorPos,
                                   glm::vec3 cameraPos, glm::vec3 hitPos,
                                   float cameraDisplayOutRaw[]);

    private:
        ArSession *ar_session_ = nullptr;
        ArFrame *ar_frame_ = nullptr;

        PendingAnchor* pendingAnchor = nullptr;

        //SKIA VARS
        sk_sp<GrContext> grContext;
        sk_sp<skottie::Animation> fAnim;
        SkScalar fAnimT = 0;

        bool install_requested_ = false;
        int width_ = 1;
        int height_ = 1;
        int display_rotation_ = 0;

        int currentObjectRotation = 0;
        float currentValue = 0;

        std::vector<glm::vec3> begins;
        std::vector<glm::vec3> ends;

        AAssetManager *const asset_manager_;

        // The anchors at which we are drawing android models
        std::vector<ArAnchor *> tracked_obj_set_;

        // Stores the randomly-selected color each plane is drawn with
        std::unordered_map<ArPlane *, glm::vec3> plane_color_map_;

        std::unordered_map<ArAnchor *, SkMatrix44> anchor_skmat4_axis_aligned_map_;
        std::unordered_map<ArAnchor *, SkMatrix44> anchor_skmat4_camera_aligned_map_;
        std::unordered_map<ArAnchor *, SkMatrix44> anchor_skmat4_snap_aligned_map_;

        std::unordered_map<ArPlane *, std::vector<ArAnchor*>> plane_anchors_map_;
        std::unordered_map<ArAnchor *, ArPlane*> anchor_plane_map_;

        // The first plane is always rendered in white, if this is true then a plane
        // at some point has been found.
        bool first_plane_has_been_found_ = false;

        PointCloudRenderer point_cloud_renderer_;
        BackgroundRenderer background_renderer_;
        PlaneRenderer plane_renderer_;

        int32_t plane_count_ = 0;
    };
}  // namespace hello_ar

#endif  // C_ARCORE_HELLOE_AR_HELLO_AR_APPLICATION_H_