Clay Engine: camera drawing

This commit is contained in:
2017-03-04 00:09:22 +01:00
parent 76c9f1a649
commit 608b96c49b
14 changed files with 381 additions and 2 deletions

View File

@@ -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)
{

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -168,6 +168,8 @@ data_to_c_simple(shaders/gpu_shader_instance_variying_size_variying_color_vert.g
data_to_c_simple(shaders/gpu_shader_instance_objectspace_variying_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_instance_screenspace_variying_color_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_instance_screen_aligned_axis_name_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_instance_camera_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_instance_distance_line_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_groundline_geom.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_groundpoint_vert.glsl SRC)

View File

@@ -149,6 +149,10 @@ typedef enum GPUBuiltinShader {
/* bone drawing */
GPU_SHADER_3D_OBJECTSPACE_VARIYING_COLOR,
GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR,
/* camera drawing */
GPU_SHADER_CAMERA,
/* distance in front of objects */
GPU_SHADER_DISTANCE_LINES,
/* axis name */
GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS,
/* instance */

View File

@@ -79,6 +79,8 @@ extern char datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl[]
extern char datatoc_gpu_shader_instance_objectspace_variying_color_vert_glsl[];
extern char datatoc_gpu_shader_instance_screenspace_variying_color_vert_glsl[];
extern char datatoc_gpu_shader_instance_screen_aligned_axis_name_vert_glsl[];
extern char datatoc_gpu_shader_instance_camera_vert_glsl[];
extern char datatoc_gpu_shader_instance_distance_line_vert_glsl[];
extern char datatoc_gpu_shader_3D_groundpoint_vert_glsl[];
extern char datatoc_gpu_shader_3D_groundline_geom_glsl[];
@@ -729,7 +731,12 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
[GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR] = { datatoc_gpu_shader_instance_screenspace_variying_color_vert_glsl,
datatoc_gpu_shader_flat_color_frag_glsl},
[GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS] = { datatoc_gpu_shader_instance_screen_aligned_axis_name_vert_glsl,
datatoc_gpu_shader_flat_color_frag_glsl},
datatoc_gpu_shader_flat_color_frag_glsl},
[GPU_SHADER_CAMERA] = { datatoc_gpu_shader_instance_camera_vert_glsl,
datatoc_gpu_shader_flat_color_frag_glsl},
[GPU_SHADER_DISTANCE_LINES] = { datatoc_gpu_shader_instance_distance_line_vert_glsl,
datatoc_gpu_shader_flat_color_frag_glsl},
[GPU_SHADER_2D_POINT_FIXED_SIZE_UNIFORM_COLOR] =
{ datatoc_gpu_shader_2D_vert_glsl, datatoc_gpu_shader_point_uniform_color_frag_glsl },

View File

@@ -0,0 +1,50 @@
uniform mat4 ViewProjectionMatrix;
/* ---- Instanciated Attribs ---- */
in float pos;
/* ---- Per instance Attribs ---- */
in vec3 color;
in vec4 corners[2]; /* trouble fetching vec2 */
in float depth;
in vec4 tria;
in mat4 InstanceModelMatrix;
flat out vec3 finalColor;
void main()
{
vec3 pPos;
if (pos == 1.0) {
pPos = vec3(corners[0].xy, depth);
}
else if (pos == 2.0) {
pPos = vec3(corners[0].zw, depth);
}
else if (pos == 3.0) {
pPos = vec3(corners[1].xy, depth);
}
else if (pos == 4.0) {
pPos = vec3(corners[1].zw, depth);
}
else if (pos == 5.0) {
pPos = vec3(tria.xy, depth);
}
else if (pos == 6.0) {
vec2 ofs = tria.xy - corners[0].xy;
ofs.x = -ofs.x;
pPos = vec3(corners[1].zw + ofs, depth);
}
else if (pos == 7.0) {
pPos = vec3(tria.zw, depth);
}
else {
pPos = vec3(0.0);
}
gl_Position = ViewProjectionMatrix * InstanceModelMatrix * vec4(pPos, 1.0);
finalColor = color;
}

View File

@@ -0,0 +1,25 @@
uniform mat4 ViewProjectionMatrix;
/* ---- Instanciated Attribs ---- */
in vec3 pos;
/* ---- Per instance Attribs ---- */
in vec3 color;
in float start;
in float end;
in mat4 InstanceModelMatrix;
uniform float size;
flat out vec4 finalColor;
void main()
{
float len = end - start;
vec3 sta = vec3(0.0, 0.0, -start);
gl_Position = ViewProjectionMatrix * InstanceModelMatrix * vec4(pos * -len + sta, 1.0);
gl_PointSize = size;
finalColor = vec4(color, 1.0);
}

View File

@@ -85,6 +85,13 @@ typedef struct Camera {
char sensor_fit;
char pad[7];
/* runtime only, used for drawing */
float drwcorners[4][2];
float drwtria[2][2];
float drwdepth, pad1;
float drwfocusmat[4][4];
float drwnormalmat[4][4];
/* Stereo settings */
struct CameraStereoSettings stereo;
} Camera;

View File

@@ -128,6 +128,7 @@ typedef struct World {
/* nodes */
struct bNodeTree *nodetree;
float mistend, pad1; /* runtime : miststa + mistdist, used for drawing camera */
ListBase gpumaterial; /* runtime */
} World;