From 5b69d7fc23e6d273efdb2c751d593c2ab58f3a03 Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Mon, 30 Dec 2019 14:50:01 +1100 Subject: [PATCH] Implemented bounding box visualization in editor --- src/game/bounding_volumes.c | 43 +++++++++++++++++++++++++++++++++++++ src/game/bounding_volumes.h | 4 ++-- src/game/editor.c | 22 ++++--------------- todo.txt | 4 ++-- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/game/bounding_volumes.c b/src/game/bounding_volumes.c index 16b9388..7742b4d 100755 --- a/src/game/bounding_volumes.c +++ b/src/game/bounding_volumes.c @@ -203,3 +203,46 @@ void bv_bounding_box_vertices_get(struct Bounding_Box* bounding_box, vec3 out_ve vec3_fill(&out_vertices[6], bounding_box->max.x, bounding_box->min.y, bounding_box->max.z); vec3_fill(&out_vertices[7], bounding_box->max.x, bounding_box->max.y, bounding_box->min.z); } + +void bv_bounding_box_vertices_get_line_visualization(struct Bounding_Box* bounding_box, vec3 out_vertices[24]) +{ + // Back + vec3_fill(&out_vertices[0], bounding_box->min.x, bounding_box->min.y, bounding_box->min.z); + vec3_fill(&out_vertices[1], bounding_box->min.x, bounding_box->max.y, bounding_box->min.z); + + vec3_fill(&out_vertices[2], bounding_box->min.x, bounding_box->min.y, bounding_box->min.z); + vec3_fill(&out_vertices[3], bounding_box->max.x, bounding_box->min.y, bounding_box->min.z); + + vec3_fill(&out_vertices[4], bounding_box->min.x, bounding_box->max.y, bounding_box->min.z); + vec3_fill(&out_vertices[5], bounding_box->max.x, bounding_box->max.y, bounding_box->min.z); + + vec3_fill(&out_vertices[6], bounding_box->max.x, bounding_box->max.y, bounding_box->min.z); + vec3_fill(&out_vertices[7], bounding_box->max.x, bounding_box->min.y, bounding_box->min.z); + + // Front + vec3_fill(&out_vertices[8], bounding_box->min.x, bounding_box->min.y, bounding_box->max.z); + vec3_fill(&out_vertices[9], bounding_box->min.x, bounding_box->max.y, bounding_box->max.z); + + vec3_fill(&out_vertices[10], bounding_box->min.x, bounding_box->min.y, bounding_box->max.z); + vec3_fill(&out_vertices[11], bounding_box->max.x, bounding_box->min.y, bounding_box->max.z); + + vec3_fill(&out_vertices[12], bounding_box->min.x, bounding_box->max.y, bounding_box->max.z); + vec3_fill(&out_vertices[13], bounding_box->max.x, bounding_box->max.y, bounding_box->max.z); + + vec3_fill(&out_vertices[14], bounding_box->max.x, bounding_box->max.y, bounding_box->max.z); + vec3_fill(&out_vertices[15], bounding_box->max.x, bounding_box->min.y, bounding_box->max.z); + + // Left + vec3_fill(&out_vertices[16], bounding_box->min.x, bounding_box->max.y, bounding_box->min.z); + vec3_fill(&out_vertices[17], bounding_box->min.x, bounding_box->max.y, bounding_box->max.z); + + vec3_fill(&out_vertices[18], bounding_box->min.x, bounding_box->min.y, bounding_box->min.z); + vec3_fill(&out_vertices[19], bounding_box->min.x, bounding_box->min.y, bounding_box->max.z); + + // Right + vec3_fill(&out_vertices[20], bounding_box->max.x, bounding_box->min.y, bounding_box->min.z); + vec3_fill(&out_vertices[21], bounding_box->max.x, bounding_box->min.y, bounding_box->max.z); + + vec3_fill(&out_vertices[22], bounding_box->max.x, bounding_box->max.y, bounding_box->min.z); + vec3_fill(&out_vertices[23], bounding_box->max.x, bounding_box->max.y, bounding_box->max.z); +} diff --git a/src/game/bounding_volumes.h b/src/game/bounding_volumes.h index d3857e8..3858507 100755 --- a/src/game/bounding_volumes.h +++ b/src/game/bounding_volumes.h @@ -53,9 +53,9 @@ int bv_intersect_frustum_box(vec4* frustum, struct Bounding_Box* box, vec3* bo int bv_intersect_frustum_box_(vec4* frustum, struct Bounding_Box* box); int bv_intersect_frustum_sphere(vec4* frustum, struct Bounding_Sphere* sphere, vec3* sphere_abs_pos, vec3* sphere_abs_scale); bool bv_intersect_frustum_point(vec4* frustum, const vec3* point); -//bool bv_intersect_sphere_ray(struct Bounding_Sphere* sphere, vec3* sphere_abs_position, struct Ray* ray); -int bv_intersect_sphere_ray(struct Bounding_Sphere* sphere, vec3* sphere_abs_position, vec3* sphere_abs_scale, struct Ray* ray); +int bv_intersect_sphere_ray(struct Bounding_Sphere* sphere, vec3* sphere_abs_position, vec3* sphere_abs_scale, struct Ray* ray); float bv_distance_ray_plane(struct Ray* ray, Plane* plane); void bv_bounding_box_vertices_get(struct Bounding_Box* box, vec3 out_vertices[8]); +void bv_bounding_box_vertices_get_line_visualization(struct Bounding_Box* bounding_box, vec3 out_vertices[24]); #endif diff --git a/src/game/editor.c b/src/game/editor.c index c27759d..e10c7da 100755 --- a/src/game/editor.c +++ b/src/game/editor.c @@ -249,24 +249,10 @@ void editor_render(struct Editor* editor, struct Camera * active_camera) } /* Draw bounding box for selected entity */ - vec3 points[8]; - bv_bounding_box_vertices_get(&editor->selected_entity->transform.bounding_box, points); - - //struct Geometry* geom = geom_get(((struct Static_Mesh*)editor->selected_entity)->model.geometry_index); - //bv_bounding_box_vertices_get(&geom->bounding_box, points); - vec3 abs_position = { 0.f, 0.f, 0.f }; - transform_get_absolute_position(editor->selected_entity, &abs_position); - vec3 scale = { 0.f, 0.f, 0.f }; - scale.x = editor->selected_entity->transform.bounding_box.max.x - editor->selected_entity->transform.bounding_box.min.x; - scale.y = editor->selected_entity->transform.bounding_box.max.y - editor->selected_entity->transform.bounding_box.min.y; - scale.z = editor->selected_entity->transform.bounding_box.max.z - editor->selected_entity->transform.bounding_box.min.z; - vec3_scale(&scale, &scale, 0.5f); - //vec3_sub(&abs_position, &abs_position, &scale); - im_box(fabsf(scale.x), fabsf(scale.y), fabsf(scale.z), abs_position, (quat) { 0.f, 0.f, 0.f, 1.f }, editor->cursor_entity_color, GDM_TRIANGLES, 3); - im_begin((vec3) { 0.f, 0.f, 0.f }, (quat) { 0.f, 0.f, 0.f, 1.f }, (vec3) { 1.f, 1.f, 1.f }, (vec4) { 1.f, 0.f, 1.f, 1.2f }, GDM_LINE_LOOP, 3); - for(int i = 0; i < 8; i++) - im_pos(points[i].x, points[i].y, points[i].z); - im_end(); + static vec3 vertices[24]; + bv_bounding_box_vertices_get_line_visualization(&editor->selected_entity->transform.bounding_box, vertices); + for(int i = 0; i <= 22; i += 2) + im_line(vertices[i], vertices[i + 1], (vec3) { 0.f, 0.f, 0.f }, (quat) { 0.f, 0.f, 0.f, 1.f }, editor->cursor_entity_color, GDM_LINES); /* Draw selected entity with projected transformation applied */ if(editor->draw_cursor_entity) diff --git a/todo.txt b/todo.txt index 506749f..7b1ffa8 100644 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,4 @@ Todo: - - Implement bounding box visualization - Fix rotation gizmo for scaled meshes - Implement ray-bounding box picking and determine whether that is enough for our needs or do we need to implement OBB - Command to create a placeholder entity of a particular type in a file @@ -400,4 +399,5 @@ Done: * Add uv tiling parameter to materials that can be serialized along with entities * Implement resetting complete transform or just rotation, translation or scale for selected entity - * Save transformation information when saving entity archetypes \ No newline at end of file + * Save transformation information when saving entity archetypes + * Implement bounding box visualization \ No newline at end of file