Blender V5.3
gpu_immediate_util.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstring>
12
13#include "DNA_userdef_types.h"
14
16#include "BLI_math_vector_c.hh"
17#include "BLI_rect.hh"
18#include "BLI_utildefines.hh"
19
20#include "GPU_immediate.hh"
21
22#include "UI_resources.hh"
23
24namespace blender {
25
26namespace gpu {
27
28static void imm_draw_circle_partial_aspect(const GPUPrimType prim_type,
29 const uint pos,
30 const float x,
31 const float y,
32 const float2 radius,
33 const int nsegments,
34 const float start,
35 const float sweep)
36{
37 /* shift & reverse angle, increase 'nsegments' to match gluPartialDisk */
38 const float angle_start = -DEG2RADF(start) + float(M_PI_2);
39 const float angle_end = -(DEG2RADF(sweep) - angle_start);
40 immBegin(prim_type, nsegments + 1);
41 for (int i = 0; i < nsegments + 1; i++) {
42 const float angle = interpf(angle_start, angle_end, (float(i) / float(nsegments)));
43 const float angle_sin = sinf(angle);
44 const float angle_cos = cosf(angle);
45 immVertex2f(pos, x + radius.x * angle_cos, y + radius.y * angle_sin);
46 }
47 immEnd();
48}
49
51 const uint pos,
52 const float x,
53 const float y,
54 const float z,
55 const float2 radius,
56 const int nsegments,
57 const float start,
58 const float sweep)
59{
60 /* shift & reverse angle, increase 'nsegments' to match gluPartialDisk */
61 const float angle_start = -DEG2RADF(start) + float(M_PI / 2);
62 const float angle_end = -(DEG2RADF(sweep) - angle_start);
63 immBegin(prim_type, nsegments + 1);
64 for (int i = 0; i < nsegments + 1; i++) {
65 const float angle = interpf(angle_start, angle_end, (float(i) / float(nsegments)));
66 const float angle_sin = sinf(angle);
67 const float angle_cos = cosf(angle);
68 immVertex3f(pos, x + radius.x * angle_cos, y + radius.y * angle_sin, z);
69 }
70 immEnd();
71}
72
74 const float x,
75 const float y,
76 const float2 radius,
77 const int nsegments,
78 const float start,
79 const float sweep)
80{
81 imm_draw_circle_partial_aspect(GPU_PRIM_LINE_STRIP, pos, x, y, radius, nsegments, start, sweep);
82}
83
85 const float x,
86 const float y,
87 const float z,
88 const float2 radius,
89 const int nsegments,
90 const float start,
91 const float sweep)
92{
94 GPU_PRIM_LINE_STRIP, pos, x, y, z, radius, nsegments, start, sweep);
95}
96
98 const float x,
99 const float y,
100 const float2 radius,
101 const float2 corner_radius,
102 const int nsegments)
103{
104 BLI_assert(corner_radius.x >= 0.0f && corner_radius.x <= radius.x);
105 BLI_assert(corner_radius.y >= 0.0f && corner_radius.y <= radius.y);
106
107 const float x_min = x - radius.x;
108 const float x_max = x + radius.x;
109 const float y_min = y - radius.y;
110 const float y_max = y + radius.y;
111 const float x_corner_min = x - radius.x + corner_radius.x;
112 const float x_corner_max = x + radius.x - corner_radius.x;
113 const float y_corner_min = y - radius.y + corner_radius.y;
114 const float y_corner_max = y + radius.y - corner_radius.y;
115
116 if (corner_radius.x == 0.0f && corner_radius.y == 0.0f) {
117 imm_draw_box_wire_2d(pos, x_min, y_min, x_max, y_max);
118 return;
119 }
120
121 if (corner_radius == radius) {
122 imm_draw_circle_wire_aspect_2d(pos, x, y, radius.x, radius.y, nsegments);
123 return;
124 }
125
126 /* Draws four corners. */
128 pos, x_corner_min, y_corner_min, corner_radius, nsegments, 180.0f, 90.0f);
130 pos, x_corner_max, y_corner_max, corner_radius, nsegments, 0, 90.0f);
132 pos, x_corner_max, y_corner_min, corner_radius, nsegments, 90.0f, 90.0f);
134 pos, x_corner_min, y_corner_max, corner_radius, nsegments, -90.0f, 90.0f);
135
136 /* Draws four edges. */
138 immVertex2f(pos, x_min, y_corner_min);
139 immVertex2f(pos, x_min, y_corner_max);
140
141 immVertex2f(pos, x_max, y_corner_max);
142 immVertex2f(pos, x_max, y_corner_min);
143
144 immVertex2f(pos, x_corner_min, y_min);
145 immVertex2f(pos, x_corner_max, y_min);
146
147 immVertex2f(pos, x_corner_max, y_max);
148 immVertex2f(pos, x_corner_min, y_max);
149 immEnd();
150}
151
153 const float x,
154 const float y,
155 const float2 radius,
156 const float2 corner_radius,
157 const int nsegments)
158{
159 BLI_assert(corner_radius.x >= 0.0f && corner_radius.x <= radius.x);
160 BLI_assert(corner_radius.y >= 0.0f && corner_radius.y <= radius.y);
161
162 /* Min and max values of x-coordinate of the rounded box. */
163 const float x_min = x - radius.x;
164 const float x_max = x + radius.x;
165 /* Min and max values of the x-coordinate of the center of the rounded corners. */
166 const float x_corner_min = x - radius.x + corner_radius.x;
167 const float x_corner_max = x + radius.x - corner_radius.x;
168
169 if (corner_radius.x == 0.0f && corner_radius.y == 0.0f) {
170 imm_draw_box_wire_3d(pos, x_min, y - radius.y, x_max, y + radius.y);
171 return;
172 }
173
174 if (corner_radius == radius) {
175 imm_draw_circle_wire_aspect_3d(pos, x, y, radius.x, radius.y, nsegments);
176 return;
177 }
178
179 /* Draws four corners. */
181 x_corner_min,
182 y - radius.y + corner_radius.y,
183 0.0f,
184 corner_radius,
185 nsegments,
186 180.0f,
187 90.0f);
189 x_corner_max,
190 y + radius.y - corner_radius.y,
191 0.0f,
192 corner_radius,
193 nsegments,
194 0.0f,
195 90.0f);
197 x_corner_max,
198 y - radius.y + corner_radius.y,
199 0.0f,
200 corner_radius,
201 nsegments,
202 90.0f,
203 90.0f);
205 x_corner_min,
206 y + radius.y - corner_radius.y,
207 0.0f,
208 corner_radius,
209 nsegments,
210 -90.0f,
211 90.0f);
212
213 /* Draws four edges. */
215 immVertex3f(pos, x_min, y - radius.y + corner_radius.y, 0.0f);
216 immVertex3f(pos, x_min, y + radius.y - corner_radius.y, 0.0f);
217
218 immVertex3f(pos, x_max, y + radius.y - corner_radius.y, 0.0f);
219 immVertex3f(pos, x_max, y - radius.y + corner_radius.y, 0.0f);
220
221 immVertex3f(pos, x_corner_min, y - radius.y, 0.0f);
222 immVertex3f(pos, x_corner_max, y - radius.y, 0.0f);
223
224 immVertex3f(pos, x_corner_max, y + radius.y, 0.0f);
225 immVertex3f(pos, x_corner_min, y + radius.y, 0.0f);
226 immEnd();
227}
228
229} // namespace gpu
230
231static const float cube_coords[8][3] = {
232 {-1, -1, -1},
233 {-1, -1, +1},
234 {-1, +1, -1},
235 {-1, +1, +1},
236 {+1, -1, -1},
237 {+1, -1, +1},
238 {+1, +1, -1},
239 {+1, +1, +1},
240};
241static const int cube_quad_index[6][4] = {
242 {0, 1, 3, 2},
243 {0, 2, 6, 4},
244 {0, 4, 5, 1},
245 {1, 5, 7, 3},
246 {2, 3, 7, 6},
247 {4, 6, 7, 5},
248};
249static const int cube_line_index[12][2] = {
250 {0, 1},
251 {0, 2},
252 {0, 4},
253 {1, 3},
254 {1, 5},
255 {2, 3},
256 {2, 6},
257 {3, 7},
258 {4, 5},
259 {4, 6},
260 {5, 7},
261 {6, 7},
262};
263
264void immRectf(uint pos, float x1, float y1, float x2, float y2)
265{
267 immVertex2f(pos, x1, y1);
268 immVertex2f(pos, x2, y1);
269 immVertex2f(pos, x2, y2);
270 immVertex2f(pos, x1, y2);
271 immEnd();
272}
273
274void immRecti(uint pos, int x1, int y1, int x2, int y2)
275{
277 immVertex2i(pos, x1, y1);
278 immVertex2i(pos, x2, y1);
279 immVertex2i(pos, x2, y2);
280 immVertex2i(pos, x1, y2);
281 immEnd();
282}
283
284void immRectf_fast(uint pos, float x1, float y1, float x2, float y2)
285{
286 immVertex2f(pos, x1, y1);
287 immVertex2f(pos, x2, y1);
288 immVertex2f(pos, x2, y2);
289
290 immVertex2f(pos, x1, y1);
291 immVertex2f(pos, x2, y2);
292 immVertex2f(pos, x1, y2);
293}
294
296 uint pos, uint col, float x1, float y1, float x2, float y2, const float color[4])
297{
298 immAttr4fv(col, color);
299 immVertex2f(pos, x1, y1);
300 immAttr4fv(col, color);
301 immVertex2f(pos, x2, y1);
302 immAttr4fv(col, color);
303 immVertex2f(pos, x2, y2);
304
305 immAttr4fv(col, color);
306 immVertex2f(pos, x1, y1);
307 immAttr4fv(col, color);
308 immVertex2f(pos, x2, y2);
309 immAttr4fv(col, color);
310 immVertex2f(pos, x1, y2);
311}
312
314 uint pos, uint col, int x1, int y1, int x2, int y2, const float color[4])
315{
316 immAttr4fv(col, color);
317 immVertex2i(pos, x1, y1);
318 immAttr4fv(col, color);
319 immVertex2i(pos, x2, y1);
320 immAttr4fv(col, color);
321 immVertex2i(pos, x2, y2);
322
323 immAttr4fv(col, color);
324 immVertex2i(pos, x1, y1);
325 immAttr4fv(col, color);
326 immVertex2i(pos, x2, y2);
327 immAttr4fv(col, color);
328 immVertex2i(pos, x1, y2);
329}
330
331void immRectf_with_texco(const uint pos, const uint tex_coord, const rctf &p, const rctf &uv)
332{
334 immAttr2f(tex_coord, uv.xmin, uv.ymin);
335 immVertex2f(pos, p.xmin, p.ymin);
336
337 immAttr2f(tex_coord, uv.xmin, uv.ymax);
338 immVertex2f(pos, p.xmin, p.ymax);
339
340 immAttr2f(tex_coord, uv.xmax, uv.ymax);
341 immVertex2f(pos, p.xmax, p.ymax);
342
343 immAttr2f(tex_coord, uv.xmax, uv.ymin);
344 immVertex2f(pos, p.xmax, p.ymin);
345 immEnd();
346}
347
348#if 0 /* more complete version in case we want that */
349void immRecti_complete(int x1, int y1, int x2, int y2, const float color[4])
350{
351 GPUVertFormat *format = immVertexFormat();
352 uint pos = add_attr(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
354 immUniformColor4fv(color);
355 immRectf(pos, x1, y1, x2, y2);
357}
358#endif
359
361{
362 immUniformColor3ub(((x) & 0xFF), (((x) >> 8) & 0xFF), (((x) >> 16) & 0xFF));
363}
364
365static void imm_draw_circle(GPUPrimType prim_type,
366 const uint shdr_pos,
367 float x,
368 float y,
369 float radius_x,
370 float radius_y,
371 int nsegments)
372{
373 if (prim_type == GPU_PRIM_LINE_LOOP) {
374 /* NOTE(Metal/AMD): For small primitives, line list more efficient than line strip.. */
375 immBegin(GPU_PRIM_LINES, nsegments * 2);
376
377 immVertex2f(shdr_pos, x + (radius_x * cosf(0.0f)), y + (radius_y * sinf(0.0f)));
378 for (int i = 1; i < nsegments; i++) {
379 const float angle = float(2 * M_PI) * (float(i) / float(nsegments));
380 immVertex2f(shdr_pos, x + (radius_x * cosf(angle)), y + (radius_y * sinf(angle)));
381 immVertex2f(shdr_pos, x + (radius_x * cosf(angle)), y + (radius_y * sinf(angle)));
382 }
383 immVertex2f(shdr_pos, x + (radius_x * cosf(0.0f)), y + (radius_y * sinf(0.0f)));
384 immEnd();
385 }
386 else {
387 immBegin(prim_type, nsegments);
388 for (int i = 0; i < nsegments; i++) {
389 const float angle = float(2 * M_PI) * (float(i) / float(nsegments));
390 immVertex2f(shdr_pos, x + (radius_x * cosf(angle)), y + (radius_y * sinf(angle)));
391 }
392 immEnd();
393 }
394}
395
396void imm_draw_circle_wire_2d(uint shdr_pos, float x, float y, float radius, int nsegments)
397{
398 imm_draw_circle(GPU_PRIM_LINE_LOOP, shdr_pos, x, y, radius, radius, nsegments);
399}
400
401void imm_draw_circle_fill_2d(uint shdr_pos, float x, float y, float radius, int nsegments)
402{
403 imm_draw_circle(GPU_PRIM_TRI_FAN, shdr_pos, x, y, radius, radius, nsegments);
404}
405
407 uint shdr_pos, float x, float y, float radius_x, float radius_y, int nsegments)
408{
409 imm_draw_circle(GPU_PRIM_LINE_LOOP, shdr_pos, x, y, radius_x, radius_y, nsegments);
410}
411
413 uint shdr_pos, float x, float y, float radius_x, float radius_y, int nsegments)
414{
415 imm_draw_circle(GPU_PRIM_TRI_FAN, shdr_pos, x, y, radius_x, radius_y, nsegments);
416}
417
419 uint pos,
420 float x,
421 float y,
422 float radius,
423 int nsegments,
424 float start,
425 float sweep)
426{
427 /* shift & reverse angle, increase 'nsegments' to match gluPartialDisk */
428 const float angle_start = -DEG2RADF(start) + float(M_PI_2);
429 const float angle_end = -(DEG2RADF(sweep) - angle_start);
430 nsegments += 1;
431 immBegin(prim_type, nsegments);
432 for (int i = 0; i < nsegments; i++) {
433 const float angle = interpf(angle_start, angle_end, (float(i) / float(nsegments - 1)));
434 const float angle_sin = sinf(angle);
435 const float angle_cos = cosf(angle);
436 immVertex2f(pos, x + radius * angle_cos, y + radius * angle_sin);
437 }
438 immEnd();
439}
440
442 uint pos,
443 float x,
444 float y,
445 float z,
446 float rad,
447 int nsegments,
448 float start,
449 float sweep)
450{
451 /* shift & reverse angle, increase 'nsegments' to match gluPartialDisk */
452 const float angle_start = -DEG2RADF(start) + float(M_PI / 2);
453 const float angle_end = -(DEG2RADF(sweep) - angle_start);
454 nsegments += 1;
455 immBegin(prim_type, nsegments);
456 for (int i = 0; i < nsegments; i++) {
457 const float angle = interpf(angle_start, angle_end, (float(i) / float(nsegments - 1)));
458 const float angle_sin = sinf(angle);
459 const float angle_cos = cosf(angle);
460 immVertex3f(pos, x + rad * angle_cos, y + rad * angle_sin, z);
461 }
462 immEnd();
463}
464
466 uint pos, float x, float y, float radius, int nsegments, float start, float sweep)
467{
468 imm_draw_circle_partial(GPU_PRIM_LINE_STRIP, pos, x, y, radius, nsegments, start, sweep);
469}
470
472 uint pos, float x, float y, float z, float radius, int nsegments, float start, float sweep)
473{
474 imm_draw_circle_partial_3d(GPU_PRIM_LINE_STRIP, pos, x, y, z, radius, nsegments, start, sweep);
475}
476
477static void imm_draw_disk_partial(GPUPrimType prim_type,
478 uint pos,
479 float x,
480 float y,
481 float rad_inner,
482 float rad_outer,
483 int nsegments,
484 float start,
485 float sweep)
486{
487 /* to avoid artifacts */
488 const float max_angle = 3 * 360;
489 CLAMP(sweep, -max_angle, max_angle);
490
491 /* shift & reverse angle, increase 'nsegments' to match gluPartialDisk */
492 const float angle_start = -DEG2RADF(start) + float(M_PI_2);
493 const float angle_end = -(DEG2RADF(sweep) - angle_start);
494 nsegments += 1;
495 immBegin(prim_type, nsegments * 2);
496 for (int i = 0; i < nsegments; i++) {
497 const float angle = interpf(angle_start, angle_end, (float(i) / float(nsegments - 1)));
498 const float angle_sin = sinf(angle);
499 const float angle_cos = cosf(angle);
500 immVertex2f(pos, x + rad_inner * angle_cos, y + rad_inner * angle_sin);
501 immVertex2f(pos, x + rad_outer * angle_cos, y + rad_outer * angle_sin);
502 }
503 immEnd();
504}
505
507 uint pos,
508 float x,
509 float y,
510 float z,
511 float rad_inner,
512 float rad_outer,
513 int nsegments,
514 float start,
515 float sweep)
516{
517 /* to avoid artifacts */
518 const float max_angle = 3 * 360;
519 CLAMP(sweep, -max_angle, max_angle);
520
521 /* shift & reverse angle, increase 'nsegments' to match gluPartialDisk */
522 const float angle_start = -DEG2RADF(start) + float(M_PI_2);
523 const float angle_end = -(DEG2RADF(sweep) - angle_start);
524 nsegments += 1;
525 immBegin(prim_type, nsegments * 2);
526 for (int i = 0; i < nsegments; i++) {
527 const float angle = interpf(angle_start, angle_end, (float(i) / float(nsegments - 1)));
528 const float angle_sin = sinf(angle);
529 const float angle_cos = cosf(angle);
530 immVertex3f(pos, x + rad_inner * angle_cos, y + rad_inner * angle_sin, z);
531 immVertex3f(pos, x + rad_outer * angle_cos, y + rad_outer * angle_sin, z);
532 }
533 immEnd();
534}
535
537 float x,
538 float y,
539 float rad_inner,
540 float rad_outer,
541 int nsegments,
542 float start,
543 float sweep)
544{
546 GPU_PRIM_TRI_STRIP, pos, x, y, rad_inner, rad_outer, nsegments, start, sweep);
547}
549 float x,
550 float y,
551 float z,
552 float rad_inner,
553 float rad_outer,
554 int nsegments,
555 float start,
556 float sweep)
557{
559 GPU_PRIM_TRI_STRIP, pos, x, y, z, rad_inner, rad_outer, nsegments, start, sweep);
560}
561
562static void imm_draw_circle_3D(GPUPrimType prim_type,
563 uint pos,
564 float x,
565 float y,
566 float radius_x,
567 float radius_y,
568 int nsegments)
569{
570 if (prim_type == GPU_PRIM_LINE_LOOP) {
571 /* NOTE(Metal/AMD): For small primitives, line list more efficient than line strip. */
572 immBegin(GPU_PRIM_LINES, nsegments * 2);
573
574 const float angle = float(2 * M_PI) / float(nsegments);
575 float xprev = cosf(-angle) * radius_x;
576 float yprev = sinf(-angle) * radius_y;
577 const float alpha = 2.0f * cosf(angle);
578
579 float xr = radius_x;
580 float yr = 0;
581
582 for (int i = 0; i < nsegments; i++) {
583 immVertex3f(pos, x + xr, y + yr, 0.0f);
584 if (i) {
585 immVertex3f(pos, x + xr, y + yr, 0.0f);
586 }
587 /* `cos[(n + 1)a] = 2cos(a)cos(na) - cos[(n - 1)a]`. */
588 const float xnext = alpha * xr - xprev;
589 /* `sin[(n + 1)a] = 2cos(a)sin(na) - sin[(n - 1)a]`. */
590 const float ynext = alpha * yr - yprev;
591 xprev = xr;
592 yprev = yr;
593 xr = xnext;
594 yr = ynext;
595 }
596 immVertex3f(pos, x + radius_x, y, 0.0f);
597 immEnd();
598 }
599 else {
600 immBegin(prim_type, nsegments);
601 for (int i = 0; i < nsegments; i++) {
602 float angle = float(2 * M_PI) * (float(i) / float(nsegments));
603 immVertex3f(pos, x + radius_x * cosf(angle), y + radius_y * sinf(angle), 0.0f);
604 }
605 immEnd();
606 }
607}
608
609void imm_draw_circle_wire_3d(uint pos, float x, float y, float radius, int nsegments)
610{
611 imm_draw_circle_3D(GPU_PRIM_LINE_LOOP, pos, x, y, radius, radius, nsegments);
612}
613
615 uint pos, float x, float y, float radius_x, float radius_y, int nsegments)
616{
617 imm_draw_circle_3D(GPU_PRIM_LINE_LOOP, pos, x, y, radius_x, radius_y, nsegments);
618}
619
620void imm_draw_circle_dashed_3d(uint pos, float x, float y, float radius, int nsegments)
621{
622 imm_draw_circle_3D(GPU_PRIM_LINES, pos, x, y, radius, radius, nsegments / 2);
623}
624
625void imm_draw_circle_fill_3d(uint pos, float x, float y, float radius, int nsegments)
626{
627 imm_draw_circle_3D(GPU_PRIM_TRI_FAN, pos, x, y, radius, radius, nsegments);
628}
629
631 uint pos, float x, float y, float radius_x, float radius_y, int nsegments)
632{
633 imm_draw_circle_3D(GPU_PRIM_TRI_FAN, pos, x, y, radius_x, radius_y, nsegments);
634}
635
636void imm_draw_box_wire_2d(uint pos, float x1, float y1, float x2, float y2)
637{
638 /* NOTE(Metal/AMD): For small primitives, line list more efficient than line-strip. */
640 immVertex2f(pos, x1, y1);
641 immVertex2f(pos, x1, y2);
642
643 immVertex2f(pos, x1, y2);
644 immVertex2f(pos, x2, y2);
645
646 immVertex2f(pos, x2, y2);
647 immVertex2f(pos, x2, y1);
648
649 immVertex2f(pos, x2, y1);
650 immVertex2f(pos, x1, y1);
651 immEnd();
652}
653
654void imm_draw_box_wire_3d(uint pos, float x1, float y1, float x2, float y2)
655{
656 /* use this version when GPUVertFormat has a vec3 position */
657 /* NOTE(Metal/AMD): For small primitives, line list more efficient than line-strip. */
659 immVertex3f(pos, x1, y1, 0.0f);
660 immVertex3f(pos, x1, y2, 0.0f);
661
662 immVertex3f(pos, x1, y2, 0.0f);
663 immVertex3f(pos, x2, y2, 0.0f);
664
665 immVertex3f(pos, x2, y2, 0.0f);
666 immVertex3f(pos, x2, y1, 0.0f);
667
668 immVertex3f(pos, x2, y1, 0.0f);
669 immVertex3f(pos, x1, y1, 0.0f);
670 immEnd();
671}
672
674 float y1,
675 float x2,
676 float y2,
677 const float color_primary[4],
678 const float color_secondary[4],
679 int checker_size)
680{
681 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
682
684
685 immUniform4fv("color1", color_primary);
686 immUniform4fv("color2", color_secondary);
687 immUniform1i("size", checker_size);
688
689 immRectf(pos, x1, y1, x2, y2);
690
692}
693void imm_draw_box_checker_2d(float x1, float y1, float x2, float y2, bool clear_alpha)
694{
695 float checker_primary[4];
696 float checker_secondary[4];
699 checker_primary[3] = clear_alpha ? 0.0 : checker_primary[3];
700 checker_secondary[3] = clear_alpha ? 0.0 : checker_secondary[3];
701 int checker_size = ui::theme::get_value(TH_TRANSPARENT_CHECKER_SIZE) * U.pixelsize;
702 imm_draw_box_checker_2d_ex(x1, y1, x2, y2, checker_primary, checker_secondary, checker_size);
703}
704
705void imm_draw_cube_fill_3d(uint pos, const float center[3], const float aspect[3])
706{
707 float coords[ARRAY_SIZE(cube_coords)][3];
708
709 for (int i = 0; i < ARRAY_SIZE(cube_coords); i++) {
710 madd_v3_v3v3v3(coords[i], center, cube_coords[i], aspect);
711 }
712
714 for (int i = 0; i < ARRAY_SIZE(cube_quad_index); i++) {
715 immVertex3fv(pos, coords[cube_quad_index[i][0]]);
716 immVertex3fv(pos, coords[cube_quad_index[i][1]]);
717 immVertex3fv(pos, coords[cube_quad_index[i][2]]);
718
719 immVertex3fv(pos, coords[cube_quad_index[i][0]]);
720 immVertex3fv(pos, coords[cube_quad_index[i][2]]);
721 immVertex3fv(pos, coords[cube_quad_index[i][3]]);
722 }
723 immEnd();
724}
725
726void imm_draw_cube_wire_3d(uint pos, const float center[3], const float aspect[3])
727{
728 float coords[ARRAY_SIZE(cube_coords)][3];
729
730 for (int i = 0; i < ARRAY_SIZE(cube_coords); i++) {
731 madd_v3_v3v3v3(coords[i], center, cube_coords[i], aspect);
732 }
733
735 for (int i = 0; i < ARRAY_SIZE(cube_line_index); i++) {
736 immVertex3fv(pos, coords[cube_line_index[i][0]]);
737 immVertex3fv(pos, coords[cube_line_index[i][1]]);
738 }
739 immEnd();
740}
741
743 const float center[3],
744 const float aspect[3],
745 const float factor)
746{
747 float coords[ARRAY_SIZE(cube_coords)][3];
748
749 for (int i = 0; i < ARRAY_SIZE(cube_coords); i++) {
750 madd_v3_v3v3v3(coords[i], center, cube_coords[i], aspect);
751 }
752
754 for (int i = 0; i < ARRAY_SIZE(cube_line_index); i++) {
755 float vec[3], co[3];
756 sub_v3_v3v3(vec, coords[cube_line_index[i][1]], coords[cube_line_index[i][0]]);
757 mul_v3_fl(vec, factor);
758
759 copy_v3_v3(co, coords[cube_line_index[i][0]]);
760 immVertex3fv(pos, co);
761 add_v3_v3(co, vec);
762 immVertex3fv(pos, co);
763
764 copy_v3_v3(co, coords[cube_line_index[i][1]]);
765 immVertex3fv(pos, co);
766 sub_v3_v3(co, vec);
767 immVertex3fv(pos, co);
768 }
769 immEnd();
770}
771
773 uint pos, uint nor, float base, float top, float height, int slices, int stacks)
774{
775 immBegin(GPU_PRIM_TRIS, 6 * slices * stacks);
776 for (int i = 0; i < slices; i++) {
777 const float angle1 = float(2 * M_PI) * (float(i) / float(slices));
778 const float angle2 = float(2 * M_PI) * (float(i + 1) / float(slices));
779 const float cos1 = cosf(angle1);
780 const float sin1 = sinf(angle1);
781 const float cos2 = cosf(angle2);
782 const float sin2 = sinf(angle2);
783
784 for (int j = 0; j < stacks; j++) {
785 float fac1 = float(j) / float(stacks);
786 float fac2 = float(j + 1) / float(stacks);
787 float r1 = base * (1.0f - fac1) + top * fac1;
788 float r2 = base * (1.0f - fac2) + top * fac2;
789 float h1 = height * (float(j) / float(stacks));
790 float h2 = height * (float(j + 1) / float(stacks));
791
792 const float v1[3] = {r1 * cos2, r1 * sin2, h1};
793 const float v2[3] = {r2 * cos2, r2 * sin2, h2};
794 const float v3[3] = {r2 * cos1, r2 * sin1, h2};
795 const float v4[3] = {r1 * cos1, r1 * sin1, h1};
796 float n1[3], n2[3];
797
798 /* calc normals */
799 sub_v3_v3v3(n1, v2, v1);
800 normalize_v3(n1);
801 n1[0] = cos1;
802 n1[1] = sin1;
803 n1[2] = 1 - n1[2];
804
805 sub_v3_v3v3(n2, v3, v4);
806 normalize_v3(n2);
807 n2[0] = cos2;
808 n2[1] = sin2;
809 n2[2] = 1 - n2[2];
810
811 /* first tri */
812 immAttr3fv(nor, n2);
813 immVertex3fv(pos, v1);
814 immVertex3fv(pos, v2);
815 immAttr3fv(nor, n1);
816 immVertex3fv(pos, v3);
817
818 /* second tri */
819 immVertex3fv(pos, v3);
820 immVertex3fv(pos, v4);
821 immAttr3fv(nor, n2);
822 immVertex3fv(pos, v1);
823 }
824 }
825 immEnd();
826}
827
829 uint pos, float base, float top, float height, int slices, int stacks)
830{
831 immBegin(GPU_PRIM_LINES, 6 * slices * stacks);
832 for (int i = 0; i < slices; i++) {
833 const float angle1 = float(2 * M_PI) * (float(i) / float(slices));
834 const float angle2 = float(2 * M_PI) * (float(i + 1) / float(slices));
835 const float cos1 = cosf(angle1);
836 const float sin1 = sinf(angle1);
837 const float cos2 = cosf(angle2);
838 const float sin2 = sinf(angle2);
839
840 for (int j = 0; j < stacks; j++) {
841 float fac1 = float(j) / float(stacks);
842 float fac2 = float(j + 1) / float(stacks);
843 float r1 = base * (1.0f - fac1) + top * fac1;
844 float r2 = base * (1.0f - fac2) + top * fac2;
845 float h1 = height * (float(j) / float(stacks));
846 float h2 = height * (float(j + 1) / float(stacks));
847
848 const float v1[3] = {r1 * cos2, r1 * sin2, h1};
849 const float v2[3] = {r2 * cos2, r2 * sin2, h2};
850 const float v3[3] = {r2 * cos1, r2 * sin1, h2};
851 const float v4[3] = {r1 * cos1, r1 * sin1, h1};
852
853 immVertex3fv(pos, v1);
854 immVertex3fv(pos, v2);
855
856 immVertex3fv(pos, v2);
857 immVertex3fv(pos, v3);
858
859 immVertex3fv(pos, v1);
860 immVertex3fv(pos, v4);
861 }
862 }
863 immEnd();
864}
865
867 uint pos, float base, float top, float height, int slices, int stacks)
868{
869 immBegin(GPU_PRIM_TRIS, 6 * slices * stacks);
870 for (int i = 0; i < slices; i++) {
871 const float angle1 = float(2 * M_PI) * (float(i) / float(slices));
872 const float angle2 = float(2 * M_PI) * (float(i + 1) / float(slices));
873 const float cos1 = cosf(angle1);
874 const float sin1 = sinf(angle1);
875 const float cos2 = cosf(angle2);
876 const float sin2 = sinf(angle2);
877
878 for (int j = 0; j < stacks; j++) {
879 float fac1 = float(j) / float(stacks);
880 float fac2 = float(j + 1) / float(stacks);
881 float r1 = base * (1.0f - fac1) + top * fac1;
882 float r2 = base * (1.0f - fac2) + top * fac2;
883 float h1 = height * (float(j) / float(stacks));
884 float h2 = height * (float(j + 1) / float(stacks));
885
886 const float v1[3] = {r1 * cos2, r1 * sin2, h1};
887 const float v2[3] = {r2 * cos2, r2 * sin2, h2};
888 const float v3[3] = {r2 * cos1, r2 * sin1, h2};
889 const float v4[3] = {r1 * cos1, r1 * sin1, h1};
890
891 /* first tri */
892 immVertex3fv(pos, v1);
893 immVertex3fv(pos, v2);
894 immVertex3fv(pos, v3);
895
896 /* second tri */
897 immVertex3fv(pos, v3);
898 immVertex3fv(pos, v4);
899 immVertex3fv(pos, v1);
900 }
901 }
902 immEnd();
903}
904
905/* Circle Drawing - Tables for Optimized Drawing Speed */
906constexpr static int CIRCLE_RESOL = 32;
907
908static void circball_array_fill(const float verts[CIRCLE_RESOL][3],
909 const float cent[3],
910 const float radius,
911 const float tmat[4][4])
912{
913 /* 32 values of sin function (still same result!) */
914 const float sinval[CIRCLE_RESOL] = {
915 0.00000000, 0.20129852, 0.39435585, 0.57126821, 0.72479278, 0.84864425, 0.93775213,
916 0.98846832, 0.99871650, 0.96807711, 0.89780453, 0.79077573, 0.65137248, 0.48530196,
917 0.29936312, 0.10116832, -0.10116832, -0.29936312, -0.48530196, -0.65137248, -0.79077573,
918 -0.89780453, -0.96807711, -0.99871650, -0.98846832, -0.93775213, -0.84864425, -0.72479278,
919 -0.57126821, -0.39435585, -0.20129852, 0.00000000,
920 };
921
922 /* 32 values of cos function (still same result!) */
923 const float cosval[CIRCLE_RESOL] = {
924 1.00000000, 0.97952994, 0.91895781, 0.82076344, 0.68896691, 0.52896401, 0.34730525,
925 0.15142777, -0.05064916, -0.25065253, -0.44039415, -0.61210598, -0.75875812, -0.87434661,
926 -0.95413925, -0.99486932, -0.99486932, -0.95413925, -0.87434661, -0.75875812, -0.61210598,
927 -0.44039415, -0.25065253, -0.05064916, 0.15142777, 0.34730525, 0.52896401, 0.68896691,
928 0.82076344, 0.91895781, 0.97952994, 1.00000000,
929 };
930
931 float vx[3], vy[3];
932 float *viter = (float *)verts;
933
934 mul_v3_v3fl(vx, tmat[0], radius);
935 mul_v3_v3fl(vy, tmat[1], radius);
936
937 for (uint a = 0; a < CIRCLE_RESOL; a++, viter += 3) {
938 viter[0] = cent[0] + sinval[a] * vx[0] + cosval[a] * vy[0];
939 viter[1] = cent[1] + sinval[a] * vx[1] + cosval[a] * vy[1];
940 viter[2] = cent[2] + sinval[a] * vx[2] + cosval[a] * vy[2];
941 }
942}
943
944void imm_drawcircball(const float cent[3], float radius, const float tmat[4][4], uint pos)
945{
946 float verts[CIRCLE_RESOL][3];
947
948 circball_array_fill(verts, cent, radius, tmat);
949
951 for (int i = 0; i < CIRCLE_RESOL; i++) {
952 immVertex3fv(pos, verts[i]);
953 }
954 immEnd();
955}
956
957} // namespace blender
#define DEG2RADF(_deg)
#define M_PI_2
#define M_PI
#define CLAMP(a, b, c)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define CIRCLE_RESOL
void imm_draw_rounded_box_wire_2d(uint pos, float x, float y, float2 radius, float2 corner_radius, int nsegments)
void imm_draw_circle_partial_wire_aspect_3d(uint pos, float x, float y, float z, float2 radius, int nsegments, float start, float sweep)
void imm_draw_circle_partial_wire_aspect_2d(uint pos, float x, float y, float2 radius, int nsegments, float start, float sweep)
static void imm_draw_circle_partial_aspect_3d(const GPUPrimType prim_type, const uint pos, const float x, const float y, const float z, const float2 radius, const int nsegments, const float start, const float sweep)
void imm_draw_rounded_box_wire_3d(uint pos, float x, float y, float2 radius, float2 corner_radius, int nsegments)
static void imm_draw_circle_partial_aspect(const GPUPrimType prim_type, const uint pos, const float x, const float y, const float2 radius, const int nsegments, const float start, const float sweep)
void get_color_4fv(int colorid, float col[4])
int get_value(int colorid)
BMesh Iterators.
void imm_draw_cylinder_wire_3d(uint pos, float base, float top, float height, int slices, int stacks)
UserDef U
Definition blender.cc:58
static void imm_draw_circle(GPUPrimType prim_type, const uint shdr_pos, float x, float y, float radius_x, float radius_y, int nsegments)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void imm_draw_cylinder_fill_normal_3d(uint pos, uint nor, float base, float top, float height, int slices, int stacks)
static void imm_draw_circle_partial_3d(GPUPrimType prim_type, uint pos, float x, float y, float z, float rad, int nsegments, float start, float sweep)
static void imm_draw_circle_3D(GPUPrimType prim_type, uint pos, float x, float y, float radius_x, float radius_y, int nsegments)
void immEnd()
void imm_draw_circle_dashed_3d(uint pos, float x, float y, float radius, int nsegments)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void imm_draw_cylinder_fill_3d(uint pos, float base, float top, float height, int slices, int stacks)
void imm_draw_circle_wire_aspect_3d(uint pos, float x, float y, float radius_x, float radius_y, int nsegments)
void imm_draw_box_checker_2d(float x1, float y1, float x2, float y2, bool clear_alpha=false)
GPUVertFormat * immVertexFormat()
void immRectf_fast_with_color(uint pos, uint col, float x1, float y1, float x2, float y2, const float color[4])
void immBegin(GPUPrimType, uint vertex_len)
@ GPU_PRIM_TRI_STRIP
@ GPU_PRIM_LINE_STRIP
@ GPU_PRIM_LINE_LOOP
void imm_draw_box_checker_2d_ex(float x1, float y1, float x2, float y2, const float color_primary[4], const float color_secondary[4], int checker_size)
void immRecti(uint pos, int x1, int y1, int x2, int y2)
@ TH_TRANSPARENT_CHECKER_SIZE
@ TH_TRANSPARENT_CHECKER_SECONDARY
@ TH_TRANSPARENT_CHECKER_PRIMARY
void immUniformColor4fv(const float rgba[4])
static const int cube_quad_index[6][4]
void imm_drawcircball(const float cent[3], float radius, const float tmat[4][4], uint pos)
void imm_draw_cube_wire_3d(uint pos, const float center[3], const float aspect[3])
void immAttr2f(uint attr_id, float x, float y)
void immUnbindProgram()
void imm_draw_circle_partial_wire_2d(uint pos, float x, float y, float radius, int nsegments, float start, float sweep)
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void sub_v3_v3(float r[3], const float a[3])
void immAttr3fv(uint attr_id, const float data[3])
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void imm_draw_circle_wire_2d(uint shdr_pos, float x, float y, float radius, int nsegments)
VecBase< float, 2 > float2
void imm_draw_disk_partial_fill_3d(uint pos, float x, float y, float z, float rad_inner, float rad_outer, int nsegments, float start, float sweep)
static void imm_draw_disk_partial(GPUPrimType prim_type, uint pos, float x, float y, float rad_inner, float rad_outer, int nsegments, float start, float sweep)
void immVertex2f(uint attr_id, float x, float y)
void immVertex2i(uint attr_id, int x, int y)
static void circball_array_fill(const float verts[CIRCLE_RESOL][3], const float cent[3], const float radius, const float tmat[4][4])
void immAttr4fv(uint attr_id, const float data[4])
void immUniform4fv(const char *name, const float data[4])
void imm_draw_cube_corners_3d(uint pos, const float center[3], const float aspect[3], float factor)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
void immUniformColor3ub(unsigned char r, unsigned char g, unsigned char b)
static void imm_draw_disk_partial_3d(GPUPrimType prim_type, uint pos, float x, float y, float z, float rad_inner, float rad_outer, int nsegments, float start, float sweep)
void immRectf_with_texco(uint pos, uint tex_coord, const rctf &p, const rctf &uv)
void imm_draw_box_wire_3d(uint pos, float x1, float y1, float x2, float y2)
void imm_draw_circle_fill_3d(uint pos, float x, float y, float radius, int nsegments)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void imm_draw_circle_partial_wire_3d(uint pos, float x, float y, float z, float radius, int nsegments, float start, float sweep)
void imm_draw_circle_fill_aspect_3d(uint pos, float x, float y, float radius_x, float radius_y, int nsegments)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
void immVertex3fv(uint attr_id, const float data[3])
void imm_draw_circle_wire_aspect_2d(uint shdr_pos, float x, float y, float radius_x, float radius_y, int nsegments)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void madd_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float c[3])
void imm_draw_circle_fill_aspect_2d(uint shdr_pos, float x, float y, float radius_x, float radius_y, int nsegments)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void imm_draw_box_wire_2d(uint pos, float x1, float y1, float x2, float y2)
void immRecti_fast_with_color(uint pos, uint col, int x1, int y1, int x2, int y2, const float color[4])
void immRectf_fast(uint pos, float x1, float y1, float x2, float y2)
MINLINE float interpf(float target, float origin, float t)
void immVertex3f(uint attr_id, float x, float y, float z)
static const float cube_coords[8][3]
void immUniform1i(const char *name, int x)
void imm_draw_disk_partial_fill_2d(uint pos, float x, float y, float rad_inner, float rad_outer, int nsegments, float start, float sweep)
static void imm_draw_circle_partial(GPUPrimType prim_type, uint pos, float x, float y, float radius, int nsegments, float start, float sweep)
void imm_draw_circle_wire_3d(uint pos, float x, float y, float radius, int nsegments)
void imm_draw_circle_fill_2d(uint shdr_pos, float x, float y, float radius, int nsegments)
void immRectf(uint pos, float x1, float y1, float x2, float y2)
void imm_cpack(uint x)
static const int cube_line_index[12][2]
void imm_draw_cube_fill_3d(uint pos, const float center[3], const float aspect[3])
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float