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
|
#import "CustomLayer.h"
#import "vtkCocoaRenderWindow.h"
#import "vtkCocoaRenderWindowInteractor.h"
#import "vtkRenderWindow.h"
#import "vtkRenderWindowInteractor.h"
#import "vtkRenderer.h"
@implementation CustomLayer
// ---------------------------------------------------------------------------------------------------------------
// This is CAOpenGLLayer's entry point for drawing. The OS calls it when it's time to draw.
- (void)drawInCGLContext:(CGLContextObj)inCGLContext
pixelFormat:(CGLPixelFormatObj)inPixelFormat
forLayerTime:(CFTimeInterval)inTimeInterval
displayTime:(nullable const CVTimeStamp*)inTimeStamp
{
assert(inCGLContext);
assert(inPixelFormat);
// Get the related view.
CustomView* customView = [self customView];
assert(customView);
// Tell VTK to render.
assert([customView renderWindowInteractor] -> GetInitialized());
vtkCocoaRenderWindow* renderWindow = [customView renderWindow];
if (renderWindow && renderWindow->GetMapped())
{
bool contextInitialised = renderWindow->Superclass::InitializeFromCurrentContext();
assert(contextInitialised);
(void)contextInitialised;
renderWindow->SetFrameBlitModeToBlitToCurrent();
renderWindow->Render();
}
// Generally, subclasses should call the superclass implementation of the method to flush the
// context after rendering. But VTK itself flushes, so it's probably not necessary to call super.
#if 0
[super drawInCGLContext:inCGLContext
pixelFormat:inPixelFormat
forLayerTime:inTimeInterval
displayTime:inTimeStamp];
#endif
}
// ---------------------------------------------------------------------------------------------------------------
// CAOpenGLLayer triggers this function when a context is needed by the receiver. Here we return the
// VTK context.
- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)inPixelFormat
{
assert(inPixelFormat);
(void)inPixelFormat;
// Get the related view.
CustomView* customView = [self customView];
assert(customView);
// Fetch the rendering window's context.
vtkCocoaRenderWindow* renderWindow = [customView renderWindow];
assert(renderWindow);
// Get the OpenGL context from VTK.
assert([customView renderWindowInteractor] -> GetInitialized());
NSOpenGLContext* openGLContext = (__bridge NSOpenGLContext*)(renderWindow->GetContextId());
assert(openGLContext);
// Convert to CGLContextObj.
CGLContextObj cglContext = (CGLContextObj)[openGLContext CGLContextObj];
assert(cglContext);
return cglContext;
}
// ---------------------------------------------------------------------------------------------------------------
- (void)releaseCGLContext:(CGLContextObj)inContext
{
(void)inContext;
}
// ---------------------------------------------------------------------------------------------------------------
- (void)releaseCGLPixelFormat:(CGLPixelFormatObj)inPixelFormat
{
(void)inPixelFormat;
}
@end
|