[osg-users] Change Graphic Context Runtimely

tianzjyh tianzjyh at 126.com
Mon Nov 21 02:08:30 PST 2016


Hi, 
  I found it difficult to achieve dynamic switching OpenGL context at run time.


  In the following code, I try to achieve this switching in a single thread(rendering thread not included), but I got a frozen window after pressing the switch key.


  My purpose is mainly to implement a quad-buffer switch option for users.


  Which step have made the froze of the main loop?


Codes:
------------------------------------------------------------------------------------------------------
1#include <windows.h>
2
3#include <osg/Node>
4#include <osgGA/GuiEventHandler>
5#include <osgGA/TrackballManipulator>
6#include <osgViewer/Viewer>
7#include <osgDB/ReadFile>
8
9osg::GraphicsContext* createGC(float x, float y, float w, float h, bool stereo)
10{
11osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
12osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
13traits->windowName = "";
14traits->windowDecoration = true;
15traits->x = x;
16traits->y = y;
17traits->width = w;
18traits->height = h;
19traits->doubleBuffer = true;
20traits->quadBufferStereo = stereo;
21traits->alpha = ds->getMinimumNumAlphaBits();
22traits->stencil = ds->getMinimumNumStencilBits();
23traits->sampleBuffers = ds->getMultiSamples();
24traits->samples = ds->getNumMultiSamples();
25
26return osg::GraphicsContext::createGraphicsContext(traits.get());
27}
28
29bool _stereo = false;
30osg::ref_ptr<osg::GraphicsContext> _newgc = NULL;
31
32class SwitchGC : public osgGA::GUIEventHandler
33{
34public:
35virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
36{
37switch (ea.getEventType())
38{
39case osgGA::GUIEventAdapter::EventType::KEYDOWN:
40{
41   if (ea.getKey() == osgGA::GUIEventAdapter::KEY_S)
42   {
43   //
44   osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
45   if (viewer)
46   {
47   _stereo = !_stereo;
48   _newgc = createGC(0, 0, 1000, 1000, _stereo);
49   //viewer->getCamera()->setGraphicsContext(gc);
50   osg::DisplaySettings::instance()->setStereo(_stereo);
51   if (_stereo)
52   {
53   osg::DisplaySettings::instance()->setStereoMode(osg::DisplaySettings::QUAD_BUFFER);
54   }
55   }
56
57   return true;
58   }
59}
60break;
61default:
62break;
63}
64
65return false;
66}
67private:
68};
69
70int main(int, char**)
71{
72osgViewer::Viewer* viewer = new osgViewer::Viewer();
73
74_stereo = !_stereo;
75osg::GraphicsContext* gc = createGC(0, 0, 1000, 1000, _stereo);
76osg::DisplaySettings::instance()->setStereo(_stereo);
77if (_stereo)
78{
79osg::DisplaySettings::instance()->setStereoMode(osg::DisplaySettings::QUAD_BUFFER);
80}
81
82
83viewer->getCamera()->setGraphicsContext(gc);
84viewer->setSceneData(osgDB::readNodeFile("cow.osg"));
85
86osg::Camera* camera = viewer->getCamera();
87const osg::GraphicsContext::Traits* traits = gc->getTraits();
88
89camera->setClearColor(osg::Vec4(0.2, 0.2, 0.2, 1.0));
90camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
91camera->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(traits->width) / static_cast<double>(traits->height), 1.0f, 10000.0f);
92
93viewer->addEventHandler(new SwitchGC());
94viewer->setCameraManipulator(new osgGA::TrackballManipulator);
95
96while (!viewer->done())
97{
98if (_newgc.get() != NULL)
99{
100osg::GraphicsContext*oldgc =  viewer->getCamera()->getGraphicsContext();
101if (oldgc)
102{
103oldgc->close(true);
104}
105
106viewer->getCamera()->setGraphicsContext(_newgc);
107_newgc = NULL;
108
109camera->setClearColor(osg::Vec4(0.2, 0.2, 0.2, 1.0));
110camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
111camera->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(traits->width) / static_cast<double>(traits->height), 1.0f, 10000.0f);
112
113viewer->realize();
114viewer->addEventHandler(new SwitchGC());
115viewer->setCameraManipulator(new osgGA::TrackballManipulator);
116
117}
118
119viewer->frame();
120
121}
122return 0;
123}
124
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20161121/852cbb1d/attachment-0001.htm>


More information about the osg-users mailing list