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_util_visualizer_3d.h"
19 #define MAX_DEPTH_WIDTH 640
20 #define MAX_DEPTH_HEIGHT 480
22 static int win_w = 1920;
23 static int win_h = 1080;
24 static bool initialized = false;
26 static int create_window_surface()
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;
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;
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);
44 return MEDIA_VISION_ERROR_NONE;
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];
52 int mv_util_visualizer_3d(mv_source_h source, float *depth, int xpos, int ypos)
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 };
63 if (create_window_surface() != MEDIA_VISION_ERROR_NONE) {
64 return MEDIA_VISION_ERROR_INTERNAL;
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;
75 captex.format = pixfmt_fourcc('R', 'G', 'B', 'A');
79 quaternion_identity(s_mdl_qtn);
80 quaternion_to_matrix(s_mdl_mtx, s_mdl_qtn);
82 matrix_identity(mtxGlobal);
83 matrix_translate(mtxGlobal, 0, 0, -300.0f);
95 axis[0] = 2 * M_PI * dy / 1080;
96 axis[1] = 2 * M_PI * dx / 1920;
99 float rot = vec3_normalize(axis);
101 quaternion_copy(s_mdl_qtn0, s_mdl_qtn);
104 quaternion_rotate(dqtn, rot, axis[0], axis[1], axis[2]);
105 quaternion_mult(s_mdl_qtn, dqtn, s_mdl_qtn);
107 quaternion_to_matrix(s_mdl_mtx, s_mdl_qtn);
108 matrix_mult(mtxGlobal, mtxGlobal, s_mdl_mtx);
110 quaternion_identity(s_mdl_qtn);
111 quaternion_to_matrix(s_mdl_mtx, s_mdl_qtn);
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;
118 float *vtx = depth_mesh.vtx_array;
119 float *uv = depth_mesh.uv_array;
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];
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;
131 vtx[3 * idx + 2] = (d * 2.0f - 1.0f) * 100.0f - 50.0f;
133 vtx[3 * idx + 2] = 10000000.0;
135 uv[2 * idx + 0] = x / (float) width;
136 uv[2 * idx + 1] = y / (float) height;
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);
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 };
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);
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);
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);
178 if (MEDIA_VISION_ERROR_NONE != err) {
179 LOGE("egl_swap: %i", err);
180 return MEDIA_VISION_ERROR_INTERNAL;
183 return MEDIA_VISION_ERROR_NONE;