ovassvepCApplication.cpp 11.2 KB
Newer Older
Jussi Lindgren's avatar
Jussi Lindgren committed
1 2 3

#if defined(TARGET_HAS_ThirdPartyOgre3D)

Jussi Lindgren's avatar
Jussi Lindgren committed
4 5 6
#include "ovassvepCApplication.h"
#include <cmath>

Jussi Lindgren's avatar
Jussi Lindgren committed
7
#include "fs/Files.h"
Jussi Lindgren's avatar
Jussi Lindgren committed
8 9 10 11 12 13 14 15 16 17

using namespace OpenViBE;
using namespace OpenViBESSVEP;
using namespace OpenViBE::Kernel;

#define MIN(a,b) ( a < b ? a : b )

	CApplication::CApplication()
: m_bContinueRendering( true ),
	m_ui32CurrentFrame( 0 ),
18 19 20 21
	m_roGUIRenderer( NULL ),
	m_poSceneNode( NULL ),
	m_poPainter( NULL ),
	m_poRoot( NULL )
Jussi Lindgren's avatar
Jussi Lindgren committed
22 23 24 25 26 27 28 29 30 31
{
}

CApplication::~CApplication()
{

	if (m_poPainter != NULL)
	{
		(*m_poLogManager) << LogLevel_Debug << "- m_poPainter\n";
		delete m_poPainter;
32
		m_poPainter = nullptr;
Jussi Lindgren's avatar
Jussi Lindgren committed
33 34 35 36 37 38 39 40 41
	}

	for (std::vector<ICommand*>::iterator it = m_oCommands.begin();
			it != m_oCommands.end(); ++it) 
	{
		(*m_poLogManager) << LogLevel_Debug << "- ICommand\n";
		if (*it != NULL)
		{
			delete *it;
42
			*it = nullptr;
Jussi Lindgren's avatar
Jussi Lindgren committed
43 44 45 46 47 48 49 50
		}
	}

	
	(*m_poLogManager) << LogLevel_Debug << "- m_poRoot\n";
	if (m_poRoot != NULL)
	{
		delete m_poRoot;
51
		m_poRoot = nullptr;
Jussi Lindgren's avatar
Jussi Lindgren committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65
	}
	
}

bool CApplication::setup(OpenViBE::Kernel::IKernelContext* poKernelContext)
{
	m_poKernelContext = poKernelContext;
	m_poLogManager = &(m_poKernelContext->getLogManager());

	IConfigurationManager* l_poConfigurationManager = &(m_poKernelContext->getConfigurationManager());

	(*m_poLogManager) << LogLevel_Debug << "  * CApplication::setup()\n";

	// Plugin config path setup
66
	OpenViBE::CString l_sPluginsPath = l_poConfigurationManager->expand("${Kernel_3DVisualizationOgrePlugins}");
Jussi Lindgren's avatar
Jussi Lindgren committed
67 68

	// Create LogManager to stop Ogre flooding the console and creating random files
69
	(*m_poLogManager) << LogLevel_Info << "Ogre plugins path is " << l_sPluginsPath << "\n";
Jussi Lindgren's avatar
Jussi Lindgren committed
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
	
	(*m_poLogManager) << LogLevel_Debug << "+ Creating Ogre logmanager\n";
	Ogre::LogManager* l_poLogManager = new Ogre::LogManager();
	(*m_poLogManager) << LogLevel_Info << "Ogre log to console : " << l_poConfigurationManager->expandAsBoolean("${SSVEP_Ogre_LogToConsole}", false) << "\n";
	CString l_sOgreLog = l_poConfigurationManager->expand("${Path_Log}") + "/openvibe-ssvep-demo-ogre.log";
	(*m_poLogManager) << LogLevel_Info << "Ogre log file : " << l_sOgreLog << "\n";
	FS::Files::createParentPath(l_sOgreLog);
	l_poLogManager->createLog(l_sOgreLog.toASCIIString(), true, l_poConfigurationManager->expandAsBoolean("${SSVEP_Ogre_LogToConsole}", false), false );
	
	// Root creation
	CString l_sOgreCfg = l_poConfigurationManager->expand("${Path_UserData}") + "/openvibe-ssvep-demo-ogre.cfg";
	(*m_poLogManager) << LogLevel_Debug << "+ m_poRoot = new Ogre::Root(...)\n";
	(*m_poLogManager) << LogLevel_Info << "Ogre cfg file : " << l_sOgreCfg << "\n";
	m_poRoot = new Ogre::Root(l_sPluginsPath.toASCIIString(), l_sOgreCfg.toASCIIString(), l_sOgreLog.toASCIIString());

	// Resource handling
	this->setupResources();

	// Configuration from file or dialog window if needed
	if (!this->configure())
	{
		(*m_poLogManager) << LogLevel_Fatal << "The configuration process ended unexpectedly.\n";
		return false;
	}

	// m_poWindow = m_poRoot->initialise(true);


	Ogre::NameValuePairList l_oOptionList;
	m_poRoot->initialise(false);

	l_oOptionList["vsync"] = "1";

	m_poWindow = m_poRoot->createRenderWindow("SSVEP Stimulator", 640, 480, l_poConfigurationManager->expandAsBoolean("${SSVEP_Ogre_FullScreen}", false), &l_oOptionList);
	m_ui32WindowWidth = m_poWindow->getWidth();
	m_ui32WindowHeight = m_poWindow->getHeight();

	m_poSceneManager = m_poRoot->createSceneManager(Ogre::ST_GENERIC);
	m_poCamera = m_poSceneManager->createCamera("SSVEPApplicationCamera");

	Ogre::SceneManager* l_poFillSceneManager = m_poRoot->createSceneManager(Ogre::ST_GENERIC);
	Ogre::Camera* l_poFillCamera = l_poFillSceneManager->createCamera("SSVEPFillCamera");
	m_poWindow->addViewport(l_poFillCamera, 0);

	m_poViewport = m_poWindow->addViewport(m_poCamera, 1);
	this->resizeViewport();
//	m_poViewport->setBackgroundColour(Ogre::ColourValue(0.0, 0.5, 0.5));

	m_poCamera->setAspectRatio(Ogre::Real(m_poViewport->getActualWidth()) / Ogre::Real(m_poViewport->getActualHeight()));

	m_poSceneNode = m_poSceneManager->getRootSceneNode()->createChildSceneNode("SSVEPApplicationNode");
121
	
Jussi Lindgren's avatar
Jussi Lindgren committed
122 123 124 125 126 127 128 129 130 131
	// initialize the painter object
	(*m_poLogManager) << LogLevel_Debug << "+ m_poPainter = new CBasicPainter(...)\n";
	m_poPainter = new CBasicPainter( this );

	(*m_poLogManager) << LogLevel_Debug << "  * initializing CEGUI\n";
	this->initCEGUI(l_poConfigurationManager->expand("${Path_Log}") + "/openvibe-ssvep-demo-cegui.log");
	(*m_poLogManager) << LogLevel_Debug << "  * CEGUI initialized\n";

	// create the vector of stimulation frequencies
	
132
	m_f64ScreenRefreshRate = (uint32_t)(l_poConfigurationManager->expandAsUInteger("${SSVEP_ScreenRefreshRate}"));
Jussi Lindgren's avatar
Jussi Lindgren committed
133 134 135

	(*m_poLogManager) << LogLevel_Info << "Specified screen refresh rate :" << m_f64ScreenRefreshRate << "Hz\n";

136
	uint32_t i = 1;
Jussi Lindgren's avatar
Jussi Lindgren committed
137 138 139 140 141

	char l_sFrequencyString[32] = "1";

	CIdentifier l_oFrequencyId = l_poConfigurationManager->createConfigurationToken("SSVEP_FrequencyId", CString(l_sFrequencyString));

142
	m_oFrequencies.push_back(std::pair<uint32_t, uint32_t>(30, 30));
Jussi Lindgren's avatar
Jussi Lindgren committed
143 144 145

	while (l_poConfigurationManager->lookUpConfigurationTokenIdentifier(l_poConfigurationManager->expand("SSVEP_Frequency_${SSVEP_FrequencyId}")) != OV_UndefinedIdentifier)
	{
146
		std::pair<uint32_t, uint32_t> l_oFrequency;
Jussi Lindgren's avatar
Jussi Lindgren committed
147

148 149
		double l_f64CurrentFrequency;
		double l_f64ApproximatedFrameCount;
Jussi Lindgren's avatar
Jussi Lindgren committed
150

151
		l_f64CurrentFrequency = (double)(l_poConfigurationManager->expandAsFloat("${SSVEP_Frequency_${SSVEP_FrequencyId}}"));
Jussi Lindgren's avatar
Jussi Lindgren committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

		l_f64ApproximatedFrameCount= m_f64ScreenRefreshRate / l_f64CurrentFrequency; 

		if (fabs(l_f64ApproximatedFrameCount - floor(l_f64ApproximatedFrameCount + 0.5)) < 0.003)
		{

			l_oFrequency.first = int(floor(l_f64ApproximatedFrameCount + 0.5)) / 2 + int(floor(l_f64ApproximatedFrameCount + 0.5)) % 2;
			l_oFrequency.second = int(floor(l_f64ApproximatedFrameCount + 0.5)) / 2;

			(*m_poLogManager) << LogLevel_Info << "Frequency number " << i << ": " << l_f64CurrentFrequency << "Hz / " << floor(l_f64ApproximatedFrameCount + 0.5) << " ( " << l_oFrequency.first << ", " << l_oFrequency.second<< ") frames @ " << m_f64ScreenRefreshRate << "fps\n";

			m_oFrequencies.push_back(l_oFrequency);	
		}
		else
		{
			(*m_poLogManager) << LogLevel_Error << "The selected frequency (" << l_f64CurrentFrequency << "Hz) is not supported by your screen.\n";
		}

		l_poConfigurationManager->releaseConfigurationToken(l_oFrequencyId);

		sprintf(l_sFrequencyString, "%d", ++i);

		l_oFrequencyId = l_poConfigurationManager->createConfigurationToken("SSVEP_FrequencyId", CString(l_sFrequencyString));
	}


	return true;
}

bool CApplication::configure()
{
	if(! m_poRoot->restoreConfig())
	{
		if( ! m_poRoot->showConfigDialog() )
		{
			(*m_poLogManager) << LogLevel_Error << "No configuration created from the dialog window.\n";
			return false;
		}
	}

	// Set hard-coded parameters, VSync in particular
	m_poRoot->getRenderSystem()->setConfigOption("VSync", "True");
//	m_poRoot->getRenderSystem()->setConfigOption("Full Screen", "Yes");
	m_poRoot->getRenderSystem()->setConfigOption("Video Mode","640 x 480 @ 16-bit colour");


	return true;
}

void CApplication::initCEGUI(const char *logFilename)
{
	// Instantiate logger before bootstrapping the system, this way we will be able to get the log redirected
	if (!CEGUI::Logger::getSingletonPtr()) 
	{
		new CEGUI::DefaultLogger();		// singleton; instantiate only, no delete
	}
	(*m_poLogManager) << LogLevel_Info << "+ CEGUI log will be in '" << logFilename << "'\n";
	FS::Files::createParentPath(logFilename);
	CEGUI::Logger::getSingleton().setLogFilename(logFilename, false);

	(*m_poLogManager) << LogLevel_Debug << "+ Creating CEGUI Ogre bootstrap\n";
	m_roGUIRenderer = &(CEGUI::OgreRenderer::bootstrapSystem(*m_poWindow));
	(*m_poLogManager) << LogLevel_Debug << "+ Creating CEGUI Scheme Manager\n";
215

216 217
#if (CEGUI_VERSION_MAJOR > 0) || (CEGUI_VERSION_MINOR >= 8)
	CEGUI::SchemeManager::getSingleton().createFromFile((CEGUI::utf8*)"TaharezLook-ov-0.8.scheme");
218
#else
Jussi Lindgren's avatar
Jussi Lindgren committed
219
	CEGUI::SchemeManager::getSingleton().create((CEGUI::utf8*)"TaharezLook-ov.scheme");
220
#endif
Jussi Lindgren's avatar
Jussi Lindgren committed
221 222 223 224 225 226

	(*m_poLogManager) << LogLevel_Debug << "+ Creating CEGUI WindowManager\n";
	m_poGUIWindowManager = CEGUI::WindowManager::getSingletonPtr();
	m_poSheet = m_poGUIWindowManager->createWindow("DefaultWindow", "Sheet");

	(*m_poLogManager) << LogLevel_Debug << "+ Setting CEGUI StyleSheet\n";
227

228 229
#if (CEGUI_VERSION_MAJOR > 0) || (CEGUI_VERSION_MINOR >= 8)
	CEGUI::System::getSingleton().getDefaultGUIContext().setRootWindow(m_poSheet);
230
#else
Jussi Lindgren's avatar
Jussi Lindgren committed
231
	CEGUI::System::getSingleton().setGUISheet(m_poSheet);
232 233
#endif

Jussi Lindgren's avatar
Jussi Lindgren committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
}

void CApplication::resizeViewport()
{
	(*m_poLogManager) << LogLevel_Trace << "Creating a new viewport\n";

	Ogre::uint32 l_ui32ViewportSize = MIN(m_ui32WindowWidth, m_ui32WindowHeight);
	(*m_poLogManager) << LogLevel_Info << "New viewport size : " << l_ui32ViewportSize << "\n";

	m_poViewport->setDimensions(
				Ogre::Real(m_ui32WindowWidth - l_ui32ViewportSize) / Ogre::Real(m_ui32WindowWidth) / 2,
				Ogre::Real(m_ui32WindowHeight - l_ui32ViewportSize) / Ogre::Real(m_ui32WindowHeight) / 2,
				Ogre::Real(l_ui32ViewportSize) / Ogre::Real(m_ui32WindowWidth),
				Ogre::Real(l_ui32ViewportSize) / Ogre::Real(m_ui32WindowHeight)
				);
}

251
void CApplication::processFrame(uint32_t ui32CurrentFrame)
Jussi Lindgren's avatar
Jussi Lindgren committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
{
	if (m_ui32WindowWidth != m_poWindow->getWidth() || m_ui32WindowHeight != m_poWindow->getHeight())
	{
		m_ui32WindowWidth = m_poWindow->getWidth();
		m_ui32WindowHeight = m_poWindow->getHeight();
		this->resizeViewport();
	}

}


void CApplication::setupResources()
{
	Ogre::ResourceGroupManager::getSingleton().createResourceGroup("SSVEP");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation((OpenViBE::Directories::getDataDir() + "/applications/ssvep-demo/resources").toASCIIString(), "FileSystem", "SSVEP");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation((OpenViBE::Directories::getDataDir() + "/applications/ssvep-demo/resources/trainer").toASCIIString(), "FileSystem", "SSVEPTrainer");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation((OpenViBE::Directories::getDataDir() + "/applications/ssvep-demo/resources/gui").toASCIIString(), "FileSystem", "CEGUI");
	Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("SSVEP");
	Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("SSVEPTrainer");
	Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("CEGUI");
}

bool CApplication::frameRenderingQueued(const Ogre::FrameEvent &evt)
{
	return (m_bContinueRendering && !m_poWindow->isClosed());
}

bool CApplication::frameStarted(const Ogre::FrameEvent &evt)
{
	++m_ui32CurrentFrame;
	m_ui32CurrentFrame = m_ui32CurrentFrame % int(m_f64ScreenRefreshRate);

	this->processFrame(m_ui32CurrentFrame);

286
	for (uint32_t i = 0; i < m_oCommands.size(); i++)
Jussi Lindgren's avatar
Jussi Lindgren committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	{
		m_oCommands[i]->processFrame();
	}

	return true;
}

void CApplication::go()
{
	(*m_poLogManager) << LogLevel_Debug << "Associating application as Ogre frame listener\n";

	m_poRoot->addFrameListener(this);

	(*m_poLogManager) << LogLevel_Debug << "Entering Ogre rendering loop\n";
	m_poRoot->startRendering();
	(*m_poLogManager) << LogLevel_Debug << "Ogre rendering loop finished ... exiting\n";
}

void CApplication::addCommand(ICommand* pCommand)
{
	m_oCommands.push_back(pCommand);
}

void CApplication::startExperiment()
{
	(*m_poLogManager) << LogLevel_Info << "[!] Experiment starting\n";
}

void CApplication::stopExperiment()
{
	(*m_poLogManager) << LogLevel_Info << "[!] Experiment halting\n";
	this->exit();
}

Jussi Lindgren's avatar
Jussi Lindgren committed
321 322

#endif