Welcome to mirror list, hosted at ThFree Co, Russian Federation.

draw_armature.c « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68d1b3d7ea40a4bb310b45fd5396c873a9ed7e8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * Copyright 2016, Blender Foundation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Blender Institute
 *
 */

/** \file draw_armature.c
 *  \ingroup draw
 */

#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "DNA_anim_types.h"
#include "DNA_armature_types.h"
#include "DNA_constraint_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_view3d_types.h"
#include "DNA_object_types.h"

#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_dlrbTree.h"
#include "BLI_utildefines.h"

#include "BKE_animsys.h"
#include "BKE_action.h"
#include "BKE_armature.h"
#include "BKE_global.h"
#include "BKE_modifier.h"
#include "BKE_nla.h"
#include "BKE_curve.h"

#include "BIF_gl.h"
#include "BIF_glutil.h"

#include "ED_armature.h"
#include "ED_keyframes_draw.h"

#include "UI_resources.h"

#include "draw_mode_pass.h"

#define BONE_VAR(eBone, pchan, var) ((eBone) ? (eBone->var) : (pchan->var))
#define BONE_FLAG(eBone, pchan) ((eBone) ? (eBone->flag) : (pchan->bone->flag))

/* *************** Armature Drawing - Coloring API ***************************** */

static float colorBoneSolid[4];
static float colorTextHi[4];
static float colorText[4];
static float colorVertexSelect[4];
static float colorVertex[4];

static const float *constColor;

static void update_color(const float const_color[4])
{
	constColor = const_color;

	UI_GetThemeColor4fv(TH_BONE_SOLID, colorBoneSolid);
	UI_GetThemeColor4fv(TH_TEXT_HI, colorTextHi);
	UI_GetThemeColor4fv(TH_TEXT, colorText);
	UI_GetThemeColor4fv(TH_VERTEX_SELECT, colorVertexSelect);
	UI_GetThemeColor4fv(TH_VERTEX, colorVertex);
}

static const float *get_bone_solid_color(EditBone *eBone, bPoseChannel *pchan, bArmature *arm)
{
	if (constColor)
		return colorBoneSolid;

	/* Edit Mode */
	if (eBone) {
		if (eBone->flag & BONE_SELECTED)
			return colorVertexSelect;
	}

	return colorBoneSolid;
}

static const float *get_bone_wire_color(EditBone *eBone, bPoseChannel *pchan, bArmature *arm)
{
	if (constColor)
		return constColor;

	if (eBone) {
		if (eBone->flag & BONE_SELECTED)
			return colorVertexSelect;
	}

	return colorVertex;
}

/* *************** Armature drawing, helper calls for parts ******************* */

static void draw_bone_update_disp_matrix(EditBone *eBone, bPoseChannel *pchan, int drawtype)
{
	float s[4][4], ebmat[4][4];
	float length;
	float (*bone_mat)[4];
	float (*disp_mat)[4];
	float (*disp_tail_mat)[4];

	/* TODO : This should be moved to depsgraph or armature refresh
	 * and not be tight to the draw pass creation.
	 * This would refresh armature without invalidating the draw cache */
	if (pchan) {
		length = pchan->bone->length;
		bone_mat = pchan->pose_mat;
		disp_mat = pchan->disp_mat;
		disp_tail_mat = pchan->disp_tail_mat;
	}
	else {
		eBone->length = len_v3v3(eBone->tail, eBone->head);
		ED_armature_ebone_to_mat4(eBone, ebmat);

		length = eBone->length;
		bone_mat = ebmat;
		disp_mat = eBone->disp_mat;
		disp_tail_mat = eBone->disp_tail_mat;
	}

	if (pchan && pchan->custom) {
		/* TODO */
	}
	else if (drawtype == ARM_ENVELOPE) {
		/* TODO */
	}
	else if (drawtype == ARM_LINE) {
		/* TODO */
	}
	else if (drawtype == ARM_WIRE) {
		/* TODO */
	}
	else if (drawtype == ARM_B_BONE) {
		/* TODO */
	}
	else {
		scale_m4_fl(s, length);
		mul_m4_m4m4(disp_mat, bone_mat, s);
		copy_m4_m4(disp_tail_mat, disp_mat);
		translate_m4(disp_tail_mat, 0.0f, 1.0f, 0.0f);
	}
}

static void draw_axes(EditBone *eBone, bPoseChannel *pchan)
{
	const float *col = (constColor) ? constColor :
	                   (BONE_FLAG(eBone, pchan) & BONE_SELECTED) ? colorTextHi : colorText;

	DRW_shgroup_bone_axes(BONE_VAR(eBone, pchan, disp_tail_mat), col);
}

static void draw_points(EditBone *eBone, bPoseChannel *pchan, bArmature *arm)
{
	const float *col_solid_root = colorBoneSolid;
	const float *col_solid_tail = colorBoneSolid;
	const float *col_wire_root = (constColor) ? constColor : colorVertex;
	const float *col_wire_tail = (constColor) ? constColor : colorVertex;

	/* Edit bone points can be selected */
	if (eBone) {
		if (eBone->flag & BONE_ROOTSEL) {
			col_solid_root = colorVertexSelect;
			col_wire_root = colorVertexSelect;
		}
		if (eBone->flag & BONE_TIPSEL) {
			col_solid_tail = colorVertexSelect;
			col_wire_tail = colorVertexSelect;
		}
	}

	/*	Draw root point if we are not connected and parent are not hidden */
	if ((BONE_FLAG(eBone, pchan) & BONE_CONNECTED) == 0) {
		if (eBone) {
			if (!((eBone->parent) && !EBONE_VISIBLE(arm, eBone->parent))) {
				DRW_shgroup_bone_point_solid(eBone->disp_mat, col_solid_root);
				DRW_shgroup_bone_point_wire(eBone->disp_mat, col_wire_root);
			}
		}
		else {
			Bone *bone = pchan->bone;
			if (!((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)))) {
				DRW_shgroup_bone_point_solid(pchan->disp_mat, col_solid_root);
				DRW_shgroup_bone_point_wire(pchan->disp_mat, col_wire_root);
			}
		}
	}

	/*	Draw tip point */
	DRW_shgroup_bone_point_solid(BONE_VAR(eBone, pchan, disp_tail_mat), col_solid_tail);
	DRW_shgroup_bone_point_wire(BONE_VAR(eBone, pchan, disp_tail_mat), col_wire_tail);
}

static void draw_bone_custom_shape(EditBone *eBone, bPoseChannel *pchan, bArmature *arm)
{
}

static void draw_bone_envelope(EditBone *eBone, bPoseChannel *pchan, bArmature *arm)
{
}

static void draw_bone_line(EditBone *eBone, bPoseChannel *pchan, bArmature *arm)
{
}

static void draw_bone_wire(EditBone *eBone, bPoseChannel *pchan, bArmature *arm)
{
}

static void draw_bone_box(EditBone *eBone, bPoseChannel *pchan, bArmature *arm)
{
}

static void draw_bone_octahedral(EditBone *eBone, bPoseChannel *pchan, bArmature *arm)
{
	const float *col_solid = get_bone_solid_color(eBone, pchan, arm);
	const float *col_wire = get_bone_wire_color(eBone, pchan, arm);

	DRW_shgroup_bone_octahedral_solid(BONE_VAR(eBone, pchan, disp_mat), col_solid);
	DRW_shgroup_bone_octahedral_wire(BONE_VAR(eBone, pchan, disp_mat), col_wire);

	draw_points(eBone, pchan, arm);
}

void draw_armature_edit(Object *ob)
{
	EditBone *eBone;
	bArmature *arm = ob->data;
	unsigned int index;

	update_color(NULL);

	for (eBone = arm->edbo->first, index = 0; eBone; eBone = eBone->next, index++) {
		if (eBone->layer & arm->layer) {
			if ((eBone->flag & BONE_HIDDEN_A) == 0) {

				draw_bone_update_disp_matrix(eBone, NULL, arm->drawtype);

				if (arm->drawtype == ARM_ENVELOPE)
					draw_bone_envelope(eBone, NULL, arm);
				else if (arm->drawtype == ARM_LINE)
					draw_bone_line(eBone, NULL, arm);
				else if (arm->drawtype == ARM_WIRE)
					draw_bone_wire(eBone, NULL, arm);
				else if (arm->drawtype == ARM_B_BONE)
					draw_bone_box(eBone, NULL, arm);
				else
					draw_bone_octahedral(eBone, NULL, arm);

				/*	Draw additional axes */
				if (arm->flag & ARM_DRAWAXES)
					draw_axes(eBone, NULL);
			}
		}
	}
}

/* if const_color is NULL do pose mode coloring */
void draw_armature_pose(Object *ob, const float const_color[4])
{
	bArmature *arm = ob->data;
	bPoseChannel *pchan;
	Bone *bone;

	update_color(const_color);

	/* We can't safely draw non-updated pose, might contain NULL bone pointers... */
	if (ob->pose->flag & POSE_RECALC) {
		BKE_pose_rebuild(ob, arm);
	}

	/* being set below */
	arm->layer_used = 0;

	for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
		bone = pchan->bone;
		arm->layer_used |= bone->layer;

		/* bone must be visible */
		if ((bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) {
			if (bone->layer & arm->layer) {

				draw_bone_update_disp_matrix(NULL, pchan, arm->drawtype);

				if ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM))
					draw_bone_custom_shape(NULL, pchan, arm);
				else if (arm->drawtype == ARM_ENVELOPE)
					draw_bone_envelope(NULL, pchan, arm);
				else if (arm->drawtype == ARM_LINE)
					draw_bone_line(NULL, pchan, arm);
				else if (arm->drawtype == ARM_WIRE)
					draw_bone_wire(NULL, pchan, arm);
				else if (arm->drawtype == ARM_B_BONE)
					draw_bone_box(NULL, pchan, arm);
				else
					draw_bone_octahedral(NULL, pchan, arm);

				/*	Draw additional axes */
				if (arm->flag & ARM_DRAWAXES)
					draw_axes(NULL, pchan);
			}
		}
	}
}