|
本帖最后由 guchengjianke 于 2016-2-13 21:50 编辑
使用QT + osgEarth2.7 搭建仿真可视化系统:
qt使用4.8.6,osg3.4, osgearth2.7 , QGraphicView没有数据驱动运行没问题,但运行中动态添加渲染对象时在debug模式下总会抛出某一处迭代器不增加的Assertion错误,而且错误callstack一直在drawbackground中,问一下有没有人遇到过这样的问题,求高手指点!
graphic中的代码如下:
#include "graphicsview.h"
#include <QObject>
GraphicsView::GraphicsView(osg::ref_ptr<osg::Group> rootnode, osg::ref_ptr<osg::Group> annorootnode,
osg::ref_ptr<osgEarth:tGui:ataManager> manager, osg::ref_ptr<osgEarth::MapNode> mapNode, osg::ref_ptr<osgViewer::Viewer> viewer,
QWidget* parent)
{
// lh fixme : 判断指针是否有效
m_pRoot = rootnode;
m_pMapNode = mapNode;
m_pAnnoRoot = annorootnode;
m_pDataManager = manager;
m_pViewer =viewer;
m_pSceneData = new osg::Group;
m_pSceneData->setName("SceneData");
m_pRoot->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
m_pRoot->addChild(m_pSceneData);
initOsgViewer();
this->currentFps = Colors::fps;
this->loop = false;
this->fpsMedian = -1;
this->fpsLabel = 0;
this->pausedLabel = 0;
this->doneAdapt = false;
this->useTimer = false;
this->updateTimer.setSingleShot(true);
this->companyLogo = 0;
this->qtLogo = 0;
this->setupWidget();
this->setupScene();
this->setupSceneItems();
}
GraphicsView::~GraphicsView()
{
release();
delete this->companyLogo;
delete this->qtLogo;
}
void GraphicsView::release()
{
if (m_pRoot)
{
m_pRoot->removeChildren(0, m_pRoot->getNumChildren());
m_pSceneData->removeChildren(0, m_pSceneData->getNumChildren());
}
if (m_pMapNode)
{
m_pMapNode = nullptr;
m_pMapSRS = nullptr;
}
m_pViewer = nullptr;
m_pEarthManipulator = nullptr;
}
void GraphicsView::init()
{
m_pRoot = new osg::Group;
m_pRoot->setName("root");
m_pSceneData = new osg::Group;
m_pSceneData->setName("SceneData");
m_pRoot->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
m_pRoot->addChild(m_pSceneData);
}
void GraphicsView::createSky()
{
}
void GraphicsView::timerEvent(QTimerEvent *event)
{
this->scene()->update();
}
void GraphicsView::drawBackground(QPainter *painter, const QRectF& rect)
{
// qtdemo中drawBackground()
Q_UNUSED(rect);
//painter->drawPixmap(QPoint(0, 0), this->background);
if (painter->paintEngine()->type() != QPaintEngine::OpenGL2)
{
return;
}
// Save the painter state
painter->save();
painter->beginNativePainting();
PreFrameUpdate();
//while (!m_pViewer->done())
//{
m_pViewer->frame();
//}
painter->endNativePainting();
painter->restore();
this->scene()->update();
//PostFrameUpdate();
}
void GraphicsView:reFrameUpdate()
{
VisualObjectManager::instance().updateEntityData();//此处添加判断模型数据是否更新完毕,更新完在进行渲染;
}
void GraphicsView::PostFrameUpdate()
{
if(getNeedModify()) getCanModify() = TRUE;
}
void GraphicsView::initOsgViewer()
{
m_pMapNode->setName("MapNode");
m_pMapSRS = m_pMapNode->getMapSRS();
//m_pViewer = new osgViewer::Viewer;
m_pEarthManipulator = new osgEarth::Util::EarthManipulator;
m_pEarthManipulator->getSettings()->setMinMaxPitch(-90.0, -7.0);
m_pViewer->setCameraManipulator(m_pEarthManipulator);
m_pViewer->addEventHandler(new osgViewer::StatsHandler);
m_pViewer->addEventHandler(new osgGA::StateSetManipulator(m_pViewer->getCamera()->getOrCreateStateSet()));
m_pViewer->getCamera()->addCullCallback(new osgEarth::Util::AutoClipPlaneCullCallback(m_pMapNode));
m_pViewer->getCamera()->setNearFarRatio(0.0000002);
m_pViewer->getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
m_pViewer->setUpViewerAsEmbeddedInWindow(0, 0, width(), height());
m_pViewer->setSceneData(m_pRoot);
m_pGraphicsWindow = dynamic_cast<osgViewer::GraphicsWindow*>(
m_pViewer->getCamera()->getGraphicsContext());
m_pEarthManipulator->setHomeViewpoint(osgEarth::Util::Viewpoint("",Colors::homeViewPoint[0], Colors::homeViewPoint[1],
Colors::homeViewPoint[2], Colors::homeViewPoint[3], Colors::homeViewPoint[4], Colors::homeViewPoint[5]));
m_pEarthManipulator->setViewpoint(osgEarth::Util::Viewpoint("",110, 33, 0, 0, -90, 2e7));
}
void GraphicsView::setupWidget()
{
QRect screenRect = QApplication::desktop()->screenGeometry(QApplication::desktop()->primaryScreen());
QRect windowRect(0, 0, Colors::winWidth, Colors::winHeight);
if (screenRect.width() < Colors::winWidth)
windowRect.setWidth(screenRect.width());
if (screenRect.height() < Colors::winHeight)
windowRect.setHeight(screenRect.height());
windowRect.moveCenter(screenRect.center());
this->setGeometry(windowRect);
this->setMinimumSize(Colors::winWidth / 10, Colors::winHeight / 10);
setWindowTitle(tr(""));
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFrameStyle(QFrame::NoFrame);
this->setRenderingSystem();
connect(&this->updateTimer, SIGNAL(timeout()), this, SLOT(tick()));
}
void GraphicsView::setRenderingSystem()
{
QWidget *viewport = 0;
#ifndef QT_NO_OPENGL
if (Colors:penGlRendering) {
QGLWidget *glw = new QGLWidget(QGLFormat(QGL::SampleBuffers));
//glw->setMouseTracking(true);
//glw->setMaximumSize(2000, 2000);
if (Colors::noScreenSync)
glw->format().setSwapInterval(0);
glw->setAutoFillBackground(false);
viewport = glw;
setCacheMode(QGraphicsView::CacheNone);
if (Colors::verbose)
qDebug() << "- using OpenGL";
} else // software rendering
#endif
{
// software rendering
viewport = new QWidget;
setCacheMode(QGraphicsView::CacheBackground);
if (Colors::verbose)
qDebug() << "- using software rendering";
}
setViewport(viewport);
//this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}
void GraphicsView::start()
{
this->switchTimerOnOff(true);
this->demoStartTime.restart();
MenuManager::instance()->itemSelected(MenuManager::ROOT, Colors::rootMenuName);
if (Colors::verbose)
qDebug("- starting demo");
}
void GraphicsView::enableMask(bool enable)
{
if (!enable || Colors::noWindowMask)
this->clearMask();
else {
QPolygon region;
region.setPoints(9,
// north side:
0, 0,
800, 0,
// east side:
// 800, 70,
// 790, 90,
// 790, 480,
// 800, 500,
800, 600,
// south side:
700, 600,
670, 590,
130, 590,
100, 600,
0, 600,
// west side:
// 0, 550,
// 10, 530,
// 10, 520,
// 0, 520,
0, 0);
this->setMask(QRegion(region));
}
}
void GraphicsView::setupScene()
{
this->scene1 = new DemoScene(this);
this->scene1->setSceneRect(0, 0, Colors::winWidth, Colors::winHeight);
setScene(this->scene1);
this->scene1->setItemIndexMethod(QGraphicsScene::NoIndex);
}
void GraphicsView::drawItems(QPainter *painter, int numItems, QGraphicsItem **items, const QStyleOptionGraphicsItem* options)
{
QGraphicsView::drawItems(painter, numItems, items, options);
}
void GraphicsView::switchTimerOnOff(bool on)
{
bool ticker = MenuManager::instance()->ticker && MenuManager::instance()->ticker->scene();
if (ticker)
MenuManager::instance()->ticker->tickOnPaint = !on || Colors::noTimerUpdate;
if (on && !Colors::noTimerUpdate){
this->useTimer = true;
this->setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
this->fpsTime = QTime::currentTime();
this->updateTimer.start(int(1000 / Colors::fps));
}
else{
this->useTimer = false;
this->updateTimer.stop();
if (Colors::softwareRendering)
if (Colors::noTicker)
this->setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);
else
this->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
else
this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}
}
bool GraphicsView::measureFps()
{
// Calculate time diff:
float t = this->fpsTime.msecsTo(QTime::currentTime());
if (t == 0)
t = 0.01f;
this->currentFps = (1000.0f / t);
this->fpsHistory += this->currentFps;
this->fpsTime = QTime::currentTime();
// Calculate median:
int size = this->fpsHistory.size();
if (size == 10){
qSort(this->fpsHistory.begin(), this->fpsHistory.end());
this->fpsMedian = this->fpsHistory.at(int(size/2));
if (this->fpsMedian == 0)
this->fpsMedian = 0.01f;
this->fpsHistory.clear();
return true;
}
return false;
}
/**
Used for adaption in case things are so slow
that no median yet has been calculated
*/
void GraphicsView::forceFpsMedianCalculation()
{
if (this->fpsMedian != -1)
return;
int size = this->fpsHistory.size();
if (size == 0){
this->fpsMedian = 0.01f;
return;
}
qSort(this->fpsHistory.begin(), this->fpsHistory.end());
this->fpsMedian = this->fpsHistory.at(int(size/2));
if (this->fpsMedian == 0)
this->fpsMedian = 0.01f;
}
void GraphicsView::tick()
{
bool medianChanged = this->measureFps();
this->checkAdapt();
if (medianChanged && this->fpsLabel && Colors::showFps)
this->fpsLabel->setText(QString("FPS: ") + QString::number(int(this->currentFps)));
if (MenuManager::instance()->ticker)
MenuManager::instance()->ticker->tick();
this->viewport()->update();
if (this->useTimer)
this->updateTimer.start(int(1000 / Colors::fps));
}
void GraphicsView::setupSceneItems()
{
if (Colors::showFps){
this->fpsLabel = new DemoTextItem(QString("FPS: --"), Colors::buttonFont(), Qt::white, -1, this->scene1, 0, DemoTextItem::DYNAMIC_TEXT);
this->fpsLabel->setZValue(1000);
this->fpsLabel->setPos(Colors::stageStartX, 600 - QFontMetricsF(Colors::buttonFont()).height() - 5);
}
this->mainSceneRoot = new QGraphicsWidget();
this->scene1->addItem(mainSceneRoot);
this->companyLogo = new ImageItem(QImage(":/images/trolltech-logo.png"), 1000, 1000, this->scene1, mainSceneRoot, true, 0.5f);
this->qtLogo = new ImageItem(QImage(":/images/qtlogo_small.png"), 1000, 1000, this->scene1, mainSceneRoot, true, 0.5f);
this->companyLogo->setZValue(100);
this->qtLogo->setZValue(100);
this->pausedLabel = new DemoTextItem(QString("AUSED"), Colors::buttonFont(), Qt::white, -1, this->scene1, 0);
this->pausedLabel->setZValue(100);
QFontMetricsF fm(Colors::buttonFont());
this->pausedLabel->setPos(Colors::stageWidth - fm.width("PAUSED"), 590 - fm.height());
this->pausedLabel->setRecursiveVisible(false);
// 初始化视角文字显示Lable
this->_viewTitleLabel = new DemoTextItem(QString(""), Colors::buttonFont(), Qt::white, -1, this->scene1, 0);
this->_viewTitleLabel->setZValue(100);
QFontMetricsF vtlfm(Colors::buttonFont());
this->_viewTitleLabel->setPos(Colors::stageWidth - fm.width("PAUSED"), 200 - fm.height());
this->_viewTitleLabel->setRecursiveVisible(false);
}
void GraphicsView::checkAdapt()
{
if (this->doneAdapt
|| Colors::noTimerUpdate
|| this->demoStartTime.elapsed() < 2000)
return;
this->doneAdapt = true;
this->forceFpsMedianCalculation();
Colors::benchmarkFps = this->fpsMedian;
if (Colors::verbose)
qDebug() << "- benchmark:" << QString::number(Colors::benchmarkFps) << "FPS";
if (Colors::noAdapt)
return;
if (this->fpsMedian < 30){
if (MenuManager::instance()->ticker && MenuManager::instance()->ticker->scene()){
this->scene1->removeItem(MenuManager::instance()->ticker);
Colors::noTimerUpdate = true;
this->switchTimerOnOff(false);
if (this->fpsLabel)
this->fpsLabel->setText(QString("FPS: (") + QString::number(this->fpsMedian) + QString(")"));
if (Colors::verbose)
qDebug() << "- benchmark adaption: removed ticker (fps < 30)";
}
if (this->fpsMedian < 20){
Colors::noAnimations = true;
if (Colors::verbose)
qDebug() << "- benchmark adaption: animations switched off (fps < 20)";
}
Colors::adapted = true;
}
}
int GraphicsView::performBenchmark()
{
/*
QTime time;
time.restart();
while (time.elapsed() < 2000)
QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
*/
return 0;
}
void GraphicsView::drawBackgroundToPixmap()
{
const QRectF r = this->scene1->sceneRect();
this->background = QPixmap(qRound(r.width()), qRound(r.height()));
this->background.fill(Qt::black);
QPainter painter(&this->background);
if (false && Colors::useEightBitPalette){
painter.fillRect(r, Colors::sceneBg1);
} else {
QImage bg(":/images/demobg.png");
painter.drawImage(0, 0, bg);
}
}
void GraphicsView::showEvent(QShowEvent * event)
{
Q_UNUSED(event);
QGraphicsView::showEvent(event);
}
void GraphicsView::toggleFullscreen()
{
if (this->isFullScreen()){
this->enableMask(true);
this->showNormal();
if (MenuManager::instance()->ticker)
MenuManager::instance()->ticker->pause(false);
}
else {
this->enableMask(false);
this->showFullScreen();
}
}
void GraphicsView::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape){
this->loop = false;
QApplication::quit();
}
else if (event->key() == Qt::Key_F1){
QString s("");
s += "Rendering system: ";
if (Colors::openGlRendering)
s += "OpenGL";
else
s += "software";
s += "\nAdapt: ";
s += Colors::noAdapt ? "off" : "on";
s += "\nAdaption occurred: ";
s += Colors::adapted ? "yes" : "no";
s += "\nOpenGL version: ";
s += Colors::glVersion;
QWidget w;
s += "\nColor bit depth: ";
s += QString::number(w.depth());
s += "\nWanted FPS: ";
s += QString::number(Colors::fps);
s += "\nBenchmarked FPS: ";
s += Colors::benchmarkFps != -1 ? QString::number(Colors::benchmarkFps) : "not calculated";
s += "\nAnimations: ";
s += Colors::noAnimations ? "off" : "on";
s += "\nBlending: ";
s += Colors::useEightBitPalette ? "off" : "on";
s += "\nTicker: ";
s += Colors::noTicker ? "off" : "on";
s += "\nPixmaps: ";
s += Colors::usePixmaps ? "on" : "off";
s += "\nRescale images on resize: ";
s += Colors::noRescale ? "off" : "on";
s += "\nTimer based updates: ";
s += Colors::noTimerUpdate ? "off" : "on";
s += "\nSeparate loop: ";
s += Colors::useLoop ? "yes" : "no";
s += "\nScreen sync: ";
s += Colors::noScreenSync ? "no" : "yes";
QMessageBox::information(0, QString("Current configuration"), s);
}
//QGraphicsView::keyPressEvent(event);
EventAdapter::keyPressEvent(event);
}
void GraphicsView::focusInEvent(QFocusEvent *)
{
if (!Colors::pause)
return;
if (MenuManager::instance()->ticker)
MenuManager::instance()->ticker->pause(false);
int code = MenuManager::instance()->currentMenuCode;
if (code == MenuManager::ROOT || code == MenuManager::MENU1RED || code == MenuManager::MENU1BLUE)
this->switchTimerOnOff(true);
this->pausedLabel->setRecursiveVisible(false);
}
void GraphicsView::focusOutEvent(QFocusEvent *)
{
if (!Colors::pause)
return;
if (MenuManager::instance()->ticker)
MenuManager::instance()->ticker->pause(true);
int code = MenuManager::instance()->currentMenuCode;
if (code == MenuManager::ROOT || code == MenuManager::MENU1RED || code == MenuManager::MENU1BLUE)
this->switchTimerOnOff(false);
this->pausedLabel->setRecursiveVisible(true);
}
void GraphicsView::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
this->resetMatrix();
this->scale(event->size().width() / (double)Colors::winWidth, event->size().height() / (double)Colors::winHeight);
EventAdapter::resizeEvent(event);
//QGraphicsView::resizeEvent(event);
DemoItem::setMatrix(this->matrix());
if (this->companyLogo){
const QRectF r = this->scene1->sceneRect();
QRectF ttb = this->companyLogo->boundingRect();
this->companyLogo->setPos(int((r.width() - ttb.width()) / 2), 595 - ttb.height());
QRectF qtb = this->qtLogo->boundingRect();
this->qtLogo->setPos(802 - qtb.width(), 0);
}
// Changing size will almost always
// hurt FPS during the changing. So
// ignore it.
this->fpsHistory.clear();
}
|
|