ce09307bc0cd4f49f5cdc4a566c4b8aafd8790c4
[blender.git] / source / blender / draw / modes / paint_vertex_mode.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/modes/paint_vertex_mode.c
23  *  \ingroup draw
24  */
25
26 #include "DRW_engine.h"
27 #include "DRW_render.h"
28
29 /* If builtin shaders are needed */
30 #include "GPU_shader.h"
31
32 #include "draw_common.h"
33
34 #include "draw_mode_engines.h"
35
36 #include "DNA_mesh_types.h"
37
38 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
39 extern struct GlobalsUboStorage ts; /* draw_common.c */
40
41 /* *********** LISTS *********** */
42
43 typedef struct PAINT_VERTEX_PassList {
44         struct DRWPass *vcolor_faces;
45         struct DRWPass *wire_overlay;
46 } PAINT_VERTEX_PassList;
47
48 typedef struct PAINT_VERTEX_FramebufferList {
49 } PAINT_VERTEX_FramebufferList;
50
51 typedef struct PAINT_VERTEX_TextureList {
52 } PAINT_VERTEX_TextureList;
53
54 typedef struct PAINT_VERTEX_StorageList {
55         struct PAINT_VERTEX_PrivateData *g_data;
56 } PAINT_VERTEX_StorageList;
57
58 typedef struct PAINT_VERTEX_Data {
59         void *engine_type; /* Required */
60         PAINT_VERTEX_FramebufferList *fbl;
61         PAINT_VERTEX_TextureList *txl;
62         PAINT_VERTEX_PassList *psl;
63         PAINT_VERTEX_StorageList *stl;
64 } PAINT_VERTEX_Data;
65
66 /* *********** STATIC *********** */
67
68 static struct {
69         struct GPUShader *vcolor_face_shader;
70         struct GPUShader *wire_overlay_shader;
71 } e_data = {NULL}; /* Engine data */
72
73 typedef struct PAINT_VERTEX_PrivateData {
74         DRWShadingGroup *fvcolor_shgrp;
75         DRWShadingGroup *lwire_shgrp;
76 } PAINT_VERTEX_PrivateData; /* Transient data */
77
78 /* *********** FUNCTIONS *********** */
79
80 static void PAINT_VERTEX_engine_init(void *UNUSED(vedata))
81 {
82         if (!e_data.vcolor_face_shader) {
83                 e_data.vcolor_face_shader = GPU_shader_get_builtin_shader(GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR_ALPHA);
84         }
85
86         if (!e_data.wire_overlay_shader) {
87                 e_data.wire_overlay_shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_FLAT_COLOR);
88         }
89 }
90
91 static float world_light;
92
93 static void PAINT_VERTEX_cache_init(void *vedata)
94 {
95         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
96         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
97
98         if (!stl->g_data) {
99                 /* Alloc transient pointers */
100                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
101         }
102
103         {
104                 /* Create a pass */
105                 psl->vcolor_faces = DRW_pass_create("Vert Color Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
106
107                 stl->g_data->fvcolor_shgrp = DRW_shgroup_create(e_data.vcolor_face_shader, psl->vcolor_faces);
108
109                 static float light[3] = {-0.3f, 0.5f, 1.0f};
110                 static float alpha = 1.0f;
111                 DRW_shgroup_uniform_vec3(stl->g_data->fvcolor_shgrp, "light", light, 1);
112                 DRW_shgroup_uniform_float(stl->g_data->fvcolor_shgrp, "alpha", &alpha, 1);
113                 DRW_shgroup_uniform_float(stl->g_data->fvcolor_shgrp, "global", &world_light, 1);
114         }
115
116         {
117                 psl->wire_overlay = DRW_pass_create("Wire Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
118
119                 stl->g_data->lwire_shgrp = DRW_shgroup_create(e_data.wire_overlay_shader, psl->wire_overlay);
120         }
121 }
122
123 static void PAINT_VERTEX_cache_populate(void *vedata, Object *ob)
124 {
125         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
126         const DRWContextState *draw_ctx = DRW_context_state_get();
127         SceneLayer *sl = draw_ctx->sl;
128
129         if (ob->type == OB_MESH && ob == sl->basact->object) {
130                 IDProperty *ces_mode_pw = BKE_object_collection_engine_get(ob, COLLECTION_MODE_PAINT_VERTEX, "");
131                 bool use_wire = BKE_collection_engine_property_value_get_bool(ces_mode_pw, "use_wire");
132                 char flag = ((Mesh *)ob->data)->editflag;
133                 struct Batch *geom;
134
135                 world_light = BKE_collection_engine_property_value_get_bool(ces_mode_pw, "use_shading") ? 0.5f : 1.0f;
136
137                 geom = DRW_cache_mesh_surface_vert_colors_get(ob);
138                 DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
139
140                 if (flag & ME_EDIT_PAINT_FACE_SEL || use_wire) {
141                         geom = DRW_cache_mesh_edges_paint_overlay_get(ob, use_wire, flag & ME_EDIT_PAINT_FACE_SEL, true);
142                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
143                 }
144         }
145 }
146
147 static void PAINT_VERTEX_draw_scene(void *vedata)
148 {
149         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
150
151         DRW_draw_pass(psl->vcolor_faces);
152         DRW_draw_pass(psl->wire_overlay);
153 }
154
155 static void PAINT_VERTEX_engine_free(void)
156 {
157 }
158
159 void PAINT_VERTEX_collection_settings_create(IDProperty *properties)
160 {
161         BLI_assert(properties &&
162                    properties->type == IDP_GROUP &&
163                    properties->subtype == IDP_GROUP_SUB_MODE_PAINT_VERTEX);
164
165         BKE_collection_engine_property_add_bool(properties, "use_shading", true);
166         BKE_collection_engine_property_add_bool(properties, "use_wire", false);
167 }
168
169 static const DrawEngineDataSize PAINT_VERTEX_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_VERTEX_Data);
170
171 DrawEngineType draw_engine_paint_vertex_type = {
172         NULL, NULL,
173         N_("PaintVertexMode"),
174         &PAINT_VERTEX_data_size,
175         &PAINT_VERTEX_engine_init,
176         &PAINT_VERTEX_engine_free,
177         &PAINT_VERTEX_cache_init,
178         &PAINT_VERTEX_cache_populate,
179         NULL,
180         NULL,
181         &PAINT_VERTEX_draw_scene
182 };