GraphicsWindowIOS: multithreaded viewer's mode support added; updated for iOS-11 SDK; view rotations fixed; GLES2, GLES3 preprocessors fixed; tested on iPhone4 and upper

This commit is contained in:
Konstantin S. Matveyev 2018-03-26 20:40:14 +03:00
parent 80a2dd924f
commit a846c0160c
2 changed files with 189 additions and 103 deletions

View File

@ -28,6 +28,7 @@
@class EAGLContext; @class EAGLContext;
@class UIWindow; @class UIWindow;
@class UIView; @class UIView;
@class UIViewController;
#else #else
class GraphicsWindowIOSGLView; class GraphicsWindowIOSGLView;
class GraphicsWindowIOSWindow; class GraphicsWindowIOSWindow;
@ -35,6 +36,7 @@ class GraphicsWindowIOSGLViewController;
class EAGLContext; class EAGLContext;
class UIWindow; class UIWindow;
class UIView; class UIView;
class UIViewController;
#endif #endif
#include <osgViewer/GraphicsWindow> #include <osgViewer/GraphicsWindow>
@ -88,6 +90,8 @@ class GraphicsWindowIOS : public osgViewer::GraphicsWindow
virtual const char* libraryName() const { return "osgViewer"; } virtual const char* libraryName() const { return "osgViewer"; }
virtual const char* className() const { return "GraphicsWindowIOS"; } virtual const char* className() const { return "GraphicsWindowIOS"; }
virtual void runOperations();
virtual bool valid() const { return _valid; } virtual bool valid() const { return _valid; }
/** Realise the GraphicsContext.*/ /** Realise the GraphicsContext.*/
@ -145,8 +149,9 @@ class GraphicsWindowIOS : public osgViewer::GraphicsWindow
}; };
typedef unsigned int DeviceOrientationFlags; typedef unsigned int DeviceOrientationFlags;
WindowData(UIView* window_or_view = NULL, DeviceOrientationFlags orientationFlags = ALL_ORIENTATIONS, float scaleFactor = -1.0f) WindowData(UIView* window_or_view = NULL, UIViewController* parentController = NULL, DeviceOrientationFlags orientationFlags = ALL_ORIENTATIONS, float scaleFactor = -1.0f)
: _windowOrView(window_or_view), : _windowOrView(window_or_view),
_parentController(parentController),
_deviceOrientationFlags(orientationFlags), _deviceOrientationFlags(orientationFlags),
_viewContentScaleFactor(scaleFactor), _viewContentScaleFactor(scaleFactor),
_createTransparentView(false), _createTransparentView(false),
@ -159,6 +164,7 @@ class GraphicsWindowIOS : public osgViewer::GraphicsWindow
void setViewContentScaleFactor(float scaleFactor) { _viewContentScaleFactor = scaleFactor; } void setViewContentScaleFactor(float scaleFactor) { _viewContentScaleFactor = scaleFactor; }
UIView* getWindowOrParentView() const { return _windowOrView; } UIView* getWindowOrParentView() const { return _windowOrView; }
UIViewController* getController() const;
bool getCreateTransparentView() { return _createTransparentView; } bool getCreateTransparentView() { return _createTransparentView; }
void setCreateTransparentView(bool b) { _createTransparentView = b; } void setCreateTransparentView(bool b) { _createTransparentView = b; }
@ -169,6 +175,7 @@ class GraphicsWindowIOS : public osgViewer::GraphicsWindow
private: private:
UIView* _windowOrView; UIView* _windowOrView;
UIViewController* _parentController;
DeviceOrientationFlags _deviceOrientationFlags; DeviceOrientationFlags _deviceOrientationFlags;
float _viewContentScaleFactor; float _viewContentScaleFactor;
bool _createTransparentView; bool _createTransparentView;

View File

@ -6,16 +6,21 @@
#import <QuartzCore/QuartzCore.h> #import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
void(^processInMainThread)(void(^funcBlock)()) = ^(void(^funcBlock)())
{
if ([NSThread isMainThread])
funcBlock();
else
dispatch_sync(dispatch_get_main_queue(), ^{ funcBlock(); });
};
#if OSG_GLES1_FEATURES #if OSG_GLES1_FEATURES
#import <OpenGLES/ES1/glext.h> #import <OpenGLES/ES1/glext.h>
#else #else
#if OSG_GLES2_FEATURES
#define MUTLI_GLES (OSG_GLES2_FEATURES && OSG_GLES3_FEATURES)
#if OSG_GLES2_FEATURES || MUTLI_GLES
#import <OpenGLES/ES2/glext.h> #import <OpenGLES/ES2/glext.h>
#endif #endif
#if OSG_GLES3_FEATURES || MUTLI_GLES #if OSG_GLES3_FEATURES
#import <OpenGLES/ES3/glext.h> #import <OpenGLES/ES3/glext.h>
#endif #endif
@ -44,10 +49,10 @@
#define GL_RGB5_A1_OES GL_RGB5_A1 #define GL_RGB5_A1_OES GL_RGB5_A1
#if OSG_GLES3_FEATURES && !MUTLI_GLES #if OSG_GLES3_FEATURES && !OSG_GLES2_FEATURES
#define glRenderbufferStorageMultisampleAPPLE glRenderbufferStorageMultisample #define glRenderbufferStorageMultisampleAPPLE glRenderbufferStorageMultisample
#define glDiscardFramebufferEXT glInvalidateFramebuffer #define glDiscardFramebufferEXT glInvalidateFramebuffer
//#define glResolveMultisampleFramebufferAPPLE glResolveMultisampleFramebuffer #define glResolveMultisampleFramebufferAPPLE glResolveMultisampleFramebuffer
#define GL_DEPTH24_STENCIL8_OES GL_DEPTH24_STENCIL8 #define GL_DEPTH24_STENCIL8_OES GL_DEPTH24_STENCIL8
#define GL_DEPTH_COMPONENT24_OES GL_DEPTH_COMPONENT24 #define GL_DEPTH_COMPONENT24_OES GL_DEPTH_COMPONENT24
@ -57,6 +62,7 @@
#endif #endif
#include "IOSUtils.h" #include "IOSUtils.h"
@ -367,7 +373,14 @@ typedef std::map<void*, unsigned int> TouchPointsIdMapping;
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer); glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _viewRenderbuffer); glBindRenderbufferOES(GL_RENDERBUFFER_OES, _viewRenderbuffer);
if ([NSThread isMainThread])
[_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer]; [_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
else
dispatch_sync(dispatch_get_main_queue(), ^{
[_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
});
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, _viewRenderbuffer); glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, _viewRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &_backingWidth); glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &_backingWidth);
@ -449,9 +462,17 @@ typedef std::map<void*, unsigned int> TouchPointsIdMapping;
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _msaaFramebuffer); glBindFramebufferOES(GL_FRAMEBUFFER_OES, _msaaFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _msaaRenderBuffer); glBindRenderbufferOES(GL_RENDERBUFFER_OES, _msaaRenderBuffer);
// Samples is the amount of pixels the MSAA buffer uses to make one pixel on the render // buffer. Use a small number like 2 for the 3G and below and 4 or more for newer models // Samples is the amount of pixels the MSAA buffer uses to make one pixel on the render
// buffer. Use a small number like 2 for the 3G and below and 4 or more for newer models
// NOTE: Formats of draw and read buffers must be identical
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, _win->getTraits()->samples, GL_RGB5_A1_OES, _backingWidth, _backingHeight); GLenum internalFormat = GL_RGB5_A1_OES;
# if OSG_GLES3_FEATURES
if ([_context API] == kEAGLRenderingAPIOpenGLES3)
internalFormat = GL_RGBA8_OES;
# endif
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, _win->getTraits()->samples, internalFormat, _backingWidth, _backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, _msaaRenderBuffer); glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, _msaaRenderBuffer);
glGenRenderbuffersOES(1, &_msaaDepthBuffer); glGenRenderbuffersOES(1, &_msaaDepthBuffer);
@ -524,65 +545,68 @@ typedef std::map<void*, unsigned int> TouchPointsIdMapping;
#if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) #if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0)
if(_msaaFramebuffer) if (_msaaFramebuffer) {
{ // Resolve the contents from the multisampling buffer into the resolve (view) buffer
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _msaaFramebuffer);
glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, _msaaFramebuffer); glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, _msaaFramebuffer);
glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, _viewFramebuffer); glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, _viewFramebuffer);
GLenum attachments[] = {GL_DEPTH_ATTACHMENT_OES, GL_COLOR_ATTACHMENT0_OES}; # if OSG_GLES3_FEATURES
if ([_context API] == kEAGLRenderingAPIOpenGLES3) {
#if !OSG_GLES3_FEATURES
glResolveMultisampleFramebufferAPPLE();
glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments);
#else
switch ([_context API])
{
case kEAGLRenderingAPIOpenGLES3:
glBlitFramebuffer(0, 0, _backingWidth, _backingHeight, glBlitFramebuffer(0, 0, _backingWidth, _backingHeight,
0, 0, _backingWidth, _backingHeight, 0, 0, _backingWidth, _backingHeight,
GL_COLOR_BUFFER_BIT, GL_LINEAR); GL_COLOR_BUFFER_BIT, GL_LINEAR);
glInvalidateFramebuffer(GL_READ_FRAMEBUFFER_APPLE, 2, attachments); }
break; else
glResolveMultisampleFramebufferAPPLE();
default: # else
#if !OSG_GLES3_FEATURES
glResolveMultisampleFramebufferAPPLE(); glResolveMultisampleFramebufferAPPLE();
#endif
glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments);
break;
}
# endif # endif
} }
#endif #endif
//swap buffers (sort of i think?) // Present Results step
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _viewRenderbuffer); glBindRenderbufferOES(GL_RENDERBUFFER_OES, _viewRenderbuffer);
//display render in context
[_context presentRenderbuffer:GL_RENDERBUFFER_OES]; [_context presentRenderbuffer:GL_RENDERBUFFER_OES];
//re bind the frame buffer for next frames renders
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer);
#if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) #if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0)
if (_msaaFramebuffer) if (_msaaFramebuffer) {
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _msaaFramebuffer);; // Invalidate (discard) step (must be after present step)
GLenum attachments[] = {GL_DEPTH_ATTACHMENT_OES, GL_COLOR_ATTACHMENT0_OES};
# if OSG_GLES3_FEATURES
if ([_context API] == kEAGLRenderingAPIOpenGLES3)
glInvalidateFramebuffer(GL_READ_FRAMEBUFFER_APPLE, 2, attachments);
else
glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments);
# else
glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments);
# endif # endif
} }
#endif
[self bindFrameBuffer];
}
// //
//bind view buffer as current for new render pass //bind view buffer as current for new render pass
// //
- (void)bindFrameBuffer { - (void)bindFrameBuffer {
//bind the frame buffer
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer);
#if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) #if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0)
if (_msaaFramebuffer) if (_msaaFramebuffer) {
glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, _msaaFramebuffer); # if OSG_GLES3_FEATURES
if ([_context API] == kEAGLRenderingAPIOpenGLES3) {
glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, _msaaFramebuffer);
glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, _viewFramebuffer);
}
else
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _msaaFramebuffer);
# else
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _msaaFramebuffer);
# endif
}
else
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer);
#else
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer);
#endif #endif
} }
@ -704,8 +728,9 @@ typedef std::map<void*, unsigned int> TouchPointsIdMapping;
@interface GraphicsWindowIOSGLViewController : UIViewController @interface GraphicsWindowIOSGLViewController : UIViewController
{ {
} }
- (void) viewDidAppear:(BOOL)animated;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration; - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration;
@ -713,6 +738,13 @@ typedef std::map<void*, unsigned int> TouchPointsIdMapping;
@implementation GraphicsWindowIOSGLViewController @implementation GraphicsWindowIOSGLViewController
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[UIViewController attemptRotationToDeviceOrientation];
if (self.view)
[self.view layoutSubviews];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{ {
@ -767,6 +799,14 @@ typedef std::map<void*, unsigned int> TouchPointsIdMapping;
using namespace osgIOS; using namespace osgIOS;
namespace osgViewer { namespace osgViewer {
UIViewController* GraphicsWindowIOS::WindowData::getController() const
{
return [_windowOrView isKindOfClass:[UIWindow class]]
? ((UIWindow*)(_windowOrView)).rootViewController
: _parentController;
}
#pragma mark GraphicsWindowIOS #pragma mark GraphicsWindowIOS
@ -807,6 +847,8 @@ bool GraphicsWindowIOS::realizeImplementation()
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
processInMainThread(^
{
BOOL bar_hidden = (_traits->windowDecoration) ? NO: YES; BOOL bar_hidden = (_traits->windowDecoration) ? NO: YES;
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED > 30100 #if __IPHONE_OS_VERSION_MIN_REQUIRED > 30100
@ -815,6 +857,7 @@ bool GraphicsWindowIOS::realizeImplementation()
[[UIApplication sharedApplication] setStatusBarHidden: bar_hidden animated:NO]; [[UIApplication sharedApplication] setStatusBarHidden: bar_hidden animated:NO];
#endif #endif
#endif #endif
});
//Get info about the requested screen //Get info about the requested screen
IOSWindowingSystemInterface* wsi = dynamic_cast<IOSWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); IOSWindowingSystemInterface* wsi = dynamic_cast<IOSWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface());
@ -843,7 +886,7 @@ bool GraphicsWindowIOS::realizeImplementation()
if (windowData->getWindowOrParentView()) if (windowData->getWindowOrParentView())
{ {
_ownsWindow = false; _ownsWindow = false;
_window = windowData->getWindowOrParentView(); _window = (GraphicsWindowIOSWindow*)(windowData->getWindowOrParentView());
} }
_deviceOrientationFlags = windowData->_deviceOrientationFlags; _deviceOrientationFlags = windowData->_deviceOrientationFlags;
@ -855,7 +898,6 @@ bool GraphicsWindowIOS::realizeImplementation()
if(_viewContentScaleFactor < 0.0f) if(_viewContentScaleFactor < 0.0f)
{_viewContentScaleFactor = screenScaleFactor;} {_viewContentScaleFactor = screenScaleFactor;}
OSG_DEBUG << "GraphicsWindowIOS::realizeImplementation / ownsWindow: " << _ownsWindow << std::endl; OSG_DEBUG << "GraphicsWindowIOS::realizeImplementation / ownsWindow: " << _ownsWindow << std::endl;
@ -863,7 +905,7 @@ bool GraphicsWindowIOS::realizeImplementation()
//but we need to create our views and windows in points. By default we create a full res buffer across all devices. This //but we need to create our views and windows in points. By default we create a full res buffer across all devices. This
//means that for backward compatibility you need to set the windowData _viewContentScaleFactor to 1.0f and set the screen res to the //means that for backward compatibility you need to set the windowData _viewContentScaleFactor to 1.0f and set the screen res to the
//res of the older gen device. //res of the older gen device.
CGRect window_bounds; __block CGRect window_bounds;
osg::Vec2 pointsOrigin = this->pixelToPoint(osg::Vec2(_traits->x, _traits->y)); osg::Vec2 pointsOrigin = this->pixelToPoint(osg::Vec2(_traits->x, _traits->y));
osg::Vec2 pointsSize = this->pixelToPoint(osg::Vec2(_traits->width, _traits->height)); osg::Vec2 pointsSize = this->pixelToPoint(osg::Vec2(_traits->width, _traits->height));
@ -875,9 +917,12 @@ bool GraphicsWindowIOS::realizeImplementation()
//if we own the window we need to create one //if we own the window we need to create one
if (_ownsWindow) if (_ownsWindow)
{
processInMainThread(^
{ {
//create the IOS window object using the viewbounds (in points) required for our context size //create the IOS window object using the viewbounds (in points) required for our context size
_window = [[GraphicsWindowIOSWindow alloc] initWithFrame: window_bounds];// styleMask: style backing: NSBackingStoreBuffered defer: NO]; _window = [[GraphicsWindowIOSWindow alloc] initWithFrame: window_bounds];// styleMask: style backing: NSBackingStoreBuffered defer: NO];
});
if (!_window) { if (!_window) {
OSG_WARN << "GraphicsWindowIOS::realizeImplementation: ERROR: Failed to create GraphicsWindowIOSWindow can not display gl view" << std::endl; OSG_WARN << "GraphicsWindowIOS::realizeImplementation: ERROR: Failed to create GraphicsWindowIOSWindow can not display gl view" << std::endl;
@ -899,10 +944,10 @@ bool GraphicsWindowIOS::realizeImplementation()
#if OSG_GLES1_FEATURES #if OSG_GLES1_FEATURES
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
#elif OSG_GLES2_FEATURES #elif OSG_GLES2_FEATURES
#if OSG_GLES3_FEATURES
#if MULTI_GLES
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]; _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
if(!_context) _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; if (!_context)
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
#else #else
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
#endif #endif
@ -915,23 +960,26 @@ bool GraphicsWindowIOS::realizeImplementation()
#if OSG_GLES1_FEATURES #if OSG_GLES1_FEATURES
OSG_FATAL << "GraphicsWindowIOS::realizeImplementation: ERROR: Failed to create a valid OpenGLES1 context" << std::endl; OSG_FATAL << "GraphicsWindowIOS::realizeImplementation: ERROR: Failed to create a valid OpenGLES1 context" << std::endl;
#elif OSG_GLES2_FEATURES && OSG_GLES3_FEATURES
OSG_FATAL << "GraphicsWindowIOS::realizeImplementation: ERROR: Failed to create a valid OpenGLES3/OpenGLES2 context" << std::endl;
#elif OSG_GLES2_FEATURES #elif OSG_GLES2_FEATURES
OSG_FATAL << "GraphicsWindowIOS::realizeImplementation: ERROR: Failed to create a valid OpenGLES2" << std::endl; OSG_FATAL << "GraphicsWindowIOS::realizeImplementation: ERROR: Failed to create a valid OpenGLES2 context" << std::endl;
#elif OSG_GLES3_FEATURES #elif OSG_GLES3_FEATURES
OSG_FATAL << "GraphicsWindowIOS::realizeImplementation: ERROR: Failed to create a valid OpenGLES3 context" << std::endl; OSG_FATAL << "GraphicsWindowIOS::realizeImplementation: ERROR: Failed to create a valid OpenGLES3 context" << std::endl;
#endif #endif
return false; return false;
} }
__block GraphicsWindowIOSGLView* theView;
processInMainThread(^
{
//create the view to display our context in our window //create the view to display our context in our window
CGRect gl_view_bounds = (_ownsWindow) ? [_window frame] : window_bounds; CGRect gl_view_bounds = (_ownsWindow) ? [_window frame] : window_bounds;
GraphicsWindowIOSGLView* theView = [[ GraphicsWindowIOSGLView alloc ] initWithFrame: gl_view_bounds : this ];
if(!theView)
{
OSG_FATAL << "GraphicsWindowIOS::realizeImplementation: ERROR: Failed to create GraphicsWindowIOSGLView, can not create frame buffers." << std::endl;
return false;
}
theView = [[ GraphicsWindowIOSGLView alloc ] initWithFrame: gl_view_bounds : this ];
if (theView)
{
[theView setAutoresizingMask: ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) ]; [theView setAutoresizingMask: ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) ];
// Apply our content scale factor to our view, this is what converts the views points // Apply our content scale factor to our view, this is what converts the views points
@ -939,6 +987,15 @@ bool GraphicsWindowIOS::realizeImplementation()
#if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0) #if defined(__IPHONE_4_0) && (__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0)
theView.contentScaleFactor = _viewContentScaleFactor; theView.contentScaleFactor = _viewContentScaleFactor;
#endif #endif
}
});
if(!theView)
{
OSG_FATAL << "GraphicsWindowIOS::realizeImplementation: ERROR: Failed to create GraphicsWindowIOSGLView, can not create frame buffers." << std::endl;
return false;
}
[theView setGraphicsWindow: this]; [theView setGraphicsWindow: this];
[theView setOpenGLContext:_context]; [theView setOpenGLContext:_context];
_view = theView; _view = theView;
@ -946,24 +1003,34 @@ bool GraphicsWindowIOS::realizeImplementation()
OSG_DEBUG << "GraphicsWindowIOS::realizeImplementation / view: " << theView << std::endl; OSG_DEBUG << "GraphicsWindowIOS::realizeImplementation / view: " << theView << std::endl;
if (getDeviceOrientationFlags() != WindowData::IGNORE_ORIENTATION) if (getDeviceOrientationFlags() != WindowData::IGNORE_ORIENTATION)
{
processInMainThread(^
{ {
_viewController = [[GraphicsWindowIOSGLViewController alloc] init]; _viewController = [[GraphicsWindowIOSGLViewController alloc] init];
_viewController.view = _view; _viewController.view = _view;
});
} }
processInMainThread(^
{
// Attach view to window // Attach view to window
[_window addSubview: _view]; [_window addSubview: _view];
if ([_window isKindOfClass:[UIWindow class]])
if ([_window isKindOfClass:[UIWindow class]]) // our controller is root
_window.rootViewController = _viewController; _window.rootViewController = _viewController;
[theView release]; else if (windowData->_parentController) // our controller is child
[windowData->_parentController addChildViewController:_viewController];
//if we own the window also make it visible //if we own the window also make it visible
if (_ownsWindow) if (_ownsWindow)
{ {
//show window //show window
[_window makeKeyAndVisible]; [_window makeKeyAndVisible];
} }
});
[theView release];
[pool release]; [pool release];
@ -1021,13 +1088,8 @@ void GraphicsWindowIOS::closeImplementation()
// makeCurrentImplementation // makeCurrentImplementation
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
bool GraphicsWindowIOS:: makeCurrentImplementation() void GraphicsWindowIOS::runOperations()
{ {
//bind the context
[EAGLContext setCurrentContext:_context];
if (_updateContext) if (_updateContext)
{ {
[_view destroyFramebuffer]; [_view destroyFramebuffer];
@ -1035,6 +1097,15 @@ bool GraphicsWindowIOS:: makeCurrentImplementation()
_updateContext = false; _updateContext = false;
} }
GraphicsContext::runOperations();
}
bool GraphicsWindowIOS::makeCurrentImplementation()
{
//bind the context
[EAGLContext setCurrentContext:_context];
//i think we also want to bind the frame buffer here //i think we also want to bind the frame buffer here
// [_view bindFrameBuffer]; // [_view bindFrameBuffer];
@ -1077,7 +1148,9 @@ bool GraphicsWindowIOS::setWindowDecorationImplementation(bool flag)
{ {
if (!_realized || !_ownsWindow) return false; if (!_realized || !_ownsWindow) return false;
BOOL bar_hidden = (flag) ? NO: YES; __block BOOL bar_hidden = (flag) ? NO: YES;
processInMainThread(^
{
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED #ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED > 30100 #if __IPHONE_OS_VERSION_MIN_REQUIRED > 30100
[[UIApplication sharedApplication] setStatusBarHidden: bar_hidden withAnimation:UIStatusBarAnimationNone]; [[UIApplication sharedApplication] setStatusBarHidden: bar_hidden withAnimation:UIStatusBarAnimationNone];
@ -1085,6 +1158,7 @@ bool GraphicsWindowIOS::setWindowDecorationImplementation(bool flag)
[[UIApplication sharedApplication] setStatusBarHidden: bar_hidden animated:NO]; [[UIApplication sharedApplication] setStatusBarHidden: bar_hidden animated:NO];
#endif #endif
#endif #endif
});
return true; return true;
} }
@ -1094,9 +1168,12 @@ bool GraphicsWindowIOS::setWindowDecorationImplementation(bool flag)
// grabFocus // grabFocus
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
void GraphicsWindowIOS::grabFocus() void GraphicsWindowIOS::grabFocus()
{
processInMainThread(^
{ {
//i think make key is the equivalent of focus on iphone //i think make key is the equivalent of focus on iphone
[_window makeKeyWindow]; [_window makeKeyWindow];
});
} }
@ -1113,8 +1190,11 @@ void GraphicsWindowIOS::grabFocusIfPointerInWindow()
// Raise the window to the top. // Raise the window to the top.
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
void GraphicsWindowIOS::raiseWindow() void GraphicsWindowIOS::raiseWindow()
{
processInMainThread(^
{ {
[_window bringSubviewToFront:_view]; [_window bringSubviewToFront:_view];
});
} }
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
@ -1205,8 +1285,7 @@ osg::Vec2 GraphicsWindowIOS::pointToPixel(const osg::Vec2& point)
osg::Vec2 GraphicsWindowIOS::pixelToPoint(const osg::Vec2& pixel) osg::Vec2 GraphicsWindowIOS::pixelToPoint(const osg::Vec2& pixel)
{ {
float scaler = 1.0f / _viewContentScaleFactor; return pixel / _viewContentScaleFactor;
return pixel * scaler;
} }
@ -1246,4 +1325,4 @@ public:
REGISTER_WINDOWINGSYSTEMINTERFACE(IOS, ConcreteIOSWindowingSystemInterface) REGISTER_WINDOWINGSYSTEMINTERFACE(IOS, ConcreteIOSWindowingSystemInterface)
}//end namspace }//end namespace