2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <mv_private.h>
18 #include "mv_util_matrix.h"
19 #include "mv_util_visualizer_3d.h"
21 #define MAX_DEPTH_WIDTH 640
22 #define MAX_DEPTH_HEIGHT 480
24 static int win_w = 1920;
25 static int win_h = 1080;
26 static bool initialized = false;
28 static int create_window_surface()
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;
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;
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);
46 return MEDIA_VISION_ERROR_NONE;
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];
54 int mv_util_visualizer_3d(mv_source_h source, float *depth, int xpos, int ypos)
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 };
65 if (create_window_surface() != MEDIA_VISION_ERROR_NONE) {
66 return MEDIA_VISION_ERROR_INTERNAL;
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;
77 captex.format = pixfmt_fourcc('R', 'G', 'B', 'A');
81 quaternion_identity(s_mdl_qtn);
82 quaternion_to_matrix(s_mdl_mtx, s_mdl_qtn);
84 matrix_identity(mtxGlobal);
85 matrix_translate(mtxGlobal, 0, 0, -300.0f);
97 axis[0] = 2 * M_PI * dy / 1080;
98 axis[1] = 2 * M_PI * dx / 1920;
101 float rot = vec3_normalize(axis);
103 quaternion_copy(s_mdl_qtn0, s_mdl_qtn);
106 quaternion_rotate(dqtn, rot, axis[0], axis[1], axis[2]);
107 quaternion_mult(s_mdl_qtn, dqtn, s_mdl_qtn);
109 quaternion_to_matrix(s_mdl_mtx, s_mdl_qtn);
110 matrix_mult(mtxGlobal, mtxGlobal, s_mdl_mtx);
112 quaternion_identity(s_mdl_qtn);
113 quaternion_to_matrix(s_mdl_mtx, s_mdl_qtn);
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;
120 float *vtx = depth_mesh.vtx_array;
121 float *uv = depth_mesh.uv_array;
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];
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;
133 vtx[3 * idx + 2] = (d * 2.0f - 1.0f) * 100.0f - 50.0f;
135 vtx[3 * idx + 2] = 10000000.0;
137 uv[2 * idx + 0] = x / (float) width;
138 uv[2 * idx + 1] = y / (float) height;
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);
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 };
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);
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);
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);
179 glDeleteTextures(1, reinterpret_cast<GLuint *>(&texid));
181 if (MEDIA_VISION_ERROR_NONE != err) {
182 LOGE("egl_swap: %i", err);
183 return MEDIA_VISION_ERROR_INTERNAL;
186 return MEDIA_VISION_ERROR_NONE;