Can't load UV coordinates correctly from a FBX

Started by
6 comments, last by xycsoscyx 6 years, 5 months ago

This works for planes, not for solid objects

 


FbxLayerElementArrayTemplate<FbxVector2>* uvVertices = NULL;

Mesh->GetTextureUV(&uvVertices);

MyUV = new XMFLOAT2[uvVertices->GetCount()];
for (int i = 0; i < Mesh->GetPolygonCount(); i++)//polygon(=mostly rectangle) count
{
	  for (int j = 0; j < Mesh->GetPolygonSize(i); j++)//retrieves number of vertices in a polygon
	  {
		 FbxVector2 uv = uvVertices->GetAt(Mesh->GetTextureUVIndex(i, j));
		 MyUV[Mesh->GetTextureUVIndex(i, j)] = XMFLOAT2((float)uv.mData[0], (float)uv.mData[1]);
      }
}

I can load the 3D model but I can't wrap up a texture. How do I duplicate vertices easily? Pls help.

Advertisement

Use the indices from the fbx to create an index buffer, you shouldn't have to duplicate any vertices in direct x. This is from Rastertek tutorials http://www.rastertek.com/dx11tut07.html , I left out the irrelevant parts:


	unsigned long* indices;
	D3D11_BUFFER_DESC indexBufferDesc;
	D3D11_SUBRESOURCE_DATA indexData;

	// Create the index array.
	indices = new unsigned long[m_indexCount];
	if(!indices)
	{
		return false;
	}

	// Load the vertex array and index array with data.
	// ...
      
	// Set up the description of the static index buffer.
	indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
	indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
	indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
	indexBufferDesc.CPUAccessFlags = 0;
	indexBufferDesc.MiscFlags = 0;
	indexBufferDesc.StructureByteStride = 0;

	// Give the subresource structure a pointer to the index data.
	indexData.pSysMem = indices;
	indexData.SysMemPitch = 0;
	indexData.SysMemSlicePitch = 0;

	// Create the index buffer.
	result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
	if(FAILED(result))
	{
		return false;
	}

	delete [] indices;
	indices = 0;

If this doesn't work or you've already done this, then use this pseudo code, but try the above first:

uv = new XMFLOAT2[numIndices];

for (int index = 0; index < numIndices; ++index)

{

    uv[index] = filesUV[index];

}

Thanks. I thought nobody was gonna answer my question. I'm talking about uv texture coordinates. As you can see from the file, cube.fbx, there are 8 vertices and 12 uvs, I should have 8 vertices and 8 uvs in order to load in direct3d. As fellow gamedev people told me, what I should do is duplicate vertices. I have no idea how to do that easily.



	Geometry: 964450576, "Geometry::", "Mesh" {
		Vertices: *24 {
			a: -0.5,-0.5,0.500000059604645,0.5,-0.5,0.500000059604645,-0.5,0.5,0.500000059604645,0.5,0.5,0.500000059604645,-0.5,0.5,-0.499999940395355,0.5,0.5,-0.499999940395355,-0.5,-0.5,-0.499999940395355,0.5,-0.5,-0.499999940395355
		} 
		PolygonVertexIndex: *30 {
			a: 0,1,-3,2,1,-4,2,3,-5,4,3,-6,4,5,-7,6,5,-8,1,7,-4,3,7,-6,6,0,-5,4,0,-3
		} 
		Edges: *17 {
			a: 0,1,2,4,5,7,8,10,11,13,14,16,17,18,19,24,25
		} 
		GeometryVersion: 124
		LayerElementNormal: 0 {
			Version: 101
			Name: ""
			MappingInformationType: "ByPolygonVertex"
			ReferenceInformationType: "Direct"
			Normals: *90 {
				a: 0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0
			} 
		}
		LayerElementUV: 0 {
			Version: 101
			Name: "map1"
			MappingInformationType: "ByPolygonVertex"
			ReferenceInformationType: "IndexToDirect"
			UV: *24 {
				a: 0.331844329833984,0.000959634780883789,0.665735721588135,0.000959277153015137,0.665736198425293,0.334850549697876,0.331844806671143,0.334850788116455,-0.00204682350158691,0.000959992408752441,0.999627113342285,0.334850549697876,0.665736436843872,0.668741941452026,0.331845045089722,0.668741941452026,0.999627113342285,0.000959277153015137,-0.00204646587371826,0.334851264953613,0.665736198425293,1.00263357162476,0.331844806671143,1.00263357162476
			} 
			UVIndex: *30 {
				a: 0,1,3,3,1,2,3,2,7,7,2,6,7,6,11,11,6,10,1,8,2,2,8,5,4,0,9,9,0,3
			} 

 

In other files the indices are grouped together (vertexIndex normalIndex uvIndex and then it repeats). I've never worked with fbx, but if each vertex index goes with the uv index in order then since they both have 30 indices you don't need more vertices. You just need the same number of indices per vertex, uv, and normal (if it wasn't using the values directly).

I'm not sure why this has negative indices, but indices should be positive, maybe fbx has a specific reason for this.

48 minutes ago, isu diss said:

PolygonVertexIndex: *30 { a: 0,1,-3,2,1,-4,2,3,-5,4,3,-6,4,5,-7,6,5,-8,1,7,-4,3,7,-6,6,0,-5,4,0,-3 }

 

Quick search shows this: https://stackoverflow.com/questions/7736845/can-anyone-explain-the-fbx-format-for-me

Note the explanation for PolygonVertexIndex, negative values denote the end of the polygon, the actual index being the absolute of the value minus one.  You should always have the same number of indices between vertices and uv coordinates, and your normals should always be your index count times 3.

Beyond that, what are you using to actually load the FBX file?  Have you considered using something line Assimp to load the format for you (along with a range of other formats)?  The end result of the Assimp scene/mesh is a lot easier to work with, and can handle auto generation of things like normals, uv coordinates, and auto triangulation of polygons.

Quote

Beyond that, what are you using to actually load the FBX file?  Have you considered using something line Assimp to load the format for you (along with a range of other formats)?  The end result of the Assimp scene/mesh is a lot easier to work with, and can handle auto generation of things like normals, uv coordinates, and auto triangulation of polygons

I use FBX SDK to load fbx files.

I haven't looked at it extensively, but it seems to be more about loading content into FBX files and working with them, instead of loading the data for rendering?  Triangulation and mesh optimization are useful parts of Assimp, and it loads it into a (more straightforward?) structure that makes it easy to handle and put into vertex/index buffers.

This topic is closed to new replies.

Advertisement