Implement weight colors for lattices in draw manager

This commit is contained in:
2017-06-27 15:59:13 +02:00
parent 6e83ace809
commit d04f30c5d8
6 changed files with 100 additions and 16 deletions

View File

@@ -30,6 +30,7 @@
#include "DNA_object_types.h"
#include "DNA_particle_types.h"
#include "DNA_modifier_types.h"
#include "DNA_lattice_types.h"
#include "BLI_utildefines.h"
#include "BLI_math.h"
@@ -2400,12 +2401,18 @@ Gwn_Batch *DRW_cache_lattice_verts_get(Object *ob)
return DRW_lattice_batch_cache_get_all_verts(lt);
}
Gwn_Batch *DRW_cache_lattice_wire_get(Object *ob)
Gwn_Batch *DRW_cache_lattice_wire_get(Object *ob, bool use_weight)
{
BLI_assert(ob->type == OB_LATTICE);
struct Lattice *lt = ob->data;
return DRW_lattice_batch_cache_get_all_edges(lt);
Lattice *lt = ob->data;
int actdef = -1;
if (use_weight && ob->defbase.first && lt->editlatt->latt->dvert) {
actdef = ob->actdef - 1;
}
return DRW_lattice_batch_cache_get_all_edges(lt, use_weight, actdef);
}
Gwn_Batch *DRW_cache_lattice_vert_overlay_get(Object *ob)

View File

@@ -143,7 +143,7 @@ struct Gwn_Batch *DRW_cache_surf_surface_get(struct Object *ob);
/* Lattice */
struct Gwn_Batch *DRW_cache_lattice_verts_get(struct Object *ob);
struct Gwn_Batch *DRW_cache_lattice_wire_get(struct Object *ob);
struct Gwn_Batch *DRW_cache_lattice_wire_get(struct Object *ob, bool use_weight);
struct Gwn_Batch *DRW_cache_lattice_vert_overlay_get(struct Object *ob);
/* Particles */

View File

@@ -66,7 +66,7 @@ struct Gwn_Batch *DRW_curve_batch_cache_get_overlay_select(struct Curve *cu);
struct Gwn_Batch *BLI_displist_batch_calc_surface(struct ListBase *lb);
/* Lattice */
struct Gwn_Batch *DRW_lattice_batch_cache_get_all_edges(struct Lattice *lt);
struct Gwn_Batch *DRW_lattice_batch_cache_get_all_edges(struct Lattice *lt, bool use_weight, const int actdef);
struct Gwn_Batch *DRW_lattice_batch_cache_get_all_verts(struct Lattice *lt);
struct Gwn_Batch *DRW_lattice_batch_cache_get_overlay_verts(struct Lattice *lt);

View File

@@ -36,8 +36,12 @@
#include "DNA_curve_types.h"
#include "DNA_lattice_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_userdef_types.h"
#include "BKE_lattice.h"
#include "BKE_deform.h"
#include "BKE_texture.h"
#include "GPU_batch.h"
@@ -130,6 +134,8 @@ typedef struct LatticeRenderData {
BPoint *bp;
int actbp;
struct MDeformVert *dvert;
} LatticeRenderData;
enum {
@@ -149,6 +155,8 @@ static LatticeRenderData *lattice_render_data_create(Lattice *lt, const int type
rdata->edit_latt = editlatt;
rdata->dvert = lt->dvert;
if (types & (LR_DATATYPE_VERT)) {
rdata->vert_len = lattice_render_verts_len_get(lt);
}
@@ -160,6 +168,8 @@ static LatticeRenderData *lattice_render_data_create(Lattice *lt, const int type
}
}
else {
rdata->dvert = NULL;
if (types & (LR_DATATYPE_VERT)) {
rdata->vert_len = lattice_render_verts_len_get(lt);
}
@@ -209,6 +219,60 @@ static const BPoint *lattice_render_data_vert_bpoint(const LatticeRenderData *rd
return &rdata->bp[vert_idx];
}
/* TODO, move into shader? */
static void rgb_from_weight(float r_rgb[3], const float weight)
{
const float blend = ((weight / 2.0f) + 0.5f);
if (weight <= 0.25f) { /* blue->cyan */
r_rgb[0] = 0.0f;
r_rgb[1] = blend * weight * 4.0f;
r_rgb[2] = blend;
}
else if (weight <= 0.50f) { /* cyan->green */
r_rgb[0] = 0.0f;
r_rgb[1] = blend;
r_rgb[2] = blend * (1.0f - ((weight - 0.25f) * 4.0f));
}
else if (weight <= 0.75f) { /* green->yellow */
r_rgb[0] = blend * ((weight - 0.50f) * 4.0f);
r_rgb[1] = blend;
r_rgb[2] = 0.0f;
}
else if (weight <= 1.0f) { /* yellow->red */
r_rgb[0] = blend;
r_rgb[1] = blend * (1.0f - ((weight - 0.75f) * 4.0f));
r_rgb[2] = 0.0f;
}
else {
/* exceptional value, unclamped or nan,
* avoid uninitialized memory use */
r_rgb[0] = 1.0f;
r_rgb[1] = 0.0f;
r_rgb[2] = 1.0f;
}
}
static void lattice_render_data_weight_col_get(const LatticeRenderData *rdata, const int vert_idx,
const int actdef, float r_col[4])
{
if (actdef > -1) {
float weight = defvert_find_weight(rdata->dvert + vert_idx, actdef);
if (U.flag & USER_CUSTOM_RANGE) {
do_colorband(&U.coba_weight, weight, r_col);
}
else {
rgb_from_weight(r_col, weight);
}
r_col[3] = 1.0f;
}
else {
zero_v4(r_col);
}
}
enum {
VFLAG_VERTEX_SELECTED = 1 << 0,
VFLAG_VERTEX_ACTIVE = 1 << 1,
@@ -341,16 +405,22 @@ void DRW_lattice_batch_cache_free(Lattice *lt)
}
/* Gwn_Batch cache usage. */
static Gwn_VertBuf *lattice_batch_cache_get_pos(LatticeRenderData *rdata, LatticeBatchCache *cache)
static Gwn_VertBuf *lattice_batch_cache_get_pos(LatticeRenderData *rdata, LatticeBatchCache *cache,
bool use_weight, const int actdef)
{
BLI_assert(rdata->types & LR_DATATYPE_VERT);
if (cache->pos == NULL) {
static Gwn_VertFormat format = { 0 };
static struct { uint pos; } attr_id;
if (format.attrib_ct == 0) {
/* initialize vertex format */
attr_id.pos = GWN_vertformat_attr_add(&format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
static struct { uint pos, col; } attr_id;
GWN_vertformat_clear(&format);
/* initialize vertex format */
attr_id.pos = GWN_vertformat_attr_add(&format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
if (use_weight) {
attr_id.col = GWN_vertformat_attr_add(&format, "color", GWN_COMP_F32, 4, GWN_FETCH_FLOAT);
}
const int vert_len = lattice_render_data_verts_len_get(rdata);
@@ -360,6 +430,13 @@ static Gwn_VertBuf *lattice_batch_cache_get_pos(LatticeRenderData *rdata, Lattic
for (int i = 0; i < vert_len; ++i) {
const BPoint *bp = lattice_render_data_vert_bpoint(rdata, i);
GWN_vertbuf_attr_set(cache->pos, attr_id.pos, i, bp->vec);
if (use_weight) {
float w_col[4];
lattice_render_data_weight_col_get(rdata, i, actdef, w_col);
GWN_vertbuf_attr_set(cache->pos, attr_id.col, i, w_col);
}
}
}
@@ -466,7 +543,7 @@ static void lattice_batch_cache_create_overlay_batches(Lattice *lt)
lattice_render_data_free(rdata);
}
Gwn_Batch *DRW_lattice_batch_cache_get_all_edges(Lattice *lt)
Gwn_Batch *DRW_lattice_batch_cache_get_all_edges(Lattice *lt, bool use_weight, const int actdef)
{
LatticeBatchCache *cache = lattice_batch_cache_get(lt);
@@ -474,7 +551,7 @@ Gwn_Batch *DRW_lattice_batch_cache_get_all_edges(Lattice *lt)
/* create batch from Lattice */
LatticeRenderData *rdata = lattice_render_data_create(lt, LR_DATATYPE_VERT | LR_DATATYPE_EDGE);
cache->all_edges = GWN_batch_create(GWN_PRIM_LINES, lattice_batch_cache_get_pos(rdata, cache),
cache->all_edges = GWN_batch_create(GWN_PRIM_LINES, lattice_batch_cache_get_pos(rdata, cache, use_weight, actdef),
lattice_batch_cache_get_edges(rdata, cache));
lattice_render_data_free(rdata);
@@ -490,7 +567,7 @@ Gwn_Batch *DRW_lattice_batch_cache_get_all_verts(Lattice *lt)
if (cache->all_verts == NULL) {
LatticeRenderData *rdata = lattice_render_data_create(lt, LR_DATATYPE_VERT);
cache->all_verts = GWN_batch_create(GWN_PRIM_POINTS, lattice_batch_cache_get_pos(rdata, cache), NULL);
cache->all_verts = GWN_batch_create(GWN_PRIM_POINTS, lattice_batch_cache_get_pos(rdata, cache, false, -1), NULL);
lattice_render_data_free(rdata);
}

View File

@@ -144,7 +144,7 @@ static void EDIT_LATTICE_engine_init(void *vedata)
*/
if (!e_data.wire_sh) {
e_data.wire_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
e_data.wire_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_SMOOTH_COLOR);
}
if (!e_data.overlay_vert_sh) {
@@ -196,7 +196,7 @@ static void EDIT_LATTICE_cache_populate(void *vedata, Object *ob)
/* Get geometry cache */
struct Gwn_Batch *geom;
geom = DRW_cache_lattice_wire_get(ob);
geom = DRW_cache_lattice_wire_get(ob, true);
DRW_shgroup_call_add(stl->g_data->wire_shgrp, geom, ob->obmat);
geom = DRW_cache_lattice_vert_overlay_get(ob);

View File

@@ -1712,7 +1712,7 @@ static void OBJECT_cache_populate(void *vedata, Object *ob)
{
Object *obedit = scene->obedit;
if (ob != obedit) {
struct Gwn_Batch *geom = DRW_cache_lattice_wire_get(ob);
struct Gwn_Batch *geom = DRW_cache_lattice_wire_get(ob, false);
if (theme_id == TH_UNDEFINED) {
theme_id = DRW_object_wire_theme_get(ob, sl, NULL);
}