C

Q3DSGeometry Class

Represents a mesh geometry. More...

Header: #include <Q3DSGeometry>
Since: Qt 3D Studio 2.4

This class was introduced in Qt 3D Studio 2.4.

Public Types

enum PrimitiveType { UnknownType, Points, LineStrip, LineLoop, Lines, …, Patches }

Public Functions

Q3DSGeometry()
virtual ~Q3DSGeometry()
void addAttribute(Attribute::Semantic semantic, Attribute::ComponentType componentType = Attribute::DefaultType)
void addAttribute(const Q3DSGeometry::Attribute &att)
Q3DSGeometry::Attribute attribute(int idx) const
int attributeCount() const
void clear()
const QByteArray &indexBuffer() const
QByteArray &indexBuffer()
Q3DSGeometry::PrimitiveType primitiveType() const
void setIndexData(const QByteArray &data)
void setPrimitiveType(Q3DSGeometry::PrimitiveType type)
void setVertexData(const QByteArray &data)
const QByteArray &vertexBuffer() const
QByteArray &vertexBuffer()

Detailed Description

This class describes the mesh geometry for dynamic mesh creation. The geometry consists of a vertex buffer and an optional index buffer. Geometry attributes are used to define how the data in these buffers should be interpreted.

For example, create a simple textured pyramid geometry:

// A vertex in vertex buffer consists of position, normal, and texture coordinates
struct Vertex {
    QVector3D position;
    QVector3D normal;
    QVector2D uv;
};

// The vertex buffer
QVector<Vertex> vertices;

// Creates a triangle into the vertex buffer
auto createTriangle = [&](const QVector3D &xyz1, const QVector2D &uv1,
                          const QVector3D &xyz2, const QVector2D &uv2,
                          const QVector3D &xyz3, const QVector2D &uv3) {
    QVector3D n = QVector3D::crossProduct(xyz2 - xyz1, xyz3 - xyz1).normalized();
    vertices.append({xyz1, n, uv1});
    vertices.append({xyz2, n, uv2});
    vertices.append({xyz3, n, uv3});
};

// Pyramid corner coordinates in local space
QVector3D xyz[5] = {{0, 0, 50}, {50, 50, -50}, {50, -50, -50}, {-50, -50, -50}, {-50, 50, -50}};

// Possible texture coordinates for triangle corners
QVector2D uv[4] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};

// Pyramid consists of four side triangles and a bottom quad made of two triangles
createTriangle(xyz[0], uv[0], xyz[1], uv[1], xyz[2], uv[2]);
createTriangle(xyz[0], uv[0], xyz[2], uv[1], xyz[3], uv[2]);
createTriangle(xyz[0], uv[0], xyz[3], uv[1], xyz[4], uv[2]);
createTriangle(xyz[0], uv[0], xyz[4], uv[1], xyz[1], uv[2]);
createTriangle(xyz[1], uv[0], xyz[4], uv[2], xyz[3], uv[1]);
createTriangle(xyz[1], uv[0], xyz[3], uv[3], xyz[2], uv[2]);

// Make a byte array out of the vertex buffer
QByteArray vertexBuffer(reinterpret_cast<const char *>(vertices.constData()),
                        vertices.size() * int(sizeof(Vertex)));

// Create the geometry. Triangle is the default primitive type, so we don't specify it.
// The order of the added attributes must match the order of the attribute data in the
// vertex buffer.
Q3DSGeometry pyramid;
pyramid.setVertexData(vertexBuffer);
pyramid.addAttribute(Q3DSGeometry::Attribute::PositionSemantic);
pyramid.addAttribute(Q3DSGeometry::Attribute::NormalSemantic);
pyramid.addAttribute(Q3DSGeometry::Attribute::TexCoordSemantic);

See also Q3DSPresentation::createMesh.

Member Type Documentation

enum Q3DSGeometry::PrimitiveType

This enumeration specifies the possible rendering primitives for the geometry. For more information about rendering primitives and how they affect the vertex data, see OpenGL documentation.

ConstantValueDescription
Q3DSGeometry::UnknownType0Primitive type is unknown.
Q3DSGeometry::Points1Geometry uses point primitives.
Q3DSGeometry::LineStrip2Geometry uses line strip primitives.
Q3DSGeometry::LineLoop3Geometry uses line loop primitives.
Q3DSGeometry::Lines4Geometry uses line primitives.
Q3DSGeometry::TriangleStrip5Geometry uses triangle strip primitives.
Q3DSGeometry::TriangleFan6Geometry uses triangle fan primitives.
Q3DSGeometry::Triangles7Geometry uses triangle primitives. This is the default primitive type.
Q3DSGeometry::Patches8Geometry uses patch primitives.

Member Function Documentation

Q3DSGeometry::Q3DSGeometry()

Constructs a new Q3DSGeometry instance.

[virtual] Q3DSGeometry::~Q3DSGeometry()

Destructor.

void Q3DSGeometry::addAttribute(Attribute::Semantic semantic, Attribute::ComponentType componentType = Attribute::DefaultType)

Sets an attribute to this geometry. The geometry attributes specify how the data in vertex and index buffers should be interpreted. Each attribute is composed of a semantic, which indicates which vertex attribute this attribute refers to and componentType, which indicates the data type of each component of the attribute data. The semantic also determines the component count in vertex buffer for the attribute. The component count is two for TexCoordSemantic and three for other vertex buffer semantics. Component count for index buffer is always one.

For example, PositionSemantic specifies the vertex position in local space, so it is composed of three components: x, y, and z-coordinates.

The order of addAttribute calls must match the order of the attributes in vertex data. The order is relevant as it is used to calculate the offset and stride of each attribute.

void Q3DSGeometry::addAttribute(const Q3DSGeometry::Attribute &att)

Sets an attribute att to this geometry.

See also addAttribute.

Q3DSGeometry::Attribute Q3DSGeometry::attribute(int idx) const

Returns an added attribute with index idx.

See also addAttribute and attributeCount.

int Q3DSGeometry::attributeCount() const

Returns the number of attributes set to this geometry.

See also addAttribute.

void Q3DSGeometry::clear()

Removes all added attributes and buffers and resets the geometry to an uninitialized state.

See also primitiveType.

const QByteArray &Q3DSGeometry::indexBuffer() const

Returns the currently set index buffer data.

See also setIndexData.

QByteArray &Q3DSGeometry::indexBuffer()

This is an overloaded function.

Q3DSGeometry::PrimitiveType Q3DSGeometry::primitiveType() const

Returns the primitive type of this geometry.

See also setPrimitiveType.

void Q3DSGeometry::setIndexData(const QByteArray &data)

Sets the index buffer to data. You must also add an attribute with IndexSemantic to the geometry.

See also addAttribute, indexBuffer, and Attribute::Semantic.

void Q3DSGeometry::setPrimitiveType(Q3DSGeometry::PrimitiveType type)

Sets the primitive type of this geometry to type.

See also primitiveType.

void Q3DSGeometry::setVertexData(const QByteArray &data)

Sets the vertex buffer to data. The data must contain all attribute data in interleaved format. You must also add attributes to the geometry to specify how the vertex buffer data should be interpreted.

See also addAttribute and vertexBuffer.

const QByteArray &Q3DSGeometry::vertexBuffer() const

Returns the currently set vertex buffer data.

See also setVertexData.

QByteArray &Q3DSGeometry::vertexBuffer()

This is an overloaded function.

Available under certain Qt licenses.
Find out more.