doxygen: add newline after \file
[blender.git] / source / blender / draw / modes / edit_armature_mode.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file
20  * \ingroup draw
21  */
22
23 #include "DRW_engine.h"
24 #include "DRW_render.h"
25
26 #include "DNA_armature_types.h"
27 #include "DNA_view3d_types.h"
28
29 #include "draw_common.h"
30
31 /* *********** LISTS *********** */
32 typedef struct EDIT_ARMATURE_PassList {
33         struct DRWPass *bone_solid[2];
34         struct DRWPass *bone_wire[2];
35         struct DRWPass *bone_outline[2];
36         struct DRWPass *bone_envelope[2];
37         struct DRWPass *bone_axes;
38         struct DRWPass *relationship[2];
39 } EDIT_ARMATURE_PassList;
40
41 typedef struct EDIT_ARMATURE_StorageList {
42         struct EDIT_ARMATURE_PrivateData *g_data;
43 } EDIT_ARMATURE_StorageList;
44
45 typedef struct EDIT_ARMATURE_Data {
46         void *engine_type;
47         DRWViewportEmptyList *fbl;
48         DRWViewportEmptyList *txl;
49         EDIT_ARMATURE_PassList *psl;
50         EDIT_ARMATURE_StorageList *stl;
51 } EDIT_ARMATURE_Data;
52
53 /* *********** STATIC *********** */
54
55 typedef struct EDIT_ARMATURE_PrivateData {
56         bool transparent_bones;
57 } EDIT_ARMATURE_PrivateData; /* Transient data */
58
59 /* *********** FUNCTIONS *********** */
60
61 static void EDIT_ARMATURE_cache_init(void *vedata)
62 {
63         EDIT_ARMATURE_PassList *psl = ((EDIT_ARMATURE_Data *)vedata)->psl;
64         EDIT_ARMATURE_StorageList *stl = ((EDIT_ARMATURE_Data *)vedata)->stl;
65         const DRWContextState *draw_ctx = DRW_context_state_get();
66
67         if (!stl->g_data) {
68                 /* Alloc transient pointers */
69                 stl->g_data = MEM_callocN(sizeof(*stl->g_data), __func__);
70         }
71         stl->g_data->transparent_bones = (draw_ctx->v3d->overlay.arm_flag & V3D_OVERLAY_ARM_TRANSP_BONES) != 0;
72
73         for (int i = 0; i < 2; ++i) {
74                 /* Solid bones */
75                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_BACK;
76                 state |= (stl->g_data->transparent_bones) ? DRW_STATE_BLEND : DRW_STATE_WRITE_DEPTH;
77                 psl->bone_solid[i] = DRW_pass_create("Bone Solid Pass", state);
78
79                 /* Bones Outline */
80                 state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL;
81                 psl->bone_outline[i] = DRW_pass_create("Bone Outline Pass", state);
82
83                 /* Wire bones */
84                 state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND;
85                 psl->bone_wire[i] = DRW_pass_create("Bone Wire Pass", state);
86
87                 /* distance outline around envelope bones */
88                 state = DRW_STATE_ADDITIVE | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_CULL_FRONT;
89                 psl->bone_envelope[i] = DRW_pass_create("Bone Envelope Outline Pass", state);
90
91                 state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL |
92                         DRW_STATE_BLEND | DRW_STATE_WIRE;
93                 psl->relationship[i] = DRW_pass_create("Bone Relationship Pass", state);
94         }
95
96         {
97                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WIRE_SMOOTH | DRW_STATE_BLEND;
98                 psl->bone_axes = DRW_pass_create("Bone Axes Pass", state);
99         }
100 }
101
102 static void EDIT_ARMATURE_cache_populate(void *vedata, Object *ob)
103 {
104         bArmature *arm = ob->data;
105
106         if (ob->type == OB_ARMATURE) {
107                 if (arm->edbo) {
108                         EDIT_ARMATURE_PassList *psl = ((EDIT_ARMATURE_Data *)vedata)->psl;
109                         EDIT_ARMATURE_StorageList *stl = ((EDIT_ARMATURE_Data *)vedata)->stl;
110
111                         int ghost = (ob->dtx & OB_DRAWXRAY) ? 1 : 0;
112
113                         DRWArmaturePasses passes = {
114                             .bone_solid = psl->bone_solid[ghost],
115                             .bone_outline = psl->bone_outline[ghost],
116                             .bone_wire = psl->bone_wire[ghost],
117                             .bone_envelope = psl->bone_envelope[ghost],
118                             .bone_axes = psl->bone_axes,
119                             .relationship_lines = psl->relationship[ghost],
120                         };
121                         DRW_shgroup_armature_edit(ob, passes, stl->g_data->transparent_bones);
122                 }
123         }
124 }
125
126 static void EDIT_ARMATURE_draw_scene(void *vedata)
127 {
128         EDIT_ARMATURE_PassList *psl = ((EDIT_ARMATURE_Data *)vedata)->psl;
129         EDIT_ARMATURE_StorageList *stl = ((EDIT_ARMATURE_Data *)vedata)->stl;
130         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
131         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
132
133         DRW_draw_pass(psl->bone_envelope[0]);
134
135         if (stl->g_data->transparent_bones) {
136                 /* For performance reason, avoid blending on MS target. */
137                 DRW_draw_pass(psl->bone_solid[0]);
138         }
139
140         MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
141
142         if (!stl->g_data->transparent_bones) {
143                 DRW_draw_pass(psl->bone_solid[0]);
144         }
145
146         DRW_draw_pass(psl->bone_outline[0]);
147         DRW_draw_pass(psl->bone_wire[0]);
148         DRW_draw_pass(psl->relationship[0]);
149
150         MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl);
151
152         if (!DRW_pass_is_empty(psl->bone_envelope[1]) ||
153             !DRW_pass_is_empty(psl->bone_solid[1]) ||
154             !DRW_pass_is_empty(psl->bone_outline[1]) ||
155             !DRW_pass_is_empty(psl->bone_wire[1]) ||
156             !DRW_pass_is_empty(psl->relationship[1]))
157         {
158                 if (DRW_state_is_fbo()) {
159                         GPU_framebuffer_bind(dfbl->default_fb);
160                         GPU_framebuffer_clear_depth(dfbl->default_fb, 1.0f);
161                 }
162
163                 DRW_draw_pass(psl->bone_envelope[1]);
164                 DRW_draw_pass(psl->bone_solid[1]);
165                 DRW_draw_pass(psl->bone_outline[1]);
166                 DRW_draw_pass(psl->bone_wire[1]);
167                 DRW_draw_pass(psl->relationship[1]);
168         }
169
170         /* Draw axes with linesmooth and outside of multisample buffer. */
171         DRW_draw_pass(psl->bone_axes);
172 }
173
174 static const DrawEngineDataSize EDIT_ARMATURE_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_ARMATURE_Data);
175
176 DrawEngineType draw_engine_edit_armature_type = {
177         NULL, NULL,
178         N_("EditArmatureMode"),
179         &EDIT_ARMATURE_data_size,
180         NULL,
181         NULL,
182         &EDIT_ARMATURE_cache_init,
183         &EDIT_ARMATURE_cache_populate,
184         NULL,
185         NULL,
186         &EDIT_ARMATURE_draw_scene,
187         NULL,
188         NULL,
189 };