# DX11 Load and render MS3D file problem in DX11

## Recommended Posts

pnt1614    407

I am trying to load and render a MS3D file in DX11 but there is a problem that texture coordinate does not match as well as the object looks like be triangulated. The left image is rendered with DX11, and the other is opened  with Milkshape 3D.

In the source code, I try to convert to LHS such as: index, z coordinate, normal vector, it has not worked.

HRESULT MeshHandler::LoadMS3DFile( std::wstring filename, ID3D11Device* pd3dDevice ) {
#pragma region local variables
// Temporary Milkshape structures
unsigned short usVertexCount = 0;
unsigned short usTriangleCount = 0;
unsigned short usGroupCount = 0;
unsigned short usMaterialCount = 0;

MS3DVertex* pMS3DVertices = NULL;
MS3DTriangle* pMS3DTriangles = NULL;
MS3DGroup* pMS3DGroups = NULL;
MS3DMaterial* pMS3DMaterials = NULL;

std::ifstream fin;
#pragma endregion

fin.open( filename.c_str(),std::ios::binary );
return NULL;

#pragma region Load all the vertices
pMS3DVertices = new MS3DVertex[usVertexCount];
m_NumOfVertices = usVertexCount;
for (int i = 0; i < usVertexCount; i++)
{
}
#pragma endregion

#pragma region Load all the triangle indices
if (usTriangleCount >0)
{
pMS3DTriangles = new MS3DTriangle[usTriangleCount];
if (pMS3DTriangles != NULL)
{
m_NumOfTriangles = usTriangleCount;
for (int i = 0; i < usTriangleCount; i++)
{

}
}
}
#pragma endregion

#pragma region Load all the group information
if (usGroupCount > 0)
{
pMS3DGroups = new MS3DGroup[usGroupCount];
if (pMS3DGroups != NULL)
{
m_NumOfGroups = usGroupCount;
m_pGroups = new Group[m_NumOfGroups];
if (m_pGroups != NULL)
{
for (int i = 0; i < usGroupCount; i++)
{
unsigned short triCount = pMS3DGroups[i].numtriangles;
pMS3DGroups[i].triangleIndices = new unsigned short[triCount];

m_pGroups[i].m_MaterialIndex = pMS3DGroups[i].materialIndex;
m_pGroups[i].m_NumTriangles = pMS3DGroups[i].numtriangles;
m_pGroups[i].m_pTriangleIndices = new unsigned short[triCount];
memcpy(m_pGroups[i].m_pTriangleIndices, pMS3DGroups[i].triangleIndices, sizeof(unsigned short) * triCount );
}
}
}
}
#pragma endregion

#pragma region Load all the material information

if (usMaterialCount > 0)
{
pMS3DMaterials = new MS3DMaterial[usMaterialCount];
m_NumOfMaterials = usMaterialCount;
m_pMaterials = new Material[m_NumOfMaterials];
if (pMS3DMaterials != NULL && m_pMaterials != NULL)
{
for (int i = 0; i < m_NumOfMaterials; i++)
{

memcpy(m_pMaterials[i].m_ambient, pMS3DMaterials[i].ambient, sizeof(float)*4);
memcpy(m_pMaterials[i].m_diffuse, pMS3DMaterials[i].diffuse, sizeof(float)*4);
memcpy(m_pMaterials[i].m_emissive, pMS3DMaterials[i].emissive, sizeof(float)*4);
memcpy(m_pMaterials[i].m_specular, pMS3DMaterials[i].specular, sizeof(float)*4);
m_pMaterials[i].m_shininess = pMS3DMaterials[i].shininess;

m_pMaterials[i].m_filename_length = strlen(pMS3DMaterials[i].texture) + 1;
strcpy(m_pMaterials[i].m_pTextureFilename, pMS3DMaterials[i].texture);
}
}
}
#pragma endregion

// Close the file (remaining file data unused)
fin.close();

//-----------------------------------------------------------------------------------------------------
// Swap the 2nd and the 3rd indices
//-----------------------------------------------------------------------------------------------------
for (int i=0; i<m_NumOfTriangles; i++)
{
unsigned short temp = pMS3DTriangles[i].vertexIndices[1];
pMS3DTriangles[i].vertexIndices[1] = pMS3DTriangles[i].vertexIndices[2];
pMS3DTriangles[i].vertexIndices[2] = temp;
}

//-----------------------------------------------------------------------------------------------------
#pragma region
//Create an array of vertices for generating a new vertex buffer
m_NumOfVertices = m_NumOfTriangles * 3;
m_pRenderVertices = new D3DVERTEX[m_NumOfVertices];
UINT iCount = 0;
for ( int i=0; i<m_NumOfGroups; i++ )
{
m_pGroups[i].m_VertexStart = iCount;
for ( int j=0; j<m_pGroups[i].m_NumTriangles; j++ )
{
int iTriangleIndex = pMS3DGroups[i].triangleIndices[j];
for ( int k=0; k<3; k++ )
{
float vx = pMS3DVertices[pMS3DTriangles[iTriangleIndex].vertexIndices[k]].vertex[0];
float vy = pMS3DVertices[pMS3DTriangles[iTriangleIndex].vertexIndices[k]].vertex[1];
float vz = pMS3DVertices[pMS3DTriangles[iTriangleIndex].vertexIndices[k]].vertex[2] * -1.0f;
m_pRenderVertices[iCount].pos = D3DXVECTOR3(vx, vy, vz);;

float nx = pMS3DTriangles[iTriangleIndex].vertexNormals[k][0];
float ny = pMS3DTriangles[iTriangleIndex].vertexNormals[k][1];
float nz = pMS3DTriangles[iTriangleIndex].vertexNormals[k][2] * -1.0f;
m_pRenderVertices[iCount].normal = D3DXVECTOR3(nx, ny, nz);

float u = pMS3DTriangles[iTriangleIndex].s[k];
float v = pMS3DTriangles[iTriangleIndex].t[k];
m_pRenderVertices[iCount].tex = D3DXVECTOR2(u, v);

++iCount;
}
}
m_pGroups[i].m_NumOfVertices = iCount - m_pGroups[i].m_VertexStart;
}

//-----------------------------------------------------------------------------------------------------
//Create an index array
//Did not test yet.
iCount = 0;
UINT NumOfIndices = m_NumOfTriangles * 3;
m_pRenderIndices = new int[NumOfIndices];
for ( int i=0; i<m_NumOfGroups; i++ )
{
m_pGroups[i].m_IndexStart = iCount;
for ( int j=0; j<m_pGroups[i].m_NumTriangles; j++ )
{
for ( int k=0; k<3; k++ )
{
m_pRenderIndices[iCount] = j*3 + k;
++iCount;
}
}
m_pGroups[i].m_IndexCount = iCount - m_pGroups[i].m_IndexStart;
}
#pragma endregion

//-----------------------------------------------------------------------------------------------------
// Releasing
//-----------------------------------------------------------------------------------------------------
if (pMS3DMaterials != NULL)
{
delete []pMS3DMaterials;
pMS3DMaterials = NULL;
}

if (pMS3DGroups != NULL)
{
for (int i = 0; i < usGroupCount; i++)
{
if (pMS3DGroups[i].triangleIndices != NULL)
{
delete []pMS3DGroups[i].triangleIndices;
pMS3DGroups[i].triangleIndices = NULL;
}
}
delete []pMS3DGroups;
pMS3DGroups = NULL;
}

if (pMS3DTriangles != NULL)
{
delete[] pMS3DTriangles;
pMS3DTriangles = NULL;
}

if (pMS3DVertices != NULL)
{
delete []pMS3DVertices;
pMS3DVertices = NULL;
}

return S_OK;
}

##### Share on other sites
Jason Z    6434

I would offer for you to take a look at my geometry loader class from Hieroglyph 3, but I think you are already aware of it...  Have you verified if there is an issue on all of the vertices, or is it only some of them?  I would recommend to do a frame grab with PIX and then compare the vertex data that you have with what you find in the Milkshape3D editor (IIRC there is a way to look at the raw data via the editor itself).

##### Share on other sites
pnt1614    407

Thank for helping me, Jason Z. I have already looked at you geometry loader class and figured out the reason. We just comment the following lines:

for (int i=0; i<m_NumOfTriangles; i++)
{
unsigned short temp = pMS3DTriangles[i].vertexIndices[1];
pMS3DTriangles[i].vertexIndices[1] = pMS3DTriangles[i].vertexIndices[2];
pMS3DTriangles[i].vertexIndices[2] = temp;
}

and we do not need to inverse z value.

## Create an account

Register a new account

• ### Similar Content

• By isu diss
I'm trying to code Rayleigh part of Nishita's model (Display Method of the Sky Color Taking into Account Multiple Scattering). I get black screen no colors. Can anyone find the issue for me?

• By Endurion
I have a gaming framework with an renderer interface. Those support DX8, DX9 and latest, DX11. Both DX8 and DX9 use fixed function pipeline, while DX11 obviously uses shaders. I've got most of the parts working fine, as in I can switch renderers and notice almost no difference. The most advanced features are 2 directional lights with a single texture
My last problem is lighting; albeit there's documentation on the D3D lighting model I still can't get the behaviour right. My mistake shows most prominently in the dark side opposite the lights. I'm pretty sure the ambient calculation is off, but that one's supposed to be the most simple one and should be hard to get wrong.
Interestingly I've been searching high and low, and have yet to find a resource that shows how to build a HLSL shader where diffuse, ambient and specular are used together with material properties. I've got various shaders for all the variations I'm supporting. I stepped through the shader with the graphics debugger, but the calculation seems to do what I want. I'm just not sure the formula is correct.
This one should suffice though, it's doing two directional lights, texture modulated with vertex color and a normal. Maybe someone can spot one (or more mistakes). And yes, this is in the vertex shader and I'm aware lighting will be as "bad" as in fixed function; that's my goal currently.
• By Mercesa
Hey folks. So I'm having this problem in which if my camera is close to a surface, the SSAO pass suddenly spikes up to around taking 16 milliseconds.
When still looking towards the same surface, but less close. The framerate resolves itself and becomes regular again.
This happens with ANY surface of my model, I am a bit clueless in regards to what could cause this. Any ideas?
In attached image: y axis is time in ms, x axis is current frame. The dips in SSAO milliseconds are when I moved away from the surface, the peaks happen when I am very close to the surface.

Edit: So I've done some more in-depth profiling with Nvidia nsight. So these are the facts from my results
Count of command buffers goes from 4 (far away from surface) to ~20(close to surface).
The command buffer duration in % goes from around ~30% to ~99%
Sometimes the CPU duration takes up to 0.03 to 0.016 milliseconds per frame while comparatively usually it takes around 0.002 milliseconds.
I am using a vertex shader which generates my full-screen quad and afterwards I do my SSAO calculations in my pixel shader, could this be a GPU driver bug? I'm a bit lost myself. It seems there could be a CPU/GPU resource stall. But why would the amount of command buffers be variable depending on distance from a surface?

Edit n2: Any resolution above 720p starts to have this issue, and I am fairly certain my SSAO is not that performance heavy it would crap itself at a bit higher resolutions.

• In DirectX 11 we have a 24 bit integer depth + 8bit stencil format for depth-stencil resources ( DXGI_FORMAT_D24_UNORM_S8_UINT ). However, in an AMD GPU documentation for consoles I have seen they mentioned, that internally this format is implemented as a 64 bit resource with 32 bits for depth (but just truncated for 24 bits) and 32 bits for stencil (truncated to 8 bits). AMD recommends using a 32 bit floating point depth buffer instead with 8 bit stencil which is this format: DXGI_FORMAT_D32_FLOAT_S8X24_UINT.
Does anyone know why this is? What is the usual way of doing this, just follow the recommendation and use a 64 bit depthstencil? Are there performance considerations or is it just recommended to not waste memory? What about Nvidia and Intel, is using a 24 bit depthbuffer relevant on their hardware?
Cheers!

• By gsc
Hi! I am trying to implement simple SSAO postprocess. The main source of my knowledge on this topic is that awesome tutorial.
But unfortunately something doesn't work... And after a few long hours I need some help. Here is my hlsl shader:
float3 randVec = _noise * 2.0f - 1.0f; // noise: vec: {[0;1], [0;1], 0} float3 tangent = normalize(randVec - normalVS * dot(randVec, normalVS)); float3 bitangent = cross(tangent, normalVS); float3x3 TBN = float3x3(tangent, bitangent, normalVS); float occlusion = 0.0; for (int i = 0; i < kernelSize; ++i) { float3 samplePos = samples[i].xyz; // samples: {[-1;1], [-1;1], [0;1]} samplePos = mul(samplePos, TBN); samplePos = positionVS.xyz + samplePos * ssaoRadius; float4 offset = float4(samplePos, 1.0f); offset = mul(offset, projectionMatrix); offset.xy /= offset.w; offset.y = -offset.y; offset.xy = offset.xy * 0.5f + 0.5f; float sampleDepth = tex_4.Sample(textureSampler, offset.xy).a; sampleDepth = vsPosFromDepth(sampleDepth, offset.xy).z; const float threshold = 0.025f; float rangeCheck = abs(positionVS.z - sampleDepth) < ssaoRadius ? 1.0 : 0.0; occlusion += (sampleDepth <= samplePos.z + threshold ? 1.0 : 0.0) * rangeCheck; } occlusion = saturate(1 - (occlusion / kernelSize)); And current result: http://imgur.com/UX2X1fc
I will really appreciate for any advice!

• 17
• 15
• 17
• 10
• 18