Fix build break
[platform/core/api/mediavision.git] / test / testsuites / common / visualizer / src / mv_util_visualizer_3d.cpp
1 /**
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd 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 #include <mv_private.h>
18 #include "mv_util_matrix.h"
19 #include "mv_util_visualizer_3d.h"
20
21 #define MAX_DEPTH_WIDTH 640
22 #define MAX_DEPTH_HEIGHT 480
23
24 static int win_w = 1920;
25 static int win_h = 1080;
26 static bool initialized = false;
27
28 static int create_window_surface()
29 {
30         int err = MEDIA_VISION_ERROR_NONE;
31         err = egl_init_with_platform_window_surface(2, 8, 0, 0, win_w, win_h);
32         if (MEDIA_VISION_ERROR_NONE != err) {
33                 LOGE("egl_init_with_platform_window_surface: %i", err);
34                 return MEDIA_VISION_ERROR_INTERNAL;
35         }
36         err = init_2d_renderer(win_w, win_h);
37         if (MEDIA_VISION_ERROR_NONE != err) {
38                 LOGE("init_2d_renderer: %i", err);
39                 return MEDIA_VISION_ERROR_INTERNAL;
40         }
41         init_cube((float) win_w / (float) win_h);
42         glClearColor(0.f, 0.f, 0.f, 1.0f);
43         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
44         glViewport(0, 0, win_w, win_h);
45
46         return MEDIA_VISION_ERROR_NONE;
47 }
48
49 //static int xpos = 100;
50 static float s_mdl_qtn0[4];
51 static float s_mdl_mtx[16];
52 static float s_mdl_qtn[4];
53
54 int mv_util_visualizer_3d(mv_source_h source, float *depth, int xpos, int ypos)
55 {
56         int texid;
57         int texw, texh;
58         int err = MEDIA_VISION_ERROR_NONE;
59         static int is_first_render3d = 1;
60         static mesh_obj_t depth_mesh;
61         static texture_2d_t captex = { 0 };
62
63         if (!initialized) {
64                 initialized = true;
65                 if (create_window_surface() != MEDIA_VISION_ERROR_NONE) {
66                         return MEDIA_VISION_ERROR_INTERNAL;
67                 }
68         }
69         err = load_texture(source, &texid, &texw, &texh);
70         if (MEDIA_VISION_ERROR_NONE != err) {
71                 LOGE("load_texture: %i", err);
72                 return MEDIA_VISION_ERROR_INTERNAL;
73         }
74         captex.texid = texid;
75         captex.width = texw;
76         captex.height = texh;
77         captex.format = pixfmt_fourcc('R', 'G', 'B', 'A');
78
79         float mtxGlobal[16];
80
81         quaternion_identity(s_mdl_qtn);
82         quaternion_to_matrix(s_mdl_mtx, s_mdl_qtn);
83
84         matrix_identity(mtxGlobal);
85         matrix_translate(mtxGlobal, 0, 0, -300.0f);
86
87         int height = texh;
88         int width = texw;
89
90         //xpos += 10;
91         //xpos = xpos % 1900;
92
93         float dx = xpos;
94         float dy = ypos;
95
96         float axis[3];
97         axis[0] = 2 * M_PI * dy / 1080;
98         axis[1] = 2 * M_PI * dx / 1920;
99         axis[2] = 0;
100
101         float rot = vec3_normalize(axis);
102
103         quaternion_copy(s_mdl_qtn0, s_mdl_qtn);
104
105         float dqtn[4];
106         quaternion_rotate(dqtn, rot, axis[0], axis[1], axis[2]);
107         quaternion_mult(s_mdl_qtn, dqtn, s_mdl_qtn);
108
109         quaternion_to_matrix(s_mdl_mtx, s_mdl_qtn);
110         matrix_mult(mtxGlobal, mtxGlobal, s_mdl_mtx);
111
112         quaternion_identity(s_mdl_qtn);
113         quaternion_to_matrix(s_mdl_mtx, s_mdl_qtn);
114
115         /* create mesh object */
116         if (is_first_render3d) {
117                 create_mesh(&depth_mesh, MAX_DEPTH_WIDTH - 1, MAX_DEPTH_HEIGHT - 1);
118                 is_first_render3d = 0;
119         }
120         float *vtx = depth_mesh.vtx_array;
121         float *uv = depth_mesh.uv_array;
122         int x, y;
123
124         /* create 3D vertex coordinate */
125         for (y = 0; y < height; y++) {
126                 for (x = 0; x < width; x++) {
127                         int idx = (y * width + x);
128                         float d = depth[idx];
129
130                         vtx[3 * idx + 0] = ((x / (float) height) * 2.0f - 1.0f) * 100.0f;
131                         vtx[3 * idx + 1] = ((y / (float) height) * 2.0f - 1.0f) * 100.0f;
132                         if (d > 0.0)
133                                 vtx[3 * idx + 2] = (d * 2.0f - 1.0f) * 100.0f - 50.0f;
134                         else
135                                 vtx[3 * idx + 2] = 10000000.0;
136
137                         uv[2 * idx + 0] = x / (float) width;
138                         uv[2 * idx + 1] = y / (float) height;
139                 }
140         }
141         float colb[] = { 1.0, 1.0, 1.0, 1.0 };
142         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
143         draw_point_arrays(mtxGlobal, vtx, uv, height * width, captex.texid, colb);
144
145         {
146                 /* (xyz)-AXIS */
147                 for (int i = -1; i <= 1; i++) {
148                         for (int j = -1; j <= 1; j++) {
149                                 float col_base[] = { 0.1, 0.5, 0.5, 0.5 };
150                                 float dx = 300.0;
151                                 float dy = 300.0;
152                                 float dz = 300.0;
153
154                                 {
155                                         float v0[3] = { -dx, i * dy, j * dz };
156                                         float v1[3] = { dx, i * dy, j * dz };
157                                         float col_red[] = { 1.0, 0.0, 0.0, 1.0 };
158                                         float *col = (i == 0 && j == 0) ? col_red : col_base;
159                                         draw_line(mtxGlobal, v0, v1, col);
160                                 }
161                                 {
162                                         float v0[3] = { i * dx, -dy, j * dz };
163                                         float v1[3] = { i * dx, dy, j * dz };
164                                         float col_green[] = { 0.0, 1.0, 0.0, 1.0 };
165                                         float *col = (i == 0 && j == 0) ? col_green : col_base;
166                                         draw_line(mtxGlobal, v0, v1, col);
167                                 }
168                                 {
169                                         float v0[3] = { i * dx, j * dy, -dz };
170                                         float v1[3] = { i * dx, j * dy, dz };
171                                         float col_blue[] = { 0.0, 0.0, 1.0, 1.0 };
172                                         float *col = (i == 0 && j == 0) ? col_blue : col_base;
173                                         draw_line(mtxGlobal, v0, v1, col);
174                                 }
175                         }
176                 }
177         }
178
179         glDeleteTextures(1, reinterpret_cast<GLuint *>(&texid));
180         err = egl_swap();
181         if (MEDIA_VISION_ERROR_NONE != err) {
182                 LOGE("egl_swap: %i", err);
183                 return MEDIA_VISION_ERROR_INTERNAL;
184         }
185
186         return MEDIA_VISION_ERROR_NONE;
187 }