Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


assimp subsets problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 newtechnology   Members   -  Reputation: 783

Like
0Likes
Like

Posted 01 February 2014 - 07:20 AM

I'm trying to add texture loading support in my model loader but i've no idea what to do now (subset problem.)

 

#include "stdafx.h"
 
DirectionalLight DirLights[3];
Material Mat;
 
Model::Model()
{
 
DirLights[0].Ambient  = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f);
DirLights[0].Diffuse  = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
DirLights[0].Specular = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
DirLights[0].Direction = XMFLOAT3(0.0f, 0.0f, 0.0f);
 
DirLights[1].Ambient  = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f);
DirLights[1].Diffuse  = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
DirLights[1].Specular = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
DirLights[1].Direction = XMFLOAT3(0.0f, 0.0f, 0.0f);
 
DirLights[2].Ambient  = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f);
DirLights[2].Diffuse  = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
DirLights[2].Specular = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
DirLights[2].Direction = XMFLOAT3(0.0f, 0.0f, 0.0f);
 
Mat.Ambient  = XMFLOAT4(0.48f, 0.77f, 0.46f, 1.0f);
Mat.Diffuse  = XMFLOAT4(0.29f, 1.0f, 0.24f, 1.0f);
Mat.Specular = XMFLOAT4(0.2f, 0.2f, 0.2f, 16.0f);
 
}
 
Model::~Model()
{
 
}
 
UINT* AttributeBuffer;
 
void Model::LoadModel(const std::string& filename)
{
 
 
 
Assimp::Importer imp;
 
const aiScene* pScene = imp.ReadFile(filename,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_GenSmoothNormals |
aiProcess_SplitLargeMeshes |
aiProcess_ConvertToLeftHanded |
aiProcess_SortByPType |
aiProcess_PreTransformVertices);
 
if (pScene == NULL)
ShowError(imp.GetErrorString());
 
std::vector<Vertex::Basic32> vertices;
std::vector<USHORT> indices;
 
 
aiMesh** meshes = pScene->mMeshes;
aiMaterial** mats = pScene->mMaterials;
 
UINT* meshID;
UINT matID;
 
aiNode* sceneRoot = pScene->mRootNode;
aiNode** children = sceneRoot->mChildren;
aiNode* child;
 
 
UINT i = 0;
bool rootNode = true;
 
while(i < sceneRoot->mNumChildren)
{
if(rootNode)
{
child = sceneRoot;
rootNode = false;
} else
{
child = children[i];
i++;
}
 
if(!(child->mNumMeshes > 0))
continue;
 
meshID = child->mMeshes;
 
mModel.mSubsetCount += child->mNumMeshes;
 
for(UINT x = 0; x < child->mNumMeshes; x++)
{
mModel.mNumVertices += meshes[meshID[x]]->mNumVertices;
mModel.mNumFaces += meshes[meshID[x]]->mNumFaces;
}
 
AttributeBuffer = new UINT[mModel.mNumFaces];
 
UINT nextVertex = 0;
UINT nextIndex = 0;
 
for(UINT x = 0; x < child->mNumMeshes; x++)
{
UINT meshStartIndex = nextVertex;
 
for(UINT  j = 0; j < meshes[meshID[x]]->mNumVertices; j++)
{
vertices[nextVertex].Pos.x = meshes[meshID[x]]->mVertices[j].x;
vertices[nextVertex].Pos.y = meshes[meshID[x]]->mVertices[j].y;
vertices[nextVertex].Pos.z = meshes[meshID[x]]->mVertices[j].z;
 
vertices[nextVertex].Normal.x = meshes[meshID[x]]->mNormals[j].x;
vertices[nextVertex].Normal.y = meshes[meshID[x]]->mNormals[j].y;
vertices[nextVertex].Normal.z = meshes[meshID[x]]->mNormals[j].z;
 
vertices[nextVertex].Tex.x = meshes[meshID[x]]->mTextureCoords[0][j].x;
vertices[nextVertex].Tex.y = meshes[meshID[x]]->mTextureCoords[0][j].y;
 
nextVertex++;
}
 
for(UINT j = 0; j < meshes[meshID[x]]->mNumFaces; j++)
{
UINT n = 0;
if(x != 0)
{
n =  j + meshes[meshID[x-1]]->mNumFaces;
} else
{
n = j;
}
 
indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[0] + meshStartIndex;
indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[1] + meshStartIndex;
indices[nextIndex++] = meshes[meshID[x]]->mFaces[j].mIndices[2] + meshStartIndex;
AttributeBuffer[n] = meshes[meshID[x]]->mMaterialIndex;
}
}
 
}
 
 
for (UINT j = 0; j < pScene->mNumMaterials; j++)
{
const aiMaterial* mat = pScene->mMaterials[j];
aiString texPath;
 
if (mat->GetTextureCount(aiTextureType_DIFFUSE) > 0 && mat->GetTexture(aiTextureType_DIFFUSE, 0, &texPath) == AI_SUCCESS)
{
char path[260];
sprintf(path, "Resources\\Textures\\%s", texPath.C_Str());
 
ID3D11ShaderResourceView* srv = Mgr.CreateTexture(path);
 
mModel.TextureSRV.push_back(srv);
}
}
  
std::vector<MeshGeometry::Subset> subsets(mModel.mSubsetCount);
 
for (int j = 0; j < child->mNumMeshes; j++)
{
aiMesh* mesh = meshes[meshID[j]];
 
}
 
 
 
 
XNA::ComputeBoundingAxisAlignedBoxFromPoints(&mModel.box, mModel.mNumVertices, &vertices[0].Pos, sizeof(Vertex::Basic32));
 
 
mModel.Mesh.SetSubsetTable(subsets);
mModel.Mesh.SetVertices(&vertices[0], mModel.mNumVertices);
mModel.Mesh.SetIndices(&indices[0], mModel.mNumFaces * 3);
 
}
 
bool Model::IntersectAABBFrustum(XNA::AxisAlignedBox& box, CXMMATRIX world)
{
XMVECTOR detView = XMMatrixDeterminant(d3d->m_Cam.View());
XMMATRIX invView = XMMatrixInverse(&detView, d3d->m_Cam.View());
 
XMMATRIX invWorld = XMMatrixInverse(&XMMatrixDeterminant(world), world);
 
XMMATRIX toLocal = XMMatrixMultiply(invView, invWorld);
 
XMVECTOR scale;
XMVECTOR rotQuat;
XMVECTOR translation;
XMMatrixDecompose(&scale, &rotQuat, &translation, toLocal);
 
XNA::Frustum localspaceFrustum;
XNA::TransformFrustum(&localspaceFrustum, &d3d->m_Frustum, XMVectorGetX(scale), rotQuat, translation);
 
    if (XNA::IntersectAxisAlignedBoxFrustum(&box, &localspaceFrustum) != 0)
return true;
 
return false;
}
 
void Model::Rotate(FLOAT X, FLOAT Y, FLOAT Z)
{
static bool x, y, z;
 
x = true;
y = true;
z = true;
 
if (X == 0)
x = false;
if (Y == 0)
y = false;
if (Z == 0)
z = false;
 
if (x)
mModel.World = XMMatrixRotationX(X);
if (y)
mModel.World = XMMatrixRotationY(Y);
if (z)
mModel.World = XMMatrixRotationZ(Z);
}
 
void Model::Scale(FLOAT X, FLOAT Y, FLOAT Z)
{
mModel.World = XMMatrixScaling(X, Y, Z);
}
 
void Model::Translate(FLOAT X, FLOAT Y, FLOAT Z)
{
mModel.World = XMMatrixTranslation(X, Y, Z); 
}
 
void Model::SetIdentifyMatrix()
{
mModel.World = XMMatrixIdentity();
}
 
void Model::Render()
{
if (!IntersectAABBFrustum(mModel.box, mModel.World))
return;
 
 
UINT Stride = sizeof(Vertex::Basic32);
UINT Offset = 0;
 
Effects::BasicFX->SetDirLights(DirLights);
Effects::BasicFX->SetEyePosW(d3d->m_Cam.GetPosition());
 
ID3DX11EffectTechnique* activeTech = Effects::BasicFX->Light2Tech;
 
    D3DX11_TECHNIQUE_DESC techDesc;
    activeTech->GetDesc(&techDesc);
 
XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(mModel.World);
XMMATRIX worldViewProj = mModel.World * d3d->m_Cam.ViewProj();
 
Effects::BasicFX->SetWorld(mModel.World);
Effects::BasicFX->SetWorldInvTranspose(worldInvTranspose);
Effects::BasicFX->SetWorldViewProj(worldViewProj);
   
    for(UINT p = 0; p < techDesc.Passes; ++p)
    {
  activeTech->GetPassByIndex(p)->Apply(0, pDeviceContext);
  
  for (UINT s = 0; s < mModel.mSubsetCount; s++)
  {    
   Effects::BasicFX->SetMaterial(mModel.Materials[s]);
   Effects::BasicFX->SetDiffuseMap(mModel.TextureSRV[s]);
   
mModel.Mesh.Draw(s);
  }
 
 
}
}
 
 
MeshGeometry::MeshGeometry()
{
VertexBuffer = nullptr;
IndexBuffer = nullptr;
 
IndexBufferFormat = DXGI_FORMAT_R16_UINT;
VertexStride = 0;
}
 
MeshGeometry::~MeshGeometry()
{
ReleaseCOM(VertexBuffer);
ReleaseCOM(IndexBuffer);
}
 
void MeshGeometry::SetIndices(const USHORT* indices, UINT count)
{
D3D11_BUFFER_DESC ibd;
    ibd.Usage = D3D11_USAGE_IMMUTABLE;
    ibd.ByteWidth = sizeof(USHORT) * count;
    ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    ibd.CPUAccessFlags = 0;
    ibd.MiscFlags = 0;
ibd.StructureByteStride = 0;
 
    D3D11_SUBRESOURCE_DATA iinitData;
    iinitData.pSysMem = indices;
 
HR(pDevice->CreateBuffer(&ibd, &iinitData, &IndexBuffer));
}
 
void MeshGeometry::SetSubsetTable(std::vector<Subset>& subsetTable)
{
SubsetTable = subsetTable;
}
 
void MeshGeometry::Draw(UINT subsetId)
{
    UINT offset = 0;
 
pDeviceContext->IASetVertexBuffers(0, 1, &VertexBuffer, &VertexStride, &offset);
pDeviceContext->IASetIndexBuffer(IndexBuffer, IndexBufferFormat, 0);
 
pDeviceContext->DrawIndexed(SubsetTable[subsetId].FaceCount * 3, SubsetTable[subsetId].FaceStart * 3,  0);
}
 
template <typename VertexType>
void MeshGeometry::SetVertices(const VertexType* vertices, UINT count)
{
ReleaseCOM(VertexBuffer);
 
VertexStride = sizeof(VertexType);
 
D3D11_BUFFER_DESC vbd;
    vbd.Usage = D3D11_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(VertexType) * count;
    vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = 0;
    vbd.MiscFlags = 0;
vbd.StructureByteStride = 0;
 
    D3D11_SUBRESOURCE_DATA vinitData;
    vinitData.pSysMem = vertices;
 
HR(pDevice->CreateBuffer(&vbd, &vinitData, &VertexBuffer));
}


Sponsor:

#2 beans222   Members   -  Reputation: 1137

Like
0Likes
Like

Posted 01 February 2014 - 12:09 PM

I'm not sure what the error is (please be more specific), but I have noticed a few things:

 

1. If you are using PreTransformVertices flag, you don't need any scene nodes to render the meshes. You can just use the array of meshes

in aiScene.

 

2. The materials are for the whole mesh subset, not per-face. AttributeBuffer should have a material index for each subset.

 

3. When rendering look up the material by the index stored for that subset.


New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

 


#3 newtechnology   Members   -  Reputation: 783

Like
0Likes
Like

Posted 02 February 2014 - 02:31 AM

i need to get vertex start, vertex count, face count, face start info for subsets.

e.g first 800 triangles should be rendered with material 0 (with reference to x to x vertices)

then rest of them should be rendered with material 1        (^^^^^)

 

any idea how to get subsets of mesh and then get material for them?


Edited by newtechnology, 02 February 2014 - 03:20 AM.


#4 beans222   Members   -  Reputation: 1137

Like
0Likes
Like

Posted 02 February 2014 - 10:36 PM

Just define a structure that has those 4 ints, have have a std::vector of that struct, one for each subset.

Build that vector in another loop through the meshes, using a running total of vertices/faces/indices (which ever you need per-subset to render).


New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

 


#5 newtechnology   Members   -  Reputation: 783

Like
0Likes
Like

Posted 03 February 2014 - 06:19 AM

I think i fixed rest of issues but now i've another issue,

the subset 0 of my model gets correctly rendered with correct material and texture but when subset 1 gets rendered, it gets rendered with both subset 0 and subset 1 and with material and texture of subset 1, basically it affects subset 0's texture and material too.

subset 0 of every mesh works perfect while subset 1 of every mesh is messed up! dunno why

here is my code

#include "stdafx.h"
 
DirectionalLight DirLights[3];
 
Model::Model()
{
DirLights[0].Ambient  = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f);
DirLights[0].Diffuse  = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
DirLights[0].Specular = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
DirLights[0].Direction = XMFLOAT3(0.707f, -0.707f, 0.0f);
 
DirLights[1].Ambient  = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f);
DirLights[1].Diffuse  = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
DirLights[1].Specular = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
DirLights[1].Direction = XMFLOAT3(-0.707f, 0.0f, 0.707f);
 
}
 
Model::~Model()
{
for (UINT i = 0; i < DiffuseMapSRV.size(); i++)
ReleaseCOM(DiffuseMapSRV[i]);
}
 
 
void Model::LoadModel(const std::string& filename)
{
 
Assimp::Importer imp;
 
const aiScene* pScene = imp.ReadFile(filename,
aiProcess_CalcTangentSpace |
        aiProcess_Triangulate |
        aiProcess_GenSmoothNormals |
        aiProcess_SplitLargeMeshes |
        aiProcess_ConvertToLeftHanded |
        aiProcess_SortByPType |
        aiProcess_PreTransformVertices);
 
if (pScene == NULL)
ShowError(imp.GetErrorString());
 
std::vector<USHORT> indices;
std::vector<MeshGeometry::Subset> subsets;
std::vector<Vertex::Basic32> vertices;
 
for (UINT i = 0; i < pScene->mNumMeshes; ++i)
{
aiMesh* mesh = pScene->mMeshes[i];
 
MeshGeometry::Subset subset;
 
subset.VertexCount = mesh->mNumVertices;
subset.VertexStart = vertices.size();
subset.FaceStart = indices.size() / 3;
subset.FaceCount = mesh->mNumFaces;
subset.ID = mesh->mMaterialIndex;
 
subsets.push_back(subset);
 
for (UINT j = 0; j < mesh->mNumVertices; ++j)
{
Vertex::Basic32 vertex;
 
vertex.Pos.x = mesh->mVertices[j].x;
vertex.Pos.y = mesh->mVertices[j].y;
vertex.Pos.z = mesh->mVertices[j].z;
 
vertex.Normal.x = mesh->mNormals[j].x;
vertex.Normal.y = mesh->mNormals[j].y;
vertex.Normal.z = mesh->mNormals[j].z;
 
vertex.Tex.x = mesh->mTextureCoords[0][j].x;
vertex.Tex.y =  mesh->mTextureCoords[0][j].y;
 
 
vertices.push_back(vertex);
}
 
for (UINT j = 0; j < mesh->mNumFaces; ++j)
{
for (UINT index = 0; index < mesh->mFaces[j].mNumIndices; ++index)
{
indices.push_back(mesh->mFaces[j].mIndices[index]);
}
}
 
aiMaterial* Mat = pScene->mMaterials[mesh->mMaterialIndex];
 
Material tempMat;
 
aiColor4D color(0.0f, 0.0f, 0.0f, 0.0f);
 
tempMat.Ambient = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f);
tempMat.Diffuse = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f);
tempMat.Specular = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f);
tempMat.Reflect = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f);
 
Mat->Get(AI_MATKEY_COLOR_AMBIENT, color);
 
tempMat.Ambient = XMFLOAT4(color.r, color.g, color.b, color.a);
 
Mat->Get(AI_MATKEY_COLOR_DIFFUSE, color);
 
tempMat.Diffuse = XMFLOAT4(color.r, color.g, color.b, color.a);
 
Mat->Get(AI_MATKEY_COLOR_SPECULAR, color);
 
tempMat.Specular = XMFLOAT4(color.r, color.g, color.b, color.a);
 
Mat->Get(AI_MATKEY_COLOR_REFLECTIVE, color);
 
tempMat.Reflect = XMFLOAT4(color.r, color.g, color.b, color.a);
 
if (tempMat.Ambient.x == 0 && tempMat.Ambient.y == 0 && tempMat.Ambient.z == 0 && tempMat.Ambient.w == 0)
tempMat.Ambient = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
 
if (tempMat.Diffuse.x == 0 && tempMat.Diffuse.y == 0 && tempMat.Diffuse.z == 0 && tempMat.Diffuse.w == 0)
tempMat.Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
 
if (tempMat.Specular.x == 0 && tempMat.Specular.y == 0 && tempMat.Specular.z == 0 && tempMat.Specular.w == 0)
tempMat.Specular = XMFLOAT4(0.6f, 0.6f, 0.6f, 16.0f);
 
   // if (tempMat.Reflect.x == 0 && tempMat.Reflect.y == 0 && tempMat.Reflect.z == 0 && tempMat.Reflect.w == 0)
//tempMat.Reflect = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
 
Materials.push_back(tempMat);
 
aiString path;
 
if (Mat->GetTextureCount(aiTextureType_DIFFUSE) > 0 && Mat->GetTexture(aiTextureType_DIFFUSE, 0, &path) == AI_SUCCESS)
{
char cpath[260];
 
sprintf(cpath, "Resources\\Textures\\%s", path.C_Str());
 
ID3D11ShaderResourceView* srv = 0;
 
HR(D3DX11CreateShaderResourceViewFromFileA(pDevice, cpath, 0, 0, &srv, 0));
 
DiffuseMapSRV.push_back(srv);
}
 
}
 
 
mModel.mSubsetCount = subsets.size();
mModel.mNumVertices = vertices.size();
mModel.mNumFaces = indices.size() / 3;
 
XNA::ComputeBoundingAxisAlignedBoxFromPoints(&mModel.box, mModel.mNumVertices, &vertices[0].Pos, sizeof(Vertex::Basic32));
 
mModel.Mesh.SetSubsetTable(subsets);
mModel.Mesh.SetVertices(&vertices[0], mModel.mNumVertices);
mModel.Mesh.SetIndices(&indices[0], mModel.mNumFaces * 3);
 
}
 
bool Model::IntersectAABBFrustum(XNA::AxisAlignedBox& box, CXMMATRIX world)
{
XMVECTOR detView = XMMatrixDeterminant(d3d->m_Cam.View());
XMMATRIX invView = XMMatrixInverse(&detView, d3d->m_Cam.View());
 
XMMATRIX invWorld = XMMatrixInverse(&XMMatrixDeterminant(world), world);
 
XMMATRIX toLocal = XMMatrixMultiply(invView, invWorld);
 
XMVECTOR scale;
XMVECTOR rotQuat;
XMVECTOR translation;
XMMatrixDecompose(&scale, &rotQuat, &translation, toLocal);
 
XNA::Frustum localspaceFrustum;
XNA::TransformFrustum(&localspaceFrustum, &d3d->m_Frustum, XMVectorGetX(scale), rotQuat, translation);
 
    if (XNA::IntersectAxisAlignedBoxFrustum(&box, &localspaceFrustum) != 0)
return true;
 
return false;
}
 
 
void Model::RotateX(FLOAT AngleX)
{
mModel.World = XMMatrixRotationX(AngleX);
}
 
void Model::RotateY(FLOAT AngleY)
{
mModel.World = XMMatrixRotationY(AngleY);
}
 
void Model::RotateZ(FLOAT AngleZ)
{
mModel.World = XMMatrixRotationZ(AngleZ);
}
 
void Model::Scale(FLOAT X, FLOAT Y, FLOAT Z)
{
mModel.World = XMMatrixScaling(X, Y, Z);
}
 
void Model::Translate(FLOAT X, FLOAT Y, FLOAT Z)
{
mModel.World = XMMatrixTranslation(X, Y, Z); 
}
 
void Model::Translate(XMFLOAT3& t)
{
mModel.World = XMMatrixTranslation(t.x, t.y, t.z);
}
 
void Model::Scale(XMFLOAT3& s)
{
mModel.World = XMMatrixScaling(s.x, s.y, s.z);
}
 
void Model::SetIdentifyMatrix()
{
mModel.World = XMMatrixIdentity();
}
 
void Model::Render()
{
if (!IntersectAABBFrustum(mModel.box, mModel.World))
return;
 
 
UINT Stride = sizeof(Vertex::Basic32);
UINT Offset = 0;
   
Effects::BasicFX->SetDirLights(DirLights);
Effects::BasicFX->SetEyePosW(d3d->m_Cam.GetPosition());
 
ID3DX11EffectTechnique* activeTech = Effects::BasicFX->Light2TexTech;
 
XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(mModel.World);
XMMATRIX worldViewProj = mModel.World * d3d->m_Cam.View() * d3d->m_Cam.Proj();
 
Effects::BasicFX->SetWorld(mModel.World);
Effects::BasicFX->SetWorldInvTranspose(worldInvTranspose);
Effects::BasicFX->SetWorldViewProj(worldViewProj);
Effects::BasicFX->SetTexTransform(XMMatrixIdentity());
 
 
    D3DX11_TECHNIQUE_DESC techDesc;
    activeTech->GetDesc(&techDesc);
 
    for(UINT p = 0; p < techDesc.Passes; ++p)
    {
  for (UINT i = 0; i < mModel.mSubsetCount; i++)
  {   
  pDeviceContext->RSSetState(0);
 
  Effects::BasicFX->SetMaterial(Materials[i]);
  Effects::BasicFX->SetDiffuseMap(DiffuseMapSRV[i]); 
 
  activeTech->GetPassByIndex(p)->Apply(0, pDeviceContext);
 
  mModel.Mesh.Draw(i);
   }
}
}
 
 
MeshGeometry::MeshGeometry()
{
VertexBuffer = nullptr;
IndexBuffer = nullptr;
 
IndexBufferFormat = DXGI_FORMAT_R16_UINT;
VertexStride = 0;
}
 
MeshGeometry::~MeshGeometry()
{
ReleaseCOM(VertexBuffer);
ReleaseCOM(IndexBuffer);
}
 
void MeshGeometry::SetIndices(const USHORT* indices, UINT count)
{
D3D11_BUFFER_DESC ibd;
    ibd.Usage = D3D11_USAGE_IMMUTABLE;
    ibd.ByteWidth = sizeof(USHORT) * count;
    ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    ibd.CPUAccessFlags = 0;
    ibd.MiscFlags = 0;
ibd.StructureByteStride = 0;
 
    D3D11_SUBRESOURCE_DATA iinitData;
    iinitData.pSysMem = indices;
 
HR(pDevice->CreateBuffer(&ibd, &iinitData, &IndexBuffer));
}
 
void MeshGeometry::SetSubsetTable(std::vector<Subset>& subsetTable)
{
SubsetTable = subsetTable;
}
 
void MeshGeometry::Draw(UINT subsetId)
{
    UINT offset = 0;
 
pDeviceContext->IASetVertexBuffers(0, 1, &VertexBuffer, &VertexStride, &offset);
pDeviceContext->IASetIndexBuffer(IndexBuffer, IndexBufferFormat, 0);
 
pDeviceContext->DrawIndexed(SubsetTable[subsetId].FaceCount * 3, SubsetTable[subsetId].FaceStart * 3,  0);
}
 
template <typename VertexType>
void MeshGeometry::SetVertices(const VertexType* vertices, UINT count)
{
ReleaseCOM(VertexBuffer);
 
VertexStride = sizeof(VertexType);
 
D3D11_BUFFER_DESC vbd;
    vbd.Usage = D3D11_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(VertexType) * count;
    vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = 0;
    vbd.MiscFlags = 0;
vbd.StructureByteStride = 0;
 
    D3D11_SUBRESOURCE_DATA vinitData;
    vinitData.pSysMem = vertices;
 
HR(pDevice->CreateBuffer(&vbd, &vinitData, &VertexBuffer));
}

how it should get rendered:

1795557_824034384289584_1120338073_n.jpg

how it gets rendered:

1174959_824034420956247_1281305822_n.jpg

when only rendered with subset 0:

T3WhYBS.png

when only rendered with subset 1:

BMRn1Di.png


Edited by newtechnology, 03 February 2014 - 08:24 AM.


#6 Krohm   Crossbones+   -  Reputation: 3190

Like
1Likes
Like

Posted 03 February 2014 - 09:25 AM

Is the sphere missing any triangles? You are concatenating all the vertices but on draw call, you don't use subset.VertexStart (LN54), maybe you forgot to use it to initialize offset (LN304)?



#7 newtechnology   Members   -  Reputation: 783

Like
0Likes
Like

Posted 03 February 2014 - 10:05 AM

I'm basically following the book's structure and he too doesn't use vertices but he stores it (dunno why)

also my model has all correct textures and materials for correct subset and i think problem is only with subsets.

 

this is what happens to dwarf with following code : http://pastebin.com/e3wDiT3q

HGco9F1.png

 

EDIT :  lol,

with sizeof(USHORT) * numfaces * 3

I used DWORD with indices but the model subset 0 was atleast rendering correctly (above image) and when i changed to the correct size

with sizeof(DWORD) * indices

then even the above image got messed up!


Edited by newtechnology, 03 February 2014 - 10:14 AM.


#8 beans222   Members   -  Reputation: 1137

Like
0Likes
Like

Posted 03 February 2014 - 12:55 PM

It looks like you forgot to change "ibd.ByteWidth = sizeof(USHORT) * count;" to use sizeof DWORD instead when creating the buffer.

Also, like Krohm said, in the call to IASetVertexBuffers, offset should be set to the starting vertex * stride .

 

Also, the per-subset material index still isn't being used.

In Model::Render, " Effects::BasicFX->SetMaterial(Materials[i]);"  should be Materials[SubsetTable[i].ID] 


New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

 


#9 ericrrichards22   Members   -  Reputation: 798

Like
2Likes
Like

Posted 03 February 2014 - 03:21 PM

I'm assuming from the snippets you've posted that you're working from Frank Luna's book.  I worked my way through that last year, so you might want to take a look at my code to integrate Assimp.  I was using C# and SlimDX, rather than C++, but the differences aren't that big.

http://www.richardssoftware.net/2013/10/loading-3d-models-using-assimpnet-and.html


Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22


#10 newtechnology   Members   -  Reputation: 783

Like
0Likes
Like

Posted 04 February 2014 - 04:56 AM



I'm assuming from the snippets you've posted that you're working from Frank Luna's book.  I worked my way through that last year, so you might want to take a look at my code to integrate Assimp.  I was using C# and SlimDX, rather than C++, but the differences aren't that big.

http://www.richardssoftware.net/2013/10/loading-3d-models-using-assimpnet-and.html

I already took a look (before you posted) and found it to be useful but somehow i don't know why my model's subset 2 (subset 1 if you start from 0) is getting rendered incorrectly.

 

@beans222:

Subet[i].ID is same as 'i' I think (I've already tried it but no luck.)

also, offset shouldn't be the problem because author in the book (Frank luna's dx11 book) uses his own format .m3d and he never uses offset (by never using, i mean he sets offset to zero or null) but instead facestart and facecount to draw subsets (I'm basically working with that book.)

 

 

EDIT:

models from Programming an RTS Game with Direct3D are rendering correctly (only tested with tree.x and stone.x) while others from outside are not.

 

EDIT2: I was wrong, actually the model (tree.x) used texture atlas :P

(there was only 1 subset)


Edited by newtechnology, 04 February 2014 - 06:07 AM.


#11 beans222   Members   -  Reputation: 1137

Like
1Likes
Like

Posted 04 February 2014 - 10:45 AM

Both of those assumptions are wrong. The material ID could be i rather often, but it's possible for two subsets to use the same material (less materials than meshes). The offset cannot always be zero. If you have DX starting from the beginning of the vertex buffer everytime, each subset's indices

will always use the first X vertices and not the right ones. A subset's indices are always 0 - num_vertices for that subset.

You could add the starting vertex to all the indices for each subset, but it would be easier to just set offset.

 

After looking through the .net code on the linked site, he is adding vertex start to all the indices. I have to assume then that the model format

used in the book has the indices like that already.


New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

 


#12 newtechnology   Members   -  Reputation: 783

Like
0Likes
Like

Posted 05 February 2014 - 05:49 AM

You're right, problem is in indices. I changed this code and now everything is working fine.

thanks @beans222 and of course @ericrrichards22

 

UNDpFP9.png

 

And another complex model

1546309_825363377490018_433032511_n.jpg

 
for (UINT j = 0; j < subset.FaceCount; j++)
{
for (UINT index = 0; index < mesh->mFaces[j].mNumIndices; index++)
{
indices.push_back(subset.VertexStart + mesh->mFaces[j].mIndices[index]);
}
}

Edited by newtechnology, 05 February 2014 - 09:43 AM.


#13 ericrrichards22   Members   -  Reputation: 798

Like
1Likes
Like

Posted 05 February 2014 - 11:46 AM

Very cool, glad you got it working


Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS