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