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

particle_mode.c « modes « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5a45a7e92df0d90207bff83f9583ce2a8e11fc3 (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
/*
 * 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 "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "DNA_particle_types.h"

#include "BKE_particle.h"
#include "BKE_pointcache.h"

#include "GPU_shader.h"
#include "GPU_batch.h"

#include "draw_common.h"

#include "draw_mode_engines.h"

#include "ED_particle.h"

#include "DEG_depsgraph_query.h"

#include "draw_cache_impl.h"

extern char datatoc_particle_strand_vert_glsl[];
extern char datatoc_particle_strand_frag_glsl[];

/* *********** LISTS *********** */

typedef struct PARTICLE_PassList {
	struct DRWPass *psys_edit_pass;
} PARTICLE_PassList;

typedef struct PARTICLE_FramebufferList {
	struct GPUFrameBuffer *fb;
} PARTICLE_FramebufferList;

typedef struct PARTICLE_TextureList {
	struct GPUTexture *texture;
} PARTICLE_TextureList;

typedef struct PARTICLE_StorageList {
	struct CustomStruct *block;
	struct PARTICLE_PrivateData *g_data;
} PARTICLE_StorageList;

typedef struct PARTICLE_Data {
	void *engine_type; /* Required */
	PARTICLE_FramebufferList *fbl;
	PARTICLE_TextureList *txl;
	PARTICLE_PassList *psl;
	PARTICLE_StorageList *stl;
} PARTICLE_Data;

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

static struct {
	struct GPUShader *strands_shader;
	struct GPUShader *points_shader;
} e_data = {NULL}; /* Engine data */

typedef struct PARTICLE_PrivateData {
	DRWShadingGroup *strands_group;
	DRWShadingGroup *inner_points_group;
	DRWShadingGroup *tip_points_group;
} PARTICLE_PrivateData; /* Transient data */

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

static void particle_engine_init(void *UNUSED(vedata))
{
	if (!e_data.strands_shader) {
		e_data.strands_shader = DRW_shader_create(
		        datatoc_particle_strand_vert_glsl,
		        NULL,
		        datatoc_particle_strand_frag_glsl,
		        "");
	}
	if (!e_data.points_shader) {
		e_data.points_shader = GPU_shader_get_builtin_shader(
		        GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR);
	}
}

static void particle_cache_init(void *vedata)
{
	PARTICLE_PassList *psl = ((PARTICLE_Data *)vedata)->psl;
	PARTICLE_StorageList *stl = ((PARTICLE_Data *)vedata)->stl;

	if (!stl->g_data) {
		/* Alloc transient pointers */
		stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
	}

	/* Create a pass */
	psl->psys_edit_pass = DRW_pass_create("PSys Edit Pass",
	                                      (DRW_STATE_WRITE_COLOR |
	                                       DRW_STATE_WRITE_DEPTH |
	                                       DRW_STATE_DEPTH_LESS |
	                                       DRW_STATE_WIRE |
	                                       DRW_STATE_POINT));

	stl->g_data->strands_group = DRW_shgroup_create(
	        e_data.strands_shader, psl->psys_edit_pass);
	stl->g_data->inner_points_group = DRW_shgroup_create(
	        e_data.points_shader, psl->psys_edit_pass);
	stl->g_data->tip_points_group = DRW_shgroup_create(
	        e_data.points_shader, psl->psys_edit_pass);

	static float size = 5.0f;
	static float outline_width = 1.0f;
	DRW_shgroup_uniform_float(stl->g_data->inner_points_group, "size", &size, 1);
	DRW_shgroup_uniform_float(stl->g_data->inner_points_group, "outlineWidth", &outline_width, 1);
	DRW_shgroup_uniform_float(stl->g_data->tip_points_group, "size", &size, 1);
	DRW_shgroup_uniform_float(stl->g_data->tip_points_group, "outlineWidth", &outline_width, 1);
}

static void particle_edit_cache_populate(void *vedata, PTCacheEdit *edit)
{
	PARTICLE_StorageList *stl = ((PARTICLE_Data *)vedata)->stl;
	const DRWContextState *draw_ctx = DRW_context_state_get();
	ParticleEditSettings *pset = PE_settings(draw_ctx->scene);
	{
		struct Gwn_Batch *strands = DRW_cache_particles_get_edit_strands(edit);
		DRW_shgroup_call_add(stl->g_data->strands_group, strands, NULL);
	}
	if (pset->selectmode == SCE_SELECT_POINT) {
		struct Gwn_Batch *points = DRW_cache_particles_get_edit_inner_points(edit);
		DRW_shgroup_call_add(stl->g_data->inner_points_group, points, NULL);
	}
	if (ELEM(pset->selectmode, SCE_SELECT_POINT, SCE_SELECT_END)) {
		struct Gwn_Batch *points = DRW_cache_particles_get_edit_tip_points(edit);
		DRW_shgroup_call_add(stl->g_data->tip_points_group, points, NULL);
	}
}

static void particle_cache_populate(void *vedata, Object *object)
{
	for (ParticleSystem *psys = object->particlesystem.first;
	     psys != NULL;
	     psys = psys->next)
	{
		if (!psys_check_enabled(object, psys, false)) {
			continue;
		}
		PTCacheEdit *edit = PE_get_current_from_psys(psys);
		if (edit == NULL) {
			continue;
		}
		particle_edit_cache_populate(vedata, edit);
		break;
	}
}

/* Optional: Post-cache_populate callback */
static void particle_cache_finish(void *UNUSED(vedata))
{
}

/* Draw time ! Control rendering pipeline from here */
static void particle_draw_scene(void *vedata)
{

	PARTICLE_PassList *psl = ((PARTICLE_Data *)vedata)->psl;

	DRW_draw_pass(psl->psys_edit_pass);
}

static void particle_engine_free(void)
{
	DRW_SHADER_FREE_SAFE(e_data.strands_shader);
}

static const DrawEngineDataSize particle_data_size =
      DRW_VIEWPORT_DATA_SIZE(PARTICLE_Data);

DrawEngineType draw_engine_particle_type = {
	NULL, NULL,
	N_("Particle Mode"),
	&particle_data_size,
	&particle_engine_init,
	&particle_engine_free,
	&particle_cache_init,
	&particle_cache_populate,
	&particle_cache_finish,
	NULL, /* draw_background but not needed by mode engines */
	&particle_draw_scene,
	NULL,
	NULL,
};