Jump to content
  • Advertisement
Sign in to follow this  
215648

texturing problem (model appearing pure black - assimp)

This topic is 2083 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 texturing support in my assimp model loading class.

my code follows this structure: http://www.richardssoftware.net/2013/10/loading-3d-models-using-assimpnet-and.html

 

I dunno why it remains black

 

here is my code

 

Model.cpp

http://pastebin.com/kmdiZNSB

 

model.h

http://pastebin.com/EA4Wf2xa

 

shader

http://pastebin.com/tPxVTd0u

 

and when i try to load a model with only material then I get vector subscript out of range error (i.e when model has no textures).

 

This is how it gets rendered :

TMSmy3y.png

This is how it should get rendered:

3MkBnlA.png

 

 

There is no problem in the model (confirmed.)

Edited by newtechnology

Share this post


Link to post
Share on other sites
Advertisement

I even tried it with manually specifying texture and materials then setting it but no luck. (only material is being used, no texturing is done.)

i dunno why

model.cpp

#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);
 
DefaultMat.Ambient = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
DefaultMat.Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
DefaultMat.Specular = XMFLOAT4(0.6f, 0.6f, 0.6f, 16.0f);
 
}
 
Model::~Model()
{
ReleaseCOM(testSRV);
}
 
 
void Model::LoadModel(const std::string& filename)
{
D3DX11CreateShaderResourceViewFromFile(pDevice, L"Resources\\Textures\\WoodCrate01.dds", 0, 0, &testSRV, 0);
 
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[35];
 
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::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->Light0TexTech;
 
    D3DX11_TECHNIQUE_DESC techDesc;
    activeTech->GetDesc(&techDesc);
 
    for(UINT p = 0; p < techDesc.Passes; ++p)
    {
 
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);
Effects::BasicFX->SetTexTransform(XMMatrixIdentity());
Effects::BasicFX->SetMaterial(DefaultMat);
Effects::BasicFX->SetDiffuseMap(testSRV);
 
 
 
  for (UINT i = 0; i < mModel.mSubsetCount; i++)
  {   
 // Effects::BasicFX->SetMaterial(DefaultMat);
 // Effects::BasicFX->SetDiffuseMap(testSRV); 
 
  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));
}

model.h

#ifndef _MODEL_H_
#define _MODEL_H_
 
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
 
 
class MeshGeometry
{
public:
struct Subset
{
Subset()
{
ID = -1;
 
VertexCount = 0;
FaceCount = 0;
 
VertexStart = 0;
FaceStart = 0;
}
 
UINT ID;
UINT VertexStart;
UINT VertexCount;
UINT FaceStart;
UINT FaceCount;
};
 
public:
MeshGeometry();
~MeshGeometry();
 
template <typename VertexType>
void SetVertices(const VertexType* vertices, UINT count);
void SetIndices(const USHORT* indices, UINT count);
void SetSubsetTable(std::vector<Subset>& subsetTable);
 
void Draw(UINT subsetID);
 
private:
MeshGeometry(const MeshGeometry& rhs);
MeshGeometry& operator=(const MeshGeometry& rhs);
 
private:
ID3D11Buffer* VertexBuffer;
ID3D11Buffer* IndexBuffer;
 
DXGI_FORMAT IndexBufferFormat;
UINT VertexStride;
 
std::vector<Subset> SubsetTable;
};
 
class Model
{
public:
Model();
~Model();
 
void LoadModel(const std::string& FileName);
void Render();
bool Intersect(XNA::AxisAlignedBox& box); 
 
void Translate(FLOAT X, FLOAT Y, FLOAT Z);
void Scale(FLOAT X, FLOAT Y, FLOAT Z);
void Rotate(FLOAT AngleX, FLOAT AngleY, FLOAT AngleZ);
void SetIdentifyMatrix();
 
private:
bool IntersectAABBFrustum(XNA::AxisAlignedBox& box, CXMMATRIX world);
private:
struct ModelData
{ 
ModelData()
{
mNumVertices = 0;
mNumFaces = 0;
mSubsetCount = 0;
 
World = XMMatrixIdentity();
 
box.Center = XMFLOAT3(0.0f, 0.0f, 0.0f);
box.Extents = XMFLOAT3(0.0f, 0.0f, 0.0f);
}
 
MeshGeometry Mesh;
 
XNA::AxisAlignedBox box;
XMMATRIX World;
 
UINT mNumVertices;
UINT mNumFaces;
UINT mSubsetCount;
 
 
};
 
std::vector<ID3D11ShaderResourceView*> DiffuseMapSRV;
std::vector<Material> Materials;
 
ID3D11ShaderResourceView* testSRV;
Material DefaultMat;
 
ModelData mModel;
TextureMgr Mgr;
};
 
 
 
#endif

EDIT: I fixed the issue by drawing that box model first then terrain

but now i have another problem

1450675_823466561013033_1076020377_n.jpg

I think texture coordinates are incorrect.

Edited by newtechnology

Share this post


Link to post
Share on other sites

It seems like the index order is incorrect, or you have the wrong culling mode?

 

Can you please share how you set the culling?

And try to invert the indices and see the effect.

 

-MIGI0027

Share this post


Link to post
Share on other sites

You're very right, I cleared rasterizer state to default and the problem was solved.

thank you very much, I appreciate it.

Edited by newtechnology

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!