CORGI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
rendermesh.h
Go to the documentation of this file.
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CORGI_COMPONENT_LIBRARY_RENDERMESH_H_
16 #define CORGI_COMPONENT_LIBRARY_RENDERMESH_H_
17 
18 #include "corgi/component.h"
21 #include "fplbase/asset_manager.h"
22 #include "fplbase/mesh.h"
23 #include "fplbase/renderer.h"
24 #include "fplbase/shader.h"
25 #include "library_components_generated.h"
26 #include "mathfu/constants.h"
27 #include "mathfu/glsl_mappings.h"
28 #include "mathfu/matrix_4x4.h"
29 
30 namespace corgi {
31 namespace component_library {
32 
33 /// @file
34 /// @addtogroup corgi_component_library
35 /// @{
36 ///
37 /// @struct RenderMeshData
38 ///
39 /// @brief The per-Entity mesh and shader data.
41  public:
42  /// @brief Default constructor for RenderMeshData.
44  : mesh(nullptr),
45  tint(mathfu::kOnes4f),
46  mesh_filename(""),
47  z_depth(0),
48  culling_mask(0),
49  pass_mask(0),
50  visible(true),
51  initialized(false),
52  default_pose(false),
54  shader_transforms(nullptr),
55  debug_name("") {}
56 
57  /// @brief Destructor for RenderMeshData.
59  delete[] shader_transforms;
60  shader_transforms = nullptr;
61  }
62 
63  /// @brief The move constructor for RenderMeshData to avoid reallocating
64  /// shader transforms.
65  ///
66  /// @param[in] other The other RenderMeshData whose data should be moved
67  /// into this RenderMeshData.
68  RenderMeshData(RenderMeshData&& other) { *this = std::move(other); }
69 
70  /// @brief The move assignment operator for RenderMeshData to avoid
71  /// reallocating shader transforms.
72  ///
73  /// @param[in] other The other RenderMeshData whose data should be moved
74  /// into this RenderMeshData.
75  ///
76  /// @return Returns a reference to the RenderMeshData that received the
77  /// data from `other`.
79  mesh = std::move(other.mesh);
80  shaders = std::move(other.shaders);
81  tint = std::move(other.tint);
82  mesh_filename = std::move(other.mesh_filename);
83  shader_filenames = std::move(other.shader_filenames);
84  z_depth = std::move(other.z_depth);
85  culling_mask = std::move(other.culling_mask);
86  pass_mask = std::move(other.pass_mask);
87  visible = std::move(other.visible);
88  initialized = std::move(other.initialized);
89  default_pose = std::move(other.default_pose);
90  num_shader_transforms = other.num_shader_transforms;
91  shader_transforms = other.shader_transforms;
92  other.shader_transforms = nullptr;
93  other.num_shader_transforms = 0;
94  debug_name = std::move(other.debug_name);
95  return *this;
96  }
97 
98  /// @brief The fplbase::Mesh for this Entity.
99  fplbase::Mesh* mesh;
100 
101  /// @brief A vector of fplbase::Shader for this Entity.
102  std::vector<fplbase::Shader*> shaders;
103 
104  /// @brief A mathfu::vec4 specifying the tinting for the Entity in RGBA.
105  mathfu::vec4 tint;
106 
107  /// @brief A std::string of the filename for the mesh, used for exporting.
108  std::string mesh_filename;
109 
110  /// @brief A std::string of the filenames for the shaders, used for exporting.
111  std::vector<std::string> shader_filenames;
112 
113  /// @brief The z distance corresponding to the depth of where the
114  /// Entity should be rendered.
115  float z_depth;
116 
117  /// @brief A bit field determining which types of culling are applied to the
118  /// Entity.
119  unsigned char culling_mask;
120 
121  /// @brief A bit field determining during which render passes to render this
122  /// Entity.
123  unsigned char pass_mask;
124 
125  /// @brief A bool determining if this Entity should be rendered.
126  bool visible;
127 
128  /// @brief A bool indicating if this RenderMesh is initialized.
130 
131  /// @brief A bool determining if the Entity should be rendered in the
132  /// default pose.
134 
135  /// @brief The number of shader transforms in the `shader_transforms`
136  /// array.
138 
139  /// @brief A mathfu::AffineTransform array that contains the shader
140  /// transforms.
141  mathfu::AffineTransform* shader_transforms;
142 
143  /// @brief The debug name of this mesh
144  std::string debug_name;
145 
146  private:
147  // Disallow copies. They're inefficient with the shader_transforms array.
150 };
151 
152 /// @brief Struct used for keeping track of and sorting our render lists.
154  /// @brief Constructor for RenderlistEntry.
156  : entity(entity_), data(data_) {}
157 
158  /// @brief The Entity associated with this RenderlistEntry.
160 
161  /// @brief The RenderMeshData associated with the `entity` in this
162  /// RenderlistEntry.
164 
165  /// @brief The greater than operator for RenderlistEntry.
166  ///
167  /// @param[in] other The other RenderlistEntry to compare if its
168  /// `RenderMeshData->z_depth` is greater than this RenderlistEntry's.
169  ///
170  /// @return Returns `true` if `other`'s `RenderMeshData->z_depth` is greater
171  /// than this RenderlistEntry's. Otherwise, it returns `false`.
172  bool operator<(const RenderlistEntry& other) const {
173  return (data->z_depth < other.data->z_depth);
174  }
175 
176  /// @brief The less than operator for RenderlistEntry.
177  ///
178  /// @param[in] other The other RenderlistEntry to compare if its
179  /// `RenderMeshData->z_depth` is less than this RenderlistEntry's.
180  ///
181  /// @return Returns `true` if `other`'s `RenderMeshData->z_depth` is less
182  /// than this RenderlistEntry's. Otherwise, it returns `false`.
183  bool operator>(const RenderlistEntry& other) const {
184  return (data->z_depth > other.data->z_depth);
185  }
186 };
187 
188 /// @brief A Component that handles the rendering of each Entities
189 /// mesh that is registered with this Component.
190 class RenderMeshComponent : public Component<RenderMeshData> {
191  public:
192  /// @brief The default distance to start culling.
193  static const int kDefaultCullDist = 80;
194 
195  /// @brief Default constructor for RenderMeshComponent.
197  : asset_manager_(nullptr),
198  culling_distance_squared_(kDefaultCullDist * kDefaultCullDist) {}
199 
200  /// @brief Destructor for RenderMeshComponent.
201  virtual ~RenderMeshComponent() {}
202 
203  /// @brief Initialize the RenderMeshComponent with the AssetManager from
204  /// the EntityManager.
205  virtual void Init();
206 
207  /// @brief Deserialize a flat binary buffer to create and populate an Entity
208  /// from raw data.
209  ///
210  /// @param[in,out] entity An EntityRef reference that points to the Entity
211  /// that is being added from the raw data.
212  /// @param[in] raw_data A void pointer to the raw FlatBuffer data.
213  virtual void AddFromRawData(EntityRef& entity, const void* raw_data);
214 
215  /// @brief Serializes a RenderMeshComponent's data for a given Entity.
216  ///
217  /// @param[in] entity An EntityRef reference that points to the Entity whose
218  /// corresponding RenderMeshData will be serialized.
219  ///
220  /// @return Returns a RawDataUniquePtr to the start of the raw data in a
221  /// flat binary buffer.
222  virtual RawDataUniquePtr ExportRawData(const EntityRef& entity) const;
223 
224  /// @brief Initialize an Entity by also adding it to the TransformComponent.
225  virtual void InitEntity(EntityRef& /*entity*/);
226 
227  /// @brief Nothing happens per frame for these Entities.
228  virtual void UpdateAllEntities(WorldTime /*delta_time*/) {}
229 
230  /// @brief Prepares to do rendering.
231  ///
232  /// @note Must be called before any `RenderPass()` calls.
233  ///
234  /// Pre-generates the draw lists, sorted by z-depth, and applies frustrum
235  /// culling. (So that `RenderPass` can just iterate through the list and draw
236  /// everything easily.)
237  ///
238  /// @param[in] camera A CameraInterface reference to the camera used to render
239  /// within the field of view.
240  void RenderPrep(const CameraInterface& camera);
241 
242  /// @brief Renders all Entities that are marked as being part of a given
243  /// render pass to the current output buffer.
244  ///
245  /// @param[in] pass_id An int used to identify and index the desired render
246  /// pass.
247  /// @param[in] camera A CameraInterface reference to the camera used to render
248  /// within the field of view.
249  /// @param[out] renderer A reference to the fplbase::Renderer to capture the
250  /// output of the render pass.
251  void RenderPass(int pass_id, const CameraInterface& camera,
252  fplbase::Renderer& renderer);
253 
254  /// @brief Renders all Entities that are marked as being part of a given
255  /// render pass to the current output buffer.
256  ///
257  /// @param[in] pass_id An int used to identify and index the desired render
258  /// pass.
259  /// @param[in] camera A CameraInterface reference to the camera used to render
260  /// within the field of view.
261  /// @param[out] renderer A reference to the fplbase::Renderer to capture the
262  /// output of the render pass.
263  /// @param[in] shader_index The shader index for the fplbase::Shader to use as
264  /// the mesh shader for this render pass.
265  void RenderPass(int pass_id, const CameraInterface& camera,
266  fplbase::Renderer& renderer,
267  size_t shader_index);
268 
269  /// @brief Goes through and renders every Entity that is visible from the
270  /// camera, in pass order.
271  ///
272  /// It Is equivalent to iterating through all of the passes and calling
273  /// `RenderPass()` on each one.
274  ///
275  /// @warning You must have called `RenderPrep()` first, in order to
276  /// pre-populate the lists of things visible from the camera!
277  ///
278  /// @param[out] renderer A reference to the fplbase::Renderer to capture the
279  /// output of the render pass.
280  /// @param[in] camera A CameraInterface reference to the camera used to
281  /// render within the field of view.
282  void RenderAllEntities(fplbase::Renderer& renderer,
283  const CameraInterface& camera);
284 
285  /// @brief Recursively sets the visibility of the Entity and all of its
286  /// children.
287  ///
288  /// @param[in] entity An EntityRef reference to the Entity that should have
289  /// itself and its children's visibility set.
290  /// @param[in] visible A bool determining if the Entities should be rendered.
291  void SetVisibilityRecursively(const EntityRef& entity, bool visible);
292 
293  /// @brief Get the light position uniform.
294  ///
295  /// @note This is a special uniform that is sent to all shaders without having
296  /// to declare it explicitly in the shader.
297  ///
298  /// @return Returns the light position uniform as a mathfu::vec3.
299  mathfu::vec3 light_position() { return light_position_; }
300 
301  /// @brief Set the light position uniform.
302  ///
303  /// @note This is a special uniform that is sent to all shaders without having
304  /// to declare it explicitly in the shader.
305  ///
306  /// @param[in] light_position A const mathfu::vec3 reference to the uniform to
307  /// set for the light position.
308  void set_light_position(const mathfu::vec3& light_position) {
309  light_position_ = light_position;
310  }
311 
312  /// @brief Set the culling distance.
313  ///
314  /// @param[in] distance A float representing the new culling distance to set.
315  void SetCullDistance(float distance) {
316  culling_distance_squared_ = distance * distance;
317  }
318 
319  /// @brief Get the culling distance squared.
320  ///
321  /// @return Returns the square of the culling distance.
322  float culling_distance_squared() const { return culling_distance_squared_; }
323 
324  /// @brief Set the square of the culling distance.
325  ///
326  /// @param[in] culling_distance_squared A float representing the square of the
327  /// culling distance that should be set.
329  culling_distance_squared_ = culling_distance_squared;
330  }
331 
332  private:
333  // Finalize the initialization of RenderMeshData if it's not completed yet.
334  // This function should be called right after the corresponding mesh is
335  // loaded.
336  void FinalizeRenderMeshDataIfRequired(RenderMeshData* rendermesh_data);
337 
338  // todo(ccornell) expand this if needed - make an array for multiple lights.
339  // Also maybe make this into a full fledged struct to store things like
340  // intensity, color, etc. (Low priority - none of our shaders support
341  // these.)
342  mathfu::vec3 light_position_;
343  fplbase::AssetManager* asset_manager_;
344  float culling_distance_squared_;
345  // An array of vectors we use for keeping track of things we're going
346  // to render.
347  std::vector<RenderlistEntry> pass_render_list_[RenderPass_Count];
348 };
349 
350 /// @}
351 
352 } // component_library
353 } // corgi
354 
357 
358 #endif // CORGI_COMPONENT_LIBRARY_RENDERMESH_H_
std::unique_ptr< uint8_t, std::function< void(uint8_t *)> > RawDataUniquePtr
A pointer type for exported raw data.
Definition: component_interface.h:63
float z_depth
The z distance corresponding to the depth of where the Entity should be rendered. ...
Definition: rendermesh.h:115
mathfu::AffineTransform * shader_transforms
A mathfu::AffineTransform array that contains the shader transforms.
Definition: rendermesh.h:141
std::vector< fplbase::Shader * > shaders
A vector of fplbase::Shader for this Entity.
Definition: rendermesh.h:102
static const int kDefaultCullDist
The default distance to start culling.
Definition: rendermesh.h:193
virtual ~RenderMeshComponent()
Destructor for RenderMeshComponent.
Definition: rendermesh.h:201
A Component is an object that encapsulates all data and logic for Entities of a particular type...
Definition: component.h:43
The per-Entity mesh and shader data.
Definition: rendermesh.h:40
RenderlistEntry(EntityRef entity_, RenderMeshData *data_)
Constructor for RenderlistEntry.
Definition: rendermesh.h:155
bool operator<(const RenderlistEntry &other) const
The greater than operator for RenderlistEntry.
Definition: rendermesh.h:172
virtual void UpdateAllEntities(WorldTime)
Nothing happens per frame for these Entities.
Definition: rendermesh.h:228
RenderMeshData & operator=(RenderMeshData &&other)
The move assignment operator for RenderMeshData to avoid reallocating shader transforms.
Definition: rendermesh.h:78
EntityRef entity
The Entity associated with this RenderlistEntry.
Definition: rendermesh.h:159
virtual RawDataUniquePtr ExportRawData(const EntityRef &entity) const
Serializes a RenderMeshComponent's data for a given Entity.
int WorldTime
A typedef that represents time in the game.
Definition: entity_common.h:49
Struct used for keeping track of and sorting our render lists.
Definition: rendermesh.h:153
RenderMeshData()
Default constructor for RenderMeshData.
Definition: rendermesh.h:43
RenderMeshComponent()
Default constructor for RenderMeshComponent.
Definition: rendermesh.h:196
bool operator>(const RenderlistEntry &other) const
The less than operator for RenderlistEntry.
Definition: rendermesh.h:183
virtual void Init()
Initialize the RenderMeshComponent with the AssetManager from the EntityManager.
float culling_distance_squared() const
Get the culling distance squared.
Definition: rendermesh.h:322
bool default_pose
A bool determining if the Entity should be rendered in the default pose.
Definition: rendermesh.h:133
A Component that handles the rendering of each Entities mesh that is registered with this Component...
Definition: rendermesh.h:190
void set_light_position(const mathfu::vec3 &light_position)
Set the light position uniform.
Definition: rendermesh.h:308
virtual void InitEntity(EntityRef &)
Initialize an Entity by also adding it to the TransformComponent.
mathfu::vec3 light_position()
Get the light position uniform.
Definition: rendermesh.h:299
void RenderPrep(const CameraInterface &camera)
Prepares to do rendering.
uint8_t num_shader_transforms
The number of shader transforms in the shader_transforms array.
Definition: rendermesh.h:137
std::vector< std::string > shader_filenames
A std::string of the filenames for the shaders, used for exporting.
Definition: rendermesh.h:111
virtual void AddFromRawData(EntityRef &entity, const void *raw_data)
Deserialize a flat binary buffer to create and populate an Entity from raw data.
~RenderMeshData()
Destructor for RenderMeshData.
Definition: rendermesh.h:58
std::string debug_name
The debug name of this mesh.
Definition: rendermesh.h:144
A reference object for pointing into the vector pool. It acts as a pointer for vector pool elements a...
Definition: vector_pool.h:72
void RenderPass(int pass_id, const CameraInterface &camera, fplbase::Renderer &renderer)
Renders all Entities that are marked as being part of a given render pass to the current output buffe...
void SetCullDistance(float distance)
Set the culling distance.
Definition: rendermesh.h:315
unsigned char pass_mask
A bit field determining during which render passes to render this Entity.
Definition: rendermesh.h:123
void SetVisibilityRecursively(const EntityRef &entity, bool visible)
Recursively sets the visibility of the Entity and all of its children.
An interface for 3D cameras, allowing them to have position, facing field of view, etc. Libraries can use this to pass around generic cameras that the game itself can implement the logic for.
Definition: camera_interface.h:31
std::string mesh_filename
A std::string of the filename for the mesh, used for exporting.
Definition: rendermesh.h:108
void RenderAllEntities(fplbase::Renderer &renderer, const CameraInterface &camera)
Goes through and renders every Entity that is visible from the camera, in pass order.
RenderMeshData(RenderMeshData &&other)
The move constructor for RenderMeshData to avoid reallocating shader transforms.
Definition: rendermesh.h:68
fplbase::Mesh * mesh
The fplbase::Mesh for this Entity.
Definition: rendermesh.h:99
#define CORGI_REGISTER_COMPONENT(ComponentType, DataType)
Definition: component_id_lookup.h:48
unsigned char culling_mask
A bit field determining which types of culling are applied to the Entity.
Definition: rendermesh.h:119
bool visible
A bool determining if this Entity should be rendered.
Definition: rendermesh.h:126
mathfu::vec4 tint
A mathfu::vec4 specifying the tinting for the Entity in RGBA.
Definition: rendermesh.h:105
void set_culling_distance_squared(float culling_distance_squared)
Set the square of the culling distance.
Definition: rendermesh.h:328
RenderMeshData * data
The RenderMeshData associated with the entity in this RenderlistEntry.
Definition: rendermesh.h:163
bool initialized
A bool indicating if this RenderMesh is initialized.
Definition: rendermesh.h:129