Fix empty struct errors in paint_vertex_mode.c
[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_StorageList {
49         struct PAINT_VERTEX_PrivateData *g_data;
50 } PAINT_VERTEX_StorageList;
51
52 typedef struct PAINT_VERTEX_Data {
53         void *engine_type; /* Required */
54         DRWViewportEmptyList *fbl;
55         DRWViewportEmptyList *txl;
56         PAINT_VERTEX_PassList *psl;
57         PAINT_VERTEX_StorageList *stl;
58 } PAINT_VERTEX_Data;
59
60 /* *********** STATIC *********** */
61
62 static struct {
63         struct GPUShader *vcolor_face_shader;
64         struct GPUShader *wire_overlay_shader;
65 } e_data = {NULL}; /* Engine data */
66
67 typedef struct PAINT_VERTEX_PrivateData {
68         DRWShadingGroup *fvcolor_shgrp;
69         DRWShadingGroup *lwire_shgrp;
70 } PAINT_VERTEX_PrivateData; /* Transient data */
71
72 /* *********** FUNCTIONS *********** */
73
74 static void PAINT_VERTEX_engine_init(void *UNUSED(vedata))
75 {
76         if (!e_data.vcolor_face_shader) {
77                 e_data.vcolor_face_shader = GPU_shader_get_builtin_shader(GPU_SHADER_SIMPLE_LIGHTING_SMOOTH_COLOR_ALPHA);
78         }
79
80         if (!e_data.wire_overlay_shader) {
81                 e_data.wire_overlay_shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_FLAT_COLOR);
82         }
83 }
84
85 static float world_light;
86
87 static void PAINT_VERTEX_cache_init(void *vedata)
88 {
89         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
90         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
91
92         if (!stl->g_data) {
93                 /* Alloc transient pointers */
94                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
95         }
96
97         {
98                 /* Create a pass */
99                 psl->vcolor_faces = DRW_pass_create("Vert Color Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
100
101                 stl->g_data->fvcolor_shgrp = DRW_shgroup_create(e_data.vcolor_face_shader, psl->vcolor_faces);
102
103                 static float light[3] = {-0.3f, 0.5f, 1.0f};
104                 static float alpha = 1.0f;
105                 DRW_shgroup_uniform_vec3(stl->g_data->fvcolor_shgrp, "light", light, 1);
106                 DRW_shgroup_uniform_float(stl->g_data->fvcolor_shgrp, "alpha", &alpha, 1);
107                 DRW_shgroup_uniform_float(stl->g_data->fvcolor_shgrp, "global", &world_light, 1);
108         }
109
110         {
111                 psl->wire_overlay = DRW_pass_create("Wire Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
112
113                 stl->g_data->lwire_shgrp = DRW_shgroup_create(e_data.wire_overlay_shader, psl->wire_overlay);
114         }
115 }
116
117 static void PAINT_VERTEX_cache_populate(void *vedata, Object *ob)
118 {
119         PAINT_VERTEX_StorageList *stl = ((PAINT_VERTEX_Data *)vedata)->stl;
120         const DRWContextState *draw_ctx = DRW_context_state_get();
121         SceneLayer *sl = draw_ctx->sl;
122
123         if (ob->type == OB_MESH && ob == sl->basact->object) {
124                 IDProperty *ces_mode_pw = BKE_object_collection_engine_get(ob, COLLECTION_MODE_PAINT_VERTEX, "");
125                 bool use_wire = BKE_collection_engine_property_value_get_bool(ces_mode_pw, "use_wire");
126                 char flag = ((Mesh *)ob->data)->editflag;
127                 struct Batch *geom;
128
129                 world_light = BKE_collection_engine_property_value_get_bool(ces_mode_pw, "use_shading") ? 0.5f : 1.0f;
130
131                 geom = DRW_cache_mesh_surface_vert_colors_get(ob);
132                 DRW_shgroup_call_add(stl->g_data->fvcolor_shgrp, geom, ob->obmat);
133
134                 if (flag & ME_EDIT_PAINT_FACE_SEL || use_wire) {
135                         geom = DRW_cache_mesh_edges_paint_overlay_get(ob, use_wire, flag & ME_EDIT_PAINT_FACE_SEL, true);
136                         DRW_shgroup_call_add(stl->g_data->lwire_shgrp, geom, ob->obmat);
137                 }
138         }
139 }
140
141 static void PAINT_VERTEX_draw_scene(void *vedata)
142 {
143         PAINT_VERTEX_PassList *psl = ((PAINT_VERTEX_Data *)vedata)->psl;
144
145         DRW_draw_pass(psl->vcolor_faces);
146         DRW_draw_pass(psl->wire_overlay);
147 }
148
149 static void PAINT_VERTEX_engine_free(void)
150 {
151 }
152
153 void PAINT_VERTEX_collection_settings_create(IDProperty *properties)
154 {
155         BLI_assert(properties &&
156                    properties->type == IDP_GROUP &&
157                    properties->subtype == IDP_GROUP_SUB_MODE_PAINT_VERTEX);
158
159         BKE_collection_engine_property_add_bool(properties, "use_shading", true);
160         BKE_collection_engine_property_add_bool(properties, "use_wire", false);
161 }
162
163 static const DrawEngineDataSize PAINT_VERTEX_data_size = DRW_VIEWPORT_DATA_SIZE(PAINT_VERTEX_Data);
164
165 DrawEngineType draw_engine_paint_vertex_type = {
166         NULL, NULL,
167         N_("PaintVertexMode"),
168         &PAINT_VERTEX_data_size,
169         &PAINT_VERTEX_engine_init,
170         &PAINT_VERTEX_engine_free,
171         &PAINT_VERTEX_cache_init,
172         &PAINT_VERTEX_cache_populate,
173         NULL,
174         NULL,
175         &PAINT_VERTEX_draw_scene
176 };