diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2017-03-04 02:09:22 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2017-03-05 20:10:08 +0300 |
commit | 608b96c49b93cad8a9b68592c00863a84d857d1b (patch) | |
tree | 9e7f37defe4676ec004abd89e9bc0b442cdee9e5 /source/blender/draw | |
parent | 76c9f1a649c19c117278615bf9e8000c595b0e36 (diff) |
Clay Engine: camera drawing
Diffstat (limited to 'source/blender/draw')
-rw-r--r-- | source/blender/draw/intern/draw_cache.c | 136 | ||||
-rw-r--r-- | source/blender/draw/intern/draw_cache.h | 5 | ||||
-rw-r--r-- | source/blender/draw/intern/draw_mode_pass.c | 137 | ||||
-rw-r--r-- | source/blender/draw/intern/draw_mode_pass.h | 1 | ||||
-rw-r--r-- | source/blender/draw/modes/edit_armature_mode.c | 2 | ||||
-rw-r--r-- | source/blender/draw/modes/edit_mesh_mode.c | 2 | ||||
-rw-r--r-- | source/blender/draw/modes/object_mode.c | 2 |
7 files changed, 284 insertions, 1 deletions
diff --git a/source/blender/draw/intern/draw_cache.c b/source/blender/draw/intern/draw_cache.c index c16e7c23f0d..399ed1032cd 100644 --- a/source/blender/draw/intern/draw_cache.c +++ b/source/blender/draw/intern/draw_cache.c @@ -45,6 +45,7 @@ static struct DRWShapeCache { Batch *drw_cube; Batch *drw_circle; Batch *drw_line; + Batch *drw_line_endpoints; Batch *drw_empty_sphere; Batch *drw_empty_cone; Batch *drw_arrows; @@ -57,6 +58,9 @@ static struct DRWShapeCache { Batch *drw_bone_point; Batch *drw_bone_point_wire; Batch *drw_bone_arrows; + Batch *drw_camera; + Batch *drw_camera_tria; + Batch *drw_camera_focus; } SHC = {NULL}; void DRW_shape_cache_free(void) @@ -75,6 +79,8 @@ void DRW_shape_cache_free(void) Batch_discard_all(SHC.drw_circle); if (SHC.drw_line) Batch_discard_all(SHC.drw_line); + if (SHC.drw_line_endpoints) + Batch_discard_all(SHC.drw_line_endpoints); if (SHC.drw_empty_sphere) Batch_discard_all(SHC.drw_empty_sphere); if (SHC.drw_empty_cone) @@ -99,6 +105,12 @@ void DRW_shape_cache_free(void) Batch_discard_all(SHC.drw_bone_point_wire); if (SHC.drw_bone_arrows) Batch_discard_all(SHC.drw_bone_arrows); + if (SHC.drw_camera) + Batch_discard_all(SHC.drw_camera); + if (SHC.drw_camera_tria) + Batch_discard_all(SHC.drw_camera_tria); + if (SHC.drw_camera_focus) + Batch_discard_all(SHC.drw_camera_focus); } /* Helper functions */ @@ -348,6 +360,32 @@ Batch *DRW_cache_single_line_get(void) return SHC.drw_line; } + +Batch *DRW_cache_single_line_endpoints_get(void) +{ + /* Z axis line */ + if (!SHC.drw_line_endpoints) { + float v1[3] = {0.0f, 0.0f, 0.0f}; + float v2[3] = {0.0f, 0.0f, 1.0f}; + + /* Position Only 3D format */ + static VertexFormat format = { 0 }; + static unsigned pos_id; + if (format.attrib_ct == 0) { + pos_id = add_attrib(&format, "pos", GL_FLOAT, 3, KEEP_FLOAT); + } + + VertexBuffer *vbo = VertexBuffer_create_with_format(&format); + VertexBuffer_allocate_data(vbo, 2); + + setAttrib(vbo, pos_id, 0, v1); + setAttrib(vbo, pos_id, 1, v2); + + SHC.drw_line_endpoints = Batch_create(GL_POINTS, vbo, NULL); + } + return SHC.drw_line_endpoints; +} + /* Empties */ Batch *DRW_cache_plain_axes_get(void) { @@ -842,6 +880,104 @@ Batch *DRW_cache_bone_arrows_get(void) return SHC.drw_bone_arrows; } +/* Camera */ +Batch *DRW_cache_camera_get(void) +{ + if (!SHC.drw_camera) { + float v0 = 0.0f; /* Center point */ + float v1 = 1.0f; /* + X + Y */ + float v2 = 2.0f; /* + X - Y */ + float v3 = 3.0f; /* - X - Y */ + float v4 = 4.0f; /* - X + Y */ + float v5 = 5.0f; /* tria + X */ + float v6 = 6.0f; /* tria - X */ + float v7 = 7.0f; /* tria + Y */ + int v_idx = 0; + + static VertexFormat format = { 0 }; + static unsigned pos_id; + if (format.attrib_ct == 0) { + /* use x coordinate to identify the vertex + * the vertex shader take care to place it + * appropriatelly */ + pos_id = add_attrib(&format, "pos", GL_FLOAT, 1, KEEP_FLOAT); + } + + /* Vertices */ + VertexBuffer *vbo = VertexBuffer_create_with_format(&format); + VertexBuffer_allocate_data(vbo, 22); + + setAttrib(vbo, pos_id, v_idx++, &v0); + setAttrib(vbo, pos_id, v_idx++, &v1); + + setAttrib(vbo, pos_id, v_idx++, &v0); + setAttrib(vbo, pos_id, v_idx++, &v2); + + setAttrib(vbo, pos_id, v_idx++, &v0); + setAttrib(vbo, pos_id, v_idx++, &v3); + + setAttrib(vbo, pos_id, v_idx++, &v0); + setAttrib(vbo, pos_id, v_idx++, &v4); + + /* camera frame */ + setAttrib(vbo, pos_id, v_idx++, &v1); + setAttrib(vbo, pos_id, v_idx++, &v2); + + setAttrib(vbo, pos_id, v_idx++, &v2); + setAttrib(vbo, pos_id, v_idx++, &v3); + + setAttrib(vbo, pos_id, v_idx++, &v3); + setAttrib(vbo, pos_id, v_idx++, &v4); + + setAttrib(vbo, pos_id, v_idx++, &v4); + setAttrib(vbo, pos_id, v_idx++, &v1); + + /* tria */ + setAttrib(vbo, pos_id, v_idx++, &v5); + setAttrib(vbo, pos_id, v_idx++, &v6); + + setAttrib(vbo, pos_id, v_idx++, &v6); + setAttrib(vbo, pos_id, v_idx++, &v7); + + setAttrib(vbo, pos_id, v_idx++, &v7); + setAttrib(vbo, pos_id, v_idx++, &v5); + + SHC.drw_camera = Batch_create(GL_LINES, vbo, NULL); + } + return SHC.drw_camera; +} + +Batch *DRW_cache_camera_tria_get(void) +{ + if (!SHC.drw_camera_tria) { + float v5 = 5.0f; /* tria + X */ + float v6 = 6.0f; /* tria - X */ + float v7 = 7.0f; /* tria + Y */ + int v_idx = 0; + + static VertexFormat format = { 0 }; + static unsigned pos_id; + if (format.attrib_ct == 0) { + /* use x coordinate to identify the vertex + * the vertex shader take care to place it + * appropriatelly */ + pos_id = add_attrib(&format, "pos", GL_FLOAT, 1, KEEP_FLOAT); + } + + /* Vertices */ + VertexBuffer *vbo = VertexBuffer_create_with_format(&format); + VertexBuffer_allocate_data(vbo, 6); + + /* tria */ + setAttrib(vbo, pos_id, v_idx++, &v5); + setAttrib(vbo, pos_id, v_idx++, &v6); + setAttrib(vbo, pos_id, v_idx++, &v7); + + SHC.drw_camera_tria = Batch_create(GL_TRIANGLES, vbo, NULL); + } + return SHC.drw_camera_tria; +} + /* Object Center */ Batch *DRW_cache_single_vert_get(void) { diff --git a/source/blender/draw/intern/draw_cache.h b/source/blender/draw/intern/draw_cache.h index 042bc65b17c..bfab441bee6 100644 --- a/source/blender/draw/intern/draw_cache.h +++ b/source/blender/draw/intern/draw_cache.h @@ -35,6 +35,7 @@ void DRW_shape_cache_free(void); struct Batch *DRW_cache_fullscreen_quad_get(void); struct Batch *DRW_cache_single_vert_get(void); struct Batch *DRW_cache_single_line_get(void); +struct Batch *DRW_cache_single_line_endpoints_get(void); /* Empties */ struct Batch *DRW_cache_plain_axes_get(void); @@ -50,6 +51,10 @@ struct Batch *DRW_cache_axis_names_get(void); struct Batch *DRW_cache_lamp_get(void); struct Batch *DRW_cache_lamp_sunrays_get(void); +/* Camera */ +struct Batch *DRW_cache_camera_get(void); +struct Batch *DRW_cache_camera_tria_get(void); + /* Speaker */ struct Batch *DRW_cache_speaker_get(void); diff --git a/source/blender/draw/intern/draw_mode_pass.c b/source/blender/draw/intern/draw_mode_pass.c index 7bf34c4a9b1..a066f8ce4f4 100644 --- a/source/blender/draw/intern/draw_mode_pass.c +++ b/source/blender/draw/intern/draw_mode_pass.c @@ -23,13 +23,17 @@ * \ingroup draw */ +#include "DNA_camera_types.h" #include "DNA_userdef_types.h" +#include "DNA_view3d_types.h" +#include "DNA_world_types.h" #include "GPU_shader.h" #include "UI_resources.h" #include "BKE_global.h" +#include "BKE_camera.h" #include "DRW_render.h" @@ -70,6 +74,15 @@ static DRWShadingGroup *center_active; static DRWShadingGroup *center_selected; static DRWShadingGroup *center_deselected; +/* Camera */ +static DRWShadingGroup *camera; +static DRWShadingGroup *camera_tria; +static DRWShadingGroup *camera_focus; +static DRWShadingGroup *camera_clip; +static DRWShadingGroup *camera_clip_points; +static DRWShadingGroup *camera_mist; +static DRWShadingGroup *camera_mist_points; + /* Colors & Constant */ GlobalsUboStorage ts; struct GPUUniformBuffer *globals_ubo = NULL; @@ -244,6 +257,36 @@ static DRWShadingGroup *shgroup_instance(DRWPass *pass, struct Batch *geom) return grp; } +static DRWShadingGroup *shgroup_camera_instance(DRWPass *pass, struct Batch *geom) +{ + GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_CAMERA); + + DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom); + DRW_shgroup_attrib_float(grp, "color", 3); + DRW_shgroup_attrib_float(grp, "corners", 8); + DRW_shgroup_attrib_float(grp, "depth", 1); + DRW_shgroup_attrib_float(grp, "tria", 4); + DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16); + + return grp; +} + + +static DRWShadingGroup *shgroup_distance_lines_instance(DRWPass *pass, struct Batch *geom) +{ + GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_DISTANCE_LINES); + static float point_size = 4.0f; + + DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom); + DRW_shgroup_attrib_float(grp, "color", 3); + DRW_shgroup_attrib_float(grp, "start", 1); + DRW_shgroup_attrib_float(grp, "end", 1); + DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16); + DRW_shgroup_uniform_float(grp, "size", &point_size, 1); + + return grp; +} + /* This Function setup the passes needed for the mode rendering. * The passes are populated by the rendering engines using the DRW_shgroup_* functions. * If a pass is not needed use NULL instead of the pass pointer */ @@ -293,7 +336,7 @@ void DRW_mode_passes_setup(DRWPass **psl_wire_overlay, /* Non Meshes Pass (Camera, empties, lamps ...) */ struct Batch *geom; - DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND; + DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND | DRW_STATE_POINT; state |= DRW_STATE_WIRE; *psl_non_meshes = DRW_pass_create("Non Meshes Pass", state); @@ -329,6 +372,24 @@ void DRW_mode_passes_setup(DRWPass **psl_wire_overlay, geom = DRW_cache_speaker_get(); speaker = shgroup_instance(*psl_non_meshes, geom); + /* Camera */ + geom = DRW_cache_camera_get(); + camera = shgroup_camera_instance(*psl_non_meshes, geom); + + geom = DRW_cache_camera_tria_get(); + camera_tria = shgroup_camera_instance(*psl_non_meshes, geom); + + geom = DRW_cache_plain_axes_get(); + camera_focus = shgroup_instance(*psl_non_meshes, geom); + + geom = DRW_cache_single_line_get(); + camera_clip = shgroup_distance_lines_instance(*psl_non_meshes, geom); + camera_mist = shgroup_distance_lines_instance(*psl_non_meshes, geom); + + geom = DRW_cache_single_line_endpoints_get(); + camera_clip_points = shgroup_distance_lines_instance(*psl_non_meshes, geom); + camera_mist_points = shgroup_distance_lines_instance(*psl_non_meshes, geom); + /* Lamps */ /* TODO * for now we create 3 times the same VBO with only lamp center coordinates @@ -555,6 +616,80 @@ void DRW_shgroup_lamp(Object *ob) DRW_shgroup_dynamic_call_add(lamp_groundpoint, ob->obmat[3]); } +void DRW_shgroup_camera(Object *ob) +{ + const struct bContext *C = DRW_get_context(); + View3D *v3d = CTX_wm_view3d(C); + Scene *scene = CTX_data_scene(C); + + Camera *cam = ob->data; + const bool is_active = (ob == v3d->camera); + float *color; + draw_object_wire_theme(ob, &color); + + float vec[4][3], asp[2], shift[2], scale[3], drawsize; + + scale[0] = 1.0f / len_v3(ob->obmat[0]); + scale[1] = 1.0f / len_v3(ob->obmat[1]); + scale[2] = 1.0f / len_v3(ob->obmat[2]); + + BKE_camera_view_frame_ex(scene, cam, cam->drawsize, false, scale, + asp, shift, &drawsize, vec); + + // /* Frame coords */ + copy_v2_v2(cam->drwcorners[0], vec[0]); + copy_v2_v2(cam->drwcorners[1], vec[1]); + copy_v2_v2(cam->drwcorners[2], vec[2]); + copy_v2_v2(cam->drwcorners[3], vec[3]); + + /* depth */ + cam->drwdepth = vec[0][2]; + + /* tria */ + cam->drwtria[0][0] = shift[0] + ((0.7f * drawsize) * scale[0]); + cam->drwtria[0][1] = shift[1] + ((drawsize * (asp[1] + 0.1f)) * scale[1]); + cam->drwtria[1][0] = shift[0]; + cam->drwtria[1][1] = shift[1] + ((1.1f * drawsize * (asp[1] + 0.7f)) * scale[1]); + + DRW_shgroup_dynamic_call_add(camera, color, cam->drwcorners, &cam->drwdepth, cam->drwtria, ob->obmat); + + /* Active cam */ + if (is_active) { + DRW_shgroup_dynamic_call_add(camera_tria, color, cam->drwcorners, &cam->drwdepth, cam->drwtria, ob->obmat); + } + + /* draw the rest in normalize object space */ + copy_m4_m4(cam->drwnormalmat, ob->obmat); + normalize_m4(cam->drwnormalmat); + + if (cam->flag & CAM_SHOWLIMITS) { + static float col[3] = {0.5f, 0.5f, 0.25f}, col_hi[3] = {1.0f, 1.0f, 0.5f}; + float sizemat[4][4], size[3] = {1.0f, 1.0f, 0.0f}; + float focusdist = BKE_camera_object_dof_distance(ob); + + copy_m4_m4(cam->drwfocusmat, cam->drwnormalmat); + translate_m4(cam->drwfocusmat, 0.0f, 0.0f, -focusdist); + size_to_mat4(sizemat, size); + mul_m4_m4m4(cam->drwfocusmat, cam->drwfocusmat, sizemat); + + DRW_shgroup_dynamic_call_add(camera_focus, (is_active ? col_hi : col), &cam->drawsize, cam->drwfocusmat); + + DRW_shgroup_dynamic_call_add(camera_clip, color, &cam->clipsta, &cam->clipend, cam->drwnormalmat); + DRW_shgroup_dynamic_call_add(camera_clip_points, (is_active ? col_hi : col), &cam->clipsta, &cam->clipend, cam->drwnormalmat); + } + + if (cam->flag & CAM_SHOWMIST) { + World *world = scene->world; + + if (world) { + static float col[3] = {0.5f, 0.5f, 0.5f}, col_hi[3] = {1.0f, 1.0f, 1.0f}; + world->mistend = world->miststa + world->mistdist; + DRW_shgroup_dynamic_call_add(camera_mist, color, &world->miststa, &world->mistend, cam->drwnormalmat); + DRW_shgroup_dynamic_call_add(camera_mist_points, (is_active ? col_hi : col), &world->miststa, &world->mistend, cam->drwnormalmat); + } + } +} + void DRW_shgroup_empty(Object *ob) { float *color; diff --git a/source/blender/draw/intern/draw_mode_pass.h b/source/blender/draw/intern/draw_mode_pass.h index 0874dc47a65..fd66120f48d 100644 --- a/source/blender/draw/intern/draw_mode_pass.h +++ b/source/blender/draw/intern/draw_mode_pass.h @@ -84,6 +84,7 @@ void DRW_mode_passes_setup(struct DRWPass **psl_wire_overlay, void DRW_shgroup_wire_outline(struct Object *ob, const bool do_front, const bool do_back, const bool do_outline); void DRW_shgroup_lamp(struct Object *ob); +void DRW_shgroup_camera(struct Object *ob); void DRW_shgroup_empty(struct Object *ob); void DRW_shgroup_speaker(struct Object *ob); void DRW_shgroup_relationship_lines(struct Object *ob); diff --git a/source/blender/draw/modes/edit_armature_mode.c b/source/blender/draw/modes/edit_armature_mode.c index e55a368c9be..5f1418340d1 100644 --- a/source/blender/draw/modes/edit_armature_mode.c +++ b/source/blender/draw/modes/edit_armature_mode.c @@ -77,6 +77,8 @@ void EDIT_ARMATURE_cache_populate(Object *ob) DRW_shgroup_lamp(ob); break; case OB_CAMERA: + DRW_shgroup_camera(ob); + break; case OB_EMPTY: DRW_shgroup_empty(ob); break; diff --git a/source/blender/draw/modes/edit_mesh_mode.c b/source/blender/draw/modes/edit_mesh_mode.c index 691c967f282..d2ef2692cb7 100644 --- a/source/blender/draw/modes/edit_mesh_mode.c +++ b/source/blender/draw/modes/edit_mesh_mode.c @@ -324,6 +324,8 @@ void EDIT_MESH_cache_populate(Object *ob) DRW_shgroup_lamp(ob); break; case OB_CAMERA: + DRW_shgroup_camera(ob); + break; case OB_EMPTY: DRW_shgroup_empty(ob); break; diff --git a/source/blender/draw/modes/object_mode.c b/source/blender/draw/modes/object_mode.c index e9fec871528..25c4bf932e4 100644 --- a/source/blender/draw/modes/object_mode.c +++ b/source/blender/draw/modes/object_mode.c @@ -67,6 +67,8 @@ void OBJECT_cache_populate(Object *ob) DRW_shgroup_lamp(ob); break; case OB_CAMERA: + DRW_shgroup_camera(ob); + break; case OB_EMPTY: DRW_shgroup_empty(ob); break; |