-
Notifications
You must be signed in to change notification settings - Fork 18
/
ThunderLoomMtl.cpp
1109 lines (1011 loc) · 37.3 KB
/
ThunderLoomMtl.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
// The paramblock version
// (when changes are made to the paramblock structure)
const int PARAM_VERSION_HIGH=2;
const int PARAM_VERSION_LOW=90;
const int PARAM_VERSION= PARAM_VERSION_HIGH*100 + PARAM_VERSION_LOW;
// NOTE: When adding new parameters, check:
// http://docs.autodesk.com/3DSMAX/16/ENU/3ds-Max-SDK-Programmer-Guide/index.html?url=files/GUID-F35959BB-2660-492F-B082-56304C70293A.htm,topicNumber=d30e52807
#include "thunderloom.h"
#define TL_PATTERN_EDITOR_IMPLEMENTATION
#include "pattern_editor.h"
// TODO: Dynamic Load
//extern "C" {
//#include "dynamic_load.h"
//}
//The plugin version (Semantic Versioning)
const int PLUGIN_VERSION_MAJOR=TL_VERSION_MAJOR;
const int PLUGIN_VERSION_MINOR=TL_VERSION_MINOR;
const int PLUGIN_VERSION_PATCH=TL_VERSION_PATCH;
// #include "Eval.h" // TODO
#include "max.h"
#include "ThunderLoomMtl.h"
//#include "resource.h"
#define _USE_MATH_DEFINES
#include <math.h>
// #include "helper.h" // TODO
#include "pattern_editor.h"
// no param block script access for VRay free
// TODO(Peter): VRay free? Demo?
#ifdef _FREE_
#define _FT(X) _T("")
#define IS_PUBLIC 0
#else
#define _FT(X) _T(X)
#define IS_PUBLIC 1
#endif // _FREE_
/*===========================================================================*\
| Class Descriptor
\*===========================================================================*/
//The class descriptor registers plugin with 3dsMax.
class ThunderLoomClassDesc : public ClassDesc2
#if GET_MAX_RELEASE(VERSION_3DSMAX) >= 6000
//This interface is used to determine whether
//a material/map flags itself as being compatible
//with a specific renderer plugin.
//The interface also provides a way of defining
//an icon that appears in the material browser
//- GetCustomMtlBrowserIcon.
, public IMtlRender_Compatibility_MtlBase
#endif
#if GET_MAX_RELEASE(VERSION_3DSMAX) >= 13900
//This interface allows materials and textures to customize their
//appearance in the Material Browser
//If implemented, the plug-in class should
//return its instance of this interface in response to
//ClassDesc::GetInterface(IMATERIAL_BROWSER_ENTRY_INFO_INTERFACE).
//The interface allows a plug-in to customize the default
//appearance of its entries, as shown in the Material Browser.
//This includes the display name, the thumbnail, and the location
//(or category) of entries.
, public IMaterialBrowserEntryInfo
#endif
{
HIMAGELIST imageList;
public:
virtual int IsPublic() { return IS_PUBLIC; }
virtual void* Create(BOOL loading = FALSE) { return new ThunderLoomMtl(loading); }
virtual const TCHAR * ClassName() { return STR_CLASSNAME; }
virtual SClass_ID SuperClassID() { return MATERIAL_CLASS_ID; }
virtual Class_ID ClassID() { return MTL_CLASSID; }
virtual const TCHAR* Category() { return NULL; }
virtual const TCHAR* InternalName() { return _T("thunderLoomMtl"); } // returns fixed parsable name (scripter-visible name)
virtual HINSTANCE HInstance() { return hInstance; } // returns owning module handle
ThunderLoomClassDesc(void) {
imageList=NULL;
#if GET_MAX_RELEASE(VERSION_3DSMAX) >= 6000
IMtlRender_Compatibility_MtlBase::Init(*this);
#endif
}
~ThunderLoomClassDesc(void) {
if (imageList) ImageList_Destroy(imageList);
imageList=NULL;
}
#if GET_MAX_RELEASE(VERSION_3DSMAX) >= 6000 //NOTE(Peter): have a minimum version requirement? minimum VERSION_3DSMAX = 14900?
// From IMtlRender_Compatibility_MtlBase
bool IsCompatibleWithRenderer(ClassDesc& rendererClassDesc) {
if (rendererClassDesc.ClassID()!=VRENDER_CLASS_ID) return false;
return true;
}
bool GetCustomMtlBrowserIcon(HIMAGELIST& hImageList, int& inactiveIndex, int& activeIndex, int& disabledIndex) {
if (!imageList) {
HBITMAP bmp=LoadBitmap(hInstance, MAKEINTRESOURCE(bm_MTL));
HBITMAP mask=LoadBitmap(hInstance, MAKEINTRESOURCE(bm_MTLMASK));
imageList=ImageList_Create(11, 11, ILC_COLOR24 | ILC_MASK, 5, 0);
int index=ImageList_Add(imageList, bmp, mask);
if (index==-1) return false;
}
if (!imageList) return false;
hImageList=imageList;
inactiveIndex=0;
activeIndex=1;
disabledIndex=2;
return true;
} //TODO(Peter): Is this needed? Do we want custom icon?
#endif
#if GET_MAX_RELEASE(VERSION_3DSMAX) >= 13900
//Get material to show up in vray section of the material browser.
FPInterface* GetInterface(Interface_ID id) {
if (IMATERIAL_BROWSER_ENTRY_INFO_INTERFACE==id) {
return static_cast<IMaterialBrowserEntryInfo*>(this);
}
return ClassDesc2::GetInterface(id);
}
// From IMaterialBrowserEntryInfo
const MCHAR* GetEntryName() const { return NULL; }
const MCHAR* GetEntryCategory() const {
#if GET_MAX_RELEASE(VERSION_3DSMAX) >= 14900
HINSTANCE hInst=GetModuleHandle(_T("sme.gup"));
if (hInst) {
//Extract a resource from the calling module's string table.
static MSTR category(MaxSDK::GetResourceStringAsMSTR(hInst,
IDS_3DSMAX_SME_MATERIALS_CATLABEL).Append(_T("\\V-Ray")));
return category.data();
}
#endif
return _T("Materials\\V-Ray");
}
Bitmap* GetEntryThumbnail() const { return NULL; }
#endif
};
static ThunderLoomClassDesc thunderLoomDesc;
ClassDesc* GetSkeletonMtlDesc() {return &thunderLoomDesc;}
/*===========================================================================*\
| Basic implimentation of a dialog handler
\*===========================================================================*/
/*===========================================================================*\
| UI stuff
\*===========================================================================*/
struct YarnTypeDlgProcData
{
ThunderLoomMtl *sm;
int yarn_type;
};
INT_PTR YarnTypeDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
class PatternRolloutDlgProc: public ParamMap2UserDlgProc{
public:
IParamMap *pmap;
ThunderLoomMtl *sm;
wchar_t directory[512];
HWND *rollups;
int num_rollups;
void update_yarn_type_rollups()
{
if(sm && sm->m_i_mtl_params){
for(int i=0; i<num_rollups; i++){
sm->m_i_mtl_params->DeleteRollupPage(rollups[i]);
}
}
free(rollups);
if(sm && sm->m_weave_parameters->pattern && sm->m_i_mtl_params){
num_rollups=sm->m_weave_parameters->num_yarn_types;
rollups=(HWND*)calloc(num_rollups,sizeof(HWND));
if(sm && sm->m_weave_parameters->pattern){
//NOTE(Vidar): Create yarn type rollups
for(int i=0; i<num_rollups; i++){
YarnTypeDlgProcData *data=(YarnTypeDlgProcData*)
calloc(1,sizeof(YarnTypeDlgProcData));
data->sm=sm;
data->yarn_type=i;
bool open=sm->m_yarn_type_rollup_open[i];
if(i==0){
//NOTE(Vidar): Common yarn rollout
rollups[0]=sm->m_i_mtl_params->AddRollupPage(
hInstance,MAKEINTRESOURCE(IDD_YARN),YarnTypeDlgProc,
L"Common Yarn Settings",
(LPARAM)data,open ? 0 : APPENDROLL_CLOSED);
} else{
wchar_t buffer[128];
wsprintf(buffer,L"Yarn type %d",i);
rollups[i]=sm->m_i_mtl_params->AddRollupPage(hInstance,
MAKEINTRESOURCE(IDD_YARN_TYPE),YarnTypeDlgProc,buffer,
(LPARAM)data,open ? 0 : APPENDROLL_CLOSED);
}
}
}
} else{
num_rollups=0;
rollups=0;
}
}
PatternRolloutDlgProc(void)
{
rollups=0;
num_rollups=0;
sm=NULL;
directory[0]=0;
}
INT_PTR DlgProc(TimeValue t,IParamMap2 *map,HWND hWnd,UINT msg,
WPARAM wParam,LPARAM lParam)
{
int id=LOWORD(wParam);
switch(msg){
case WM_INITDIALOG:
{
update_yarn_type_rollups();
wchar_t buffer[512];
//swprintf(buffer,L"Thunder Loom v%d.%d.%d (paramblock v%d.%02d)",
// PLUGIN_VERSION_MAJOR,PLUGIN_VERSION_MINOR,PLUGIN_VERSION_PATCH,
// PARAM_VERSION_HIGH,PARAM_VERSION_LOW);
swprintf(buffer,512,L"Thunder Loom v%d.%d.%d",
PLUGIN_VERSION_MAJOR,PLUGIN_VERSION_MINOR,PLUGIN_VERSION_PATCH);
SetWindowText(GetDlgItem(hWnd,IDC_VERSION),buffer);
break;
}
case WM_DESTROY:
for(int i=0;i<num_rollups;i++){
bool open=sm->m_i_mtl_params->IsRollupPanelOpen(rollups[i]);
sm->m_yarn_type_rollup_open[i]=open;
}
if(rollups){
free(rollups);
}
rollups=0;
num_rollups=0;
break;
case WM_COMMAND:
{
switch(LOWORD(wParam)){
case IDC_WIFFILE_BUTTON:
{
switch(HIWORD(wParam)){
case BN_CLICKED:
{
char *filename = (char*)calloc(512,sizeof(char));
OPENFILENAMEA openfilename={0};
openfilename.lStructSize=sizeof(OPENFILENAMEA);
openfilename.hwndOwner=NULL;
static const char *filters=
"Pattern Files | *.WIF;*.PTN\0*.WIF;*.PTN\0"
"All Files | *.*\0*.*\0"
;
openfilename.lpstrFilter=filters;
openfilename.lpstrFile=filename;
openfilename.lpstrDefExt="ptn";
openfilename.nMaxFile=512;
openfilename.lpstrTitle="Load pattern";
GetOpenFileNameA(&openfilename);
if(filename[0]!=0){
const char *error=0;
tlWeaveParameters* param = tl_weave_pattern_from_file(filename, &error);
// TODO: Dynamic Load
//#define DYNAMIC_FUNC_ARG_TYPES const char *, const char **
//#define DYNAMIC_FUNC_ARG_NAMES filename, &error
// CALL_DYNAMIC_FUNC(tl_weave_pattern_from_file, tlWeaveParameters *, param)
//#undef DYNAMIC_FUNC_ARG_TYPES
//#undef DYNAMIC_FUNC_ARG_NAMES
if(error){
MessageBoxA(NULL,error,"ERROR!",MB_OK|MB_ICONERROR);
} else{
//TODO(Vidar):Make this into a function, it is identical to the
// code when closing the pattern editor, below
//NOTE(Vidar):Set parameters...
sm->m_weave_parameters=param;
int realworld;
sm->pblock->GetValue(mtl_realworld,0,realworld,
sm->ivalid);
sm->m_weave_parameters->realworld_uv=realworld;
sm->pblock->GetValue(mtl_uscale,0,
sm->m_weave_parameters->uscale,
sm->ivalid);
sm->pblock->GetValue(mtl_vscale,0,
sm->m_weave_parameters->vscale,
sm->ivalid);
sm->pblock->GetValue(mtl_uvrotation,0,
sm->m_weave_parameters->uvrotation,
sm->ivalid);
int num_yarn_types=sm->m_weave_parameters->
num_yarn_types;
sm->m_yarn_type_rollup_open[0]=1;
for(int i=1;i<num_yarn_types;i++){
sm->m_yarn_type_rollup_open[i]=0;
}
//TODO(Peter): Test this with more complicate wif files!
//Set count for subtexmaps
sm->pblock->SetCount(texmaps,
num_yarn_types* NUMBER_OF_YRN_TEXMAPS);
map->Invalidate();
update_yarn_type_rollups();
//NOTE(Vidar): Hack to update the preview ball
sm->pblock->SetValue(mtl_dummy,0,0.5f);
}
}
free(filename);
break;
}
}
break;
}
case IDC_PATTERNEDITOR_BUTTON:
{
switch(HIWORD(wParam)){
case BN_CLICKED:
{
HWND parent_hwnd=GetAncestor(hWnd,GA_ROOT);
EnableWindow(parent_hwnd,false);
tlWeaveParameters* param = tl_pattern_editor(sm->m_weave_parameters);
// TODO: Dynamic Load
//#define DYNAMIC_FUNC_ARG_TYPES tlWeaveParameters *
//#define DYNAMIC_FUNC_ARG_NAMES sm->m_weave_parameters
// CALL_DYNAMIC_FUNC(tl_pattern_editor, tlWeaveParameters *, param)
//#undef DYNAMIC_FUNC_ARG_TYPES
//#undef DYNAMIC_FUNC_ARG_NAMES
sm->m_weave_parameters = param;
EnableWindow(parent_hwnd,true);
SetForegroundWindow(parent_hwnd);
//NOTE(Vidar):Set parameters...
int realworld;
sm->pblock->GetValue(mtl_realworld,0,realworld,
sm->ivalid);
sm->m_weave_parameters->realworld_uv=realworld;
sm->pblock->GetValue(mtl_uscale,0,
sm->m_weave_parameters->uscale,
sm->ivalid);
sm->pblock->GetValue(mtl_vscale,0,
sm->m_weave_parameters->vscale,
sm->ivalid);
sm->pblock->GetValue(mtl_uvrotation,0,
sm->m_weave_parameters->uvrotation,
sm->ivalid);
int num_yarn_types=sm->m_weave_parameters->
num_yarn_types;
sm->m_yarn_type_rollup_open[0]=1;
for(int i=1;i<num_yarn_types;i++){
sm->m_yarn_type_rollup_open[i]=0;
}
//TODO(Peter): Test this with more complicate wif files!
//Set count for subtexmaps
sm->pblock->SetCount(texmaps,
num_yarn_types* NUMBER_OF_YRN_TEXMAPS);
map->Invalidate();
update_yarn_type_rollups();
//NOTE(Vidar): Hack to update the preview ball
sm->pblock->SetValue(mtl_dummy,0,0.5f);
break;
}
}
break;
}
}
break;
}
}
return FALSE;
}
void DeleteThis()
{
if(rollups){
free(rollups);
}
rollups=0;
num_rollups=0;
sm=NULL;
}
void SetThing(ReferenceTarget *m)
{
sm=(ThunderLoomMtl*)m;
update_yarn_type_rollups();
}
};
/*===========================================================================*\
| Paramblock2 Descriptor
\*===========================================================================*/
enum {rollout_pattern, rollout_yarn_type};
static ParamBlockDesc2 thunder_loom_param_blk_desc(
mtl_params, _T("Test mtl params"), 0,
&thunderLoomDesc, P_AUTO_CONSTRUCT + P_AUTO_UI + P_MULTIMAP + P_VERSION,
PARAM_VERSION, 0,
//rollouts
1,
//2,
rollout_pattern, IDD_BLENDMTL, IDS_PATTERN, 0, 0,
new PatternRolloutDlgProc(),
mtl_uscale, _FT("uscale"), TYPE_FLOAT, P_ANIMATABLE, 0,
p_default, 1.f,
p_range, 0.f, 99999.f,
p_ui, rollout_pattern, TYPE_SPINNER, EDITTYPE_FLOAT,
IDC_USCALE_EDIT, IDC_USCALE_SPIN, 0.1f,
PB_END,
mtl_vscale, _FT("vscale"), TYPE_FLOAT, P_ANIMATABLE, 0,
p_default, 1.f,
p_range, 0.f, 99999.f,
p_ui, rollout_pattern, TYPE_SPINNER, EDITTYPE_FLOAT,
IDC_VSCALE_EDIT, IDC_VSCALE_SPIN, 0.1f,
PB_END,
mtl_uvrotation, _FT("uvrotation"), TYPE_FLOAT, P_ANIMATABLE, 0,
p_default, 0.f,
p_range, -360.f, 360.f,
p_ui, rollout_pattern, TYPE_SPINNER, EDITTYPE_FLOAT,
IDC_UVROTATION_EDIT, IDC_UVROTATION_SPIN, 0.1f,
PB_END,
// TODO(Peter): remove?
mtl_dummy, _FT("dummy"), TYPE_FLOAT, P_ANIMATABLE, 0,
p_default, 1.f,
PB_END,
mtl_realworld, _FT("realworld"), TYPE_BOOL, 0, 0,
p_default, FALSE,
p_ui, rollout_pattern, TYPE_SINGLECHEKBOX, IDC_REALWORLD_CHECK,
PB_END,
texmaps, _T("yarnmaplist"), TYPE_TEXMAP_TAB, 0, P_VARIABLE_SIZE, 0,
//no ui, for the array
//handled through Proc
PB_END,
PB_END
);
INT_PTR YarnTypeDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
int id = LOWORD(wParam);
switch (msg)
{
case WM_INITDIALOG:{
YarnTypeDlgProcData *data = (YarnTypeDlgProcData*)lParam;
SetWindowLongPtr(hWnd,GWLP_USERDATA,(LONG_PTR)data);
tlWeaveParameters *params = data->sm->m_weave_parameters;
tlYarnType yarn_type=params->yarn_types[data->yarn_type];
//NOTE(Peter): List params we want in the interface
#undef TL_YARN_PARAMETERS
#define TL_YARN_PARAMETERS\
TL_FLOAT_PARAM(umax)\
TL_FLOAT_PARAM(yarnsize)\
TL_FLOAT_PARAM(psi)\
TL_FLOAT_PARAM(delta_x)\
TL_COLOR_PARAM(specular_color)\
TL_FLOAT_PARAM(specular_noise)\
TL_COLOR_PARAM(color)\
TL_COLOR_PARAM(opacity)\
TL_FLOAT_PARAM(rho)\
//NOTE(Vidar): Setup spinners
#define TL_FLOAT_PARAM(name){\
HWND spinner_hwnd = GetDlgItem(hWnd, IDC_##name##_SPIN);\
HWND edit_hwnd = GetDlgItem(hWnd, IDC_##name##_EDIT);\
if(spinner_hwnd && edit_hwnd){\
ISpinnerControl * s = GetISpinner(spinner_hwnd);\
s->LinkToEdit(edit_hwnd, EDITTYPE_FLOAT);\
s->SetLimits(0.f, 1.f, FALSE);\
s->SetScale(0.01f);\
s->SetValue(yarn_type.name, FALSE);\
ReleaseISpinner(s);\
if(data->yarn_type > 0){\
uint8_t enabled=yarn_type.name##_enabled;\
Button_SetCheck(\
GetDlgItem(hWnd,IDC_##name##_OVERRIDE),enabled);\
EnableWindow(spinner_hwnd,enabled);\
EnableWindow(edit_hwnd,enabled);\
}\
}\
}
//NOTE(Vidar): Setup color picker
#define TL_COLOR_PARAM(name){\
HWND swatch_hwnd=GetDlgItem(hWnd,IDC_##name##_SWATCH);\
IColorSwatch *swatch=GetIColorSwatch(swatch_hwnd);\
Color col(yarn_type.name.r,yarn_type.name.g,\
yarn_type.name.b);\
swatch->SetColor(col);\
ReleaseIColorSwatch(swatch);\
if(data->yarn_type>0){\
uint8_t enabled=yarn_type.name##_enabled;\
Button_SetCheck(GetDlgItem(hWnd,IDC_##name##_OVERRIDE),enabled);\
EnableWindow(swatch_hwnd,enabled);\
}\
}
TL_YARN_PARAMETERS
#undef TL_FLOAT_PARAM
#undef TL_COLOR_PARAM
for(int i=0; i<NUMBER_OF_YRN_TEXMAPS; i++){
int subtexmap_id=
data->yarn_type*
NUMBER_OF_YRN_TEXMAPS+i;
Texmap *texmap=data->sm->GetSubTexmap(subtexmap_id);
wchar_t *caption=L"";
if(texmap){
caption=L"M";
}
HWND btn_hwnd = GetDlgItem(hWnd,texmapBtnIDCs[i]);
SetWindowText(btn_hwnd,caption);
}
break;
}
case WM_DESTROY:
break;
#define GET_YARN_TYPE\
YarnTypeDlgProcData *data = (YarnTypeDlgProcData*)\
GetWindowLongPtr(hWnd,GWLP_USERDATA);\
tlYarnType * yarn_type = &data->sm->m_weave_parameters->yarn_types[\
data->yarn_type];
//NOTE(Vidar): Hack to update the preview ball
#define UPDATE_BALL\
data->sm->pblock->SetValue(mtl_dummy,0,0.5f);
//NOTE(Vidar): Update checkbox
case WM_COMMAND: {
switch(HIWORD(wParam)){
case BN_CLICKED:{
GET_YARN_TYPE
switch(id){
#define TL_FLOAT_PARAM(name) case IDC_##name##_OVERRIDE:{\
HWND spin_hwnd = GetDlgItem(hWnd,IDC_##name##_SPIN);\
HWND edit_hwnd = GetDlgItem(hWnd,IDC_##name##_EDIT);\
bool check = Button_GetCheck((HWND)lParam);\
EnableWindow(spin_hwnd,check);\
EnableWindow(edit_hwnd,check);\
yarn_type->name##_enabled = check;\
break;\
}
#define TL_COLOR_PARAM(name) case IDC_##name##_OVERRIDE:{\
HWND swatch_hwnd = GetDlgItem(hWnd,IDC_##name##_SWATCH);\
bool check = Button_GetCheck((HWND)lParam);\
EnableWindow(swatch_hwnd,check);\
yarn_type->name##_enabled = check;\
break;\
}
TL_YARN_PARAMETERS
#undef TL_FLOAT_PARAM
#undef TL_COLOR_PARAM
//NOTE(Vidar): Texmap buttons
default: {
if(data->sm->m_weave_parameters->pattern) {
for(int i = 0; i < NUMBER_OF_YRN_TEXMAPS; i++) {
if (LOWORD(wParam) == texmapBtnIDCs[i]
&& HIWORD(wParam) == BN_CLICKED){
//set mtl!
int subtexmap_id=
data->yarn_type*
NUMBER_OF_YRN_TEXMAPS+i;
//User pressed Texmap button for ith submap
PostMessage(data->sm->m_hwMtlEdit,
WM_TEXMAP_BUTTON,subtexmap_id,
(LPARAM)data->sm);
}
}
}
}
}
UPDATE_BALL;
break;
}
}
}
//NOTE(Vidar): Update float parameters
case CC_SPINNER_CHANGE: {
GET_YARN_TYPE
ISpinnerControl *spinner = (ISpinnerControl*)lParam;
switch(id){
#define TL_FLOAT_PARAM(name)\
case IDC_##name##_SPIN:\
yarn_type->name = spinner->GetFVal(); break;
#define TL_COLOR_PARAM(name)
TL_YARN_PARAMETERS
#undef TL_FLOAT_PARAM
#undef TL_COLOR_PARAM
}
UPDATE_BALL
break;
}
//NOTE(Vidar): Update color parameters
case CC_COLOR_CHANGE:{
GET_YARN_TYPE
IColorSwatch *swatch = (IColorSwatch*)lParam;
COLORREF col = swatch->GetColor();
switch(id){
#define TL_FLOAT_PARAM(name)
#define TL_COLOR_PARAM(name)\
case IDC_##name##_SWATCH:\
yarn_type->name.r = (float)GetRValue(col)/255.f;\
yarn_type->name.g = (float)GetGValue(col)/255.f;\
yarn_type->name.b = (float)GetBValue(col)/255.f;\
break;
TL_YARN_PARAMETERS
#undef TL_FLOAT_PARAM
#undef TL_COLOR_PARAM
}
UPDATE_BALL
break;
}
case WM_CONTEXTMENU:
{
GET_YARN_TYPE
ThunderLoomMtl *sm=data->sm;
// Note(Peter): Allow for right click context menu when rightclicking
// yarn texmaps, much like the way when clicking regular texmaps buttons
// handled by a paramblock. Is probably a a better way to get the same
// menu without, cannot find how though, this will do for now.
POINT ptScreen={LOWORD(lParam), HIWORD(lParam)};
POINT ptClient=ptScreen;
ScreenToClient(hWnd,&ptClient);
HWND hWndChild=RealChildWindowFromPoint(hWnd,ptClient); //Get handle to clicked child window
for(int i=0; i<NUMBER_OF_YRN_TEXMAPS; i++){
HWND btn_hwnd=GetDlgItem(hWnd,texmapBtnIDCs[i]);
if(hWndChild==btn_hwnd){
//We right clicked on a texture button!
int subtex_id=NUMBER_OF_YRN_TEXMAPS*data->yarn_type+i;
Texmap *texmap=sm->GetSubTexmap(subtex_id);
if(!texmap){
break;
}
HMENU hMenu=CreatePopupMenu();
LPCTSTR menuLabel=L"Clear\0";
AppendMenu(hMenu,MF_STRING,IDR_MENU_TEX_CLEAR,menuLabel);
int return_value=TrackPopupMenu(hMenu,TPM_LEFTALIGN+TPM_TOPALIGN+TPM_RETURNCMD+TPM_RIGHTBUTTON+TPM_NOANIMATION,ptScreen.x,ptScreen.y,0,hWnd,0);
if(return_value==IDR_MENU_TEX_CLEAR){
//We clicked on Clear item in context menu
sm->SetSubTexmap(subtex_id,NULL);
SetWindowText(btn_hwnd,L"");
}
}
}
UPDATE_BALL;
}
}
return FALSE;
}
/*===========================================================================*\
| Constructor and Reset systems
| Ask the ClassDesc2 to make the AUTO_CONSTRUCT paramblocks and wire them in
\*===========================================================================*/
void ThunderLoomMtl::Reset() {
//TODO(Vidar): Better default pattern...
uint8_t warp_above[] = { 0,1,1,0 };
uint8_t yarn_type[] = { 1,2,2,1 };
uint32_t pattern_width = 2;
uint32_t pattern_height = 2;
tlColor yarn_colors[2];
yarn_colors[0].r = 0.3f; yarn_colors[0].g = 0.3f; yarn_colors[0].b = 0.3f;
yarn_colors[1].r = 1.f; yarn_colors[1].g = 1.f; yarn_colors[1].b = 1.f;
uint32_t num_yarn_types = 2;
tlWeaveParameters* param = tl_weave_pattern_from_data(warp_above, yarn_type, num_yarn_types, yarn_colors, pattern_width, pattern_height);
// TODO: Dynamic Load
//#define DYNAMIC_FUNC_ARG_TYPES uint8_t*, uint8_t*, uint32_t, tlColor*, uint32_t, uint32_t
//#define DYNAMIC_FUNC_ARG_NAMES warp_above, yarn_type, num_yarn_types, yarn_colors, pattern_width, pattern_height
// CALL_DYNAMIC_FUNC(tl_weave_pattern_from_data, tlWeaveParameters *, param)
//#undef DYNAMIC_FUNC_ARG_TYPES
//#undef DYNAMIC_FUNC_ARG_NAMES
m_weave_parameters = param;
m_i_mtl_params = 0;
ivalid.SetEmpty();
thunderLoomDesc.Reset(this);
}
ThunderLoomMtl::ThunderLoomMtl(BOOL loading) {
m_i_mtl_params = 0;
pblock=NULL;
ivalid.SetEmpty();
thunderLoomDesc.MakeAutoParamBlocks(this);
Reset();
}
ParamDlg* ThunderLoomMtl::CreateParamDlg(HWND hwMtlEdit, IMtlParams *imp) {
m_hwMtlEdit = hwMtlEdit;
m_imp = imp;
IAutoMParamDlg* masterDlg
= thunderLoomDesc.CreateParamDlgs(hwMtlEdit, imp, this);
m_i_mtl_params = imp;
masterDlg->SetThing(this);
return masterDlg;
}
BOOL ThunderLoomMtl::SetDlgThing(ParamDlg* dlg) {
return FALSE;
}
Interval ThunderLoomMtl::Validity(TimeValue t) {
Interval temp=FOREVER;
pblock->GetValidity(t, temp);
//Update(t, temp);
//TODO(Peter): is validity okay?
// do we need to loop through each texmap and check fro validity there to?
return temp;
}
/*===========================================================================*\
| Subanim & References support
\*===========================================================================*/
RefTargetHandle ThunderLoomMtl::GetReference(int i) {
if (i==0) return pblock;
return NULL;
}
void ThunderLoomMtl::SetReference(int i, RefTargetHandle rtarg) {
if (i==0) pblock=(IParamBlock2*) rtarg;
}
TSTR ThunderLoomMtl::SubAnimName(int i) {
if (i==0) return _T("Parameters");
return _T("");
}
Animatable* ThunderLoomMtl::SubAnim(int i) {
if (i==0) return pblock;
return NULL;
}
RefResult ThunderLoomMtl::NotifyRefChanged(NOTIFY_REF_CHANGED_ARGS) {
switch (message) {
case REFMSG_CHANGE:
ivalid.SetEmpty();
if (hTarget == pblock) {
if (m_weave_parameters->pattern) {
ParamID changing_param = pblock->LastNotifyParamID();
thunder_loom_param_blk_desc.InvalidateUI(changing_param);
//TODO(Vidar): Not sure about the TimeValue... using 0 for now
switch(changing_param){
case mtl_realworld:
{
int realworld;
pblock->GetValue(mtl_realworld,0,realworld,ivalid);
m_weave_parameters->realworld_uv=realworld;
break;
}
case mtl_uscale:
{
pblock->GetValue(mtl_uscale,0,m_weave_parameters->uscale,
ivalid);
break;
}
case mtl_vscale:
{
pblock->GetValue(mtl_vscale,0,m_weave_parameters->vscale,
ivalid);
break;
}
case mtl_uvrotation:
{
pblock->GetValue(mtl_uvrotation,0,m_weave_parameters->uvrotation,
ivalid);
break;
}
}
}
break;
}
}
return(REF_SUCCEED);
}
// Texmaps
int ThunderLoomMtl::NumSubTexmaps() {
return this->m_weave_parameters->num_yarn_types*NUMBER_OF_YRN_TEXMAPS;
}
Texmap* ThunderLoomMtl::GetSubTexmap(int i) {
if (i < NumSubTexmaps()) {
return pblock->GetTexmap(texmaps, 0, i);
} else {
return NULL;
}
}
void ThunderLoomMtl::SetSubTexmap(int i, Texmap* m) {
if (i < NumSubTexmaps()) {
pblock->SetValue(texmaps, 0, m, i);
}
}
TSTR ThunderLoomMtl::GetSubTexmapSlotName(int i) {
int yrntexmap_id = i;
switch(yrntexmap_id % NUMBER_OF_YRN_TEXMAPS) {
#define YARN_TYPE_TEXMAP(param) case yrn_texmaps_##param: return L#param;
YARN_TYPE_TEXMAP_PARAMETERS
#undef YARN_TYPE_TEXMAP
default:
return L"";
}
return L"";
}
TSTR ThunderLoomMtl::GetSubTexmapTVName(int i) {
return GetSubTexmapSlotName(i);
}
/*===========================================================================*\
| Standard IO
\*===========================================================================*/
#define MTL_HDR_CHUNK 0x4000
#define YARN_TYPE_CHUNK 0x0200
IOResult ThunderLoomMtl::Save(ISave *isave)
{
IOResult res;
isave->BeginChunk(MTL_HDR_CHUNK);
res=MtlBase::Save(isave);
if(res!=IO_OK) return res;
isave->EndChunk();
isave->BeginChunk(YARN_TYPE_CHUNK);
//NOTE(Vidar):Save yarn types
long len=0;
unsigned char* data = tl_pattern_to_ptn_file(m_weave_parameters, &len);
// TODO: Dynamic Load
//#define DYNAMIC_FUNC_ARG_TYPES tlWeaveParameters*, long*
//#define DYNAMIC_FUNC_ARG_NAMES m_weave_parameters, &len
// CALL_DYNAMIC_FUNC(tl_pattern_to_ptn_file, unsigned char *, data)
//#undef DYNAMIC_FUNC_ARG_TYPES
//#undef DYNAMIC_FUNC_ARG_NAMES
ULONG nb;
int version=2;
isave->Write((unsigned char*)&version,
sizeof(int),&nb);
isave->Write(data,len,&nb);
free(data);
isave->EndChunk();
return IO_OK;
}
IOResult ThunderLoomMtl::Load(ILoad *iload) {
IOResult res;
int id;
unsigned long n_read = 0;
ULONG nb;
while (IO_OK==(res=iload->OpenChunk())) {
switch(id = iload->CurChunkID()) {
case MTL_HDR_CHUNK:
res = MtlBase::Load(iload);
break;
case YARN_TYPE_CHUNK:
{
int version;
iload->Read((unsigned char*)&version,
sizeof(int), &nb);
unsigned long len=iload->CurChunkLengthRemaining();
const char *error_buffer=0;
if(version==90){
unsigned char *data=(unsigned char *)calloc(len+sizeof(int),1);
*(int*)data=1;
iload->Read(data+sizeof(int),len,&nb);
tlWeaveParameters* param = tl_weave_pattern_from_ptn(data, len, &error_buffer);
// TODO: Dynamic Load
//#define DYNAMIC_FUNC_ARG_TYPES unsigned char*, long, const char **
//#define DYNAMIC_FUNC_ARG_NAMES data, len, &error_buffer
// CALL_DYNAMIC_FUNC(tl_weave_pattern_from_ptn, tlWeaveParameters *, param)
//#undef DYNAMIC_FUNC_ARG_TYPES
//#undef DYNAMIC_FUNC_ARG_NAMES
m_weave_parameters = param;
free(data);
}else{
unsigned char *data=(unsigned char *)calloc(len,1);
iload->Read(data,len,&nb);
tlWeaveParameters* param = tl_weave_pattern_from_ptn(data, len, &error_buffer);
// TODO: Dynamic Load
// #define DYNAMIC_FUNC_ARG_TYPES unsigned char*, long, const char **
// #define DYNAMIC_FUNC_ARG_NAMES data, len, &error_buffer
// CALL_DYNAMIC_FUNC(tl_weave_pattern_from_ptn, tlWeaveParameters *, param)
// #undef DYNAMIC_FUNC_ARG_TYPES
// #undef DYNAMIC_FUNC_ARG_NAMES
m_weave_parameters = param;
free(data);
}
//TODO(Vidar):Initialize m_weave_parameters to default pattern if there was an error
// (if it is 0)
if(error_buffer!=0){
OutputDebugStringA("Error:\n");
OutputDebugStringA(error_buffer);
OutputDebugStringA("\n");
}
//NOTE(Vidar):Load m_weave_parameters
/*
tlWeaveParameters *params = (tlWeaveParameters*)calloc(1,sizeof(tlWeaveParameters));
iload->Read((unsigned char*)params,
sizeof(tlWeaveParameters), &nb);
params->yarn_types =
(tlYarnType*)calloc(params->num_yarn_types,
sizeof(tlYarnType));
iload->Read((unsigned char*)params->yarn_types,
params->num_yarn_types * sizeof(tlYarnType), &nb);
int num_entries = params->pattern_width * params->pattern_height;
params->pattern = (PatternEntry*)calloc(num_entries,
sizeof(PatternEntry));
iload->Read((unsigned char*)params->pattern,
num_entries * sizeof(PatternEntry), &nb);
m_weave_parameters = params;
*/
break;
}
}
iload->CloseChunk();
if (res!=IO_OK) return res;
}
return IO_OK;
}
/*===========================================================================*\
| Updating and cloning
\*===========================================================================*/
RefTargetHandle ThunderLoomMtl::Clone(RemapDir &remap) {
ThunderLoomMtl *mnew = new ThunderLoomMtl(FALSE);
*((MtlBase*)mnew) = *((MtlBase*)this);
BaseClone(this, mnew, remap);
mnew->ReplaceReference(0, remap.CloneRef(pblock));
mnew->ivalid.SetEmpty();
mnew->m_weave_parameters=
(tlWeaveParameters*)calloc(1,sizeof(tlWeaveParameters));
*mnew->m_weave_parameters=*m_weave_parameters;
if(m_weave_parameters->pattern){
int num_entries=m_weave_parameters->pattern_width *
m_weave_parameters->pattern_height;
PatternEntry *pattern;
pattern=(PatternEntry*)calloc(num_entries,sizeof(PatternEntry));
memcpy(pattern,m_weave_parameters->pattern,
sizeof(PatternEntry)*num_entries);
mnew->m_weave_parameters->pattern=pattern;
int num_yarn_types = m_weave_parameters->num_yarn_types;
mnew->m_weave_parameters->yarn_types=(tlYarnType*)calloc(num_yarn_types,
sizeof(tlYarnType));
memcpy(mnew->m_weave_parameters->yarn_types,
m_weave_parameters->yarn_types,
num_yarn_types*sizeof(tlYarnType));
}
return (RefTargetHandle) mnew;
}
void ThunderLoomMtl::NotifyChanged() {
NotifyDependents(FOREVER, PART_ALL, REFMSG_CHANGE);
}
void ThunderLoomMtl::Update(TimeValue t, Interval& valid) {
//TODO(Peter): Verify this!, call update on textures
//if( pblock->GetTexmap(mtl_warpvar) )
// pblock->GetTexmap(mtl_warpvar)->Update(t, ivalid);
if (!ivalid.InInterval(t)) {
ivalid.SetInfinite();
}
valid &= ivalid;
}
/*===========================================================================*\
| Determine the characteristics of the material (required for Mtl class)
\*===========================================================================*/
void ThunderLoomMtl::SetAmbient(Color c, TimeValue t) {}
void ThunderLoomMtl::SetDiffuse(Color c, TimeValue t) {}
void ThunderLoomMtl::SetSpecular(Color c, TimeValue t) {}
void ThunderLoomMtl::SetShininess(float v, TimeValue t) {}
Color ThunderLoomMtl::GetAmbient(int mtlNum, BOOL backFace) {
return Color(0,0,0);
}
Color ThunderLoomMtl::GetDiffuse(int mtlNum, BOOL backFace) {
return Color(0.8f, 0.8f, 0.8f);
}