- Added OsgGraphicsWindowSDL2 in OsgGraph

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@6128 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: a814cdbe2e8a62fb9d6969dc5496b7a29d2d6d57
Former-commit-id: 78cc40b4cca90e7ec0ba9b11743c060e26585d42
This commit is contained in:
torcs-ng 2015-09-15 21:58:11 +00:00
parent bdf0b821fd
commit d547ebd60b
4 changed files with 362 additions and 3 deletions

View file

@ -25,6 +25,7 @@ SET(OSGGRAPH_HEADERS Utils/OsgVectorArrayAdapter.h
Render/OsgReflectionMapping.h Render/OsgReflectionMapping.h
Render/OsgShader.h Render/OsgShader.h
Viewer/OsgGraphicsWindow.h
Viewer/OsgView.h Viewer/OsgView.h
Viewer/OsgScreens.h Viewer/OsgScreens.h
Viewer/OsgCamera.h Viewer/OsgCamera.h
@ -64,6 +65,7 @@ SET(OSGGRAPH_SOURCES Utils/OsgMath.cpp
Render/OsgReflectionMapping.cpp Render/OsgReflectionMapping.cpp
Render/OsgShader.cpp Render/OsgShader.cpp
Viewer/OsgGraphicsWindow.cpp
Viewer/OsgView.cpp Viewer/OsgView.cpp
Viewer/OsgScreens.cpp Viewer/OsgScreens.cpp
Viewer/OsgCamera.cpp Viewer/OsgCamera.cpp

View file

@ -0,0 +1,227 @@
/***************************************************************************
file : OsgGraphicsWindows.cpp
created : Thu Sep 15 15:23:49 CEST 2015
copyright : (C)2015 by Xavier Bertaux
email : bertauxx@yahoo.fr
version : $Id: OsgGraphicsWindows.cpp 6125 2015-09-15 06:02:49Z torcs-ng $
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* Based on OpenMW code *
* *
***************************************************************************/
#include "OsgGraphicsWindow.h"
#include <SDL_video.h>
#include <osg/DeleteHandler>
#include <osg/Version>
namespace OSGUtil
{
OsgGraphicsWindowSDL2::~OsgGraphicsWindowSDL2()
{
close(true);
}
OsgGraphicsWindowSDL2::OsgGraphicsWindowSDL2(osg::GraphicsContext::Traits *traits)
: m_Window(0)
, m_Context(0)
, m_Valid(false)
, m_Realized(false)
, m_OwnsWindow(false)
{
_traits = traits;
init();
if(valid())
{
setState(new osg::State);
getState()->setGraphicsContext(this);
if(_traits.valid() && _traits->sharedContext.valid())
{
getState()->setContextID(_traits->sharedContext->getState()->getContextID());
incrementContextIDUsageCount(getState()->getContextID());
}
else
{
getState()->setContextID(osg::GraphicsContext::createNewContextID());
}
}
}
bool OsgGraphicsWindowSDL2::setWindowDecorationImplementation(bool flag)
{
if(!m_Window) return false;
SDL_SetWindowBordered(m_Window, flag ? SDL_TRUE : SDL_FALSE);
return true;
}
bool OsgGraphicsWindowSDL2::setWindowRectangleImplementation(int x, int y, int width, int height)
{
if(!m_Window) return false;
SDL_SetWindowPosition(m_Window, x, y);
SDL_SetWindowSize(m_Window, width, height);
return true;
}
void OsgGraphicsWindowSDL2::setWindowName(const std::string &name)
{
if(!m_Window) return;
SDL_SetWindowTitle(m_Window, name.c_str());
_traits->windowName = name;
}
void OsgGraphicsWindowSDL2::setCursor(MouseCursor mouseCursor)
{
_traits->useCursor = false;
}
void OsgGraphicsWindowSDL2::init()
{
if(m_Valid) return;
if(!_traits.valid())
return;
WindowData *inheritedWindowData = dynamic_cast<WindowData*>(_traits->inheritedWindowData.get());
m_Window = inheritedWindowData ? inheritedWindowData->m_Window : NULL;
m_OwnsWindow = (m_Window == 0);
if(m_OwnsWindow)
{
OSG_NOTICE<<"Error: No SDL window provided."<<std::endl;
return;
}
// SDL will change the current context when it creates a new one, so we
// have to get the current one to be able to restore it afterward.
SDL_Window *oldWin = SDL_GL_GetCurrentWindow();
SDL_GLContext oldCtx = SDL_GL_GetCurrentContext();
m_Context = SDL_GL_CreateContext(m_Window);
if(!m_Context)
{
OSG_NOTICE<< "Error: Unable to create OpenGL graphics context: "<<SDL_GetError() <<std::endl;
return;
}
SDL_GL_SetSwapInterval(_traits->vsync ? 1 : 0);
SDL_GL_MakeCurrent(oldWin, oldCtx);
m_Valid = true;
#if OSG_MIN_VERSION_REQUIRED(3,3,4)
getEventQueue()->syncWindowRectangleWithGraphicsContext();
#else
getEventQueue()->syncWindowRectangleWithGraphcisContext();
#endif
}
bool OsgGraphicsWindowSDL2::realizeImplementation()
{
if(m_Realized)
{
OSG_NOTICE<< "GraphicsWindowSDL2::realizeImplementation() Already realized" <<std::endl;
return true;
}
if(!m_Valid) init();
if(!m_Valid) return false;
SDL_ShowWindow(m_Window);
#if OSG_MIN_VERSION_REQUIRED(3,3,4)
getEventQueue()->syncWindowRectangleWithGraphicsContext();
#else
getEventQueue()->syncWindowRectangleWithGraphcisContext();
#endif
m_Realized = true;
return true;
}
bool OsgGraphicsWindowSDL2::makeCurrentImplementation()
{
if(!m_Realized)
{
OSG_NOTICE<<"Warning: GraphicsWindow not realized, cannot do makeCurrent."<<std::endl;
return false;
}
return SDL_GL_MakeCurrent(m_Window, m_Context)==0;
}
bool OsgGraphicsWindowSDL2::releaseContextImplementation()
{
if(!m_Realized)
{
OSG_NOTICE<< "Warning: GraphicsWindow not realized, cannot do releaseContext." <<std::endl;
return false;
}
return SDL_GL_MakeCurrent(NULL, NULL) == 0;
}
void OsgGraphicsWindowSDL2::closeImplementation()
{
if(m_Context)
SDL_GL_DeleteContext(m_Context);
m_Context = NULL;
if(m_Window && m_OwnsWindow)
SDL_DestroyWindow(m_Window);
m_Window = NULL;
m_Valid = false;
m_Realized = false;
}
void OsgGraphicsWindowSDL2::swapBuffersImplementation()
{
if(!m_Realized) return;
SDL_GL_SwapWindow(m_Window);
}
void OsgGraphicsWindowSDL2::setSyncToVBlank(bool on)
{
SDL_Window *oldWin = SDL_GL_GetCurrentWindow();
SDL_GLContext oldCtx = SDL_GL_GetCurrentContext();
SDL_GL_MakeCurrent(m_Window, m_Context);
SDL_GL_SetSwapInterval(on ? 1 : 0);
SDL_GL_MakeCurrent(oldWin, oldCtx);
}
void OsgGraphicsWindowSDL2::raiseWindow()
{
SDL_RaiseWindow(m_Window);
}
}

View file

@ -0,0 +1,106 @@
/***************************************************************************
file : OsgGraphicsWindows.h
created : Thu Sep 15 15:23:49 CEST 2015
copyright : (C)2015 by Xavier Bertaux
email : bertauxx@yahoo.fr
version : $Id: OsgGraphicsWindows.h 6125 2015-09-15 06:02:49Z torcs-ng $
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* Based on OpenMW code *
* *
***************************************************************************/
#ifndef OSGGRAPHICSWINDOW_H
#define OSGGRAPHICSWINDOW_H
#include <SDL_video.h>
#include <osgViewer/GraphicsWindow>
namespace OSGUtil
{
class OsgGraphicsWindowSDL2 : public osgViewer::GraphicsWindow
{
SDL_Window* m_Window;
SDL_GLContext m_Context;
bool m_Valid;
bool m_Realized;
bool m_OwnsWindow;
void init();
virtual ~OsgGraphicsWindowSDL2();
public:
OsgGraphicsWindowSDL2(osg::GraphicsContext::Traits *traits);
virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const OsgGraphicsWindowSDL2*>(object)!=0; }
virtual const char* libraryName() const { return "osgViewer"; }
virtual const char* className() const { return "OsgGraphicsWindowSDL2"; }
virtual bool valid() const { return m_Valid; }
/** Realise the GraphicsContext.*/
virtual bool realizeImplementation();
/** Return true if the graphics context has been realised and is ready to use.*/
virtual bool isRealizedImplementation() const { return m_Realized; }
/** Close the graphics context.*/
virtual void closeImplementation();
/** Make this graphics context current.*/
virtual bool makeCurrentImplementation();
/** Release the graphics context.*/
virtual bool releaseContextImplementation();
/** Swap the front and back buffers.*/
virtual void swapBuffersImplementation();
/** Set sync-to-vblank. */
virtual void setSyncToVBlank(bool on);
/** Set Window decoration.*/
virtual bool setWindowDecorationImplementation(bool flag);
/** Raise specified window */
virtual void raiseWindow();
/** Set the window's position and size.*/
virtual bool setWindowRectangleImplementation(int x, int y, int width, int height);
/** Set the name of the window */
virtual void setWindowName(const std::string &name);
/** Set mouse cursor to a specific shape.*/
virtual void setCursor(MouseCursor cursor);
/** Get focus.*/
virtual void grabFocus() {}
/** Get focus on if the pointer is in this window.*/
virtual void grabFocusIfPointerInWindow() {}
/** WindowData is used to pass in the SDL2 window handle attached to the GraphicsContext::Traits structure. */
struct WindowData : public osg::Referenced
{
WindowData(SDL_Window *window) : m_Window(window)
{ }
SDL_Window *m_Window;
};
};
}
#endif /* OSGGRAPHICSWINDOW_H */

View file

@ -26,11 +26,16 @@
#include <osg/FrontFace> #include <osg/FrontFace>
#include "OsgScreens.h" #include "OsgScreens.h"
#include "OsgGraphicsWindow.h"
#include "OsgDebugHUD.h" #include "OsgDebugHUD.h"
#include "OsgReflectionMapping.h" #include "OsgReflectionMapping.h"
#include "OsgMain.h" #include "OsgMain.h"
#include "OsgCar.h" #include "OsgCar.h"
#if SDL_MAJOR_VERSION >= 2
extern SDL_Window* GfuiWindow;
#endif
SDScreens::SDScreens() : SDScreens::SDScreens() :
root(NULL), root(NULL),
mirrorScene(NULL), mirrorScene(NULL),
@ -61,6 +66,7 @@ void SDScreens::Init(int x,int y, int width, int height, osg::ref_ptr<osg::Node>
//intialising main screen //intialising main screen
viewer = new osgViewer::Viewer; viewer = new osgViewer::Viewer;
#if 1 //SDL_MAJOR_VERSION < 2
//viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); //viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
//SDView * view = new SDView(viewer->getCamera(),0,0, m_Winw, m_Winh, mirrorCam.get()); //SDView * view = new SDView(viewer->getCamera(),0,0, m_Winw, m_Winh, mirrorCam.get());
//osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gw = viewer->setUpViewerAsEmbeddedInWindow(0, 0, m_Winw, m_Winh); //osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gw = viewer->setUpViewerAsEmbeddedInWindow(0, 0, m_Winw, m_Winh);
@ -69,7 +75,25 @@ void SDScreens::Init(int x,int y, int width, int height, osg::ref_ptr<osg::Node>
osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gw = viewer->setUpViewerAsEmbeddedInWindow(0, 0, width, height); osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gw = viewer->setUpViewerAsEmbeddedInWindow(0, 0, width, height);
viewer->getCamera()->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); viewer->getCamera()->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
viewer->getCamera()->setPreDrawCallback(new CameraDrawnCallback); viewer->getCamera()->setPreDrawCallback(new CameraDrawnCallback);
//viewer->realize(); #else
viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
SDL_GetWindowPosition(GfuiWindow, &traits->x, &traits->y);
SDL_GetWindowSize(GfuiWindow, &traits->width, &traits->height);
traits->windowName = SDL_GetWindowTitle(GfuiWindow);
traits->windowDecoration = !(SDL_GetWindowFlags(GfuiWindow)&SDL_WINDOW_BORDERLESS);
traits->screenNum = SDL_GetWindowDisplayIndex(GfuiWindow);
traits->vsync = true;
traits->doubleBuffer = true;
traits->inheritedWindowData = new OSGUtil::OsgGraphicsWindowSDL2::WindowData(GfuiWindow);
osg::ref_ptr<OSGUtil::OsgGraphicsWindowSDL2> gw = new OSGUtil::OsgGraphicsWindowSDL2(traits);
viewer->getCamera()->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
viewer->getCamera()->setPreDrawCallback(new CameraDrawnCallback);
if(!gw->valid()) throw
std::runtime_error("Failed to create GraphicsContext");
#endif
osg::ref_ptr<osg::Camera> mirrorCam = new osg::Camera; osg::ref_ptr<osg::Camera> mirrorCam = new osg::Camera;
mirrorCam->setGraphicsContext(gw); mirrorCam->setGraphicsContext(gw);