Ejemplo n.º 1
0
void tst_QVectorArray::create3DArray()
{
    QVector3DArray array;
    QVERIFY(array.isEmpty());

    array.append(1.0f, 2.0f, 3.0f);
    array.append(3.0f, 4.0f, 5.0f);
    array.append(QVector3D(5.0f, 6.0f, 7.0f));

    QCOMPARE(array.size(), 3);
    QVERIFY(array[0] == QVector3D(1.0f, 2.0f, 3.0f));
    QVERIFY(array[1] == QVector3D(3.0f, 4.0f, 5.0f));
    QVERIFY(array[2] == QVector3D(5.0f, 6.0f, 7.0f));

    array.append(QVector3D(7.0f, 8.0f, 9.0f),
                 QVector3D(9.0f, 10.0f, 11.0f));
    array.append(QVector3D(11.0f, 12.0f, 13.0f),
                 QVector3D(13.0f, 14.0f, 15.0f),
                 QVector3D(15.0f, 16.0f, 17.0f));
    array.append(QVector3D(17.0f, 18.0f, 19.0f),
                 QVector3D(19.0f, 20.0f, 21.0f),
                 QVector3D(21.0f, 22.0f, 23.0f));

    for (int index = 0; index < array.size(); ++index) {
        QVERIFY(array[index] == QVector3D(index * 2 + 1,
                                          index * 2 + 2,
                                          index * 2 + 3));
    }

    QVector3DArray array2(34);
    QCOMPARE(array2.size(), 34);
    for (int index = 0; index < array2.size(); ++index)
        QCOMPARE(array2[index], QVector3D(0.0f, 0.0f, 0.0f));

    QVector3DArray array3(15, QVector3D(1.0f, 2.0f, 3.0f));
    QCOMPARE(array3.size(), 15);
    for (int index = 0; index < array3.size(); ++index)
        QCOMPARE(array3[index], QVector3D(1.0f, 2.0f, 3.0f));
}
Ejemplo n.º 2
0
void tst_QGLBuilder::addQuadBenchMarks(const QVector3DArray &data, int type)
{
    int size = data.size();
    if (type == Test_3)
    {
        QBENCHMARK {
            TestBuilder builder;
            builder.newSection(QGL::Smooth);
            builder.section()->setMapThreshold(3);
            for (int i = 0; (i+3) < size; i += 4)
            {
                QGeometryData op;
                op.appendVertex(data[i], data[i+1], data[i+2], data[i+3]);
                builder.addQuads(op);
            }
            builder.finalizedSceneNode();
        }
    }
Ejemplo n.º 3
0
void QGLBezierPatchesPrivate::subdivide(QGLBuilder *list) const
{
    QGeometryData prim;
    int count = positions.size();
    for (int posn = 0; (posn + 15) < count; posn += 16) {
        // Construct a QGLBezierPatch object from the next high-level patch.
        QGLBezierPatch patch;
        int vertex;
        for (int vertex = 0; vertex < 16; ++vertex)
            patch.points[vertex] = positions[posn + vertex];
        QVector2D tex1, tex2;
        if (!textureCoords.isEmpty()) {
            tex1 = textureCoords[(posn / 16) * 2];
            tex2 = textureCoords[(posn / 16) * 2 + 1];
        } else {
            tex1 = QVector2D(0.0f, 0.0f);
            tex2 = QVector2D(1.0f, 1.0f);
        }
        qreal xtex = tex1.x();
        qreal ytex = tex1.y();
        qreal wtex = tex2.x() - xtex;
        qreal htex = tex2.y() - ytex;
        for (int corner = 0; corner < 4; ++corner) {
            vertex = posn + cornerOffsets[corner];
            QVector3D n = patch.normal(cornerS[corner], cornerT[corner]);
            patch.indices[corner] = prim.count();
            prim.appendVertex(patch.points[cornerOffsets[corner]]);
            prim.appendNormal(n);
            prim.appendTexCoord
                (QVector2D(xtex + wtex * cornerS[corner],
                           ytex + htex * cornerT[corner]));
        }

        // Subdivide the patch and generate the final triangles.
        patch.recursiveSubDivide(&prim, subdivisionDepth,
                                 xtex, ytex, wtex, htex);
    }
    list->addTriangles(prim);
}
Ejemplo n.º 4
0
qreal QGLBezierPatchesPrivate::intersection
    (const QRay3D &ray, bool anyIntersection, QVector2D *texCoord, int *bestPatch) const
{
    int count = positions.size();
    qreal result = qSNaN();
    QVector2D tc;
    if (bestPatch)
        *bestPatch = -1;
    for (int posn = 0; (posn + 15) < count; posn += 16) {
        QGLBezierPatch patch;
        for (int vertex = 0; vertex < 16; ++vertex)
            patch.points[vertex] = positions[posn + vertex];
        QVector2D tex1, tex2;
        if (!textureCoords.isEmpty()) {
            tex1 = textureCoords[(posn / 16) * 2];
            tex2 = textureCoords[(posn / 16) * 2 + 1];
        } else {
            tex1 = QVector2D(0.0f, 0.0f);
            tex2 = QVector2D(1.0f, 1.0f);
        }
        qreal xtex = tex1.x();
        qreal ytex = tex1.y();
        qreal wtex = tex2.x() - xtex;
        qreal htex = tex2.y() - ytex;
        qreal prev = result;
        result = patch.intersection
            (result, subdivisionDepth, ray, anyIntersection,
             xtex, ytex, wtex, htex, &tc);
        if (bestPatch && result != prev)
            *bestPatch = posn / 16;
        if (anyIntersection && !qIsNaN(result))
            break;
    }
    if (texCoord && !qIsNaN(result))
        *texCoord = tc;
    return result;
}
Ejemplo n.º 5
0
void tst_QVectorArray::vector3DArray()
{
    QVector3DArray array;
    QVERIFY(array.isEmpty());

    array.append(1.0f, 2.0f, 3.0f);
    array.append(3.0f, 4.0f, 5.0f);
    array.append(QVector3D(5.0f, 6.0f, 7.0f));

    QCOMPARE(array.size(), 3);
    QVERIFY(array[0] == QVector3D(1.0f, 2.0f, 3.0f));
    QVERIFY(array[1] == QVector3D(3.0f, 4.0f, 5.0f));
    QVERIFY(array[2] == QVector3D(5.0f, 6.0f, 7.0f));

    array.append(QVector3D(7.0f, 8.0f, 9.0f),
                 QVector3D(9.0f, 10.0f, 11.0f));
    array.append(QVector3D(11.0f, 12.0f, 13.0f),
                 QVector3D(13.0f, 14.0f, 15.0f),
                 QVector3D(15.0f, 16.0f, 17.0f));
    array.append(QVector3D(17.0f, 18.0f, 19.0f),
                 QVector3D(19.0f, 20.0f, 21.0f),
                 QVector3D(21.0f, 22.0f, 23.0f));

    for (int index = 0; index < array.size(); ++index) {
        QVERIFY(array[index] == QVector3D(index * 2 + 1,
                                          index * 2 + 2,
                                          index * 2 + 3));
    }

    int size = array.size();
    QVector3DArray result = array.scaled(1.0);
    // check did not change the original
    QCOMPARE(array.size(), size);
    QCOMPARE(array[0], QVector3D(1.0f, 2.0f, 3.0));
    QCOMPARE(array[4], QVector3D(9.0f, 10.0f, 11.0f));
    // result should be copy - mult by 1.0 costs nothing
    QVERIFY(!result.isDetached());
    QCOMPARE(result.size(), size);
    QCOMPARE(result[0], QVector3D(1.0f, 2.0f, 3.0f));
    QCOMPARE(result[4], QVector3D(9.0f, 10.0f, 11.0f));
    QCOMPARE(result[10], QVector3D(21.0f, 22.0f, 23.0f));
    // now actually do a scale
    result = array.scaled(2.0);
    QCOMPARE(result.size(), size);
    QCOMPARE(array.size(), size);
    QCOMPARE(result[0], QVector3D(2.0f, 4.0f, 6.0f));
    QCOMPARE(result[4], QVector3D(18.0f, 20.0f, 22.0f));
    QCOMPARE(result[10], QVector3D(42.0f, 44.0f, 46.0f));

    array.scale(1.0);
    QCOMPARE(array.size(), size);  // should all be the same
    QCOMPARE(array[0], QVector3D(1.0f, 2.0f, 3.0f));
    QCOMPARE(array[4], QVector3D(9.0f, 10.0f, 11.0f));
    QCOMPARE(array[10], QVector3D(21.0f, 22.0f, 23.0f));

    array.scale(2.0);
    QCOMPARE(array.size(), size);  // size should be the same
    QCOMPARE(array[0], QVector3D(2.0f, 4.0f, 6.0f));
    QCOMPARE(array[4], QVector3D(18.0f, 20.0f, 22.0f));
    QCOMPARE(array[10], QVector3D(42.0f, 44.0f, 46.0f));
}
Ejemplo n.º 6
0
static void drawBox(QGLPainter *painter, float width, float height, float depth)
{
    QVector3D const vertices[] = {
        /* 0 = 000 */ QVector3D( width,  height,  depth),
        /* 1 = 001 */ QVector3D( width,  height, -depth),
        /* 2 = 010 */ QVector3D( width, -height,  depth),
        /* 3 = 011 */ QVector3D( width, -height, -depth),
        /* 4 = 100 */ QVector3D(-width,  height,  depth),
        /* 5 = 101 */ QVector3D(-width,  height, -depth),
        /* 6 = 110 */ QVector3D(-width, -height,  depth),
        /* 7 = 111 */ QVector3D(-width, -height, -depth),
    };

#if 0
    static int const indices[][2] = {
        { 0, 1 },
        { 0, 2 },
        { 0, 4 },
        { 1, 3 },
        { 1, 5 },
        { 2, 3 },
        { 2, 6 },
        { 3, 7 },
        { 4, 5 },
        { 4, 6 },
        { 5, 7 },
        { 6, 7 },
    };
#else
    static int const indices[][2] = {
        // back
        { 7, 3 },
        { 3, 1 },
        { 1, 5 },
        { 5, 7 },

        // front
        { 6, 2 },
        { 2, 0 },
        { 0, 4 },
        { 4, 6 },

        // right side
        { 1, 0 },
        { 2, 3 },

        // left side
        { 5, 4 },
        { 6, 7 },
    };
#endif

    QVector3DArray vertexData;

    for (size_t i = 0; i < sizeof indices / sizeof indices[1]; i++)
    {
        vertexData
            << vertices[indices[i][0]]
            << vertices[indices[i][1]];
    }

    int lineCount = vertexData.size();

    painter->setColor(Qt::white);
    painter->setVertexAttribute(QGL::Position, vertexData);
    painter->draw(QGL::Lines, lineCount);
    painter->setColor(Qt::white);
}
Ejemplo n.º 7
0
QT_BEGIN_NAMESPACE

/*!
    \class QGLCylinder
    \brief The QGLCylinder class represents the geometry of a simple cylinder/cone in 3D space.
    \since 4.8
    \ingroup qt3d
    \ingroup qt3d::geometry

    The following example creates a cone with a top diameter of 1 unit,
    a bottom diameter of 2 units in diameter and height of 3 units.

    It then draws it at (10, 25, 0) in a QGLPainter:

    \code
    QGLBuilder builder;
    builder << QGLCylinder(1.0,2.0,3.0);
    QGLSceneNode *node = builder.finalizedSceneNode();

    painter.translate(10, 25, 0);
    node->draw(&painter);
    \endcode

    Note that the bottom circle of the cylinder will always be centred at (0,0,0)
    unless otherwise transformed after cylinder creation.

    The QGLCylinder class specifies positions, normals and 2D texture
    co-ordinates for all of the vertices that make up the cylinder.

    The texture co-ordinates are fixed at construction time.  This
    is because constructing the cylinder can involve generating additional
    vertices which need to interpolate the texture co-ordinates of their
    neighboring vertices.

    The QGLCylinder is divided into slices and layers.  The slices value
    indicate number of triangular sections into which the top and bottom
    circles of the cylinder are broken into.  Consequently it also sets the
    number of facets which run the length of the cylinder.  More slices
    results in a smoother circumference.

    The layers value indicates the number of longitudinal sections the
    cylinder is broken into.  Fewer layers means that the side facets of the
    cylinder will be made up of fewer, very long, triangles, while a higher
    number of layers will produce many and smaller triangles.  Often it is
    desirable to avoid large triangles as they may cause inefficiencies in
    texturing/lighting on certain platforms.

    The end-caps and sides of the cylinder are independent sections of the
    scene-graph, and so may be textured separately.

    Textures are wrapped around the sides of thecylinder in such a way that
    the texture may distort across the x axis if the top and bottom diameters
    of the cylinder differ (ie. the cylinder forms a truncated cone).  Textures
    begin and end at the centre points of the top and bottom end-caps of the
    cylinder.  This wrapping means that textures on either end-cap may be
    distorted.

    Texture coordinates are assigned as shown below.

    \image cylinder-texture-coords.png

    It is worth noting that the cylinder class can, in fact, be used to generate
    any regular solid polygonal prism.  A rectangular prism can be created, for
    example, by creating a 4 sided cylinder.  Likewise a hexagonal prism is
    simply a 6 sided cylinder.

    With this knowledge, and an understanding of the texture coordinate mapping,
    it is possible to make custom textures which will be usable with these
    three dimensional objects.

    \sa QGLBuilder
*/


/*!
    \fn QGLCylinder::QGLCylinder(float diameterTop, float diameterBase , float height, int slices, int layers, bool top, bool base)

    Constructs the geometry for a cylinder with top of diameter \a diameterTop,
    a base of diameter \a diameterBase, and a height of \a height.

    The resultant mesh will be divided around the vertical axis of the cylinder
    into \a slices individual wedges, and shall be formed of \a layers stacked
    to form the cylinder.

    If the values for \a top or \a base are true, then the cylinder will be
    created with solid endcaps.  Otherwise, it shall form a hollow pipe.

    units on a side.
*/


/*!
    \fn float QGLCylinder::diameterTop() const

    Returns the diameter of the top of the cylinder.

    The default value is 1.

    \sa setDiameterTop()
*/

/*!
    \fn void QGLCylinder::setDiameterTop(float diameter)

    Sets the diameter of the top of this cylinder to \a diameter.

    \sa diameterTop()
*/

/*!
    \fn float QGLCylinder::diameterBottom() const

    Returns the diameter of the bottom of the cylinder.

    The default value is 1.

    \sa setDiameterBottom()
*/

/*!
    \fn void QGLCylinder::setDiameterBottom(float diameter)

    Sets the diameter of the bottom of this cylinder to \a diameter.

    \sa diameterBottom()
*/

/*!
    \fn float QGLCylinder::height() const

    Returns the height of the cylinder.

    The default value is 1.0

    \sa setDiameterBottom()
*/

/*!
    \fn void QGLCylinder::setHeight(float height)

    Sets the height of this cylinder to \a height.

    \sa diameterBottom()
*/


/*!
    \fn int QGLCylinder::slices() const

    Returns the number of triangular slices the cylinder is divided into
    around its polar axis.

    The default is 6.

    \sa setSlices()
*/

/*!
    \fn int QGLCylinder::setSlices(int slices)

    Sets the number of triangular \a slices the cylinder is divided into
    around its polar axis.

    \sa slices()
*/

/*!
    \fn int QGLCylinder::layers() const

    Returns the number of cylindrical layers the cylinder is divided into
    along its height.

    The default is 3.

    \sa setLayers()
*/

/*!
    \fn int QGLCylinder::setLayers(int layers)

    Sets the number of stacked \a layers the cylinder is divided into
    along its height.

    \sa layers()
*/

/*!
    \fn  bool QGLCylinder::topEnabled() const

    Returns true if the top of the cyclinder will be created when
    building the mesh.

    The default is true.

    \sa setTopEnabled()
*/

/*!
    \fn void QGLCylinder::setTopEnabled(bool top)

    Set whether the top end-cap of the cylinder will be created when
    building the mesh.  If \a top is true, the end-cap will be created.

    \sa topEnabled()
*/

/*!
    \fn  bool QGLCylinder::baseEnabled() const

    Returns true if the base of the cyclinder will be created when
    building the mesh.

    The default is true.

    \sa setBaseEnabled()
*/

/*!
    \fn void QGLCylinder::setBaseEnabled(bool base)

    Set whether the base end-cap of the cylinder will be created when
    building the mesh.  If \a base is true, the end-cap will be created.

    \sa baseEnabled()
*/

/*!
    \relates QGLCylinder

    Builds the geometry for \a cylinder within the specified
    geometry \a builder.
*/

QGLBuilder& operator<<(QGLBuilder& builder, const QGLCylinder& cylinder)
{
    int nCaps = (cylinder.topEnabled()?1:0) + (cylinder.baseEnabled()?1:0);
    Q_ASSERT(cylinder.layers() >= 1 + nCaps);

    float numSlices = float(cylinder.slices());
    float numLayers = float(cylinder.layers() - nCaps); // minus top and base caps
    float topRadius = cylinder.diameterTop() / 2.0f;
    float bottomRadius = cylinder.diameterBottom() / 2.0f;

    float angle = 0.0f;
    float angleIncrement = (2.0f * M_PI) / numSlices;
    float radius = topRadius;
    float radiusIncrement = float(bottomRadius-topRadius) / numLayers;
    float height = float(cylinder.height());
    float heightDecrement = height / numLayers;
    height *= 0.5f;

    float textureHeight = 1.0f;
    float textureDecrement = 1.0f / numLayers;

    QGeometryData oldLayer;

    // layer 0: Top cap
    {
        QGeometryData newLayer;
        //Generate a circle of vertices for this layer.
        for (int i=0; i<cylinder.slices(); i++) {
            newLayer.appendVertex(QVector3D(radius * cosf(angle), radius * sinf(angle), height));
            angle+=angleIncrement;
        }
        angle = 0.0f;
        QVector3D center = newLayer.center();
        // Generate texture coordinates (including an extra seam vertex for textures).
        newLayer.appendVertex(newLayer.vertex(0));
        newLayer.generateTextureCoordinates();
        for (int i = 0; i < newLayer.count(); ++i)
            newLayer.texCoord(i).setY(textureHeight);
        if (cylinder.topEnabled()) {
            QGeometryData top;
            builder.newSection();
            builder.currentNode()->setObjectName(QStringLiteral("Cylinder Top"));
            top.appendVertex(center);
            top.appendVertexArray(newLayer.vertices());
            //Generate a circle of texture vertices for this layer.
            top.appendTexCoord(QVector2D(0.5f, 0.5f));
            for (int i=1; i<top.count(); i++) {
                top.appendTexCoord(QVector2D(0.5f * cosf(angle) + 0.5f, 0.5f * sinf(angle) + 0.5f));
                angle+=angleIncrement;
            }
            angle = 0;
            builder.addTriangulatedFace(top);
        }
        oldLayer.clear();
        oldLayer.appendGeometry(newLayer);
    }

    // intermediate layers
    for (int layerCount=0; layerCount<(cylinder.layers()-nCaps); ++layerCount) {
        radius+=radiusIncrement;
        height-=heightDecrement;
        textureHeight-=textureDecrement;
        QGeometryData newLayer;
        //Generate a circle of vertices for this layer.
        for (int i=0; i<cylinder.slices(); ++i) {
            newLayer.appendVertex(QVector3D(radius * cosf(angle), radius * sinf(angle), height));
            angle+=angleIncrement;
        }
        angle = 0.0f;
        // Generate texture coordinates (including an extra seam vertex for textures).
        newLayer.appendVertex(newLayer.vertex(0));
        newLayer.generateTextureCoordinates();
        for (int i = 0; i < newLayer.count(); ++i)
            newLayer.texCoord(i).setY(textureHeight);

        if (layerCount==0) {
            builder.newSection();
            builder.currentNode()->setObjectName(QStringLiteral("Cylinder Sides"));
        }
        builder.addQuadsInterleaved(oldLayer, newLayer);

        oldLayer.clear();
        oldLayer.appendGeometry(newLayer);
    }

    // last layer: Base cap
    if (cylinder.baseEnabled()) {
        builder.newSection();
        builder.currentNode()->setObjectName(QStringLiteral("Cylinder Base"));
        QGeometryData base;
        {
            QVector3DArray vvv = oldLayer.vertices();
            for (int i=0; i<vvv.size()-1; ++i)
                base.appendVertex(vvv.at(i));
            QVector3D center = base.center();
            base.appendVertex(vvv.at(0));
            base.appendVertex(center);
        }
        //Generate a circle of texture vertices for this layer.
        for (int i=1; i<base.count(); i++) {
            base.appendTexCoord(QVector2D(0.5f * cosf(angle) + 0.5f, 0.5f * sinf(angle) + 0.5f));
            angle+=angleIncrement;
        }
        base.appendTexCoord(QVector2D(0.5f, 0.5f));
        angle = 0.0f;
        //we need to reverse the above to draw it properly - windings!
        builder.addTriangulatedFace(base.reversed());
    }

    return builder;
}