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