/*! Construct a new QuadPlane with \a size, subdivided \a level times. By default the plane is 100.0f x 100.0f, and is subdivided 3 times - that is into an 8 x 8 grid. It is centered on the origin, and lies in the z = 0 plane. */ QuadPlane::QuadPlane(QObject *parent, QSizeF size, int level) : QGLSceneNode(parent) { setObjectName(QLatin1String("QuadPlane")); if (level > 8) level = 8; if (level < 1) level = 1; int divisions = 1; for ( ; level--; divisions *= 2) {} // integer 2**n QSizeF div = size / float(divisions); QSizeF half = size / 2.0f; QGLBuilder builder; QGeometryData zip; QGeometryData zip2; for (int yy = 0; yy <= divisions; ++yy) { float y = half.height() - float(yy) * div.height(); float texY = float(yy) / divisions; for (int xx = 0; xx <= divisions; ++xx) { float x = half.width() - float(xx) * div.width(); float texX = float(xx) / divisions; zip.appendVertex(QVector3D(x, y, 0)); zip.appendTexCoord(QVector2D(1.0f - texX, 1.0f - texY)); } if (yy > 0) builder.addQuadsInterleaved(zip, zip2); zip2 = zip; zip2.detach(); zip.clear(); } QGLSceneNode *n = builder.finalizedSceneNode(); addNode(n); }
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); }
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; }
void tst_QGeometryData::interleaveWith() { QVector3D a(1.1, 1.2, 1.3); QVector3D b(2.1, 2.2, 2.3); QVector3D c(3.1, 3.2, 3.3); QVector3D d(4.1, 4.2, 4.3); QVector3D vx(0.7071, 0.7071, 0.0); QVector2D at(0.11, 0.12); QVector2D bt(0.21, 0.22); QVector2D ct(0.31, 0.32); QVector2D dt(0.41, 0.42); QVector2D tx(1.0, 1.0); QGeometryData data; data.appendVertex(a, b, c, d); data.appendTexCoord(at, bt, ct, dt); QGeometryData dat2; // count is the smaller of the two - nothing in this null case // also make sure the argument doesnt somehow change - its a const // so it shouldn't... dat2.interleaveWith(data); QCOMPARE(data.count(), 4); QCOMPARE(data.vertex(0), a); QCOMPARE(dat2.count(), 0); QCOMPARE(dat2.count(QGL::Position), 0); QCOMPARE(dat2.fields(), quint32(0)); // dat2 is smaller and has less fields dat2.appendVertex(a + vx, b + vx); dat2.interleaveWith(data); QCOMPARE(data.count(), 4); QCOMPARE(data.vertex(0), a); QCOMPARE(dat2.count(), 4); QCOMPARE(dat2.count(QGL::Position), 4); QCOMPARE(dat2.count(QGL::TextureCoord0), 0); QCOMPARE(dat2.fields(), QGL::fieldMask(QGL::Position)); QCOMPARE(dat2.vertex(0), a + vx); QCOMPARE(dat2.vertex(1), a); QCOMPARE(dat2.vertex(2), b + vx); QCOMPARE(dat2.vertex(3), b); // full zip with both sides have 4 verts & textures dat2.clear(); for (int i = 0; i < data.count(); ++i) { dat2.appendVertex(data.vertex(i) + vx); dat2.appendTexCoord(data.texCoord(i) + tx); } dat2.interleaveWith(data); QCOMPARE(dat2.count(), 8); QCOMPARE(dat2.count(QGL::Position), 8); QCOMPARE(dat2.count(QGL::TextureCoord0), 8); QCOMPARE(dat2.fields(), QGL::fieldMask(QGL::Position) | QGL::fieldMask(QGL::TextureCoord0)); QCOMPARE(dat2.vertex(0), a + vx); QCOMPARE(dat2.vertex(1), a); QCOMPARE(dat2.vertex(4), c + vx); QCOMPARE(dat2.vertex(7), d); QCOMPARE(dat2.texCoord(0), at + tx); QCOMPARE(dat2.texCoord(3), bt); QCOMPARE(dat2.texCoord(7), dt); }
/*! \relates QGLSphere Builds the geometry for \a sphere within the specified display \a list. */ QGLBuilder& operator<<(QGLBuilder& list, const QGLSphere& sphere) { qreal scale = sphere.diameter(); int divisions = qMax(sphere.subdivisionDepth() - 1, 0); // define a 0 division sphere as 4 points around the equator, 4 points around the bisection at poles. // each division doubles the number of points. // since each pass of each loop does half a sphere, we multiply by 2 rather than 4. int total = 2*(1 << divisions); //list.begin(QGL::TRIANGLE); //QGeometryData *prim = list.currentPrimitive(); QGeometryData prim; const QVector3D initialVector(0, 0, 1); const QVector3D zAxis(0, 0, 1); const QVector3D yAxis(0, 1, 0); for(int vindex = 0; vindex < total; vindex++) { qreal vFrom = qreal(vindex) / qreal(total); qreal vTo = qreal(vindex+1) / qreal(total); QQuaternion ryFrom = QQuaternion::fromAxisAndAngle(yAxis, 180.0f * vFrom); QQuaternion ryTo = QQuaternion::fromAxisAndAngle(yAxis, 180.0f * vTo); for (int uindex = 0; uindex < 2*total; uindex++) { qreal uFrom = qreal(uindex) / qreal(total); qreal uTo = qreal(uindex+1) / qreal(total); QQuaternion rzFrom = QQuaternion::fromAxisAndAngle(zAxis, 180.0f * uFrom); QQuaternion rzTo = QQuaternion::fromAxisAndAngle(zAxis, 180.0f * uTo); // four points QVector3D na, nb, nc, nd; QVector3D va, vb, vc, vd; na = ryFrom.rotatedVector(initialVector); na = rzFrom.rotatedVector(na); nb = ryTo.rotatedVector(initialVector); nb = rzFrom.rotatedVector(nb); nc = ryTo.rotatedVector(initialVector); nc = rzTo.rotatedVector(nc); nd = ryFrom.rotatedVector(initialVector); nd = rzTo.rotatedVector(nd); QVector2D ta(uFrom/2.0f, 1.0-vFrom); QVector2D tb(uFrom/2.0f, 1.0-vTo); QVector2D tc(uTo/2.0f, 1.0-vTo); QVector2D td(uTo/2.0f, 1.0-vFrom); va = na * scale / 2.0f; vb = nb * scale / 2.0f; vc = nc * scale / 2.0f; vd = nd * scale / 2.0f; prim.appendVertex(va, vb, vc); prim.appendNormal(na, nb, nc); prim.appendTexCoord(ta, tb, tc); prim.appendVertex(va, vc, vd); prim.appendNormal(na, nc, nd); prim.appendTexCoord(ta, tc, td); } } list.addTriangles(prim); return list; }
//------------------------------------------------------------------------------ QGLBuilder& operator << ( QGLBuilder& builder, const QGLEllipsoid& ellipsoid ) { // Determine the number of slices and stacks to generate. static int const numberOfSlicesForSubdivisionDepth[] = { 8, 8, 16, 16, 32, 32, 64, 64, 128, 128 }; static int const numberOfStacksForSubdivisionDepth[] = { 4, 8, 8, 16, 16, 32, 32, 64, 64, 128 }; const unsigned int numberOfSlices = numberOfSlicesForSubdivisionDepth[ ellipsoid.GetSubdivisionDepth() - 1 ]; const unsigned int numberOfStacks = numberOfStacksForSubdivisionDepth[ ellipsoid.GetSubdivisionDepth() - 1 ]; // Precompute sin/cos values for the slices. const unsigned int maxSlices = 128 + 1; const unsigned int maxStacks = 128 + 1; qreal sliceSin[ maxSlices ]; qreal sliceCos[ maxSlices ]; for( unsigned int slice = 0; slice < numberOfSlices; ++slice ) { const qreal angle = 2 * M_PI * (numberOfSlices - 1 - slice) / numberOfSlices; sliceSin[slice] = qFastSin(angle); sliceCos[slice] = qFastCos(angle); } // Join first and last slice. sliceSin[numberOfSlices] = sliceSin[0]; sliceCos[numberOfSlices] = sliceCos[0]; // Precompute sin/cos values for the stacks. qreal stackSin[ maxStacks ]; qreal stackCos[ maxStacks ]; for( unsigned int stack = 0; stack <= numberOfStacks; ++stack ) { // Efficiently handle end-points which also ensure geometry comes to a point at the poles (no round-off). if( stack == 0 ) { stackSin[stack] = 0.0f; stackCos[stack] = 1.0f; } else if( stack == numberOfStacks ) { stackSin[stack] = 0.0f; stackCos[stack] = -1.0f; } else { const qreal angle = M_PI * stack / numberOfStacks; stackSin[stack] = qFastSin(angle); stackCos[stack] = qFastCos(angle); } } // Half the dimensions of the ellipsoid for calculations below (centroid of ellipsoid is 0, 0, 0.) const qreal xRadius = 0.5 * ellipsoid.GetXDiameter(); const qreal yRadius = 0.5 * ellipsoid.GetYDiameter(); const qreal zRadius = 0.5 * ellipsoid.GetZDiameter(); const qreal oneOverXRadiusSquared = 1.0 / (xRadius * xRadius); const qreal oneOverYRadiusSquared = 1.0 / (yRadius * yRadius); const qreal oneOverZRadiusSquared = 1.0 / (zRadius * zRadius); // Create the stacks. for( unsigned int stack = 0; stack < numberOfStacks; ++stack ) { QGeometryData quadStrip; for( unsigned int slice = 0; slice <= numberOfSlices; ++slice ) { // Equation for ellipsoid surface is x^2/xRadius^2 + y^2/yRadius^2 + z^2/zRadius^2 = 1 // Location of vertices can be specified in terms of "polar coordinates". const qreal nextx = xRadius * stackSin[stack+1] * sliceSin[slice]; const qreal nexty = yRadius * stackSin[stack+1] * sliceCos[slice]; const qreal nextz = zRadius * stackCos[stack+1]; quadStrip.appendVertex( QVector3D( nextx, nexty, nextz) ); // Equation for ellipsoid surface is Surface = x^2/xRadius^2 + y^2/yRadius^2 + z^2/zRadius^2 - 1 // Gradient for ellipsoid is x/xRadius^2*Nx> + y/yRadius^2*Ny> + z/zRadius^2*Nz> // Gradient for sphere simplifies to x*Nx> + y*Ny> + z*Nz> // const qreal nextGradientx = stackSin[stack+1] * sliceSin[slice]; // const qreal nextGradienty = stackSin[stack+1] * sliceCos[slice]; // const qreal nextGradientz = stackCos[stack+1]; const qreal nextGradientx = nextx * oneOverXRadiusSquared; const qreal nextGradienty = nexty * oneOverYRadiusSquared; const qreal nextGradientz = nextz * oneOverZRadiusSquared; const qreal nextGradientMagSquared = nextGradientx * nextGradientx + nextGradienty * nextGradienty + nextGradientz * nextGradientz; const qreal oneOverNextGradientMagnitude = 1.0 / sqrt( nextGradientMagSquared ); quadStrip.appendNormal( oneOverNextGradientMagnitude * QVector3D( nextGradientx, nextGradienty, nextGradientz ) ); quadStrip.appendTexCoord( QVector2D(1.0f - qreal(slice) / numberOfSlices, 1.0f - qreal(stack + 1) / numberOfStacks) ); const qreal x = xRadius * stackSin[stack] * sliceSin[slice]; const qreal y = yRadius * stackSin[stack] * sliceCos[slice]; const qreal z = zRadius * stackCos[stack]; quadStrip.appendVertex( QVector3D( x, y, z) ); // const qreal gradientx = stackSin[stack] * sliceSin[slice]; // const qreal gradienty = stackSin[stack] * sliceCos[slice]; // const qreal gradientz = stackCos[stack]; const qreal gradientx = x * oneOverXRadiusSquared; const qreal gradienty = y * oneOverYRadiusSquared; const qreal gradientz = z * oneOverZRadiusSquared; const qreal gradientMagSquared = gradientx * gradientx + gradienty * gradienty + gradientz * gradientz; const qreal oneOverGradientMagnitude = 1.0 / sqrt( gradientMagSquared ); quadStrip.appendNormal( oneOverGradientMagnitude * QVector3D( gradientx, gradienty, gradientz) ); quadStrip.appendTexCoord( QVector2D(1.0f - qreal(slice) / numberOfSlices, 1.0f - qreal(stack) / numberOfStacks) ); } // The quad strip stretches from pole to pole. builder.addQuadStrip( quadStrip ); } return builder; }