Fixed transformed bounding box calculation

dev
Shariq Shah 6 years ago
parent 4238c2b16f
commit a9c6acf721
  1. 18
      assets/entities/cube_uv.symtres
  2. BIN
      assets/models/cube.symbres
  3. 6
      assets/scenes/Level_1.symtres
  4. 8
      src/common/linmath.c
  5. 13
      src/game/bounding_volumes.c
  6. 1
      src/game/bounding_volumes.h
  7. 2
      src/game/debug_vars.c
  8. 20
      src/game/editor.c
  9. 7
      src/game/geometry.c
  10. 2
      src/game/im_render.c
  11. 9
      src/game/player.c
  12. 3
      src/game/renderer.c
  13. 46
      src/game/transform.c
  14. 1
      todo.txt

@ -0,0 +1,18 @@
Entity
{
type : 6
scale : 68.000 3.000 1.000
material : 0
rotation : 0.000 0.000 0.000 1.000
diffuse_color : 1.000 1.000 1.000 1.000
geometry : cube.symbres
specular : 1.0000
active : true
diffuse_texture : default.tga
diffuse : 1.0000
position : -17.000 4.000 -87.000
specular_strength : 1.0000
name : Cube
uv_scale : 15.000 1.000
}

Binary file not shown.

@ -16,9 +16,9 @@ Player
{ {
type : 2 type : 2
scale : 1.000 1.000 1.000 scale : 1.000 1.000 1.000
rotation : 0.000 -0.489 0.000 0.873 rotation : 0.000 -0.760 0.000 0.651
active : true active : true
position : -51.333 6.103 -36.095 position : -68.875 1.832 -69.569
name : Player name : Player
camera_clear_color : 0.298 0.600 0.898 1.000 camera_clear_color : 0.298 0.600 0.898 1.000
} }
@ -36,7 +36,7 @@ Scene_Entity_Entry
{ {
scale : 68.000 3.000 1.000 scale : 68.000 3.000 1.000
rotation : 0.000 0.000 0.000 1.000 rotation : 0.000 0.000 0.000 1.000
position : -18.000 3.000 -57.000 position : -17.000 4.000 -87.000
filename : cube_uv filename : cube_uv
name : Cube name : Cube
} }

@ -81,11 +81,11 @@ void vec3_fill(vec3* res, float x, float y, float z)
res->z = z; res->z = z;
} }
void vec3_add(vec3* res, const vec3* v1, const vec3* v3) void vec3_add(vec3* res, const vec3* v1, const vec3* v2)
{ {
res->x = v1->x + v3->x; res->x = v1->x + v2->x;
res->y = v1->y + v3->y; res->y = v1->y + v2->y;
res->z = v1->z + v3->z; res->z = v1->z + v2->z;
} }
void vec3_sub(vec3* res, const vec3* v1, const vec3* v2) void vec3_sub(vec3* res, const vec3* v1, const vec3* v2)

@ -190,3 +190,16 @@ float bv_distance_ray_plane(struct Ray* ray, Plane* plane)
return INFINITY; return INFINITY;
} }
void bv_bounding_box_vertices_get(struct Bounding_Box* bounding_box, vec3 out_vertices[8])
{
vec3_fill(&out_vertices[0], bounding_box->min.x, bounding_box->min.y, bounding_box->min.z);
vec3_fill(&out_vertices[1], bounding_box->max.x, bounding_box->min.y, bounding_box->min.z);
vec3_fill(&out_vertices[2], bounding_box->min.x, bounding_box->max.y, bounding_box->min.z);
vec3_fill(&out_vertices[3], bounding_box->min.x, bounding_box->min.y, bounding_box->max.z);
vec3_fill(&out_vertices[4], bounding_box->max.x, bounding_box->max.y, bounding_box->max.z);
vec3_fill(&out_vertices[5], bounding_box->min.x, bounding_box->max.y, bounding_box->max.z);
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);
}

@ -56,5 +56,6 @@ 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); //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); float bv_distance_ray_plane(struct Ray* ray, Plane* plane);
void bv_bounding_box_vertices_get(struct Bounding_Box* box, vec3 out_vertices[8]);
#endif #endif

@ -12,7 +12,7 @@ void debug_vars_init(struct Debug_Vars* debug_vars)
{ {
debug_vars->visible = false; debug_vars->visible = false;
debug_vars->location = DVL_TOP_RIGHT; debug_vars->location = DVL_TOP_RIGHT;
debug_vars->window_width = 300; debug_vars->window_width = 350;
debug_vars->window_height = 500; debug_vars->window_height = 500;
debug_vars->row_height = 14; debug_vars->row_height = 14;
debug_vars->row_height_color = 16; debug_vars->row_height_color = 16;

@ -248,6 +248,26 @@ 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();
/* Draw selected entity with projected transformation applied */ /* Draw selected entity with projected transformation applied */
if(editor->draw_cursor_entity) if(editor->draw_cursor_entity)
{ {

@ -71,11 +71,12 @@ void geom_bounding_volume_generate(int index)
if(vertex->y < box->min.y) box->min.y = vertex->y; if(vertex->y < box->min.y) box->min.y = vertex->y;
if(vertex->z < box->min.z) box->min.z = vertex->z; if(vertex->z < box->min.z) box->min.z = vertex->z;
} }
vec3_add(&sphere->center, &box->max, &box->min); vec3_sub(&sphere->center, &box->max, &box->min);
vec3_scale(&sphere->center, &sphere->center, 0.5f); vec3_scale(&sphere->center, &sphere->center, 0.5f);
vec3 len_vec; vec3 len_vec;
vec3_sub(&len_vec, &box->max, &sphere->center); //vec3_sub(&len_vec, &box->max, &sphere->center);
sphere->radius = fabsf(vec3_len(&len_vec)); //sphere->radius = fabsf(vec3_len(&len_vec));
sphere->radius = fabsf(vec3_len(&sphere->center));
} }
void geom_bounding_volume_generate_all(void) void geom_bounding_volume_generate_all(void)

@ -250,6 +250,7 @@ void im_render(struct Camera* active_viewer)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glEnable(GL_MULTISAMPLE); glEnable(GL_MULTISAMPLE);
glDisable(GL_CULL_FACE);
shader_bind(IM_State.im_shader); shader_bind(IM_State.im_shader);
{ {
static mat4 mvp, translation, rotation, scale; static mat4 mvp, translation, rotation, scale;
@ -291,6 +292,7 @@ void im_render(struct Camera* active_viewer)
} }
shader_unbind(); shader_unbind();
glEnable(GL_CULL_FACE);
glDisable(GL_BLEND); glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glDisable(GL_MULTISAMPLE); glDisable(GL_MULTISAMPLE);

@ -12,6 +12,7 @@
#include "../system/platform.h" #include "../system/platform.h"
#include "game.h" #include "game.h"
#include "debug_vars.h" #include "debug_vars.h"
#include "geometry.h"
void player_init(struct Player* player, struct Scene* scene) void player_init(struct Player* player, struct Scene* scene)
{ {
@ -143,7 +144,15 @@ void player_update(struct Player* player, struct Scene* scene, float dt)
scene_ray_intersect(scene, &ray, &ray_result); scene_ray_intersect(scene, &ray, &ray_result);
} }
vec3 mesh_abs = { 0.f, 0.f, 0.f };
transform_get_absolute_position(player->mesh, &mesh_abs);
debug_vars_show_vec3("Player Position", &player->base.transform.position); debug_vars_show_vec3("Player Position", &player->base.transform.position);
debug_vars_show_vec3("Mesh Position", &mesh_abs);
debug_vars_show_vec3("Min", &player->mesh->base.transform.bounding_box.min);
debug_vars_show_vec3("Max", &player->mesh->base.transform.bounding_box.max);
struct Geometry* geom = geom_get(player->mesh->model.geometry_index);
debug_vars_show_vec3("Geom Min", &geom->bounding_box.min);
debug_vars_show_vec3("Geom Max", &geom->bounding_box.max);
debug_vars_show_texture("Player Camera Render", player->camera_node->render_tex); debug_vars_show_texture("Player Camera Render", player->camera_node->render_tex);
debug_vars_show_texture("Player Camera Depth", player->camera_node->depth_tex); debug_vars_show_texture("Player Camera Depth", player->camera_node->depth_tex);
} }

@ -366,8 +366,7 @@ void renderer_render(struct Renderer* renderer, struct Scene* scene)
if(!(mesh->base.flags & EF_ACTIVE)) continue; if(!(mesh->base.flags & EF_ACTIVE)) continue;
struct Model* model = &mesh->model; struct Model* model = &mesh->model;
struct Transform* transform = &mesh->base.transform; struct Transform* transform = &mesh->base.transform;
//int geometry = model->geometry_index; int geometry = model->geometry_index;
int geometry = geom_find("sphere.symbres");// ->geometry_index;
mat4_identity(&mvp); mat4_identity(&mvp);
mat4_mul(&mvp, &active_camera->view_proj_mat, &transform->trans_mat); mat4_mul(&mvp, &active_camera->view_proj_mat, &transform->trans_mat);
shader_set_uniform_mat4(renderer->debug_shader, "mvp", &mvp); shader_set_uniform_mat4(renderer->debug_shader, "mvp", &mvp);

@ -3,9 +3,13 @@
#include "../common/array.h" #include "../common/array.h"
#include "entity.h" #include "entity.h"
#include "../common/utils.h" #include "../common/utils.h"
#include "../common/num_types.h"
#include "bounding_volumes.h"
#include "geometry.h"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <float.h>
void transform_init(struct Entity* entity, struct Entity* parent) void transform_init(struct Entity* entity, struct Entity* parent)
{ {
@ -25,28 +29,32 @@ void transform_init(struct Entity* entity, struct Entity* parent)
void transform_update_bounding_box(struct Entity* entity) void transform_update_bounding_box(struct Entity* entity)
{ {
struct Bounding_Box* bounding_box = &entity->transform.bounding_box; struct Bounding_Box* box = &entity->transform.bounding_box;
vec3 transformed_points[8]; vec3_fill(&box->min, FLT_MAX, FLT_MAX, FLT_MAX);
vec3_fill(&transformed_points[0], bounding_box->min.x, bounding_box->min.y, bounding_box->min.z); vec3_fill(&box->max, -FLT_MAX, -FLT_MAX, -FLT_MAX);
vec3_fill(&transformed_points[1], bounding_box->max.x, bounding_box->min.y, bounding_box->min.z); vec3 box_vertices[8];
vec3_fill(&transformed_points[2], bounding_box->min.x, bounding_box->max.y, bounding_box->min.z); if(entity->type == ET_STATIC_MESH)
vec3_fill(&transformed_points[3], bounding_box->min.x, bounding_box->min.y, bounding_box->max.z); {
struct Static_Mesh* mesh = (struct Static_Mesh*)entity;
vec3_fill(&transformed_points[4], bounding_box->max.x, bounding_box->max.y, bounding_box->max.z); struct Geometry* geometry = geom_get(mesh->model.geometry_index);
vec3_fill(&transformed_points[5], bounding_box->min.x, bounding_box->max.y, bounding_box->max.z); bv_bounding_box_vertices_get(&geometry->bounding_box, box_vertices);
vec3_fill(&transformed_points[6], bounding_box->max.x, bounding_box->min.y, bounding_box->max.z); }
vec3_fill(&transformed_points[7], bounding_box->max.x, bounding_box->max.y, bounding_box->min.z); else
{
bv_bounding_box_vertices_get(box, box_vertices);
}
for(int i = 0; i < 8; i++) for(int i = 0; i < 8; i++)
{ {
vec3_mul_mat4(&transformed_points[i], &transformed_points[i], &entity->transform.trans_mat); vec3 transformed_vertex = { 0.f, 0.f, 0.f };
if(transformed_points[i].x < bounding_box->min.x) bounding_box->min.x = transformed_points[i].x; vec3_mul_mat4(&transformed_vertex, &box_vertices[i], &entity->transform.trans_mat);
if(transformed_points[i].y < bounding_box->min.y) bounding_box->min.y = transformed_points[i].y; if(transformed_vertex.x < box->min.x) box->min.x = transformed_vertex.x;
if(transformed_points[i].z < bounding_box->min.z) bounding_box->min.z = transformed_points[i].z; if(transformed_vertex.y < box->min.y) box->min.y = transformed_vertex.y;
if(transformed_vertex.z < box->min.z) box->min.z = transformed_vertex.z;
if(transformed_points[i].x > bounding_box->max.x) bounding_box->max.x = transformed_points[i].x;
if(transformed_points[i].y > bounding_box->max.y) bounding_box->max.y = transformed_points[i].y; if(transformed_vertex.x > box->max.x) box->max.x = transformed_vertex.x;
if(transformed_points[i].z > bounding_box->max.z) bounding_box->max.z = transformed_points[i].z; if(transformed_vertex.y > box->max.y) box->max.y = transformed_vertex.y;
if(transformed_vertex.z > box->max.z) box->max.z = transformed_vertex.z;
} }
} }

@ -1,5 +1,6 @@
Todo: Todo:
- Implement bounding box visualization - 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 - 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 - Command to create a placeholder entity of a particular type in a file
- Re-write/Overhaul bounding volumes and ray intersection - Re-write/Overhaul bounding volumes and ray intersection

Loading…
Cancel
Save