The Dirtchamber
A mixed reality testing environment for real-time global illumination algorithms
simple_mesh.h
Go to the documentation of this file.
1 /*
2  * Dune D3D library - Tobias Alexander Franke 2011
3  * For copyright and license see LICENSE
4  * http://www.tobias-franke.eu
5  */
6 
9 #ifndef DUNE_SIMPLE_MESH
10 #define DUNE_SIMPLE_MESH
11 
12 #include <D3D11.h>
13 #include <vector>
14 
15 #include "d3d_tools.h"
16 #include "mesh.h"
17 #include "logger.h"
18 
19 namespace dune
20 {
31  class simple_mesh : public d3d_mesh
32  {
33  protected:
34  struct vertex
35  {
36  DirectX::XMFLOAT3 v;
37  };
38 
39  std::vector<vertex> vertices_;
40 
41  ID3D11Buffer* mesh_data_;
42 
43  protected:
44  virtual const D3D11_INPUT_ELEMENT_DESC* vertex_desc()
45  {
46  static D3D11_INPUT_ELEMENT_DESC l[] =
47  {
48  { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
49  0
50  };
51 
52  return l;
53  }
54 
55  public:
56  simple_mesh() :
57  vertices_(),
58  mesh_data_()
59  {
60  }
61 
62  virtual ~simple_mesh() {}
63 
65  void push_back(DirectX::XMFLOAT3& p)
66  {
67  vertex v = { p };
68  vertices_.push_back(v);
69  }
70 
79  virtual void create(ID3D11Device* device, const tstring& name)
80  {
81  tclog << L"Creating: simple geometry " << name << L" with " << num_faces() << " faces" << std::endl;
82 
83  D3D11_BUFFER_DESC bd;
84  ZeroMemory(&bd, sizeof(bd));
85  bd.Usage = D3D11_USAGE_DEFAULT;
86  bd.ByteWidth = sizeof(vertex)* 3;
87  bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
88  bd.CPUAccessFlags = 0;
89 
90  D3D11_SUBRESOURCE_DATA InitData;
91  ZeroMemory(&InitData, sizeof(InitData));
92  InitData.pSysMem = &vertices_[0];
93 
94  assert_hr(device->CreateBuffer(&bd, &InitData, &mesh_data_));
95  }
96 
97  virtual void render(ID3D11DeviceContext* context, DirectX::XMFLOAT4X4* to_clip = nullptr)
98  {
99  context->VSSetShader(vs_, nullptr, 0);
100  context->IASetInputLayout(vertex_layout_);
101  context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
102 
103  static const UINT stride = sizeof(vertex);
104  static const UINT offset = 0;
105 
106  context->IASetVertexBuffers(0, 1, &mesh_data_, &stride, &offset);
107 
108  context->Draw(static_cast<UINT>(num_vertices()), 0);
109  }
110 
111  virtual size_t num_vertices()
112  {
113  return vertices_.size();
114  }
115 
116  virtual size_t num_faces()
117  {
118  return num_vertices() / 3;
119  }
120 
121  virtual void destroy()
122  {
123  safe_release(mesh_data_);
124  vertices_.clear();
125 
127  }
128  };
129 }
130 
131 #endif
virtual void destroy()
Destroy a mesh and free all its resources.
Definition: mesh.cpp:42
virtual size_t num_faces()
Returns the number of faces of a mesh.
Definition: simple_mesh.h:116
virtual void create(ID3D11Device *device, const tstring &name)
Create a new simple_mesh.
Definition: simple_mesh.h:79
virtual void destroy()
Destroy a mesh and free all its resources.
Definition: simple_mesh.h:121
void push_back(DirectX::XMFLOAT3 &p)
Add a new vertex p to the mesh.
Definition: simple_mesh.h:65
virtual size_t num_vertices()
Returns the number of vertices of a mesh.
Definition: simple_mesh.h:111
#define assert_hr(x)
An assert for HRESULTs.
Definition: d3d_tools.h:148
void safe_release(T &obj)
Safely release an object of type T, setting it to nullptr afterwards.
Definition: d3d_tools.h:61
virtual void render(ID3D11DeviceContext *context, DirectX::XMFLOAT4X4 *to_clip=nullptr)
Renders the mesh using the shaders previously set.
Definition: simple_mesh.h:97
Main D3D mesh interface class.
Definition: mesh.h:129
A very simple mesh class with position-only vertex attributes.
Definition: simple_mesh.h:31