forked from StereoKit/StereoKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_bvh.cpp
More file actions
213 lines (160 loc) · 6.58 KB
/
Copy pathdemo_bvh.cpp
File metadata and controls
213 lines (160 loc) · 6.58 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "demo_bvh.h"
#include <cstdio> // For sprintf()
#include <stereokit.h>
#include <stereokit_ui.h>
using namespace sk;
#include <vector>
using namespace std;
///////////////////////////////////////////
model_t model_to_intersect = {};
float model_scale = 1.0f;
matrix model_scale_matrix;
bounds_t model_to_intersection_bounds;
pose_t model_pose = { vec3{0,0,-0.5f}, quat_identity };
mesh_t mesh_from, mesh_to;
pose_t from_pose, to_pose;
ray_t world_ray, model_ray;
ray_t intersection;
material_t ray_material;
mesh_t isec_sphere;
float intersect_time = 0;
bool32_t use_bvh = true;
bool have_intersection;
cull_ cull_mode;
///////////////////////////////////////////
void demo_bvh_load_model(const char* filename) {
model_release(model_to_intersect);
model_to_intersect = model_create_file(filename);
log_infof("%d nodes", model_node_count(model_to_intersect));
for (int i = 0; i < model_node_count(model_to_intersect); i++)
{
mesh_t mesh = model_node_get_mesh(model_to_intersect, i);
if (mesh != nullptr)
log_infof("mesh[%d] %d vertices, %d triangles", i, mesh_get_vert_count(mesh), mesh_get_ind_count(mesh)/3);
}
// Scale large models to unit size
bounds_t b = model_get_bounds(model_to_intersect);
float m = vec3_magnitude (b.dimensions);
if (m > 1.0f)
model_scale = 1.0f / vec3_magnitude(b.dimensions);
model_scale_matrix = matrix_s(vec3{ model_scale, model_scale, model_scale });
model_to_intersection_bounds = model_get_bounds(model_to_intersect);
model_to_intersection_bounds.center *= model_scale;
model_to_intersection_bounds.dimensions *= model_scale;
// Initial ray endpoints
from_pose = {(b.center - 0.7f*b.dimensions)*model_scale+model_pose.position, quat_identity};
to_pose = {(b.center + 0.7f*b.dimensions)*model_scale+model_pose.position, quat_identity};
}
///////////////////////////////////////////
void demo_bvh_init() {
mesh_from = mesh_gen_sphere(0.04f, 16);
mesh_to = mesh_gen_cone (0.04f, 0.1f, vec3{0.0f,0.0f,-1.0f}, 16);
isec_sphere = mesh_gen_sphere(0.01f, 8);
ray_material = material_copy(material_find(default_id_material));
material_set_color(ray_material, "color", color128{1.0f,0.5f,0.0f,1.0f});
demo_bvh_load_model("Radio.glb");
cull_mode = cull_back;
}
///////////////////////////////////////////
void demo_bvh_update() {
static pose_t window_pose = pose_t{ {0.6f,0,-0.25f}, quat_lookat({0.25f,0.0f,0.0f}, {0,0,0}) };
ui_window_begin("Options", &window_pose, vec2{ 24 }*cm2m);
if (ui_button("Load Model")) {
file_filter_t picker_filter[] = { {".glb"}, {".gltf"} };
platform_file_picker(picker_mode_open, nullptr, [](void*, bool32_t confirm, const char* filename) {
if (confirm)
demo_bvh_load_model(filename);
}, picker_filter, 2);
}
ui_toggle("Use BVH", use_bvh);
bool32_t cull_mode_back = cull_mode == cull_back;
bool32_t cull_mode_front = cull_mode == cull_front;
bool32_t cull_mode_none = cull_mode == cull_none;
ui_text("Cull: "); ui_sameline();
if (ui_toggle("Back", cull_mode_back))
cull_mode = cull_back;
ui_sameline();
if (ui_toggle("Front", cull_mode_front))
cull_mode = cull_front;
ui_sameline();
if (ui_toggle("None", cull_mode_none))
cull_mode = cull_none;
float time_ms = 1000*(intersect_time);
if (time_ms < 0.1f)
ui_text("< 0.1ms");
else
{
char s[64];
snprintf(s, 64, "%.3f ms", time_ms);
ui_text(s);
}
if (ui_button("Reset pose"))
model_pose = pose_t{vec3{0,0,-0.5f}, quat_identity};
ui_window_end();
// XXX add toggle model scale?
// XXX could add switch to disable isec testing (or one-shot), for large models
// Model being intersected
ui_handle_begin("model", model_pose, nullptr, model_to_intersection_bounds, false);
model_draw (model_to_intersect, model_scale_matrix);
ui_handle_end ();
// World-space ray defined by two endpoint handles
ui_handle_begin("from", from_pose, nullptr, mesh_get_bounds(mesh_from), false, ui_move_pos_only);
mesh_draw (mesh_from, ray_material, matrix_identity);
ui_handle_end ();
to_pose.orientation = quat_lookat(from_pose.position, to_pose.position);
ui_handle_begin("to", to_pose, nullptr, mesh_get_bounds(mesh_to), false, ui_move_pos_only);
mesh_draw (mesh_to, ray_material, matrix_identity);
ui_handle_end ();
// Compute model-space ray
world_ray = {from_pose.position, to_pose.position-from_pose.position};
matrix model_to_world_matrix = model_scale_matrix * pose_matrix(model_pose);
matrix world_to_model_matrix = matrix_invert(model_to_world_matrix);
model_ray = matrix_transform_ray(world_to_model_matrix, world_ray);
// Intersection test
const double t0 = time_total_raw();
mesh_t isec_mesh = nullptr;
matrix isec_matrix = matrix_identity;
uint32_t isec_start_inds = 0;
have_intersection = use_bvh
? model_ray_intersect_bvh_detailed(model_to_intersect, model_ray, cull_mode, &intersection, &isec_mesh, &isec_matrix, &isec_start_inds)
: model_ray_intersect (model_to_intersect, model_ray, cull_mode, &intersection);
intersect_time = (float)(time_total_raw() - t0);
// Intersection result
if (have_intersection)
{
// Green: intersection found, show point, including normal
line_add(from_pose.position, to_pose.position, color32{0,255,0,255}, color32{0,255,0,255}, 0.005f);
vec3 world_isec_point = matrix_transform_pt(model_to_world_matrix, intersection.pos);
// Ignore scale for normal
vec3 world_isec_normal = matrix_transform_dir(pose_matrix(model_pose), intersection.dir);
mesh_draw(isec_sphere, ray_material, matrix_t(world_isec_point));
line_add(world_isec_point, world_isec_point+world_isec_normal*0.1f,
color32{0,0,255,255}, color32{0,0,255,255}, 0.005f);
if (isec_mesh != nullptr)
{
//log_infof("%d\n", isec_start_inds);
// Highlight intersected triangle
vert_t v1, v2, v3;
mesh_get_triangle(isec_mesh, isec_start_inds, &v1, &v2, &v3);
vec3 p = matrix_transform_pt(model_to_world_matrix,
matrix_transform_pt(isec_matrix, v1.pos));
vec3 q = matrix_transform_pt(model_to_world_matrix,
matrix_transform_pt(isec_matrix, v2.pos));
vec3 r = matrix_transform_pt(model_to_world_matrix,
matrix_transform_pt(isec_matrix, v3.pos));
line_add(p, q, color32{0,255,0,255}, color32{0,255,0,255}, 0.005f);
line_add(q, r, color32{0,255,0,255}, color32{0,255,0,255}, 0.005f);
line_add(r, p, color32{0,255,0,255}, color32{0,255,0,255}, 0.005f);
}
}
else
{
// Red: no intersection
line_add(from_pose.position, to_pose.position, color32{255,0,0,255}, color32{255,0,0,255}, 0.005f);
}
}
///////////////////////////////////////////
void demo_bvh_shutdown() {
// Release everything
model_release(model_to_intersect);
}