Directx11 MilkShape 3d file, load and render problem

Directx11 MilkShape 3d file, load and render problem

Postby oldscruffy66 » 13 Mar 2015, 19:55

I'm new to graphics programming but have made some strides. I can load a MilkShape 3d file but rendering has some issue.
My problem would see that texture coordinates do not seem to line up well or at all.


Attached files are how the model looks when render in my engine. The file was downloaded from http://tf3dm.com/3d-model/bandit-bruiser-1085.html
The file loads fine MilkShape 3D. I suspect issue is how define Vertex and Indice buffers.

The LoadFromFile method is provided by Milkshape. And after extensive debugging seems to work just fine. Then I created loadTextures and initializebuffers.
I can render what seems both textures and in correct locations with exception to the the visible or missing texture.

bool CMS3DFile::LoadFromFile(const char* lpszFileName)
{
FILE *fp = fopen(lpszFileName, "rb");
if (!fp)
return false;

size_t i;
ms3d_header_t header;
fread(&header, 1, sizeof(ms3d_header_t), fp);

if (strncmp(header.id, "MS3D000000", 10) != 0)
return false;

if (header.version != 4)
return false;

// vertices
word nNumVertices;
fread(&nNumVertices, 1, sizeof(word), fp);
_i->arrVertices.resize(nNumVertices);
fread(&_i->arrVertices[0], nNumVertices, sizeof(ms3d_vertex_t), fp);

// triangles
word nNumTriangles;
fread(&nNumTriangles, 1, sizeof(word), fp);
_i->arrTriangles.resize(nNumTriangles);
fread(&_i->arrTriangles[0], nNumTriangles, sizeof(ms3d_triangle_t), fp);

// edges
std::set<unsigned int> setEdgePair;
for (i = 0; i < _i->arrTriangles.size(); i++)
{
word a, b;
a = _i->arrTriangles[i].vertexIndices[0];
b = _i->arrTriangles[i].vertexIndices[1];
if (a > b)
std::swap(a, b);
if (setEdgePair.find(MAKEDWORD(a, b)) == setEdgePair.end())
setEdgePair.insert(MAKEDWORD(a, b));

a = _i->arrTriangles[i].vertexIndices[1];
b = _i->arrTriangles[i].vertexIndices[2];
if (a > b)
std::swap(a, b);
if (setEdgePair.find(MAKEDWORD(a, b)) == setEdgePair.end())
setEdgePair.insert(MAKEDWORD(a, b));

a = _i->arrTriangles[i].vertexIndices[2];
b = _i->arrTriangles[i].vertexIndices[0];
if (a > b)
std::swap(a, b);
if (setEdgePair.find(MAKEDWORD(a, b)) == setEdgePair.end())
setEdgePair.insert(MAKEDWORD(a, b));
}

for(std::set<unsigned int>::iterator it = setEdgePair.begin(); it != setEdgePair.end(); ++it)
{
unsigned int EdgePair = *it;
ms3d_edge_t Edge;
Edge.edgeIndices[0] = (word) EdgePair;
Edge.edgeIndices[1] = (word) ((EdgePair >> 16) & 0xFFFF);
_i->arrEdges.push_back(Edge);
}

// groups
word nNumGroups;
fread(&nNumGroups, 1, sizeof(word), fp);
_i->arrGroups.resize(nNumGroups);
for (i = 0; i < nNumGroups; i++)
{
fread(&_i->arrGroups[i].flags, 1, sizeof(byte), fp);
fread(&_i->arrGroups[i].name, 32, sizeof(char), fp);
fread(&_i->arrGroups[i].numtriangles, 1, sizeof(word), fp);
_i->arrGroups[i].triangleIndices = new word[_i->arrGroups[i].numtriangles];
fread(_i->arrGroups[i].triangleIndices, _i->arrGroups[i].numtriangles, sizeof(word), fp);
fread(&_i->arrGroups[i].materialIndex, 1, sizeof(char), fp);
}

// materials
word nNumMaterials;
fread(&nNumMaterials, 1, sizeof(word), fp);
_i->arrMaterials.resize(nNumMaterials);
fread(&_i->arrMaterials[0], nNumMaterials, sizeof(ms3d_material_t), fp);

fread(&_i->fAnimationFPS, 1, sizeof(float), fp);
fread(&_i->fCurrentTime, 1, sizeof(float), fp);
fread(&_i->iTotalFrames, 1, sizeof(int), fp);

// joints
word nNumJoints;
fread(&nNumJoints, 1, sizeof(word), fp);
_i->arrJoints.resize(nNumJoints);
for (i = 0; i < nNumJoints; i++)
{
fread(&_i->arrJoints[i].flags, 1, sizeof(byte), fp);
fread(&_i->arrJoints[i].name, 32, sizeof(char), fp);
fread(&_i->arrJoints[i].parentName, 32, sizeof(char), fp);
fread(&_i->arrJoints[i].rotation, 3, sizeof(float), fp);
fread(&_i->arrJoints[i].position, 3, sizeof(float), fp);
fread(&_i->arrJoints[i].numKeyFramesRot, 1, sizeof(word), fp);
fread(&_i->arrJoints[i].numKeyFramesTrans, 1, sizeof(word), fp);
_i->arrJoints[i].keyFramesRot = new ms3d_keyframe_rot_t[_i->arrJoints[i].numKeyFramesRot];
_i->arrJoints[i].keyFramesTrans = new ms3d_keyframe_pos_t[_i->arrJoints[i].numKeyFramesTrans];
fread(_i->arrJoints[i].keyFramesRot, _i->arrJoints[i].numKeyFramesRot, sizeof(ms3d_keyframe_rot_t), fp);
fread(_i->arrJoints[i].keyFramesTrans, _i->arrJoints[i].numKeyFramesTrans, sizeof(ms3d_keyframe_pos_t), fp);
}

fclose(fp);

return true;
}
bool CMS3DFile::LoadTextures(ID3D11Device* device)
{
HRESULT result;
WCHAR thestr[128];
WCHAR yyy[400];
LPCWSTR cstr4,cstr5;
ms3d_material_t *ppMaterial;


GetMaterialAt(0, &ppMaterial);
MultiByteToWideChar(0,0,ppMaterial->texture,127,thestr,128);
swprintf_s(yyy, L"../Engine/data/ms3d/bandit_Bruiser/%s", thestr);
cstr4 = yyy;

result = D3DX11CreateShaderResourceViewFromFile(device, cstr4, NULL, NULL, &m_headTexture, NULL);
if(FAILED(result))
{
return false;
}

GetMaterialAt(1, &ppMaterial);
MultiByteToWideChar(0,0,ppMaterial->texture,127,thestr,128);
swprintf_s(yyy, L"../Engine/data/ms3d/bandit_Bruiser/%s", thestr);
cstr5 = yyy;

result = D3DX11CreateShaderResourceViewFromFile(device, cstr5, NULL, NULL, &m_bodyTexture, NULL);
if(FAILED(result))
{
return false;
}


return true;
}


bool CMS3DFile::InitializeBuffers(ID3D11Device* device)
{
int vertexCount;
UINT iCount = 0;

VertexType* vertices;
unsigned long* indices;
HRESULT result;
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, indexData;

vertexCount = GetNumTriangles() * 3;
vertices = new VertexType[vertexCount];

int numGroups = GetNumGroups();
iCount = 0;
for (int x = 0; x < numGroups; x++)
{
ms3d_group_t *group;
GetGroupAt(x,&group);
for (size_t j = 0; j < group->numtriangles; j++)
{
int iTriangleIndex = group->triangleIndices[j];
ms3d_triangle_t *triangle;
GetTriangleAt(iTriangleIndex,&triangle);
for ( int k=0; k<3; k++ )
{
ms3d_vertex_t *lvertex;
GetVertexAt(triangle->vertexIndices[k],&lvertex);
float vx = lvertex->vertex[0];
float vy = lvertex->vertex[1];
float vz = lvertex->vertex[2];// * -1.0f;
vertices[iCount].position = D3DXVECTOR3(vx, vy, vz);;

float u = triangle->s[k];
float v = triangle->t[k];
vertices[iCount].texture = D3DXVECTOR2(u, v);

float nx = triangle->vertexNormals[k][0];
float ny = triangle->vertexNormals[k][1];
float nz = triangle->vertexNormals[k][2];// * -1.0f;
vertices[iCount].normal = D3DXVECTOR3(nx, ny, nz);
++iCount;
}
}
}

iCount = 0;
UINT NumIndices = GetNumTriangles() * 3;
m_IndexCount = NumIndices;
indices = new unsigned long[NumIndices];

for ( int x=0; x<numGroups; x++ )
{
ms3d_group_t *group;
GetGroupAt(x,&group);
for ( int j=0; j<group->numtriangles; j++ )
{
for ( int k=0; k<3; k++ )
{
indices[iCount] = j*3 + k;
++iCount;
}
}
}



// Set up the description of the static vertex buffer.
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType) * vertexCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;

// Give the subresource structure a pointer to the vertex data.
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;

// Now create the vertex buffer.
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
if(FAILED(result))
{
return false;
}

// Set up the description of the static index buffer.
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) * NumIndices;
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;
}

// Release the arrays now that the vertex and index buffers have been created and loaded.
delete [] vertices;
vertices = 0;

delete [] indices;
indices = 0;

return true;
}
Attachments
Engine_Bandit_Bruiser_head.JPG
Engine_Bandit_Bruiser.JPG
oldscruffy66
 
Posts: 4
Joined: 13 Mar 2015, 19:27

Re: Directx11 MilkShape 3d file, load and render problem

Postby mdtrooper » 16 Mar 2015, 13:32

Do you try a opensource editor such as Blender?
And I have a Patreon in https://www.patreon.com/migueldedios.
User avatar
mdtrooper
 
Posts: 185
Joined: 26 Jul 2012, 13:24
Location: Spain

Re: Directx11 MilkShape 3d file, load and render problem

Postby rogerdv » 16 Mar 2015, 16:36

Blender, and a more recent/open format. Try assimp to have a wide range of 3D formats, it also loads the animations.
User avatar
rogerdv
 
Posts: 289
Joined: 10 Dec 2009, 18:26

Re: Directx11 MilkShape 3d file, load and render problem

Postby oldscruffy66 » 18 Mar 2015, 17:36

I found the issue. The ms3d file has 2 groups. I created a second entity to render the second group.
oldscruffy66
 
Posts: 4
Joined: 13 Mar 2015, 19:27

Who is online

Users browsing this forum: No registered users and 1 guest