summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/invoker/graphics/openscenegraph/CompositeDisplay.cpp
blob: bf4fac75840bb68ef534e330383ae3402208664d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
 *  @file
 *  @author     2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#include "CompositeDisplay.h"
#if 0
#ifdef MACOSX
USE_GRAPICSWINDOW_IMPLEMENTATION(Cocoa)
#else
USE_GRAPHICSWINDOW()
#endif
#endif

CompositeDisplay::CompositeDisplay(unsigned int x,
                                   unsigned int y,
                                   unsigned int width,
                                   unsigned int height,
                                   int screenId) {
	_waitForViewOp = false;
	unsigned int tWidth = 0;
	unsigned int tHeight = 0;
	getResolution(tWidth, tHeight, screenId);

	osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
	traits->doubleBuffer = true;
	traits->sharedContext = 0;
	traits->screenNum = screenId;

	if (width == 0 || height == 0 || (width == tWidth && height == tHeight)) {
		// fullscreen
		traits->windowDecoration = false;
		traits->width = tWidth;
		traits->height = tHeight;
		traits->x = 0;
		traits->y = 0;
	} else {
		// Start with given resolution
		traits->windowDecoration = true;
		traits->x = x;
		traits->y = y;
		traits->width = width;
		traits->height = height;
	}

	_gc = osg::GraphicsContext::createGraphicsContext(traits.get());
	if (_gc.valid()) {
		_gc->setClearColor(osg::Vec4f(1.0f,1.0f,1.0f,1.0f));
		_gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	}

	_width = traits->width;
	_height = traits->height;

	setRunMaxFrameRate(30);
//  setRunFrameScheme(osgViewer::ViewerBase::ON_DEMAND);
	setThreadingModel(osgViewer::Viewer::AutomaticSelection);
}

CompositeDisplay::~CompositeDisplay() {}

void CompositeDisplay::frame(double simulationTime) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	CompositeViewer::frame();
}

bool CompositeDisplay::checkNeedToDoFrame() {
	return CompositeViewer::checkNeedToDoFrame();
}

void CompositeDisplay::addView(const std::string& name, osg::Viewport* v, osgViewer::View* view) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	_viewports[name] = v;

	_views[name] = view;
	_views[name]->setName(name);
	_views[name]->getCamera()->setName(name);
	_views[name]->setCameraManipulator(new osgGA::TrackballManipulator);

	// add the state manipulator
	osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
	statesetManipulator->setStateSet(_views[name]->getCamera()->getOrCreateStateSet());
	_views[name]->addEventHandler( statesetManipulator.get() );

	_views[name]->addEventHandler( new osgViewer::StatsHandler );
	_views[name]->addEventHandler( new osgViewer::HelpHandler );
	_views[name]->addEventHandler( new osgViewer::WindowSizeHandler );
	_views[name]->addEventHandler( new osgViewer::ThreadingHandler );

	_views[name]->getCamera()->setViewport(v);

	// set graphic context
	_views[name]->getCamera()->setGraphicsContext(_gc.get());
	CompositeViewer::addView(_views[name]);

}

void CompositeDisplay::moveView(const std::string& name, osg::Viewport* v) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	const osg::GraphicsContext::Traits* traits = _gc->getTraits();
	osg::Viewport* absoluteVp = new osg::Viewport(v->x() * (traits->width/100.0),
	        v->y() * (traits->height/100.0),
	        v->width() * (traits->width/100.0),
	        v->height() * (traits->height/100.0));
	_views[name]->getCamera()->setViewport(absoluteVp);
}

void CompositeDisplay::removeView(const std::string& name) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);

	_views[name]->getCamera()->setGraphicsContext(NULL);
	_views[name]->getCamera()->setViewport(NULL);

	CompositeViewer::removeView(_views[name]);

	if (_views.find(name) != _views.end()) {
		_views.erase(name);
	}
	if (_viewports.find(name) != _viewports.end())
		_viewports.erase(name);
}

osg::GraphicsContext::WindowingSystemInterface* CompositeDisplay::wsi = NULL;
void CompositeDisplay::getResolution(unsigned int& width, unsigned int& height, int screenId) {
	if (!wsi)
		wsi = osg::GraphicsContext::getWindowingSystemInterface();
	if (wsi) {
		wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(screenId), width, height);
	} else {
		width = 800;
		height = 600;
	}
}