Ocean
OBJScene.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_SCENEDESCRIPTION_SDL_OBJ_OBJ_SCENE_H
9 #define META_OCEAN_SCENEDESCRIPTION_SDL_OBJ_OBJ_SCENE_H
10 
13 
15 
19 
20 namespace Ocean
21 {
22 
23 namespace SceneDescription
24 {
25 
26 namespace SDL
27 {
28 
29 namespace OBJ
30 {
31 
32 /**
33  * This class holds the entire scene contained in one obj file.
34  * @ingroup scenedescriptionsdlobj
35  */
36 class OCEAN_SCENEDESCRIPTION_SDL_OBJ_EXPORT OBJScene : public SDLScene
37 {
38  public:
39 
40  /**
41  * Definition of different face types.
42  */
43  enum FaceType
44  {
45  /// Face holding vertices only.
46  TYPE_V = 0,
47  /// Face holding vertices and normals.
48  TYPE_VN = 1,
49  /// Face holding vertices and texture coordinates.
50  TYPE_VT = 2,
51  /// Face holding vertices, texture coordinates and normals.
52  TYPE_VNT = TYPE_VN | TYPE_VT
53  };
54 
55  /**
56  * This class holds vertex, normal and texture indices for a triangle.
57  */
58  class Face
59  {
60  public:
61 
62  /**
63  * Creates a new face object.
64  * @param vertexIndices Vertex indices of the face
65  * @param normalIndices Normal indices of the face
66  * @param textureIndices Texture coordinate indices of the face
67  */
68  inline Face(const Rendering::VertexIndices& vertexIndices, const Rendering::VertexIndices& normalIndices, const Rendering::VertexIndices& textureIndices);
69 
70  /**
71  * Returns the face type of this object.
72  * @return Face type
73  */
74  inline FaceType type() const;
75 
76  /**
77  * Returns the vertex indices of the face.
78  * @return Face vertex indices
79  */
80  inline const Rendering::VertexIndices& vertexIndices() const;
81 
82  /**
83  * Returns the normal indices of the face.
84  * @return Face normal indices
85  */
86  inline const Rendering::VertexIndices& normalIndices() const;
87 
88  /**
89  * Returns the texture coordinate indices of the face.
90  * @return Face texture coordinate indices
91  */
92  inline const Rendering::VertexIndices& textureIndices() const;
93 
94  protected:
95 
96  /// Vertex indices.
98 
99  /// Noraml indices.
101 
102  /// Texture coordinate indices.
104  };
105 
106  /**
107  * Definition of a vector holding materials.
108  */
109  typedef std::vector<Material> Materials;
110 
111  /**
112  * Definition of a material index.
113  */
114  typedef unsigned int MaterialIndex;
115 
116  /**
117  * Definition of an invalid material index.
118  */
119  static constexpr MaterialIndex invalidMaterialIndex_ = OBJScene::MaterialIndex(-1);
120 
121  /**
122  * Definition of a map mapping material names to material indices.
123  */
124  typedef std::unordered_map<std::string, unsigned int> MaterialIndexMap;
125 
126  /**
127  * Definition of a vector holding faces.
128  */
129  typedef std::vector<Face> Faces;
130 
131  /**
132  * Definition of a pair of face type and material index.
133  */
134  typedef std::pair<FaceType, MaterialIndex> FacePair;
135 
136  /**
137  * Definition of a map mapping face pairs to faces.
138  */
139  typedef std::map<FacePair, Faces> FacesMap;
140 
141  public:
142 
143  /**
144  * Creates a new OBJ scene object.
145  * @param filename Scene filename
146  */
147  OBJScene(const std::string& filename);
148 
149  /**
150  * Destructs an OBJ scene object.
151  */
152  ~OBJScene() override;
153 
154  /**
155  * Returns the vertices of the entire obj scene.
156  * @return Scene vertices
157  */
158  inline const Rendering::Vertices& vertices() const;
159 
160  /**
161  * Returns the normals of the entire obj scene.
162  * @return Scene normals
163  */
164  inline const Rendering::Normals& normals() const;
165 
166  /**
167  * Returns the texture coordinates of the entire obj scene.
168  * @return Scene texture coordinates
169  */
170  inline const Rendering::TextureCoordinates& textureCoordinates() const;
171 
172  /**
173  * Returns all faces of the entire obj scene.
174  * @return The scene's faces
175  */
176  inline const FacesMap& facesMap() const;
177 
178  /**
179  * Sets the materials of the obj scene.
180  * @param materials The materials for the scene
181  */
182  void setMaterials(Materials&& materials);
183 
184  /**
185  * Adds a new vertex to the scene.
186  * @param vertex Vertex to add
187  */
188  inline void addVertex(const Rendering::Vertex& vertex);
189 
190  /**
191  * Adds a new normal to the scene.
192  * @param normal Normal to add
193  */
194  inline void addNormal(const Rendering::Normal& normal);
195 
196  /**
197  * Adds a new texture coordinate to the scene.
198  * @param textureCoordinate Texture coordinate to add
199  */
200  inline void addTextureCoordinate(const Rendering::TextureCoordinate& textureCoordinate);
201 
202  /**
203  * Adds a new face to the scene.
204  * @param face New face to add
205  */
206  void addFace(const Face& face);
207 
208  /**
209  * Sets the current selected material.
210  * @param material Current material name
211  */
212  void setCurrentMaterial(const std::string& material);
213 
214  protected:
215 
216  /**
217  * Applies the entire scene to the rendering engine.
218  * @see Scene::internalApply().
219  */
221 
222  /**
223  * Creates a new triangle mesh object.
224  * @param engine Rendering engine to use
225  * @return Group holding triangle meshes
226  */
228 
229  protected:
230 
231  /// Vector holding all vertices of the obj scene.
233 
234  /// Vector holding all normals of the obj scene.
236 
237  /// Vector holding all texture coordinate of the obj scene.
239 
240  /// Map holding all faces of the obj scene.
242 
243  /// All material objects of this scene.
245 
246  /// Map mapping material names to material indices.
248 
249  /// Current selected material index.
250  unsigned int selectedMaterialIndex_ = invalidMaterialIndex_;
251 };
252 
253 inline OBJScene::Face::Face(const Rendering::VertexIndices& vertexIndices, const Rendering::VertexIndices& normalIndices, const Rendering::VertexIndices& textureIndices) :
254  vertexIndices_(vertexIndices),
255  normalIndices_(normalIndices),
256  textureIndices_(textureIndices)
257 {
258  ocean_assert(vertexIndices.size() > 2);
259  ocean_assert(normalIndices_.empty() || normalIndices_.size() == vertexIndices.size());
260  ocean_assert(textureIndices_.empty() || textureIndices_.size() == vertexIndices.size());
261 }
262 
264 {
265  return FaceType((unsigned int)(!normalIndices_.empty()) | ((unsigned int)(!textureIndices_.empty()) << 1));
266 }
267 
269 {
270  return vertexIndices_;
271 }
272 
274 {
275  return normalIndices_;
276 }
277 
279 {
280  return textureIndices_;
281 }
282 
284 {
285  return vertices_;
286 }
287 
289 {
290  return normals_;
291 }
292 
294 {
295  return textureCoordinates_;
296 }
297 
299 {
300  return facesMap_;
301 }
302 
303 inline void OBJScene::addVertex(const Rendering::Vertex& vertex)
304 {
305  vertices_.emplace_back(vertex);
306 }
307 
308 inline void OBJScene::addNormal(const Rendering::Normal& normal)
309 {
310  normals_.emplace_back(normal);
311 }
312 
314 {
315  textureCoordinates_.emplace_back(textureCoordinate);
316 }
317 
318 }
319 
320 }
321 
322 }
323 
324 }
325 
326 #endif // META_OCEAN_SCENEDESCRIPTION_SDL_OBJ_OBJ_SCENE_H
This class holds vertex, normal and texture indices for a triangle.
Definition: OBJScene.h:59
Rendering::VertexIndices vertexIndices_
Vertex indices.
Definition: OBJScene.h:97
Rendering::VertexIndices textureIndices_
Texture coordinate indices.
Definition: OBJScene.h:103
const Rendering::VertexIndices & textureIndices() const
Returns the texture coordinate indices of the face.
Definition: OBJScene.h:278
Rendering::VertexIndices normalIndices_
Noraml indices.
Definition: OBJScene.h:100
const Rendering::VertexIndices & vertexIndices() const
Returns the vertex indices of the face.
Definition: OBJScene.h:268
const Rendering::VertexIndices & normalIndices() const
Returns the normal indices of the face.
Definition: OBJScene.h:273
FaceType type() const
Returns the face type of this object.
Definition: OBJScene.h:263
Face(const Rendering::VertexIndices &vertexIndices, const Rendering::VertexIndices &normalIndices, const Rendering::VertexIndices &textureIndices)
Creates a new face object.
Definition: OBJScene.h:253
This class holds the entire scene contained in one obj file.
Definition: OBJScene.h:37
Rendering::TextureCoordinates textureCoordinates_
Vector holding all texture coordinate of the obj scene.
Definition: OBJScene.h:238
void setCurrentMaterial(const std::string &material)
Sets the current selected material.
void addVertex(const Rendering::Vertex &vertex)
Adds a new vertex to the scene.
Definition: OBJScene.h:303
MaterialIndexMap materialIndexMap_
Map mapping material names to material indices.
Definition: OBJScene.h:247
OBJScene(const std::string &filename)
Creates a new OBJ scene object.
std::unordered_map< std::string, unsigned int > MaterialIndexMap
Definition of a map mapping material names to material indices.
Definition: OBJScene.h:124
FaceType
Definition of different face types.
Definition: OBJScene.h:44
Rendering::SceneRef internalApply(const Rendering::EngineRef &engine) override
Applies the entire scene to the rendering engine.
void addNormal(const Rendering::Normal &normal)
Adds a new normal to the scene.
Definition: OBJScene.h:308
void setMaterials(Materials &&materials)
Sets the materials of the obj scene.
std::map< FacePair, Faces > FacesMap
Definition of a map mapping face pairs to faces.
Definition: OBJScene.h:139
Rendering::Vertices vertices_
Vector holding all vertices of the obj scene.
Definition: OBJScene.h:232
const Rendering::Normals & normals() const
Returns the normals of the entire obj scene.
Definition: OBJScene.h:288
void addTextureCoordinate(const Rendering::TextureCoordinate &textureCoordinate)
Adds a new texture coordinate to the scene.
Definition: OBJScene.h:313
const Rendering::Vertices & vertices() const
Returns the vertices of the entire obj scene.
Definition: OBJScene.h:283
void addFace(const Face &face)
Adds a new face to the scene.
Rendering::Normals normals_
Vector holding all normals of the obj scene.
Definition: OBJScene.h:235
std::pair< FaceType, MaterialIndex > FacePair
Definition of a pair of face type and material index.
Definition: OBJScene.h:134
const Rendering::TextureCoordinates & textureCoordinates() const
Returns the texture coordinates of the entire obj scene.
Definition: OBJScene.h:293
unsigned int MaterialIndex
Definition of a material index.
Definition: OBJScene.h:114
std::vector< Material > Materials
Definition of a vector holding materials.
Definition: OBJScene.h:109
Rendering::NodeRef createTriangles(const Rendering::EngineRef &engine)
Creates a new triangle mesh object.
Materials materials_
All material objects of this scene.
Definition: OBJScene.h:244
std::vector< Face > Faces
Definition of a vector holding faces.
Definition: OBJScene.h:129
~OBJScene() override
Destructs an OBJ scene object.
FacesMap facesMap_
Map holding all faces of the obj scene.
Definition: OBJScene.h:241
const FacesMap & facesMap() const
Returns all faces of the entire obj scene.
Definition: OBJScene.h:298
This class implements the base class for all sdl scene object providing access to all elements of a s...
Definition: SDLScene.h:39
std::vector< Vertex > Vertices
Definition of a vector holding vertices.
Definition: rendering/Rendering.h:119
std::vector< TextureCoordinate > TextureCoordinates
Definition of a vector holding texture coordinates.
Definition: rendering/Rendering.h:113
std::vector< Normal > Normals
Definition of a vector holding normals.
Definition: rendering/Rendering.h:107
std::vector< VertexIndex > VertexIndices
Definition of a vector holding vertex indices.
Definition: rendering/Rendering.h:101
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15