헤더파일

DirectX 프레임워크 만들기 - FBX에서 노말, UV, 정점 얻어오기 본문

DirectX

DirectX 프레임워크 만들기 - FBX에서 노말, UV, 정점 얻어오기

헤더파일 2018. 1. 18. 19:38

struct TextureVertex

{

D3DXVECTOR3 position;

D3DXVECTOR2 texture;

D3DXVECTOR3 normal;

};


정점버퍼에 넘겨줄 구조체를 정의합니다.

position은 FBX의 ControlPoint에서 따오는 것으로 모델의 정점입니다.

texture는 Texture의 UV좌표입니다.

normal은 해당 정점에서의 노말벡터를 의미합니다.


std::vector<TextureVertex> verticies;

이 구조체로 verticies라는 벡터를 만듭니다.



FbxManager* lSdkManager = FbxManager::Create(); 


// Create the IO settings object.

FbxIOSettings *ios = FbxIOSettings::Create(lSdkManager, IOSROOT);

lSdkManager->SetIOSettings(ios);


// Create an importer using the SDK manager.

FbxImporter* lImporter = FbxImporter::Create(lSdkManager, "");


// Use the first argument as the filename for the importer.

if (!lImporter->Initialize(path, -1, lSdkManager->GetIOSettings())) 

{

printf("Call to FbxImporter::Initialize() failed.\n");

printf("Error returned: %s\n\n", lImporter->GetStatus().GetErrorString());

exit(-1);

}

// Create a new scene so that it can be populated by the imported file.

FbxScene* lScene = FbxScene::Create(lSdkManager, "myScene");


// Import the contents of the file into the scene.

lImporter->Import(lScene);


// The file is imported, so get rid of the importer.

lImporter->Destroy();


FbxNode* pFbxRootNode = lScene->GetRootNode();


신에서 루트노드를 얻어온 후에


FbxMesh* lMesh = pFbxRootNode->GetMesh();

const int lVertexCount = lMesh->GetControlPointsCount();

const int lPolygonCount = lMesh->GetPolygonCount();

int vertexCounter = 0;

FbxGeometryElementNormal* normalEl = lMesh->GetElementNormal();


루트노트를 이용해서 메시를 얻어옵니다.

메시에서 컨트롤 포인트의 갯수와 폴리곤 갯수, 노말벡터를 얻어옵니다.





for (int lPolygonIndex = 0; lPolygonIndex < lPolygonCount; lPolygonIndex++)

{

const int lVerticeCount = lMesh->GetPolygonSize(lPolygonIndex);

for (int lVerticeIndex = 0; lVerticeIndex < lVerticeCount; lVerticeIndex++)

{

D3DXVECTOR3 position;

D3DXVECTOR2 texture;

D3DXVECTOR3 normalvector;



auto index = lMesh->GetPolygonVertex(lPolygonIndex, lVerticeIndex);//현재 루프에서의 정점을 구합니다.

position.x = lVertexArray[index].mData[0];

position.y = lVertexArray[index].mData[1];

position.z = lVertexArray[index].mData[2];



FbxVector4 normal = normalEl->GetDirectArray().GetAt(vertexCounter);//노말벡터를 구합니다.

normalvector.x = (float)normal[0];

normalvector.y = (float)normal[1];

normalvector.z = (float)normal[2];



FbxVector2 fbxTexCoord;

FbxStringList UVSetNameList;

for (int l = 0; l < lMesh->GetElementUVCount(); ++l)//텍스쳐 uv를 구합니다.

{

FbxGeometryElementUV* leUV = lMesh->GetElementUV(l);

int lTextureUVIndex = lMesh->GetTextureUVIndex(lPolygonIndex, lVerticeIndex);

fbxTexCoord = leUV->GetDirectArray().GetAt(lTextureUVIndex);

// Convert to floats

texture[0] = static_cast<float>(fbxTexCoord[0]);

texture[1] = static_cast<float>(fbxTexCoord[1]);

}

//위 for문은 하나의 fbx파일이 여려개의 텍스처를 사용할 때 유용합니다.

//저는 연습용이기에 두개가 있어도 하나만 뽑았습니다.


verticies.push_back({ position,texture ,normalvector });//얻은 값들을 구조체 벡터에 넣습니다.

}

}


이렇게 만든 구조체 벡터를 정점버퍼 생성할 때 넘겨주면 됩니다.

'DirectX' 카테고리의 다른 글

셰이더공부 - 정반사광  (0) 2018.02.25
셰이더 공부 - 난반사광  (0) 2018.02.24
셰이더공부 - 텍스쳐 매핑  (0) 2018.02.23
셰이더 공부 - Color Shader  (0) 2018.02.23
DirectX11 - 정점버퍼, 셰이더 생성  (0) 2018.01.24
Comments