Ocean
Loading...
Searching...
No Matches
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
20namespace Ocean
21{
22
23namespace SceneDescription
24{
25
26namespace SDL
27{
28
29namespace OBJ
30{
31
32/**
33 * This class holds the entire scene contained in one obj file.
34 * @ingroup scenedescriptionsdlobj
35 */
36class OCEAN_SCENEDESCRIPTION_SDL_OBJ_EXPORT OBJScene : public SDLScene
37{
38 public:
39
40 /**
41 * Definition of different face types.
42 */
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 using Materials = std::vector<Material>;
110
111 /**
112 * Definition of a material index.
113 */
114 using MaterialIndex = unsigned int;
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 using MaterialIndexMap = std::unordered_map<std::string, unsigned int>;
125
126 /**
127 * Definition of a vector holding faces.
128 */
129 using Faces = std::vector<Face>;
130
131 /**
132 * Definition of a pair of face type and material index.
133 */
134 using FacePair = std::pair<FaceType, MaterialIndex>;
135
136 /**
137 * Definition of a map mapping face pairs to faces.
138 */
139 using FacesMap = std::map<FacePair, Faces>;
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 * Adds materials to the obj scene.
180 * An obj file may contain several `mtllib` statements, the materials of all of them accumulate.
181 * @param materials The materials to add to the scene
182 */
183 void addMaterials(Materials&& materials);
184
185 /**
186 * Adds a new vertex to the scene.
187 * @param vertex Vertex to add
188 */
189 inline void addVertex(const Rendering::Vertex& vertex);
190
191 /**
192 * Adds a new normal to the scene.
193 * @param normal Normal to add
194 */
195 inline void addNormal(const Rendering::Normal& normal);
196
197 /**
198 * Adds a new texture coordinate to the scene.
199 * @param textureCoordinate Texture coordinate to add
200 */
201 inline void addTextureCoordinate(const Rendering::TextureCoordinate& textureCoordinate);
202
203 /**
204 * Adds a new face to the scene.
205 * @param face New face to add
206 */
207 void addFace(const Face& face);
208
209 /**
210 * Sets the current selected material.
211 * @param material Current material name
212 */
213 void setCurrentMaterial(const std::string& material);
214
215 protected:
216
217 /**
218 * Applies the entire scene to the rendering engine.
219 * @see Scene::internalApply().
220 */
222
223 /**
224 * Creates a new triangle mesh object.
225 * @param engine Rendering engine to use
226 * @return Group holding triangle meshes
227 */
229
230 protected:
231
232 /// Vector holding all vertices of the obj scene.
234
235 /// Vector holding all normals of the obj scene.
237
238 /// Vector holding all texture coordinate of the obj scene.
240
241 /// Map holding all faces of the obj scene.
243
244 /// All material objects of this scene.
246
247 /// Map mapping material names to material indices.
249
250 /// Current selected material index.
251 unsigned int selectedMaterialIndex_ = invalidMaterialIndex_;
252};
253
254inline OBJScene::Face::Face(const Rendering::VertexIndices& vertexIndices, const Rendering::VertexIndices& normalIndices, const Rendering::VertexIndices& textureIndices) :
255 vertexIndices_(vertexIndices),
256 normalIndices_(normalIndices),
257 textureIndices_(textureIndices)
258{
259 ocean_assert(vertexIndices.size() > 2);
260 ocean_assert(normalIndices_.empty() || normalIndices_.size() == vertexIndices.size());
261 ocean_assert(textureIndices_.empty() || textureIndices_.size() == vertexIndices.size());
262}
263
265{
266 return FaceType((unsigned int)(!normalIndices_.empty()) | ((unsigned int)(!textureIndices_.empty()) << 1));
267}
268
270{
271 return vertexIndices_;
272}
273
275{
276 return normalIndices_;
277}
278
280{
281 return textureIndices_;
282}
283
285{
286 return vertices_;
287}
288
290{
291 return normals_;
292}
293
298
300{
301 return facesMap_;
302}
303
304inline void OBJScene::addVertex(const Rendering::Vertex& vertex)
305{
306 vertices_.emplace_back(vertex);
307}
308
309inline void OBJScene::addNormal(const Rendering::Normal& normal)
310{
311 normals_.emplace_back(normal);
312}
313
315{
316 textureCoordinates_.emplace_back(textureCoordinate);
317}
318
319}
320
321}
322
323}
324
325}
326
327#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:279
Rendering::VertexIndices normalIndices_
Noraml indices.
Definition OBJScene.h:100
const Rendering::VertexIndices & vertexIndices() const
Returns the vertex indices of the face.
Definition OBJScene.h:269
const Rendering::VertexIndices & normalIndices() const
Returns the normal indices of the face.
Definition OBJScene.h:274
FaceType type() const
Returns the face type of this object.
Definition OBJScene.h:264
Face(const Rendering::VertexIndices &vertexIndices, const Rendering::VertexIndices &normalIndices, const Rendering::VertexIndices &textureIndices)
Creates a new face object.
Definition OBJScene.h:254
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:239
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:304
void addMaterials(Materials &&materials)
Adds materials to the obj scene.
MaterialIndexMap materialIndexMap_
Map mapping material names to material indices.
Definition OBJScene.h:248
OBJScene(const std::string &filename)
Creates a new OBJ scene object.
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.
std::unordered_map< std::string, unsigned int > MaterialIndexMap
Definition of a map mapping material names to material indices.
Definition OBJScene.h:124
unsigned int MaterialIndex
Definition of a material index.
Definition OBJScene.h:114
void addNormal(const Rendering::Normal &normal)
Adds a new normal to the scene.
Definition OBJScene.h:309
Rendering::Vertices vertices_
Vector holding all vertices of the obj scene.
Definition OBJScene.h:233
std::vector< Material > Materials
Definition of a vector holding materials.
Definition OBJScene.h:109
const Rendering::Normals & normals() const
Returns the normals of the entire obj scene.
Definition OBJScene.h:289
void addTextureCoordinate(const Rendering::TextureCoordinate &textureCoordinate)
Adds a new texture coordinate to the scene.
Definition OBJScene.h:314
const Rendering::Vertices & vertices() const
Returns the vertices of the entire obj scene.
Definition OBJScene.h:284
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:236
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:294
std::map< FacePair, Faces > FacesMap
Definition of a map mapping face pairs to faces.
Definition OBJScene.h:139
std::vector< Face > Faces
Definition of a vector holding faces.
Definition OBJScene.h:129
Rendering::NodeRef createTriangles(const Rendering::EngineRef &engine)
Creates a new triangle mesh object.
Materials materials_
All material objects of this scene.
Definition OBJScene.h:245
~OBJScene() override
Destructs an OBJ scene object.
FacesMap facesMap_
Map holding all faces of the obj scene.
Definition OBJScene.h:242
const FacesMap & facesMap() const
Returns all faces of the entire obj scene.
Definition OBJScene.h:299
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< 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
std::vector< TextureCoordinate > TextureCoordinates
Definition of a vector holding texture coordinates.
Definition rendering/Rendering.h:113
std::vector< Vertex > Vertices
Definition of a vector holding vertices.
Definition rendering/Rendering.h:119
The namespace covering the entire Ocean framework.
Definition Accessor.h:15