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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
|
/*
* 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.
*
* The Original Code is Copyright (C) 2008 Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup spview3d
*/
#include <math.h>
#include "BLI_jitter_2d.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_rect.h"
#include "BLI_string.h"
#include "BLI_string_utils.h"
#include "BLI_threads.h"
#include "BKE_armature.h"
#include "BKE_camera.h"
#include "BKE_collection.h"
#include "BKE_context.h"
#include "BKE_customdata.h"
#include "BKE_global.h"
#include "BKE_image.h"
#include "BKE_key.h"
#include "BKE_layer.h"
#include "BKE_main.h"
#include "BKE_object.h"
#include "BKE_paint.h"
#include "BKE_scene.h"
#include "BKE_studiolight.h"
#include "BKE_unit.h"
#include "BLF_api.h"
#include "BLT_translation.h"
#include "DNA_armature_types.h"
#include "DNA_brush_types.h"
#include "DNA_camera_types.h"
#include "DNA_key_types.h"
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "DNA_view3d_types.h"
#include "DNA_windowmanager_types.h"
#include "DRW_engine.h"
#include "DRW_select_buffer.h"
#include "ED_gpencil.h"
#include "ED_info.h"
#include "ED_keyframing.h"
#include "ED_screen.h"
#include "ED_screen_types.h"
#include "ED_transform.h"
#include "ED_view3d_offscreen.h"
#include "DEG_depsgraph_query.h"
#include "GPU_batch.h"
#include "GPU_batch_presets.h"
#include "GPU_framebuffer.h"
#include "GPU_immediate.h"
#include "GPU_immediate_util.h"
#include "GPU_material.h"
#include "GPU_matrix.h"
#include "GPU_state.h"
#include "GPU_viewport.h"
#include "MEM_guardedalloc.h"
#include "UI_interface.h"
#include "UI_resources.h"
#include "RE_engine.h"
#include "WM_api.h"
#include "WM_types.h"
#include "RNA_access.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "view3d_intern.h" /* own include */
#define M_GOLDEN_RATIO_CONJUGATE 0.618033988749895f
#define VIEW3D_OVERLAY_LINEHEIGHT (0.9f * U.widget_unit)
/* -------------------------------------------------------------------- */
/** \name General Functions
* \{ */
/**
* \note keep this synced with #ED_view3d_mats_rv3d_backup/#ED_view3d_mats_rv3d_restore
*/
void ED_view3d_update_viewmat(Depsgraph *depsgraph,
const Scene *scene,
View3D *v3d,
ARegion *region,
const float viewmat[4][4],
const float winmat[4][4],
const rcti *rect,
bool offscreen)
{
RegionView3D *rv3d = region->regiondata;
/* setup window matrices */
if (winmat) {
copy_m4_m4(rv3d->winmat, winmat);
}
else {
view3d_winmatrix_set(depsgraph, region, v3d, rect);
}
/* setup view matrix */
if (viewmat) {
copy_m4_m4(rv3d->viewmat, viewmat);
}
else {
float rect_scale[2];
if (rect) {
rect_scale[0] = (float)BLI_rcti_size_x(rect) / (float)region->winx;
rect_scale[1] = (float)BLI_rcti_size_y(rect) / (float)region->winy;
}
/* NOTE: calls BKE_object_where_is_calc for camera... */
view3d_viewmatrix_set(depsgraph, scene, v3d, rv3d, rect ? rect_scale : NULL);
}
/* update utility matrices */
mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
invert_m4_m4(rv3d->persinv, rv3d->persmat);
invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
/* calculate GLSL view dependent values */
/* store window coordinates scaling/offset */
if (!offscreen && rv3d->persp == RV3D_CAMOB && v3d->camera) {
rctf cameraborder;
ED_view3d_calc_camera_border(scene, depsgraph, region, v3d, rv3d, &cameraborder, false);
rv3d->viewcamtexcofac[0] = (float)region->winx / BLI_rctf_size_x(&cameraborder);
rv3d->viewcamtexcofac[1] = (float)region->winy / BLI_rctf_size_y(&cameraborder);
rv3d->viewcamtexcofac[2] = -rv3d->viewcamtexcofac[0] * cameraborder.xmin / (float)region->winx;
rv3d->viewcamtexcofac[3] = -rv3d->viewcamtexcofac[1] * cameraborder.ymin / (float)region->winy;
}
else {
rv3d->viewcamtexcofac[0] = rv3d->viewcamtexcofac[1] = 1.0f;
rv3d->viewcamtexcofac[2] = rv3d->viewcamtexcofac[3] = 0.0f;
}
/* Calculate pixel-size factor once, this is used for lights and object-centers. */
{
/* NOTE: '1.0f / len_v3(v1)' replaced 'len_v3(rv3d->viewmat[0])'
* because of float point precision problems at large values T23908. */
float v1[3], v2[3];
float len_px, len_sc;
v1[0] = rv3d->persmat[0][0];
v1[1] = rv3d->persmat[1][0];
v1[2] = rv3d->persmat[2][0];
v2[0] = rv3d->persmat[0][1];
v2[1] = rv3d->persmat[1][1];
v2[2] = rv3d->persmat[2][1];
len_px = 2.0f / sqrtf(min_ff(len_squared_v3(v1), len_squared_v3(v2)));
if (rect) {
len_sc = (float)max_ii(BLI_rcti_size_x(rect), BLI_rcti_size_y(rect));
}
else {
len_sc = (float)MAX2(region->winx, region->winy);
}
rv3d->pixsize = len_px / len_sc;
}
}
static void view3d_main_region_setup_view(Depsgraph *depsgraph,
Scene *scene,
View3D *v3d,
ARegion *region,
const float viewmat[4][4],
const float winmat[4][4],
const rcti *rect)
{
RegionView3D *rv3d = region->regiondata;
ED_view3d_update_viewmat(depsgraph, scene, v3d, region, viewmat, winmat, rect, false);
/* set for opengl */
GPU_matrix_projection_set(rv3d->winmat);
GPU_matrix_set(rv3d->viewmat);
}
static void view3d_main_region_setup_offscreen(Depsgraph *depsgraph,
const Scene *scene,
View3D *v3d,
ARegion *region,
const float viewmat[4][4],
const float winmat[4][4])
{
RegionView3D *rv3d = region->regiondata;
ED_view3d_update_viewmat(depsgraph, scene, v3d, region, viewmat, winmat, NULL, true);
/* set for opengl */
GPU_matrix_projection_set(rv3d->winmat);
GPU_matrix_set(rv3d->viewmat);
}
static bool view3d_stereo3d_active(wmWindow *win,
const Scene *scene,
View3D *v3d,
RegionView3D *rv3d)
{
if ((scene->r.scemode & R_MULTIVIEW) == 0) {
return false;
}
if ((v3d->camera == NULL) || (v3d->camera->type != OB_CAMERA) || rv3d->persp != RV3D_CAMOB) {
return false;
}
switch (v3d->stereo3d_camera) {
case STEREO_MONO_ID:
return false;
break;
case STEREO_3D_ID:
/* win will be NULL when calling this from the selection or draw loop. */
if ((win == NULL) || (WM_stereo3d_enabled(win, true) == false)) {
return false;
}
if (((scene->r.views_format & SCE_VIEWS_FORMAT_MULTIVIEW) != 0) &&
!BKE_scene_multiview_is_stereo3d(&scene->r)) {
return false;
}
break;
/* We always need the stereo calculation for left and right cameras. */
case STEREO_LEFT_ID:
case STEREO_RIGHT_ID:
default:
break;
}
return true;
}
/* setup the view and win matrices for the multiview cameras
*
* unlike view3d_stereo3d_setup_offscreen, when view3d_stereo3d_setup is called
* we have no winmatrix (i.e., projection matrix) defined at that time.
* Since the camera and the camera shift are needed for the winmat calculation
* we do a small hack to replace it temporarily so we don't need to change the
* view3d)main_region_setup_view() code to account for that.
*/
static void view3d_stereo3d_setup(
Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *region, const rcti *rect)
{
bool is_left;
const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
const char *viewname;
/* show only left or right camera */
if (v3d->stereo3d_camera != STEREO_3D_ID) {
v3d->multiview_eye = v3d->stereo3d_camera;
}
is_left = v3d->multiview_eye == STEREO_LEFT_ID;
viewname = names[is_left ? STEREO_LEFT_ID : STEREO_RIGHT_ID];
/* update the viewport matrices with the new camera */
if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
Camera *data, *data_eval;
float viewmat[4][4];
float shiftx;
data = (Camera *)v3d->camera->data;
data_eval = (Camera *)DEG_get_evaluated_id(depsgraph, &data->id);
shiftx = data_eval->shiftx;
BLI_thread_lock(LOCK_VIEW3D);
data_eval->shiftx = BKE_camera_multiview_shift_x(&scene->r, v3d->camera, viewname);
BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
view3d_main_region_setup_view(depsgraph, scene, v3d, region, viewmat, NULL, rect);
data_eval->shiftx = shiftx;
BLI_thread_unlock(LOCK_VIEW3D);
}
else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
float viewmat[4][4];
Object *view_ob = v3d->camera;
Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
BLI_thread_lock(LOCK_VIEW3D);
v3d->camera = camera;
BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
view3d_main_region_setup_view(depsgraph, scene, v3d, region, viewmat, NULL, rect);
v3d->camera = view_ob;
BLI_thread_unlock(LOCK_VIEW3D);
}
}
#ifdef WITH_XR_OPENXR
static void view3d_xr_mirror_setup(const wmWindowManager *wm,
Depsgraph *depsgraph,
Scene *scene,
View3D *v3d,
ARegion *region,
const rcti *rect)
{
RegionView3D *rv3d = region->regiondata;
float viewmat[4][4];
const float lens_old = v3d->lens;
if (!WM_xr_session_state_viewer_pose_matrix_info_get(&wm->xr, viewmat, &v3d->lens)) {
/* Can't get info from XR session, use fallback values. */
copy_m4_m4(viewmat, rv3d->viewmat);
v3d->lens = lens_old;
}
view3d_main_region_setup_view(depsgraph, scene, v3d, region, viewmat, NULL, rect);
/* Set draw flags. */
SET_FLAG_FROM_TEST(v3d->flag2,
(wm->xr.session_settings.draw_flags & V3D_OFSDRAW_XR_SHOW_CONTROLLERS) != 0,
V3D_XR_SHOW_CONTROLLERS);
SET_FLAG_FROM_TEST(v3d->flag2,
(wm->xr.session_settings.draw_flags & V3D_OFSDRAW_XR_SHOW_CUSTOM_OVERLAYS) !=
0,
V3D_XR_SHOW_CUSTOM_OVERLAYS);
/* Hide navigation gizmo since it gets distorted if the view matrix has a scale factor. */
v3d->gizmo_flag |= V3D_GIZMO_HIDE_NAVIGATE;
/* Reset overridden View3D data. */
v3d->lens = lens_old;
}
#endif /* WITH_XR_OPENXR */
/**
* Set the correct matrices
*/
void ED_view3d_draw_setup_view(const wmWindowManager *wm,
wmWindow *win,
Depsgraph *depsgraph,
Scene *scene,
ARegion *region,
View3D *v3d,
const float viewmat[4][4],
const float winmat[4][4],
const rcti *rect)
{
RegionView3D *rv3d = region->regiondata;
#ifdef WITH_XR_OPENXR
/* Setup the view matrix. */
if (ED_view3d_is_region_xr_mirror_active(wm, v3d, region)) {
view3d_xr_mirror_setup(wm, depsgraph, scene, v3d, region, rect);
}
else
#endif
if (view3d_stereo3d_active(win, scene, v3d, rv3d)) {
view3d_stereo3d_setup(depsgraph, scene, v3d, region, rect);
}
else {
view3d_main_region_setup_view(depsgraph, scene, v3d, region, viewmat, winmat, rect);
}
#ifndef WITH_XR_OPENXR
UNUSED_VARS(wm);
#endif
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Draw View Border
* \{ */
static void view3d_camera_border(const Scene *scene,
struct Depsgraph *depsgraph,
const ARegion *region,
const View3D *v3d,
const RegionView3D *rv3d,
rctf *r_viewborder,
const bool no_shift,
const bool no_zoom)
{
CameraParams params;
rctf rect_view, rect_camera;
Object *camera_eval = DEG_get_evaluated_object(depsgraph, v3d->camera);
/* get viewport viewplane */
BKE_camera_params_init(¶ms);
BKE_camera_params_from_view3d(¶ms, depsgraph, v3d, rv3d);
if (no_zoom) {
params.zoom = 1.0f;
}
BKE_camera_params_compute_viewplane(¶ms, region->winx, region->winy, 1.0f, 1.0f);
rect_view = params.viewplane;
/* get camera viewplane */
BKE_camera_params_init(¶ms);
/* fallback for non camera objects */
params.clip_start = v3d->clip_start;
params.clip_end = v3d->clip_end;
BKE_camera_params_from_object(¶ms, camera_eval);
if (no_shift) {
params.shiftx = 0.0f;
params.shifty = 0.0f;
}
BKE_camera_params_compute_viewplane(
¶ms, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
rect_camera = params.viewplane;
/* get camera border within viewport */
r_viewborder->xmin = ((rect_camera.xmin - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) *
region->winx;
r_viewborder->xmax = ((rect_camera.xmax - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) *
region->winx;
r_viewborder->ymin = ((rect_camera.ymin - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) *
region->winy;
r_viewborder->ymax = ((rect_camera.ymax - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) *
region->winy;
}
void ED_view3d_calc_camera_border_size(const Scene *scene,
Depsgraph *depsgraph,
const ARegion *region,
const View3D *v3d,
const RegionView3D *rv3d,
float r_size[2])
{
rctf viewborder;
view3d_camera_border(scene, depsgraph, region, v3d, rv3d, &viewborder, true, true);
r_size[0] = BLI_rctf_size_x(&viewborder);
r_size[1] = BLI_rctf_size_y(&viewborder);
}
void ED_view3d_calc_camera_border(const Scene *scene,
Depsgraph *depsgraph,
const ARegion *region,
const View3D *v3d,
const RegionView3D *rv3d,
rctf *r_viewborder,
const bool no_shift)
{
view3d_camera_border(scene, depsgraph, region, v3d, rv3d, r_viewborder, no_shift, false);
}
static void drawviewborder_grid3(uint shdr_pos, float x1, float x2, float y1, float y2, float fac)
{
float x3, y3, x4, y4;
x3 = x1 + fac * (x2 - x1);
y3 = y1 + fac * (y2 - y1);
x4 = x1 + (1.0f - fac) * (x2 - x1);
y4 = y1 + (1.0f - fac) * (y2 - y1);
immBegin(GPU_PRIM_LINES, 8);
immVertex2f(shdr_pos, x1, y3);
immVertex2f(shdr_pos, x2, y3);
immVertex2f(shdr_pos, x1, y4);
immVertex2f(shdr_pos, x2, y4);
immVertex2f(shdr_pos, x3, y1);
immVertex2f(shdr_pos, x3, y2);
immVertex2f(shdr_pos, x4, y1);
immVertex2f(shdr_pos, x4, y2);
immEnd();
}
/* harmonious triangle */
static void drawviewborder_triangle(
uint shdr_pos, float x1, float x2, float y1, float y2, const char golden, const char dir)
{
float ofs;
float w = x2 - x1;
float h = y2 - y1;
immBegin(GPU_PRIM_LINES, 6);
if (w > h) {
if (golden) {
ofs = w * (1.0f - M_GOLDEN_RATIO_CONJUGATE);
}
else {
ofs = h * (h / w);
}
if (dir == 'B') {
SWAP(float, y1, y2);
}
immVertex2f(shdr_pos, x1, y1);
immVertex2f(shdr_pos, x2, y2);
immVertex2f(shdr_pos, x2, y1);
immVertex2f(shdr_pos, x1 + (w - ofs), y2);
immVertex2f(shdr_pos, x1, y2);
immVertex2f(shdr_pos, x1 + ofs, y1);
}
else {
if (golden) {
ofs = h * (1.0f - M_GOLDEN_RATIO_CONJUGATE);
}
else {
ofs = w * (w / h);
}
if (dir == 'B') {
SWAP(float, x1, x2);
}
immVertex2f(shdr_pos, x1, y1);
immVertex2f(shdr_pos, x2, y2);
immVertex2f(shdr_pos, x2, y1);
immVertex2f(shdr_pos, x1, y1 + ofs);
immVertex2f(shdr_pos, x1, y2);
immVertex2f(shdr_pos, x2, y1 + (h - ofs));
}
immEnd();
}
static void drawviewborder(Scene *scene, Depsgraph *depsgraph, ARegion *region, View3D *v3d)
{
float x1, x2, y1, y2;
float x1i, x2i, y1i, y2i;
rctf viewborder;
Camera *ca = NULL;
RegionView3D *rv3d = region->regiondata;
if (v3d->camera == NULL) {
return;
}
if (v3d->camera->type == OB_CAMERA) {
ca = v3d->camera->data;
}
ED_view3d_calc_camera_border(scene, depsgraph, region, v3d, rv3d, &viewborder, false);
/* the offsets */
x1 = viewborder.xmin;
y1 = viewborder.ymin;
x2 = viewborder.xmax;
y2 = viewborder.ymax;
GPU_line_width(1.0f);
/* apply offsets so the real 3D camera shows through */
/* NOTE: quite un-scientific but without this bit extra
* 0.0001 on the lower left the 2D border sometimes
* obscures the 3D camera border */
/* NOTE: with VIEW3D_CAMERA_BORDER_HACK defined this error isn't noticeable
* but keep it here in case we need to remove the workaround */
x1i = (int)(x1 - 1.0001f);
y1i = (int)(y1 - 1.0001f);
x2i = (int)(x2 + (1.0f - 0.0001f));
y2i = (int)(y2 + (1.0f - 0.0001f));
uint shdr_pos = GPU_vertformat_attr_add(
immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
/* First, solid lines. */
{
immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
/* passepartout, specified in camera edit buttons */
if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001f) {
const float winx = (region->winx + 1);
const float winy = (region->winy + 1);
float alpha = 1.0f;
if (ca->passepartalpha != 1.0f) {
GPU_blend(GPU_BLEND_ALPHA);
alpha = ca->passepartalpha;
}
immUniformColor4f(0.0f, 0.0f, 0.0f, alpha);
if (x1i > 0.0f) {
immRectf(shdr_pos, 0.0f, winy, x1i, 0.0f);
}
if (x2i < winx) {
immRectf(shdr_pos, x2i, winy, winx, 0.0f);
}
if (y2i < winy) {
immRectf(shdr_pos, x1i, winy, x2i, y2i);
}
if (y2i > 0.0f) {
immRectf(shdr_pos, x1i, y1i, x2i, 0.0f);
}
GPU_blend(GPU_BLEND_NONE);
immUniformThemeColor3(TH_BACK);
imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
}
#ifdef VIEW3D_CAMERA_BORDER_HACK
if (view3d_camera_border_hack_test == true) {
immUniformColor3ubv(view3d_camera_border_hack_col);
imm_draw_box_wire_2d(shdr_pos, x1i + 1, y1i + 1, x2i - 1, y2i - 1);
view3d_camera_border_hack_test = false;
}
#endif
immUnbindProgram();
}
/* When overlays are disabled, only show camera outline & passepartout. */
if (v3d->flag2 & V3D_HIDE_OVERLAYS) {
return;
}
/* And now, the dashed lines! */
immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
{
float viewport_size[4];
GPU_viewport_size_get_f(viewport_size);
immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
immUniform1i("colors_len", 0); /* "simple" mode */
immUniform1f("dash_width", 6.0f);
immUniform1f("dash_factor", 0.5f);
/* outer line not to confuse with object selection */
if (v3d->flag2 & V3D_LOCK_CAMERA) {
immUniformThemeColor(TH_REDALERT);
imm_draw_box_wire_2d(shdr_pos, x1i - 1, y1i - 1, x2i + 1, y2i + 1);
}
immUniformThemeColor3(TH_VIEW_OVERLAY);
imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
}
/* Render Border. */
if (scene->r.mode & R_BORDER) {
float x3, y3, x4, y4;
x3 = floorf(x1 + (scene->r.border.xmin * (x2 - x1))) - 1;
y3 = floorf(y1 + (scene->r.border.ymin * (y2 - y1))) - 1;
x4 = floorf(x1 + (scene->r.border.xmax * (x2 - x1))) + (U.pixelsize - 1);
y4 = floorf(y1 + (scene->r.border.ymax * (y2 - y1))) + (U.pixelsize - 1);
immUniformColor3f(1.0f, 0.25f, 0.25f);
imm_draw_box_wire_2d(shdr_pos, x3, y3, x4, y4);
}
/* safety border */
if (ca) {
GPU_blend(GPU_BLEND_ALPHA);
immUniformThemeColorAlpha(TH_VIEW_OVERLAY, 0.75f);
if (ca->dtx & CAM_DTX_CENTER) {
float x3, y3;
x3 = x1 + 0.5f * (x2 - x1);
y3 = y1 + 0.5f * (y2 - y1);
immBegin(GPU_PRIM_LINES, 4);
immVertex2f(shdr_pos, x1, y3);
immVertex2f(shdr_pos, x2, y3);
immVertex2f(shdr_pos, x3, y1);
immVertex2f(shdr_pos, x3, y2);
immEnd();
}
if (ca->dtx & CAM_DTX_CENTER_DIAG) {
immBegin(GPU_PRIM_LINES, 4);
immVertex2f(shdr_pos, x1, y1);
immVertex2f(shdr_pos, x2, y2);
immVertex2f(shdr_pos, x1, y2);
immVertex2f(shdr_pos, x2, y1);
immEnd();
}
if (ca->dtx & CAM_DTX_THIRDS) {
drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f / 3.0f);
}
if (ca->dtx & CAM_DTX_GOLDEN) {
drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f - M_GOLDEN_RATIO_CONJUGATE);
}
if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'A');
}
if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'B');
}
if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'A');
}
if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'B');
}
if (ca->flag & CAM_SHOW_SAFE_MARGINS) {
UI_draw_safe_areas(shdr_pos,
&(const rctf){
.xmin = x1,
.xmax = x2,
.ymin = y1,
.ymax = y2,
},
scene->safe_areas.title,
scene->safe_areas.action);
if (ca->flag & CAM_SHOW_SAFE_CENTER) {
UI_draw_safe_areas(shdr_pos,
&(const rctf){
.xmin = x1,
.xmax = x2,
.ymin = y1,
.ymax = y2,
},
scene->safe_areas.title_center,
scene->safe_areas.action_center);
}
}
if (ca->flag & CAM_SHOWSENSOR) {
/* determine sensor fit, and get sensor x/y, for auto fit we
* assume and square sensor and only use sensor_x */
float sizex = scene->r.xsch * scene->r.xasp;
float sizey = scene->r.ysch * scene->r.yasp;
int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, sizex, sizey);
float sensor_x = ca->sensor_x;
float sensor_y = (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO) ? ca->sensor_x : ca->sensor_y;
/* determine sensor plane */
rctf rect;
if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
float sensor_scale = (x2i - x1i) / sensor_x;
float sensor_height = sensor_scale * sensor_y;
rect.xmin = x1i;
rect.xmax = x2i;
rect.ymin = (y1i + y2i) * 0.5f - sensor_height * 0.5f;
rect.ymax = rect.ymin + sensor_height;
}
else {
float sensor_scale = (y2i - y1i) / sensor_y;
float sensor_width = sensor_scale * sensor_x;
rect.xmin = (x1i + x2i) * 0.5f - sensor_width * 0.5f;
rect.xmax = rect.xmin + sensor_width;
rect.ymin = y1i;
rect.ymax = y2i;
}
/* draw */
immUniformThemeColorShadeAlpha(TH_VIEW_OVERLAY, 100, 255);
/* TODO: Was using:
* `UI_draw_roundbox_4fv(false, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f, color);`
* We'll probably need a new imm_draw_line_roundbox_dashed or that - though in practice the
* 2.0f round corner effect was nearly not visible anyway. */
imm_draw_box_wire_2d(shdr_pos, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
}
GPU_blend(GPU_BLEND_NONE);
}
immUnbindProgram();
/* end dashed lines */
/* camera name - draw in highlighted text color */
if (ca && ((v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0) && (ca->flag & CAM_SHOWNAME)) {
UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
BLF_draw_default(x1i,
y1i - (0.7f * U.widget_unit),
0.0f,
v3d->camera->id.name + 2,
sizeof(v3d->camera->id.name) - 2);
}
}
static void drawrenderborder(ARegion *region, View3D *v3d)
{
/* use the same program for everything */
uint shdr_pos = GPU_vertformat_attr_add(
immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
GPU_line_width(1.0f);
immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
float viewport_size[4];
GPU_viewport_size_get_f(viewport_size);
immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
immUniform1i("colors_len", 0); /* "simple" mode */
immUniform4f("color", 1.0f, 0.25f, 0.25f, 1.0f);
immUniform1f("dash_width", 6.0f);
immUniform1f("dash_factor", 0.5f);
imm_draw_box_wire_2d(shdr_pos,
v3d->render_border.xmin * region->winx,
v3d->render_border.ymin * region->winy,
v3d->render_border.xmax * region->winx,
v3d->render_border.ymax * region->winy);
immUnbindProgram();
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Other Elements
* \{ */
/** could move this elsewhere, but tied into #ED_view3d_grid_scale */
float ED_scene_grid_scale(const Scene *scene, const char **r_grid_unit)
{
/* apply units */
if (scene->unit.system) {
const void *usys;
int len;
BKE_unit_system_get(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
if (usys) {
int i = BKE_unit_base_get(usys);
if (r_grid_unit) {
*r_grid_unit = BKE_unit_display_name_get(usys, i);
}
return (float)BKE_unit_scalar_get(usys, i) / scene->unit.scale_length;
}
}
return 1.0f;
}
float ED_view3d_grid_scale(const Scene *scene, View3D *v3d, const char **r_grid_unit)
{
return v3d->grid * ED_scene_grid_scale(scene, r_grid_unit);
}
#define STEPS_LEN 8
void ED_view3d_grid_steps(const Scene *scene,
View3D *v3d,
RegionView3D *rv3d,
float r_grid_steps[STEPS_LEN])
{
const void *usys;
int len;
BKE_unit_system_get(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
float grid_scale = v3d->grid;
BLI_assert(STEPS_LEN >= len);
if (usys) {
if (rv3d->view == RV3D_VIEW_USER) {
/* Skip steps */
len = BKE_unit_base_get(usys) + 1;
}
grid_scale /= scene->unit.scale_length;
int i;
for (i = 0; i < len; i++) {
r_grid_steps[i] = (float)BKE_unit_scalar_get(usys, len - 1 - i) * grid_scale;
}
for (; i < STEPS_LEN; i++) {
/* Fill last slots */
r_grid_steps[i] = 10.0f * r_grid_steps[i - 1];
}
}
else {
if (rv3d->view != RV3D_VIEW_USER) {
/* Allow 3 more subdivisions. */
grid_scale /= powf(v3d->gridsubdiv, 3);
}
int subdiv = 1;
for (int i = 0;; i++) {
r_grid_steps[i] = grid_scale * subdiv;
if (i == STEPS_LEN - 1) {
break;
}
subdiv *= v3d->gridsubdiv;
}
}
}
/* Simulates the grid scale that is actually viewed.
* The actual code is seen in `object_grid_frag.glsl` (see `grid_res`).
* Currently the simulation is only done when RV3D_VIEW_IS_AXIS. */
float ED_view3d_grid_view_scale(Scene *scene,
View3D *v3d,
ARegion *region,
const char **r_grid_unit)
{
float grid_scale;
RegionView3D *rv3d = region->regiondata;
if (!rv3d->is_persp && RV3D_VIEW_IS_AXIS(rv3d->view)) {
/* Decrease the distance between grid snap points depending on zoom. */
float dist = 12.0f / (region->sizex * rv3d->winmat[0][0]);
float grid_steps[STEPS_LEN];
ED_view3d_grid_steps(scene, v3d, rv3d, grid_steps);
/* Skip last item, in case the 'mid_dist' is greater than the largest unit. */
int i;
for (i = 0; i < ARRAY_SIZE(grid_steps) - 1; i++) {
grid_scale = grid_steps[i];
if (grid_scale > dist) {
break;
}
}
if (r_grid_unit) {
const void *usys;
int len;
BKE_unit_system_get(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
if (usys) {
*r_grid_unit = BKE_unit_display_name_get(usys, len - i - 1);
}
}
}
else {
grid_scale = ED_view3d_grid_scale(scene, v3d, r_grid_unit);
}
return grid_scale;
}
#undef STEPS_LEN
static void draw_view_axis(RegionView3D *rv3d, const rcti *rect)
{
const float k = U.rvisize * U.pixelsize; /* axis size */
/* axis alpha offset (rvibright has range 0-10) */
const int bright = -20 * (10 - U.rvibright);
/* Axis center in screen coordinates.
*
* - Unit size offset so small text doesn't draw outside the screen
* - Extra X offset because of the panel expander.
*/
const float startx = rect->xmax - (k + UI_UNIT_X * 1.5);
const float starty = rect->ymax - (k + UI_UNIT_Y);
float axis_pos[3][2];
uchar axis_col[3][4];
int axis_order[3] = {0, 1, 2};
axis_sort_v3(rv3d->viewinv[2], axis_order);
for (int axis_i = 0; axis_i < 3; axis_i++) {
int i = axis_order[axis_i];
/* get position of each axis tip on screen */
float vec[3] = {0.0f};
vec[i] = 1.0f;
mul_qt_v3(rv3d->viewquat, vec);
axis_pos[i][0] = startx + vec[0] * k;
axis_pos[i][1] = starty + vec[1] * k;
/* get color of each axis */
UI_GetThemeColorShade3ubv(TH_AXIS_X + i, bright, axis_col[i]); /* rgb */
axis_col[i][3] = 255 * hypotf(vec[0], vec[1]); /* alpha */
}
/* draw axis lines */
GPU_line_width(2.0f);
GPU_line_smooth(true);
GPU_blend(GPU_BLEND_ALPHA);
GPUVertFormat *format = immVertexFormat();
uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
immBegin(GPU_PRIM_LINES, 6);
for (int axis_i = 0; axis_i < 3; axis_i++) {
int i = axis_order[axis_i];
immAttr4ubv(col, axis_col[i]);
immVertex2f(pos, startx, starty);
immAttr4ubv(col, axis_col[i]);
immVertex2fv(pos, axis_pos[i]);
}
immEnd();
immUnbindProgram();
GPU_line_smooth(false);
/* draw axis names */
for (int axis_i = 0; axis_i < 3; axis_i++) {
int i = axis_order[axis_i];
const char axis_text[2] = {'x' + i, '\0'};
BLF_color4ubv(BLF_default(), axis_col[i]);
BLF_draw_default(axis_pos[i][0] + 2, axis_pos[i][1] + 2, 0.0f, axis_text, 1);
}
}
#ifdef WITH_INPUT_NDOF
/* draw center and axis of rotation for ongoing 3D mouse navigation */
static void draw_rotation_guide(const RegionView3D *rv3d)
{
float o[3]; /* center of rotation */
float end[3]; /* endpoints for drawing */
uchar color[4] = {0, 108, 255, 255}; /* bright blue so it matches device LEDs */
negate_v3_v3(o, rv3d->ofs);
GPU_blend(GPU_BLEND_ALPHA);
GPU_depth_mask(false); /* don't overwrite zbuf */
GPUVertFormat *format = immVertexFormat();
uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
immBindBuiltinProgram(GPU_SHADER_3D_SMOOTH_COLOR);
if (rv3d->rot_angle != 0.0f) {
/* -- draw rotation axis -- */
float scaled_axis[3];
const float scale = rv3d->dist;
mul_v3_v3fl(scaled_axis, rv3d->rot_axis, scale);
immBegin(GPU_PRIM_LINE_STRIP, 3);
color[3] = 0; /* more transparent toward the ends */
immAttr4ubv(col, color);
add_v3_v3v3(end, o, scaled_axis);
immVertex3fv(pos, end);
# if 0
color[3] = 0.2f + fabsf(rv3d->rot_angle); /* modulate opacity with angle */
/* ^^ neat idea, but angle is frame-rate dependent, so it's usually close to 0.2 */
# endif
color[3] = 127; /* more opaque toward the center */
immAttr4ubv(col, color);
immVertex3fv(pos, o);
color[3] = 0;
immAttr4ubv(col, color);
sub_v3_v3v3(end, o, scaled_axis);
immVertex3fv(pos, end);
immEnd();
/* -- draw ring around rotation center -- */
{
# define ROT_AXIS_DETAIL 13
const float s = 0.05f * scale;
const float step = 2.0f * (float)(M_PI / ROT_AXIS_DETAIL);
float q[4]; /* rotate ring so it's perpendicular to axis */
const int upright = fabsf(rv3d->rot_axis[2]) >= 0.95f;
if (!upright) {
const float up[3] = {0.0f, 0.0f, 1.0f};
float vis_angle, vis_axis[3];
cross_v3_v3v3(vis_axis, up, rv3d->rot_axis);
vis_angle = acosf(dot_v3v3(up, rv3d->rot_axis));
axis_angle_to_quat(q, vis_axis, vis_angle);
}
immBegin(GPU_PRIM_LINE_LOOP, ROT_AXIS_DETAIL);
color[3] = 63; /* somewhat faint */
immAttr4ubv(col, color);
float angle = 0.0f;
for (int i = 0; i < ROT_AXIS_DETAIL; i++, angle += step) {
float p[3] = {s * cosf(angle), s * sinf(angle), 0.0f};
if (!upright) {
mul_qt_v3(q, p);
}
add_v3_v3(p, o);
immVertex3fv(pos, p);
}
immEnd();
# undef ROT_AXIS_DETAIL
}
color[3] = 255; /* solid dot */
}
else {
color[3] = 127; /* see-through dot */
}
immUnbindProgram();
/* -- draw rotation center -- */
immBindBuiltinProgram(GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR);
GPU_point_size(5.0f);
immBegin(GPU_PRIM_POINTS, 1);
immAttr4ubv(col, color);
immVertex3fv(pos, o);
immEnd();
immUnbindProgram();
GPU_blend(GPU_BLEND_NONE);
GPU_depth_mask(true);
}
#endif /* WITH_INPUT_NDOF */
/**
* Render and camera border
*/
static void view3d_draw_border(const bContext *C, ARegion *region)
{
Scene *scene = CTX_data_scene(C);
Depsgraph *depsgraph = CTX_data_expect_evaluated_depsgraph(C);
RegionView3D *rv3d = region->regiondata;
View3D *v3d = CTX_wm_view3d(C);
if (rv3d->persp == RV3D_CAMOB) {
drawviewborder(scene, depsgraph, region, v3d);
}
else if (v3d->flag2 & V3D_RENDER_BORDER) {
drawrenderborder(region, v3d);
}
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Draw Text & Info
* \{ */
/**
* Draw Info
*/
static void view3d_draw_grease_pencil(const bContext *UNUSED(C))
{
/* TODO: viewport. */
}
/**
* Viewport Name
*/
static const char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
{
const char *name = NULL;
switch (rv3d->view) {
case RV3D_VIEW_FRONT:
if (rv3d->persp == RV3D_ORTHO) {
name = IFACE_("Front Orthographic");
}
else {
name = IFACE_("Front Perspective");
}
break;
case RV3D_VIEW_BACK:
if (rv3d->persp == RV3D_ORTHO) {
name = IFACE_("Back Orthographic");
}
else {
name = IFACE_("Back Perspective");
}
break;
case RV3D_VIEW_TOP:
if (rv3d->persp == RV3D_ORTHO) {
name = IFACE_("Top Orthographic");
}
else {
name = IFACE_("Top Perspective");
}
break;
case RV3D_VIEW_BOTTOM:
if (rv3d->persp == RV3D_ORTHO) {
name = IFACE_("Bottom Orthographic");
}
else {
name = IFACE_("Bottom Perspective");
}
break;
case RV3D_VIEW_RIGHT:
if (rv3d->persp == RV3D_ORTHO) {
name = IFACE_("Right Orthographic");
}
else {
name = IFACE_("Right Perspective");
}
break;
case RV3D_VIEW_LEFT:
if (rv3d->persp == RV3D_ORTHO) {
name = IFACE_("Left Orthographic");
}
else {
name = IFACE_("Left Perspective");
}
break;
default:
if (rv3d->persp == RV3D_CAMOB) {
if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
Camera *cam;
cam = v3d->camera->data;
if (cam->type == CAM_PERSP) {
name = IFACE_("Camera Perspective");
}
else if (cam->type == CAM_ORTHO) {
name = IFACE_("Camera Orthographic");
}
else {
BLI_assert(cam->type == CAM_PANO);
name = IFACE_("Camera Panoramic");
}
}
else {
name = IFACE_("Object as Camera");
}
}
else {
name = (rv3d->persp == RV3D_ORTHO) ? IFACE_("User Orthographic") :
IFACE_("User Perspective");
}
}
return name;
}
static void draw_viewport_name(ARegion *region, View3D *v3d, int xoffset, int *yoffset)
{
RegionView3D *rv3d = region->regiondata;
const char *name = view3d_get_name(v3d, rv3d);
const char *name_array[3] = {name, NULL, NULL};
int name_array_len = 1;
const int font_id = BLF_default();
/* 6 is the maximum size of the axis roll text. */
/* increase size for unicode languages (Chinese in utf-8...) */
#ifdef WITH_INTERNATIONAL
char tmpstr[96 + 6];
#else
char tmpstr[32 + 6];
#endif
BLF_enable(font_id, BLF_SHADOW);
BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
BLF_shadow_offset(font_id, 1, -1);
if (RV3D_VIEW_IS_AXIS(rv3d->view) && (rv3d->view_axis_roll != RV3D_VIEW_AXIS_ROLL_0)) {
const char *axis_roll;
switch (rv3d->view_axis_roll) {
case RV3D_VIEW_AXIS_ROLL_90:
axis_roll = " 90\xC2\xB0";
break;
case RV3D_VIEW_AXIS_ROLL_180:
axis_roll = " 180\xC2\xB0";
break;
default:
axis_roll = " -90\xC2\xB0";
break;
}
name_array[name_array_len++] = axis_roll;
}
if (v3d->localvd) {
name_array[name_array_len++] = IFACE_(" (Local)");
}
/* Indicate that clipping region is enabled. */
if (rv3d->rflag & RV3D_CLIPPING) {
name_array[name_array_len++] = IFACE_(" (Clipped)");
}
if (name_array_len > 1) {
BLI_string_join_array(tmpstr, sizeof(tmpstr), name_array, name_array_len);
name = tmpstr;
}
UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
*yoffset -= VIEW3D_OVERLAY_LINEHEIGHT;
BLF_draw_default(xoffset, *yoffset, 0.0f, name, sizeof(tmpstr));
BLF_disable(font_id, BLF_SHADOW);
}
/**
* Draw info beside axes in bottom left-corner:
* frame-number, collection, object name, bone name (if available), marker name (if available).
*/
static void draw_selected_name(
Scene *scene, ViewLayer *view_layer, Object *ob, int xoffset, int *yoffset)
{
const int cfra = CFRA;
const char *msg_pin = " (Pinned)";
const char *msg_sep = " : ";
const int font_id = BLF_default();
char info[300];
char *s = info;
s += sprintf(s, "(%d)", cfra);
if ((ob == NULL) || (ob->mode == OB_MODE_OBJECT)) {
LayerCollection *layer_collection = view_layer->active_collection;
s += sprintf(s,
" %s%s",
BKE_collection_ui_name_get(layer_collection->collection),
(ob == NULL) ? "" : " |");
}
/*
* info can contain:
* - a frame (7 + 2)
* - a collection name (MAX_NAME + 3)
* - 3 object names (MAX_NAME)
* - 2 BREAD_CRUMB_SEPARATORs (6)
* - a SHAPE_KEY_PINNED marker and a trailing '\0' (9+1) - translated, so give some room!
* - a marker name (MAX_NAME + 3)
*/
/* get name of marker on current frame (if available) */
const char *markern = BKE_scene_find_marker_name(scene, cfra);
/* check if there is an object */
if (ob) {
*s++ = ' ';
s += BLI_strcpy_rlen(s, ob->id.name + 2);
/* name(s) to display depends on type of object */
if (ob->type == OB_ARMATURE) {
bArmature *arm = ob->data;
/* show name of active bone too (if possible) */
if (arm->edbo) {
if (arm->act_edbone) {
s += BLI_strcpy_rlen(s, msg_sep);
s += BLI_strcpy_rlen(s, arm->act_edbone->name);
}
}
else if (ob->mode & OB_MODE_POSE) {
if (arm->act_bone) {
if (arm->act_bone->layer & arm->layer) {
s += BLI_strcpy_rlen(s, msg_sep);
s += BLI_strcpy_rlen(s, arm->act_bone->name);
}
}
}
}
else if (ELEM(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
/* try to display active bone and active shapekey too (if they exist) */
if (ob->type == OB_MESH && ob->mode & OB_MODE_WEIGHT_PAINT) {
Object *armobj = BKE_object_pose_armature_get(ob);
if (armobj && armobj->mode & OB_MODE_POSE) {
bArmature *arm = armobj->data;
if (arm->act_bone) {
if (arm->act_bone->layer & arm->layer) {
s += BLI_strcpy_rlen(s, msg_sep);
s += BLI_strcpy_rlen(s, arm->act_bone->name);
}
}
}
}
Key *key = BKE_key_from_object(ob);
if (key) {
KeyBlock *kb = BLI_findlink(&key->block, ob->shapenr - 1);
if (kb) {
s += BLI_strcpy_rlen(s, msg_sep);
s += BLI_strcpy_rlen(s, kb->name);
if (ob->shapeflag & OB_SHAPE_LOCK) {
s += BLI_strcpy_rlen(s, IFACE_(msg_pin));
}
}
}
}
/* color depends on whether there is a keyframe */
if (id_frame_has_keyframe(
(ID *)ob, /* BKE_scene_ctime_get(scene) */ (float)cfra, ANIMFILTER_KEYS_LOCAL)) {
UI_FontThemeColor(font_id, TH_TIME_KEYFRAME);
}
else if (ED_gpencil_has_keyframe_v3d(scene, ob, cfra)) {
UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
}
else {
UI_FontThemeColor(font_id, TH_TEXT_HI);
}
}
else {
/* no object */
if (ED_gpencil_has_keyframe_v3d(scene, NULL, cfra)) {
UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
}
else {
UI_FontThemeColor(font_id, TH_TEXT_HI);
}
}
if (markern) {
s += sprintf(s, " <%s>", markern);
}
BLF_enable(font_id, BLF_SHADOW);
BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
BLF_shadow_offset(font_id, 1, -1);
*yoffset -= VIEW3D_OVERLAY_LINEHEIGHT;
BLF_draw_default(xoffset, *yoffset, 0.0f, info, sizeof(info));
BLF_disable(font_id, BLF_SHADOW);
}
static void draw_grid_unit_name(
Scene *scene, ARegion *region, View3D *v3d, int xoffset, int *yoffset)
{
RegionView3D *rv3d = region->regiondata;
if (!rv3d->is_persp && RV3D_VIEW_IS_AXIS(rv3d->view)) {
const char *grid_unit = NULL;
int font_id = BLF_default();
ED_view3d_grid_view_scale(scene, v3d, region, &grid_unit);
if (grid_unit) {
char numstr[32] = "";
UI_FontThemeColor(font_id, TH_TEXT_HI);
if (v3d->grid != 1.0f) {
BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
}
*yoffset -= VIEW3D_OVERLAY_LINEHEIGHT;
BLF_enable(font_id, BLF_SHADOW);
BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
BLF_shadow_offset(font_id, 1, -1);
BLF_draw_default(xoffset, *yoffset, 0.0f, numstr[0] ? numstr : grid_unit, sizeof(numstr));
BLF_disable(font_id, BLF_SHADOW);
}
}
}
/**
* Information drawn on top of the solid plates and composed data
*/
void view3d_draw_region_info(const bContext *C, ARegion *region)
{
RegionView3D *rv3d = region->regiondata;
View3D *v3d = CTX_wm_view3d(C);
Scene *scene = CTX_data_scene(C);
wmWindowManager *wm = CTX_wm_manager(C);
Main *bmain = CTX_data_main(C);
ViewLayer *view_layer = CTX_data_view_layer(C);
#ifdef WITH_INPUT_NDOF
if ((U.ndof_flag & NDOF_SHOW_GUIDE) && ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) == 0) &&
(rv3d->persp != RV3D_CAMOB)) {
/* TODO: draw something else (but not this) during fly mode */
draw_rotation_guide(rv3d);
}
#endif
/* correct projection matrix */
ED_region_pixelspace(region);
/* local coordinate visible rect inside region, to accommodate overlapping ui */
const rcti *rect = ED_region_visible_rect(region);
view3d_draw_border(C, region);
view3d_draw_grease_pencil(C);
BLF_batch_draw_begin();
if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_NAVIGATE)) {
/* pass */
}
else {
switch ((eUserpref_MiniAxisType)U.mini_axis_type) {
case USER_MINI_AXIS_TYPE_GIZMO:
/* The gizmo handles its own drawing. */
break;
case USER_MINI_AXIS_TYPE_MINIMAL:
draw_view_axis(rv3d, rect);
case USER_MINI_AXIS_TYPE_NONE:
break;
}
}
int xoffset = rect->xmin + (0.5f * U.widget_unit);
int yoffset = rect->ymax - (0.1f * U.widget_unit);
if ((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0 && (v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0) {
if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_no_scrub(wm)) {
ED_scene_draw_fps(scene, xoffset, &yoffset);
}
else if (U.uiflag & USER_SHOW_VIEWPORTNAME) {
draw_viewport_name(region, v3d, xoffset, &yoffset);
}
if (U.uiflag & USER_DRAWVIEWINFO) {
Object *ob = OBACT(view_layer);
draw_selected_name(scene, view_layer, ob, xoffset, &yoffset);
}
if (v3d->gridflag & (V3D_SHOW_FLOOR | V3D_SHOW_X | V3D_SHOW_Y | V3D_SHOW_Z)) {
/* draw below the viewport name */
draw_grid_unit_name(scene, region, v3d, xoffset, &yoffset);
}
DRW_draw_region_engine_info(xoffset, &yoffset, VIEW3D_OVERLAY_LINEHEIGHT);
}
if ((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0 && (v3d->overlay.flag & V3D_OVERLAY_STATS)) {
View3D *v3d_local = v3d->localvd ? v3d : NULL;
ED_info_draw_stats(
bmain, scene, view_layer, v3d_local, xoffset, &yoffset, VIEW3D_OVERLAY_LINEHEIGHT);
}
BLF_batch_draw_end();
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Draw Viewport Contents
* \{ */
static void view3d_draw_view(const bContext *C, ARegion *region)
{
ED_view3d_draw_setup_view(CTX_wm_manager(C),
CTX_wm_window(C),
CTX_data_expect_evaluated_depsgraph(C),
CTX_data_scene(C),
region,
CTX_wm_view3d(C),
NULL,
NULL,
NULL);
/* Only 100% compliant on new spec goes below */
DRW_draw_view(C);
}
RenderEngineType *ED_view3d_engine_type(const Scene *scene, int drawtype)
{
/*
* Temporary viewport draw modes until we have a proper system.
* all modes are done in the draw manager, except external render
* engines like Cycles.
*/
RenderEngineType *type = RE_engines_find(scene->r.engine);
if (drawtype == OB_MATERIAL && (type->flag & RE_USE_EEVEE_VIEWPORT)) {
return RE_engines_find(RE_engine_id_BLENDER_EEVEE);
}
return type;
}
void view3d_main_region_draw(const bContext *C, ARegion *region)
{
Main *bmain = CTX_data_main(C);
View3D *v3d = CTX_wm_view3d(C);
view3d_draw_view(C, region);
DRW_cache_free_old_batches(bmain);
BKE_image_free_old_gputextures(bmain);
GPU_pass_cache_garbage_collect();
/* No depth test for drawing action zones afterwards. */
GPU_depth_test(GPU_DEPTH_NONE);
v3d->runtime.flag &= ~V3D_RUNTIME_DEPTHBUF_OVERRIDDEN;
/* TODO: Clear cache? */
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Off-screen Drawing
* \{ */
static void view3d_stereo3d_setup_offscreen(Depsgraph *depsgraph,
const Scene *scene,
View3D *v3d,
ARegion *region,
const float winmat[4][4],
const char *viewname)
{
/* update the viewport matrices with the new camera */
if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
float viewmat[4][4];
const bool is_left = STREQ(viewname, STEREO_LEFT_NAME);
BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
view3d_main_region_setup_offscreen(depsgraph, scene, v3d, region, viewmat, winmat);
}
else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
float viewmat[4][4];
Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
view3d_main_region_setup_offscreen(depsgraph, scene, v3d, region, viewmat, winmat);
}
}
void ED_view3d_draw_offscreen(Depsgraph *depsgraph,
const Scene *scene,
eDrawType drawtype,
View3D *v3d,
ARegion *region,
int winx,
int winy,
const float viewmat[4][4],
const float winmat[4][4],
bool is_image_render,
bool draw_background,
const char *viewname,
const bool do_color_management,
const bool restore_rv3d_mats,
GPUOffScreen *ofs,
GPUViewport *viewport)
{
RegionView3D *rv3d = region->regiondata;
RenderEngineType *engine_type = ED_view3d_engine_type(scene, drawtype);
/* Store `orig` variables. */
struct {
struct bThemeState theme_state;
/* #View3D */
eDrawType v3d_shading_type;
/* #Region */
int region_winx, region_winy;
rcti region_winrct;
/* #RegionView3D */
/**
* Needed so the value won't be left overwritten,
* Without this the #wmPaintCursor can't use the pixel size & view matrices for drawing.
*/
struct RV3DMatrixStore *rv3d_mats;
} orig = {
.v3d_shading_type = v3d->shading.type,
.region_winx = region->winx,
.region_winy = region->winy,
.region_winrct = region->winrct,
.rv3d_mats = ED_view3d_mats_rv3d_backup(region->regiondata),
};
UI_Theme_Store(&orig.theme_state);
UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
/* Set temporary new size. */
region->winx = winx;
region->winy = winy;
region->winrct.xmin = 0;
region->winrct.ymin = 0;
region->winrct.xmax = winx;
region->winrct.ymax = winy;
/* There are too many functions inside the draw manager that check the shading type,
* so use a temporary override instead. */
v3d->shading.type = drawtype;
/* Set flags. */
G.f |= G_FLAG_RENDER_VIEWPORT;
{
/* free images which can have changed on frame-change
* warning! can be slow so only free animated images - campbell */
BKE_image_free_anim_gputextures(G.main); /* XXX :((( */
}
GPU_matrix_push_projection();
GPU_matrix_identity_set();
GPU_matrix_push();
GPU_matrix_identity_set();
if ((viewname != NULL && viewname[0] != '\0') && (viewmat == NULL) &&
rv3d->persp == RV3D_CAMOB && v3d->camera) {
view3d_stereo3d_setup_offscreen(depsgraph, scene, v3d, region, winmat, viewname);
}
else {
view3d_main_region_setup_offscreen(depsgraph, scene, v3d, region, viewmat, winmat);
}
/* main drawing call */
DRW_draw_render_loop_offscreen(depsgraph,
engine_type,
region,
v3d,
is_image_render,
draw_background,
do_color_management,
ofs,
viewport);
GPU_matrix_pop_projection();
GPU_matrix_pop();
/* Restore all `orig` members. */
region->winx = orig.region_winx;
region->winy = orig.region_winy;
region->winrct = orig.region_winrct;
/* Optionally do _not_ restore rv3d matrices (e.g. they are used/stored in the ImBuff for
* reprojection, see texture_paint_image_from_view_exec(). */
if (restore_rv3d_mats) {
ED_view3d_mats_rv3d_restore(region->regiondata, orig.rv3d_mats);
}
MEM_freeN(orig.rv3d_mats);
UI_Theme_Restore(&orig.theme_state);
v3d->shading.type = orig.v3d_shading_type;
G.f &= ~G_FLAG_RENDER_VIEWPORT;
}
/**
* Creates own fake 3d views (wrapping #ED_view3d_draw_offscreen). Similar too
* #ED_view_draw_offscreen_imbuf_simple, but takes view/projection matrices as arguments.
*/
void ED_view3d_draw_offscreen_simple(Depsgraph *depsgraph,
Scene *scene,
View3DShading *shading_override,
eDrawType drawtype,
int winx,
int winy,
uint draw_flags,
const float viewmat[4][4],
const float winmat[4][4],
float clip_start,
float clip_end,
bool is_xr_surface,
bool is_image_render,
bool draw_background,
const char *viewname,
const bool do_color_management,
GPUOffScreen *ofs,
GPUViewport *viewport)
{
View3D v3d = {NULL};
ARegion ar = {NULL};
RegionView3D rv3d = {{{0}}};
v3d.regionbase.first = v3d.regionbase.last = &ar;
ar.regiondata = &rv3d;
ar.regiontype = RGN_TYPE_WINDOW;
View3DShading *source_shading_settings = &scene->display.shading;
if (draw_flags & V3D_OFSDRAW_OVERRIDE_SCENE_SETTINGS && shading_override != NULL) {
source_shading_settings = shading_override;
}
memcpy(&v3d.shading, source_shading_settings, sizeof(View3DShading));
v3d.shading.type = drawtype;
if (shading_override) {
/* Pass. */
}
else if (drawtype == OB_MATERIAL) {
v3d.shading.flag = V3D_SHADING_SCENE_WORLD | V3D_SHADING_SCENE_LIGHTS;
}
if ((draw_flags & ~V3D_OFSDRAW_OVERRIDE_SCENE_SETTINGS) == V3D_OFSDRAW_NONE) {
v3d.flag2 = V3D_HIDE_OVERLAYS;
}
else {
if (draw_flags & V3D_OFSDRAW_SHOW_ANNOTATION) {
v3d.flag2 |= V3D_SHOW_ANNOTATION;
}
if (draw_flags & V3D_OFSDRAW_SHOW_GRIDFLOOR) {
v3d.gridflag |= V3D_SHOW_FLOOR | V3D_SHOW_X | V3D_SHOW_Y;
v3d.grid = 1.0f;
v3d.gridlines = 16;
v3d.gridsubdiv = 10;
}
if (draw_flags & V3D_OFSDRAW_SHOW_SELECTION) {
v3d.flag |= V3D_SELECT_OUTLINE;
}
if (draw_flags & V3D_OFSDRAW_XR_SHOW_CONTROLLERS) {
v3d.flag2 |= V3D_XR_SHOW_CONTROLLERS;
}
if (draw_flags & V3D_OFSDRAW_XR_SHOW_CUSTOM_OVERLAYS) {
v3d.flag2 |= V3D_XR_SHOW_CUSTOM_OVERLAYS;
}
/* Disable other overlays (set all available _HIDE_ flags). */
v3d.overlay.flag |= V3D_OVERLAY_HIDE_CURSOR | V3D_OVERLAY_HIDE_TEXT |
V3D_OVERLAY_HIDE_MOTION_PATHS | V3D_OVERLAY_HIDE_BONES |
V3D_OVERLAY_HIDE_OBJECT_XTRAS | V3D_OVERLAY_HIDE_OBJECT_ORIGINS;
v3d.flag |= V3D_HIDE_HELPLINES;
}
if (is_xr_surface) {
v3d.flag |= V3D_XR_SESSION_SURFACE;
}
rv3d.persp = RV3D_PERSP;
v3d.clip_start = clip_start;
v3d.clip_end = clip_end;
/* Actually not used since we pass in the projection matrix. */
v3d.lens = 0;
ED_view3d_draw_offscreen(depsgraph,
scene,
drawtype,
&v3d,
&ar,
winx,
winy,
viewmat,
winmat,
is_image_render,
draw_background,
viewname,
do_color_management,
true,
ofs,
viewport);
}
/**
* Utility func for ED_view3d_draw_offscreen
*
* \param ofs: Optional off-screen buffer, can be NULL.
* (avoids re-creating when doing multiple GL renders).
*/
ImBuf *ED_view3d_draw_offscreen_imbuf(Depsgraph *depsgraph,
Scene *scene,
eDrawType drawtype,
View3D *v3d,
ARegion *region,
int sizex,
int sizey,
eImBufFlags imbuf_flag,
int alpha_mode,
const char *viewname,
const bool restore_rv3d_mats,
/* output vars */
GPUOffScreen *ofs,
char err_out[256])
{
RegionView3D *rv3d = region->regiondata;
const bool draw_sky = (alpha_mode == R_ADDSKY);
/* view state */
bool is_ortho = false;
float winmat[4][4];
if (ofs && ((GPU_offscreen_width(ofs) != sizex) || (GPU_offscreen_height(ofs) != sizey))) {
/* sizes differ, can't reuse */
ofs = NULL;
}
GPUFrameBuffer *old_fb = GPU_framebuffer_active_get();
if (old_fb) {
GPU_framebuffer_restore();
}
const bool own_ofs = (ofs == NULL);
DRW_opengl_context_enable();
if (own_ofs) {
/* bind */
ofs = GPU_offscreen_create(sizex, sizey, true, GPU_RGBA8, err_out);
if (ofs == NULL) {
DRW_opengl_context_disable();
return NULL;
}
}
GPU_offscreen_bind(ofs, true);
/* read in pixels & stamp */
ImBuf *ibuf = IMB_allocImBuf(sizex, sizey, 32, imbuf_flag);
/* render 3d view */
if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
CameraParams params;
Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
const Object *camera_eval = DEG_get_evaluated_object(depsgraph, camera);
BKE_camera_params_init(¶ms);
/* fallback for non camera objects */
params.clip_start = v3d->clip_start;
params.clip_end = v3d->clip_end;
BKE_camera_params_from_object(¶ms, camera_eval);
BKE_camera_multiview_params(&scene->r, ¶ms, camera_eval, viewname);
BKE_camera_params_compute_viewplane(¶ms, sizex, sizey, scene->r.xasp, scene->r.yasp);
BKE_camera_params_compute_matrix(¶ms);
is_ortho = params.is_ortho;
copy_m4_m4(winmat, params.winmat);
}
else {
rctf viewplane;
float clip_start, clipend;
is_ortho = ED_view3d_viewplane_get(
depsgraph, v3d, rv3d, sizex, sizey, &viewplane, &clip_start, &clipend, NULL);
if (is_ortho) {
orthographic_m4(winmat,
viewplane.xmin,
viewplane.xmax,
viewplane.ymin,
viewplane.ymax,
-clipend,
clipend);
}
else {
perspective_m4(winmat,
viewplane.xmin,
viewplane.xmax,
viewplane.ymin,
viewplane.ymax,
clip_start,
clipend);
}
}
/* XXX(jbakker): `do_color_management` should be controlled by the caller. Currently when doing a
* viewport render animation and saving to an 8bit file format, color management would be applied
* twice. Once here, and once when saving the saving to disk. In this case the Save As Render
* option cannot be controlled either. But when doing an off-screen render you want to do the
* color management here.
*
* This option was added here to increase the performance for quick view-port preview renders.
* When using workbench the color differences haven't been reported as a bug. But users also use
* the viewport rendering to render Eevee scenes. In the later situation the saved colors are
* totally wrong. */
const bool do_color_management = (ibuf->rect_float == NULL);
ED_view3d_draw_offscreen(depsgraph,
scene,
drawtype,
v3d,
region,
sizex,
sizey,
NULL,
winmat,
true,
draw_sky,
viewname,
do_color_management,
restore_rv3d_mats,
ofs,
NULL);
if (ibuf->rect_float) {
GPU_offscreen_read_pixels(ofs, GPU_DATA_FLOAT, ibuf->rect_float);
}
else if (ibuf->rect) {
GPU_offscreen_read_pixels(ofs, GPU_DATA_UBYTE, ibuf->rect);
}
/* unbind */
GPU_offscreen_unbind(ofs, true);
if (own_ofs) {
GPU_offscreen_free(ofs);
}
DRW_opengl_context_disable();
if (old_fb) {
GPU_framebuffer_bind(old_fb);
}
if (ibuf->rect_float && ibuf->rect) {
IMB_rect_from_float(ibuf);
}
return ibuf;
}
/**
* Creates own fake 3d views (wrapping #ED_view3d_draw_offscreen_imbuf)
*
* \param ofs: Optional off-screen buffer can be NULL.
* (avoids re-creating when doing multiple GL renders).
*
* \note used by the sequencer
*/
ImBuf *ED_view3d_draw_offscreen_imbuf_simple(Depsgraph *depsgraph,
Scene *scene,
View3DShading *shading_override,
eDrawType drawtype,
Object *camera,
int width,
int height,
eImBufFlags imbuf_flag,
eV3DOffscreenDrawFlag draw_flags,
int alpha_mode,
const char *viewname,
GPUOffScreen *ofs,
char err_out[256])
{
View3D v3d = {NULL};
ARegion region = {NULL};
RegionView3D rv3d = {{{0}}};
/* connect data */
v3d.regionbase.first = v3d.regionbase.last = ®ion;
region.regiondata = &rv3d;
region.regiontype = RGN_TYPE_WINDOW;
v3d.camera = camera;
View3DShading *source_shading_settings = &scene->display.shading;
if (draw_flags & V3D_OFSDRAW_OVERRIDE_SCENE_SETTINGS && shading_override != NULL) {
source_shading_settings = shading_override;
}
memcpy(&v3d.shading, source_shading_settings, sizeof(View3DShading));
if (drawtype == OB_RENDER) {
/* Don't use external engines for preview. Fall back to solid instead of Eevee as rendering
* with Eevee is potentially slow due to compiling shaders and loading textures, and the
* depsgraph may not have been updated to have all the right geometry attributes. */
if (!(BKE_scene_uses_blender_eevee(scene) || BKE_scene_uses_blender_workbench(scene))) {
drawtype = OB_SOLID;
}
}
if (drawtype == OB_MATERIAL) {
v3d.shading.flag = V3D_SHADING_SCENE_WORLD | V3D_SHADING_SCENE_LIGHTS;
v3d.shading.render_pass = SCE_PASS_COMBINED;
}
else if (drawtype == OB_RENDER) {
v3d.shading.flag = V3D_SHADING_SCENE_WORLD_RENDER | V3D_SHADING_SCENE_LIGHTS_RENDER;
v3d.shading.render_pass = SCE_PASS_COMBINED;
}
else if (drawtype == OB_TEXTURE) {
drawtype = OB_SOLID;
v3d.shading.light = V3D_LIGHTING_STUDIO;
v3d.shading.color_type = V3D_SHADING_TEXTURE_COLOR;
}
v3d.shading.type = drawtype;
v3d.flag2 = V3D_HIDE_OVERLAYS;
/* HACK: When rendering gpencil objects this opacity is used to mix vertex colors in when not in
* render mode. */
v3d.overlay.gpencil_vertex_paint_opacity = 1.0f;
if (draw_flags & V3D_OFSDRAW_SHOW_ANNOTATION) {
v3d.flag2 |= V3D_SHOW_ANNOTATION;
}
if (draw_flags & V3D_OFSDRAW_SHOW_GRIDFLOOR) {
v3d.gridflag |= V3D_SHOW_FLOOR | V3D_SHOW_X | V3D_SHOW_Y;
}
v3d.shading.background_type = V3D_SHADING_BACKGROUND_WORLD;
rv3d.persp = RV3D_CAMOB;
copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
normalize_m4(rv3d.viewinv);
invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
{
CameraParams params;
const Object *view_camera_eval = DEG_get_evaluated_object(
depsgraph, BKE_camera_multiview_render(scene, v3d.camera, viewname));
BKE_camera_params_init(¶ms);
BKE_camera_params_from_object(¶ms, view_camera_eval);
BKE_camera_multiview_params(&scene->r, ¶ms, view_camera_eval, viewname);
BKE_camera_params_compute_viewplane(¶ms, width, height, scene->r.xasp, scene->r.yasp);
BKE_camera_params_compute_matrix(¶ms);
copy_m4_m4(rv3d.winmat, params.winmat);
v3d.clip_start = params.clip_start;
v3d.clip_end = params.clip_end;
v3d.lens = params.lens;
}
mul_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
invert_m4_m4(rv3d.persinv, rv3d.viewinv);
return ED_view3d_draw_offscreen_imbuf(depsgraph,
scene,
v3d.shading.type,
&v3d,
®ion,
width,
height,
imbuf_flag,
alpha_mode,
viewname,
true,
ofs,
err_out);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Viewport Clipping
* \{ */
static bool view3d_clipping_test(const float co[3], const float clip[6][4])
{
if (plane_point_side_v3(clip[0], co) > 0.0f) {
if (plane_point_side_v3(clip[1], co) > 0.0f) {
if (plane_point_side_v3(clip[2], co) > 0.0f) {
if (plane_point_side_v3(clip[3], co) > 0.0f) {
return false;
}
}
}
}
return true;
}
/**
* Return true when `co` is hidden by the 3D views clipping planes.
*
* \param local: When true use local (object-space) #ED_view3d_clipping_local must run first,
* then all comparisons can be done in local-space.
* \return True when `co` is outside all clipping planes.
*
* \note Callers should check #RV3D_CLIPPING_ENABLED first.
*/
bool ED_view3d_clipping_test(const RegionView3D *rv3d, const float co[3], const bool is_local)
{
return view3d_clipping_test(co, is_local ? rv3d->clip_local : rv3d->clip);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Back-Draw for Selection
* \{ */
/**
* \note Only use in object mode.
*/
static void validate_object_select_id(struct Depsgraph *depsgraph,
ViewLayer *view_layer,
ARegion *region,
View3D *v3d,
Object *obact)
{
/* TODO: Use a flag in the selection engine itself. */
if (v3d->runtime.flag & V3D_RUNTIME_DEPTHBUF_OVERRIDDEN) {
return;
}
Object *obact_eval = DEG_get_evaluated_object(depsgraph, obact);
BLI_assert(region->regiontype == RGN_TYPE_WINDOW);
UNUSED_VARS_NDEBUG(region);
if (obact_eval && (obact_eval->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT) ||
BKE_paint_select_face_test(obact_eval))) {
/* do nothing */
}
/* texture paint mode sampling */
else if (obact_eval && (obact_eval->mode & OB_MODE_TEXTURE_PAINT) &&
(v3d->shading.type > OB_WIRE)) {
/* do nothing */
}
else if ((obact_eval && (obact_eval->mode & OB_MODE_PARTICLE_EDIT)) && !XRAY_ENABLED(v3d)) {
/* do nothing */
}
else {
v3d->runtime.flag |= V3D_RUNTIME_DEPTHBUF_OVERRIDDEN;
return;
}
if (obact_eval && ((obact_eval->base_flag & BASE_VISIBLE_DEPSGRAPH) != 0)) {
Base *base = BKE_view_layer_base_find(view_layer, obact);
DRW_select_buffer_context_create(&base, 1, -1);
}
v3d->runtime.flag |= V3D_RUNTIME_DEPTHBUF_OVERRIDDEN;
}
/* TODO: Creating, attaching texture, and destroying a framebuffer is quite slow.
* Calling this function should be avoided during interactive drawing. */
static void view3d_opengl_read_Z_pixels(GPUViewport *viewport, rcti *rect, void *data)
{
GPUTexture *depth_tx = GPU_viewport_depth_texture(viewport);
GPUFrameBuffer *depth_read_fb = NULL;
GPU_framebuffer_ensure_config(&depth_read_fb,
{
GPU_ATTACHMENT_TEXTURE(depth_tx),
GPU_ATTACHMENT_NONE,
});
GPU_framebuffer_bind(depth_read_fb);
GPU_framebuffer_read_depth(depth_read_fb,
rect->xmin,
rect->ymin,
BLI_rcti_size_x(rect),
BLI_rcti_size_y(rect),
GPU_DATA_FLOAT,
data);
GPU_framebuffer_restore();
GPU_framebuffer_free(depth_read_fb);
}
void ED_view3d_select_id_validate(ViewContext *vc)
{
validate_object_select_id(vc->depsgraph, vc->view_layer, vc->region, vc->v3d, vc->obact);
}
/**
* allow for small values [0.5 - 2.5],
* and large values, FLT_MAX by clamping by the area size
*/
int ED_view3d_backbuf_sample_size_clamp(ARegion *region, const float dist)
{
return (int)min_ff(ceilf(dist), (float)max_ii(region->winx, region->winx));
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Z-Depth Utilities
* \{ */
void view3d_depths_rect_create(ARegion *region, rcti *rect, ViewDepths *r_d)
{
/* clamp rect by region */
rcti r = {
.xmin = 0,
.xmax = region->winx - 1,
.ymin = 0,
.ymax = region->winy - 1,
};
/* Constrain rect to depth bounds */
BLI_rcti_isect(&r, rect, rect);
/* assign values to compare with the ViewDepths */
int x = rect->xmin;
int y = rect->ymin;
int w = BLI_rcti_size_x(rect);
int h = BLI_rcti_size_y(rect);
if (w <= 0 || h <= 0) {
r_d->depths = NULL;
return;
}
r_d->x = x;
r_d->y = y;
r_d->w = w;
r_d->h = h;
r_d->depths = MEM_mallocN(sizeof(float) * w * h, "View depths Subset");
{
GPUViewport *viewport = WM_draw_region_get_viewport(region);
view3d_opengl_read_Z_pixels(viewport, rect, r_d->depths);
/* Range is assumed to be this as they are never changed. */
r_d->depth_range[0] = 0.0;
r_d->depth_range[1] = 1.0;
}
}
/* NOTE: with nouveau drivers the glReadPixels() is very slow. T24339. */
static ViewDepths *view3d_depths_create(ARegion *region)
{
ViewDepths *d = MEM_callocN(sizeof(ViewDepths), "ViewDepths");
d->w = region->winx;
d->h = region->winy;
{
GPUViewport *viewport = WM_draw_region_get_viewport(region);
GPUTexture *depth_tx = GPU_viewport_depth_texture(viewport);
uint32_t *int_depths = GPU_texture_read(depth_tx, GPU_DATA_UINT_24_8, 0);
d->depths = (float *)int_depths;
/* Convert in-place. */
int pixel_count = GPU_texture_width(depth_tx) * GPU_texture_height(depth_tx);
for (int i = 0; i < pixel_count; i++) {
d->depths[i] = (int_depths[i] >> 8u) / (float)0xFFFFFF;
}
/* Assumed to be this as they are never changed. */
d->depth_range[0] = 0.0;
d->depth_range[1] = 1.0;
}
return d;
}
/* Utility function to find the closest Z value, use for auto-depth. */
float view3d_depth_near(ViewDepths *d)
{
/* Convert to float for comparisons. */
const float near = (float)d->depth_range[0];
const float far_real = (float)d->depth_range[1];
float far = far_real;
const float *depths = d->depths;
float depth = FLT_MAX;
int i = (int)d->w * (int)d->h; /* Cast to avoid short overflow. */
/* Far is both the starting 'far' value
* and the closest value found. */
while (i--) {
depth = *depths++;
if ((depth < far) && (depth > near)) {
far = depth;
}
}
return far == far_real ? FLT_MAX : far;
}
/**
* Redraw the viewport depth buffer.
*
* \param mode: V3D_DEPTH_NO_GPENCIL - Redraw viewport without Grease Pencil and Annotations.
* V3D_DEPTH_GPENCIL_ONLY - Redraw viewport with Grease Pencil and Annotations only.
* V3D_DEPTH_OBJECT_ONLY - Redraw viewport with active object only.
* \param update_cache: If true, store the entire depth buffer in #rv3d->depths.
*/
void ED_view3d_depth_override(Depsgraph *depsgraph,
ARegion *region,
View3D *v3d,
Object *obact,
eV3DDepthOverrideMode mode,
ViewDepths **r_depths)
{
if (v3d->runtime.flag & V3D_RUNTIME_DEPTHBUF_OVERRIDDEN) {
/* Force redraw if `r_depths` is required. */
if (!r_depths || *r_depths != NULL) {
return;
}
}
struct bThemeState theme_state;
Scene *scene = DEG_get_evaluated_scene(depsgraph);
RegionView3D *rv3d = region->regiondata;
short flag = v3d->flag;
/* temp set drawtype to solid */
/* Setting these temporarily is not nice */
v3d->flag &= ~V3D_SELECT_OUTLINE;
/* Tools may request depth outside of regular drawing code. */
UI_Theme_Store(&theme_state);
UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
ED_view3d_draw_setup_view(
G_MAIN->wm.first, NULL, depsgraph, scene, region, v3d, NULL, NULL, NULL);
/* get surface depth without bias */
rv3d->rflag |= RV3D_ZOFFSET_DISABLED;
/* Needed in cases the 3D Viewport isn't already setup. */
WM_draw_region_viewport_ensure(region, SPACE_VIEW3D);
WM_draw_region_viewport_bind(region);
GPUViewport *viewport = WM_draw_region_get_viewport(region);
/* When Blender is starting, a click event can trigger a depth test while the viewport is not
* yet available. */
if (viewport != NULL) {
switch (mode) {
case V3D_DEPTH_NO_GPENCIL:
DRW_draw_depth_loop(depsgraph, region, v3d, viewport);
break;
case V3D_DEPTH_GPENCIL_ONLY:
DRW_draw_depth_loop_gpencil(depsgraph, region, v3d, viewport);
break;
case V3D_DEPTH_OBJECT_ONLY:
DRW_draw_depth_object(
scene, region, v3d, viewport, DEG_get_evaluated_object(depsgraph, obact));
break;
}
if (r_depths) {
if (*r_depths) {
ED_view3d_depths_free(*r_depths);
}
*r_depths = view3d_depths_create(region);
}
}
WM_draw_region_viewport_unbind(region);
rv3d->rflag &= ~RV3D_ZOFFSET_DISABLED;
v3d->flag = flag;
v3d->runtime.flag |= V3D_RUNTIME_DEPTHBUF_OVERRIDDEN;
UI_Theme_Restore(&theme_state);
}
void ED_view3d_depths_free(ViewDepths *depths)
{
if (depths->depths) {
MEM_freeN(depths->depths);
}
MEM_freeN(depths);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Custom-data Utilities
* \{ */
void ED_view3d_datamask(const bContext *C,
const Scene *UNUSED(scene),
const View3D *v3d,
CustomData_MeshMasks *r_cddata_masks)
{
if (ELEM(v3d->shading.type, OB_TEXTURE, OB_MATERIAL, OB_RENDER)) {
r_cddata_masks->lmask |= CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL;
r_cddata_masks->vmask |= CD_MASK_ORCO | CD_MASK_PROP_COLOR;
}
else if (v3d->shading.type == OB_SOLID) {
if (v3d->shading.color_type == V3D_SHADING_TEXTURE_COLOR) {
r_cddata_masks->lmask |= CD_MASK_MLOOPUV;
}
if (v3d->shading.color_type == V3D_SHADING_VERTEX_COLOR) {
r_cddata_masks->lmask |= CD_MASK_MLOOPCOL;
r_cddata_masks->vmask |= CD_MASK_ORCO | CD_MASK_PROP_COLOR;
}
}
if ((CTX_data_mode_enum(C) == CTX_MODE_EDIT_MESH) &&
(v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_WEIGHT)) {
r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
}
if ((CTX_data_mode_enum(C) == CTX_MODE_SCULPT)) {
r_cddata_masks->vmask |= CD_MASK_PAINT_MASK;
r_cddata_masks->pmask |= CD_MASK_SCULPT_FACE_SETS;
}
}
/* Goes over all modes and view3d settings. */
void ED_view3d_screen_datamask(const bContext *C,
const Scene *scene,
const bScreen *screen,
CustomData_MeshMasks *r_cddata_masks)
{
CustomData_MeshMasks_update(r_cddata_masks, &CD_MASK_BAREMESH);
/* Check if we need tfaces & mcols due to view mode. */
LISTBASE_FOREACH (const ScrArea *, area, &screen->areabase) {
if (area->spacetype == SPACE_VIEW3D) {
ED_view3d_datamask(C, scene, area->spacedata.first, r_cddata_masks);
}
}
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Region View Matrix Backup/Restore
* \{ */
/**
* Store values from #RegionView3D, set when drawing.
* This is needed when we draw with to a viewport using a different matrix
* (offscreen drawing for example).
*
* Values set by #ED_view3d_update_viewmat should be handled here.
*/
struct RV3DMatrixStore {
float winmat[4][4];
float viewmat[4][4];
float viewinv[4][4];
float persmat[4][4];
float persinv[4][4];
float viewcamtexcofac[4];
float pixsize;
};
struct RV3DMatrixStore *ED_view3d_mats_rv3d_backup(struct RegionView3D *rv3d)
{
struct RV3DMatrixStore *rv3dmat = MEM_mallocN(sizeof(*rv3dmat), __func__);
copy_m4_m4(rv3dmat->winmat, rv3d->winmat);
copy_m4_m4(rv3dmat->viewmat, rv3d->viewmat);
copy_m4_m4(rv3dmat->persmat, rv3d->persmat);
copy_m4_m4(rv3dmat->persinv, rv3d->persinv);
copy_m4_m4(rv3dmat->viewinv, rv3d->viewinv);
copy_v4_v4(rv3dmat->viewcamtexcofac, rv3d->viewcamtexcofac);
rv3dmat->pixsize = rv3d->pixsize;
return rv3dmat;
}
void ED_view3d_mats_rv3d_restore(struct RegionView3D *rv3d, struct RV3DMatrixStore *rv3dmat_pt)
{
struct RV3DMatrixStore *rv3dmat = rv3dmat_pt;
copy_m4_m4(rv3d->winmat, rv3dmat->winmat);
copy_m4_m4(rv3d->viewmat, rv3dmat->viewmat);
copy_m4_m4(rv3d->persmat, rv3dmat->persmat);
copy_m4_m4(rv3d->persinv, rv3dmat->persinv);
copy_m4_m4(rv3d->viewinv, rv3dmat->viewinv);
copy_v4_v4(rv3d->viewcamtexcofac, rv3dmat->viewcamtexcofac);
rv3d->pixsize = rv3dmat->pixsize;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name FPS Drawing
* \{ */
/**
* \note The info that this uses is updated in #ED_refresh_viewport_fps,
* which currently gets called during #SCREEN_OT_animation_step.
*/
void ED_scene_draw_fps(const Scene *scene, int xoffset, int *yoffset)
{
ScreenFrameRateInfo *fpsi = scene->fps_info;
char printable[16];
if (!fpsi || !fpsi->lredrawtime || !fpsi->redrawtime) {
return;
}
printable[0] = '\0';
/* Doing an average for a more robust calculation. */
fpsi->redrawtimes_fps[fpsi->redrawtime_index] = (float)(1.0 /
(fpsi->lredrawtime - fpsi->redrawtime));
float fps = 0.0f;
int tot = 0;
for (int i = 0; i < REDRAW_FRAME_AVERAGE; i++) {
if (fpsi->redrawtimes_fps[i]) {
fps += fpsi->redrawtimes_fps[i];
tot++;
}
}
if (tot) {
fpsi->redrawtime_index = (fpsi->redrawtime_index + 1) % REDRAW_FRAME_AVERAGE;
fps = fps / tot;
}
const int font_id = BLF_default();
/* Is this more than half a frame behind? */
if (fps + 0.5f < (float)(FPS)) {
UI_FontThemeColor(font_id, TH_REDALERT);
BLI_snprintf(printable, sizeof(printable), IFACE_("fps: %.2f"), fps);
}
else {
UI_FontThemeColor(font_id, TH_TEXT_HI);
BLI_snprintf(printable, sizeof(printable), IFACE_("fps: %i"), (int)(fps + 0.5f));
}
BLF_enable(font_id, BLF_SHADOW);
BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
BLF_shadow_offset(font_id, 1, -1);
*yoffset -= VIEW3D_OVERLAY_LINEHEIGHT;
BLF_draw_default(xoffset, *yoffset, 0.0f, printable, sizeof(printable));
BLF_disable(font_id, BLF_SHADOW);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Calculate Render Border
* \{ */
static bool view3d_main_region_do_render_draw(const Scene *scene)
{
RenderEngineType *type = RE_engines_find(scene->r.engine);
return (type && type->view_update && type->view_draw);
}
bool ED_view3d_calc_render_border(
const Scene *scene, Depsgraph *depsgraph, View3D *v3d, ARegion *region, rcti *rect)
{
RegionView3D *rv3d = region->regiondata;
bool use_border;
/* Test if there is a 3d view rendering. */
if (v3d->shading.type != OB_RENDER || !view3d_main_region_do_render_draw(scene)) {
return false;
}
/* Test if there is a border render. */
if (rv3d->persp == RV3D_CAMOB) {
use_border = (scene->r.mode & R_BORDER) != 0;
}
else {
use_border = (v3d->flag2 & V3D_RENDER_BORDER) != 0;
}
if (!use_border) {
return false;
}
/* Compute border. */
if (rv3d->persp == RV3D_CAMOB) {
rctf viewborder;
ED_view3d_calc_camera_border(scene, depsgraph, region, v3d, rv3d, &viewborder, false);
rect->xmin = viewborder.xmin + scene->r.border.xmin * BLI_rctf_size_x(&viewborder);
rect->ymin = viewborder.ymin + scene->r.border.ymin * BLI_rctf_size_y(&viewborder);
rect->xmax = viewborder.xmin + scene->r.border.xmax * BLI_rctf_size_x(&viewborder);
rect->ymax = viewborder.ymin + scene->r.border.ymax * BLI_rctf_size_y(&viewborder);
}
else {
rect->xmin = v3d->render_border.xmin * region->winx;
rect->xmax = v3d->render_border.xmax * region->winx;
rect->ymin = v3d->render_border.ymin * region->winy;
rect->ymax = v3d->render_border.ymax * region->winy;
}
BLI_rcti_translate(rect, region->winrct.xmin, region->winrct.ymin);
BLI_rcti_isect(®ion->winrct, rect, rect);
return true;
}
/** \} */
|