isu diss

DX11 Can't load UV coordinates correctly from a FBX

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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];

}

Share this post


Link to post
Share on other sites

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
			} 

 

Share this post


Link to post
Share on other sites

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 }

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Announcements

  • Forum Statistics

    • Total Topics
      628370
    • Total Posts
      2982296
  • Similar Content

    • By joeblack
      Hi,
      im reading about specular aliasing because of mip maps, as far as i understood it, you need to compute fetched normal lenght and detect now its changed from unit length. I’m currently using BC5 normal maps, so i reconstruct z in shader and therefore my normals are normalized. Can i still somehow use antialiasing or its not needed? Thanks.
    • By 51mon
      I want to change the sampling behaviour to SampleLevel(coord, ddx(coord.y).xx, ddy(coord.y).xx). I was just wondering if it's possible without explicit shader code, e.g. with some flags or so?
    • By GalacticCrew
      Hello,
      I want to improve the performance of my game (engine) and some of your helped me to make a GPU Profiler. After creating the GPU Profiler, I started to measure the time my GPU needs per frame. I refined my GPU time measurements to find my bottleneck.
      Searching the bottleneck
      Rendering a small scene in an Idle state takes around 15.38 ms per frame. 13.54 ms (88.04%) are spent while rendering the scene, 1.57 ms (10.22%) are spent during the SwapChain.Present call (no VSync!) and the rest is spent on other tasks like rendering the UI. I further investigated the scene rendering, since it takes über 88% of my GPU frame rendering time.
      When rendering my scene, most of the time (80.97%) is spent rendering my models. The rest is spent to render the background/skybox, updating animation data, updating pixel shader constant buffer, etc. It wasn't really suprising that most of the time is spent for my models, so I further refined my measurements to find the actual bottleneck.
      In my example scene, I have five animated NPCs. When rendering these NPCs, most actions are almost for free. Setting the proper shaders in the input layout (0.11%), updating vertex shader constant buffers (0.32%), setting textures (0.24%) and setting vertex and index buffers (0.28%). However, the rest of the GPU time (99.05% !!) is spent in two function calls: DrawIndexed and DrawIndexedInstance.
      I searched this forum and the web for other articles and threads about these functions, but I haven't found a lot of useful information. I use SharpDX and .NET Framework 4.5 to develop my game (engine). The developer of SharpDX said, that "The method DrawIndexed in SharpDX is a direct call to DirectX" (Source). DirectX 11 is widely used and SharpDX is "only" a wrapper for DirectX functions, I assume the problem is in my code.
      How I render my scene
      When rendering my scene, I render one model after another. Each model has one or more parts and one or more positions. For example, a human model has parts like head, hands, legs, torso, etc. and may be placed in different locations (on the couch, on a street, ...). For static elements like furniture, houses, etc. I use instancing, because the positions never change at run-time. Dynamic models like humans and monster don't use instancing, because positions change over time.
      When rendering a model, I use this work-flow:
      Set vertex and pixel shaders, if they need to be updated (e.g. PBR shaders, simple shader, depth info shaders, ...) Set animation data as constant buffer in the vertex shader, if the model is animated Set generic vertex shader constant buffer (world matrix, etc.) Render all parts of the model. For each part: Set diffuse, normal, specular and emissive texture shader views Set vertex buffer Set index buffer Call DrawIndexedInstanced for instanced models and DrawIndexed models What's the problem
      After my GPU profiling, I know that over 99% of the rendering time for a single model is spent in the DrawIndexedInstanced and DrawIndexed function calls. But why do they take so long? Do I have to try to optimize my vertex or pixel shaders? I do not use other types of shaders at the moment. "Le Comte du Merde-fou" suggested in this post to merge regions of vertices to larger vertex buffers to reduce the number of Draw calls. While this makes sense to me, it does not explain why rendering my five (!) animated models takes that much GPU time. To make sure I don't analyse something I wrong, I made sure to not use the D3D11_CREATE_DEVICE_DEBUG flag and to run as Release version in Visual Studio as suggested by Hodgman in this forum thread.
      My engine does its job. Multi-texturing, animation, soft shadowing, instancing, etc. are all implemented, but I need to reduce the GPU load for performance reasons. Each frame takes less than 3ms CPU time by the way. So the problem is on the GPU side, I believe.
    • By noodleBowl
      I was wondering if someone could explain this to me
      I'm working on using the windows WIC apis to load in textures for DirectX 11. I see that sometimes the WIC Pixel Formats do not directly match a DXGI Format that is used in DirectX. I see that in cases like this the original WIC Pixel Format is converted into a WIC Pixel Format that does directly match a DXGI Format. And doing this conversion is easy, but I do not understand the reason behind 2 of the WIC Pixel Formats that are converted based on Microsoft's guide
      I was wondering if someone could tell me why Microsoft's guide on this topic says that GUID_WICPixelFormat40bppCMYKAlpha should be converted into GUID_WICPixelFormat64bppRGBA and why GUID_WICPixelFormat80bppCMYKAlpha should be converted into GUID_WICPixelFormat64bppRGBA
      In one case I would think that: 
      GUID_WICPixelFormat40bppCMYKAlpha would convert to GUID_WICPixelFormat32bppRGBA and that GUID_WICPixelFormat80bppCMYKAlpha would convert to GUID_WICPixelFormat64bppRGBA, because the black channel (k) values would get readded / "swallowed" into into the CMY channels
      In the second case I would think that:
      GUID_WICPixelFormat40bppCMYKAlpha would convert to GUID_WICPixelFormat64bppRGBA and that GUID_WICPixelFormat80bppCMYKAlpha would convert to GUID_WICPixelFormat128bppRGBA, because the black channel (k) bits would get redistributed amongst the remaining 4 channels (CYMA) and those "new bits" added to those channels would fit in the GUID_WICPixelFormat64bppRGBA and GUID_WICPixelFormat128bppRGBA formats. But also seeing as there is no GUID_WICPixelFormat128bppRGBA format this case is kind of null and void
      I basically do not understand why Microsoft says GUID_WICPixelFormat40bppCMYKAlpha and GUID_WICPixelFormat80bppCMYKAlpha should convert to GUID_WICPixelFormat64bppRGBA in the end
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
  • Popular Now