Advertisement
Guest User

model.cpp

a guest
Feb 2nd, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.63 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. DirectionalLight DirLights[3];
  4.  
  5. Model::Model()
  6. {
  7.     DirLights[0].Ambient  = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f);
  8.     DirLights[0].Diffuse  = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
  9.     DirLights[0].Specular = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
  10.     DirLights[0].Direction = XMFLOAT3(0.707f, -0.707f, 0.0f);
  11.  
  12.     DirLights[1].Ambient  = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f);
  13.     DirLights[1].Diffuse  = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
  14.     DirLights[1].Specular = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
  15.     DirLights[1].Direction = XMFLOAT3(-0.707f, 0.0f, 0.707f);
  16.  
  17. }
  18.  
  19. Model::~Model()
  20. {
  21.    
  22. }
  23.  
  24.  
  25. void Model::LoadModel(const std::string& filename)
  26. {
  27.     Assimp::Importer imp;
  28.  
  29.     const aiScene* pScene = imp.ReadFile(filename,
  30.         aiProcess_CalcTangentSpace |
  31.         aiProcess_Triangulate |
  32.         aiProcess_GenSmoothNormals |
  33.         aiProcess_SplitLargeMeshes |
  34.         aiProcess_ConvertToLeftHanded |
  35.         aiProcess_SortByPType |
  36.         aiProcess_PreTransformVertices);
  37.  
  38.     if (pScene == NULL)
  39.         ShowError(imp.GetErrorString());
  40.    
  41.     std::vector<USHORT> indices;
  42.     std::vector<MeshGeometry::Subset> subsets;
  43.     std::vector<Vertex::Basic32> vertices;
  44.  
  45.     for (UINT i = 0; i < pScene->mNumMeshes; i++)
  46.     {
  47.         aiMesh* mesh = pScene->mMeshes[i];
  48.  
  49.         MeshGeometry::Subset subset;
  50.  
  51.         subset.VertexCount = mesh->mNumVertices;
  52.         subset.VertexStart = vertices.size();
  53.         subset.FaceStart = indices.size() / 3;
  54.         subset.FaceCount = mesh->mNumFaces;
  55.         subset.ID = mesh->mMaterialIndex;
  56.  
  57.         subsets.push_back(subset);
  58.  
  59.         for (UINT j = 0; j < mesh->mNumVertices; j++)
  60.         {
  61.             Vertex::Basic32 vertex;
  62.  
  63.             vertex.Pos.x = mesh->mVertices[j].x;
  64.             vertex.Pos.y = mesh->mVertices[j].y;
  65.             vertex.Pos.z = mesh->mVertices[j].z;
  66.  
  67.             vertex.Normal.x = mesh->mNormals[j].x;
  68.             vertex.Normal.y = mesh->mNormals[j].y;
  69.             vertex.Normal.z = mesh->mNormals[j].z;
  70.  
  71.             vertex.Tex.x = mesh->mTextureCoords[0][j].x;
  72.             vertex.Tex.y =  mesh->mTextureCoords[0][j].y;
  73.            
  74.  
  75.             vertices.push_back(vertex);
  76.         }
  77.  
  78.         for (UINT j = 0; j < mesh->mNumFaces; j++)
  79.         {
  80.             for (UINT index = 0; index < mesh->mFaces[j].mNumIndices; index++)
  81.             {
  82.                 indices.push_back(mesh->mFaces[j].mIndices[index]);
  83.             }
  84.         }
  85.  
  86.         aiMaterial* Mat = pScene->mMaterials[mesh->mMaterialIndex];
  87.  
  88.         Material tempMat;
  89.  
  90.         aiColor4D color(0.0f, 0.0f, 0.0f, 0.0f);
  91.  
  92.         tempMat.Ambient = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f);
  93.         tempMat.Diffuse = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f);
  94.         tempMat.Specular = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f);
  95.         tempMat.Reflect = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f);
  96.        
  97.         Mat->Get(AI_MATKEY_COLOR_AMBIENT, color);
  98.    
  99.         tempMat.Ambient = XMFLOAT4(color.r, color.g, color.b, color.a);
  100.  
  101.         Mat->Get(AI_MATKEY_COLOR_DIFFUSE, color);
  102.  
  103.         tempMat.Diffuse = XMFLOAT4(color.r, color.g, color.b, color.a);
  104.  
  105.         Mat->Get(AI_MATKEY_COLOR_SPECULAR, color);
  106.  
  107.         tempMat.Specular = XMFLOAT4(color.r, color.g, color.b, color.a);
  108.  
  109.         Mat->Get(AI_MATKEY_COLOR_REFLECTIVE, color);
  110.  
  111.         tempMat.Reflect = XMFLOAT4(color.r, color.g, color.b, color.a);
  112.  
  113.         if (tempMat.Ambient.x == 0 && tempMat.Ambient.y == 0 && tempMat.Ambient.z == 0 && tempMat.Ambient.w == 0)
  114.             tempMat.Ambient = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f);
  115.  
  116.         if (tempMat.Diffuse.x == 0 && tempMat.Diffuse.y == 0 && tempMat.Diffuse.z == 0 && tempMat.Diffuse.w == 0)
  117.             tempMat.Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
  118.  
  119.         if (tempMat.Specular.x == 0 && tempMat.Specular.y == 0 && tempMat.Specular.z == 0 && tempMat.Specular.w == 0)
  120.             tempMat.Specular = XMFLOAT4(0.6f, 0.6f, 0.6f, 16.0f);
  121.  
  122.         // if (tempMat.Reflect.x == 0 && tempMat.Reflect.y == 0 && tempMat.Reflect.z == 0 && tempMat.Reflect.w == 0)
  123.             //tempMat.Reflect = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
  124.  
  125.         Materials.push_back(tempMat);
  126.    
  127.         aiString path;
  128.  
  129.         if (Mat->GetTextureCount(aiTextureType_DIFFUSE) > 0 && Mat->GetTexture(aiTextureType_DIFFUSE, 0, &path) == AI_SUCCESS)
  130.         {
  131.             char cpath[35];
  132.  
  133.             sprintf(cpath, "Resources\\Textures\\%s", path.C_Str());
  134.  
  135.             ID3D11ShaderResourceView* srv = 0;
  136.            
  137.             HR(D3DX11CreateShaderResourceViewFromFileA(pDevice, cpath, 0, 0, &srv, 0));
  138.  
  139.             if (srv == 0)
  140.                 ShowError("texture not loaded.");
  141.  
  142.             DiffuseMapSRV.push_back(srv);
  143.         }
  144.  
  145.     }
  146.    
  147.  
  148.     mModel.mSubsetCount = subsets.size();
  149.     mModel.mNumVertices = vertices.size();
  150.     mModel.mNumFaces = indices.size() / 3;
  151.  
  152.     XNA::ComputeBoundingAxisAlignedBoxFromPoints(&mModel.box, mModel.mNumVertices, &vertices[0].Pos, sizeof(Vertex::Basic32));
  153.  
  154.     mModel.Mesh.SetSubsetTable(subsets);
  155.     mModel.Mesh.SetVertices(&vertices[0], mModel.mNumVertices);
  156.     mModel.Mesh.SetIndices(&indices[0], mModel.mNumFaces * 3);
  157.  
  158. }
  159.  
  160. bool Model::IntersectAABBFrustum(XNA::AxisAlignedBox& box, CXMMATRIX world)
  161. {
  162.     XMVECTOR detView = XMMatrixDeterminant(d3d->m_Cam.View());
  163.     XMMATRIX invView = XMMatrixInverse(&detView, d3d->m_Cam.View());
  164.  
  165.     XMMATRIX invWorld = XMMatrixInverse(&XMMatrixDeterminant(world), world);
  166.  
  167.     XMMATRIX toLocal = XMMatrixMultiply(invView, invWorld);
  168.      
  169.     XMVECTOR scale;
  170.     XMVECTOR rotQuat;
  171.     XMVECTOR translation;
  172.     XMMatrixDecompose(&scale, &rotQuat, &translation, toLocal);
  173.  
  174.     XNA::Frustum localspaceFrustum;
  175.     XNA::TransformFrustum(&localspaceFrustum, &d3d->m_Frustum, XMVectorGetX(scale), rotQuat, translation);
  176.  
  177.     if (XNA::IntersectAxisAlignedBoxFrustum(&box, &localspaceFrustum) != 0)
  178.         return true;
  179.    
  180.     return false;
  181. }
  182.  
  183. void Model::Rotate(FLOAT X, FLOAT Y, FLOAT Z)
  184. {
  185.     static bool x, y, z;
  186.  
  187.     x = true;
  188.     y = true;
  189.     z = true;
  190.  
  191.     if (X == 0)
  192.         x = false;
  193.     if (Y == 0)
  194.         y = false;
  195.     if (Z == 0)
  196.         z = false;
  197.  
  198.     if (x)
  199.         mModel.World = XMMatrixRotationX(X);
  200.     if (y)
  201.         mModel.World = XMMatrixRotationY(Y);
  202.     if (z)
  203.         mModel.World = XMMatrixRotationZ(Z);
  204. }
  205.  
  206. void Model::Scale(FLOAT X, FLOAT Y, FLOAT Z)
  207. {
  208.     mModel.World = XMMatrixScaling(X, Y, Z);
  209. }
  210.  
  211. void Model::Translate(FLOAT X, FLOAT Y, FLOAT Z)
  212. {
  213.     mModel.World = XMMatrixTranslation(X, Y, Z);
  214. }
  215.  
  216. void Model::SetIdentifyMatrix()
  217. {
  218.     mModel.World = XMMatrixIdentity();
  219. }
  220.  
  221. void Model::Render()
  222. {
  223.     if (!IntersectAABBFrustum(mModel.box, mModel.World))
  224.         return;
  225.  
  226.  
  227.     UINT Stride = sizeof(Vertex::Basic32);
  228.     UINT Offset = 0;
  229.    
  230.  
  231.     Effects::BasicFX->SetDirLights(DirLights);
  232.     Effects::BasicFX->SetEyePosW(d3d->m_Cam.GetPosition());
  233.  
  234.     ID3DX11EffectTechnique* activeTech = Effects::BasicFX->Light2TexTech;
  235.  
  236.     D3DX11_TECHNIQUE_DESC techDesc;
  237.     activeTech->GetDesc(&techDesc);
  238.  
  239.     XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(mModel.World);
  240.     XMMATRIX worldViewProj = mModel.World * d3d->m_Cam.ViewProj();
  241.  
  242.     Effects::BasicFX->SetWorld(mModel.World);
  243.     Effects::BasicFX->SetWorldInvTranspose(worldInvTranspose);
  244.     Effects::BasicFX->SetWorldViewProj(worldViewProj);
  245.     Effects::BasicFX->SetTexTransform(XMMatrixIdentity());
  246.  
  247.            
  248.  
  249.     for(UINT p = 0; p < techDesc.Passes; ++p)
  250.     {
  251.        for (UINT i = 0; i < mModel.mSubsetCount; i++)
  252.        {  
  253.            Effects::BasicFX->SetDiffuseMap(DiffuseMapSRV[i]);
  254.            Effects::BasicFX->SetMaterial(Materials[i]);
  255.  
  256.            activeTech->GetPassByIndex(p)->Apply(0, pDeviceContext);
  257.            mModel.Mesh.Draw(i);
  258.         }
  259.     }
  260. }
  261.  
  262.  
  263. MeshGeometry::MeshGeometry()
  264. {
  265.     VertexBuffer = nullptr;
  266.     IndexBuffer = nullptr;
  267.  
  268.     IndexBufferFormat = DXGI_FORMAT_R16_UINT;
  269.     VertexStride = 0;
  270. }
  271.  
  272. MeshGeometry::~MeshGeometry()
  273. {
  274.     ReleaseCOM(VertexBuffer);
  275.     ReleaseCOM(IndexBuffer);
  276. }
  277.  
  278. void MeshGeometry::SetIndices(const USHORT* indices, UINT count)
  279. {
  280.     D3D11_BUFFER_DESC ibd;
  281.     ibd.Usage = D3D11_USAGE_IMMUTABLE;
  282.     ibd.ByteWidth = sizeof(USHORT) * count;
  283.     ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
  284.     ibd.CPUAccessFlags = 0;
  285.     ibd.MiscFlags = 0;
  286.     ibd.StructureByteStride = 0;
  287.  
  288.     D3D11_SUBRESOURCE_DATA iinitData;
  289.     iinitData.pSysMem = indices;
  290.  
  291.     HR(pDevice->CreateBuffer(&ibd, &iinitData, &IndexBuffer));
  292. }
  293.  
  294. void MeshGeometry::SetSubsetTable(std::vector<Subset>& subsetTable)
  295. {
  296.     SubsetTable = subsetTable;
  297. }
  298.  
  299. void MeshGeometry::Draw(UINT subsetId)
  300. {
  301.     UINT offset = 0;
  302.  
  303.     pDeviceContext->IASetVertexBuffers(0, 1, &VertexBuffer, &VertexStride, &offset);
  304.     pDeviceContext->IASetIndexBuffer(IndexBuffer, IndexBufferFormat, 0);
  305.  
  306.     pDeviceContext->DrawIndexed(SubsetTable[subsetId].FaceCount * 3, SubsetTable[subsetId].FaceStart * 3,  0);
  307. }
  308.  
  309. template <typename VertexType>
  310. void MeshGeometry::SetVertices(const VertexType* vertices, UINT count)
  311. {
  312.     ReleaseCOM(VertexBuffer);
  313.  
  314.     VertexStride = sizeof(VertexType);
  315.  
  316.     D3D11_BUFFER_DESC vbd;
  317.     vbd.Usage = D3D11_USAGE_IMMUTABLE;
  318.     vbd.ByteWidth = sizeof(VertexType) * count;
  319.     vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  320.     vbd.CPUAccessFlags = 0;
  321.     vbd.MiscFlags = 0;
  322.     vbd.StructureByteStride = 0;
  323.  
  324.     D3D11_SUBRESOURCE_DATA vinitData;
  325.     vinitData.pSysMem = vertices;
  326.  
  327.     HR(pDevice->CreateBuffer(&vbd, &vinitData, &VertexBuffer));
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement