#include "osgWidget.h"
#include <QHBoxLayout>
#include <osg/LineWidth>
osgWidget::osgWidget(osgViewer::ViewerBase::ThreadingModel threadingModel)
:QWidget()
{
setThreadingModel(threadingModel); //qt5 must single thread.else be crash
setKeyEventSetsDone(0);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
QWidget *mainWidget = getWidgetFromGraphicWinQt(createGraphicWindow(20, 20 ,640, 480), makeCoordinate());
mainLayout->addWidget(mainWidget);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(update()));
m_timer.start(10); //刷新(重绘)频率
}
osgWidget::~osgWidget()
{
}
osg::Geode *osgWidget::makeCoordinate()
{
//创建保存几何信息的对象
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
//创建四个顶点
osg::ref_ptr<osg::Vec3Array> vPoint = new osg::Vec3Array();
vPoint->push_back(osg::Vec3(300, 280, 200));
vPoint->push_back(osg::Vec3(200, 360, 0)); //1
vPoint->push_back(osg::Vec3(300, 280, 200));
vPoint->push_back(osg::Vec3(300, 200, 0)); //2
vPoint->push_back(osg::Vec3(300, 280, 200));
vPoint->push_back(osg::Vec3(400, 360, 0)); //3
vPoint->push_back(osg::Vec3(200, 360, 0));
vPoint->push_back(osg::Vec3(300, 200, 0)); //t-l
vPoint->push_back(osg::Vec3(200, 360, 0));
vPoint->push_back(osg::Vec3(400, 360, 0)); //t-r
vPoint->push_back(osg::Vec3(300, 200, 0));
vPoint->push_back(osg::Vec3(400, 360, 0)); //l-r
geom->setVertexArray(vPoint.get());
//为每个顶点指定一种颜色
osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array();
c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); //坐标原点为蓝色
c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); //z blue
c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); //坐标原点为蓝色
c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); //z blue
c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); //坐标原点为蓝色
c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); //z blue
//如果没指定颜色则会变为黑色
geom->setColorArray(c.get());
geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
//三个轴
geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 2)); //Y
geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::LINES, 2, 2)); //Y
geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::LINES, 4, 2)); //Z
geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::LINES, 6, 2)); //Y
geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::LINES, 8, 2)); //Y
geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::LINES, 10, 2)); //Z
osg::ref_ptr<osgText::Text> pTextOAuxis = new osgText::Text;
pTextOAuxis->setText(QString("O(300,280,200)").toStdWString().c_str());
pTextOAuxis->setFont("Fonts/simhei.ttf");
pTextOAuxis->setAxisAlignment(osgText::Text::SCREEN);
pTextOAuxis->setCharacterSize(10);
pTextOAuxis->setPosition(osg::Vec3(250, 280, 200));
osg::ref_ptr<osgText::Text> pTextAAuxis = new osgText::Text;
pTextAAuxis->setText(QString("A(200,360,0)").toStdWString().c_str());
pTextAAuxis->setFont("Fonts/simhei.ttf");
pTextAAuxis->setAxisAlignment(osgText::Text::SCREEN);
pTextAAuxis->setCharacterSize(10);
pTextAAuxis->setPosition(osg::Vec3(150, 360, 0));
osg::ref_ptr<osgText::Text> pTextBAuxis = new osgText::Text;
pTextBAuxis->setText(QString("B(300,200,0)").toStdWString().c_str());
pTextBAuxis->setFont("Fonts/simhei.ttf");
pTextBAuxis->setAxisAlignment(osgText::Text::SCREEN);
pTextBAuxis->setCharacterSize(10);
pTextBAuxis->setPosition(osg::Vec3(250, 200, 0));
osg::ref_ptr<osgText::Text> pTextCAuxis = new osgText::Text;
pTextCAuxis->setText(QString("C(400,360,0)").toStdWString().c_str());
pTextCAuxis->setFont("Fonts/simhei.ttf");
pTextCAuxis->setAxisAlignment(osgText::Text::SCREEN);
pTextCAuxis->setCharacterSize(10);
pTextCAuxis->setPosition(osg::Vec3(350, 360, 0));
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
geode->getOrCreateStateSet()->setAttribute(new osg::LineWidth(3.0), osg::StateAttribute::ON);
//geode->addDrawable(pShapeDrawable.get());
geode->addDrawable(geom.get());
geode->addDrawable(pTextOAuxis.get());
geode->addDrawable(pTextAAuxis.get());
geode->addDrawable(pTextBAuxis.get());
geode->addDrawable(pTextCAuxis.get());
return geode.release();
}
osgQt::GraphicsWindowQt* osgWidget::createGraphicWindow(int x, int y, int width, int height, const QString strName, bool bWindosDecoration)
{
osg::DisplaySettings *ds = osg::DisplaySettings::instance().get();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->x = x;
traits->y = y;
traits->width = width;
traits->height = height;
traits->doubleBuffer = true;
traits->alpha = ds->getMinimumNumAccumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = ds->getNumMultiSamples();
return new osgQt::GraphicsWindowQt(traits.get());
}
QWidget *osgWidget::getWidgetFromGraphicWinQt(osgQt::GraphicsWindowQt *winQt, osg::Node *scene)
{
osgViewer::View *view = new osgViewer::View;
addView(view);
osg::Camera *camera = view->getCamera();
camera->setGraphicsContext(winQt);
const osg::GraphicsContext::Traits *pTrait = winQt->getTraits();
camera->setClearColor(osg::Vec4(0.2, 0.2, 0.6, 1.0));
camera->setViewport(new osg::Viewport(0, 0, pTrait->width, pTrait->height));
camera->setProjectionMatrixAsPerspective(30, static_cast<double>(pTrait->width) / static_cast<double>(pTrait->height), 1.0, 10000);
view->setSceneData(scene);
view->addEventHandler(new osgViewer::StatsHandler);
view->setCameraManipulator(new osgGA::TrackballManipulator);
return winQt->getGLWidget();
}
void osgWidget::paintEvent(QPaintEvent *e)
{
frame();
}