-1

I have been using some OpenGL code for my objective-c camera capture session, which I barely understand. Now OpenGL is depreciated, and I have no idea how to convert this little bit of OpenGL code to metal. If anyone knows both enough to convert the below please help.

if (self.eaglContext != [EAGLContext currentContext]) {
    [EAGLContext setCurrentContext:self.eaglContext];
}
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

there is a little more OpenGL I didn't see:

_eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
_videoPreviewView = [[GLKView alloc] initWithFrame:self.view.bounds context:_eaglContext];
_videoPreviewView.enableSetNeedsDisplay = NO;
_videoPreviewView.transform = CGAffineTransformMakeRotation(M_PI_2);
_videoPreviewView.frame = self.view.bounds;
[self.view addSubview:_videoPreviewView];
[self.view sendSubviewToBack:_videoPreviewView];
[_videoPreviewView bindDrawable];
_videoPreviewViewBounds = CGRectZero;
_videoPreviewViewBounds.size.width = _videoPreviewView.drawableWidth;
_videoPreviewViewBounds.size.height = _videoPreviewView.drawableHeight;
_ciContext = [CIContext contextWithEAGLContext:_eaglContext options:@{kCIContextWorkingColorSpace : [NSNull null]} ];

1 Answers1

3

It's not a trivial conversion from OpenGL to Metal. The first step would be to replace the GLKView with an MTKView. You'll want to then create an id<MTKViewDelegate> to handle the actual drawing, resizing, etc. I recommend watching the Adopting Metal video from WWDC 2016. It shows how to get up and running with an MTKView.

To do the blending, you'll need to set the blending options on the MTLRenderPipelineColorAttachmentDescriptor that you use to create the render pipeline state. You'll need to set the blendingEnabled property to YES and set the rgbBlendOperation property to MTLBlendOperationAdd. Then you'll set the sourceRGBBlendFactor to MTLBlendFactorOne and the destinationRGBBlendFactor to MTLBlendFactorOneMinusSourceAlpha.

You can create a CIContext from a id<MTLDevice> via +[CIContext contextWithMetalDevice:options:]

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • Could you also have a look at this? https://stackoverflow.com/questions/55910290/avaudiosession-plays-sound-on-receiver-or-mic-switches-to-front?noredirect=1#comment98576729_55910290 – Spring May 03 '19 at 16:05
  • Sorry I have never used the AV framework, so don't know anything about that. – user1118321 May 04 '19 at 01:24