Object Mode: Add to EvaluationContext & DRWContextState
[blender.git] / source / blender / draw / modes / pose_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/pose_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 extern GlobalsUboStorage ts;
37
38 /* *********** LISTS *********** */
39 /* All lists are per viewport specific datas.
40  * They are all free when viewport changes engines
41  * or is free itself. Use POSE_engine_init() to
42  * initialize most of them and POSE_cache_init()
43  * for POSE_PassList */
44
45 typedef struct POSE_PassList {
46         struct DRWPass *bone_solid;
47         struct DRWPass *bone_wire;
48         struct DRWPass *bone_envelope;
49         struct DRWPass *relationship;
50 } POSE_PassList;
51
52 typedef struct POSE_StorageList {
53         struct POSE_PrivateData *g_data;
54 } POSE_StorageList;
55
56 typedef struct POSE_Data {
57         void *engine_type;
58         DRWViewportEmptyList *fbl;
59         DRWViewportEmptyList *txl;
60         POSE_PassList *psl;
61         POSE_StorageList *stl;
62 } POSE_Data;
63
64 /* *********** STATIC *********** */
65
66 typedef struct POSE_PrivateData {
67         DRWShadingGroup *relationship_lines;
68 } POSE_PrivateData; /* Transient data */
69
70 /* *********** FUNCTIONS *********** */
71
72 /* Here init all passes and shading groups
73  * Assume that all Passes are NULL */
74 static void POSE_cache_init(void *vedata)
75 {
76         POSE_PassList *psl = ((POSE_Data *)vedata)->psl;
77         POSE_StorageList *stl = ((POSE_Data *)vedata)->stl;
78
79         if (!stl->g_data) {
80                 /* Alloc transient pointers */
81                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
82         }
83
84         {
85                 /* Solid bones */
86                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
87                 psl->bone_solid = DRW_pass_create("Bone Solid Pass", state);
88         }
89
90         {
91                 /* Wire bones */
92                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND;
93                 psl->bone_wire = DRW_pass_create("Bone Wire Pass", state);
94         }
95
96         {
97                 /* distance outline around envelope bones */
98                 DRWState state = DRW_STATE_ADDITIVE | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS | DRW_STATE_BLEND;
99                 psl->bone_envelope = DRW_pass_create("Bone Envelope Outline Pass", state);
100         }
101
102         {
103                 /* Non Meshes Pass (Camera, empties, lamps ...) */
104                 DRWState state =
105                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS |
106                         DRW_STATE_BLEND | DRW_STATE_WIRE;
107                 psl->relationship = DRW_pass_create("Bone Relationship Pass", state);
108
109                 /* Relationship Lines */
110                 stl->g_data->relationship_lines = shgroup_dynlines_uniform_color(psl->relationship, ts.colorWire);
111                 DRW_shgroup_state_enable(stl->g_data->relationship_lines, DRW_STATE_STIPPLE_3);
112         }
113 }
114
115 /* Add geometry to shadingGroups. Execute for each objects */
116 static void POSE_cache_populate(void *vedata, Object *ob)
117 {
118         POSE_PassList *psl = ((POSE_Data *)vedata)->psl;
119         POSE_StorageList *stl = ((POSE_Data *)vedata)->stl;
120         const DRWContextState *draw_ctx = DRW_context_state_get();
121
122         /* In the future this will allow us to implement face manipulators,
123          * and similar functionalities. For now we handle only pose bones. */
124
125         if (ob->type == OB_ARMATURE) {
126                 if (DRW_pose_mode_armature(ob, draw_ctx->obact)) {
127                         DRW_shgroup_armature_pose(
128                                 ob, psl->bone_solid, psl->bone_wire, psl->bone_envelope,
129                                 stl->g_data->relationship_lines);
130                 }
131         }
132 }
133
134 /**
135  * Return true if armature should be handled by the pose mode engine.
136  */
137 bool DRW_pose_mode_armature(Object *ob, Object *active_ob)
138 {
139         const DRWContextState *draw_ctx = DRW_context_state_get();
140
141         /* Pode armature is handled by pose mode engine. */
142         if ((ob == active_ob) && ((draw_ctx->object_mode & OB_MODE_POSE) != 0)) {
143                 return true;
144         }
145
146         /* Armature parent is also handled by pose mode engine. */
147         if ((active_ob != NULL) && ((draw_ctx->object_mode & OB_MODE_WEIGHT_PAINT) != 0)) {
148                 if (active_ob->parent == ob) {
149                         return true;
150                 }
151         }
152
153         return false;
154 }
155
156 /* Draw time ! Control rendering pipeline from here */
157 static void POSE_draw_scene(void *vedata)
158 {
159         POSE_PassList *psl = ((POSE_Data *)vedata)->psl;
160
161         DRW_draw_pass(psl->bone_envelope);
162         DRW_draw_pass(psl->bone_wire);
163         DRW_draw_pass(psl->bone_solid);
164         DRW_draw_pass(psl->relationship);
165 }
166
167 /* Create collection settings here.
168  *
169  * Be sure to add this function there :
170  * source/blender/draw/DRW_engine.h
171  * source/blender/blenkernel/intern/layer.c
172  * source/blenderplayer/bad_level_call_stubs/stubs.c
173  *
174  * And relevant collection settings to :
175  * source/blender/makesrna/intern/rna_scene.c
176  * source/blender/blenkernel/intern/layer.c
177  */
178 #if 0
179 void POSE_collection_settings_create(CollectionEngineSettings *ces)
180 {
181         BLI_assert(ces);
182         // BKE_collection_engine_property_add_int(ces, "foo", 37);
183 }
184 #endif
185
186 static const DrawEngineDataSize POSE_data_size = DRW_VIEWPORT_DATA_SIZE(POSE_Data);
187
188 DrawEngineType draw_engine_pose_type = {
189         NULL, NULL,
190         N_("PoseMode"),
191         &POSE_data_size,
192         NULL,
193         NULL,
194         &POSE_cache_init,
195         &POSE_cache_populate,
196         NULL,
197         NULL,
198         &POSE_draw_scene,
199         NULL,
200         NULL,
201 };