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