Repository navigation
Expand file tree
/
Copy pathmodel.cpp
More file actions
998 lines (754 loc) · 23.8 KB
/
Copy pathmodel.cpp
File metadata and controls
998 lines (754 loc) · 23.8 KB
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
//=============================================================================
//
// モデルの処理 [model.cpp]
// Author :
//
//=============================================================================
#define _CRT_SECURE_NO_WARNINGS
#include "main.h"
#include "model.h"
#include "camera.h"
#include <vector>
#include <iostream>
#include "texture.h"
//*****************************************************************************
// マクロ定義
//*****************************************************************************
#define VALUE_MOVE_MODEL (0.50f) // 移動速度
#define RATE_MOVE_MODEL (0.20f) // 移動慣性係数
#define VALUE_ROTATE_MODEL (XM_PI * 0.05f) // 回転速度
#define RATE_ROTATE_MODEL (0.20f) // 回転慣性係数
#define SCALE_MODEL (10.0f) // 回転慣性係数
//*****************************************************************************
// 構造体定義
//*****************************************************************************
// マテリアル構造体
struct MODEL_MATERIAL
{
char Name[256];
MATERIAL Material;
char TextureName[256];
};
// 描画サブセット構造体
struct SUBSET
{
unsigned short StartIndex;
unsigned short IndexNum;
MODEL_MATERIAL Material;
};
// モデル構造体
struct MODEL
{
VERTEX_3D *VertexArray;
unsigned short VertexNum;
unsigned short *IndexArray;
unsigned short IndexNum;
SUBSET *SubsetArray;
unsigned short SubsetNum;
};
//*****************************************************************************
// グローバル変数
//*****************************************************************************
//*****************************************************************************
// プロトタイプ宣言
//*****************************************************************************
void LoadObj( char *FileName, MODEL *Model );
void LoadMaterial( char *FileName, MODEL_MATERIAL **MaterialArray, unsigned short *MaterialNum );
//=============================================================================
// 初期化処理
//=============================================================================
void LoadModel( char *FileName, DX11_MODEL *Model )
{
MODEL model;
LoadObj( FileName, &model );
// 頂点バッファ生成
{
D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof( VERTEX_3D ) * model.VertexNum;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA sd;
ZeroMemory( &sd, sizeof(sd) );
sd.pSysMem = model.VertexArray;
GetDevice()->CreateBuffer( &bd, &sd, &Model->VertexBuffer );
}
// インデックスバッファ生成
{
D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof( unsigned short ) * model.IndexNum;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA sd;
ZeroMemory( &sd, sizeof(sd) );
sd.pSysMem = model.IndexArray;
GetDevice()->CreateBuffer( &bd, &sd, &Model->IndexBuffer );
}
// サブセット設定
{
Model->SubsetArray = new DX11_SUBSET[ model.SubsetNum ];
Model->SubsetNum = model.SubsetNum;
//Model->SubsetArray[0].Material.Texture = NULL;
for( unsigned short i = 0; i < model.SubsetNum; i++ )
{
Model->SubsetArray[i].StartIndex = model.SubsetArray[i].StartIndex;
Model->SubsetArray[i].IndexNum = model.SubsetArray[i].IndexNum;
Model->SubsetArray[i].Material.Material = model.SubsetArray[i].Material.Material;
Model->SubsetArray[i].Material.Texture = NULL;
D3DX11CreateShaderResourceViewFromFile( GetDevice(),
model.SubsetArray[i].Material.TextureName,
NULL,
NULL,
&Model->SubsetArray[i].Material.Texture,
NULL );
}
}
delete[] model.VertexArray;
delete[] model.IndexArray;
delete[] model.SubsetArray;
}
//=============================================================================
// 終了処理
//=============================================================================
void UnloadModel( DX11_MODEL *Model )
{
for (unsigned short i = 0; i < Model->SubsetNum; i++)
{
if (Model->SubsetArray[i].Material.Texture)
{
Model->SubsetArray[i].Material.Texture->Release();
Model->SubsetArray[i].Material.Texture = NULL;
}
}
if( Model->VertexBuffer ) Model->VertexBuffer->Release();
if( Model->IndexBuffer ) Model->IndexBuffer->Release();
if( Model->SubsetArray ) delete[] Model->SubsetArray;
}
//=============================================================================
// 描画処理
//=============================================================================
void DrawModel( DX11_MODEL *Model )
{
// 頂点バッファ設定
UINT stride = sizeof( VERTEX_3D );
UINT offset = 0;
GetDeviceContext()->IASetVertexBuffers( 0, 1, &Model->VertexBuffer, &stride, &offset );
// インデックスバッファ設定
GetDeviceContext()->IASetIndexBuffer( Model->IndexBuffer, DXGI_FORMAT_R16_UINT, 0 );
// プリミティブトポロジ設定
GetDeviceContext()->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
for( unsigned short i = 0; i < Model->SubsetNum; i++ )
{
// マテリアル設定
SetMaterial( Model->SubsetArray[i].Material.Material );
// テクスチャ設定
if (Model->SubsetArray[i].Material.Material.noTexSampling == 0)
{
GetDeviceContext()->PSSetShaderResources(0, 1, &Model->SubsetArray[i].Material.Texture);
}
// ポリゴン描画
GetDeviceContext()->DrawIndexed( Model->SubsetArray[i].IndexNum, Model->SubsetArray[i].StartIndex, 0 );
}
}
void DrawFBXModel(DX11_MODEL* Model)
{
// 頂点バッファ設定
UINT stride = sizeof(VERTEX_3D);
UINT offset = 0;
GetDeviceContext()->IASetVertexBuffers(0, 1, &Model->VertexBuffer, &stride, &offset);
// インデックスバッファ設定
GetDeviceContext()->IASetIndexBuffer(Model->IndexBuffer, DXGI_FORMAT_R32_UINT, 0);
// プリミティブトポロジ設定
GetDeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
for (unsigned short i = 0; i < Model->SubsetNum; i++)
{
// マテリアル設定
SetMaterial(Model->SubsetArray[i].Material.Material);
// テクスチャ設定
if (Model->SubsetArray[i].Material.Material.noTexSampling == 0)
{
GetDeviceContext()->PSSetShaderResources(0, 1, &Model->SubsetArray[i].Material.Texture);
}
// ポリゴン描画
GetDeviceContext()->DrawIndexed(Model->SubsetArray[i].IndexNum, Model->SubsetArray[i].StartIndex, 0);
}
}
//モデル読込////////////////////////////////////////////
void LoadObj( char *FileName, MODEL *Model )
{
XMFLOAT3 *positionArray;
XMFLOAT3 *normalArray;
XMFLOAT2 *texcoordArray;
unsigned short positionNum = 0;
unsigned short normalNum = 0;
unsigned short texcoordNum = 0;
unsigned short vertexNum = 0;
unsigned short indexNum = 0;
unsigned short in = 0;
unsigned short subsetNum = 0;
MODEL_MATERIAL *materialArray = NULL;
unsigned short materialNum = 0;
char str[256];
char *s;
char c;
FILE *file;
file = fopen( FileName, "rt" );
if( file == NULL )
{
printf( "エラー:LoadModel %s \n", FileName );
return;
}
//要素数カウント
while( TRUE )
{
fscanf( file, "%s", str );
if( feof( file ) != 0 )
break;
if( strcmp( str, "v" ) == 0 )
{
positionNum++;
}
else if( strcmp( str, "vn" ) == 0 )
{
normalNum++;
}
else if( strcmp( str, "vt" ) == 0 )
{
texcoordNum++;
}
else if( strcmp( str, "usemtl" ) == 0 )
{
subsetNum++;
}
else if( strcmp( str, "f" ) == 0 )
{
in = 0;
do
{
fscanf( file, "%s", str );
vertexNum++;
in++;
c = fgetc( file );
}
while( c != '\n' && c!= '\r' );
//四角は三角に分割
if( in == 4 )
in = 6;
indexNum += in;
}
}
//メモリ確保
positionArray = new XMFLOAT3[ positionNum ];
normalArray = new XMFLOAT3[ normalNum ];
texcoordArray = new XMFLOAT2[ texcoordNum ];
Model->VertexArray = new VERTEX_3D[ vertexNum ];
Model->VertexNum = vertexNum;
Model->IndexArray = new unsigned short[ indexNum ];
Model->IndexNum = indexNum;
Model->SubsetArray = new SUBSET[ subsetNum ];
Model->SubsetNum = subsetNum;
//要素読込
XMFLOAT3 *position = positionArray;
XMFLOAT3 *normal = normalArray;
XMFLOAT2 *texcoord = texcoordArray;
unsigned short vc = 0;
unsigned short ic = 0;
unsigned short sc = 0;
fseek( file, 0, SEEK_SET );
while( TRUE )
{
fscanf( file, "%s", str );
if( feof( file ) != 0 )
break;
if( strcmp( str, "mtllib" ) == 0 )
{
//マテリアルファイル
fscanf( file, "%s", str );
char path[256];
// strcpy( path, "data/model/" );
//----------------------------------- フォルダー対応
strcpy(path, FileName);
char* adr = path;
char* ans = adr;
while (1)
{
adr = strstr(adr, "/");
if (adr == NULL) break;
else ans = adr;
adr++;
}
if (path != ans) ans++;
*ans = 0;
//-----------------------------------
strcat( path, str );
LoadMaterial( path, &materialArray, &materialNum );
}
else if( strcmp( str, "o" ) == 0 )
{
//オブジェクト名
fscanf( file, "%s", str );
}
else if( strcmp( str, "v" ) == 0 )
{
//頂点座標
fscanf( file, "%f", &position->x );
fscanf( file, "%f", &position->y );
fscanf( file, "%f", &position->z );
position->x *= SCALE_MODEL;
position->y *= SCALE_MODEL;
position->z *= SCALE_MODEL;
position++;
}
else if( strcmp( str, "vn" ) == 0 )
{
//法線
fscanf( file, "%f", &normal->x );
fscanf( file, "%f", &normal->y );
fscanf( file, "%f", &normal->z );
normal++;
}
else if( strcmp( str, "vt" ) == 0 )
{
//テクスチャ座標
fscanf( file, "%f", &texcoord->x );
fscanf( file, "%f", &texcoord->y );
texcoord->y = 1.0f - texcoord->y;
texcoord++;
}
else if( strcmp( str, "usemtl" ) == 0 )
{
//マテリアル
fscanf( file, "%s", str );
if( sc != 0 )
Model->SubsetArray[ sc - 1 ].IndexNum = ic - Model->SubsetArray[ sc - 1 ].StartIndex;
Model->SubsetArray[ sc ].StartIndex = ic;
for( unsigned short i = 0; i < materialNum; i++ )
{
if( strcmp( str, materialArray[i].Name ) == 0 )
{
Model->SubsetArray[ sc ].Material.Material = materialArray[i].Material;
strcpy( Model->SubsetArray[ sc ].Material.TextureName, materialArray[i].TextureName );
strcpy( Model->SubsetArray[ sc ].Material.Name, materialArray[i].Name );
break;
}
}
sc++;
}
else if( strcmp( str, "f" ) == 0 )
{
//面
in = 0;
do
{
fscanf( file, "%s", str );
s = strtok( str, "/" );
Model->VertexArray[vc].Position = positionArray[ atoi( s ) - 1 ];
if( s[ strlen( s ) + 1 ] != '/' )
{
//テクスチャ座標が存在しない場合もある
s = strtok( NULL, "/" );
Model->VertexArray[vc].TexCoord = texcoordArray[ atoi( s ) - 1 ];
}
s = strtok( NULL, "/" );
Model->VertexArray[vc].Normal = normalArray[ atoi( s ) - 1 ];
Model->VertexArray[vc].Diffuse = XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f );
Model->IndexArray[ic] = vc;
ic++;
vc++;
in++;
c = fgetc( file );
}
while( c != '\n' && c != '\r' );
//四角は三角に分割
if( in == 4 )
{
Model->IndexArray[ic] = vc - 4;
ic++;
Model->IndexArray[ic] = vc - 2;
ic++;
}
}
}
if( sc != 0 )
Model->SubsetArray[ sc - 1 ].IndexNum = ic - Model->SubsetArray[ sc - 1 ].StartIndex;
delete[] positionArray;
delete[] normalArray;
delete[] texcoordArray;
delete[] materialArray;
fclose(file);
}
//マテリアル読み込み///////////////////////////////////////////////////////////////////
void LoadMaterial( char *FileName, MODEL_MATERIAL **MaterialArray, unsigned short *MaterialNum )
{
char str[256];
FILE *file;
file = fopen( FileName, "rt" );
if( file == NULL )
{
printf( "エラー:LoadMaterial %s \n", FileName );
return;
}
MODEL_MATERIAL *materialArray;
unsigned short materialNum = 0;
//要素数カウント
while( TRUE )
{
fscanf( file, "%s", str );
if( feof( file ) != 0 )
break;
if( strcmp( str, "newmtl" ) == 0 )
{
materialNum++;
}
}
//メモリ確保
materialArray = new MODEL_MATERIAL[ materialNum ];
ZeroMemory(materialArray, sizeof(MODEL_MATERIAL)*materialNum);
//要素読込
int mc = -1;
fseek( file, 0, SEEK_SET );
while( TRUE )
{
fscanf( file, "%s", str );
if( feof( file ) != 0 )
break;
if( strcmp( str, "newmtl" ) == 0 )
{
//マテリアル名
mc++;
fscanf( file, "%s", materialArray[ mc ].Name );
strcpy( materialArray[ mc ].TextureName, "" );
materialArray[mc].Material.noTexSampling = 1;
}
else if( strcmp( str, "Ka" ) == 0 )
{
//アンビエント
fscanf( file, "%f", &materialArray[ mc ].Material.Ambient.x );
fscanf( file, "%f", &materialArray[ mc ].Material.Ambient.y );
fscanf( file, "%f", &materialArray[ mc ].Material.Ambient.z );
materialArray[ mc ].Material.Ambient.w = 1.0f;
}
else if( strcmp( str, "Kd" ) == 0 )
{
//ディフューズ
fscanf( file, "%f", &materialArray[ mc ].Material.Diffuse.x );
fscanf( file, "%f", &materialArray[ mc ].Material.Diffuse.y );
fscanf( file, "%f", &materialArray[ mc ].Material.Diffuse.z );
// Mayaでテクスチャを貼ると0.0fになっちゃうみたいなので
if ((materialArray[mc].Material.Diffuse.x + materialArray[mc].Material.Diffuse.y + materialArray[mc].Material.Diffuse.z) == 0.0f)
{
materialArray[mc].Material.Diffuse.x = materialArray[mc].Material.Diffuse.y = materialArray[mc].Material.Diffuse.z = 1.0f;
}
materialArray[ mc ].Material.Diffuse.w = 1.0f;
}
else if( strcmp( str, "Ks" ) == 0 )
{
//スペキュラ
fscanf( file, "%f", &materialArray[ mc ].Material.Specular.x );
fscanf( file, "%f", &materialArray[ mc ].Material.Specular.y );
fscanf( file, "%f", &materialArray[ mc ].Material.Specular.z );
materialArray[ mc ].Material.Specular.w = 1.0f;
}
else if( strcmp( str, "Ns" ) == 0 )
{
//スペキュラ強度
fscanf( file, "%f", &materialArray[ mc ].Material.Shininess );
}
else if( strcmp( str, "d" ) == 0 )
{
//アルファ
fscanf( file, "%f", &materialArray[ mc ].Material.Diffuse.w );
}
else if( strcmp( str, "map_Kd" ) == 0 )
{
//テクスチャ
fscanf( file, "%s", str );
char path[256];
// strcpy( path, "data/model/" );
//----------------------------------- フォルダー対応
strcpy(path, FileName);
char* adr = path;
char* ans = adr;
while (1)
{
adr = strstr(adr, "/");
if (adr == NULL) break;
else ans = adr;
adr++;
}
if (path != ans) ans++;
*ans = 0;
//-----------------------------------
strcat( path, str );
strcat( materialArray[ mc ].TextureName, path );
materialArray[mc].Material.noTexSampling = 0;
}
}
*MaterialArray = materialArray;
*MaterialNum = materialNum;
fclose(file);
}
// モデルの全マテリアルのディフューズを取得する。Max16個分にしてある
void GetModelDiffuse(DX11_MODEL *Model, XMFLOAT4 *diffuse)
{
int max = (Model->SubsetNum < MODEL_MAX_MATERIAL) ? Model->SubsetNum : MODEL_MAX_MATERIAL;
for (unsigned short i = 0; i < max; i++)
{
// ディフューズ設定
diffuse[i] = Model->SubsetArray[i].Material.Material.Diffuse;
}
}
// モデルの指定マテリアルのディフューズをセットする。
void SetModelDiffuse(DX11_MODEL *Model, int mno, XMFLOAT4 diffuse)
{
// ディフューズ設定
Model->SubsetArray[mno].Material.Material.Diffuse = diffuse;
}
// FBXモデルの読み込み
void LoadFBXModel(char* FileName, DX11_MODEL* dxModel)
{
FbxManager* fbxManager = FbxManager::Create();
FbxIOSettings* ios = FbxIOSettings::Create(fbxManager, IOSROOT);
fbxManager->SetIOSettings(ios);
FbxImporter* importer = FbxImporter::Create(fbxManager, "");
FbxScene* scene = FbxScene::Create(fbxManager, "scene");
FbxAxisSystem sceneAxisSystem = scene->GetGlobalSettings().GetAxisSystem();
FbxAxisSystem dxAxisSystem(FbxAxisSystem::eDirectX);
if (sceneAxisSystem != dxAxisSystem) {
dxAxisSystem.ConvertScene(scene); //座標系をDirectXに変換
}
FbxSystemUnit sceneUnit = scene->GetGlobalSettings().GetSystemUnit();
if (sceneUnit != FbxSystemUnit::m) {
FbxSystemUnit::m.ConvertScene(scene);
}
if (!importer->Initialize(FileName, -1, fbxManager->GetIOSettings())) {
std::cout << "FBX Import failed" << std::endl;
return;
}
importer->Import(scene);
importer->Destroy();
FbxNode* root = scene->GetRootNode();
if (!root) return;
std::vector<VERTEX_3D> vertices;
std::vector<UINT> indices;
for (int i = 0; i < root->GetChildCount(); ++i) {
FbxNode* node = root->GetChild(i);
FbxMesh* mesh = node->GetMesh();
if (!mesh) continue;
FbxVector4* ctrlPoints = mesh->GetControlPoints();
FbxAMatrix transform = node->EvaluateGlobalTransform();
FbxStringList uvSetNames;
mesh->GetUVSetNames(uvSetNames);
const char* uvSet = (uvSetNames.GetCount() > 0) ? uvSetNames[0] : "";
int polyCount = mesh->GetPolygonCount();
for (int i = 0; i < polyCount; ++i) {
int polySize = mesh->GetPolygonSize(i);
if (polySize != 3) continue; // skip non-triangle
for (int j = 0; j < 3; ++j) {
VERTEX_3D v;
int ctrlIdx = mesh->GetPolygonVertex(i, j);
FbxVector4 pos = mesh->GetControlPointAt(ctrlIdx);
FbxVector4 worldPos = transform.MultT(pos);
v.Position = XMFLOAT3(
static_cast<float>(worldPos[0]),
static_cast<float>(worldPos[1]),
static_cast<float>(worldPos[2])
);
FbxVector4 normal;
if (mesh->GetPolygonVertexNormal(i, j, normal)) {
normal = transform.MultT(normal);
normal.Normalize();
v.Normal = XMFLOAT3(
static_cast<float>(normal[0]),
static_cast<float>(normal[1]),
static_cast<float>(normal[2])
);
}
v.Diffuse = XMFLOAT4(1, 1, 1, 1);
FbxVector2 uv;
bool unmapped;
if (mesh->GetPolygonVertexUV(i, j, uvSet, uv, unmapped)) {
/*v.TexCoord = XMFLOAT2(
static_cast<float>(uv[0]),
static_cast<float>(uv[1])
);*/
// UVは逆にした、この下の使う
v.TexCoord = XMFLOAT2(
static_cast<float>(uv[0]),
1.0f - static_cast<float>(uv[1]) // Flip V axis
);
}
vertices.push_back(v);
indices.push_back((UINT)indices.size());
}
}
if (indices.size() % 3 != 0)
{
return;
}
dxModel->SubsetNum = 1;
dxModel->SubsetArray = new DX11_SUBSET[1];
dxModel->SubsetArray[0].StartIndex = 0;
dxModel->SubsetArray[0].IndexNum = (UINT)indices.size();
dxModel->SubsetArray[0].Material.Material.Diffuse = XMFLOAT4(1, 1, 1, 1);
dxModel->SubsetArray[0].Material.Material.Ambient = XMFLOAT4(0.1f, 0.1f, 0.1f, 1);
dxModel->SubsetArray[0].Material.Material.Specular = XMFLOAT4(1, 1, 1, 1);
dxModel->SubsetArray[0].Material.Material.Emission = XMFLOAT4(0, 0, 0, 1);
dxModel->SubsetArray[0].Material.Material.Shininess = 8.0f;
dxModel->SubsetArray[0].Material.Material.noTexSampling = 1;
dxModel->SubsetArray[0].Material.Texture = nullptr;
//テクスチャを読み込む
FbxSurfaceMaterial* material = node->GetMaterial(0);
if (material) {
FbxProperty prop = material->FindProperty(FbxSurfaceMaterial::sDiffuse);
if (prop.IsValid()) {
FbxDouble3 color = prop.Get<FbxDouble3>();
dxModel->SubsetArray[0].Material.Material.Diffuse = XMFLOAT4((float)color[0], (float)color[1], (float)color[2], 1.0f);
FbxFileTexture* texture = prop.GetSrcObject<FbxFileTexture>(0);
if (texture) {
const char* filepath = texture->GetFileName();
D3DX11CreateShaderResourceViewFromFile(
GetDevice(), filepath, NULL, NULL,
&dxModel->SubsetArray[0].Material.Texture, NULL);
dxModel->SubsetArray[0].Material.Material.noTexSampling = 0;
}
}
}
break;
}
//VertexBuffer
{
D3D11_BUFFER_DESC bd = {};
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(VERTEX_3D) * (UINT)vertices.size();
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vertices.data();
GetDevice()->CreateBuffer(&bd, &sd, &dxModel->VertexBuffer);
}
//IndexBuffer
{
D3D11_BUFFER_DESC bd = {};
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(UINT) * (UINT)indices.size();
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = indices.data();
GetDevice()->CreateBuffer(&bd, &sd, &dxModel->IndexBuffer);
}
fbxManager->Destroy();
}
// Assimpを使ったモデル読み込み
AMODEL* ModelLoad(const char* FileName)
{
AMODEL* model = new AMODEL;
const std::string modelPath(FileName);
model->AiScene = aiImportFile(FileName, aiProcessPreset_TargetRealtime_MaxQuality | aiProcess_ConvertToLeftHanded);
assert(model->AiScene);
model->VertexBuffer = new ID3D11Buffer * [model->AiScene->mNumMeshes];//頂点データポインター
model->IndexBuffer = new ID3D11Buffer * [model->AiScene->mNumMeshes];//インデックスデータポインター
for (unsigned int m = 0; m < model->AiScene->mNumMeshes; m++)
{
aiMesh* mesh = model->AiScene->mMeshes[m];
// 頂点バッファ生成
{
VERTEX_3D* vertex = new VERTEX_3D[mesh->mNumVertices];//頂点数分の配列領域作成
for (unsigned int v = 0; v < mesh->mNumVertices; v++)
{
vertex[v].Position = XMFLOAT3(mesh->mVertices[v].x, -mesh->mVertices[v].z, mesh->mVertices[v].y);
vertex[v].TexCoord = XMFLOAT2(mesh->mTextureCoords[0][v].x, mesh->mTextureCoords[0][v].y);
vertex[v].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[v].Normal = XMFLOAT3(mesh->mNormals[v].x, -mesh->mNormals[v].z, mesh->mNormals[v].y);
}
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(VERTEX_3D) * mesh->mNumVertices;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11_SUBRESOURCE_DATA sd;
ZeroMemory(&sd, sizeof(sd));
sd.pSysMem = vertex;
GetDevice()->CreateBuffer(&bd, &sd, &model->VertexBuffer[m]);
delete[] vertex;
}
// インデックスバッファ生成
{
unsigned int* index = new unsigned int[mesh->mNumFaces * 3];//ポリゴン数数*3
for (unsigned int f = 0; f < mesh->mNumFaces; f++)
{
const aiFace* face = &mesh->mFaces[f];
assert(face->mNumIndices == 3);
index[f * 3 + 0] = face->mIndices[0];
index[f * 3 + 1] = face->mIndices[1];
index[f * 3 + 2] = face->mIndices[2];
}
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(unsigned int) * mesh->mNumFaces * 3;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA sd;
ZeroMemory(&sd, sizeof(sd));
sd.pSysMem = index;
GetDevice()->CreateBuffer(&bd, &sd, &model->IndexBuffer[m]);
delete[] index;
}
}
//テクスチャ読み込み
for (int i = 0; i < model->AiScene->mNumTextures; i++)
{
aiTexture* aitexture = model->AiScene->mTextures[i];
ID3D11ShaderResourceView* texture;
TexMetadata metadata;
ScratchImage image;
LoadFromWICMemory(aitexture->pcData, aitexture->mWidth, WIC_FLAGS_NONE, &metadata, image);
CreateShaderResourceView(GetDevice(), image.GetImages(), image.GetImageCount(), metadata, &texture);
assert(texture);
model->Texture[aitexture->mFilename.data] = texture;
}
return model;
}
void ModelRelease(AMODEL* model)
{
for (unsigned int m = 0; m < model->AiScene->mNumMeshes; m++)
{
model->VertexBuffer[m]->Release();
model->IndexBuffer[m]->Release();
}
delete[] model->VertexBuffer;
delete[] model->IndexBuffer;
for (std::pair<const std::string, ID3D11ShaderResourceView*> pair : model->Texture)
{
pair.second->Release();
}
aiReleaseImport(model->AiScene);
delete model;
}
void ModelDraw(AMODEL* model)
{
// プリミティブトポロジ設定
GetDeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
for (unsigned int m = 0; m < model->AiScene->mNumMeshes; m++)
{
aiMesh* mesh = model->AiScene->mMeshes[m];
// テクスチャ設定
aiString texture;
aiMaterial* aimaterial = model->AiScene->mMaterials[mesh->mMaterialIndex];
aimaterial->GetTexture(aiTextureType_DIFFUSE, 0, &texture);
if (texture != aiString(""))
GetDeviceContext()->PSSetShaderResources(0, 1, &model->Texture[texture.data]);
// 頂点バッファ設定
UINT stride = sizeof(VERTEX_3D);
UINT offset = 0;
GetDeviceContext()->IASetVertexBuffers(0, 1, &model->VertexBuffer[m], &stride, &offset);
// インデックスバッファ設定
GetDeviceContext()->IASetIndexBuffer(model->IndexBuffer[m], DXGI_FORMAT_R32_UINT, 0);
// ポリゴン描画
GetDeviceContext()->DrawIndexed(mesh->mNumFaces * 3, 0, 0);
}
}