Implement particle velocity and acceleration visualization
[blender.git] / source / blender / draw / intern / draw_common.c
1 /*
2  * Copyright 2016, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file blender/draw/intern/draw_common.c
23  *  \ingroup draw
24  */
25
26 #include "DRW_render.h"
27
28 #include "GPU_shader.h"
29 #include "GPU_texture.h"
30
31 #include "UI_resources.h"
32
33 #include "BKE_global.h"
34 #include "BKE_texture.h"
35
36 #include "draw_common.h"
37
38
39 #if 0
40 #define UI_COLOR_RGB_FROM_U8(r, g, b, v4) \
41         ARRAY_SET_ITEMS(v4, (float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, 1.0)
42 #endif
43 #define UI_COLOR_RGBA_FROM_U8(r, g, b, a, v4) \
44         ARRAY_SET_ITEMS(v4, (float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, (float)a / 255.0f)
45
46 /* Colors & Constant */
47 GlobalsUboStorage ts;
48 struct GPUUniformBuffer *globals_ubo = NULL;
49 struct GPUTexture *globals_ramp = NULL;
50
51 void DRW_globals_update(void)
52 {
53         UI_GetThemeColor4fv(TH_WIRE, ts.colorWire);
54         UI_GetThemeColor4fv(TH_WIRE_EDIT, ts.colorWireEdit);
55         UI_GetThemeColor4fv(TH_ACTIVE, ts.colorActive);
56         UI_GetThemeColor4fv(TH_SELECT, ts.colorSelect);
57         UI_GetThemeColor4fv(TH_TRANSFORM, ts.colorTransform);
58         UI_GetThemeColor4fv(TH_GROUP_ACTIVE, ts.colorGroupActive);
59         UI_GetThemeColorShade4fv(TH_GROUP_ACTIVE, -25, ts.colorGroupSelect);
60         UI_GetThemeColor4fv(TH_GROUP, ts.colorGroup);
61         UI_COLOR_RGBA_FROM_U8(0x88, 0xFF, 0xFF, 155, ts.colorLibrarySelect);
62         UI_COLOR_RGBA_FROM_U8(0x55, 0xCC, 0xCC, 155, ts.colorLibrary);
63         UI_GetThemeColor4fv(TH_LAMP, ts.colorLamp);
64         UI_GetThemeColor4fv(TH_SPEAKER, ts.colorSpeaker);
65         UI_GetThemeColor4fv(TH_CAMERA, ts.colorCamera);
66         UI_GetThemeColor4fv(TH_EMPTY, ts.colorEmpty);
67         UI_GetThemeColor4fv(TH_VERTEX, ts.colorVertex);
68         UI_GetThemeColor4fv(TH_VERTEX_SELECT, ts.colorVertexSelect);
69         UI_GetThemeColor4fv(TH_EDITMESH_ACTIVE, ts.colorEditMeshActive);
70         UI_GetThemeColor4fv(TH_EDGE_SELECT, ts.colorEdgeSelect);
71         UI_GetThemeColor4fv(TH_EDGE_SEAM, ts.colorEdgeSeam);
72         UI_GetThemeColor4fv(TH_EDGE_SHARP, ts.colorEdgeSharp);
73         UI_GetThemeColor4fv(TH_EDGE_CREASE, ts.colorEdgeCrease);
74         UI_GetThemeColor4fv(TH_EDGE_BEVEL, ts.colorEdgeBWeight);
75         UI_GetThemeColor4fv(TH_EDGE_FACESEL, ts.colorEdgeFaceSelect);
76         UI_GetThemeColor4fv(TH_FACE, ts.colorFace);
77         UI_GetThemeColor4fv(TH_FACE_SELECT, ts.colorFaceSelect);
78         UI_GetThemeColor4fv(TH_NORMAL, ts.colorNormal);
79         UI_GetThemeColor4fv(TH_VNORMAL, ts.colorVNormal);
80         UI_GetThemeColor4fv(TH_LNORMAL, ts.colorLNormal);
81         UI_GetThemeColor4fv(TH_FACE_DOT, ts.colorFaceDot);
82         UI_GetThemeColor4fv(TH_BACK, ts.colorBackground);
83
84         /* Grid */
85         UI_GetThemeColorShade4fv(TH_GRID, 10, ts.colorGrid);
86         /* emphasise division lines lighter instead of darker, if background is darker than grid */
87         UI_GetThemeColorShade4fv(TH_GRID,
88                 (ts.colorGrid[0] + ts.colorGrid[1] + ts.colorGrid[2] + 0.12 >
89                 ts.colorBackground[0] + ts.colorBackground[1] + ts.colorBackground[2])
90                 ? 20 : -10, ts.colorGridEmphasise);
91         /* Grid Axis */
92         UI_GetThemeColorBlendShade4fv(TH_GRID, TH_AXIS_X, 0.5f, -10, ts.colorGridAxisX);
93         UI_GetThemeColorBlendShade4fv(TH_GRID, TH_AXIS_Y, 0.5f, -10, ts.colorGridAxisY);
94         UI_GetThemeColorBlendShade4fv(TH_GRID, TH_AXIS_Z, 0.5f, -10, ts.colorGridAxisZ);
95
96         UI_GetThemeColorShadeAlpha4fv(TH_TRANSFORM, 0, -80, ts.colorDeselect);
97         UI_GetThemeColorShadeAlpha4fv(TH_WIRE, 0, -30, ts.colorOutline);
98         UI_GetThemeColorShadeAlpha4fv(TH_LAMP, 0, 255, ts.colorLampNoAlpha);
99
100         ts.sizeLampCenter = (U.obcenter_dia + 1.5f) * U.pixelsize;
101         ts.sizeLampCircle = U.pixelsize * 9.0f;
102         ts.sizeLampCircleShadow = ts.sizeLampCircle + U.pixelsize * 3.0f;
103
104         /* M_SQRT2 to be at least the same size of the old square */
105         ts.sizeVertex = ceil(UI_GetThemeValuef(TH_VERTEX_SIZE) * M_SQRT2 / 2.0f);
106         ts.sizeFaceDot = ceil(UI_GetThemeValuef(TH_FACEDOT_SIZE) * M_SQRT2);
107         ts.sizeEdge = 1.0f / 2.0f; /* TODO Theme */
108         ts.sizeEdgeFix = 0.5f + 2.0f * (2.0f * (MAX2(ts.sizeVertex, ts.sizeEdge)) * M_SQRT1_2);
109
110         /* TODO Waiting for notifiers to invalidate cache */
111         if (globals_ubo) {
112                 DRW_uniformbuffer_free(globals_ubo);
113         }
114
115         globals_ubo = DRW_uniformbuffer_create(sizeof(GlobalsUboStorage), &ts);
116
117         ColorBand ramp = {0};
118         float *colors;
119         int col_size;
120
121         ramp.tot = 3;
122         ramp.data[0].a = 1.0f;
123         ramp.data[0].b = 1.0f;
124         ramp.data[0].pos = 0.0f;
125         ramp.data[1].a = 1.0f;
126         ramp.data[1].g = 1.0f;
127         ramp.data[1].pos = 0.5f;
128         ramp.data[2].a = 1.0f;
129         ramp.data[2].r = 1.0f;
130         ramp.data[2].pos = 1.0f;
131
132         colorband_table_RGBA(&ramp, &colors, &col_size);
133         globals_ramp = GPU_texture_create_1D(col_size, colors, NULL);
134
135         MEM_freeN(colors);
136 }
137
138 /* ********************************* SHGROUP ************************************* */
139
140 DRWShadingGroup *shgroup_dynlines_uniform_color(DRWPass *pass, float color[4])
141 {
142         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
143
144         DRWShadingGroup *grp = DRW_shgroup_line_batch_create(sh, pass);
145         DRW_shgroup_uniform_vec4(grp, "color", color, 1);
146
147         return grp;
148 }
149
150 DRWShadingGroup *shgroup_dynpoints_uniform_color(DRWPass *pass, float color[4], float *size)
151 {
152         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA);
153
154         DRWShadingGroup *grp = DRW_shgroup_point_batch_create(sh, pass);
155         DRW_shgroup_uniform_vec4(grp, "color", color, 1);
156         DRW_shgroup_uniform_float(grp, "size", size, 1);
157         DRW_shgroup_state_enable(grp, DRW_STATE_POINT);
158
159         return grp;
160 }
161
162 DRWShadingGroup *shgroup_groundlines_uniform_color(DRWPass *pass, float color[4])
163 {
164         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_GROUNDLINE);
165
166         DRWShadingGroup *grp = DRW_shgroup_point_batch_create(sh, pass);
167         DRW_shgroup_uniform_vec4(grp, "color", color, 1);
168
169         return grp;
170 }
171
172 DRWShadingGroup *shgroup_groundpoints_uniform_color(DRWPass *pass, float color[4])
173 {
174         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_GROUNDPOINT);
175
176         DRWShadingGroup *grp = DRW_shgroup_point_batch_create(sh, pass);
177         DRW_shgroup_uniform_vec4(grp, "color", color, 1);
178         DRW_shgroup_state_enable(grp, DRW_STATE_POINT);
179
180         return grp;
181 }
182
183 DRWShadingGroup *shgroup_instance_screenspace(DRWPass *pass, struct Batch *geom, float *size)
184 {
185         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_SCREENSPACE_VARIYING_COLOR);
186
187         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom);
188         DRW_shgroup_attrib_float(grp, "world_pos", 3);
189         DRW_shgroup_attrib_float(grp, "color", 3);
190         DRW_shgroup_uniform_float(grp, "size", size, 1);
191         DRW_shgroup_uniform_float(grp, "pixel_size", DRW_viewport_pixelsize_get(), 1);
192         DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
193         DRW_shgroup_state_enable(grp, DRW_STATE_STIPPLE_3);
194
195         return grp;
196 }
197
198 DRWShadingGroup *shgroup_instance_objspace_solid(DRWPass *pass, struct Batch *geom, float (*obmat)[4])
199 {
200         static float light[3] = {0.0f, 0.0f, 1.0f};
201         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_OBJECTSPACE_SIMPLE_LIGHTING_VARIYING_COLOR);
202
203         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom);
204         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
205         DRW_shgroup_attrib_float(grp, "color", 4);
206         DRW_shgroup_uniform_mat4(grp, "ObjectModelMatrix", (float *)obmat);
207         DRW_shgroup_uniform_vec3(grp, "light", light, 1);
208
209         return grp;
210 }
211
212 DRWShadingGroup *shgroup_instance_objspace_wire(DRWPass *pass, struct Batch *geom, float (*obmat)[4])
213 {
214         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_OBJECTSPACE_VARIYING_COLOR);
215
216         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom);
217         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
218         DRW_shgroup_attrib_float(grp, "color", 4);
219         DRW_shgroup_uniform_mat4(grp, "ObjectModelMatrix", (float *)obmat);
220
221         return grp;
222 }
223
224 DRWShadingGroup *shgroup_instance_screen_aligned(DRWPass *pass, struct Batch *geom)
225 {
226         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED);
227
228         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom);
229         DRW_shgroup_attrib_float(grp, "color", 3);
230         DRW_shgroup_attrib_float(grp, "size", 1);
231         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
232         DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
233
234         return grp;
235 }
236
237 DRWShadingGroup *shgroup_instance_axis_names(DRWPass *pass, struct Batch *geom)
238 {
239         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_INSTANCE_SCREEN_ALIGNED_AXIS);
240
241         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom);
242         DRW_shgroup_attrib_float(grp, "color", 3);
243         DRW_shgroup_attrib_float(grp, "size", 1);
244         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
245         DRW_shgroup_uniform_vec3(grp, "screen_vecs[0]", DRW_viewport_screenvecs_get(), 2);
246
247         return grp;
248 }
249
250 DRWShadingGroup *shgroup_instance_scaled(DRWPass *pass, struct Batch *geom)
251 {
252         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SCALE);
253
254         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom);
255         DRW_shgroup_attrib_float(grp, "color", 3);
256         DRW_shgroup_attrib_float(grp, "size", 3);
257         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
258
259         return grp;
260 }
261
262 DRWShadingGroup *shgroup_instance(DRWPass *pass, struct Batch *geom)
263 {
264         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE);
265
266         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom);
267         DRW_shgroup_attrib_float(grp, "color", 3);
268         DRW_shgroup_attrib_float(grp, "size", 1);
269         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
270
271         return grp;
272 }
273
274 DRWShadingGroup *shgroup_camera_instance(DRWPass *pass, struct Batch *geom)
275 {
276         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_CAMERA);
277
278         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom);
279         DRW_shgroup_attrib_float(grp, "color", 3);
280         DRW_shgroup_attrib_float(grp, "corners", 8);
281         DRW_shgroup_attrib_float(grp, "depth", 1);
282         DRW_shgroup_attrib_float(grp, "tria", 4);
283         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
284
285         return grp;
286 }
287
288 DRWShadingGroup *shgroup_distance_lines_instance(DRWPass *pass, struct Batch *geom)
289 {
290         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_DISTANCE_LINES);
291         static float point_size = 4.0f;
292
293         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom);
294         DRW_shgroup_attrib_float(grp, "color", 3);
295         DRW_shgroup_attrib_float(grp, "start", 1);
296         DRW_shgroup_attrib_float(grp, "end", 1);
297         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
298         DRW_shgroup_uniform_float(grp, "size", &point_size, 1);
299
300         return grp;
301 }
302
303 DRWShadingGroup *shgroup_spot_instance(DRWPass *pass, struct Batch *geom)
304 {
305         GPUShader *sh_inst = GPU_shader_get_builtin_shader(GPU_SHADER_INSTANCE_EDGES_VARIYING_COLOR);
306         static bool True = true;
307         static bool False = false;
308
309         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh_inst, pass, geom);
310         DRW_shgroup_attrib_float(grp, "color", 3);
311         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
312         DRW_shgroup_uniform_bool(grp, "drawFront", &False, 1);
313         DRW_shgroup_uniform_bool(grp, "drawBack", &False, 1);
314         DRW_shgroup_uniform_bool(grp, "drawSilhouette", &True, 1);
315
316         return grp;
317 }
318
319 DRWShadingGroup *shgroup_instance_bone_envelope_wire(DRWPass *pass, struct Batch *geom, float (*obmat)[4])
320 {
321         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_WIRE);
322
323         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom);
324         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
325         DRW_shgroup_attrib_float(grp, "color", 4);
326         DRW_shgroup_attrib_float(grp, "radius_head", 1);
327         DRW_shgroup_attrib_float(grp, "radius_tail", 1);
328         DRW_shgroup_attrib_float(grp, "distance", 1);
329         DRW_shgroup_uniform_mat4(grp, "ObjectModelMatrix", (float *)obmat);
330
331         return grp;
332 }
333
334 DRWShadingGroup *shgroup_instance_bone_envelope_solid(DRWPass *pass, struct Batch *geom, float (*obmat)[4])
335 {
336         static float light[3] = {0.0f, 0.0f, 1.0f};
337         GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_INSTANCE_BONE_ENVELOPE_SOLID);
338
339         DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom);
340         DRW_shgroup_attrib_float(grp, "InstanceModelMatrix", 16);
341         DRW_shgroup_attrib_float(grp, "color", 4);
342         DRW_shgroup_attrib_float(grp, "radius_head", 1);
343         DRW_shgroup_attrib_float(grp, "radius_tail", 1);
344         DRW_shgroup_uniform_mat4(grp, "ObjectModelMatrix", (float *)obmat);
345         DRW_shgroup_uniform_vec3(grp, "light", light, 1);
346
347         return grp;
348 }
349
350
351 /* ******************************************** COLOR UTILS *********************************************** */
352
353 /* TODO FINISH */
354 /**
355  * Get the wire color theme_id of an object based on it's state
356  * \a r_color is a way to get a pointer to the static color var associated
357  */
358 int DRW_object_wire_theme_get(Object *ob, SceneLayer *sl, float **r_color)
359 {
360         const bool is_edit = (ob->mode & OB_MODE_EDIT) != 0;
361         const bool active = (sl->basact && sl->basact->object == ob);
362         /* confusing logic here, there are 2 methods of setting the color
363          * 'colortab[colindex]' and 'theme_id', colindex overrides theme_id.
364          *
365          * note: no theme yet for 'colindex' */
366         int theme_id = is_edit ? TH_WIRE_EDIT : TH_WIRE;
367
368         if (//(scene->obedit == NULL) &&
369             ((G.moving & G_TRANSFORM_OBJ) != 0) &&
370             ((ob->base_flag & BASE_SELECTED) != 0))
371         {
372                 theme_id = TH_TRANSFORM;
373         }
374         else {
375                 /* Sets the 'theme_id' or fallback to wire */
376                 if ((ob->flag & OB_FROMGROUP) != 0) {
377                         if ((ob->base_flag & BASE_SELECTED) != 0) {
378                                 theme_id = TH_GROUP_ACTIVE;
379                         }
380                         else {
381                                 theme_id = TH_GROUP;
382                         }
383                 }
384                 else {
385                         if ((ob->base_flag & BASE_SELECTED) != 0) {
386                                 theme_id = (active) ? TH_ACTIVE : TH_SELECT;
387                         }
388                         else {
389                                 if (ob->type == OB_LAMP) theme_id = TH_LAMP;
390                                 else if (ob->type == OB_SPEAKER) theme_id = TH_SPEAKER;
391                                 else if (ob->type == OB_CAMERA) theme_id = TH_CAMERA;
392                                 else if (ob->type == OB_EMPTY) theme_id = TH_EMPTY;
393                                 /* fallback to TH_WIRE */
394                         }
395                 }
396         }
397
398         if (r_color != NULL) {
399                 switch (theme_id) {
400                         case TH_WIRE_EDIT:    *r_color = ts.colorTransform; break;
401                         case TH_ACTIVE:       *r_color = ts.colorActive; break;
402                         case TH_SELECT:       *r_color = ts.colorSelect; break;
403                         case TH_GROUP:        *r_color = ts.colorGroup; break;
404                         case TH_GROUP_ACTIVE: *r_color = ts.colorGroupActive; break;
405                         case TH_TRANSFORM:    *r_color = ts.colorTransform; break;
406                         case OB_SPEAKER:      *r_color = ts.colorSpeaker; break;
407                         case OB_CAMERA:       *r_color = ts.colorCamera; break;
408                         case OB_EMPTY:        *r_color = ts.colorEmpty; break;
409                         case OB_LAMP:         *r_color = ts.colorLamp; break;
410                         default:              *r_color = ts.colorWire; break;
411                 }
412
413                 /* uses darker active color for non-active + selected */
414                 if ((theme_id == TH_GROUP_ACTIVE) && !active) {
415                         *r_color = ts.colorGroupSelect;
416                 }
417         }
418
419         return theme_id;
420 }
421
422 /* XXX This is utter shit, better find something more general */
423 float *DRW_color_background_blend_get(int theme_id)
424 {
425         static float colors[11][4];
426         float *ret;
427
428         switch (theme_id) {
429                 case TH_WIRE_EDIT:    ret = colors[0]; break;
430                 case TH_ACTIVE:       ret = colors[1]; break;
431                 case TH_SELECT:       ret = colors[2]; break;
432                 case TH_GROUP:        ret = colors[3]; break;
433                 case TH_GROUP_ACTIVE: ret = colors[4]; break;
434                 case TH_TRANSFORM:    ret = colors[5]; break;
435                 case OB_SPEAKER:      ret = colors[6]; break;
436                 case OB_CAMERA:       ret = colors[7]; break;
437                 case OB_EMPTY:        ret = colors[8]; break;
438                 case OB_LAMP:         ret = colors[9]; break;
439                 default:              ret = colors[10]; break;
440         }
441
442         UI_GetThemeColorBlendShade4fv(theme_id, TH_BACK, 0.5, 0, ret);
443
444         return ret;
445 }