ModelViewer 0.1
Template for CPP projects
Loading...
Searching...
No Matches
model_viewer::geometry::ObjGeometry Class Referencefinal

Class representing the geometry of an OBJ model. More...

#include <ObjGeometry.hpp>

Inheritance diagram for model_viewer::geometry::ObjGeometry:
Collaboration diagram for model_viewer::geometry::ObjGeometry:

Public Member Functions

 ObjGeometry (QQuick3DObject *parent=nullptr)
 Constructor.
 
void setMesh (const std::vector< Vector3 > &vertices, const std::vector< Vector3 > &normals, const std::vector< TextureCoordinate > &textureCoords)
 Set the mesh data.
 

Private Member Functions

void setModelData (const std::vector< Vector3 > &vertices, const std::vector< Vector3 > &normals, const std::vector< TextureCoordinate > &textureCoords)
 Set the model data to the geometry.
 
void configureVertexLayout ()
 Configure the vertex layout of the geometry based on the data.
 

Private Attributes

bool _hasNormals = false
 Whether the geometry contains normals.
 
bool _hasTextureCoords = false
 Whether the geometry contains texture coordinates.
 

Detailed Description

Class representing the geometry of an OBJ model.

Constructor & Destructor Documentation

◆ ObjGeometry()

model_viewer::geometry::ObjGeometry::ObjGeometry ( QQuick3DObject * parent = nullptr)
explicit

Constructor.

Parameters
parentParent QQuick3DObject, nullptr by default
12: QQuick3DGeometry(parent) {}

Member Function Documentation

◆ configureVertexLayout()

void model_viewer::geometry::ObjGeometry::configureVertexLayout ( )
private

Configure the vertex layout of the geometry based on the data.

59 {
60 int stride = 3 * sizeof(float);
61 int offset = 0;
62 addAttribute(Attribute::PositionSemantic, offset, Attribute::F32Type);
63 offset += 3 * sizeof(float);
64 if (_hasNormals) {
65 stride += 3 * sizeof(float);
66 addAttribute(Attribute::NormalSemantic, offset, Attribute::F32Type);
67 offset += 3 * sizeof(float);
68 }
70 stride += 2 * sizeof(float);
71 addAttribute(Attribute::TexCoord0Semantic, offset, Attribute::F32Type);
72 offset += 2 * sizeof(float);
73 }
74 setStride(stride);
75 setPrimitiveType(PrimitiveType::Triangles);
76}
bool _hasNormals
Whether the geometry contains normals.
Definition ObjGeometry.hpp:26
bool _hasTextureCoords
Whether the geometry contains texture coordinates.
Definition ObjGeometry.hpp:29

References _hasNormals, and _hasTextureCoords.

Referenced by setMesh().

Here is the caller graph for this function:

◆ setMesh()

void model_viewer::geometry::ObjGeometry::setMesh ( const std::vector< Vector3 > & vertices,
const std::vector< Vector3 > & normals,
const std::vector< TextureCoordinate > & textureCoords )

Set the mesh data.

Parameters
verticesVertices of the model
normalsNormals of the model
textureCoordsTexture coordinates of the model
16 {
17 clear();
18 _hasNormals = !normals.empty();
19 _hasTextureCoords = !textureCoords.empty();
20 setModelData(vertices, normals, textureCoords);
22 update();
23}
void setModelData(const std::vector< Vector3 > &vertices, const std::vector< Vector3 > &normals, const std::vector< TextureCoordinate > &textureCoords)
Set the model data to the geometry.
Definition ObjGeometry.cpp:25
void configureVertexLayout()
Configure the vertex layout of the geometry based on the data.
Definition ObjGeometry.cpp:59

References _hasNormals, _hasTextureCoords, configureVertexLayout(), and setModelData().

Here is the call graph for this function:

◆ setModelData()

void model_viewer::geometry::ObjGeometry::setModelData ( const std::vector< Vector3 > & vertices,
const std::vector< Vector3 > & normals,
const std::vector< TextureCoordinate > & textureCoords )
private

Set the model data to the geometry.

Parameters
verticesVertices of the model
normalsNormals of the model
textureCoordsTexture coordinates of the model
27 {
28 QByteArray vertexData;
29
30 const qsizetype vertexSize = static_cast<qsizetype>(vertices.size()) *
31 static_cast<qsizetype>(sizeof(Vector3));
32 const qsizetype normalSize = static_cast<qsizetype>(normals.size()) *
33 static_cast<qsizetype>(sizeof(Vector3));
34 const qsizetype textureSize =
35 static_cast<qsizetype>(textureCoords.size()) *
36 static_cast<qsizetype>(sizeof(TextureCoordinate));
37 vertexData.resize(vertexSize + normalSize + textureSize);
38 float *vertexDataPointer = reinterpret_cast<float *>(vertexData.data());
39 for (size_t i = 0; i < vertices.size(); ++i) {
40 const auto &[vx, vy, vz] = vertices[i];
41 *vertexDataPointer++ = vx;
42 *vertexDataPointer++ = vy;
43 *vertexDataPointer++ = vz;
44 if (_hasNormals) {
45 const auto &[nx, ny, nz] = normals[i];
46 *vertexDataPointer++ = nx;
47 *vertexDataPointer++ = ny;
48 *vertexDataPointer++ = nz;
49 }
51 const auto &[u, v] = textureCoords[i];
52 *vertexDataPointer++ = u;
53 *vertexDataPointer++ = v;
54 }
55 }
56 setVertexData(vertexData);
57}

References _hasNormals, and _hasTextureCoords.

Referenced by setMesh().

Here is the caller graph for this function:

Member Data Documentation

◆ _hasNormals

bool model_viewer::geometry::ObjGeometry::_hasNormals = false
private

Whether the geometry contains normals.

Referenced by configureVertexLayout(), setMesh(), and setModelData().

◆ _hasTextureCoords

bool model_viewer::geometry::ObjGeometry::_hasTextureCoords = false
private

Whether the geometry contains texture coordinates.

Referenced by configureVertexLayout(), setMesh(), and setModelData().


The documentation for this class was generated from the following files: