The Dirtchamber
A mixed reality testing environment for real-time global illumination algorithms
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_MESH
10 #define DUNE_MESH
11 
12 #include <D3D11.h>
13 #include <DirectXMath.h>
14 
15 #include <memory>
16 #include <cassert>
17 
18 #include "unicode.h"
19 
20 namespace dune
21 {
27  template<typename V>
28  struct aabb
29  {
30  protected:
31  V bb_max_;
32  V bb_min_;
33  DirectX::XMFLOAT4X4 world_;
34 
35  protected:
43  inline void init_bb(const V& p)
44  {
45  bb_max_ = bb_min_ = p;
46  }
47 
56  void update_bb(const V& p)
57  {
58  bb_max_ = max(p, bb_max_);
59  bb_min_ = min(p, bb_min_);
60  }
61 
62  public:
64  inline V center() const { return bb_min_ + (bb_max_ - bb_min_)/2.0f; };
65 
67  inline V bb_max() const { return bb_max_; };
68 
70  inline V bb_min() const { return bb_min_; };
71 
73  inline virtual void set_world(const DirectX::XMFLOAT4X4& world)
74  {
75  world_ = world;
76  }
77 
79  inline const DirectX::XMFLOAT4X4& world()
80  {
81  return world_;
82  }
83  };
84 
94  bool is_visible(const aabb<DirectX::XMFLOAT3>& bbox, const DirectX::XMFLOAT4X4& to_clip);
95 
101  template<typename V>
102  struct mesh : public aabb<V>
103  {
104  public:
106  virtual size_t num_vertices() = 0;
107 
109  virtual size_t num_faces() = 0;
110  };
111 
113  const D3D11_INPUT_ELEMENT_DESC standard_vertex_desc[] =
114  {
115  { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
116  { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
117  { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
118  0
119  };
120 
129  struct d3d_mesh : public mesh<DirectX::XMFLOAT3>
130  {
131  protected:
132  ID3D11VertexShader* vs_;
133  ID3D11PixelShader* ps_;
134 
135  ID3D11InputLayout* vertex_layout_;
136 
137  INT diffuse_tex_slot_;
138  INT normal_tex_slot_;
139  INT specular_tex_slot_;
140 
141  protected:
142  /* !\brief Returns the vertex_descriptor of a basic d3d_mesh. */
143  virtual const D3D11_INPUT_ELEMENT_DESC* vertex_desc() { return standard_vertex_desc; }
144 
145  public:
146  d3d_mesh();
147  virtual ~d3d_mesh() {}
148 
150  virtual void set_shader(ID3D11Device* device, ID3DBlob* input_binary, ID3D11VertexShader* vs, ID3D11PixelShader* ps);
151 
153  virtual void set_shader_slots(INT diffuse_tex = -1,
154  INT normal_tex = -1,
155  INT specular_tex = -1)
156  {
157  diffuse_tex_slot_ = diffuse_tex;
158  normal_tex_slot_ = normal_tex;
159  specular_tex_slot_ = specular_tex;
160  }
161 
163  virtual void create(ID3D11Device* device, const tstring& filename) = 0;
164 
166  virtual void destroy();
167 
177  virtual void render(ID3D11DeviceContext* context, DirectX::XMFLOAT4X4* to_clip = nullptr) = 0;
178  };
179 
191  void load_model(ID3D11Device* device, const tstring& file, std::shared_ptr<d3d_mesh>& ptr);
192 }
193 
194 #endif // MESH
virtual void destroy()
Destroy a mesh and free all its resources.
Definition: mesh.cpp:42
void load_model(ID3D11Device *device, const tstring &file, std::shared_ptr< d3d_mesh > &ptr)
Load a model specified by a filename.
Definition: mesh.cpp:16
virtual void set_shader(ID3D11Device *device, ID3DBlob *input_binary, ID3D11VertexShader *vs, ID3D11PixelShader *ps)
Sets the input layout, vertex- and pixel shader of this mesh.
Definition: mesh.cpp:59
void update_bb(const V &p)
Update the bounding-box with a new point.
Definition: mesh.h:56
bool is_visible(const aabb< DirectX::XMFLOAT3 > &bbox, const DirectX::XMFLOAT4X4 &to_clip)
Check if a bounding-box is visible given a clipping matrix.
Definition: mesh.cpp:74
virtual size_t num_vertices()=0
Returns the number of vertices of a mesh.
virtual void set_world(const DirectX::XMFLOAT4X4 &world)
Update the world matrix of this mesh.
Definition: mesh.h:73
An axis-aligned bounding-box.
Definition: mesh.h:28
void init_bb(const V &p)
Initialize the bounding-box with a first point.
Definition: mesh.h:43
virtual void create(ID3D11Device *device, const tstring &filename)=0
Create a mesh from a given filename.
const DirectX::XMFLOAT4X4 & world()
Returns the current world matrix of this mesh.
Definition: mesh.h:79
V bb_min() const
Returns the minimum (lower/left/front) of the bounding-box.
Definition: mesh.h:70
virtual size_t num_faces()=0
Returns the number of faces of a mesh.
const D3D11_INPUT_ELEMENT_DESC standard_vertex_desc[]
The standard layout of a d3d_mesh.
Definition: mesh.h:113
virtual void set_shader_slots(INT diffuse_tex=-1, INT normal_tex=-1, INT specular_tex=-1)
Sets three register numbers to identify the slots the pixel shader is looking for surface textures...
Definition: mesh.h:153
Basic interface of a mesh.
Definition: mesh.h:102
virtual void render(ID3D11DeviceContext *context, DirectX::XMFLOAT4X4 *to_clip=nullptr)=0
Renders the mesh using the shaders previously set.
V center() const
Returns the center of the bounding-box.
Definition: mesh.h:64
Main D3D mesh interface class.
Definition: mesh.h:129
V bb_max() const
Returns the maximum (upper/right/back) of the bounding-box.
Definition: mesh.h:67