ARKit的理论常识差不多到此停止了,下一篇将会介绍若何应用OpenGL ES衬着ARFrame里的内容。
实现篇
本文所用的代码在 https://github.com/SquarePants1991/OpenGLESLearn.git 的ARKit分支中。
@interface VideoPlane : GLObject@property (assign, nonatomic) GLuint yuv_yTexture;@property (assign, nonatomic) GLuint yuv_uvTexture;- (instancetype)initWithGLContext:(GLContext *)context;- (void)update:(NSTimeInterval)timeSinceLastUpdate;- (void)draw:(GLContext *)glContext;@end...- (void)draw:(GLContext *)glContext { [glContext setUniformMatrix4fv:@"modelMatrix" value:self.modelMatrix]; bool canInvert; GLKMatrix4 normalMatrix = GLKMatrix4InvertAndTranspose(self.modelMatrix, &canInvert); [glContext setUniformMatrix4fv:@"normalMatrix" value:canInvert ? normalMatrix : GLKMatrix4Identity]; [glContext bindTextureName:self.yuv_yTexture to:GL_TEXTURE0 uniformName:@"yMap"]; [glContext bindTextureName:self.yuv_uvTexture to:GL_TEXTURE1 uniformName:@"uvMap"]; [glContext drawTrianglesWithVAO:vao vertexCount:6];}本文所用OpenGL基本代率攀来自OpenGL ES系列,具备衬着几何体,纹理等基本功能,实现细节将不赘述。
集成ARKit的关键代码都在ARGLBaseViewController中。我们来看一下它的代码。
AR最根本的概念就是将虚拟的计算机图形和真实情况浇忧⒛技巧。该技巧有很多种实现方法。
-
本文所用的代码在https://github.com/SquarePants1991/OpenGLESLearn.git的ARKit分支中。
处理ARFrame
道理篇
- (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame { // 同步YUV信息到 yTexture 和 uvTexture CVPixelBufferRef pixelBuffer = frame.capturedImage; GLsizei imageWidth = (GLsizei)CVPixelBufferGetWidthOfPlane(pixelBuffer, 0); GLsizei imageHeight = (GLsizei)CVPixelBufferGetHeightOfPlane(pixelBuffer, 0); void * baseAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0); glBindTexture(GL_TEXTURE_2D, self.yTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, imageWidth, imageHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, baseAddress); glBindTexture(GL_TEXTURE_2D, 0); imageWidth = (GLsizei)CVPixelBufferGetWidthOfPlane(pixelBuffer, 1); imageHeight = (GLsizei)CVPixelBufferGetHeightOfPlane(pixelBuffer, 1); void *laAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1); glBindTexture(GL_TEXTURE_2D, self.uvTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, imageWidth, imageHeight, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, laAddress); glBindTexture(GL_TEXTURE_2D, 0); self.videoPlane.yuv_yTexture = self.yTexture; self.videoPlane.yuv_uvTexture = self.uvTexture; [self setupViewport: CGSizeMake(imageHeight, imageWidth)]; // 同步摄像机 matrix_float4x4 cameraMatrix = matrix_invert([frame.camera transform]); GLKMatrix4 newCameraMatrix = GLKMatrix4Identity; for (int col = 0; col < 4; ++col) { for (int row = 0; row < 4; ++row) { newCameraMatrix.m[col * 4 + row] = cameraMatrix.columns[col][row]; } } self.cameraMatrix = newCameraMatrix; GLKVector3 forward = GLKVector3Make(-self.cameraMatrix.m13, -self.cameraMatrix.m23, -self.cameraMatrix.m33); GLKMatrix4 rotationMatrix = GLKMatrix4MakeRotation(M_PI / 2, forward.x, forward.y, forward.z); self.cameraMatrix = GLKMatrix4Multiply(rotationMatrix, newCameraMatrix);}膳绫擎的代码展示了若何处理ARKit捕获的ARFrame,ARFrame的capturedImage存储了摄像头捕获的图片信息,类型是CVPixelBufferRef。默认情况下,图片信息的格局是YUV,经由过程两个Plane来存储,也可以懂得为两张图片。一张格局是Y(Luminance),保存了明度信息,另一张是UV(Chrominance、Chroma),保存了色度和浓度。我们须要把这两张图分别绑定到不呵9依υ?理上,然后在Shader中应用算法将YUV转换成RGB。下面是处理纹理的Fragment Shader,应用公式进行色彩转换。
precision highp float;varying vec3 fragNormal;varying vec2 fragUV;uniform float elapsedTime;uniform mat4 normalMatrix;uniform sampler2D yMap;uniform sampler2D uvMap;void main(void) { vec4 Y_planeColor = texture2D(yMap, fragUV); vec4 CbCr_planeColor = texture2D(uvMap, fragUV); float Cb, Cr, Y; float R ,G, B; Y = Y_planeColor.r * 255.0; Cb = CbCr_planeColor.r * 255.0 - 128.0; Cr = CbCr_planeColor.a * 255.0 - 128.0; R = 1.402 * Cr + Y; G = -0.344 * Cb - 0.714 * Cr + Y; B = 1.772 * Cb + Y; vec4 videoColor = vec4(R / 255.0, G / 255.0, B / 255.0, 1.0); gl_FragColor = videoColor;}处理并绑定好纹理后,为了包管不合屏幕尺寸下,纹理不被非等比拉伸,所以对viewport进行重了新计算[self setupViewport: CGSizeMake(imageHeight, imageWidth)];。接下来将ARKit计算出来的摄像机的变换赋值给self.cameraMatrix。留意ARKit捕获的图片须要扭转90度后才能正常显示,所以在设置Viewport时特意倒置了宽和高,并在最后对摄像机进行了扭转。
VideoPlane
VideoPlane是为了显示视频编写的几何体,它可以或许接收两个纹理,Y和UV。
其他的功能很简单,就是绘制一个正方形,最终合营显示视频的Shader,衬着YUV格局的数据。
推荐阅读
【51CTO.com快译】 有些开辟团队十分推许主动化测试,弗成否定,主动化测试在回归测试和检查冗余组件方面不掉>>>详细阅读
本文标题:ARKit & OpenGL ES - ARKit 原理及实现
地址:http://www.17bianji.com/lsqh/36017.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示