Blender  V2.93
draw_debug.c
Go to the documentation of this file.
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 2018, Blender Foundation.
17  */
18 
25 #include "MEM_guardedalloc.h"
26 
27 #include "DNA_object_types.h"
28 
29 #include "BKE_object.h"
30 
31 #include "BLI_link_utils.h"
32 
33 #include "GPU_immediate.h"
34 
35 #include "draw_debug.h"
36 #include "draw_manager.h"
37 
38 /* --------- Register --------- */
39 
40 /* Matrix applied to all points before drawing. Could be a stack if needed. */
41 static float g_modelmat[4][4];
42 
44 {
46 }
47 
48 void DRW_debug_modelmat(const float modelmat[4][4])
49 {
50  copy_m4_m4(g_modelmat, modelmat);
51 }
52 
53 void DRW_debug_line_v3v3(const float v1[3], const float v2[3], const float color[4])
54 {
55  DRWDebugLine *line = MEM_mallocN(sizeof(DRWDebugLine), "DRWDebugLine");
56  mul_v3_m4v3(line->pos[0], g_modelmat, v1);
57  mul_v3_m4v3(line->pos[1], g_modelmat, v2);
58  copy_v4_v4(line->color, color);
60 }
61 
62 void DRW_debug_polygon_v3(const float (*v)[3], const int vert_len, const float color[4])
63 {
64  BLI_assert(vert_len > 1);
65 
66  for (int i = 0; i < vert_len; i++) {
67  DRW_debug_line_v3v3(v[i], v[(i + 1) % vert_len], color);
68  }
69 }
70 
71 /* NOTE: g_modelmat is still applied on top. */
72 void DRW_debug_m4(const float m[4][4])
73 {
74  float v0[3] = {0.0f, 0.0f, 0.0f};
75  float v1[3] = {1.0f, 0.0f, 0.0f};
76  float v2[3] = {0.0f, 1.0f, 0.0f};
77  float v3[3] = {0.0f, 0.0f, 1.0f};
78 
79  mul_m4_v3(m, v0);
80  mul_m4_v3(m, v1);
81  mul_m4_v3(m, v2);
82  mul_m4_v3(m, v3);
83 
84  DRW_debug_line_v3v3(v0, v1, (float[4]){1.0f, 0.0f, 0.0f, 1.0f});
85  DRW_debug_line_v3v3(v0, v2, (float[4]){0.0f, 1.0f, 0.0f, 1.0f});
86  DRW_debug_line_v3v3(v0, v3, (float[4]){0.0f, 0.0f, 1.0f, 1.0f});
87 }
88 
89 void DRW_debug_bbox(const BoundBox *bbox, const float color[4])
90 {
91  DRW_debug_line_v3v3(bbox->vec[0], bbox->vec[1], color);
92  DRW_debug_line_v3v3(bbox->vec[1], bbox->vec[2], color);
93  DRW_debug_line_v3v3(bbox->vec[2], bbox->vec[3], color);
94  DRW_debug_line_v3v3(bbox->vec[3], bbox->vec[0], color);
95 
96  DRW_debug_line_v3v3(bbox->vec[4], bbox->vec[5], color);
97  DRW_debug_line_v3v3(bbox->vec[5], bbox->vec[6], color);
98  DRW_debug_line_v3v3(bbox->vec[6], bbox->vec[7], color);
99  DRW_debug_line_v3v3(bbox->vec[7], bbox->vec[4], color);
100 
101  DRW_debug_line_v3v3(bbox->vec[0], bbox->vec[4], color);
102  DRW_debug_line_v3v3(bbox->vec[1], bbox->vec[5], color);
103  DRW_debug_line_v3v3(bbox->vec[2], bbox->vec[6], color);
104  DRW_debug_line_v3v3(bbox->vec[3], bbox->vec[7], color);
105 }
106 
107 void DRW_debug_m4_as_bbox(const float m[4][4], const float color[4], const bool invert)
108 {
109  BoundBox bb;
110  const float min[3] = {-1.0f, -1.0f, -1.0f}, max[3] = {1.0f, 1.0f, 1.0f};
111  float project_matrix[4][4];
112  if (invert) {
113  invert_m4_m4(project_matrix, m);
114  }
115  else {
116  copy_m4_m4(project_matrix, m);
117  }
118 
120  for (int i = 0; i < 8; i++) {
121  mul_project_m4_v3(project_matrix, bb.vec[i]);
122  }
123  DRW_debug_bbox(&bb, color);
124 }
125 
126 void DRW_debug_sphere(const float center[3], const float radius, const float color[4])
127 {
128  float size_mat[4][4];
129  DRWDebugSphere *sphere = MEM_mallocN(sizeof(DRWDebugSphere), "DRWDebugSphere");
130  /* Bake all transform into a Matrix4 */
131  scale_m4_fl(size_mat, radius);
132  copy_m4_m4(sphere->mat, g_modelmat);
133  translate_m4(sphere->mat, center[0], center[1], center[2]);
134  mul_m4_m4m4(sphere->mat, sphere->mat, size_mat);
135 
136  copy_v4_v4(sphere->color, color);
138 }
139 
140 /* --------- Render --------- */
141 
142 static void drw_debug_draw_lines(void)
143 {
145 
146  if (count == 0) {
147  return;
148  }
149 
150  GPUVertFormat *vert_format = immVertexFormat();
151  uint pos = GPU_vertformat_attr_add(vert_format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
152  uint col = GPU_vertformat_attr_add(vert_format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
153 
155 
157 
158  while (DST.debug.lines) {
159  void *next = DST.debug.lines->next;
160 
163 
166 
168  DST.debug.lines = next;
169  }
170  immEnd();
171 
173 }
174 
175 static void drw_debug_draw_spheres(void)
176 {
178 
179  if (count == 0) {
180  return;
181  }
182 
183  float one = 1.0f;
184  GPUVertFormat vert_format = {0};
186  &vert_format, "InstanceModelMatrix", GPU_COMP_F32, 16, GPU_FETCH_FLOAT);
187  uint col = GPU_vertformat_attr_add(&vert_format, "color", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
188  uint siz = GPU_vertformat_attr_add(&vert_format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
189 
191 
193 
194  int v = 0;
195  while (DST.debug.spheres) {
196  void *next = DST.debug.spheres->next;
197 
200  GPU_vertbuf_attr_set(inst_vbo, siz, v, &one);
201  v++;
202 
204  DST.debug.spheres = next;
205  }
206 
207  GPUBatch *empty_sphere = DRW_cache_empty_sphere_get();
208 
209  GPUBatch *draw_batch = GPU_batch_create(GPU_PRIM_LINES, empty_sphere->verts[0], NULL);
210  GPU_batch_instbuf_set(draw_batch, inst_vbo, true);
212 
213  float persmat[4][4];
214  DRW_view_persmat_get(NULL, persmat, false);
215  GPU_batch_uniform_mat4(draw_batch, "ViewProjectionMatrix", persmat);
216 
217  GPU_batch_draw(draw_batch);
218  GPU_batch_discard(draw_batch);
219 }
220 
221 void drw_debug_draw(void)
222 {
225 }
226 
227 void drw_debug_init(void)
228 {
230 }
General operations, lookup, etc. for blender objects.
void BKE_boundbox_init_from_minmax(struct BoundBox *bb, const float min[3], const float max[3])
Definition: object.c:3778
#define BLI_assert(a)
Definition: BLI_assert.h:58
void mul_project_m4_v3(const float M[4][4], float vec[3])
Definition: math_matrix.c:824
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:262
void unit_m4(float m[4][4])
Definition: rct.c:1140
void translate_m4(float mat[4][4], float tx, float ty, float tz)
Definition: math_matrix.c:2325
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1278
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:732
void scale_m4_fl(float R[4][4], float scale)
Definition: math_matrix.c:2309
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:95
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:742
MINLINE void copy_v4_v4(float r[4], const float a[4])
unsigned int uint
Definition: BLI_sys_types.h:83
Object is a sort of wrapper for general info.
NSNotificationCenter * center
GPUBatch
Definition: GPU_batch.h:93
void GPU_batch_discard(GPUBatch *)
Definition: gpu_batch.cc:127
#define GPU_batch_create(prim, verts, elem)
Definition: GPU_batch.h:107
#define GPU_batch_uniform_mat4(batch, name, val)
Definition: GPU_batch.h:147
void GPU_batch_program_set_builtin(GPUBatch *batch, eGPUBuiltinShader shader_id)
Definition: gpu_batch.cc:299
void GPU_batch_instbuf_set(GPUBatch *, GPUVertBuf *, bool own_vbo)
Definition: gpu_batch.cc:141
void GPU_batch_draw(GPUBatch *batch)
Definition: gpu_batch.cc:234
void immAttr4fv(uint attr_id, const float data[4])
void immUnbindProgram(void)
void immBindBuiltinProgram(eGPUBuiltinShader shader_id)
GPUVertFormat * immVertexFormat(void)
void immVertex3fv(uint attr_id, const float data[3])
void immBegin(GPUPrimType, uint vertex_len)
void immEnd(void)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
@ GPU_PRIM_LINES
Definition: GPU_primitive.h:36
@ GPU_SHADER_3D_FLAT_COLOR
Definition: GPU_shader.h:208
@ GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE
Definition: GPU_shader.h:368
#define GPU_vertbuf_create_with_format(format)
struct GPUVertBuf GPUVertBuf
void GPU_vertbuf_data_alloc(GPUVertBuf *, uint v_len)
void GPU_vertbuf_attr_set(GPUVertBuf *, uint a_idx, uint v_idx, const void *data)
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
GPUBatch * DRW_cache_empty_sphere_get(void)
Definition: draw_cache.c:1081
void DRW_debug_modelmat(const float modelmat[4][4])
Definition: draw_debug.c:48
static float g_modelmat[4][4]
Definition: draw_debug.c:41
void DRW_debug_m4_as_bbox(const float m[4][4], const float color[4], const bool invert)
Definition: draw_debug.c:107
void DRW_debug_polygon_v3(const float(*v)[3], const int vert_len, const float color[4])
Definition: draw_debug.c:62
void DRW_debug_m4(const float m[4][4])
Definition: draw_debug.c:72
void drw_debug_draw(void)
Definition: draw_debug.c:221
void DRW_debug_sphere(const float center[3], const float radius, const float color[4])
Definition: draw_debug.c:126
void DRW_debug_line_v3v3(const float v1[3], const float v2[3], const float color[4])
Definition: draw_debug.c:53
static void drw_debug_draw_spheres(void)
Definition: draw_debug.c:175
static void drw_debug_draw_lines(void)
Definition: draw_debug.c:142
void DRW_debug_bbox(const BoundBox *bbox, const float color[4])
Definition: draw_debug.c:89
void DRW_debug_modelmat_reset(void)
Definition: draw_debug.c:43
void drw_debug_init(void)
Definition: draw_debug.c:227
DRWManager DST
Definition: draw_manager.c:111
void DRW_view_persmat_get(const DRWView *view, float mat[4][4], bool inverse)
GPUVertBuf * inst_vbo
Definition: drawnode.c:3781
uint pos
uint col
int count
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static ulong * next
#define min(a, b)
Definition: sort.c:51
float vec[8][3]
float pos[2][3]
Definition: draw_manager.h:471
struct DRWDebugLine * next
Definition: draw_manager.h:470
float color[4]
Definition: draw_manager.h:472
float mat[4][4]
Definition: draw_manager.h:477
struct DRWDebugSphere * next
Definition: draw_manager.h:476
float color[4]
Definition: draw_manager.h:478
DRWDebugSphere * spheres
Definition: draw_manager.h:581
DRWDebugLine * lines
Definition: draw_manager.h:580
struct DRWManager::@296 debug
CCL_NAMESPACE_BEGIN ccl_device float invert(float color, float factor)
Definition: svm_invert.h:19
float max