-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathopennurbs_subd_texture.cpp
1903 lines (1681 loc) · 65 KB
/
opennurbs_subd_texture.cpp
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
//
// Copyright (c) 1993-2022 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
#include "opennurbs.h"
#if !defined(ON_COMPILING_OPENNURBS)
// This check is included in all opennurbs source .c and .cpp files to insure
// ON_COMPILING_OPENNURBS is defined when opennurbs source is compiled.
// When opennurbs source is being compiled, ON_COMPILING_OPENNURBS is defined
// and the opennurbs .h files alter what is declared and how it is declared.
#error ON_COMPILING_OPENNURBS must be defined when compiling opennurbs
#endif
#include "opennurbs_subd_data.h"
unsigned char ON_SubD::ObsoleteTextureDomainTypeFromTextureCoordinateType(ON_SubDTextureCoordinateType texture_coordinate_type)
{
/*
OBSOLETE enum class ON_SubDTextureDomainType : unsigned char
{
///<summary>
/// Texture domains are not set.
///</summary>
Unset = 0,
///<summary>
/// Each face texture domain is a unique rectangle of normalized texture space.
///</summary>
PerFace = 1,
///<summary>
/// Each face texture domain is a unique rectangle of normalized texture space.
/// When possible, faces are partitioned into quad groups. Adjacent members
/// of the group are assigned adjacent rectangles in texture space.
///</summary>
Packed = 2,
///<summary>
/// All face texture domain values are zero.
///</summary>
Zero = 3,
///<summary>
/// All face texture domain values are nan.
///</summary>
Nan = 4,
///<summary>
/// All face texture domain is (0,1)x(0,1).
///</summary>
UnitSquare = 5,
///<summary>
/// Code outside of opennurbs set the values. No other information is available.
///</summary>
Custom = 7,
};
*/
unsigned char obsolete_texture_domain_type = 0;
switch (texture_coordinate_type)
{
case ON_SubDTextureCoordinateType::Unset:
obsolete_texture_domain_type = 0; // OBSOLETE ON_SubDTextureDomainType::Unset = 0
break;
case ON_SubDTextureCoordinateType::Unpacked:
obsolete_texture_domain_type = 1; // OBSOLETE ON_SubDTextureDomainType::PerFace = 1
break;
case ON_SubDTextureCoordinateType::Packed:
obsolete_texture_domain_type = 2; // OBSOLETE ON_SubDTextureDomainType::Packed = 2
break;
case ON_SubDTextureCoordinateType::Zero:
obsolete_texture_domain_type = 3; // OBSOLETE ON_SubDTextureDomainType::Zero = 3
break;
case ON_SubDTextureCoordinateType::Nan:
obsolete_texture_domain_type = 4; // OBSOLETE ON_SubDTextureDomainType::Nan = 4
break;
case ON_SubDTextureCoordinateType::FromFaceTexturePoints:
obsolete_texture_domain_type = 6; // no corresponding OBSOLETE ON_SubDTextureDomainType
break;
case ON_SubDTextureCoordinateType::FromMapping:
obsolete_texture_domain_type = 7; // OBSOLETE ON_SubDTextureDomainType::Custom = 7
break;
default:
obsolete_texture_domain_type = 0;
break;
}
return obsolete_texture_domain_type;
}
ON_SubDTextureCoordinateType ON_SubD::TextureCoordinateTypeFromObsoleteTextureDomainType(
unsigned int obsolete_texture_domain_type_as_unsigned
)
{
/*
OBSOLETE enum class ON_SubDTextureDomainType : unsigned char
{
///<summary>
/// Texture domains are not set.
///</summary>
Unset = 0,
///<summary>
/// Each face texture domain is a unique rectangle of normalized texture space.
///</summary>
PerFace = 1,
///<summary>
/// Each face texture domain is a unique rectangle of normalized texture space.
/// When possible, faces are partitioned into quad groups. Adjacent members
/// of the group are assigned adjacent rectangles in texture space.
///</summary>
Packed = 2,
///<summary>
/// All face texture domain values are zero.
///</summary>
Zero = 3,
///<summary>
/// All face texture domain values are nan.
///</summary>
Nan = 4,
///<summary>
/// All face texture domain is (0,1)x(0,1).
///</summary>
UnitSquare = 5,
///<summary>
/// Code outside of opennurbs set the values. No other information is available.
///</summary>
Custom = 7,
};
*/
switch (obsolete_texture_domain_type_as_unsigned)
{
case 0: // OBSOLETE ON_SubDTextureDomainType::Unset = 0
return ON_SubDTextureCoordinateType::Unset;
break;
case 1: // OBSOLETE ON_SubDTextureDomainType::PerFace = 1
return ON_SubDTextureCoordinateType::Unpacked;
break;
case 2: // OBSOLETE ON_SubDTextureDomainType::Packed = 2
return ON_SubDTextureCoordinateType::Packed;
break;
case 3: // OBSOLETE ON_SubDTextureDomainType:: Zero = 3
return ON_SubDTextureCoordinateType::Zero;
break;
case 4: // OBSOLETE ON_SubDTextureDomainType::Nan = 4
return ON_SubDTextureCoordinateType::Nan;
break;
case 5: // OBSOLETE ON_SubDTextureDomainType::UnitSquare = 5
return ON_SubDTextureCoordinateType::Unset;
break;
case 6: // no corresponding OBSOLETE ON_SubDTextureDomainType
return ON_SubDTextureCoordinateType::FromFaceTexturePoints;
break;
case 7: // OBSOLETE ON_SubDTextureDomainType::Custom = 7
return ON_SubDTextureCoordinateType::FromMapping;
break;
}
return ON_SubDTextureCoordinateType::Unset;
}
ON_SubDTextureCoordinateType ON_SubD::TextureCoordinateTypeFromUnsigned
(
unsigned int texture_coordinate_type_as_unsigned
)
{
switch (texture_coordinate_type_as_unsigned)
{
ON_ENUM_FROM_UNSIGNED_CASE(ON_SubDTextureCoordinateType::Unset);
ON_ENUM_FROM_UNSIGNED_CASE(ON_SubDTextureCoordinateType::Unpacked);
ON_ENUM_FROM_UNSIGNED_CASE(ON_SubDTextureCoordinateType::Packed);
ON_ENUM_FROM_UNSIGNED_CASE(ON_SubDTextureCoordinateType::Zero);
ON_ENUM_FROM_UNSIGNED_CASE(ON_SubDTextureCoordinateType::Nan);
ON_ENUM_FROM_UNSIGNED_CASE(ON_SubDTextureCoordinateType::FromFaceTexturePoints);
ON_ENUM_FROM_UNSIGNED_CASE(ON_SubDTextureCoordinateType::FromMapping);
}
return ON_SUBD_RETURN_ERROR(ON_SubDTextureCoordinateType::Unset);
}
const ON_wString ON_SubD::TextureCoordinateTypeToString(
ON_SubDTextureCoordinateType texture_coordinate_type
)
{
const wchar_t* s;
switch (texture_coordinate_type)
{
case ON_SubDTextureCoordinateType::Unset:
s = L"Unset";
break;
case ON_SubDTextureCoordinateType::Unpacked:
s = L"Unpacked";
break;
case ON_SubDTextureCoordinateType::Packed:
s = L"Packed";
break;
case ON_SubDTextureCoordinateType::Zero:
s = L"Zero";
break;
case ON_SubDTextureCoordinateType::Nan:
s = L"Nan";
break;
case ON_SubDTextureCoordinateType::FromFaceTexturePoints:
s = L"FromFaceTexturePoints";
break;
case ON_SubDTextureCoordinateType::FromMapping:
s = L"FromMapping";
break;
default:
s = nullptr;
break;
}
return (nullptr != s && 0 != s[0])
? ON_wString(s)
: ON_wString::FormatToString(L"ON_SubDTextureCoordinateType(%u)", ((unsigned)static_cast<unsigned char>(texture_coordinate_type)));
}
//////////////////////////////////////////////////////////////////////////////
//
// ON_SubDimple - texture coordinates
//
#if 1
#pragma region ON_SubD - texture coordinates
void ON_SubDimple::SetTextureMappingTag(const ON_MappingTag &mapping_tag) const
{
if (0 != ON_MappingTag::CompareAll(this->m_texture_mapping_tag, mapping_tag))
{
this->m_texture_mapping_tag = mapping_tag;
ChangeRenderContentSerialNumber();
}
}
const ON_MappingTag ON_SubDimple::TextureMappingTag(bool bIgnoreTextureCoordinateType) const
{
return
(bIgnoreTextureCoordinateType || ON_SubDTextureCoordinateType::FromMapping == TextureCoordinateType())
? m_texture_mapping_tag
: ON_MappingTag::Unset;
}
#pragma endregion
#endif
const ON_MappingTag ON_SubDimple::ColorsMappingTag() const
{
return m_colors_mapping_tag;
}
void ON_SubDimple::SetColorsMappingTag(const ON_MappingTag& mapping_tag) const
{
if (0 != ON_MappingTag::CompareAll(this->m_colors_mapping_tag, mapping_tag))
{
this->m_colors_mapping_tag = mapping_tag;
ChangeRenderContentSerialNumber();
}
}
//////////////////////////////////////////////////////////////////////////////
//
// ON_SubD - texture coordinates
//
#if 1
#pragma region ON_SubD - texture coordinates
ON_SubDTextureCoordinateType ON_SubD::TextureCoordinateType() const
{
const ON_SubDimple* subdimple = SubDimple();
return (nullptr != subdimple) ? subdimple->TextureCoordinateType() : ON_SubDTextureCoordinateType::Unset;
}
ON_SubDTextureCoordinateType ON_SubDimple::TextureCoordinateType() const
{
return m_texture_coordinate_type;
}
unsigned int ON_SubD::TexturePointsAreSet() const
{
unsigned int count = 0;
ON_SubDFaceIterator fit(*this);
for (const ON_SubDFace* f = fit.FirstFace(); nullptr != f; f = fit.NextFace())
{
if (f->TexturePointsAreSet())
++count;
}
return count;
}
unsigned int ON_SubD::ClearTexturePoints() const
{
const bool bUpdateFragments = ON_SubDTextureCoordinateType::FromFaceTexturePoints == this->TextureCoordinateType();
const ON_SubDimple* subdimple = SubDimple();
const unsigned count = (nullptr != subdimple) ? subdimple->ClearTexturePoints() : 0;
if (bUpdateFragments)
this->Internal_SetFragmentTextureCoordinatesWithoutMapping();
return count;
}
unsigned int ON_SubDimple::ClearTexturePoints() const
{
bool bSetPacked = ON_SubDTextureCoordinateType::FromFaceTexturePoints == this->TextureCoordinateType();
if ( bSetPacked)
this->SetTextureCoordinateType(ON_SubDTextureCoordinateType::Unset);
const ON_SubDLevel& level = this->ActiveLevel();
unsigned int count = 0;
for (const ON_SubDFace* f = level.m_face[0]; nullptr != f; f = f->m_next_face)
{
if (f->TexturePointsAreSet())
++count;
this->ReturnFaceTexturePoints(f);
if (bSetPacked && false == f->PackRectIsSet())
bSetPacked = false;
}
if (bSetPacked)
this->SetTextureCoordinateType(ON_SubDTextureCoordinateType::Packed);
return count;
}
void ON_SubDimple::SetTextureCoordinateType(
ON_SubDTextureCoordinateType texture_coordinate_type
) const
{
if (m_texture_coordinate_type != texture_coordinate_type)
{
ChangeRenderContentSerialNumber();
m_texture_coordinate_type = texture_coordinate_type;
}
}
const ON_2udex ON_SubD::TextureDomainGridSize(
unsigned minimum_rectangle_count,
double image_width,
double image_height
)
{
if (0 == minimum_rectangle_count)
return ON_2udex(1, 1);
unsigned i = (unsigned)floor(sqrt((double)(minimum_rectangle_count)));
while (i < minimum_rectangle_count && i*i < minimum_rectangle_count)
++i;
unsigned int j = i;
if (j > 1 && (j - 1)*i >= minimum_rectangle_count)
--j;
return (image_height > image_width) ? ON_2udex(j, i) : ON_2udex(i, j);
}
double ON_SubDFace::PackRectGapInPixels(
double pack_rect_distance_in_pixels
)
{
// In order to produce repeatable rendering results,
// it is critical that TextureImageSuggestedMinimumSize be a
// constant and context independent value.
// As is clearly stated in the definition of ON_SubD::TextureImageSuggestedMinimumSize,
// this code assumes ON_SubD::TextureImageSuggestedMinimumSize is a power of 2 and
// ON_SubD::TextureImageSuggestedMinimumSize >= 512.
// As a result the following code assumes min_distance_in_pixels >= 8.0 and an integer value;
const double min_distance_in_pixels = ((double)(ON_SubD::TextureImageSuggestedMinimumSize))/64.0;
if (pack_rect_distance_in_pixels >= min_distance_in_pixels)
{
if (pack_rect_distance_in_pixels <= 2.0 * min_distance_in_pixels)
return 1.0;
if (pack_rect_distance_in_pixels <= 4.0 * min_distance_in_pixels)
return 2.0;
return 3.0;
}
return 0.0;
}
const ON_2udex ON_SubDFace::GetNgonSubPackRectSizeAndDelta(
unsigned int ngon_edge_count,
ON_2dVector ngon_face_pack_rect_size,
ON_2dVector& ngon_sub_pack_rect_size,
ON_2dVector& ngon_sub_pack_rect_delta
)
{
ON_2udex grid_size(0, 0);
ngon_sub_pack_rect_size = ON_2dVector::ZeroVector;
ngon_sub_pack_rect_delta = ON_2dVector::ZeroVector;
// This function determines how to partition ngon_face_pack_rect_size into
// ngon_edge_count sub quads that the ngon will use when it is rendered.
// Validate input parameters
if (ngon_edge_count < 5 || ngon_edge_count > ON_SubDFace::MaximumEdgeCount)
{
ON_SUBD_ERROR("Invalid ngon_edge_count parameter. Value must be >= 5 and <= ON_SubDFace::MaximumEdgeCount.");
return grid_size;
}
if (false == (ngon_face_pack_rect_size.x > 0.0 && ngon_face_pack_rect_size.x < ON_UNSET_POSITIVE_VALUE))
{
ON_SUBD_ERROR("Invalid ngon_face_pack_rect_size.x parameter. Value must be > 0.");
return grid_size;
}
if (false == (ngon_face_pack_rect_size.y > 0.0 && ngon_face_pack_rect_size.y < ON_UNSET_POSITIVE_VALUE))
{
ON_SUBD_ERROR("Invalid ngon_face_pack_rect_size.y parameter. Value must be > 0.");
return grid_size;
}
// The ngon's face pack rect will be partitioned into grid_size.i X grid_size.j sub pack rects.
const int pack_rect_aspectdex
= (ngon_face_pack_rect_size.x > ngon_face_pack_rect_size.y)
? 1
: ((ngon_face_pack_rect_size.x < ngon_face_pack_rect_size.y) ? -1 : 0)
;
grid_size = ON_SubD::TextureDomainGridSize(ngon_edge_count, 0.0, 0.0);
if (grid_size.i * grid_size.j < ngon_edge_count)
{
ON_SUBD_ERROR("Failed to get a valid grid_size.");
return ON_2udex(0, 0);
}
else
{
const int grid_aspectdex
= (grid_size.i > grid_size.j)
? 1
: ((grid_size.i < grid_size.j) ? -1 : 0)
;
if (grid_aspectdex * pack_rect_aspectdex < 0)
grid_size = ON_2udex(grid_size.j, grid_size.i);
}
// Estimate image pixels assigned to this ngon's pack rect.
// In order to produce repeatable rendering results,
// it is critical that TextureImageSuggestedMinimumSize be a
// constant and context independent value.
//
// The portion of the image assigned to the ngon face pack rect
// is rect_image_width X rect_image_height pixels.
// All variables ending in "size" have pixels as units.
const double subd_image_size = ON_SubD::TextureImageSuggestedMinimumSize;
const double rect_image_size[2] = {
subd_image_size * ngon_face_pack_rect_size.x,
subd_image_size * ngon_face_pack_rect_size.y
};
double subrect_image_size[2] = {
rect_image_size[0] / ((double)(grid_size.i)),
rect_image_size[1] / ((double)(grid_size.j))
};
double subrect_gap_size[2] = {
ON_SubDFace::PackRectGapInPixels(subrect_image_size[0]),
ON_SubDFace::PackRectGapInPixels(subrect_image_size[1])
};
// As is clearly stated in the definition of ON_SubD::TextureImageSuggestedMinimumSize,
// this code assumes ON_SubD::TextureImageSuggestedMinimumSize is a power of 2 and
// ON_SubD::TextureImageSuggestedMinimumSize >= 512.
// As a result the following code assumes min_distance_in_pixels >= 8.0 and an integer value;
//const double min_distance_in_pixels = ((double)(ON_SubD::TextureImageSuggestedMinimumSize)) / 64.0;
bool bApplyGap[2] = { false,false };
for (int i = 0; i < 2; ++i)
{
bApplyGap[i] = (subrect_gap_size[i] >= 1.0 && 8.0 * subrect_gap_size[i] <= subrect_image_size[i]);
if (bApplyGap[i])
subrect_image_size[i] = floor(subrect_image_size[i]) - subrect_gap_size[i];
}
// Dividing by subd_image_size converts pixel values to normalized dimensionless pack subrect values.
ngon_sub_pack_rect_size = ON_2dVector(
bApplyGap[0] ? (subrect_image_size[0] / subd_image_size) : (1.0 - ON_EPSILON) * (ngon_face_pack_rect_size.x / ((double)(grid_size.i))),
bApplyGap[1] ? (subrect_image_size[1] / subd_image_size) : (1.0 - ON_EPSILON) * (ngon_face_pack_rect_size.y / ((double)(grid_size.j)))
);
ngon_sub_pack_rect_delta = ON_2dVector(
ngon_sub_pack_rect_size.x + (bApplyGap[0] ? (subrect_gap_size[0] / subd_image_size) : 0.0),
ngon_sub_pack_rect_size.y + (bApplyGap[1] ? (subrect_gap_size[1] / subd_image_size) : 0.0)
);
return grid_size;
}
void ON_SubD::SetTextureCoordinateType(
ON_SubDTextureCoordinateType texture_coordinate_type
) const
{
const ON_SubDimple* subdimple = SubDimple();
if (nullptr != subdimple)
subdimple->SetTextureCoordinateType(texture_coordinate_type);
}
bool ON_SubD::TextureMappingRequired() const
{
if (ON_SubDTextureCoordinateType::FromMapping == TextureCoordinateType())
{
const ON_MappingTag texture_mapping_tag = TextureMappingTag(false);
if (
ON_TextureMapping::TYPE::no_mapping != texture_mapping_tag.m_mapping_type
&& ON_TextureMapping::TYPE::srfp_mapping != texture_mapping_tag.m_mapping_type
&& texture_mapping_tag.IsSet()
)
return true; // need an explicit ON_TextureMapping class to evaluate texture coordinates.
}
return false;
}
ON_SubDTextureCoordinateType ON_SubD::Internal_BestChoiceTextureCoordinateType(
const class ON_TextureMapping& available_mapping
) const
{
ON_SubDTextureCoordinateType subd_texture_coordinate_type = TextureCoordinateType();
if (ON_SubDTextureCoordinateType::FromMapping == subd_texture_coordinate_type)
{
const ON_MappingTag texture_mapping_tag = TextureMappingTag(false);
switch (texture_mapping_tag.m_mapping_type)
{
case ON_TextureMapping::TYPE::no_mapping:
subd_texture_coordinate_type = ON_SubDTextureCoordinateType::Unset;
break;
case ON_TextureMapping::TYPE::srfp_mapping:
subd_texture_coordinate_type = ON_SubDTextureCoordinateType::Packed; // <- breakpoint here when debugging texture issues
break;
default:
{
const bool bMappingAvailable
= ON_TextureMapping::TYPE::srfp_mapping != texture_mapping_tag.m_mapping_type
&& texture_mapping_tag.IsSet()
&& texture_mapping_tag.m_mapping_type == available_mapping.m_type
&& texture_mapping_tag.m_mapping_crc == available_mapping.MappingCRC()
;
if (false == bMappingAvailable)
subd_texture_coordinate_type = ON_SubDTextureCoordinateType::Unset; // <- breakpoint here when debugging texture issues
}
break;
}
}
if (ON_SubDTextureCoordinateType::Unset == subd_texture_coordinate_type)
subd_texture_coordinate_type = ON_SubD::DefaultTextureCoordinateType;
return subd_texture_coordinate_type;
}
bool ON_SubD::Internal_SetFragmentTextureCoordinatesWithoutMapping() const
{
// face_corners[] initialized to unpacked.
ON_SubDTextureCoordinateType subd_texture_coordinate_type = Internal_BestChoiceTextureCoordinateType(ON_TextureMapping::Unset);
bool bPacked = false;
bool bUnpacked = false;
bool bConstant = false;
bool bFromFaceTexturePoints = false;
ON_3dPoint face_corners[4];
switch (subd_texture_coordinate_type)
{
default:
// no break here
case ON_SubDTextureCoordinateType::FromMapping:
ON_SUBD_ERROR("Bug - these cases should have been eliminated in code above.");
subd_texture_coordinate_type = ON_SubDTextureCoordinateType::Packed;
// no break here
case ON_SubDTextureCoordinateType::Packed:
bPacked = true;
// If packing information is present, these values get updated for each face.
face_corners[0] = ON_3dPoint::NanPoint;
face_corners[1] = ON_3dPoint::NanPoint;
face_corners[2] = ON_3dPoint::NanPoint;
face_corners[3] = ON_3dPoint::NanPoint;
break;
case ON_SubDTextureCoordinateType::Unpacked:
bUnpacked = true;
// fragment grid order for unit texture corners
face_corners[0] = ON_3dPoint(0, 0, 0);
face_corners[1] = ON_3dPoint(1, 0, 0);
face_corners[2] = ON_3dPoint(0, 1, 0);
face_corners[3] = ON_3dPoint(1, 1, 0);
break;
case ON_SubDTextureCoordinateType::Zero:
bConstant = true;
face_corners[0] = ON_3dPoint::Origin;
face_corners[1] = ON_3dPoint::Origin;
face_corners[2] = ON_3dPoint::Origin;
face_corners[3] = ON_3dPoint::Origin;
break;
case ON_SubDTextureCoordinateType::Nan:
bConstant = true;
face_corners[0] = ON_3dPoint::NanPoint;
face_corners[1] = ON_3dPoint::NanPoint;
face_corners[2] = ON_3dPoint::NanPoint;
face_corners[3] = ON_3dPoint::NanPoint;
break;
case ON_SubDTextureCoordinateType::FromFaceTexturePoints:
bFromFaceTexturePoints = true;
face_corners[0] = ON_3dPoint::NanPoint;
face_corners[1] = ON_3dPoint::NanPoint;
face_corners[2] = ON_3dPoint::NanPoint;
face_corners[3] = ON_3dPoint::NanPoint;
break;
}
ON_SHA1_Hash hash = ON_SHA1_Hash::EmptyContentHash;
if (bPacked || bUnpacked || bConstant || bFromFaceTexturePoints)
{
ON_SubDFaceIterator fit(*this);
// DELETE // // estimate face image size - used to add empty space around ngon-subdfragments
// DELETE // const unsigned int face_image_size = ON_SubD::TextureImageSuggestedMinimumSize(ON_SubD::TextureDomainGridSize(fit.FaceCount(), 0, 0));
for (const ON_SubDFace* f = fit.FirstFace(); nullptr != f; f = fit.NextFace())
{
if (f->m_edge_count < 3)
continue;
const ON_SubDMeshFragment* fragment = f->MeshFragments();
if (nullptr == fragment)
continue;
const unsigned short frag_count = (4 == f->m_edge_count) ? 1 : f->m_edge_count;
if (bUnpacked || bConstant)
{
for (unsigned short frag_dex = 0; frag_dex < frag_count && nullptr != fragment; ++frag_dex, fragment = fragment->m_next_fragment)
fragment->SetTextureCoordinateCornersForExperts(true, face_corners, true);
continue;
}
if (bFromFaceTexturePoints && f->TexturePointsAreSet())
{
// All the code in this if clause handles setting custom texture coordinates.
ON_3dPoint frag_texture_corners[4];
if (1 == frag_count)
{
// f is a quad face and there is one fragment
frag_texture_corners[0] = f->TexturePoint(0);
frag_texture_corners[1] = f->TexturePoint(1);
frag_texture_corners[2] = f->TexturePoint(2);
frag_texture_corners[3] = f->TexturePoint(3);
fragment->SetTextureCoordinateCornersForExperts(false, frag_texture_corners, true);
}
else if ( frag_count >= 3)
{
// f is a n-gon with n subd fragments
const int k = 2;
frag_texture_corners[(2 + k) % 4] = f->TextureCenterPoint();
ON_Line L(f->TexturePoint(f->m_edge_count - 1), f->TexturePoint(0));
frag_texture_corners[(1 + k) % 4] = L.PointAt(0.5);
for (unsigned short frag_dex = 0; frag_dex < frag_count && nullptr != fragment; ++frag_dex, fragment = fragment->m_next_fragment)
{
L.from = L.to;
L.to = f->TexturePoint((frag_dex + 1) % frag_count);
frag_texture_corners[(3 + k) % 4] = frag_texture_corners[(1 + k) % 4];
frag_texture_corners[(0 + k) % 4] = L.from;
frag_texture_corners[(1 + k) % 4] = L.PointAt(0.5);
fragment->SetTextureCoordinateCornersForExperts(false, frag_texture_corners, true);
}
}
continue;
}
// The remaining code in this scope handles setting packed texture coordinates on face fragments.
// We end up here when bPacked or a face is missing texture points.
if (f->PackRectIsSet())
{
// even when bPacked is false, use packed coordinates as a fallback
face_corners[0] = f->PackRectCorner(true, 0);
face_corners[1] = f->PackRectCorner(true, 1);
face_corners[2] = f->PackRectCorner(true, 2);
face_corners[3] = f->PackRectCorner(true, 3);
}
else
{
face_corners[0] = ON_2dPoint::NanPoint;
face_corners[1] = ON_2dPoint::NanPoint;
face_corners[2] = ON_2dPoint::NanPoint;
face_corners[3] = ON_2dPoint::NanPoint;
}
if (1 == frag_count || 3 == frag_count)
{
// A quad ON_SubDFace has a single fragment and that fragment gets the entire face_texture_domain
// A 3-gon ON_SubDFace has 3 fragments that get a portion of the entire face_texture_domain
for (unsigned short frag_dex = 0; frag_dex < frag_count && nullptr != fragment; ++frag_dex, fragment = fragment->m_next_fragment)
fragment->SetQuadOr3gonFaceFragmentTextureCoordinateCorners(true, face_corners, true);
}
else if (frag_count >= 5)
{
// A n-gon ON_SubDFace (n has n fragments that get a portion of the entire face_texture_domain
// General case n-gon with n >= 5.
// This face in an n-gon with n fragments that each get a portion of the
// texture domain assigned to the face.
const ON_2dVector face_pack_rect_size = f->PackRectIsSet() ? f->PackRectSize() : ON_2dVector(1.0, 1.0);
ON_2dVector ngon_sub_pack_rect_size = ON_2dVector::NanVector;
ON_2dVector ngon_sub_pack_rect_delta = ON_2dVector::NanVector;
const ON_2udex ngon_grid_size = ON_SubDFace::GetNgonSubPackRectSizeAndDelta(
frag_count,
face_pack_rect_size,
ngon_sub_pack_rect_size,
ngon_sub_pack_rect_delta
);
for (unsigned short frag_dex = 0; frag_dex < frag_count && nullptr != fragment; ++frag_dex, fragment = fragment->m_next_fragment)
fragment->SetNgonFaceFragmentTextureCoordinateCorners(true, face_corners, face_pack_rect_size, ngon_grid_size, ngon_sub_pack_rect_size, ngon_sub_pack_rect_delta, true);
}
}
hash = ON_SubD::TextureSettingsHash(subd_texture_coordinate_type, ON_MappingTag::Unset);
ChangeRenderContentSerialNumber();
}
Internal_SetFragmentTextureCoordinatesTextureSettingsHash(hash);
return true;
}
void ON_SubD::SetTextureMappingTag(const ON_MappingTag &mapping_tag) const
{
const ON_SubDimple* subdimple = SubDimple();
if (nullptr != subdimple)
subdimple->SetTextureMappingTag(mapping_tag);
}
void ON_SubD::ClearFragmentTextureCoordinatesTextureSettingsHash() const
{
Internal_SetFragmentTextureCoordinatesTextureSettingsHash(ON_SHA1_Hash::EmptyContentHash);
}
static bool SetGridMeshMappingTCs(const unsigned int pointCount, const double* P, const size_t P_stride, const double* N, const size_t N_stride, double* T, const size_t T_stride, const ON_TextureMapping& mapping, const ON_Xform* P_xform, const ON_Xform* N_xform)
{
return false;
}
bool ON_SubD::SetFragmentTextureCoordinates(
const class ON_TextureMapping& mapping,
bool bLazy
) const
{
const ON_SubDTextureCoordinateType subd_texture_coordinate_type = Internal_BestChoiceTextureCoordinateType(mapping);
const ON_MappingTag mapping_tag = this->TextureMappingTag(false);
const ON_SHA1_Hash hash = ON_SubD::TextureSettingsHash(subd_texture_coordinate_type, mapping_tag);
if (bLazy)
{
// fragment texture coordinates match what's be asked for.
const ON_SHA1_Hash current_hash = FragmentTextureCoordinatesTextureSettingsHash();
if (hash == current_hash)
return true;
}
if (ON_SubDTextureCoordinateType::FromMapping != subd_texture_coordinate_type || false == this->TextureMappingRequired())
return Internal_SetFragmentTextureCoordinatesWithoutMapping();
// we have a valid and nontrivial mapping.
const ON_Xform subd_xform(mapping_tag.Transform());
const bool bApplySubDXform = ON_MappingTag::TransformTreatedIsIdentity(&subd_xform) ? false : true;
ON_Xform P_xform, N_xform;
if (bApplySubDXform)
subd_xform.GetMappingXforms(P_xform, N_xform);
ON_3dPoint tc;
ON_SubDMeshFragmentIterator frit(*this);
for (const ON_SubDMeshFragment* fragment = frit.FirstFragment(); nullptr != fragment; fragment = frit.NextFragment())
{
const unsigned P_count = fragment->PointCount();
if (P_count < 4)
continue;
const double* P = fragment->m_P;
const size_t P_stride = fragment->m_P_stride;
unsigned T_count = fragment->TextureCoordinateCount();
if (P_count != T_count)
continue;
double* T = fragment->m_T;
if (nullptr == T)
continue;
size_t T_stride = fragment->m_T_stride;
if (0 == T_stride)
{
T_stride = 3;
T_count = 1;
}
const unsigned N_count = fragment->NormalCount();
const double* N = (N_count == P_count) ? fragment->m_N : &ON_3dVector::ZeroVector.x;
const size_t N_stride = (N_count == P_count) ? fragment->m_N_stride : 0;
if (T_count != P_count || N_count != P_count || !SetGridMeshMappingTCs(P_count, P, P_stride, N, N_stride, T, T_stride, mapping, bApplySubDXform ? &P_xform : nullptr, bApplySubDXform ? &N_xform : nullptr))
{
for (double* T1 = T + T_stride * T_count; T < T1; T += T_stride, P += P_stride, N += N_stride)
{
const bool ok = bApplySubDXform ?
mapping.Evaluate(ON_3dPoint(P), ON_3dVector(N), &tc, P_xform, N_xform) :
mapping.Evaluate(ON_3dPoint(P), ON_3dVector(N), &tc);
if (!ok)
tc = ON_3dPoint::NanPoint;
T[0] = tc.x;
T[1] = tc.y;
T[2] = tc.z;
}
}
}
Internal_SetFragmentTextureCoordinatesTextureSettingsHash(hash);
return true;
}
void ON_SubD::Internal_SetFragmentTextureCoordinatesTextureSettingsHash(ON_SHA1_Hash hash) const
{
const ON_SubDimple* subdimple = SubDimple();
if (nullptr != subdimple)
subdimple->Internal_SetFragmentTextureCoordinatesTextureSettingsHash(hash);
}
const ON_SHA1_Hash ON_SubD::FragmentTextureCoordinatesTextureSettingsHash() const
{
const ON_SubDimple* subdimple = SubDimple();
return (nullptr != subdimple) ? subdimple->FragmentTextureCoordinatesTextureSettingsHash() : ON_SHA1_Hash::EmptyContentHash;
}
const ON_MappingTag ON_SubD::TextureMappingTag(bool bIgnoreTextureCoordinateType) const
{
const ON_SubDimple* subdimple = SubDimple();
return (nullptr != subdimple) ? subdimple->TextureMappingTag(bIgnoreTextureCoordinateType) : ON_MappingTag();
}
const ON_SHA1_Hash ON_SubD::TextureSettingsHash(ON_SubDTextureCoordinateType texture_coordinate_type, const ON_MappingTag& texture_mapping_tag)
{
bool bHashMapping = false;
switch (texture_coordinate_type)
{
case ON_SubDTextureCoordinateType::Unset:
break;
case ON_SubDTextureCoordinateType::Unpacked:
break;
case ON_SubDTextureCoordinateType::Packed:
break;
case ON_SubDTextureCoordinateType::Zero:
break;
case ON_SubDTextureCoordinateType::Nan:
break;
case ON_SubDTextureCoordinateType::FromFaceTexturePoints:
break;
case ON_SubDTextureCoordinateType::FromMapping:
switch (texture_mapping_tag.m_mapping_type)
{
case ON_TextureMapping::TYPE::no_mapping:
texture_coordinate_type = ON_SubD::DefaultTextureCoordinateType;
break;
case ON_TextureMapping::TYPE::srfp_mapping:
texture_coordinate_type = ON_SubDTextureCoordinateType::Packed;
break;
default:
if (texture_mapping_tag.IsSet())
bHashMapping = true;
else
texture_coordinate_type = ON_SubD::DefaultTextureCoordinateType;
break;
}
break;
default:
ON_SUBD_ERROR("Invalid texture_coordinate_type parameter");
texture_coordinate_type = ON_SubDTextureCoordinateType::Unset;
break;
}
ON_SHA1 sha1;
sha1.AccumulateBytes(&texture_coordinate_type, sizeof(texture_coordinate_type)); // 1 byte
if (bHashMapping)
sha1.AccumulateSubHash(texture_mapping_tag.Hash());
return sha1.Hash();
}
const ON_SHA1_Hash ON_SubD::TextureSettingsHash() const
{
return ON_SubD::TextureSettingsHash(this->TextureCoordinateType(), this->TextureMappingTag(false));
}
#pragma endregion
#endif
//////////////////////////////////////////////////////////////////////////////
//
// ON_SubDFace - texture coordinates
//
void ON_SubDFace::SetMaterialChannelIndex(int material_channel_index) const
{
if ( material_channel_index > 0 && material_channel_index <= ON_Material::MaximumMaterialChannelIndex )
{
m_material_channel_index = (unsigned short)material_channel_index;
}
else
{
if (0 != material_channel_index)
{
ON_ERROR("Invalid material_channel_index value.");
}
ClearMaterialChannelIndex(); // makes it easier to detect when per face setting is cleared.
}
}
void ON_SubDFace::ClearMaterialChannelIndex() const
{
m_material_channel_index = 0;
}
int ON_SubDFace::MaterialChannelIndex() const
{
return (int)m_material_channel_index;
}
void ON_SubDFace::SetPerFaceColor(
ON_Color color
) const
{
if (ON_Color::UnsetColor == color)
ClearPerFaceColor(); // makes it easier to detect when the per face setting is cleared.
else
m_per_face_color = color;
}
void ON_SubDFace::ClearPerFaceColor() const
{
m_per_face_color = ON_Color::UnsetColor;
}
const ON_Color ON_SubDFace::PerFaceColor() const
{
return m_per_face_color;
}
unsigned int ON_SubD::ClearPerFaceMaterialChannelIndices()
{
unsigned change_count = 0;
ON_SubDFaceIterator fit(*this);
for (const ON_SubDFace* f = fit.FirstFace(); nullptr != f; f = fit.NextFace())
{
if (0 != f->MaterialChannelIndex())
{
f->ClearMaterialChannelIndex();
++change_count;
}
}
if (change_count>0)
ChangeRenderContentSerialNumber();
return change_count;
}
bool ON_SubD::HasPerFaceMaterialChannelIndices() const
{
ON_SubDFaceIterator fit(*this);
for (const ON_SubDFace* f = fit.FirstFace(); nullptr != f; f = fit.NextFace())
{
if (0 != f->MaterialChannelIndex())
return true;
}
return false;
}
unsigned int ON_SubD::ClearPerFaceColors() const
{
unsigned change_count = 0;
ON_SubDFaceIterator fit(*this);
for (const ON_SubDFace* f = fit.FirstFace(); nullptr != f; f = fit.NextFace())
{
if (ON_Color::UnsetColor != f->PerFaceColor())
{
f->ClearPerFaceColor();
++change_count;
}
}
if (change_count > 0)
ChangeRenderContentSerialNumber();
return change_count;
}
bool ON_SubD::HasPerFaceColors() const
{
ON_SubDFaceIterator fit(*this);
for (const ON_SubDFace* f = fit.FirstFace(); nullptr != f; f = fit.NextFace())