Welcome to mirror list, hosted at ThFree Co, Russian Federation.

edit_strands_mode.c « modes « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 38b601efc9bb4756d6d80ef7a402048c55bf576b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright 2016, Blender Foundation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Blender Institute
 *
 */

/** \file blender/draw/modes/particle_mode.c
 *  \ingroup draw
 */

#include "DRW_engine.h"
#include "DRW_render.h"

#include "BLI_ghash.h"

#include "DNA_view3d_types.h"

#include "BKE_editstrands.h"

#include "GPU_shader.h"
#include "GPU_texture.h"

#include "draw_common.h"

#include "draw_mode_engines.h"

extern GlobalsUboStorage ts;

extern char datatoc_edit_strands_vert_glsl[];
extern char datatoc_gpu_shader_point_varying_color_frag_glsl[];
extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[];

/* *********** LISTS *********** */
/* All lists are per viewport specific datas.
 * They are all free when viewport changes engines
 * or is free itself. Use EDIT_STRANDS_engine_init() to
 * initialize most of them and EDIT_STRANDS_cache_init()
 * for EDIT_STRANDS_PassList */

typedef struct EDIT_STRANDS_PassList {
	/* Declare all passes here and init them in
	 * EDIT_STRANDS_cache_init().
	 * Only contains (DRWPass *) */
	struct DRWPass *wires;
	struct DRWPass *tips;
	struct DRWPass *roots;
	struct DRWPass *points;
} EDIT_STRANDS_PassList;

typedef struct EDIT_STRANDS_StorageList {
	/* Contains any other memory block that the engine needs.
	 * Only directly MEM_(m/c)allocN'ed blocks because they are
	 * free with MEM_freeN() when viewport is freed.
	 * (not per object) */
	struct CustomStruct *block;
	struct EDIT_STRANDS_PrivateData *g_data;
} EDIT_STRANDS_StorageList;

typedef struct EDIT_STRANDS_Data {
	/* Struct returned by DRW_viewport_engine_data_get.
	 * If you don't use one of these, just make it a (void *) */
	// void *fbl;
	void *engine_type; /* Required */
	DRWViewportEmptyList *fbl;
	DRWViewportEmptyList *txl;
	EDIT_STRANDS_PassList *psl;
	EDIT_STRANDS_StorageList *stl;
} EDIT_STRANDS_Data;

/* *********** STATIC *********** */

static struct {
	/* Custom shaders :
	 * Add sources to source/blender/draw/modes/shaders
	 * init in EDIT_STRANDS_engine_init();
	 * free in EDIT_STRANDS_engine_free(); */
	struct GPUShader *edit_point_shader;
	struct GPUShader *edit_wire_shader;
} e_data = {NULL}; /* Engine data */

typedef struct EDIT_STRANDS_PrivateData {
	/* resulting curve as 'wire' for fast editmode drawing */
	DRWShadingGroup *wires_shgrp;
	DRWShadingGroup *tips_shgrp;
	DRWShadingGroup *roots_shgrp;
	DRWShadingGroup *points_shgrp;
} EDIT_STRANDS_PrivateData; /* Transient data */

/* *********** FUNCTIONS *********** */

/* Init Textures, Framebuffers, Storage and Shaders.
 * It is called for every frames.
 * (Optional) */
static void EDIT_STRANDS_engine_init(void *vedata)
{
	EDIT_STRANDS_StorageList *stl = ((EDIT_STRANDS_Data *)vedata)->stl;

	UNUSED_VARS(stl);

	/* Init Framebuffers like this: order is attachment order (for color texs) */
	/*
	 * DRWFboTexture tex[2] = {{&txl->depth, DRW_TEX_DEPTH_24, 0},
	 *                         {&txl->color, DRW_TEX_RGBA_8, DRW_TEX_FILTER}};
	 */

	/* DRW_framebuffer_init takes care of checking if
	 * the framebuffer is valid and has the right size*/
	/*
	 * float *viewport_size = DRW_viewport_size_get();
	 * DRW_framebuffer_init(&fbl->occlude_wire_fb,
	 *                     (int)viewport_size[0], (int)viewport_size[1],
	 *                     tex, 2);
	 */

	if (!e_data.edit_point_shader) {
		e_data.edit_point_shader = DRW_shader_create(
		                               datatoc_edit_strands_vert_glsl,
		                               NULL,
		                               datatoc_gpu_shader_point_varying_color_frag_glsl,
		                               NULL);
	}
	
	if (!e_data.edit_wire_shader) {
		e_data.edit_wire_shader = DRW_shader_create(
		                              datatoc_edit_strands_vert_glsl,
		                              NULL,
		                              datatoc_gpu_shader_3D_smooth_color_frag_glsl,
		                              NULL);
	}
}

/* Cleanup when destroying the engine.
 * This is not per viewport ! only when quitting blender.
 * Mostly used for freeing shaders */
static void EDIT_STRANDS_engine_free(void)
{
	DRW_SHADER_FREE_SAFE(e_data.edit_point_shader);
	DRW_SHADER_FREE_SAFE(e_data.edit_wire_shader);
}

/* Here init all passes and shading groups
 * Assume that all Passes are NULL */
static void EDIT_STRANDS_cache_init(void *vedata)
{
	EDIT_STRANDS_PassList *psl = ((EDIT_STRANDS_Data *)vedata)->psl;
	EDIT_STRANDS_StorageList *stl = ((EDIT_STRANDS_Data *)vedata)->stl;
	
	if (!stl->g_data) {
		/* Alloc transient pointers */
		stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
	}
	
	{
		/* Strand wires */
		DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND;
		psl->wires = DRW_pass_create("Strand Wire Verts Pass", state);
		
		stl->g_data->wires_shgrp = DRW_shgroup_create(e_data.edit_wire_shader, psl->wires);
		DRW_shgroup_uniform_vec4(stl->g_data->wires_shgrp, "color", ts.colorWireEdit, 1);
		DRW_shgroup_uniform_vec4(stl->g_data->wires_shgrp, "colorSelect", ts.colorEdgeSelect, 1);
	}
	
	{
		/* Tip vertices */
		DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND;
		psl->tips = DRW_pass_create("Strand Tip Verts Pass", state);
		
		stl->g_data->tips_shgrp = DRW_shgroup_create(e_data.edit_point_shader, psl->tips);
		DRW_shgroup_uniform_vec4(stl->g_data->tips_shgrp, "color", ts.colorVertex, 1);
		DRW_shgroup_uniform_vec4(stl->g_data->tips_shgrp, "colorSelect", ts.colorVertexSelect, 1);
		DRW_shgroup_uniform_float(stl->g_data->tips_shgrp, "sizeVertex", &ts.sizeVertex, 1);
	}
	
	{
		/* Root vertices */
		DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND;
		psl->roots = DRW_pass_create("Strand Root Verts Pass", state);
		
		stl->g_data->roots_shgrp = DRW_shgroup_create(e_data.edit_point_shader, psl->roots);
		DRW_shgroup_uniform_vec4(stl->g_data->roots_shgrp, "color", ts.colorVertex, 1);
		DRW_shgroup_uniform_vec4(stl->g_data->roots_shgrp, "colorSelect", ts.colorVertexSelect, 1);
		DRW_shgroup_uniform_float(stl->g_data->roots_shgrp, "sizeVertex", &ts.sizeVertex, 1);
	}
	
	{
		/* Interior vertices */
		DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND;
		psl->points = DRW_pass_create("Strand Interior Verts Pass", state);
		
		stl->g_data->points_shgrp = DRW_shgroup_create(e_data.edit_point_shader, psl->points);
		DRW_shgroup_uniform_vec4(stl->g_data->points_shgrp, "color", ts.colorVertex, 1);
		DRW_shgroup_uniform_vec4(stl->g_data->points_shgrp, "colorSelect", ts.colorVertexSelect, 1);
		DRW_shgroup_uniform_float(stl->g_data->points_shgrp, "sizeVertex", &ts.sizeVertex, 1);
	}
}

static void edit_strands_add_ob_to_pass(
        Scene *scene, Object *ob, BMEditStrands *edit,
        DRWShadingGroup *tips_shgrp,
        DRWShadingGroup *roots_shgrp,
        DRWShadingGroup *points_shgrp,
        DRWShadingGroup *wires_shgrp)
{
	HairEditSettings *tsettings = &scene->toolsettings->hair_edit;
	
	{
		struct Gwn_Batch *geom = DRW_cache_editstrands_get_wires(edit);
		DRW_shgroup_call_add(wires_shgrp, geom, ob->obmat);
	}

	switch (tsettings->select_mode) {
		case HAIR_SELECT_TIP: {
			struct Gwn_Batch *geom = DRW_cache_editstrands_get_tips(edit);
			DRW_shgroup_call_add(tips_shgrp, geom, ob->obmat);
			break;
		}
		
		case HAIR_SELECT_STRAND: {
#if 0
			struct Gwn_Batch *geom = DRW_cache_editstrands_get_roots(edit);
			DRW_shgroup_call_add(roots_shgrp, geom, ob->obmat);
#else
			UNUSED_VARS(roots_shgrp);
#endif
			break;
		}
		
		case HAIR_SELECT_VERTEX: {
			struct Gwn_Batch *geom = DRW_cache_editstrands_get_points(edit);
			DRW_shgroup_call_add(points_shgrp, geom, ob->obmat);
			break;
		}
	}
}

/* Add geometry to shadingGroups. Execute for each objects */
static void EDIT_STRANDS_cache_populate(void *vedata, Object *ob)
{
	EDIT_STRANDS_StorageList *stl = ((EDIT_STRANDS_Data *)vedata)->stl;

	BMEditStrands *edit = BKE_editstrands_from_object(ob);
	const DRWContextState *draw_ctx = DRW_context_state_get();
	Scene *scene = draw_ctx->scene;

	// Don't draw strands while editing the object itself
	if (ob == scene->obedit)
		return;

	if (edit) {
		edit_strands_add_ob_to_pass(scene, ob, edit,
		            stl->g_data->tips_shgrp, stl->g_data->roots_shgrp,
		            stl->g_data->points_shgrp, stl->g_data->wires_shgrp);
	}
}

/* Optional: Post-cache_populate callback */
static void EDIT_STRANDS_cache_finish(void *vedata)
{
	EDIT_STRANDS_PassList *psl = ((EDIT_STRANDS_Data *)vedata)->psl;
	EDIT_STRANDS_StorageList *stl = ((EDIT_STRANDS_Data *)vedata)->stl;

	/* Do something here! dependant on the objects gathered */
	UNUSED_VARS(psl, stl);
}

/* Draw time ! Control rendering pipeline from here */
static void EDIT_STRANDS_draw_scene(void *vedata)
{
	EDIT_STRANDS_PassList *psl = ((EDIT_STRANDS_Data *)vedata)->psl;

	DRW_draw_pass(psl->wires);
	DRW_draw_pass(psl->points);
	DRW_draw_pass(psl->roots);
	DRW_draw_pass(psl->tips);
	
	/* If you changed framebuffer, double check you rebind
	 * the default one with its textures attached before finishing */
}

static const DrawEngineDataSize STRANDS_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_STRANDS_Data);

DrawEngineType draw_engine_edit_strands_type = {
	NULL, NULL,
	N_("EditStrandsMode"),
	&STRANDS_data_size,
	EDIT_STRANDS_engine_init,
	EDIT_STRANDS_engine_free,
	&EDIT_STRANDS_cache_init,
	&EDIT_STRANDS_cache_populate,
	&EDIT_STRANDS_cache_finish,
	NULL, /* draw_background but not needed by mode engines */
	&EDIT_STRANDS_draw_scene
};