The renderings are as follows

The whole process

  1. Controller property declaration and preparation
  2. Create layers and render graphics
  3. Implement the GLKViewDelegate delegate method
  4. Use the update function to achieve the update effect
  5. Custom function implementation: mainly the effect of X, Y, Z button click events

Controller property declaration and preparation

// // dnViewController.m // 002--GLKit triangle transform // // Created by Dean on 2020/08/16. // Copyright © 2020 muxinjian.all rights reserved. // #import "DNViewController.h" @interface DNViewController() @property(nonatomic,strong)EAGLContext *mContext; @property(nonatomic,strong)GLKBaseEffect *mEffect; @property(nonatomic,assign)int count; @property(nonatomic,assign)float XDegree; @property(nonatomic,assign)float YDegree; @property(nonatomic,assign)float ZDegree; // whether to rotate X,Y,Z @property(nonatomic,assign) BOOL XB; @property(nonatomic,assign) BOOL YB; @property(nonatomic,assign) BOOL ZB; @end @implementation DNViewController { dispatch_source_t timer; }Copy the code

Create layers and render graphics

  1. An implementation of viewDidLoad
-(void)viewDidLoad { [super viewDidLoad]; //1. Create a new layer [self setupContext]; //2. Render [self render]; }Copy the code
  1. Implementation of the setupContext function
Create a new layer -(void)setupContext {//1. New OpenGL ES context self. MContext = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2]; GLKView *view = (GLKView *)self.view; view.context = self.mContext; view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888; view.drawableDepthFormat = GLKViewDrawableDepthFormat24; [EAGLContext setCurrentContext:self.mContext]; glEnable(GL_DEPTH_TEST); }Copy the code
  1. Implementation of the Render function
Void render {// void render {// void render; The middle three elements, Is a vertex color value GLfloat attrArr [] = {0.5 f, f 0.5, 0.0 f, f 1.0, 0.0 f, 1.0 f, / / upper left 0.5 f, f, 0.5 0.0 f, 1.0 f, f 0.0, 1.0, f / / upper right - 0.5 f, 0.5 f, f 0.0, 1.0 f, f 1.0, 1.0, f / / lower left 0.5 f to 0.5 f, f 0.0, 1.0 f, f 1.0, 1.0, f / / lower 0.0 f, f 0.0, 1.0, f 0.0 f, f 1.0, 0.0, f / / vertex}; / / 2. The drawing index GLuint indices [] = {0, 3, 2, 0, 1, 3, 0, 2, 4, 0, 4, 1, 2, 3, 4, 1, 4, 3,}; Self. count = sizeof(indices) /sizeof(GLuint); GL_ARRAY_BUFFER GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(attrArr), attrArr, GL_STATIC_DRAW); GL_ELEMENT_ARRAY_BUFFER GLuint index; glGenBuffers(1, &index); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); / / use vertex data glEnableVertexAttribArray (GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, NULL); / / using the color data glEnableVertexAttribArray (GLKVertexAttribColor); glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, (GLfloat *)NULL + 3); // self.mEffect = [[GLKBaseEffect alloc]init]; // CGSize size = self.viewbound.size; float aspect = fabs(size.width / size.height); GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective (GLKMathDegreesToRadians (90.0), the aspect, 0.1 f, 100.0); ProjectionMatrix = GLKMatrix4Scale(projectionMatrix, 1.0f, 1.0f, 1.0f); self.mEffect.transform.projectionMatrix = projectionMatrix; GLKMatrix4 modelViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, -2.0f); self.mEffect.transform.modelviewMatrix = modelViewMatrix; // Double seconds = 0.1; timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()); Dispatch_source_set_timer (timer, DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC, 0.0); Dispatch_source_set_event_handler (timer, ^{self.xdegree += 0.1f * self.xb; Self. YDegree += 0.1f * self.YB; Self. ZDegree += 0.1f * self.ZB; }); dispatch_resume(timer); }Copy the code

Implement the GLKViewDelegate delegate method

- (void)glkView:(glkView *)view drawInRect:(CGRect)rect {glClearColor(0.3f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); [self.mEffect prepareToDraw]; glDrawElements(GL_TRIANGLES, self.count, GL_UNSIGNED_INT, 0); }Copy the code

Use the update function to achieve the update effect

Void update {GLKMatrix4 modelViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, -2.5); modelViewMatrix = GLKMatrix4RotateX(modelViewMatrix, self.XDegree); modelViewMatrix = GLKMatrix4RotateY(modelViewMatrix, self.YDegree); modelViewMatrix = GLKMatrix4RotateZ(modelViewMatrix, self.ZDegree); self.mEffect.transform.modelviewMatrix = modelViewMatrix; }Copy the code

Custom function implementation: mainly the effect of X, Y, Z button click events

#pragma mark -XYZClick - (IBAction)XClick:(id)sender { _XB = ! _XB; } - (IBAction)YClick:(id)sender { _YB = ! _YB; } - (IBAction)ZClick:(id)sender { _ZB = ! _ZB; } @endCopy the code