Skip to content

Instantly share code, notes, and snippets.

@wuyongrui
Created April 20, 2016 01:53
Show Gist options
  • Select an option

  • Save wuyongrui/6e6c17cbbe4e72f764bb6cbc1820b4e9 to your computer and use it in GitHub Desktop.

Select an option

Save wuyongrui/6e6c17cbbe4e72f764bb6cbc1820b4e9 to your computer and use it in GitHub Desktop.
OpenGL ES绘制底图+点击添加小图块
//
// OpenGLView.m
#import "OpenGLView.h"
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
#import <QuartzCore/QuartzCore.h>
#import "Utils+OpenGL.h"
const GLfloat Vertices[] = {
1, 1, 0, // 右下,红色
1, -1, 0,// 左下,黑色
-1, 1, 0, // 左上,蓝色
-1, -1, 0
};
// 颜色数组
const GLfloat Colors[] = {
0,0,0,1, // 左下,黑色
1,0,0,1, // 右下,红色
0,0,1,1, // 左上,蓝色
1,0,0,1
};
@interface OpenGLView() {
EAGLContext *_context;
CAEAGLLayer *_layer;
GLuint _renderColorbuffer;
GLuint _frameBuffer;
GLuint _positionSlot;
GLuint _colorSlot;
// tex
// GLuint _textureSlot;
// GLuint _textureCoordsSlot;
// GLuint _textureId;
}
@end
@implementation OpenGLView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
[self setupLayer];
[self setupContext];
[self setupRenderbuffer];
[self setupFramebuffer];
[self setupVBOs];
[self compileShaders];
// glEnable(GL_BLEND);
// glBlendFunc(<#GLenum sfactor#>, <#GLenum dfactor#>)
[self render];
}
return self;
}
+ (Class)layerClass {
return [CAEAGLLayer class];
}
#pragma mark setup
- (void)setupLayer {
_layer = (CAEAGLLayer *) self.layer;
_layer.opaque = YES;
}
- (void)setupContext {
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!_context) {
NSLog(@"setup context fail.");
exit(1);
}
if (![EAGLContext setCurrentContext:_context]) {
NSLog(@"setup context fail.");
exit(1);
}
}
- (void)setupRenderbuffer {
glGenRenderbuffers(1, &_renderColorbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _renderColorbuffer);
[_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_layer];
}
- (void)setupFramebuffer {
glGenFramebuffers(1, &_frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderColorbuffer);
}
- (void)setupVBOs {
// GLuint vertexBuffer;
// glGenBuffers(1, &vertexBuffer);
// glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
// glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
//
// GLuint indexBuffer;
// glGenBuffers(1, &indexBuffer);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
}
- (void)compileShaders {
GLuint vertexShader = [Utils compileShader:@"VertexShader_ShowImage"
withType:GL_VERTEX_SHADER];
GLuint fragmentShader = [Utils compileShader:@"FragmentShader_ShowImage"
withType:GL_FRAGMENT_SHADER];
GLuint programHandle = glCreateProgram();
glAttachShader(programHandle, vertexShader);
glAttachShader(programHandle, fragmentShader);
glLinkProgram(programHandle);
glUseProgram(programHandle);
_positionSlot = glGetAttribLocation(programHandle, "Position");
_colorSlot = glGetAttribLocation(programHandle, "SourceColor");
GLint linkSuccess;
glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess);
if (linkSuccess == GL_FALSE) {
GLchar messages[256];
glGetProgramInfoLog(programHandle, sizeof(messages), 0, &messages[0]);
NSString *messageString = [NSString stringWithUTF8String:messages];
NSLog(@"%@", messageString);
exit(1);
}
glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);
}
- (void)render {
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 100, self.frame.size.width, self.frame.size.width - 200);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, 0, Colors);
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, Vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:self];
glViewport(point.x, CGRectGetHeight(self.frame) - point.y, 20, 20);
// 取出Colors数组中的每个坐标点的颜色值,赋给_colorSlot
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, 0, Colors);
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, Vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment