This case is in OpenGL ES case 06: GLKit using index drawing case on the basis of the new texture and color mixed fill function

Compared with the texture color filling in GLSL case, GLKit is much simpler, because most of the code has been packaged by Apple and we just need to use it

The whole case is also an example of OpenGL ES case 06: GLKit using Index Drawing and OpenGL ES Introduction: GLKit loading image case synthesis

Based on the original code, the overall picture that needs to be modified is shown below

Add texture data and operations to render graphics function

  • Added texture coordinates to vertex data
// The first three elements are vertex data; The middle three elements are vertex colors, Last 2 is texture coordinates GLfloat attrArr [] = {0.5 f, f 0.5, 0.0 f, f 1.0, 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, 0.0 f, 1.0 f, f 1.0, 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};Copy the code
  • Note: You need to modify the total length of the vertices and colors read, otherwise the drawing will have problems
/ / -- -- -- -- -- - use the texture data glEnableVertexAttribArray (GLKVertexAttribTexCoord0); glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*8, (GLfloat*)NULL+6);Copy the code
  • Get texture picture & Load texture
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"mouse" ofType:@" JPG "]; // ------ obtain texture path NSString *filePath = [[NSBundle mainBundle] pathForResource:@"mouse" ofType:@" JPG "]; NSDictionary *option = [NSDictionary dictionaryWithObjectsAndKeys:@"1", GLKTextureLoaderOriginBottomLeft, nil]; GLKTextureInfo *info = [GLKTextureLoader textureWithContentsOfFile:filePath options:option error:nil];Copy the code
  • Set texture information in Effect
//------ shader self.mEffect = [[GLKBaseEffect alloc] init]; self.mEffect.texture2d0.enabled = GL_TRUE; self.mEffect.texture2d0.name = info.name;Copy the code

See Github-11_02_GLkit_ Triangle Transform + Texture and Color Mix OC, 11_02_GLKit Triangle Transform + Texture and color Mix _Swift for the complete code