Opencv for iOS study notes (8)-rendering ar scenario 1 with OpenGL

Source: Internet
Author: User

Original address: opencv for iOS study notes (8)-rendering ar scenario 1 with OpenGL

Create an OpenGL rendering layer

To use OpenGL functions in your application, you must add a drawing board for your application to present the rendered scenario to the user.

To encapsulate the initialization logic of OpenGL canvas, we will introduce the eaglview class:

/// Eaglview. h // opencvdemo /// created by Apple on 13-4-24. // copyright (c) 2013 hxy. all rights reserved. // # import <uikit/uikit. h> # import <opengles/ES1/GL. h> # import <opengles/ES1/glext. h> # import <opengles/ES2/GL. h> # import <opengles/ES2/glext. h> @ Class eaglcontext; @ interface eaglview: uiview {@ private gluint defaframeframebuffer, colorrenderbuffer, depthrenderbuffer;} @ property (nonatomic, retain) eaglcontext * context; @ property (readonly) glint framebufferwidth, framebufferheight; // initialize the canvas-(void) initcontext; // set the frame cache-(void) setframebuffer; // render the frame cache-(void) presentframebuffer; @ end

/// Eaglview. M // opencvdemo /// created by Apple on 13-4-24. // copyright (c) 2013 hxy. all rights reserved. // # import "eaglview. H "@ interface eaglview (private) // creates a frame cache-(void) createframebuffer; // deletes all caches-(void) deleteframebuffer; @ end @ implementation eaglview @ synthesize context, framebufferheight, framebufferwidth;-(void) dealloc {[self deleteframebuffer]; If ([eaglcontext currentcontext] = context ){ [Eaglcontext setcurrentcontext: Nil];} [Super dealloc];} + (class) layerclass {return [caeagllayer class];}-(void) layoutsubviews {// when setting the frame cache next time, the frame cache will re-create [self deleteframebuffer];}-(void) initcontext {If (! Context) {eaglrenderingapi API = keaglrenderingapiopengles2; Context = [[eaglcontext alloc] initwithapi: API]; If (! Context) {nslog (@ "failed to create the canvas, and an error occurred while running! "); Exit (1); return;} If (! [Eaglcontext setcurrentcontext: Context]) {nslog (@ "tragedy! "); Exit (1); Return ;}}- (void) setcontext :( eaglcontext *) newcontext {If (context! = Newcontext) {[self deleteframebuffer]; Context = newcontext; [eaglcontext setcurrentcontext: Nil] ;}}- (caeagllayer *) mylayer {return (caeagllayer *) self. layer;}-(ID) initwithcoder :( nscoder *) adecoder {If (Self = [Super initwithcoder: adecoder]) {caeagllayer * layer = [self mylayer]; layer. opaque = true; layer. drawableproperties = [nsdictionary dictionarywithobjectsandkeys: [nsnumber numberwithbool: False], encoding, keaglcolorformatrgba8, keagldrawablepropertycolorformat, nil]; [self initcontext];} return self;}-(void) setframebuffer {If (context) {[eaglcontext setcurrentcontext: context]; If (! Depthrenderbuffer) {[self createframebuffer];} glbindframebuffer (gl_framebuffer, defaultframebuffer); glviewport (0, 0, framebufferwidth, framebufferheight); glclear (callback | callback);}-(void) presentframebuffer {bool success = false; If (context) {[eaglcontext setcurrentcontext: Context]; glbindrenderbuffer (1, colorrenderbuffer); success = [context presentrenderbuffer: G L_renderbuffer] ;}}- (void) createframebuffer {// The default frame cache if (Context &&! Defaframeframebuffer) {[eaglcontext setcurrentcontext: Context]; // creates the default frame cache object glgenframebuffers (1, & (defaframeframebuffer); glbindframebuffer (gl_framebuffer, defaframeframebuffer); // creates a color cache, allocate memory glgenrenderbuffers (1, & colorrenderbuffer); glbindrenderbuffer (gl_renderbuffer, colorrenderbuffer); [context renderbufferstorage: gl_renderbuffer fromdrawable: [self mylayer]; callback (gl_render Buffer, buffers, & framebufferwidth); buffers (gl_renderbuffer, buffers, & framebufferheight); glframebufferrenderbuffer (gl_framebuffer, buffers, gl_renderbuffer, colorrenderbuffer); // generates a deep cache glgenrenderbuffers (1, & depthrenderbuffer); glbindrenderbuffer (gl_renderbuffer, depthrenderbuffer); glrenderbufferstorage (gl_renderbuffer, gl_depth_c Omponent16, framebufferwidth, framebufferheight); glframebufferrenderbuffer (gl_framebuffer, latency, gl_renderbuffer, depthrenderbuffer); // check the status if (glcheckframebufferstatus (gl_framebuffer )! = Gl_framebuffer_complete) {nslog (@ "failed to make complete framebuffer object % x", glcheckframebufferstatus (gl_framebuffer) ;}}- (void) deleteframebuffer {If (context) {[eaglcontext setcurrentcontext: Context]; If (defaframeframebuffer) {buffers (1, & defaframeframebuffer); defaframeframebuffer = 0;} If (colorrenderbuffer) {gldeleterenderbuffers (1, & colorrenderbuffer ); colorrenderbuffer = 0;} If (depthrenderbuffer) {gldeleterenderbuffers (1, & depthrenderbuffer); depthrenderbuffer = 0 ;}}} @ end

Rendering ar scenarios

The above eaglview does not include any 3D object virtualization method. Of course, we did it on purpose. The task of eaglview is to provide a rendering canvas. Separating the responsibilities of each class will help us change the logic of the virtualization process in the next step.

To achieve the virtualization of augmented reality, we will create a separate class visualizationcontroller:

@ Interface simplevisualizationcontroller: nsobject {eaglview * m_glview; gluint vertex; STD: vector <transformation> m_transformations; m_calibration; cgsize m_framesize;}-(ID) vertex :( eaglview *) view calibration :( cameracalibration) Calibration framesize :( cgsize) size; // render the AR to the eaglview target view-(void) drawframe;-(void) updatebackground :( bgravideoframe) frame; -(void) settransformationlist :( const STD: vector <transformation> &) transformations; @ end

Unfinished

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.