This example plugin creates a new driver that demonstrates how to use OpenGL draw calls from a subclass of osg::Drawable that gets added to the normalized 2D camera of all DtOsgChannels.
To get the reticle geode added to any new channels, hook up signal_channelCreated from the drivers constructor:
This example is a plug-in. You can run it by running ./bin64/exampleGLDrawingPlugin_stealth.bat (on Windows) or ./bin64/exampleGLDrawingPlugin_stealth.sh (on Linux). For more information about running examples, please see Running Applications and Examples.
#ifdef _WIN32
#ifdef EXAMPLEGLDRAWINGPLUGIN_EXPORTS
#define DT_DLL_EXAMPLEGLDRAWINGPLUGIN __declspec ( dllexport )
#else
#define DT_DLL_EXAMPLEGLDRAWINGPLUGIN __declspec ( dllimport )
#endif
#else
#define DT_DLL_EXAMPLEGLDRAWINGPLUGIN
#endif
#define DT_DE_PLUGIN_EXPORT_MACRO DT_DLL_EXAMPLEGLDRAWINGPLUGIN
#include <matrix/vlMath.h>
#include <osg/Drawable>
#include <osg/Geode>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <boost/bind/bind.hpp>
using namespace makVrv;
static vrvSignalsLib::connection postInitializeConnection;
class DtCustomDrawable : public osg::Drawable
{
public:
DtCustomDrawable()
{
myIsReticleEnabled = false;
myIsCubeEnabled = false;
setSupportsDisplayList(false);
setUseDisplayList(false);
};
DtCustomDrawable(const DtCustomDrawable& drawable,
const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY) :
{
myIsReticleEnabled = drawable.myIsReticleEnabled;
myIsCubeEnabled = drawable.myIsCubeEnabled;
setSupportsDisplayList(false);
setUseDisplayList(false);
}
virtual ~DtCustomDrawable()
{
};
META_Object(test, DtCustomDrawable)
virtual void drawImplementation(osg::RenderInfo& state) const
{
if (myIsReticleEnabled)
renderReticle(state);
if (myIsCubeEnabled)
renderCube(state);
}
virtual void renderReticle(osg::RenderInfo& state) const
{
osg::Viewport* viewport = state.getCurrentCamera()->getViewport();
if (!viewport)
{
return;
}
int width = viewport->width();
int height = viewport->height();
float aspect = viewport->width() / viewport->height();
glPushMatrix();
glPushAttrib(GL_LINE_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (width >= height)
{
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
}
else
{
gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
}
int lineWidth = 10;
if ((width * .01) < lineWidth)
{
lineWidth = width * .01;
}
glLineWidth(lineWidth);
glBegin(GL_LINE_LOOP);
glColor3f(0.0f, 1.0f, 0.0f);
float radius = 0.5;
float centerX = 0.0;
float centerY = 0.0;
for (int i=0; i < 360; i++)
{
float radians = i * M_DEG2RAD;
glVertex2f(centerX + cos(radians)*radius,
centerY + sin(radians)*radius);
}
glEnd();
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 0.0f);
float extra = 0.06;
glVertex2f(centerX - (radius + extra), centerY);
glVertex2f(centerX - radius/2, centerY);
glVertex2f(centerX + (radius + extra), centerY);
glVertex2f(centerX + radius/2, centerY);
glVertex2f(centerX, centerY - (radius + extra));
glVertex2f(centerX, centerY - radius/2);
glVertex2f(centerX, centerY + (radius + extra));
glVertex2f(centerX, centerY + radius/2);
glEnd();
glLineWidth(2.0);
glBegin(GL_LINES);
glVertex2f(centerX - radius/2, centerY);
glVertex2f(centerX + radius/2, centerY);
glVertex2f(centerX, centerY - radius/2);
glVertex2f(centerX, centerY + radius/2);
glEnd();
glPopAttrib();
glPopMatrix();
}
virtual void renderCube(osg::RenderInfo& renderInfo) const
{
glPushAttrib(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_GREATER);
glPushAttrib(GL_LIGHTING_BIT);
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
osg::Matrixd projMat = renderInfo.getState()->getProjectionMatrix();
glLoadMatrixd( projMat.ptr() );
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0, 0.0, -distance);
static int frame = 0;
++frame;
float rotDeg = (float)frame / 10.0f;
glRotatef(rotDeg, 0.0f, 1.0f, 0.0f);
glRotatef(rotDeg, 1.0f, 0.0f, 0.0f);
float size = 100.0f;
glScalef(size, size, size);
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f( 0.5f, -0.5f, 0.5f);
glEnd();
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex3f( 0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f( 0.5f, -0.5f, -0.5f);
glEnd();
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f( 0.5f, 0.5f, -0.5f);
glVertex3f( 0.5f, -0.5f, -0.5f);
glVertex3f( 0.5f, -0.5f, 0.5f);
glEnd();
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f( -0.5f, 0.5f, 0.5f);
glVertex3f( -0.5f, 0.5f, -0.5f);
glVertex3f( -0.5f, -0.5f, -0.5f);
glVertex3f( -0.5f, -0.5f, 0.5f);
glEnd();
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f( 0.5f, 0.5f, -0.5f);
glVertex3f( -0.5f, 0.5f, -0.5f);
glVertex3f( -0.5f, 0.5f, 0.5f);
glEnd();
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f( 0.5f, -0.5f, 0.5f);
glVertex3f( 0.5f, -0.5f, -0.5f);
glVertex3f( -0.5f, -0.5f, -0.5f);
glVertex3f( -0.5f, -0.5f, 0.5f);
glEnd();
glPopAttrib();
glPopAttrib();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
bool myIsReticleEnabled;
bool myIsCubeEnabled;
};
{
public:
: makVrv::
DtDriver( am,
"DtExampleGLDrawingDriver" )
{
myAgentManager.de()).signal_channelCreated.connect(
boost::bind(&DtExampleGLDrawingDriver::slot_channelCreated, this,
boost::placeholders::_1, boost::placeholders::_2));
}
virtual ~DtExampleGLDrawingDriver()
{
}
virtual const std::string& className() const
{
static std::string name = "DtExampleGLDrawingDriver";
return name;
}
{
if (!osgChannel || !myReticleGeode || !myCubeGeode)
{
return;
}
}
virtual bool onStart()
{
myReticle = new DtCustomDrawable();
myReticle->myIsReticleEnabled = true;
myReticle->myIsCubeEnabled = false;
myReticleGeode = new osg::Geode();
myReticleGeode->setName("Reticle Geode");
myReticleGeode->addDrawable(myReticle);
myCube = new DtCustomDrawable();
myCube->myIsReticleEnabled = false;
myCube->myIsCubeEnabled = true;
myCubeGeode = new osg::Geode();
myCubeGeode->setName("Cube Geode");
myCubeGeode->addDrawable(myCube);
osg::StateSet* ss = myReticleGeode->getOrCreateStateSet();
ss->setAttributeAndModes(new osg::Program(), osg::StateAttribute::OFF);
ss->setTextureMode(0, GL_TEXTURE_1D, osg::StateAttribute::OFF);
ss->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
ss->setTextureMode(0, GL_TEXTURE_3D, osg::StateAttribute::OFF);
ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
ss->setMode(GL_BLEND, osg::StateAttribute::OFF);
ss->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
ss->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
myCubeGeode->setStateSet(ss);
DtWindowManager::WindowList::iterator winIter = wins.begin();
for(winIter; winIter != wins.end(); ++winIter)
{
DtChannelManager::ChannelMap::iterator chanIter=channels.begin();
for(chanIter; chanIter!=channels.end(); ++chanIter)
{
if (osgChannel)
{
}
}
}
return true;
}
virtual bool onStop()
{
return true;
}
virtual bool onTick()
{
return true;
}
protected:
osg::ref_ptr<DtCustomDrawable> myReticle;
osg::ref_ptr<osg::Geode> myReticleGeode;
osg::ref_ptr<DtCustomDrawable> myCube;
osg::ref_ptr<osg::Geode> myCubeGeode;
};
{
DtExampleGLDrawingDriver* driver =
new DtExampleGLDrawingDriver(de.
agentManager());
}
{
{
}
}
{
return true;
}