The GL scene graph


The GL scene graph is the view of the X3D scene graph specialized in rendering. The structure of the scene graph is slighlty different from the X3D scene graph, At that moment this structure renders only static 3D objets, and the main node is X3DTK::GL::IndexedFaceSet. GL nodes inherit the scene graph API of which description can be found here.

The GL scene graph is designed to be integrated in a viewer, because of the optimized version of the GL nodes, the interface is not easy to use. It is commonly used in the X3DTK::GL::Renderer processor.

Displaying a scene in OpenGL

The simplest thing you can do to display a scene in OpenGL is to use the X3DTK::SimpleX3DGLScene class which encapsulates basic operations. Check the X3DViewer example for more details.

Loading a GL scene graph

You may need to manipulate more precisely the GL scene graph. The GL scene graph is built from an X3D scene graph using a the X3DTK::X3D::GLBuilder.
// scene is an X3D scene previously loaded X3DTK::GL::Scene *glscene = X3DTK::Singleton<X3DTK::X3D::GLBuilder>::getInstance()->build(scene);

Using the GL scene graph

The main node is X3DTK::GL::IndexedFaceSet which inherits X3DTK::GL::X3DComposedGeometryNode. We present here the two nodes, the other nodes are less important.


This node contains informations relative to the vertices and the display of them. For example:
X3DTK::GL::X3DComposedGeometryNode *N; // Returns the GL format of the vertices. GLenum format = N->getVertexFormat();
There are other attributes that you can find in the description of the node.


For performance reasons, GL vertex arrays have been chosen to implement it. That is why the node structure with a Coordinate node, a Normal node, a Color node and a TextureCoordinate node disappearxand are now contained in the vertex arrays. As previous attributes are optional, the node contains 4 vertex arrays but only one of them is filled.

Access to the vertex arrays is made:

X3DTK::GL::IndexedFaceSet *I; // Getting the vertex array if (I->getVertexFormat() == GL_T2F_N3F_V3F) std::vector<T2F_N3F_V3F> &array = I->T2F_N3F_V3F_vertexArray(); // Getting the index array MFInt32 &indexArray = I->indexArray(); // Getting the X3DToGLIndex const std::vector<MFInt32> &x3dglindex = I->getX3DToGLIndex();
The faces are indexed enabling the share of vertices. Faces are defined by three vertices. For this reason, the coordinate index array in the X3DTK::X3D::IndexedFaceSet is different from this one. Nevertheless only some vertices are duplicated in the X3DTK::GL::IndexedFaceSet node. That is why we can find the relation with the X3DToGLIndex.

fig 1 : For one X3D index, there are several GL indexes.

If there are several GL indexes, it means that the point has been duplicated, its coordinates are the same but one of the other attribute like color is different.

For more details about how to use the X3DTK::GL::IndexedFaceSet node, you can check the glNormalViewer and simpleAnimationViewer examples.

Extending the GL scene graph

X3DTK::GL::X3DNode interface

Three methods are added to the X3DTK::X3DAbstractNode interface. setX3DReference and getX3DReference define the link with the related X3D node. The update method is used to update the attributes of the node. Here is an example:
namespace X3DTK { namespace GL { void Box::update() { if (getX3DReference() == 0) return; X3D::Box *B = static_cast<X3D::Box *>(getX3DReference()); _size = B->getSize(); _boxArray = BoxDrawArray::getInstance(); } } }

X3DTK::GL::X3DGeometryNode interface

A draw method is added to the X3DTK::GL::X3DNode interface. An example:
namespace X3DTK { namespace GL { void Box::draw() const { glEnable(GL_CULL_FACE); glFrontFace(GL_CW); glCullFace(GL_BACK); glPushMatrix(); glScalef(_size.x, _size.y, _size.z); glInterleavedArrays(GL_N3F_V3F, 0, _boxArray->getBoxVertexArrayAddress()); glDrawElements(GL_TRIANGLES, _boxArray->getBoxSize(), GL_UNSIGNED_INT, _boxArray->getBoxIndexArrayAddress()); glPopMatrix(); glDisable(GL_CULL_FACE); } } }


Here is the list of the examples where the GL scene graph is used:
Generated on Fri Jul 30 12:02:31 2004 for X3DToolKit by doxygen 1.3.6