Added osgFX - Marco Jez's special effects nodekit.

This commit is contained in:
Robert Osfield
2003-08-26 21:17:22 +00:00
parent a782cbad60
commit 2c4c2ad068
22 changed files with 3520 additions and 157 deletions

View File

@@ -0,0 +1,95 @@
#include "Frame.h"
#include <osgText/Text>
Frame::Frame()
: osg::Geode(),
bgcolor_(0.5f, 0.5f, 0.5f, 1.0f),
rect_(0, 0, 100, 100),
caption_("Frame")
{
}
Frame::Frame(const Frame &copy, const osg::CopyOp &copyop)
: osg::Geode(copy, copyop),
bgcolor_(copy.bgcolor_),
rect_(copy.rect_),
caption_(copy.caption_)
{
}
void Frame::rebuild()
{
removeDrawable(0, getNumDrawables());
addDrawable(build_quad(rect_, bgcolor_));
addDrawable(build_quad(Rect(rect_.x0 + 4, rect_.y1 - 24, rect_.x1 - 4, rect_.y1 - 4), osg::Vec4(0, 0, 0, bgcolor_.w()), false, 0.1f));
osg::ref_ptr<osgText::Text> caption_text = new osgText::Text;
caption_text->setText(caption_);
caption_text->setColor(osg::Vec4(1, 1, 1, 1));
caption_text->setAlignment(osgText::Text::CENTER_CENTER);
caption_text->setFont("arial.ttf");
caption_text->setCharacterSize(16);
caption_text->setFontResolution(16, 16);
caption_text->setPosition(osg::Vec3((rect_.x0 + rect_.x1) / 2, rect_.y1 - 15, 0.2f));
addDrawable(caption_text.get());
rebuild_client_area(Rect(rect_.x0 + 4, rect_.y0 + 4, rect_.x1 - 4, rect_.y1 - 28));
}
osg::Geometry *Frame::build_quad(const Rect &rect, const osg::Vec4 &color, bool shadow, float z)
{
const float shadow_space = 8;
const float shadow_size = 10;
osg::ref_ptr<osg::Geometry> geo = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> vx = new osg::Vec3Array;
vx->push_back(osg::Vec3(rect.x0, rect.y0, z));
vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
vx->push_back(osg::Vec3(rect.x1, rect.y1, z));
vx->push_back(osg::Vec3(rect.x0, rect.y1, z));
if (shadow) {
vx->push_back(osg::Vec3(rect.x0+shadow_space, rect.y0-shadow_size, z));
vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z));
vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
vx->push_back(osg::Vec3(rect.x0+shadow_space, rect.y0, z));
vx->push_back(osg::Vec3(rect.x1, rect.y1-shadow_space, z));
vx->push_back(osg::Vec3(rect.x1, rect.y0, z));
vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y0-shadow_size, z));
vx->push_back(osg::Vec3(rect.x1+shadow_size, rect.y1-shadow_space, z));
}
geo->setVertexArray(vx.get());
osg::ref_ptr<osg::Vec4Array> clr = new osg::Vec4Array;
clr->push_back(color);
clr->push_back(color);
clr->push_back(color);
clr->push_back(color);
if (shadow) {
float alpha = color.w() * 0.5f;
const osg::Vec3 black(0, 0, 0);
clr->push_back(osg::Vec4(black, 0));
clr->push_back(osg::Vec4(black, 0));
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, alpha));
clr->push_back(osg::Vec4(black, 0));
clr->push_back(osg::Vec4(black, 0));
}
geo->setColorArray(clr.get());
geo->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
geo->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, shadow? 12: 4));
return geo.take();
}

View File

@@ -0,0 +1,47 @@
#ifndef FRAME_H_
#define FRAME_H_
#include <osg/Geode>
#include <osg/Geometry>
struct Rect {
float x0, y0, x1, y1;
Rect() {}
Rect(float x0_, float y0_, float x1_, float y1_): x0(x0_), y0(y0_), x1(x1_), y1(y1_) {}
inline float width() const { return x1 - x0; }
inline float height() const { return y0 - y1; }
};
class Frame: public osg::Geode {
public:
Frame();
Frame(const Frame &copy, const osg::CopyOp &copyop = osg::CopyOp::SHALLOW_COPY);
META_Node(osgfxbrowser, Frame);
inline const std::string &getCaption() const { return caption_; }
inline void setCaption(const std::string &caption) { caption_ = caption; }
inline const osg::Vec4 &getBackgroundColor() const { return bgcolor_; }
inline void setBackgroundColor(const osg::Vec4 &bgcolor) { bgcolor_ = bgcolor; }
inline const Rect &getRect() const { return rect_; }
inline void setRect(const Rect &rect) { rect_ = rect; }
static osg::Geometry *build_quad(const Rect &rect, const osg::Vec4 &color, bool shadow = true, float z = 0);
virtual void rebuild();
protected:
virtual ~Frame() {}
Frame &operator()(const Frame &) { return *this; }
virtual void rebuild_client_area(const Rect & /*client_rect*/) {}
private:
osg::Vec4 bgcolor_;
Rect rect_;
std::string caption_;
};
#endif

View File

@@ -0,0 +1,18 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgfxbrowser.cpp\
Frame.cpp\
LIBS += -losgProducer -lProducer -losgText -losgGA -losgDB -losgUtil -losg -losgFX $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
INSTFILES = \
$(CXXFILES)\
GNUmakefile.inst=GNUmakefile
EXEC = osgfxbrowser
INC += $(X_INC)
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,15 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgfxbrowser.cpp\
Frame.cpp\
LIBS += -losgProducer -lProducer -losgText -losgDB -losgText -losgUtil -losg -losgFX $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
EXEC = osgfxbrowser
INC += $(PRODUCER_INCLUDE_DIR) $(X_INC)
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,376 @@
#include <osg/Group>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/Projection>
#include <osg/MatrixTransform>
#include <osg/BlendFunc>
#include <osg/LightSource>
#include <osgProducer/Viewer>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgText/Text>
#include <osgUtil/Optimizer>
#include <osgGA/GUIEventAdapter>
#include <osgGA/GUIActionAdapter>
#include <osgFX/Registry>
#include <osgFX/Effect>
#include "Frame.h"
#include <vector>
#include <string>
class RotateCallback: public osg::NodeCallback {
public:
RotateCallback(): osg::NodeCallback(), enabled_(true) {}
void operator()(osg::Node *node, osg::NodeVisitor *nv)
{
osg::MatrixTransform *xform = dynamic_cast<osg::MatrixTransform *>(node);
if (xform && enabled_) {
double t = nv->getFrameStamp()->getReferenceTime();
xform->setMatrix(osg::Matrix::rotate(t, osg::Vec3(0, 0, 1)));
}
traverse(node, nv);
}
bool enabled_;
};
// yes, I know global variables are not good things in C++
// but in this case it is useful... :-P
RotateCallback *rotate_cb;
class EffectPanel: public Frame {
public:
class KeyboardHandler: public osgGA::GUIEventHandler {
public:
KeyboardHandler(EffectPanel *ep): ep_(ep) {}
bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &)
{
if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN) {
if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Right) {
ep_->setEffectIndex(ep_->getEffectIndex()+1);
return true;
}
if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Left) {
ep_->setEffectIndex(ep_->getEffectIndex()-1);
return true;
}
if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Return) {
ep_->setNodeMask(0xffffffff - ep_->getNodeMask());
return true;
}
if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Delete) {
ep_->setEffectsEnabled(!ep_->getEffectsEnabled());
return true;
}
if (ea.getKey() == 'x') {
osgDB::writeNodeFile(*ep_->getRoot(), "osgfx_model.osg");
std::cout << "written nodes to \"osgfx_model.osg\"\n";
return true;
}
if (ea.getKey() == 'r') {
rotate_cb->enabled_ = !rotate_cb->enabled_;
return true;
}
}
return false;
}
private:
osg::ref_ptr<EffectPanel> ep_;
};
EffectPanel()
: Frame(),
selected_fx_(-1),
fxen_(true),
root_(new osg::Group),
hints_color_(0.75f, 0.75f, 0.75f, 1.0f),
name_color_(1, 1, 1, 1),
desc_color_(1, 1, 0.7f, 1)
{
setBackgroundColor(osg::Vec4(0.3f, 0.1f, 0.15f, 0.75f));
std::cout << "INFO: available osgFX effects:\n";
osgFX::Registry::Effect_map emap = osgFX::Registry::instance()->getEffectMap();
for (osgFX::Registry::Effect_map::const_iterator i=emap.begin(); i!=emap.end(); ++i) {
std::cout << "INFO: \t" << i->first << "\n";
osg::ref_ptr<osgFX::Effect> effect = static_cast<osgFX::Effect *>(i->second->cloneType());
effects_.push_back(effect.get());
}
std::cout << "INFO: " << emap.size() << " effect(s) ready.\n";
if (!effects_.empty()) {
selected_fx_ = 0;
}
}
inline osg::Group *getRoot() { return root_.get(); }
inline void setRoot(osg::Group *node) { root_ = node; }
inline osg::Node *getScene() { return scene_.get(); }
inline void setScene(osg::Node *node) { scene_ = node; }
inline bool getEffectsEnabled() const { return fxen_; }
inline void setEffectsEnabled(bool v)
{
fxen_ = v;
if (getSelectedEffect()) {
getSelectedEffect()->setEnabled(fxen_);
}
}
inline int getEffectIndex() const { return selected_fx_; }
inline void setEffectIndex(int i)
{
if (i >= static_cast<int>(effects_.size())) i = 0;
if (i < 0) i = static_cast<int>(effects_.size()-1);
selected_fx_ = i;
rebuild();
}
inline osgFX::Effect *getSelectedEffect()
{
if (selected_fx_ >= 0 && selected_fx_ < static_cast<int>(effects_.size())) {
return effects_[selected_fx_].get();
}
return 0;
}
protected:
void rebuild_client_area(const Rect &client_rect)
{
osg::ref_ptr<osgText::Font> arial = osgText::readFontFile("fonts/arial.ttf");
osg::ref_ptr<osgText::Text> hints = new osgText::Text;
hints->setFont(arial.get());
hints->setColor(hints_color_);
hints->setAlignment(osgText::Text::CENTER_BOTTOM);
hints->setCharacterSize(13);
hints->setFontResolution(13, 13);
hints->setPosition(osg::Vec3((client_rect.x0+client_rect.x1)/2, client_rect.y0 + 4, 0.1f));
hints->setText("<RETURN> show/hide this panel <LEFT> previous effect <RIGHT> next effect <DEL> enable/disable effects 'x' save to file 'r' rotate/stop");
addDrawable(hints.get());
std::string effect_name = "No Effect Selected";
std::string effect_description = "";
if (selected_fx_ >= 0 && selected_fx_ < static_cast<int>(effects_.size())) {
effect_name = effects_[selected_fx_]->effectName();
std::string author_name = effects_[selected_fx_]->effectAuthor();
if (!author_name.empty()) {
effect_description = author_name = "AUTHOR: " + std::string(effects_[selected_fx_]->effectAuthor()) + std::string("\n\n");
}
effect_description += "DESCRIPTION:\n" + std::string(effects_[selected_fx_]->effectDescription());
if (scene_.valid() && root_.valid()) {
root_->removeChild(0, root_->getNumChildren());
osg::ref_ptr<osgFX::Effect> effect = effects_[selected_fx_].get();
effect->setEnabled(fxen_);
effect->setChild(scene_.get());
effect->setUpDemo();
root_->addChild(effect.get());
}
}
osg::ref_ptr<osgText::Text> ename = new osgText::Text;
ename->setFont(arial.get());
ename->setColor(name_color_);
ename->setAlignment(osgText::Text::CENTER_TOP);
ename->setCharacterSize(32);
ename->setFontResolution(32, 32);
ename->setPosition(osg::Vec3((client_rect.x0 + client_rect.x1) / 2, client_rect.y1 - 22, 0.1f));
ename->setText(effect_name);
addDrawable(ename.get());
osg::ref_ptr<osgText::Text> edesc = new osgText::Text;
edesc->setMaximumWidth(client_rect.width() - 16);
edesc->setFont(arial.get());
edesc->setColor(desc_color_);
edesc->setAlignment(osgText::Text::LEFT_TOP);
edesc->setCharacterSize(16);
edesc->setFontResolution(16, 16);
edesc->setPosition(osg::Vec3(client_rect.x0 + 8, client_rect.y1 - 60, 0.1f));
edesc->setText(effect_description);
addDrawable(edesc.get());
}
private:
int selected_fx_;
typedef std::vector<osg::ref_ptr<osgFX::Effect> > Effect_list;
Effect_list effects_;
bool fxen_;
osg::ref_ptr<osg::Group> root_;
osg::ref_ptr<osg::Node> scene_;
osg::Vec4 hints_color_;
osg::Vec4 name_color_;
osg::Vec4 desc_color_;
};
osg::Group *build_hud_base(osg::Group *root)
{
osg::ref_ptr<osg::Projection> proj = new osg::Projection(osg::Matrix::ortho2D(0, 1024, 0, 768));
proj->setCullingActive(false);
root->addChild(proj.get());
osg::ref_ptr<osg::MatrixTransform> xform = new osg::MatrixTransform(osg::Matrix::identity());
xform->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE);
proj->addChild(xform.get());
osg::StateSet *ss = xform->getOrCreateStateSet();
ss->setRenderBinDetails(100, "RenderBin");
ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
ss->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
osg::ref_ptr<osg::BlendFunc> bf = new osg::BlendFunc;
ss->setAttributeAndModes(bf.get());
return xform.take();
}
EffectPanel *build_gui(osg::Group *root)
{
osg::ref_ptr<osg::Group> hud = build_hud_base(root);
osg::ref_ptr<EffectPanel> effect_panel = new EffectPanel;
effect_panel->setCaption("osgFX Effect Browser");
effect_panel->setRect(Rect(20, 20, 1000, 280));
hud->addChild(effect_panel.get());
return effect_panel.take();
}
void build_world(osg::Group *root, osg::Node *scene, osgProducer::Viewer &viewer)
{
osg::ref_ptr<EffectPanel> effect_panel = build_gui(root);
effect_panel->setScene(scene);
effect_panel->rebuild();
viewer.getEventHandlerList().push_front(new EffectPanel::KeyboardHandler(effect_panel.get()));
root->addChild(effect_panel->getRoot());
}
int main(int argc, char *argv[])
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc, argv);
// set up the usage document, in case we need to print out how to use this program.
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName() + " is a simple browser that allows you to apply osgFX effects to models interactively.");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName() + " [options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("-h or --help", "Display this information");
arguments.getApplicationUsage()->addKeyboardMouseBinding("Left", "Apply previous effect");
arguments.getApplicationUsage()->addKeyboardMouseBinding("Right", "Apply next effect");
arguments.getApplicationUsage()->addKeyboardMouseBinding("Del", "Enable or disable osgFX");
arguments.getApplicationUsage()->addKeyboardMouseBinding("Return", "Show or hide the effect information panel");
arguments.getApplicationUsage()->addKeyboardMouseBinding("x", "Save the scene graph with current effect applied");
// construct the viewer.
osgProducer::Viewer viewer(arguments);
// set up the value with sensible default event handlers.
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
// get details on keyboard and mouse bindings used by the viewer.
viewer.getUsage(*arguments.getApplicationUsage());
// if user request help write it out to cout.
if (arguments.read("-h") || arguments.read("--help")) {
arguments.getApplicationUsage()->write(std::cout);
return 1;
}
// any option left unread are converted into errors to write out later.
arguments.reportRemainingOptionsAsUnrecognized();
// report any errors if they have occured when parsing the program aguments.
if (arguments.errors()) {
arguments.writeErrorMessages(std::cout);
return 1;
}
if (arguments.argc() <= 1) {
arguments.getApplicationUsage()->write(std::cout, osg::ApplicationUsage::COMMAND_LINE_OPTION);
return 1;
}
osg::Timer timer;
osg::Timer_t start_tick = timer.tick();
// read the scene from the list of file specified commandline args.
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
// if no model has been successfully loaded report failure.
if (!loadedModel) {
std::cout << arguments.getApplicationName() << ": No data loaded" << std::endl;
return 1;
}
osg::Timer_t end_tick = timer.tick();
std::cout << "Time to load = " << timer.delta_s(start_tick,end_tick) << std::endl;
// optimize the scene graph, remove rendundent nodes and state etc.
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel.get());
// set up a transform to rotate the model
osg::ref_ptr<osg::MatrixTransform> xform = new osg::MatrixTransform;
rotate_cb = new RotateCallback;
xform->setUpdateCallback(rotate_cb);
xform->addChild(loadedModel.get());
osg::ref_ptr<osg::Light> light = new osg::Light;
light->setLightNum(0);
light->setDiffuse(osg::Vec4(1, 1, 1, 1));
light->setPosition(osg::Vec4(1, -1, 1, 0));
osg::ref_ptr<osg::LightSource> root = new osg::LightSource;
root->setLight(light.get());
root->setLocalStateSetModes();
build_world(root.get(), xform.get(), viewer);
// set the scene to render
viewer.setSceneData(root.get());
// create the windows and run the threads.
viewer.realize();
while(!viewer.done())
{
// wait for all cull and draw threads to complete.
viewer.sync();
// update the scene by traversing it with the the update visitor which will
// call all node update callbacks and animations.
viewer.update();
// fire off the cull and draw traversals of the scene.
viewer.frame();
}
// wait for all cull and draw threads to complete before exit.
viewer.sync();
return 0;
}

View File

@@ -13,7 +13,557 @@
#include <osgUtil/Optimizer>
#include <osgProducer/Viewer>
// main viewer code at bottom of file.
// experimental templated rendering code, please ignore...
// will move to osg::Geometry once complete.
// Robert Osfield, August 2003.
#if 0
struct DrawArrays
{
virtual void draw() const = 0;
};
struct DrawVertex
{
inline void operator () (const osg::Vec2* v) const { glVertex2fv(v->ptr()); }
inline void operator () (const osg::Vec3* v) const { glVertex3fv(v->ptr()); }
inline void operator () (const osg::Vec4* v) const { glVertex4fv(v->ptr()); }
};
struct DrawNormal
{
inline void operator () (const osg::Vec3* v) const { glNormal3fv(v->ptr()); }
};
struct DrawColor
{
inline void operator () (const osg::Vec4* v) const { glColor4fv(v->ptr()); }
};
struct DrawTexCoord
{
inline void operator () (const osg::Vec2* v) const { glTexCoord2fv(v->ptr()); }
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template< class F1, class T1>
struct DrawFunctor_T : public DrawArrays
{
inline DrawFunctor_T(T1* begin1, T1* end1): _begin1(begin1),_end1(end1) {}
virtual void draw() const
{
for(T1* ptr1=_begin1;
ptr1!=_end1;
++ptr1)
{
F1(ptr1);
}
}
T1* _begin1;
T1* _end1;
};
template< class F1, class T1, class I1>
struct DrawFunctor_TI : public DrawArrays
{
inline DrawFunctor_TI(T1* begin1, I1* ibegin1, I1* iend1): _begin1(begin1),_ibegin1(ibegin1),_iend1(end1) {}
virtual void draw() const
{
for(I1* iptr1=_ibegin1;
iptr1!=_iend1;
++iptr1)
{
F1(_begin1[*iptr1]);
}
}
T1* _begin1;
I1* _ibegin1;
I1* _iend1;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template< class F1, class T1, class F2, class T2>
struct DrawFunctor_TT : public DrawArrays
{
inline DrawFunctor_TT(T1* begin1, T1* end1, T2* begin2): _begin1(begin1),_end1(end1),_begin2(begin2) {}
virtual void draw() const
{
T2* ptr2=_begin2;
for(T1* ptr1=_begin1;
ptr1!=_end1;
++ptr1,++ptr2)
{
F2(ptr2);
F1(ptr1);
}
}
T1* _begin1;
T1* _end1;
T2* _begin2;
};
template< class F1, class T1, class I1, class F2, class T2>
struct DrawFunctor_TIT : public DrawArrays
{
inline DrawFunctor_TIT(T1* begin1, I1* ibegin1, I1* iend1, T2* begin2): _begin1(begin1),_ibegin1(ibegin1),_iend1(end1) {}
virtual void draw() const
{
T2* ptr2=_begin2;
for(I1* iptr1=_ibegin1;
iptr1!=_iend1;
++iptr1,++ptr2)
{
F2(_ptr2);
F1(_begin1[*iptr1]);
}
}
T1* _begin1;
I1* _ibegin1;
I1* _iend1;
T2* _begin2;
};
template< class F1, class T1, class F2, class T2, class I2>
struct DrawFunctor_TTI : public DrawArrays
{
inline DrawFunctor_TTI(T1* begin1, T1* end1, T2* begin2, I2* ibegin2): _begin1(begin1),_end1(end1),_begin2(begin2),_ibegin2(ibegin2) {}
virtual void draw() const
{
I2* iptr2 = _ibegin2;
for(T1* ptr1=_begin1;
ptr1!=_end1;
++ptr1,++iptr2)
{
F2(_begin2[*iptr2]);
F1(ptr1);
}
}
T1* _begin1;
T1* _end1;
T2* _begin2;
I2* _ibegin2;
};
template< class F1, class T1, class F2, class I1, class T2, class I2>
struct DrawFunctor_TITI : public DrawArrays
{
inline DrawFunctor_TITI(T1* begin1, I1* ibegin1, I2* iend1, T2* begin2, I2* ibegin2): _begin1(begin1),_ibegin1(ibegin1), _iend1(end1),_begin2(begin2),_ibegin2(ibegin2) {}
virtual void draw() const
{
I2* iptr2 = _ibegin2;
for(T1* iptr1=_ibegin1;
iptr1!=_iend1;
++iptr1,++iptr2)
{
F2(_begin2[*iptr2]);
F1(_begin1[*iptr1]);
}
}
T1* _begin1;
I1* _ibegin1;
I1* _iend1;
T2* _begin2;
I2* _ibegin2;
};
template< class F1, class T1, class F2, class T2, class I>
struct DrawFunctor_TT_I : public DrawArrays
{
inline DrawFunctor_TT_I(T1* begin1, T2* begin2, I* ibegin, I* iend): _begin1(begin1),_begin2(begin2),_ibegin(ibegin),_iend(iend) {}
virtual void draw() const
{
I index;
for(I* iptr=_ibegin;
iptr!=_iend;
++iptr)
{
index = *iptr;
F2(_begin2[index]);
F1(_begin1[index]);
}
}
T1* _begin1;
T2* _begin2;
I* _ibegin;
I* _iend;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template< class F1, class T1, class F2, class T2, class F3, class T3>
struct DrawFunctor_TTT : public DrawArrays
{
inline DrawFunctor_TTT(T1* begin1, T1* end1, T2* begin2, T3* begin): _begin1(begin1),_end1(end1),_begin2(begin2),_begin3(begin3) {}
virtual void draw() const
{
T2* ptr2=_begin2;
T3* ptr3=_begin3;
for(T1* ptr1=_begin1;
ptr1!=_end1;
++ptr1,++ptr2,++ptr3)
{
F3(ptr3);
F2(ptr2);
F1(ptr1);
}
}
T1* _begin1;
T1* _end1;
T2* _begin2;
T3* _begin3;
};
template< class F1, class T1, class I1, class F2, class T2, class F3, class T3>
struct DrawFunctor_TITT : public DrawArrays
{
inline DrawFunctor_TITT(T1* begin1, I1* ibegin1, I1* iend1, T2* begin2, T3* begin): _begin1(begin1),_ibegin1(ibegin1),_iend1(end1),_begin3(begin3) {}
virtual void draw() const
{
T2* ptr2=_begin2;
T3* ptr3=_begin3;
for(I1* iptr1=_ibegin1;
iptr1!=_iend1;
++iptr1,++ptr2,++ptr3)
{
F3(_ptr3);
F2(_ptr2);
F1(_begin1[*iptr1]);
}
}
T1* _begin1;
I1* _ibegin1;
I1* _iend1;
T2* _begin2;
T3* _begin3;
};
template< class F1, class T1, class F2, class T2, class I2, class F3, class T3>
struct DrawFunctor_TTIT : public DrawArrays
{
inline DrawFunctor_TTIT(T1* begin1, T1* end1, T2* begin2, I2* ibegin2, T3* begin): _begin1(begin1),_end1(end1),_begin2(begin2),_ibegin2(ibegin2),_begin3(begin3) {}
virtual void draw() const
{
I2* iptr2 = _ibegin2;
T3* ptr3 = _begin3;
for(T1* ptr1=_begin1;
ptr1!=_end1;
++ptr1,++iptr2,++ptr3)
{
F3(ptr3);
F2(_begin2[*iptr2]);
F1(ptr1);
}
}
T1* _begin1;
T1* _end1;
T2* _begin2;
I2* _ibegin2;
T3* _begin3;
};
template< class F1, class T1, class F2, class I1, class T2, class I2, class F3, class T3>
struct DrawFunctor_TITIT : public DrawArrays
{
inline DrawFunctor_TITIT(T1* begin1, I1* ibegin1, I2* iend1, T2* begin2, I2* ibegin2, T3* begin3): _begin1(begin1),_ibegin1(ibegin1), _iend1(end1),_begin2(begin2),_ibegin2(ibegin2),_begin3(begin3) {}
virtual void draw() const
{
I2* iptr2 = _ibegin2;
T3* ptr3 = _begin3;
for(I1* iptr1=_ibegin1;
iptr1!=_iend1;
++iptr1,++iptr2,++ptr3)
{
F3(ptr3);
F2(_begin2[*iptr2]);
F1(_begin1[*iptr1]);
}
}
T1* _begin1;
I1* _ibegin1;
I1* _iend1;
T2* _begin2;
I2* _ibegin2;
T3* _begin3;
};
template< class F1, class T1, class F2, class T2, class I2, class F3, class T3, class I3>
struct DrawFunctor_TTITI : public DrawArrays
{
inline DrawFunctor_TTITI(T1* begin1, T1* end1, T2* begin2, I2* ibegin2, T3* begin3, I3* ibegin3): _begin1(begin1),_end1(end1),_begin2(begin2),_ibegin2(ibegin2),_begin3(begin3),_ibegin3(begin3) {}
virtual void draw() const
{
I2* iptr2 = _ibegin2;
I3* iptr3 = _ibegin3;
for(T1* ptr1=_begin1;
ptr1!=_end1;
++ptr1,++iptr2,++iptr3)
{
F3(_begin3[*iptr3]);
F2(_begin2[*iptr2]);
F1(ptr1);
}
}
T1* _begin1;
T1* _end1;
T2* _begin2;
I2* _ibegin2;
T3* _begin3;
I3* _ibegin3;
};
template< class F1, class T1, class I1, class F2, class T2, class I2, class F3, class T3, class I3>
struct DrawFunctor_TITITI : public DrawArrays
{
inline DrawFunctor_TITITI(T1* begin1, I1* ibegin1, I2* iend1, T2* begin2, I2* ibegin2, T3* begin3, I3* ibegin3): _begin1(begin1),_ibegin1(ibegin1), _iend1(end1),_begin2(begin2),_ibegin2(ibegin2),_begin3(begin3),_ibegin3(begin3) {}
virtual void draw() const
{
I2* iptr2 = _ibegin2;
I3* iptr3 = _ibegin3;
for(I1* iptr1=_ibegin1;
iptr1!=_iend1;
++iptr1,++iptr2,++iptr3)
{
F3(_begin3[*iptr3]);
F2(_begin2[*iptr2]);
F1(_begin1[*iptr1]);
}
}
T1* _begin1;
I1* _ibegin1;
I1* _iend1;
T2* _begin2;
I2* _ibegin2;
T3* _begin3;
I3* _ibegin3;
};
template< class F1, class T1, class F2, class T2, class F3, class T3, class I>
struct DrawFunctor_TTT_I : public DrawArrays
{
inline DrawFunctor_TTT_I(T1* begin1, T2* begin2, T3* begin3, I* ibegin, I* iend): _begin1(begin1),_begin2(begin2),_begin3(begin3),_ibegin(ibegin),_iend(iend) {}
virtual void draw() const
{
I index;
for(I* iptr=_ibegin;
iptr!=_iend;
++iptr)
{
index = *iptr;
F3(_begin3[index]);
F2(_begin2[index]);
F1(_begin1[index]);
}
}
T1* _begin1;
T2* _begin2;
T3* _begin3;
I* _ibegin;
I* _iend;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// single vertex attribute
typedef DrawFunctor_T<DrawVertex,osg::Vec3> V3;
typedef DrawFunctor_TI<DrawVertex,osg::Vec3,unsigned short> V3i;
// two attributes - vertex + one other attribute
typedef DrawFunctor_TT<DrawVertex,osg::Vec3,DrawNormal,osg::Vec3> V3N3;
typedef DrawFunctor_TT<DrawVertex,osg::Vec3,DrawColor,osg::Vec4> V3C4;
typedef DrawFunctor_TT<DrawVertex,osg::Vec3,DrawTexCoord,osg::Vec2> V3T2;
typedef DrawFunctor_TIT<DrawVertex,osg::Vec3,unsigned short,DrawNormal,osg::Vec3> V3iN3;
typedef DrawFunctor_TIT<DrawVertex,osg::Vec3,unsigned short,DrawColor,osg::Vec4> V3iC4;
typedef DrawFunctor_TIT<DrawVertex,osg::Vec3,unsigned short,DrawTexCoord,osg::Vec2> V3iT2;
typedef DrawFunctor_TTI<DrawVertex,osg::Vec3,DrawNormal,osg::Vec3,unsigned short> V3N3i;
typedef DrawFunctor_TTI<DrawVertex,osg::Vec3,DrawColor,osg::Vec4,unsigned short> V3C4i;
typedef DrawFunctor_TTI<DrawVertex,osg::Vec3,DrawTexCoord,osg::Vec2,unsigned short> V3T2i;
typedef DrawFunctor_TT_I<DrawVertex,osg::Vec3,DrawNormal,osg::Vec3,unsigned short> V3N3_i;
typedef DrawFunctor_TT_I<DrawVertex,osg::Vec3,DrawColor,osg::Vec4,unsigned short> V3C4_i;
typedef DrawFunctor_TT_I<DrawVertex,osg::Vec3,DrawTexCoord,osg::Vec2,unsigned short> V3T2_i;
// three attributes - vertex + two other attributes.
typedef DrawFunctor_TTT<DrawVertex,osg::Vec3,DrawNormal,osg::Vec3,DrawColor,osg::Vec4> V3N3C4;
typedef DrawFunctor_TTT<DrawVertex,osg::Vec3,DrawNormal,osg::Vec3,DrawTexCoord,osg::Vec2> V3N3T2;
typedef DrawFunctor_TTT<DrawVertex,osg::Vec3,DrawColor,osg::Vec4,DrawTexCoord,osg::Vec2> V3C4T2;
typedef DrawFunctor_TITT<DrawVertex,osg::Vec3,unsigned short,DrawNormal,osg::Vec3,DrawColor,osg::Vec4> V3iN3C4;
typedef DrawFunctor_TITT<DrawVertex,osg::Vec3,unsigned short,DrawNormal,osg::Vec3,DrawTexCoord,osg::Vec2> V3iN3T2;
typedef DrawFunctor_TITT<DrawVertex,osg::Vec3,unsigned short,DrawColor,osg::Vec4,DrawTexCoord,osg::Vec2> V3iC4T2;
typedef DrawFunctor_TTIT<DrawVertex,osg::Vec3,DrawNormal,osg::Vec3,unsigned short,DrawColor,osg::Vec4> V3N3iC4;
typedef DrawFunctor_TTIT<DrawVertex,osg::Vec3,DrawNormal,osg::Vec3,unsigned short,DrawTexCoord,osg::Vec2> V3N3iT2;
typedef DrawFunctor_TTIT<DrawVertex,osg::Vec3,DrawColor,osg::Vec4,unsigned short,DrawNormal,osg::Vec3> V3C4iN3;
typedef DrawFunctor_TTIT<DrawVertex,osg::Vec3,DrawColor,osg::Vec4,unsigned short,DrawTexCoord,osg::Vec2> V3C4iT2;
typedef DrawFunctor_TTIT<DrawVertex,osg::Vec3,DrawTexCoord,osg::Vec2,unsigned short,DrawColor,osg::Vec4> V3T2iC4;
typedef DrawFunctor_TTIT<DrawVertex,osg::Vec3,DrawTexCoord,osg::Vec2,unsigned short,DrawNormal,osg::Vec3> V3T2iN3;
typedef DrawFunctor_TTITI<DrawVertex,osg::Vec3,DrawNormal,osg::Vec3,unsigned short,DrawColor,osg::Vec4,unsigned short> V3N3iC4i;
typedef DrawFunctor_TTITI<DrawVertex,osg::Vec3,DrawNormal,osg::Vec3,unsigned short,DrawTexCoord,osg::Vec2,unsigned short> V3N3iT2i;
typedef DrawFunctor_TTITI<DrawVertex,osg::Vec3,DrawColor,osg::Vec4,unsigned short,DrawTexCoord,osg::Vec2,unsigned short> V3C4iT2i;
typedef DrawFunctor_TTT_I<DrawVertex,osg::Vec3,DrawNormal,osg::Vec3,DrawColor,osg::Vec4,unsigned short> V3N3C4_i;
typedef DrawFunctor_TTT_I<DrawVertex,osg::Vec3,DrawNormal,osg::Vec3,DrawTexCoord,osg::Vec2,unsigned short> V3N3T2_i;
typedef DrawFunctor_TTT_I<DrawVertex,osg::Vec3,DrawColor,osg::Vec4,DrawTexCoord,osg::Vec2,unsigned short> V3C4T2_i;
class MyDrawable : public osg::Drawable
{
public:
MyDrawable();
virtual void drawImplementation(osg::State& state);
virtual bool computeBound() const;
enum RenderingMode
{
v,
vI,// 2
vn,
vIn,
vnI,
vInI,
vn_sI, // 5
vc,
vIc,
vnc,
vInc,
vnIc,
vInIc,
vcI,
vIcI,
vncI,
vIncI,
vnIcI,
vInIcI,
vnc_sI, // 13
vt,
vIt,
vnt,
vInt,
vnIt,
vInIt,
vct,
vIct,
vnct,
vInct,
vnIct,
vInIct,
vcIt,
vIcIt,
vncIt,
vIncIt,
vnIcIt,
vInIcIt,
vtI,
vItI,
vntI,
vIntI,
vnItI,
vInItI,
vctI,
vIctI,
vnctI,
vInctI,
vnIctI,
vInIctI,
vcItI,
vIcItI,
vncItI,
vIncItI,
vnIcItI,
vInIcItI
vnct_sI // 37
}; // 57 combinations.
GLenum _primitiveType;
ref_ptr<Vec3Array> _vertices;
ref_ptr<UShortArray> _vertexIndices;
ref_ptr<Vec3Array> _normals;
ref_ptr<UShortArray> _normalIndices;
ref_ptr<Vec4Array> _colors;
ref_ptr<UShortArray> _colorIndices;
ref_ptr<Vec2Array> _texcoords;
ref_ptr<UShortArray> _texcoordIndices;
}
#endif
int main( int argc, char **argv )
{