The Dirtchamber
A mixed reality testing environment for real-time global illumination algorithms
assimp_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_ASSIMP_MESH
10 #define DUNE_ASSIMP_MESH
11 
12 #include <vector>
13 #include <map>
14 
15 #include <assimp/scene.h>
16 #include <assimp/Importer.hpp>
17 #include <assimp/postprocess.h>
18 
19 #include "mesh.h"
20 #include "cbuffer.h"
21 
22 namespace dune
23 {
35  class assimp_mesh : public d3d_mesh
36  {
37  public:
39  struct vertex
40  {
41  DirectX::XMFLOAT3 position;
42  DirectX::XMFLOAT3 normal;
43  DirectX::XMFLOAT3 tangent;
44  DirectX::XMFLOAT4 color;
45  DirectX::XMFLOAT2 texcoord[AI_MAX_NUMBER_OF_TEXTURECOORDS];
46  };
47 
48  protected:
50  struct mesh_info : public aabb<DirectX::XMFLOAT3>
51  {
52  UINT vstart_index;
53  UINT istart_index;
54  UINT num_faces;
55  UINT num_vertices;
56  UINT material_index;
57 
58  void update(const DirectX::XMFLOAT3& v, bool first)
59  {
60  if (first)
61  init_bb(v);
62  else
63  update_bb(v);
64  }
65  };
66 
67  protected:
68  Assimp::Importer importer_;
69  std::vector<mesh_info> mesh_infos_;
70  std::vector<unsigned int> indices_;
71 
72  private:
73  // warning: do not confuse with functions num_vertices() and num_faces()
74  UINT num_faces_;
75  UINT num_vertices_;
76 
77  protected:
79  virtual void push_back(vertex v) = 0;
80 
81  virtual void load_internal(const aiScene* scene, aiNode* node, aiMatrix4x4 transform);
82 
83  void load(const tstring& file);
84 
85  public:
86  assimp_mesh();
87  virtual ~assimp_mesh() {};
88 
89  const aiScene* const assimp_scene() const;
90  virtual void destroy();
91 
92  size_t num_vertices();
93  size_t num_faces();
94  };
95 
97  const D3D11_INPUT_ELEMENT_DESC gilgamesh_vertex_desc[5] =
98  {
99  { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
100  { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
101  { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
102  { "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0 },
103  0
104  };
105 
113  class gilga_mesh : public assimp_mesh
114  {
115  protected:
117  enum
118  {
119  SHADING_OFF,
120  SHADING_DEFAULT,
121  SHADING_IBL,
122  SHADING_AREA_LIGHT
123  };
124 
127  {
128  DirectX::XMFLOAT3 position;
129  DirectX::XMFLOAT3 normal;
130  DirectX::XMFLOAT2 texcoord;
131  DirectX::XMFLOAT3 tangent;
132  };
133 
136  {
137  DirectX::XMFLOAT4X4 world;
138  };
139 
142  {
143  DirectX::XMFLOAT4 diffuse_color;
144  DirectX::XMFLOAT4 specular_color;
145  DirectX::XMFLOAT4 emissive_color;
146  BOOL has_diffuse_tex;
147  BOOL has_normal_tex;
148  BOOL has_specular_tex;
149  BOOL has_alpha_tex;
150  UINT shading_mode;
151  FLOAT roughness;
152  FLOAT refractive_index;
153  FLOAT pad;
154  };
155 
157  struct mesh_data
158  {
159  ID3D11Buffer* index;
160  ID3D11Buffer* vertex;
161  DirectX::XMFLOAT4 diffuse_color;
162  DirectX::XMFLOAT4 specular_color;
163  DirectX::XMFLOAT4 emissive_color;
164  ID3D11ShaderResourceView* diffuse_tex;
165  ID3D11ShaderResourceView* emissive_tex;
166  ID3D11ShaderResourceView* specular_tex;
167  ID3D11ShaderResourceView* normal_tex;
168  ID3D11ShaderResourceView* alpha_tex;
169  UINT shading_mode;
170  FLOAT roughness;
171  FLOAT refractive_index;
172  };
173 
174  protected:
175  cbuffer<mesh_data_ps> cb_mesh_data_ps_;
176  cbuffer<mesh_data_vs> cb_mesh_data_vs_;
177 
178  sampler_state ss_;
179 
180  INT alpha_tex_slot_;
181 
182  std::vector<gilga_vertex> vertices_;
183  std::vector<mesh_data> meshes_;
184 
185  protected:
186  void push_back(vertex v);
187 
188  virtual const D3D11_INPUT_ELEMENT_DESC* vertex_desc() { return gilgamesh_vertex_desc; }
189 
190  public:
191  gilga_mesh();
192  virtual ~gilga_mesh() {};
193 
194  virtual void create(ID3D11Device* device, const tstring& file);
195  virtual void render(ID3D11DeviceContext* context, DirectX::XMFLOAT4X4* to_clip = nullptr);
196  virtual void destroy();
197 
199  void prepare_context(ID3D11DeviceContext* context);
200 
202  void render_direct(ID3D11DeviceContext* context, DirectX::XMFLOAT4X4* to_clip);
203 
212  void set_alpha_slot(INT alpha_tex = -1)
213  {
214  alpha_tex_slot_ = alpha_tex;
215  }
216  };
217 }
218 
219 #endif
void set_alpha_slot(INT alpha_tex=-1)
Set the texture register for alpha textures.
Definition: assimp_mesh.h:212
Mesh data for CPU side useage.
Definition: assimp_mesh.h:157
A d3d_mesh created with the help of Assimp.
Definition: assimp_mesh.h:35
virtual void create(ID3D11Device *device, const tstring &file)
Create a mesh from a given filename.
Definition: assimp_mesh.cpp:343
const D3D11_INPUT_ELEMENT_DESC gilgamesh_vertex_desc[5]
The default layout for a gigla_mesh.
Definition: assimp_mesh.h:97
A vertex with all its attributes as loaded by Assimp.
Definition: assimp_mesh.h:39
void update_bb(const DirectX::XMFLOAT3 &p)
Update the bounding-box with a new point.
Definition: mesh.h:56
Mesh constant buffer data uploaded to the pixel shader.
Definition: assimp_mesh.h:141
virtual void render(ID3D11DeviceContext *context, DirectX::XMFLOAT4X4 *to_clip=nullptr)
Renders the mesh using the shaders previously set.
Definition: assimp_mesh.cpp:243
size_t num_faces()
Returns the number of faces of a mesh.
Definition: assimp_mesh.cpp:83
An axis-aligned bounding-box.
Definition: mesh.h:28
Default implementation of assimp_mesh (pun intended).
Definition: assimp_mesh.h:113
virtual void destroy()
Destroy a mesh and free all its resources.
Definition: assimp_mesh.cpp:498
void init_bb(const DirectX::XMFLOAT3 &p)
Initialize the bounding-box with a first point.
Definition: mesh.h:43
Mesh constant buffer data uploaded to the vertex shader.
Definition: assimp_mesh.h:135
A wrapper class for a sampler state.
Definition: shader_resource.h:49
void prepare_context(ID3D11DeviceContext *context)
Prepares the context for rendering a gilga_mesh with the correct shader.
Definition: assimp_mesh.cpp:232
void render_direct(ID3D11DeviceContext *context, DirectX::XMFLOAT4X4 *to_clip)
Render the gilga_mesh without touching the current state, which is useful if the shader has been set ...
Definition: assimp_mesh.cpp:249
A wrapper for constant buffers.
Definition: cbuffer.h:27
void push_back(vertex v)
Push back a new vertex: this member needs to be overloaded in derived assimp_mesh objects...
Definition: assimp_mesh.cpp:220
virtual void destroy()
Destroy a mesh and free all its resources.
Definition: assimp_mesh.cpp:64
A gilga_vertex comes with position, normal, one texture coordinate and a tanget.
Definition: assimp_mesh.h:126
A helper struct to supply information about submeshes loaded by Assimp.
Definition: assimp_mesh.h:50
virtual void push_back(vertex v)=0
Push back a new vertex: this member needs to be overloaded in derived assimp_mesh objects...
Main D3D mesh interface class.
Definition: mesh.h:129
size_t num_vertices()
Returns the number of vertices of a mesh.
Definition: assimp_mesh.cpp:73