Draw Manager: Fix blended color.
[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 = UI_GetThemeValuef(TH_VERTEX_SIZE) * M_SQRT2 / 2.0f;
103         ts.sizeFaceDot = 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_set(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_set(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", DRW_viewport_screenvecs_get(), 2);
170         DRW_shgroup_state_set(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, "ModelMatrix", (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, "ModelMatrix", (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", 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", 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 /* ******************************************** COLOR UTILS *********************************************** */
297
298 /* TODO FINISH */
299 /* Get the wire color theme_id of an object based on it's state
300  * **color is a way to get a pointer to the static color var associated */
301 int DRW_object_wire_theme_get(Object *ob, SceneLayer *sl, float **color)
302 {
303         const bool is_edit = (ob->mode & OB_MODE_EDIT) != 0;
304         const bool active = (sl->basact && sl->basact->object == ob);
305         /* confusing logic here, there are 2 methods of setting the color
306          * 'colortab[colindex]' and 'theme_id', colindex overrides theme_id.
307          *
308          * note: no theme yet for 'colindex' */
309         int theme_id = is_edit ? TH_WIRE_EDIT : TH_WIRE;
310
311         if (//(scene->obedit == NULL) &&
312             ((G.moving & G_TRANSFORM_OBJ) != 0) &&
313             ((ob->base_flag & BASE_SELECTED) != 0))
314         {
315                 theme_id = TH_TRANSFORM;
316         }
317         else {
318                 /* Sets the 'theme_id' or fallback to wire */
319                 if ((ob->flag & OB_FROMGROUP) != 0) {
320                         if ((ob->base_flag & BASE_SELECTED) != 0) {
321                                 theme_id = TH_GROUP_ACTIVE;
322                         }
323                         else {
324                                 theme_id = TH_GROUP;
325                         }
326                 }
327                 else {
328                         if ((ob->base_flag & BASE_SELECTED) != 0) {
329                                 theme_id = (active) ? TH_ACTIVE : TH_SELECT;
330                         }
331                         else {
332                                 if (ob->type == OB_LAMP) theme_id = TH_LAMP;
333                                 else if (ob->type == OB_SPEAKER) theme_id = TH_SPEAKER;
334                                 else if (ob->type == OB_CAMERA) theme_id = TH_CAMERA;
335                                 else if (ob->type == OB_EMPTY) theme_id = TH_EMPTY;
336                                 /* fallback to TH_WIRE */
337                         }
338                 }
339         }
340
341         if (color != NULL) {
342                 switch (theme_id) {
343                         case TH_WIRE_EDIT:    *color = ts.colorTransform; break;
344                         case TH_ACTIVE:       *color = ts.colorActive; break;
345                         case TH_SELECT:       *color = ts.colorSelect; break;
346                         case TH_GROUP:        *color = ts.colorGroup; break;
347                         case TH_GROUP_ACTIVE: *color = ts.colorGroupActive; break;
348                         case TH_TRANSFORM:    *color = ts.colorTransform; break;
349                         case OB_SPEAKER:      *color = ts.colorSpeaker; break;
350                         case OB_CAMERA:       *color = ts.colorCamera; break;
351                         case OB_EMPTY:        *color = ts.colorEmpty; break;
352                         case OB_LAMP:         *color = ts.colorLamp; break;
353                         default:              *color = ts.colorWire; break;
354                 }
355
356                 /* uses darker active color for non-active + selected */
357                 if ((theme_id == TH_GROUP_ACTIVE) && !active) {
358                         *color = ts.colorGroupSelect;
359                 }
360         }
361
362         return theme_id;
363 }
364
365 /* XXX This is utter shit, better find something more general */
366 float *DRW_color_background_blend_get(int theme_id)
367 {
368         static float colors[11][4];
369         float *ret;
370
371         switch (theme_id) {
372                 case TH_WIRE_EDIT:    ret = colors[0]; break;
373                 case TH_ACTIVE:       ret = colors[1]; break;
374                 case TH_SELECT:       ret = colors[2]; break;
375                 case TH_GROUP:        ret = colors[3]; break;
376                 case TH_GROUP_ACTIVE: ret = colors[4]; break;
377                 case TH_TRANSFORM:    ret = colors[5]; break;
378                 case OB_SPEAKER:      ret = colors[6]; break;
379                 case OB_CAMERA:       ret = colors[7]; break;
380                 case OB_EMPTY:        ret = colors[8]; break;
381                 case OB_LAMP:         ret = colors[9]; break;
382                 default:              ret = colors[10]; break;
383         }
384
385         UI_GetThemeColorBlendShade4fv(theme_id, TH_BACK, 0.5, 0, ret);
386
387         return ret;
388 }