diff --git a/assets/entities/cube_uv.symtres b/assets/entities/cube_uv.symtres new file mode 100755 index 0000000..f7b5653 --- /dev/null +++ b/assets/entities/cube_uv.symtres @@ -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 +} + diff --git a/assets/models/cube.symbres b/assets/models/cube.symbres index 805312f..7306f16 100755 Binary files a/assets/models/cube.symbres and b/assets/models/cube.symbres differ diff --git a/assets/scenes/Level_1.symtres b/assets/scenes/Level_1.symtres index 53bc997..e4ebde5 100755 --- a/assets/scenes/Level_1.symtres +++ b/assets/scenes/Level_1.symtres @@ -16,9 +16,9 @@ Player { type : 2 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 - position : -51.333 6.103 -36.095 + position : -68.875 1.832 -69.569 name : Player 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 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 name : Cube } diff --git a/src/common/linmath.c b/src/common/linmath.c index dcfe751..6e30a87 100755 --- a/src/common/linmath.c +++ b/src/common/linmath.c @@ -81,11 +81,11 @@ void vec3_fill(vec3* res, float x, float y, float 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->y = v1->y + v3->y; - res->z = v1->z + v3->z; + res->x = v1->x + v2->x; + res->y = v1->y + v2->y; + res->z = v1->z + v2->z; } void vec3_sub(vec3* res, const vec3* v1, const vec3* v2) diff --git a/src/game/bounding_volumes.c b/src/game/bounding_volumes.c index 2728d35..16b9388 100755 --- a/src/game/bounding_volumes.c +++ b/src/game/bounding_volumes.c @@ -190,3 +190,16 @@ float bv_distance_ray_plane(struct Ray* ray, Plane* plane) 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); +} diff --git a/src/game/bounding_volumes.h b/src/game/bounding_volumes.h index ed6d5ed..d3857e8 100755 --- a/src/game/bounding_volumes.h +++ b/src/game/bounding_volumes.h @@ -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); 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]); #endif diff --git a/src/game/debug_vars.c b/src/game/debug_vars.c index 0452369..42bb016 100755 --- a/src/game/debug_vars.c +++ b/src/game/debug_vars.c @@ -12,7 +12,7 @@ void debug_vars_init(struct Debug_Vars* debug_vars) { debug_vars->visible = false; debug_vars->location = DVL_TOP_RIGHT; - debug_vars->window_width = 300; + debug_vars->window_width = 350; debug_vars->window_height = 500; debug_vars->row_height = 14; debug_vars->row_height_color = 16; diff --git a/src/game/editor.c b/src/game/editor.c index c1e3dfc..c27759d 100755 --- a/src/game/editor.c +++ b/src/game/editor.c @@ -247,6 +247,26 @@ void editor_render(struct Editor* editor, struct Camera * active_camera) break; } } + + /* 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 */ if(editor->draw_cursor_entity) @@ -280,10 +300,10 @@ void editor_render(struct Editor* editor, struct Camera * active_camera) { static mat4 mvp; shader_set_uniform_vec4(renderer->debug_shader, "debug_color", &editor->hovered_entity_color); - struct Static_Mesh* mesh = editor->hovered_entity; - struct Model* model = &mesh->model; + struct Static_Mesh* mesh = editor->hovered_entity; + struct Model* model = &mesh->model; struct Transform* transform = &mesh->base.transform; - int geometry = model->geometry_index; + int geometry = model->geometry_index; mat4_identity(&mvp); mat4_mul(&mvp, &active_camera->view_proj_mat, &transform->trans_mat); shader_set_uniform_mat4(renderer->debug_shader, "mvp", &mvp); diff --git a/src/game/geometry.c b/src/game/geometry.c index 285ad58..1d6d1c1 100755 --- a/src/game/geometry.c +++ b/src/game/geometry.c @@ -71,11 +71,12 @@ void geom_bounding_volume_generate(int index) if(vertex->y < box->min.y) box->min.y = vertex->y; 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 len_vec; - vec3_sub(&len_vec, &box->max, &sphere->center); - sphere->radius = fabsf(vec3_len(&len_vec)); + //vec3_sub(&len_vec, &box->max, &sphere->center); + //sphere->radius = fabsf(vec3_len(&len_vec)); + sphere->radius = fabsf(vec3_len(&sphere->center)); } void geom_bounding_volume_generate_all(void) diff --git a/src/game/im_render.c b/src/game/im_render.c index f64d3fd..61aa976 100755 --- a/src/game/im_render.c +++ b/src/game/im_render.c @@ -250,6 +250,7 @@ void im_render(struct Camera* active_viewer) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glEnable(GL_MULTISAMPLE); + glDisable(GL_CULL_FACE); shader_bind(IM_State.im_shader); { static mat4 mvp, translation, rotation, scale; @@ -291,6 +292,7 @@ void im_render(struct Camera* active_viewer) } shader_unbind(); + glEnable(GL_CULL_FACE); glDisable(GL_BLEND); glDisable(GL_DEPTH_TEST); glDisable(GL_MULTISAMPLE); diff --git a/src/game/player.c b/src/game/player.c index a57a7b9..83d6cd9 100755 --- a/src/game/player.c +++ b/src/game/player.c @@ -12,6 +12,7 @@ #include "../system/platform.h" #include "game.h" #include "debug_vars.h" +#include "geometry.h" 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); } + 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("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 Depth", player->camera_node->depth_tex); } diff --git a/src/game/renderer.c b/src/game/renderer.c index e699a25..c581085 100755 --- a/src/game/renderer.c +++ b/src/game/renderer.c @@ -366,8 +366,7 @@ void renderer_render(struct Renderer* renderer, struct Scene* scene) if(!(mesh->base.flags & EF_ACTIVE)) continue; struct Model* model = &mesh->model; struct Transform* transform = &mesh->base.transform; - //int geometry = model->geometry_index; - int geometry = geom_find("sphere.symbres");// ->geometry_index; + int geometry = model->geometry_index; mat4_identity(&mvp); mat4_mul(&mvp, &active_camera->view_proj_mat, &transform->trans_mat); shader_set_uniform_mat4(renderer->debug_shader, "mvp", &mvp); diff --git a/src/game/transform.c b/src/game/transform.c index 2a91f46..1da45f9 100755 --- a/src/game/transform.c +++ b/src/game/transform.c @@ -3,9 +3,13 @@ #include "../common/array.h" #include "entity.h" #include "../common/utils.h" +#include "../common/num_types.h" +#include "bounding_volumes.h" +#include "geometry.h" #include #include +#include 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) { - struct Bounding_Box* bounding_box = &entity->transform.bounding_box; - vec3 transformed_points[8]; - vec3_fill(&transformed_points[0], bounding_box->min.x, bounding_box->min.y, bounding_box->min.z); - vec3_fill(&transformed_points[1], bounding_box->max.x, bounding_box->min.y, bounding_box->min.z); - vec3_fill(&transformed_points[2], bounding_box->min.x, bounding_box->max.y, bounding_box->min.z); - vec3_fill(&transformed_points[3], bounding_box->min.x, bounding_box->min.y, bounding_box->max.z); - - vec3_fill(&transformed_points[4], bounding_box->max.x, bounding_box->max.y, bounding_box->max.z); - vec3_fill(&transformed_points[5], bounding_box->min.x, bounding_box->max.y, bounding_box->max.z); - 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); + struct Bounding_Box* box = &entity->transform.bounding_box; + vec3_fill(&box->min, FLT_MAX, FLT_MAX, FLT_MAX); + vec3_fill(&box->max, -FLT_MAX, -FLT_MAX, -FLT_MAX); + vec3 box_vertices[8]; + if(entity->type == ET_STATIC_MESH) + { + struct Static_Mesh* mesh = (struct Static_Mesh*)entity; + struct Geometry* geometry = geom_get(mesh->model.geometry_index); + bv_bounding_box_vertices_get(&geometry->bounding_box, box_vertices); + } + else + { + bv_bounding_box_vertices_get(box, box_vertices); + } for(int i = 0; i < 8; i++) { - vec3_mul_mat4(&transformed_points[i], &transformed_points[i], &entity->transform.trans_mat); - if(transformed_points[i].x < bounding_box->min.x) bounding_box->min.x = transformed_points[i].x; - if(transformed_points[i].y < bounding_box->min.y) bounding_box->min.y = transformed_points[i].y; - if(transformed_points[i].z < bounding_box->min.z) bounding_box->min.z = transformed_points[i].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_points[i].z > bounding_box->max.z) bounding_box->max.z = transformed_points[i].z; + vec3 transformed_vertex = { 0.f, 0.f, 0.f }; + vec3_mul_mat4(&transformed_vertex, &box_vertices[i], &entity->transform.trans_mat); + if(transformed_vertex.x < box->min.x) box->min.x = transformed_vertex.x; + 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_vertex.x > box->max.x) box->max.x = transformed_vertex.x; + 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; } } diff --git a/todo.txt b/todo.txt index 870915c..506749f 100644 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,6 @@ 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 - Re-write/Overhaul bounding volumes and ray intersection