From 3be1a71957ddd0dcd47599f3db9c1c187d6d1017 Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Sat, 27 Apr 2019 16:42:18 +1000 Subject: [PATCH] Implemnted filled circles and arcs and made arcs handle negative angle values better --- src/game/editor.c | 29 +++++++++++++++++++---------- src/game/geometry.c | 11 ++++++----- src/game/geometry.h | 1 + src/game/im_render.c | 25 +++++++++++++++++++------ src/game/im_render.h | 6 ++++-- todo.txt | 12 +++++++----- 6 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/game/editor.c b/src/game/editor.c index 933a025..f2691ce 100755 --- a/src/game/editor.c +++ b/src/game/editor.c @@ -121,7 +121,7 @@ void editor_init(struct Editor* editor) editor->grid_scale = 1.f; editor->tool_mesh_draw_enabled = 1; editor->tool_snap_enabled = 1; - editor->tool_rotate_arc_radius = 10.f; + editor->tool_rotate_arc_radius = 5.f; editor->tool_rotate_arc_segments = 50.f; editor->tool_rotate_axis_selection_enabled = true; editor->axis_line_length = 500.f; @@ -131,7 +131,7 @@ void editor_init(struct Editor* editor) vec3_fill(&editor->tool_mesh_position, 0.f, 0.f, 0.f); vec4_fill(&editor->tool_mesh_color, 0.f, 1.f, 1.f, 1.f); vec4_fill(&editor->selected_entity_colour, 0.96, 0.61, 0.17, 1.f); - vec4_fill(&editor->grid_color, 0.3f, 0.3f, 0.3f, 0.1f); + vec4_fill(&editor->grid_color, 0.3f, 0.3f, 0.3f, 0.7f); vec4_fill(&editor->axis_color_x, 0.87, 0.32, 0.40, 1.f); vec4_fill(&editor->axis_color_y, 0.53, 0.67, 0.28, 1.f); vec4_fill(&editor->axis_color_z, 0.47, 0.67, 0.89, 1.f); @@ -479,16 +479,16 @@ void editor_update(struct Editor* editor, float dt) { quat rotation = { 0.f, 0.f, 0.f, 1.f }; vec3 scale = { 1.f, 1.f, 1.f }; - im_circle(editor->tool_rotate_arc_radius, editor->tool_rotate_arc_segments, editor->tool_mesh_position, rotation, editor->axis_color_z, 3); + im_circle(editor->tool_rotate_arc_radius, editor->tool_rotate_arc_segments, false, editor->tool_mesh_position, rotation, editor->axis_color_z, 3); //im_arc(editor->tool_rotate_arc_radius, 0.f, 90.f, editor->tool_rotate_arc_segments, editor->tool_mesh_position, rotation, editor->axis_color_z, 3); quat_axis_angle(&rotation, &UNIT_X, -90.f); - im_circle(editor->tool_rotate_arc_radius, editor->tool_rotate_arc_segments, editor->tool_mesh_position, rotation, editor->axis_color_y, 3); + im_circle(editor->tool_rotate_arc_radius, editor->tool_rotate_arc_segments, false, editor->tool_mesh_position, rotation, editor->axis_color_y, 3); //im_arc(editor->tool_rotate_arc_radius, 90.f, 180.f, editor->tool_rotate_arc_segments, editor->tool_mesh_position, rotation, editor->axis_color_y, 3); quat_identity(&rotation); quat_axis_angle(&rotation, &UNIT_Y, -90.f); - im_circle(editor->tool_rotate_arc_radius, editor->tool_rotate_arc_segments, editor->tool_mesh_position, rotation, editor->axis_color_x, 3); + im_circle(editor->tool_rotate_arc_radius, editor->tool_rotate_arc_segments, false, editor->tool_mesh_position, rotation, editor->axis_color_x, 3); //im_arc(editor->tool_rotate_arc_radius, 0.f, 90.f, editor->tool_rotate_arc_segments, editor->tool_mesh_position, rotation, editor->axis_color_x, 3); if(editor->current_axis != EDITOR_AXIS_NONE) @@ -502,10 +502,15 @@ void editor_update(struct Editor* editor, float dt) case EDITOR_AXIS_Z: vec4_assign(&arc_color, &editor->axis_color_z); break; } - if(editor->tool_rotate_amount == 0.f) - im_arc(editor->tool_rotate_arc_radius / 2.f, 0.f, 1.f, editor->tool_rotate_arc_segments, editor->tool_mesh_position, rotation, arc_color, 2); - else - im_arc(editor->tool_rotate_arc_radius / 2.f, 0.f, editor->tool_rotate_amount, editor->tool_rotate_arc_segments, editor->tool_mesh_position, rotation, arc_color, 3); + arc_color.w = 0.1f; + im_circle(editor->tool_rotate_arc_radius, editor->tool_rotate_arc_segments, true, editor->tool_mesh_position, rotation, arc_color, 2); + if(editor->tool_rotate_amount != 0.f) + { + arc_color.w = 0.5f; + im_arc(editor->tool_rotate_arc_radius / 2.f, 0.f, editor->tool_rotate_amount, editor->tool_rotate_arc_segments, true, editor->tool_mesh_position, rotation, arc_color, 4); + } + + } } break; @@ -712,7 +717,11 @@ void editor_on_mousemotion(const struct Event* event) } else /* Rotate on selected axis */ { - editor->tool_rotate_amount += editor->current_axis == EDITOR_AXIS_X ? event->mousemotion.xrel / 2 : -event->mousemotion.xrel / 2; + editor->tool_rotate_amount += event->mousemotion.xrel / 2; + if(editor->tool_rotate_amount > 360.f) + editor->tool_rotate_amount = editor->tool_rotate_amount - 360.f; + else if(editor->tool_rotate_amount < -360.f) + editor->tool_rotate_amount = editor->tool_rotate_amount + 360.f; } } break; diff --git a/src/game/geometry.c b/src/game/geometry.c index c478eac..9193431 100755 --- a/src/game/geometry.c +++ b/src/game/geometry.c @@ -26,11 +26,12 @@ void geom_init(void) geometry_list = array_new(struct Geometry); empty_indices = array_new(int); draw_modes = array_new_cap(GLenum, GDM_NUM_DRAWMODES); - draw_modes[GDM_TRIANGLES] = GL_TRIANGLES; - draw_modes[GDM_LINES] = GL_LINES; - draw_modes[GDM_POINTS] = GL_POINTS; - draw_modes[GDM_LINE_STRIP] = GL_LINE_STRIP; - draw_modes[GDM_LINE_LOOP] = GL_LINE_LOOP; + draw_modes[GDM_TRIANGLES] = GL_TRIANGLES; + draw_modes[GDM_LINES] = GL_LINES; + draw_modes[GDM_POINTS] = GL_POINTS; + draw_modes[GDM_LINE_STRIP] = GL_LINE_STRIP; + draw_modes[GDM_LINE_LOOP] = GL_LINE_LOOP; + draw_modes[GDM_TRIANGLE_FAN] = GL_TRIANGLE_FAN; } int geom_find(const char* filename) diff --git a/src/game/geometry.h b/src/game/geometry.h index bd87086..81eba32 100755 --- a/src/game/geometry.h +++ b/src/game/geometry.h @@ -17,6 +17,7 @@ enum Geometry_Draw_Mode GDM_POINTS, GDM_LINE_STRIP, GDM_LINE_LOOP, + GDM_TRIANGLE_FAN, GDM_NUM_DRAWMODES }; diff --git a/src/game/im_render.c b/src/game/im_render.c index 8009ef2..1a4628d 100755 --- a/src/game/im_render.c +++ b/src/game/im_render.c @@ -161,22 +161,30 @@ void im_line(vec3 p1, vec3 p2, vec3 position, quat rotation, vec3 scale, vec4 co im_end(); } -void im_circle(float radius, int num_divisions, vec3 position, quat rotation, vec4 color, int draw_order) +void im_circle(float radius, int num_divisions, bool filled, vec3 position, quat rotation, vec4 color, int draw_order) { - im_arc(radius, 0.f, 360.f, num_divisions, position, rotation, color, draw_order); + im_arc(radius, 0.f, 360.f, num_divisions, filled, position, rotation, color, draw_order); } -void im_arc(float radius, float angle_start, float angle_end, int num_divisions, vec3 position, quat rotation, vec4 color, int draw_order) +void im_arc(float radius, float angle_start, float angle_end, int num_divisions, bool filled, vec3 position, quat rotation, vec4 color, int draw_order) { - im_begin(position, rotation, (vec3) { 1.f, 1.f, 1.f }, color, GDM_LINE_LOOP, draw_order); + im_begin(position, rotation, (vec3) { 1.f, 1.f, 1.f }, color, filled ? GDM_TRIANGLE_FAN : GDM_LINE_LOOP, draw_order); float arc_degrees = angle_end - angle_start; float increment = arc_degrees / num_divisions; if(arc_degrees != 360) im_pos(0.f, 0.f, 0.f); - for(float i = angle_start; i <= angle_end; i+= increment) - im_pos(sinf(i * M_PI / 180.f) * radius, cosf(i * M_PI / 180.f) * radius, 0.f); + if(angle_start < angle_end) + { + for(float i = angle_start; i <= angle_end; i += increment) + im_pos(sinf(i * M_PI / 180.f) * radius, cosf(i * M_PI / 180.f) * radius, 0.f); + } + else + { + for(float i = angle_start; i >= angle_end; i += increment) + im_pos(sinf(i * M_PI / 180.f) * radius, cosf(i * M_PI / 180.f) * radius, 0.f); + } im_end(); } @@ -205,6 +213,11 @@ void im_render(struct Camera* active_viewer) if(IM_State.curr_geom + 1 > 1) qsort(IM_State.geometries, IM_State.curr_geom + 1, sizeof(struct IM_Geom), &im_sort_func); + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_BLEND); shader_bind(IM_State.im_shader); { static mat4 mvp, translation, rotation, scale; diff --git a/src/game/im_render.h b/src/game/im_render.h index 0991171..4a658e1 100755 --- a/src/game/im_render.h +++ b/src/game/im_render.h @@ -3,6 +3,8 @@ #include "../common/linmath.h" +#include + struct IM_Vertex { vec3 position; @@ -44,8 +46,8 @@ void im_pos(float x, float y, float z); void im_box(float x, float y, float z, vec3 position, quat rotation, vec4 color, int draw_mode, int draw_order); void im_sphere(float radius, vec3 position, quat rotation, vec4 color, int draw_mode, int draw_order); void im_line(vec3 p1, vec3 p2, vec3 position, quat rotation, vec3 scale, vec4 color, int draw_order); -void im_circle(float radius, int num_divisions, vec3 position, quat rotation, vec4 color, int draw_order); -void im_arc(float radius, float angle_start, float angle_end, int num_divisions, vec3 position, quat rotation, vec4 color, int draw_order); +void im_circle(float radius, int num_divisions, bool filled, vec3 position, quat rotation, vec4 color, int draw_order); +void im_arc(float radius, float angle_start, float angle_end, int num_divisions, bool filled, vec3 position, quat rotation, vec4 color, int draw_order); void im_end(void); void im_render(struct Camera* active_viewer); diff --git a/todo.txt b/todo.txt index f8c4d46..f7f1d88 100644 --- a/todo.txt +++ b/todo.txt @@ -1,10 +1,7 @@ Todo: - - Impement im_filled_circle - - Draw rotation gizmo using filled circles to better reflect the underlying logic + - Only show rotation gizmo for one axis at a time + - Rotate mesh along mouse movement or show what the rotation is going to look like by using a wireframe version of mesh when rotating - Match amount to be rotate with actual axes and the gizmo arc being drawn - - Handle negative values in im_arc - - Rotate mode tool widget - - Complete rotate mode - Handle all other axes combinations - Better, more accurate picking - Highlight if we are about to select an entity or perform the tool action like translate when mouse is hovered and an entity can be selected at that location @@ -257,4 +254,9 @@ Done: * Toggle between relative and static grid i.e, grid that moves along with the selected object or grid that remains stationary at the origin * Implement circle drawing with immediate mode renderer * Implement arc drawing with renderer + * Impement im_filled_circle + * Draw rotation gizmo using filled circles to better reflect the underlying logic + * Handle negative values in im_arc + * Rotate mode tool widget + * Complete rotate mode