02f208900e4e623bd27b6bd16b90e2c08e48e8e7
[blender.git] / source / blender / draw / modes / edit_armature_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/edit_armature_mode.c
23  *  \ingroup draw
24  */
25
26 #include "DRW_engine.h"
27 #include "DRW_render.h"
28
29 #include "DNA_armature_types.h"
30 #include "DNA_view3d_types.h"
31
32 #include "draw_common.h"
33
34 #include "draw_mode_engines.h"
35
36 extern GlobalsUboStorage ts;
37
38 /* *********** LISTS *********** */
39 typedef struct EDIT_ARMATURE_PassList {
40         struct DRWPass *bone_solid[2];
41         struct DRWPass *bone_wire[2];
42         struct DRWPass *bone_outline[2];
43         struct DRWPass *bone_envelope[2];
44         struct DRWPass *bone_axes;
45         struct DRWPass *relationship[2];
46 } EDIT_ARMATURE_PassList;
47
48 typedef struct EDIT_ARMATURE_StorageList {
49         struct EDIT_ARMATURE_PrivateData *g_data;
50 } EDIT_ARMATURE_StorageList;
51
52 typedef struct EDIT_ARMATURE_Data {
53         void *engine_type;
54         DRWViewportEmptyList *fbl;
55         DRWViewportEmptyList *txl;
56         EDIT_ARMATURE_PassList *psl;
57         EDIT_ARMATURE_StorageList *stl;
58 } EDIT_ARMATURE_Data;
59
60 /* *********** STATIC *********** */
61
62 typedef struct EDIT_ARMATURE_PrivateData {
63         bool transparent_bones;
64 } EDIT_ARMATURE_PrivateData; /* Transient data */
65
66 /* *********** FUNCTIONS *********** */
67
68 static void EDIT_ARMATURE_cache_init(void *vedata)
69 {
70         EDIT_ARMATURE_PassList *psl = ((EDIT_ARMATURE_Data *)vedata)->psl;
71         EDIT_ARMATURE_StorageList *stl = ((EDIT_ARMATURE_Data *)vedata)->stl;
72         const DRWContextState *draw_ctx = DRW_context_state_get();
73
74         if (!stl->g_data) {
75                 /* Alloc transient pointers */
76                 stl->g_data = MEM_callocN(sizeof(*stl->g_data), __func__);
77         }
78         stl->g_data->transparent_bones = (draw_ctx->v3d->overlay.arm_flag & V3D_OVERLAY_ARM_TRANSP_BONES) != 0;
79
80         for (int i = 0; i < 2; ++i) {
81                 /* Solid bones */
82                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK;
83                 state |= (stl->g_data->transparent_bones) ? DRW_STATE_BLEND : DRW_STATE_WRITE_DEPTH;
84                 psl->bone_solid[i] = DRW_pass_create("Bone Solid Pass", state);
85
86                 /* Bones Outline */
87                 state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
88                 psl->bone_outline[i] = DRW_pass_create("Bone Outline Pass", state);
89
90                 /* Wire bones */
91                 state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND;
92                 psl->bone_wire[i] = DRW_pass_create("Bone Wire Pass", state);
93
94                 /* distance outline around envelope bones */
95                 state = DRW_STATE_ADDITIVE | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_FRONT;
96                 psl->bone_envelope[i] = DRW_pass_create("Bone Envelope Outline Pass", state);
97
98                 state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL |
99                         DRW_STATE_BLEND | DRW_STATE_WIRE;
100                 psl->relationship[i] = DRW_pass_create("Bone Relationship Pass", state);
101         }
102
103         {
104                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WIRE_SMOOTH | DRW_STATE_BLEND;
105                 psl->bone_axes = DRW_pass_create("Bone Axes Pass", state);
106         }
107 }
108
109 static void EDIT_ARMATURE_cache_populate(void *vedata, Object *ob)
110 {
111         bArmature *arm = ob->data;
112
113         if (ob->type == OB_ARMATURE) {
114                 if (arm->edbo) {
115                         EDIT_ARMATURE_PassList *psl = ((EDIT_ARMATURE_Data *)vedata)->psl;
116                         EDIT_ARMATURE_StorageList *stl = ((EDIT_ARMATURE_Data *)vedata)->stl;
117
118                         int ghost = (ob->dtx & OB_DRAWXRAY) ? 1 : 0;
119
120                         DRWArmaturePasses passes = {
121                             .bone_solid = psl->bone_solid[ghost],
122                             .bone_outline = psl->bone_outline[ghost],
123                             .bone_wire = psl->bone_wire[ghost],
124                             .bone_envelope = psl->bone_envelope[ghost],
125                             .bone_axes = psl->bone_axes,
126                             .relationship_lines = psl->relationship[ghost],
127                         };
128                         DRW_shgroup_armature_edit(ob, passes, stl->g_data->transparent_bones);
129                 }
130         }
131 }
132
133 static void EDIT_ARMATURE_draw_scene(void *vedata)
134 {
135         EDIT_ARMATURE_PassList *psl = ((EDIT_ARMATURE_Data *)vedata)->psl;
136         EDIT_ARMATURE_StorageList *stl = ((EDIT_ARMATURE_Data *)vedata)->stl;
137         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
138         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
139
140         DRW_draw_pass(psl->bone_envelope[0]);
141
142         if (stl->g_data->transparent_bones) {
143                 /* For performance reason, avoid blending on MS target. */
144                 DRW_draw_pass(psl->bone_solid[0]);
145         }
146
147         MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
148
149         if (!stl->g_data->transparent_bones) {
150                 DRW_draw_pass(psl->bone_solid[0]);
151         }
152
153         DRW_draw_pass(psl->bone_outline[0]);
154         DRW_draw_pass(psl->bone_wire[0]);
155         DRW_draw_pass(psl->relationship[0]);
156
157         MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl);
158
159         if (!DRW_pass_is_empty(psl->bone_envelope[1]) ||
160             !DRW_pass_is_empty(psl->bone_solid[1]) ||
161             !DRW_pass_is_empty(psl->bone_outline[1]) ||
162             !DRW_pass_is_empty(psl->bone_wire[1]) ||
163             !DRW_pass_is_empty(psl->relationship[1]))
164         {
165                 if (DRW_state_is_fbo()) {
166                         GPU_framebuffer_bind(dfbl->default_fb);
167                         GPU_framebuffer_clear_depth(dfbl->default_fb, 1.0f);
168                 }
169
170                 DRW_draw_pass(psl->bone_envelope[1]);
171                 DRW_draw_pass(psl->bone_solid[1]);
172                 DRW_draw_pass(psl->bone_outline[1]);
173                 DRW_draw_pass(psl->bone_wire[1]);
174                 DRW_draw_pass(psl->relationship[1]);
175         }
176
177         /* Draw axes with linesmooth and outside of multisample buffer. */
178         DRW_draw_pass(psl->bone_axes);
179 }
180
181 static const DrawEngineDataSize EDIT_ARMATURE_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_ARMATURE_Data);
182
183 DrawEngineType draw_engine_edit_armature_type = {
184         NULL, NULL,
185         N_("EditArmatureMode"),
186         &EDIT_ARMATURE_data_size,
187         NULL,
188         NULL,
189         &EDIT_ARMATURE_cache_init,
190         &EDIT_ARMATURE_cache_populate,
191         NULL,
192         NULL,
193         &EDIT_ARMATURE_draw_scene,
194         NULL,
195         NULL,
196 };