• Advertisement
Sign in to follow this  

DX11 assimp subsets problem

This topic is 1445 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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));
}

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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)?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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] 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites


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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Now

  • Advertisement
  • Similar Content

    • By AxeGuywithanAxe
      I wanted to see how others are currently handling descriptor heap updates and management.
      I've read a few articles and there tends to be three major strategies :
      1 ) You split up descriptor heaps per shader stage ( i.e one for vertex shader , pixel , hull, etc)
      2) You have one descriptor heap for an entire pipeline
      3) You split up descriptor heaps for update each update frequency (i.e EResourceSet_PerInstance , EResourceSet_PerPass , EResourceSet_PerMaterial, etc)
      The benefits of the first two approaches is that it makes it easier to port current code, and descriptor / resource descriptor management and updating tends to be easier to manage, but it seems to be not as efficient.
      The benefits of the third approach seems to be that it's the most efficient because you only manage and update objects when they change.
    • By evelyn4you
      hi,
      until now i use typical vertexshader approach for skinning with a Constantbuffer containing the transform matrix for the bones and an the vertexbuffer containing bone index and bone weight.
      Now i have implemented realtime environment  probe cubemaping so i have to render my scene from many point of views and the time for skinning takes too long because it is recalculated for every side of the cubemap.
      For Info i am working on Win7 an therefore use one Shadermodel 5.0 not 5.x that have more options, or is there a way to use 5.x in Win 7
      My Graphic Card is Directx 12 compatible NVidia GTX 960
      the member turanszkij has posted a good for me understandable compute shader. ( for Info: in his engine he uses an optimized version of it )
      https://turanszkij.wordpress.com/2017/09/09/skinning-in-compute-shader/
      Now my questions
       is it possible to feed the compute shader with my orignial vertexbuffer or do i have to copy it in several ByteAdressBuffers as implemented in the following code ?
        the same question is about the constant buffer of the matrixes
       my more urgent question is how do i feed my normal pipeline with the result of the compute Shader which are 2 RWByteAddressBuffers that contain position an normal
      for example i could use 2 vertexbuffer bindings
      1 containing only the uv coordinates
      2.containing position and normal
      How do i copy from the RWByteAddressBuffers to the vertexbuffer ?
       
      (Code from turanszkij )
      Here is my shader implementation for skinning a mesh in a compute shader:
      1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 struct Bone { float4x4 pose; }; StructuredBuffer<Bone> boneBuffer;   ByteAddressBuffer vertexBuffer_POS; // T-Pose pos ByteAddressBuffer vertexBuffer_NOR; // T-Pose normal ByteAddressBuffer vertexBuffer_WEI; // bone weights ByteAddressBuffer vertexBuffer_BON; // bone indices   RWByteAddressBuffer streamoutBuffer_POS; // skinned pos RWByteAddressBuffer streamoutBuffer_NOR; // skinned normal RWByteAddressBuffer streamoutBuffer_PRE; // previous frame skinned pos   inline void Skinning(inout float4 pos, inout float4 nor, in float4 inBon, in float4 inWei) {  float4 p = 0, pp = 0;  float3 n = 0;  float4x4 m;  float3x3 m3;  float weisum = 0;   // force loop to reduce register pressure  // though this way we can not interleave TEX - ALU operations  [loop]  for (uint i = 0; ((i &lt; 4) &amp;&amp; (weisum&lt;1.0f)); ++i)  {  m = boneBuffer[(uint)inBon].pose;  m3 = (float3x3)m;   p += mul(float4(pos.xyz, 1), m)*inWei;  n += mul(nor.xyz, m3)*inWei;   weisum += inWei;  }   bool w = any(inWei);  pos.xyz = w ? p.xyz : pos.xyz;  nor.xyz = w ? n : nor.xyz; }   [numthreads(1024, 1, 1)] void main( uint3 DTid : SV_DispatchThreadID ) {  const uint fetchAddress = DTid.x * 16; // stride is 16 bytes for each vertex buffer now...   uint4 pos_u = vertexBuffer_POS.Load4(fetchAddress);  uint4 nor_u = vertexBuffer_NOR.Load4(fetchAddress);  uint4 wei_u = vertexBuffer_WEI.Load4(fetchAddress);  uint4 bon_u = vertexBuffer_BON.Load4(fetchAddress);   float4 pos = asfloat(pos_u);  float4 nor = asfloat(nor_u);  float4 wei = asfloat(wei_u);  float4 bon = asfloat(bon_u);   Skinning(pos, nor, bon, wei);   pos_u = asuint(pos);  nor_u = asuint(nor);   // copy prev frame current pos to current frame prev pos streamoutBuffer_PRE.Store4(fetchAddress, streamoutBuffer_POS.Load4(fetchAddress)); // write out skinned props:  streamoutBuffer_POS.Store4(fetchAddress, pos_u);  streamoutBuffer_NOR.Store4(fetchAddress, nor_u); }  
    • By mister345
      Hi, can someone please explain why this is giving an assertion EyePosition!=0 exception?
       
      _lightBufferVS->viewMatrix = DirectX::XMMatrixLookAtLH(XMLoadFloat3(&_lightBufferVS->position), XMLoadFloat3(&_lookAt), XMLoadFloat3(&up));
      It looks like DirectX doesnt want the 2nd parameter to be a zero vector in the assertion, but I passed in a zero vector with this exact same code in another program and it ran just fine. (Here is the version of the code that worked - note XMLoadFloat3(&m_lookAt) parameter value is (0,0,0) at runtime - I debugged it - but it throws no exceptions.
          m_viewMatrix = DirectX::XMMatrixLookAtLH(XMLoadFloat3(&m_position), XMLoadFloat3(&m_lookAt), XMLoadFloat3(&up)); Here is the repo for the broken code (See LightClass) https://github.com/mister51213/DirectX11Engine/blob/master/DirectX11Engine/LightClass.cpp
      and here is the repo with the alternative version of the code that is working with a value of (0,0,0) for the second parameter.
      https://github.com/mister51213/DX11Port_SoftShadows/blob/master/Engine/lightclass.cpp
    • By mister345
      Hi, can somebody please tell me in clear simple steps how to debug and step through an hlsl shader file?
      I already did Debug > Start Graphics Debugging > then captured some frames from Visual Studio and
      double clicked on the frame to open it, but no idea where to go from there.
       
      I've been searching for hours and there's no information on this, not even on the Microsoft Website!
      They say "open the  Graphics Pixel History window" but there is no such window!
      Then they say, in the "Pipeline Stages choose Start Debugging"  but the Start Debugging option is nowhere to be found in the whole interface.
      Also, how do I even open the hlsl file that I want to set a break point in from inside the Graphics Debugger?
       
      All I want to do is set a break point in a specific hlsl file, step thru it, and see the data, but this is so unbelievably complicated
      and Microsoft's instructions are horrible! Somebody please, please help.
       
       
       

    • By mister345
      I finally ported Rastertek's tutorial # 42 on soft shadows and blur shading. This tutorial has a ton of really useful effects and there's no working version anywhere online.
      Unfortunately it just draws a black screen. Not sure what's causing it. I'm guessing the camera or ortho matrix transforms are wrong, light directions, or maybe texture resources not being properly initialized.  I didnt change any of the variables though, only upgraded all types and functions DirectX3DVector3 to XMFLOAT3, and used DirectXTK for texture loading. If anyone is willing to take a look at what might be causing the black screen, maybe something pops out to you, let me know, thanks.
      https://github.com/mister51213/DX11Port_SoftShadows
       
      Also, for reference, here's tutorial #40 which has normal shadows but no blur, which I also ported, and it works perfectly.
      https://github.com/mister51213/DX11Port_ShadowMapping
       
  • Advertisement