QGLSceneNode ClassThe QGLSceneNode class defines a node in a 3D scene. More... #include <QGLSceneNode> Inherits: QObject. This class was introduced in Qt 4.8. Public Types
Properties
Public Functions
Signals
Protected Functions
Additional Inherited MembersDetailed DescriptionThe QGLSceneNode class defines a node in a 3D scene. QGLSceneNode represents one component of a scene. The QGLSceneNode class manages materials, effects and transformations for itself and for its child nodes. As a general approach to 3D applications, a tree of QGLSceneNodes may be constructed, geometry added to them, materials and transformations applied, all during application initialization; and then by simply calling the draw() function the scene is easily rendered for each frame. GeometryMultiple QGLSceneNodes can reference the same geometry, whilst applying different transformations and treatments to it. Since QGLSceneNode is a QObject sub class it cannot be copied directly, so instead use the clone() function for this purpose. A scene node allows referencing into sub-parts of geometry, via the start and count properties. The start index is an offset into the geometry at which drawing will start. The default start index is 0, so that drawing will start from the beginning of the geometry. The count dictates how many vertices will be drawn. The default count is 0, which instructs the underlying geometry to draw all vertices. A node may have no geometry, that is geometry().count() == 0. This is useful for example to have one node controlling or collecting together several child nodes to be manipulated as a unit. MaterialsAlso a node may have a local material. This allows drawing the same geometry with different materials (which includes different textures). When accessing a QGLSceneNode via QML, or for simple applications, the pointer based material functions are convenient and intuitive, saving the trouble of adding the material pointer to the correct palette: For more complex applications; for example building model loaders, or for larger scenes; where you need to explicitly manage materials via a palette, use the index based functions: The behaviour of both with respect to faces is the same - if a material() is specified but no backMaterial() is specified, then the material() is applied to both faces; if both material() and backMaterial() are non-null then they are applied to their specific faces. TransformationsTypically the local transformation matrix is set by the process that constructed the node: in the case of an imported model, it is likely to have been specified by the model file. To make individual changes to the location or orientation of this node, use the position() and transforms() properties. Scene GraphUse childNodes() to obtain the list of child nodes, and add and remove child nodes by the addNode() and removeNode() methods. If a QGLSceneNode is constructed with a QGLSceneNode parent, then addNode() will be called implicitly on the parent. A child may be a child multiple times, a child may be under more than one parent, and several parents may reference the same child. There is however no protection against cycles, so a child must not be a parent of itself, even if indirectly. A child node for the purposes of rendering means a child added via the addNode() method. The default QGLSceneNode constructor will check to see if its parent is a QGLSceneNode and add itself via the addNode() function if it is. To help debug a scene, use the qDumpScene() function to get a printout on stderr of the entire structure of the scene below the argument node. Debugging Lighting NormalsThe ViewNormals option is an advanced feature for use when inspecting and debugging models or geometry in a scene. The lighting normals are displayed as a straight line from the vertex pointing in the direction of the lighting normal. This is useful for example to show where normals are inverted or wrongly calculated. The setting of the ViewNormals flag is not propagated to child nodes, instead set the flag to true for the node or nodes where its needed. To set the flag on all child nodes use code like: foreach (QGLSceneNode *node, scene.allChildren()) node->setNormalViewEnabled(true); See also QGLAbstractScene. Member Type Documentation
flags QGLSceneNode::Options |
Constant | Value | Description |
---|---|---|
QGLSceneNode::NoOptions | 0x0000 | Do not enable any QGLSceneNode options. |
QGLSceneNode::CullBoundingBox | 0x0001 | Perform a cull using boundingBox() before attempting to draw the geometry(). |
QGLSceneNode::ViewNormals | 0x0002 | Enables the display of lighting normals for debugging purposes. |
QGLSceneNode::ReportCulling | 0x0004 | Send a signal when an object is displayed or culled. |
QGLSceneNode::HideNode | 0x0008 | Hide this node so it, and all its children, are excluded from rendering. |
The Options type is a typedef for QFlags<Option>. It stores an OR combination of Option values.
See also setOptions().
This property holds this property is a pointer to any QGLMaterial instance for this node's back faces.
This material is applied to the back faces, if non-null. The default value of this property is null. When this property is null, any non-null material set on the material() property will be applied to front and back faces.
To apply a material to the front faces use the material() property.
Getting this property is exactly equivalent to palette()->material(backMaterialIndex()).
Setting this property causes the material if not already in this nodes palette to be added, and then the corresponding index to be set for this scene node.
Setting this property is exactly equivalent to:
int index = d->palette->indexOf(material); if (index == -1) index = d->palette->addMaterial(material); setBackMaterialIndex(index);
Access functions:
QGLMaterial * | backMaterial() const |
void | setBackMaterial(QGLMaterial * material) |
Notifier signal:
void | updated() |
This property holds this property is a pointer to a QGLMaterial instance for this node.
This material is applied to all faces if the backMaterial() property is set to null, which is the default. If the backMaterial() property is non-null then this material is only applied to the front faces.
To apply a material to the back faces use the backMaterial() property.
Getting this property is exactly equivalent to palette()->material(materialIndex()).
Setting this property causes the material if not already in this nodes palette to be added, and then the corresponding index to be set for this scene node.
Setting this property is equivalent to:
int index = d->palette->indexOf(material); if (index == -1) index = d->palette->addMaterial(material); setMaterialIndex(index);
If setting this property, when no palette exists one is created, as a convenience - but this is suitable only for experimental code and for very small numbers of nodes. In debug mode a warning is printed in this case.
Generally one common palette should be created, and set on each node. This also allows nodes to share materials and their textures.
Access functions:
QGLMaterial * | material() const |
void | setMaterial(QGLMaterial * material) |
Notifier signal:
void | updated() |
See also materialIndex() and setMaterialIndex().
This property holds various option flags effecting rendering.
This property containst several flags that modify normal rendering and culling on a QGLSceneNode. See Options for a full description of the available flags.
Access functions:
QGLSceneNode::Options | options() const |
void | setOptions(QGLSceneNode::Options options) |
Notifier signal:
void | updated() |
See also setOption() and Options.
This property holds the amounts of x, y and z axis translation for this node.
Since most nodes are situated relative to (0, 0, 0) when imported as part of a model or constructed programatically, the translation is effectively the position of the model in the scene.
The x, y and z axis translations can also be specified individually as separate properties x, y, and z
Access functions:
QVector3D | position() const |
void | setPosition(const QVector3D & p) |
Notifier signal:
void | updated() |
This property holds the amount of x axis translation for this node.
Access functions:
qreal | x() const |
void | setX(qreal x) |
Notifier signal:
void | updated() |
See also position().
This property holds the amount of y axis translation for this node.
Access functions:
qreal | y() const |
void | setY(qreal y) |
Notifier signal:
void | updated() |
See also position().
This property holds the amount of z axis translation for this node.
Access functions:
qreal | z() const |
void | setZ(qreal z) |
Notifier signal:
void | updated() |
See also position().
Constructs a new scene node and attaches it to parent. If parent is a QGLSceneNode then this node is added to it as a child.
Constructs a new scene node referencing geometry and attaches it to parent. If parent is a QGLSceneNode then this node is added to it as a child.
Destroys this scene node.
Adds the node to the list of child nodes for this node.
This function does nothing if node is null or is already a child of this node. If the node is the node itself, a warning about an attempt to add a node to itself is printed, and no add takes place.
If the aim is to have the same geometry displayed several times under a given node, each time with different transformations, use the clone() call to create copies of the node and then apply the transformations to the copies.
Alternatively, create modifier nodes with the transformations and add the geometry bearing node to each with addNode():
QGLBuilder builder; builder << CarWheel(5.0f); // some car wheel geometry QGLSceneNode wheel = builder.finalizedSceneNode(); QGLSceneNode frontLeft = new QGLSceneNode(m_sceneRoot); frontLeft->addNode(wheel); frontLeft->setPosition(QVector3D(1.0f, 2.0f, 0.0f)); QGLSceneNode frontRight = new QGLSceneNode(m_sceneRoot); frontRight->addNode(wheel); frontRight->setPosition(QVector3D(-1.0f, 2.0f, 0.0f)); QGLSceneNode backLeft = new QGLSceneNode(m_sceneRoot); backLeft->addNode(wheel); backLeft->setPosition(QVector3D(1.0f, -2.0f, 0.0f)); QGLSceneNode backRight = new QGLSceneNode(m_sceneRoot); backRight->addNode(wheel); backRight->setPosition(QVector3D(-1.0f, -2.0f, 0.0f));
Because a child node can be validly added to many different nodes, calling addNode() does not normally affect the QObject::parent() ownership. However, if node does not currently have a QObject::parent(), the parent will be set to this node.
See also removeNode(), clone(), and addNodes().
Adds the members of nodes to the list of child nodes for this node.
See also addNode() and removeNodes().
Adds a single transform to this node, to be applied to the node after all current members of transformations() have been applied.
In the mathematical sense, transform is applied to the modelview matrix before the current members of transformations() are applied in reverse order.
See also transforms() and setTransforms().
Returns a list including recursively all child nodes under this node. Each child node only appears once, even if it is included multiple times in the scene graph.
See also children().
Creates a new QGLSceneNode that is a copy of this scene node, with parent as the parent of the new copy. If parent is NULL then parent is set to this nodes parent.
The copy will reference the same underlying geometry and have all effects, transforms and other properties copied from this node.
The copy returned will have the same child nodes, except all child nodes whose objectName() is equal to name.
Creates a new QGLSceneNode that is a copy of this scene node, with parent as the parent of the new copy. If parent is NULL then parent is set to this nodes parent.
The copy will reference the same underlying geometry and have all effects, transforms and other properties copied from this node.
The copy returned will have the same child nodes, except all child nodes whose objectName() is in the list of names.
Returns the back material index for this scene node.
See also setBackMaterialIndex().
Returns a bounding box for the portion of the geometry referenced by this scene node. If the value of start() is 0, and count() is the same as geometry()->size() then the bounding box will be the same as geometry()->boundingBox(). However if the scene node only references some part of the geometry, a bounding box for this section is calculated.
If this scene node has child nodes then the bounding box will be the calculated union of the bounding box for this nodes geometry (if any) and the bounding boxes of the children.
The calculated value is cached and returned on subsequent calls, but could be expensive to calculate initially.
Returns a list of the child nodes for this node. This list is not recursively generated, it includes only the nodes which are immediate children of this node.
See also allChildren().
Creates a new QGLSceneNode that is a copy of this scene node, with parent as the parent of the new copy. If parent is NULL then parent is set to this nodes parent.
The copy will reference the same underlying geometry, child nodes, and have all effects, transforms and other properties copied from this node. The only property that is not copied is pickNode().
See also cloneNoChildren().
Creates a new QGLSceneNode that is a copy of this scene node, with parent as the parent of the new copy. If parent is NULL then parent is set to this nodes parent.
The copy will reference the same underlying geometry, and have all effects, transforms and other properties copied from this node. The children() and pickNodes() are not cloned.
See also clone().
Creates a new QGLSceneNode that is a copy of this scene node, with parent as the parent of the new copy. If parent is NULL then parent is set to this nodes parent.
The copy will reference the same underlying geometry, will create clones of all of the child nodes, and have all effects, transforms and other properties copied from this node. The only property that is not copied is pickNode().
See also cloneNoChildren().
Returns the count of the vertices to render from geometry() for this scene node. The default is zero, meaning that this node uses all vertices from start() up to the last logical vertex in the underlying geometry().
See also setCount() and start().
Signals that the node was culled due to falling wholly outside the view frustum. This signal can only fire if both QGLSceneNode::CullBoundingBox and QGLSceneNode::ReportCulling options are both set.
Signals that the node was displayed - or at least its geometry was sent to the GPU for rendering, since the GPU might still clip or occlude the node. This signal can only fire if both QGLSceneNode::CullBoundingBox and QGLSceneNode::ReportCulling options are both set.
Draws this scene node and its children on the painter.
In detail this function:
Note that the draw() method does not restore the effect. If the first step above results in a change to the current QGL::Standard effect then it will remain set to that effect. In general any painting method should ensure the effect it requires is current.
The way draw is implemented ensures that this nodes effects, materials and transformations will apply by default to its child nodes. Transformations are cumulative, but effects and materials override those of any parent node.
Note that if the HideNode option is set for this node, neither it nor its children will be drawn.
Draws the geometry of the node onto the painter.
This is the function which performs the actual drawing step in the draw function below.
Override this function to perform special processing on this node, after transformation & culling are applied and before sequencing of materials & effects are done; but just before (or after) the actual draw step.
This default implementation simply draws the nodes geometry onto the painter.
Example:
void MySpecialNode::geometryDraw(QGLPainter *painter) { // at this point the node has survived culling, the model-view // matrix is transformed into this nodes frame of reference, // materials & effects have been applied as this node appears // in its place in the render sequence doMySpecialProcessing(); // call parent implementation to do actual draw QGLSceneNode::geometryDraw(painter); }
Returns the drawing mode to use to render geometry(). The default is QGL::Triangles.
See also setDrawingMode().
Returns the drawing width for this node.
Drawing width is used only when drawing lines or points (ie. when the drawing mode is points, lines, line-strips, etc).
See also setDrawingWidth() and drawingMode().
Returns the local effect associated with this node. The default value is QGL::FlatColor. If the value of hasEffect() is false, then this the value of effect() is ignored.
See also setEffect() and hasEffect().
This function creates a copy of the scenegraph with its root at the current node, with a specific sub-branch specified by name removed from it. The exact node from which to prune is specified by the double-colon separated path described by name.
The function will create a copy of the scenegraph using the cloneWithChildren() method, from which the specified sub-branch shall be removed.
If a parent is specified then the sub-node will have that parent added to it.
The inverse of this function, get(), will return only the sub-branch, leaving the current scengraph intact.
For example, consider the following scenegraph:
Node 1 - Node 2 / Root \ Node 3 - Node 4
By using the following code:
QString branch("Root::Node1"); QGLSceneNode *newGraph = except(branch);
We can create a copy of the scenegraph with the branch pruned, as shown below:
Root \ Node 3 - Node 4
See also findSceneNode(), clone(), cloneWithChildren(), and get().
Like the previous version of except, this creates a copy of the scenegraph with its root at the current node. However, this version allows users to prune multiple sub-branches using the list of sub-branches specified by the names list.
Thus for the scenegraph:
Node 1 - Node 2 - Node 3 / Root \ Node 4 - Node 5 - Node 6
The code:
QString branch1("Root::Node1::Node2"); QString branch2("Root::Node4::Node5}"); QStringList branchList; branchList << branch1 << branch2; QGLSceneNode *newGraph = except(branchList);
Would yield the scenegraph:
Node 1 / Root \ Node 4
Once again, this copy will be reparented with the parameter parent if it exists.
See also findSceneNode(), clone(), cloneWithChildren(), and get().
Return a specific scene node based on a unqiue path specified by nodePath. This path contains the names of the nodes in the scene-graph from the root node through to a specific target node.
Individual node names are separated by a double colon "::".
For example, given the following scene graph:
Item2 / Item1 \ Item3 - Item4
We could get the QGLSceneNode for "Item4" using the following function call:
QString searchPath("Item1::Item3::Item4"); QGLSceneNode * targetNode = findSceneNode(searchPath);
The function will automatically skip unnamed nodes. For example, consider this scene graph:
Item2 / <unnamed> - Item1 \ Item3 - <unnamed> - Item4
The function nodePath will return the QGLSceneNode for node "Item4".
"Item1::Item3::Item4"
This is because the unnamed nodes are skipped. Likewise blank or unnamed nodes in the search path will be ignored, so the following string will give the same result as the previous example:
"::Item1::Item3::::Item4"
The function assumes that there are no repeated instances of the same name at any given ply of the scenegraph. For example, the following scenegraph is invalid because of the repeat of Sphere1 in the same layer of the graph:
Sphere1
/
Cube1
\
Sphere1
The function also assumes that the names of nodes do not contain double colons. For example, a node with the name: "Sphere::1" would result in an invalid graph.
If a third party model contains such names it is still possible to retrieve the nodes by using the variant of findSceneNode() which takes a list of node-names rather than the double-colon separated string in nodePath.
See also findSceneNode().
This functions exactly the same way as the previous findSceneNode function except in this case the nodePath is specified as a QStringList composed of the individual nodes along the path to the target node.
The other findSceneNode function acts by splitting its nodePath into a QStringList wherever a double colon (::) is found and passing the result to this function.
See also findSceneNode().
Returns the geometry associated with this node, or a null QGeometryData if no geometry has been associated with it.
See also setGeometry().
This function retrieve a sub-branch of the scenegraph with its root at the current node. The exact node to retrieve is specified by the double-colon separated path described by name.
This node will be located using the method described by findSceneNode() and will be copied using the clone() function.
If a parent is specified then the sub-node will have that parent added to it.
If the user sets the forceCopy parameter to true then the cloneWithChildren() function shall be used instead of the clone() function while copying. This will result in all of the child-nodes being forcibly copied as well resulting in less efficient memory usage, though allowing the modification of the newly created branch without altering the original in any way.
The inverse of this function, except(), can be used to get everything except a specific sub-branch.
See also findSceneNode(), clone(), cloneWithChildren(), get(), and except().
This function is similar to the previous instance of the get() function. In this case, however, the user can specify multiple node-paths to get.
These nodes will be cloned in an identical way to the single-node instance of get, using the parent and forceCopy parameters in the same way.
The resulting set of branches will then be added to a newly created root node, creating a composite branch. For example, consider the simple scenegraph below:
Node 1 - Node 2 - Node 3 / Root \ Node 4 - Node 5 - Node 6
If we now call the get() function with the following code.
QString branch1("Root::Node 1::Node 2"); QString branch2("Root::Node 4::Node 5"); QStringList branchList; branchList << branch1 << branch2; QGLSceneNode *newBranch = get(branchList);
In this case the function will create a new root node for the output, and graft the two branches onto it, as follows:
Node 2 - Node 3 / New-Root \ Node 5 - Node 6
See also findSceneNode(), clone(), cloneWithChildren(), get(), and except().
Returns true if the local effect on this node is enabled, otherwise returns false.
See also setEffectEnabled() and setEffect().
Returns the local transform associated with this node. If no local transform has been explicitly set, this method returns a QMatrix4x4 set to the identity matrix.
The local transform is typically set during model loading or geometry construction, and is a feature of the geometry.
In general to change the location or orientation of the node use the position() or transforms() properties instead.
See also setLocalTransform(), position(), and transforms().
Returns the material index for this scene node.
See also setMaterialIndex().
Creates a new QGLSceneNode that is a copy of this scene node, with parent as the parent of the new copy. If parent is NULL then parent is set to this nodes parent.
The copy will reference the same underlying geometry and have all effects, transforms and other properties copied from this node.
The copy returned will have only one child node. This child node will be the first child node of this one which has its objectName() equal to name.
See also clone() and allExcept().
Creates a new QGLSceneNode that is a copy of this scene node, with parent as the parent of the new copy. If parent is NULL then parent is set to this nodes parent.
The copy will reference the same underlying geometry and have all effects, transforms and other properties copied from this node.
The copy returned will have only the child nodes from this whose objectName() is in the list of names.
See also clone() and allExcept().
Returns the palette of materials used by this scene node, or NULL if no palette has been set.
See also setPalette().
Returns the pick node for this scene node, if one was set; otherwise NULL (0) is returned.
See also setPickNode().
Removes the child node matching node from this node.
If the QObject::parent() ownership of node was set to this node, then its parent will be changed to another parent node if it had multiple parents.
If node had only a single parent, then its parent will be set to null, effectively detaching it from the QObject ownership rules of the scene graph. The caller is then responsible for deleting node.
If the QObject::parent() of node was not a scene node parent, then it will be left unmodified.
See also addNode() and removeNodes().
Removes the members of nodes from the list of child nodes for this node.
See also removeNode() and addNodes().
Sets the back material index for this scene node to material.
See also backMaterialIndex() and materialIndex().
Sets the count of the vertices to render from geometry() for this scene node.
See also count() and setStart().
Sets the drawing mode to use to render geometry().
Note: this function changes the drawing mode, but the underlying geometry() still consists of the triangles that were added. Thus, this function is only useful for converting the drawing mode into QGL::Points to display the geometry() as a point cloud instead of a triangle mesh. The other enums from QGL::DrawingMode will give unpredictable results.
See also drawingMode().
Sets the drawing width to the given value.
Drawing width is used only when drawing lines or points (ie. when the drawing mode is points, lines, line-strips, etc).
See also drawingWidth() and drawingMode().
Sets the local effect associated with this node to be effect. hasEffect() will return true after calling this method.
The QGLSceneNode::draw() function will ensure that effect is applied to the QGLPainter before drawing any geometry.
See also effect() and hasEffect().
Sets whether the current value of effect() or userEffect() will be applied to the QGLPainter prior to drawing. If enabled is true, then the effect is applied, otherwise it is not.
See also setEffect(), effect(), and hasEffect().
Sets the geometry associated with this node to be geometry. Typically the geometry will be some type of mesh object. The default implementation of the QGLSceneNode::draw() method will call the geometry's draw() method.
See also geometry().
Sets the local transform associated with this node to be transform. The default implementation of the QGLSceneNode::draw() method will apply this transform to the QGLPainter before drawing any geometry.
See also localTransform().
Sets the material index for this scene node to material.
See also materialIndex().
Enables or disables option according to value.
See also options() and setOptions().
Sets the palette of materials for this scene node to palette.
See also palette().
Sets the pick node for this scene node to node.
See also pickNode().
Sets the start index within geometry() that should be used to render fragments for this scene node.
See also start() and setCount().
Sets the list of transformations to apply to this node to transforms.
The transformations are applied to the node itself, so a QGraphicsScale3D followed by a QGraphicsTranslation3D will first scale the node in its local co-ordinate system, and then translate the node a new location.
In the mathematical sense, the transformations are applied to the modelview matrix in the reverse order in which they appear in transforms.
The position() is applied after all other transformations have been applied.
See also transforms(), addTransform(), and position().
Sets the local effect associated with this node to be the custom effect. hasEffect() will return true after calling this method.
This custom effect will supersede any standard effect.
The default implementation of QGLSceneNode::apply() will set this effect during initialization of the model.
The default implementation of the QGLSceneNode::draw() method will ensure that effect is applied to the QGLPainter before drawing any geometry.
See also userEffect() and hasEffect().
Returns the starting index within geometry() that should be used to render fragments for this scene node. The default value is 0, indicating that the 0'th logical vertex in geometry() is the start.
See also setStart() and count().
Returns the list of transformations to apply to this node. The default is the empty list.
The transformations are applied to the node itself, so a QGraphicsScale3D followed by a QGraphicsTranslation3D will first scale the node in its local co-ordinate system, and then translate the node a new location.
In the mathematical sense, the transformations are applied to the modelview matrix in the reverse order in which they appear in this list.
The position() is applied after all other transformations have been applied.
See also setTransforms(), addTransform(), and position().
Returns the user effect associated with this node, or NULL if one is not set. The default value is NULL. If the value of hasEffect() is false, then this effect is ignored.
See also setUserEffect() and hasEffect().