Blender  V3.3
draw_debug.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2018 Blender Foundation. */
3 
10 #include "MEM_guardedalloc.h"
11 
12 #include "DNA_object_types.h"
13 
14 #include "BKE_object.h"
15 
16 #include "BLI_link_utils.h"
17 
18 #include "GPU_immediate.h"
19 #include "GPU_matrix.h"
20 
21 #include "draw_debug.h"
22 #include "draw_manager.h"
23 
24 /* --------- Register --------- */
25 
26 /* Matrix applied to all points before drawing. Could be a stack if needed. */
27 static float g_modelmat[4][4];
28 
30 {
32 }
33 
34 void DRW_debug_modelmat(const float modelmat[4][4])
35 {
36  copy_m4_m4(g_modelmat, modelmat);
37 }
38 
39 void DRW_debug_line_v3v3(const float v1[3], const float v2[3], const float color[4])
40 {
41  DRWDebugLine *line = MEM_mallocN(sizeof(DRWDebugLine), "DRWDebugLine");
42  mul_v3_m4v3(line->pos[0], g_modelmat, v1);
43  mul_v3_m4v3(line->pos[1], g_modelmat, v2);
44  copy_v4_v4(line->color, color);
46 }
47 
48 void DRW_debug_polygon_v3(const float (*v)[3], const int vert_len, const float color[4])
49 {
50  BLI_assert(vert_len > 1);
51 
52  for (int i = 0; i < vert_len; i++) {
53  DRW_debug_line_v3v3(v[i], v[(i + 1) % vert_len], color);
54  }
55 }
56 
57 void DRW_debug_m4(const float m[4][4])
58 {
59  float v0[3] = {0.0f, 0.0f, 0.0f};
60  float v1[3] = {1.0f, 0.0f, 0.0f};
61  float v2[3] = {0.0f, 1.0f, 0.0f};
62  float v3[3] = {0.0f, 0.0f, 1.0f};
63 
64  mul_m4_v3(m, v0);
65  mul_m4_v3(m, v1);
66  mul_m4_v3(m, v2);
67  mul_m4_v3(m, v3);
68 
69  DRW_debug_line_v3v3(v0, v1, (float[4]){1.0f, 0.0f, 0.0f, 1.0f});
70  DRW_debug_line_v3v3(v0, v2, (float[4]){0.0f, 1.0f, 0.0f, 1.0f});
71  DRW_debug_line_v3v3(v0, v3, (float[4]){0.0f, 0.0f, 1.0f, 1.0f});
72 }
73 
74 void DRW_debug_bbox(const BoundBox *bbox, const float color[4])
75 {
76  DRW_debug_line_v3v3(bbox->vec[0], bbox->vec[1], color);
77  DRW_debug_line_v3v3(bbox->vec[1], bbox->vec[2], color);
78  DRW_debug_line_v3v3(bbox->vec[2], bbox->vec[3], color);
79  DRW_debug_line_v3v3(bbox->vec[3], bbox->vec[0], color);
80 
81  DRW_debug_line_v3v3(bbox->vec[4], bbox->vec[5], color);
82  DRW_debug_line_v3v3(bbox->vec[5], bbox->vec[6], color);
83  DRW_debug_line_v3v3(bbox->vec[6], bbox->vec[7], color);
84  DRW_debug_line_v3v3(bbox->vec[7], bbox->vec[4], color);
85 
86  DRW_debug_line_v3v3(bbox->vec[0], bbox->vec[4], color);
87  DRW_debug_line_v3v3(bbox->vec[1], bbox->vec[5], color);
88  DRW_debug_line_v3v3(bbox->vec[2], bbox->vec[6], color);
89  DRW_debug_line_v3v3(bbox->vec[3], bbox->vec[7], color);
90 }
91 
92 void DRW_debug_m4_as_bbox(const float m[4][4], const float color[4], const bool invert)
93 {
94  BoundBox bb;
95  const float min[3] = {-1.0f, -1.0f, -1.0f}, max[3] = {1.0f, 1.0f, 1.0f};
96  float project_matrix[4][4];
97  if (invert) {
98  invert_m4_m4(project_matrix, m);
99  }
100  else {
101  copy_m4_m4(project_matrix, m);
102  }
103 
105  for (int i = 0; i < 8; i++) {
106  mul_project_m4_v3(project_matrix, bb.vec[i]);
107  }
108  DRW_debug_bbox(&bb, color);
109 }
110 
111 void DRW_debug_sphere(const float center[3], const float radius, const float color[4])
112 {
113  float size_mat[4][4];
114  DRWDebugSphere *sphere = MEM_mallocN(sizeof(DRWDebugSphere), "DRWDebugSphere");
115  /* Bake all transform into a Matrix4 */
116  scale_m4_fl(size_mat, radius);
117  copy_m4_m4(sphere->mat, g_modelmat);
118  translate_m4(sphere->mat, center[0], center[1], center[2]);
119  mul_m4_m4m4(sphere->mat, sphere->mat, size_mat);
120 
121  copy_v4_v4(sphere->color, color);
123 }
124 
125 /* --------- Render --------- */
126 
127 static void drw_debug_draw_lines(void)
128 {
130 
131  if (count == 0) {
132  return;
133  }
134 
135  GPUVertFormat *vert_format = immVertexFormat();
136  uint pos = GPU_vertformat_attr_add(vert_format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
137  uint col = GPU_vertformat_attr_add(vert_format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
138 
140 
142 
143  while (DST.debug.lines) {
144  void *next = DST.debug.lines->next;
145 
148 
151 
153  DST.debug.lines = next;
154  }
155  immEnd();
156 
158 }
159 
160 static void drw_debug_draw_spheres(void)
161 {
163 
164  if (count == 0) {
165  return;
166  }
167 
168  float persmat[4][4];
169  DRW_view_persmat_get(NULL, persmat, false);
170 
171  GPUBatch *empty_sphere = DRW_cache_empty_sphere_get();
173  while (DST.debug.spheres) {
174  void *next = DST.debug.spheres->next;
175  float MVP[4][4];
176 
177  mul_m4_m4m4(MVP, persmat, DST.debug.spheres->mat);
178  GPU_batch_uniform_mat4(empty_sphere, "ModelViewProjectionMatrix", MVP);
179  GPU_batch_uniform_4fv(empty_sphere, "color", DST.debug.spheres->color);
180  GPU_batch_draw(empty_sphere);
181 
183  DST.debug.spheres = next;
184  }
185 }
186 
187 void drw_debug_draw(void)
188 {
191 }
192 
193 void drw_debug_init(void)
194 {
196 }
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.cc:3645
#define BLI_assert(a)
Definition: BLI_assert.h:46
void mul_project_m4_v3(const float M[4][4], float vec[3])
Definition: math_matrix.c:820
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:259
void unit_m4(float m[4][4])
Definition: rct.c:1090
void translate_m4(float mat[4][4], float tx, float ty, float tz)
Definition: math_matrix.c:2318
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1287
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:729
void scale_m4_fl(float R[4][4], float scale)
Definition: math_matrix.c:2297
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:77
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:739
MINLINE void copy_v4_v4(float r[4], const float a[4])
unsigned int uint
Definition: BLI_sys_types.h:67
Object is a sort of wrapper for general info.
NSNotificationCenter * center
GPUBatch
Definition: GPU_batch.h:78
#define GPU_batch_uniform_mat4(batch, name, val)
Definition: GPU_batch.h:157
void GPU_batch_program_set_builtin(GPUBatch *batch, eGPUBuiltinShader shader_id)
Definition: gpu_batch.cc:287
void GPU_batch_draw(GPUBatch *batch)
Definition: gpu_batch.cc:223
#define GPU_batch_uniform_4fv(batch, name, val)
Definition: GPU_batch.h:152
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:20
@ GPU_SHADER_3D_UNIFORM_COLOR
Definition: GPU_shader.h:230
@ GPU_SHADER_3D_FLAT_COLOR
Definition: GPU_shader.h:238
@ 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.
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
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:1083
void DRW_debug_modelmat(const float modelmat[4][4])
Definition: draw_debug.c:34
static float g_modelmat[4][4]
Definition: draw_debug.c:27
void DRW_debug_m4_as_bbox(const float m[4][4], const float color[4], const bool invert)
Definition: draw_debug.c:92
void DRW_debug_polygon_v3(const float(*v)[3], const int vert_len, const float color[4])
Definition: draw_debug.c:48
void DRW_debug_m4(const float m[4][4])
Definition: draw_debug.c:57
void drw_debug_draw(void)
Definition: draw_debug.c:187
void DRW_debug_sphere(const float center[3], const float radius, const float color[4])
Definition: draw_debug.c:111
void DRW_debug_line_v3v3(const float v1[3], const float v2[3], const float color[4])
Definition: draw_debug.c:39
static void drw_debug_draw_spheres(void)
Definition: draw_debug.c:160
static void drw_debug_draw_lines(void)
Definition: draw_debug.c:127
void DRW_debug_bbox(const BoundBox *bbox, const float color[4])
Definition: draw_debug.c:74
void DRW_debug_modelmat_reset(void)
Definition: draw_debug.c:29
void drw_debug_init(void)
Definition: draw_debug.c:193
DRWManager DST
Definition: draw_manager.c:104
void DRW_view_persmat_get(const DRWView *view, float mat[4][4], bool inverse)
uint pos
uint col
int count
CCL_NAMESPACE_BEGIN ccl_device float invert(float color, float factor)
Definition: invert.h:8
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
static ulong * next
#define min(a, b)
Definition: sort.c:35
float vec[8][3]
float pos[2][3]
Definition: draw_manager.h:500
struct DRWDebugLine * next
Definition: draw_manager.h:499
float color[4]
Definition: draw_manager.h:501
float mat[4][4]
Definition: draw_manager.h:506
struct DRWDebugSphere * next
Definition: draw_manager.h:505
float color[4]
Definition: draw_manager.h:507
struct DRWManager::@314 debug
DRWDebugSphere * spheres
Definition: draw_manager.h:654
DRWDebugLine * lines
Definition: draw_manager.h:653
float max