SHARE
TWEET

ModelClass.cpp

a guest May 1st, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ModelClass.h"
  2.  
  3.  
  4.  
  5. ModelClass::ModelClass() {
  6.  
  7. }
  8.  
  9. ModelClass::ModelClass(const ModelClass& other) {
  10.     m_meshes = other.m_meshes;
  11.     m_vertexBuffers = other.m_vertexBuffers;
  12. }
  13.  
  14. ModelClass::~ModelClass() {
  15.  
  16. }
  17.  
  18. bool ModelClass::Initialize(ID3D11Device* device) {
  19.     bool result;
  20.     result = InitializeVertexBuffers(device);
  21.     if (!result) {
  22.         cout << "[ModelClass]\tError: failed InitializeBuffers()." << endl;
  23.         return false;
  24.     }
  25.     return true;
  26. }
  27.  
  28. void ModelClass::SetMeshSubsets(vector<Mesh>& subsets) {
  29.     m_meshes = subsets;
  30. }
  31.  
  32. void ModelClass::Shutdown() {
  33.     ShutdownBuffers();
  34. }
  35.  
  36. bool ModelClass::InitializeVertexBuffers(ID3D11Device* device) {
  37.     D3D11_BUFFER_DESC vertexBufferDesc;
  38.     D3D11_SUBRESOURCE_DATA vertexData;
  39.     HRESULT result;
  40.  
  41.     for (size_t i = 0; i < m_meshes.size(); i++) {
  42.         vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  43.         vertexBufferDesc.ByteWidth = (UINT)(m_meshes[i].vertices.size() * sizeof(VertexType));
  44.         vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  45.         vertexBufferDesc.StructureByteStride = 0;
  46.         vertexBufferDesc.CPUAccessFlags = 0;
  47.         vertexBufferDesc.MiscFlags = 0;
  48.  
  49.         vertexData.pSysMem = m_meshes[i].vertices.data();
  50.         vertexData.SysMemPitch = 0;
  51.         vertexData.SysMemSlicePitch = 0;
  52.  
  53.         ID3D11Buffer* vertexBuffer;
  54.         result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &vertexBuffer);
  55.         if (FAILED(result)) {
  56.             cout << "[ModelClass]\tError: failed to create vertex buffer." << endl;
  57.             return false;
  58.         }
  59.         m_vertexBuffers.push_back(vertexBuffer);
  60.     }
  61.     return true;
  62. }
  63.  
  64. void ModelClass::ShutdownBuffers() {
  65.     while (!m_vertexBuffers.empty()) {
  66.         m_vertexBuffers.back()->Release();
  67.         m_vertexBuffers.erase(m_vertexBuffers.end());
  68.     }
  69. }
  70.  
  71. void ModelClass::Render(ID3D11DeviceContext* deviceContext, ShadersTable* shadersTable, MaterialLibrary* matLibrary, TexturesTable* texturesTable, D3DXMATRIX& worldMatrix, D3DXMATRIX& viewMatrix, D3DXMATRIX& projectionMatrix) {
  72.     unsigned int stride;
  73.     unsigned int offset;
  74.  
  75.     stride = sizeof(VertexType);
  76.     offset = 0;
  77.  
  78.     for (size_t i = 0; i < m_vertexBuffers.size(); i++) {
  79.         deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffers[i], &stride, &offset);
  80.         deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  81.         Material* currentMaterial = matLibrary->GetMaterialByName(m_meshes[i].material);
  82.         if (!currentMaterial) {
  83.             cout << "[ModelClass]\tWarning: unable to get material " << m_meshes[i].material << " during rendering. " << endl;
  84.         }
  85.         else {
  86.             ShaderClass* shader = shadersTable->GetShaderByName(currentMaterial->type);
  87.             if (shader == nullptr) {
  88.                 cout << "[ModelClass]\tError: unable to get shader " << m_meshes[i].material << " from ShadersTable." << endl;
  89.             }
  90.             else {
  91.                 shader->Render(deviceContext, texturesTable, m_meshes[i].vertices.size(), worldMatrix, viewMatrix, projectionMatrix, currentMaterial);
  92.             }
  93.         }
  94.     }
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top