rendering

process

0. Preparation

  • ViewController inherits from GLKViewController
  • #import
  • Defining extended attributes
// context @property(nonatomic,strong)EAGLContext *mContext; //GLKBaseEffect @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;Copy the code
  • The timer dispatch_source_t
dispatch_source_t timer;
Copy the code

1. Create a new layer and set GLKView and 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

2. Render graphics

  • Vertex data
F GLfloat attrArr [] = {0.5, 0.5 f to 0.0 f to 1.0 f to 0.0 f to 1.0 f, / / upper left 0.5 f, f, 0.5 0.0 f, f 1.0, 0.0 f, 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};Copy the code
  • Index of the drawing
		GLuint indices[] =
    {
        0, 3, 2,
        0, 1, 3,
        0, 2, 4,
        0, 4, 1,
        2, 3, 4,
        1, 4, 3,
    };
Copy the code
  • The number of vertices
self.count = sizeof(indices) /sizeof(GLuint);
Copy the code
  • Put the array of vertices into the array buffer GL_ARRAY_BUFFER
	GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(attrArr), attrArr, GL_STATIC_DRAW);
Copy the code
  • Store the index array into the index buffer 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);
Copy the code
  • Use vertex data GLKVertexAttribPosition
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, NULL);
Copy the code
  • Use the color data GLKVertexAttribColor
	glEnableVertexAttribArray(GLKVertexAttribColor);
    glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, (GLfloat *)NULL + 3);
Copy the code
  • Initialize the shader
self.mEffect = [[GLKBaseEffect alloc]init];
Copy the code
  • Projection view
CGSize size = self.view.bounds.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;Copy the code
  • Model view
	GLKMatrix4 modelViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, -2.0f);
    self.mEffect.transform.modelviewMatrix = modelViewMatrix;
Copy the code
  • The timer
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

GLKViewDelegate

  • Scenario change update -update
-(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
  • Redraw – glkView: drawInRect:
- (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

IBAction Click event

- (IBAction)XClick:(id)sender { _XB = ! _XB; } - (IBAction)YClick:(id)sender { _YB = ! _YB; } - (IBAction)ZClick:(id)sender { _ZB = ! _ZB; }Copy the code

Complete code implementation:

#import "ccViewController.h" @interface CCViewController() // context @property(nonatomic,strong)EAGLContext *mContext; //GLKBaseEffect @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 CCViewController { dispatch_source_t timer; } -(void)viewDidLoad { [super viewDidLoad]; //1. Create a new layer [self setupContext]; //2. Render [self render]; } //2. Render -(void)render {//1. Vertex data // The first three elements are vertex data; The middle three elements are vertex colors, Last 2 is texture coordinates / / GLfloat attrArr [] = / / {/ f / 0.5, 0.5 f, 0.0 f, 1.0 f, f 0.0, 1.0 f, 0.0 f, 1.0 f, / / left/f / 0.5, 0.5 f, 0.0 f, 1.0 f, f, 0.0 to 1.0 f, f 1.0, 1.0, f / / right/f / 0.5, 0.5 f, f, 0.0 1.0 f, f 1.0, 1.0 f, f 0.0, 0.0 f, the left lower / / / / / f / 0.5, 0.5 f, 0.0 f, 1.0 f, f, 1.0 to 1.0 f, f 1.0, 0.0, f / / right/f / 0.0, 0.0 f, f 1.0, 0.0 f, f 1.0, 0.0 f, f 0.5, 0.5, f / / vertex / /}; F GLfloat attrArr [] = {0.5, 0.5 f to 0.0 f to 1.0 f to 0.0 f to 1.0 f, / / upper left 0.5 f, f, 0.5 0.0 f, f 1.0, 0.0 f, 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); 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; } - (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); } //1. 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); } #pragma mark -XYZClick - (IBAction)XClick:(id)sender { _XB = ! _XB; } - (IBAction)YClick:(id)sender { _YB = ! _YB; } - (IBAction)ZClick:(id)sender { _ZB = ! _ZB; } @endCopy the code

Texture + color mix render

Need to modify render function: add texture coordinates + load texture

F GLfloat attrArr [] = {0.5, 0.5 f to 0.0 f to 1.0 f to 0.0 f to 1.0 f to 0.0 f to 1.0 f, / / upper left 0.5 f, f 0.5, 0.0 f, f 1.0, 0.0 f, f 1.0, 1.0, f 1.0 f, / / upper right - 0.5 f, 0.5 f, f, 0.0 1.0 f, f 1.0, 1.0 f, f 0.0, 0.0, f / / lower left 0.5 f to 0.5 f, f 0.0, 1.0, f 1.0 f, f 1.0, 1.0, f 0.0 0.0 f, / / lower 0.0 f, f, f 1.0, 0.0 f, f 1.0, 0.0 f, f 0.5, 0.5, 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); GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(attrArr), attrArr, GL_DYNAMIC_DRAW); GLuint index; glGenBuffers(1, &index); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*8, NULL); glEnableVertexAttribArray(GLKVertexAttribColor); glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*8, (GLfloat *)NULL+3); glEnableVertexAttribArray(GLKVertexAttribTexCoord0); glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*8, (GLfloat *)NULL+6); NSString *filePath = [[NSBundle mainBundle]pathForResource:@"kunkun" ofType:@" JPG "]; NSString *filePath = [[NSBundle mainBundle]pathForResource:@"kunkun" ofType:@" JPG "]; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:@"1",GLKTextureLoaderOriginBottomLeft, nil]; GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithContentsOfFile:filePath options:options error:nil]; self.mEffect = [[GLKBaseEffect alloc]init]; self.mEffect.texture2d0.enabled = GL_TRUE; self.mEffect.texture2d0.name = textureInfo.name; CGSize size = self.view.bounds.size; float aspect = fabs(size.width/size.height); GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective (GLKMathDegreesToRadians (90.0), the aspect, 0.1, 100.0 f); self.mEffect.transform.projectionMatrix = projectionMatrix; GLKMatrix4 modelViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0, 0, -2); self.mEffect.transform.modelviewMatrix = modelViewMatrix; // timer: -> GCD 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