assimp/samples/SimpleTexturedDirectx11/SimpleTexturedDirectx11/Mesh.h

108 lines
2.7 KiB
C
Raw Normal View History

2017-06-03 07:25:20 +00:00
#ifndef MESH_H
#define MESH_H
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <stdexcept>
2017-06-03 07:25:20 +00:00
#include <d3d11_1.h>
#include <DirectXMath.h>
2020-03-07 11:19:55 +00:00
2017-06-03 07:25:20 +00:00
using namespace DirectX;
#include "SafeRelease.hpp"
2017-06-03 07:25:20 +00:00
struct VERTEX {
FLOAT X, Y, Z;
XMFLOAT2 texcoord;
};
struct Texture {
std::string type;
std::string path;
2017-06-03 07:25:20 +00:00
ID3D11ShaderResourceView *texture;
2020-03-07 11:19:55 +00:00
void Release() {
SafeRelease(texture);
}
2017-06-03 07:25:20 +00:00
};
class Mesh {
public:
std::vector<VERTEX> vertices_;
std::vector<UINT> indices_;
std::vector<Texture> textures_;
ID3D11Device *dev_;
2020-03-07 11:19:55 +00:00
Mesh(ID3D11Device *dev, const std::vector<VERTEX>& vertices, const std::vector<UINT>& indices, const std::vector<Texture>& textures) :
vertices_(vertices),
indices_(indices),
textures_(textures),
dev_(dev),
VertexBuffer_(nullptr),
IndexBuffer_(nullptr) {
this->setupMesh(this->dev_);
2020-03-07 11:19:55 +00:00
}
void Draw(ID3D11DeviceContext *devcon) {
UINT stride = sizeof(VERTEX);
UINT offset = 0;
devcon->IASetVertexBuffers(0, 1, &VertexBuffer_, &stride, &offset);
devcon->IASetIndexBuffer(IndexBuffer_, DXGI_FORMAT_R32_UINT, 0);
2020-03-07 11:19:55 +00:00
devcon->PSSetShaderResources(0, 1, &textures_[0].texture);
2020-03-07 11:19:55 +00:00
devcon->DrawIndexed(static_cast<UINT>(indices_.size()), 0, 0);
2020-03-07 11:19:55 +00:00
}
void Close() {
SafeRelease(VertexBuffer_);
SafeRelease(IndexBuffer_);
2020-03-07 11:19:55 +00:00
}
2017-06-03 07:25:20 +00:00
private:
2020-03-07 11:19:55 +00:00
// Render data
ID3D11Buffer *VertexBuffer_, *IndexBuffer_;
2020-03-07 11:19:55 +00:00
// Functions
// Initializes all the buffer objects/arrays
void setupMesh(ID3D11Device *dev) {
HRESULT hr;
D3D11_BUFFER_DESC vbd;
vbd.Usage = D3D11_USAGE_IMMUTABLE;
vbd.ByteWidth = static_cast<UINT>(sizeof(VERTEX) * vertices_.size());
2020-03-07 11:19:55 +00:00
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA initData;
initData.pSysMem = &vertices_[0];
2020-03-07 11:19:55 +00:00
hr = dev->CreateBuffer(&vbd, &initData, &VertexBuffer_);
2020-03-07 11:19:55 +00:00
if (FAILED(hr)) {
Close();
throw std::runtime_error("Failed to create vertex buffer.");
}
D3D11_BUFFER_DESC ibd;
ibd.Usage = D3D11_USAGE_IMMUTABLE;
ibd.ByteWidth = static_cast<UINT>(sizeof(UINT) * indices_.size());
2020-03-07 11:19:55 +00:00
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
initData.pSysMem = &indices_[0];
2020-03-07 11:19:55 +00:00
hr = dev->CreateBuffer(&ibd, &initData, &IndexBuffer_);
2020-03-07 11:19:55 +00:00
if (FAILED(hr)) {
Close();
throw std::runtime_error("Failed to create index buffer.");
}
}
2017-06-03 07:25:20 +00:00
};
#endif