Created
July 15, 2014 18:10
-
-
Save agesome/9bf4cec0aee18fc6e27b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
arstudio::ItemViewRenderer * ItemView::createRenderer() const | |
{ | |
return new ItemViewRenderer; | |
} | |
======= | |
#ifndef ITEM_VIEW_RENDERER_H | |
#define ITEM_VIEW_RENDERER_H | |
//#include <ItemView.hpp> | |
#include <KeyboardCameraManipulator.hpp> | |
#include <QOpenGLFramebufferObject> | |
#include <QOpenGLFunctions> | |
#include <QQuickFramebufferObject> | |
#include <osgViewer/Viewer> | |
#include <osgViewer/Renderer> | |
#include <osgGA/OrbitManipulator> | |
#include <osg/ShapeDrawable> | |
#include <osg/Geometry> | |
#include <osg/LineWidth> | |
#include <osgText/Text> | |
#include <QtDebug> | |
namespace arstudio { | |
class ItemViewRenderer : public QQuickFramebufferObject::Renderer | |
{ | |
public: | |
ItemViewRenderer(); | |
QOpenGLFramebufferObject * createFramebufferObject(const QSize &size); | |
void render(); | |
void synchronize(QQuickFramebufferObject * item); | |
private: | |
void osg_initialize(); | |
void create_axis (); | |
QSize m_size; | |
QOpenGLFramebufferObject * m_fbo; | |
osg::ref_ptr<osgViewer::Viewer> m_osg_viewer; | |
osg::ref_ptr<osgGA::OrbitManipulator> m_osg_orbit; | |
osg::ref_ptr<KeyboardCameraManipulator> m_osg_firstperson; | |
osg::ref_ptr<osg::Group> m_osg_scene; | |
osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> m_osg_window_handle; | |
/* | |
* we will not delete this in the destructor, should be deleted internally by | |
* OSG since osg::Geode derives Referenced; TODO: verify? | |
*/ | |
osg::Geode * m_sequence_node; | |
}; | |
} | |
#endif // ITEM_VIEW_RENDERER_H | |
======= | |
#include <ItemViewRenderer.hpp> | |
namespace arstudio { | |
ItemViewRenderer::ItemViewRenderer() | |
: QQuickFramebufferObject::Renderer() | |
, m_fbo(nullptr) | |
{ | |
osg_initialize(); | |
qDebug() << __FUNCTION__; | |
} | |
QOpenGLFramebufferObject * | |
ItemViewRenderer::createFramebufferObject(const QSize &size) | |
{ | |
qDebug() << __FUNCTION__; | |
QOpenGLFramebufferObjectFormat format; | |
format.setAttachment (QOpenGLFramebufferObject::CombinedDepthStencil); | |
m_fbo = new QOpenGLFramebufferObject (size, format); | |
int w = size.width(), h = size.height(); | |
auto handle = m_osg_viewer->setUpViewerAsEmbeddedInWindow (0, 0, w, h); | |
osg::Camera * c = m_osg_viewer->getCamera (); | |
handle->resized (0, 0, w, h); | |
c->setViewport (0, 0, w, h); | |
c->setProjectionMatrixAsPerspective (30.0f, (float) w / h, 1.0f, 10000.0f); | |
return m_fbo; | |
} | |
void ItemViewRenderer::render() | |
{ | |
QOpenGLFunctions f (QOpenGLContext::currentContext ()); | |
// непонятный костыль | |
f.glUseProgram(0); | |
f.glDisable(GL_DEPTH_TEST); | |
f.glDisable(GL_BLEND); | |
// actual OSG rendering happens here | |
m_osg_viewer->frame (); | |
qDebug() << __FUNCTION__; | |
} | |
void ItemViewRenderer::synchronize(QQuickFramebufferObject *item) | |
{ | |
qDebug() << __FUNCTION__; | |
} | |
void ItemViewRenderer::osg_initialize() | |
{ | |
m_osg_viewer = new osgViewer::Viewer; | |
m_osg_viewer->setThreadingModel (osgViewer::Viewer::SingleThreaded); | |
osg::Light * light = m_osg_viewer->getLight (); | |
light->setAmbient (osg::Vec4 (.2, .2, .2, .2)); | |
osg::Camera * camera = m_osg_viewer->getCamera (); | |
// background color for whatever is rendered with OSG | |
camera->setClearColor (osg::Vec4 (.05, .05, .05, 1.0)); | |
m_osg_orbit = new osgGA::OrbitManipulator; | |
constexpr int flags = KeyboardCameraManipulator::UPDATE_MODEL_SIZE; | |
m_osg_firstperson = new KeyboardCameraManipulator (flags); | |
m_osg_firstperson->setVelocity (0.05); | |
m_osg_firstperson->setHomePosition (osg::Vec3 (3, 0, 3), osg::Vec3 (0, 0, 0), | |
osg::Vec3 (0, 0, 1)); | |
m_osg_orbit->setHomePosition (osg::Vec3 (3, 0, 3), osg::Vec3 (0, 0, 0), | |
osg::Vec3 (0, 0, 1)); | |
// stop spinning when releasing LMB while moving the mouse | |
m_osg_orbit->setAllowThrow (false); | |
m_osg_viewer->setCameraManipulator (m_osg_orbit.get ()); | |
m_osg_viewer->home (); | |
m_osg_scene = new osg::Group; | |
m_sequence_node = new osg::Geode; | |
m_osg_scene->addChild (m_sequence_node); | |
m_osg_viewer->setSceneData (m_osg_scene.get ()); | |
create_axis(); | |
} | |
void | |
ItemViewRenderer::create_axis () | |
{ | |
osg::Geode * axis = new osg::Geode; | |
osg::ShapeDrawable * s; | |
osg::Cylinder * cyl; | |
osg::Cone * cone; | |
// cylinder radius | |
constexpr float cr = 0.01; | |
// cone height | |
constexpr float ch = 8 * cr; | |
// Z arrow | |
cyl = new osg::Cylinder (osg::Vec3 (0, 0, 0.5), cr, 1); | |
s = new osg::ShapeDrawable (cyl); | |
axis->addDrawable (s); | |
cone = new osg::Cone (osg::Vec3 (0, 0, 1), 2 * cr, ch); | |
s = new osg::ShapeDrawable (cone); | |
s->setColor (osg::Vec4 (0, 0, .9, 1.0)); | |
axis->addDrawable (s); | |
// axis->addDrawable (show_text (osg::Vec3 (0, 0, 1.1), "Z")); | |
// X arrow | |
cyl = new osg::Cylinder (osg::Vec3 (0.5, 0, 0), cr, 1); | |
s = new osg::ShapeDrawable (cyl); | |
cyl->setRotation (osg::Quat (M_PI_2, osg::Vec3 (0, 1, 0))); | |
axis->addDrawable (s); | |
cone = new osg::Cone (osg::Vec3 (1, 0, 0), 2 * cr, ch); | |
s = new osg::ShapeDrawable (cone); | |
cone->setRotation (osg::Quat (M_PI_2, osg::Vec3 (0, 1, 0))); | |
s->setColor (osg::Vec4 (.9, .9, 0, 1.0)); | |
axis->addDrawable (s); | |
// axis->addDrawable (show_text (osg::Vec3 (1.1, 0, 0), "X")); | |
// Y arrow | |
cyl = new osg::Cylinder (osg::Vec3 (0, 0.5, 0), cr, 1); | |
s = new osg::ShapeDrawable (cyl); | |
cyl->setRotation (osg::Quat (M_PI_2, osg::Vec3 (1, 0, 0))); | |
axis->addDrawable (s); | |
cone = new osg::Cone (osg::Vec3 (0, 1, 0), 2 * cr, ch); | |
s = new osg::ShapeDrawable (cone); | |
cone->setRotation (osg::Quat (-M_PI_2, osg::Vec3 (1, 0, 0))); | |
s->setColor (osg::Vec4 (.9, 0, 0, 1.0)); | |
axis->addDrawable (s); | |
// axis->addDrawable (show_text (osg::Vec3 (0, 1.1, 0), "Y")); | |
m_osg_scene->addChild (axis); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, just found your snippet while looking for a convenient way to render a osg scene in a qtquick item. Tried your snippet, but could not get the osg content rendered correctly (first frame black, after re-sizing buffer stays the same). I suppose it has something to do with the OpenGL Context State of OSG. Did you have the same issue?