Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members Related Pages
Introduction
This is a complete example on how to create a new X3DNode by deriving an exisiting node. Here we define an X3D::FCylinder
node, deriving from X3D::Cylinder
and that enables the control of the cylinder discretization.
deriving X3D::Cylinder
We derive X3D::Cylinder
and we add a section attribute. Note that the copy constructor is protected, because the nodes must be passed by pointers, and not by value. Nevertheless we define a clone method. For the node be loaded, the loadAttributes has to be redefined, as well as the writeAttributes, to enable the writing of the FCylinder node in an X3D file.
loading X3D::FCylinder
To load the node, we have to derive the creator for the nodes belonging to the Geometry3D, because X3D::FCylinder
belongs to it.
deriving GL::Cylinder
To render X3D::FCylinder
, we first have to define its openGL equivalent node, GL::FCylinder
. Note that there are different namespaces for the scene graphs X3D and GL. There is no attribute to add to the GL::Cylinder. The update method which updates the attributes of the node from the equivalent X3D::FCylinder
x3dReference has to be redefined. To the contrary, the render has not to be redefined.
building GL::FCylinder
We derive the GLBuilderGeometry3DVisitor
, which will create the GL::FCylinder
from an X3D::FCylinder
.
code
FCylinder
FCylinder.h
#ifndef FCYLINDER_H
#define FCYLINDER_H
#include <X3DTK/kernel.h>
namespace X3DTK {
namespace X3D {
class FCylinder : public Cylinder
{
public:
FCylinder();
FCylinder(SFBool bottom,
SFFloat radius,
SFFloat height,
SFBool side,
SFBool top,
SFInt32 section);
virtual SFNode clone() const;
void setSection(SFInt32 section);
inline SFInt32 getSection() const {return _section;};
virtual void load(const X3DFileElement *element);
virtual SFString write() const;
protected:
FCylinder(const FCylinder &C);
private:
SFInt32 _section;
};
}
}
#endif
FCylinder.cpp
#include "FCylinder.h"
namespace X3DTK {
namespace X3D {
FCylinder::FCylinder()
: Cylinder(), _section(8)
{
defineTypeName("FCylinder");
}
FCylinder::FCylinder(SFBool bottom, SFFloat radius, SFFloat height, SFBool side, SFBool top, SFInt32 section)
: Cylinder(bottom, radius, height, side, top), _section(section)
{
defineTypeName("FCylinder");
}
FCylinder::FCylinder(const FCylinder &C)
: Cylinder(C), _section(C._section)
{
}
SFNode FCylinder::clone() const
{
return new FCylinder(*this);
}
void FCylinder::setSection(SFInt32 section)
{
_section = section;
}
void FCylinder::load(const X3DFileElement *element)
{
Cylinder::load(element);
int index;
index = element->getIndexAttribute("section");
if (index != -1)
_section = element->getAttribute(index).toInt();
}
SFString FCylinder::write() const
{
SFString attr;
if (_section != 8)
attr += " section=\"" + toSFString(_section) + "\"";
return Cylinder::write() + attr;
}
}
}
MyGeometry3DCreator
MyGeometry3DCreator.h
#ifndef MYGEOMETRY3DCREATOR_H
#define MYGEOMETRY3DCREATOR_H
#include <X3DTK/kernel.h>
namespace X3DTK {
namespace X3D {
class MyGeometry3DCreator : public DefaultGeometry3DCreator
{
public:
MyGeometry3DCreator();
SFNode createFCylinder() const;
};
}
}
#endif
MyGeometry3DCreator.cpp
#include "MyGeometry3DCreator.h"
#include "FCylinder.h"
namespace X3DTK {
namespace X3D {
MyGeometry3DCreator::MyGeometry3DCreator()
: DefaultGeometry3DCreator()
{
defineNewCreationFunction<MyGeometry3DCreator>("FCylinder", &MyGeometry3DCreator::createFCylinder);
}
SFNode MyGeometry3DCreator::createFCylinder() const
{
return new FCylinder();
}
}
}
GLFCylinder
GLFCylinder.h
#ifndef GLFCYLINDER_H
#define GLFCYLINDER_H
#include <X3DTK/kernel.h>
namespace X3DTK {
namespace GL {
class FCylinder : public Cylinder
{
public:
FCylinder();
virtual SFNode clone() const;
virtual void update();
protected:
FCylinder(const FCylinder &C);
};
}
}
#endif
GLFCylinder.cpp
#include "GLFCylinder.h"
#include "FCylinder.h"
namespace X3DTK {
namespace GL {
FCylinder::FCylinder()
: Cylinder()
{
defineTypeName("FCylinder");
}
FCylinder::FCylinder(const FCylinder &C)
: Cylinder(C)
{
}
SFNode FCylinder::clone() const
{
return new FCylinder(*this);
}
void FCylinder::update()
{
X3D::FCylinder *C = static_cast<X3D::FCylinder *>(x3dReference);
setBottom(C->getBottom());
setRadius(C->getRadius());
setHeight(C->getHeight());
setSide(C->getSide());
setTop(C->getTop());
setCylinderArray(CylinderDrawArray::getInstanceOfSection(C->getSection()));
}
}
}
MyGLBuilderGeometry3DVisitor
MyGLBuilderGeometry3DVisitor.h
#ifndef MYGLBUILDERGEOMETRY3DVISITOR_H
#define MYGLBUILDERGEOMETRY3DVISITOR_H
#include <X3DTK/glbuilder.h>
namespace X3DTK {
namespace X3D {
class FCylinder;
class MyGLBuilderGeometry3DVisitor : public GLBuilderGeometry3DVisitor
{
public:
MyGLBuilderGeometry3DVisitor();
virtual void enterFCylinder(FCylinder *C) const;
};
}
}
#endif
MyGLBuilderGeometry3DVisitor.cpp
#include "MyGLBuilderGeometry3DVisitor.h"
#include "FCylinder.h"
#include "GLFCylinder.h"
#include <iostream>
using namespace std;
namespace X3DTK {
namespace X3D {
MyGLBuilderGeometry3DVisitor::MyGLBuilderGeometry3DVisitor()
: GLBuilderGeometry3DVisitor()
{
defineNewEnterFunction<MyGLBuilderGeometry3DVisitor, FCylinder>(&MyGLBuilderGeometry3DVisitor::enterFCylinder);
}
void MyGLBuilderGeometry3DVisitor::enterFCylinder(FCylinder *C) const
{
GL::FCylinder *GC = new GL::FCylinder();
GC->setX3DReference(C);
globalVariables->getTop()->addChild(GC);
globalVariables->pushNode(GC);
}
}
}
Viewer
Viewer.h
Viewer.cpp
Generated on Mon Sep 8 16:35:28 2003 for X3DToolKit by
1.3