1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * Copyright 2019, Blender Foundation.
17 */
18
19 /** \file
20 * \ingroup draw_engine
21 */
22
23 #include "DRW_render.h"
24
25 #include "DEG_depsgraph_query.h"
26
27 #include "DNA_particle_types.h"
28
29 #include "BKE_pointcache.h"
30
31 #include "ED_particle.h"
32
33 #include "overlay_private.h"
34
35 /* -------------------------------------------------------------------- */
36 /** \name Edit Particles
37 * \{ */
38
OVERLAY_edit_particle_cache_init(OVERLAY_Data * vedata)39 void OVERLAY_edit_particle_cache_init(OVERLAY_Data *vedata)
40 {
41 OVERLAY_PassList *psl = vedata->psl;
42 OVERLAY_PrivateData *pd = vedata->stl->pd;
43 const DRWContextState *draw_ctx = DRW_context_state_get();
44 ParticleEditSettings *pset = PE_settings(draw_ctx->scene);
45 GPUShader *sh;
46 DRWShadingGroup *grp;
47
48 pd->edit_particle.use_weight = (pset->brushtype == PE_BRUSH_WEIGHT);
49 pd->edit_particle.select_mode = pset->selectmode;
50
51 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
52 DRW_PASS_CREATE(psl->edit_particle_ps, state | pd->clipping_state);
53
54 sh = OVERLAY_shader_edit_particle_strand();
55 pd->edit_particle_strand_grp = grp = DRW_shgroup_create(sh, psl->edit_particle_ps);
56 DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
57 DRW_shgroup_uniform_bool_copy(grp, "useWeight", pd->edit_particle.use_weight);
58 DRW_shgroup_uniform_texture(grp, "weightTex", G_draw.weight_ramp);
59
60 sh = OVERLAY_shader_edit_particle_point();
61 pd->edit_particle_point_grp = grp = DRW_shgroup_create(sh, psl->edit_particle_ps);
62 DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
63 }
64
OVERLAY_edit_particle_cache_populate(OVERLAY_Data * vedata,Object * ob)65 void OVERLAY_edit_particle_cache_populate(OVERLAY_Data *vedata, Object *ob)
66 {
67 OVERLAY_PrivateData *pd = vedata->stl->pd;
68 const DRWContextState *draw_ctx = DRW_context_state_get();
69 Scene *scene_orig = (Scene *)DEG_get_original_id(&draw_ctx->scene->id);
70
71 /* Usually the edit structure is created by Particle Edit Mode Toggle
72 * operator, but sometimes it's invoked after tagging hair as outdated
73 * (for example, when toggling edit mode). That makes it impossible to
74 * create edit structure for until after next dependency graph evaluation.
75 *
76 * Ideally, the edit structure will be created here already via some
77 * dependency graph callback or so, but currently trying to make it nicer
78 * only causes bad level calls and breaks design from the past.
79 */
80 Object *ob_orig = DEG_get_original_object(ob);
81 PTCacheEdit *edit = PE_create_current(draw_ctx->depsgraph, scene_orig, ob_orig);
82 if (edit == NULL) {
83 /* Happens when trying to edit particles in EMITTER mode without
84 * having them cached.
85 */
86 return;
87 }
88 /* NOTE: We need to pass evaluated particle system, which we need
89 * to find first.
90 */
91 ParticleSystem *psys = ob->particlesystem.first;
92 LISTBASE_FOREACH (ParticleSystem *, psys_orig, &ob_orig->particlesystem) {
93 if (PE_get_current_from_psys(psys_orig) == edit) {
94 break;
95 }
96 psys = psys->next;
97 }
98 if (psys == NULL) {
99 printf("Error getting evaluated particle system for edit.\n");
100 return;
101 }
102
103 struct GPUBatch *geom;
104 {
105 geom = DRW_cache_particles_get_edit_strands(ob, psys, edit, pd->edit_particle.use_weight);
106 DRW_shgroup_call(pd->edit_particle_strand_grp, geom, NULL);
107 }
108
109 if (pd->edit_particle.select_mode == SCE_SELECT_POINT) {
110 geom = DRW_cache_particles_get_edit_inner_points(ob, psys, edit);
111 DRW_shgroup_call(pd->edit_particle_point_grp, geom, NULL);
112 }
113
114 if (ELEM(pd->edit_particle.select_mode, SCE_SELECT_POINT, SCE_SELECT_END)) {
115 geom = DRW_cache_particles_get_edit_tip_points(ob, psys, edit);
116 DRW_shgroup_call(pd->edit_particle_point_grp, geom, NULL);
117 }
118 }
119
OVERLAY_edit_particle_draw(OVERLAY_Data * vedata)120 void OVERLAY_edit_particle_draw(OVERLAY_Data *vedata)
121 {
122 OVERLAY_PassList *psl = vedata->psl;
123 OVERLAY_FramebufferList *fbl = vedata->fbl;
124
125 if (DRW_state_is_fbo()) {
126 GPU_framebuffer_bind(fbl->overlay_default_fb);
127 }
128
129 DRW_draw_pass(psl->edit_particle_ps);
130 }
131
132 /** \} */
133
134 /* -------------------------------------------------------------------- */
135 /** \name Particles
136 * \{ */
137
OVERLAY_particle_cache_init(OVERLAY_Data * vedata)138 void OVERLAY_particle_cache_init(OVERLAY_Data *vedata)
139 {
140 OVERLAY_PassList *psl = vedata->psl;
141 OVERLAY_PrivateData *pd = vedata->stl->pd;
142 const DRWContextState *draw_ctx = DRW_context_state_get();
143 ParticleEditSettings *pset = PE_settings(draw_ctx->scene);
144 GPUShader *sh;
145 DRWShadingGroup *grp;
146
147 pd->edit_particle.use_weight = (pset->brushtype == PE_BRUSH_WEIGHT);
148 pd->edit_particle.select_mode = pset->selectmode;
149
150 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
151 DRW_PASS_CREATE(psl->particle_ps, state | pd->clipping_state);
152
153 sh = OVERLAY_shader_particle_dot();
154 pd->particle_dots_grp = grp = DRW_shgroup_create(sh, psl->particle_ps);
155 DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
156 DRW_shgroup_uniform_texture(grp, "weightTex", G_draw.ramp);
157
158 sh = OVERLAY_shader_particle_shape();
159 pd->particle_shapes_grp = grp = DRW_shgroup_create(sh, psl->particle_ps);
160 DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
161 DRW_shgroup_uniform_texture(grp, "weightTex", G_draw.ramp);
162 }
163
OVERLAY_particle_cache_populate(OVERLAY_Data * vedata,Object * ob)164 void OVERLAY_particle_cache_populate(OVERLAY_Data *vedata, Object *ob)
165 {
166 OVERLAY_PrivateData *pd = vedata->stl->pd;
167
168 LISTBASE_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
169 if (!DRW_object_is_visible_psys_in_active_context(ob, psys)) {
170 continue;
171 }
172
173 ParticleSettings *part = psys->part;
174 int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
175
176 if (part->type == PART_HAIR) {
177 /* Hairs should have been rendered by the render engine.*/
178 continue;
179 }
180
181 if (!ELEM(draw_as, PART_DRAW_NOT, PART_DRAW_OB, PART_DRAW_GR)) {
182 struct GPUBatch *geom = DRW_cache_particles_get_dots(ob, psys);
183 struct GPUBatch *shape = NULL;
184 DRWShadingGroup *grp;
185
186 /* TODO(fclem): Here would be a good place for preemptive culling. */
187
188 /* fclem: Is color even usefull in our modern context? */
189 Material *ma = BKE_object_material_get(ob, part->omat);
190 float color[4] = {0.6f, 0.6f, 0.6f, part->draw_size};
191 if (ma != NULL) {
192 copy_v3_v3(color, &ma->r);
193 }
194
195 switch (draw_as) {
196 default:
197 case PART_DRAW_DOT:
198 grp = DRW_shgroup_create_sub(pd->particle_dots_grp);
199 DRW_shgroup_uniform_vec4_copy(grp, "color", color);
200 DRW_shgroup_call(grp, geom, NULL);
201 break;
202 case PART_DRAW_AXIS:
203 case PART_DRAW_CIRC:
204 case PART_DRAW_CROSS:
205 grp = DRW_shgroup_create_sub(pd->particle_shapes_grp);
206 DRW_shgroup_uniform_vec4_copy(grp, "color", color);
207 shape = DRW_cache_particles_get_prim(draw_as);
208 DRW_shgroup_call_instances_with_attrs(grp, NULL, shape, geom);
209 break;
210 }
211 }
212 }
213 }
214
OVERLAY_particle_draw(OVERLAY_Data * vedata)215 void OVERLAY_particle_draw(OVERLAY_Data *vedata)
216 {
217 OVERLAY_PassList *psl = vedata->psl;
218
219 DRW_draw_pass(psl->particle_ps);
220 }
221
222 /** \} */
223