如何在OpenGL ES中绘制多个2D对象并在iOS中通过触摸移动它们?

时间:2015-05-16 17:07:31

标签: ios objective-c opengl-es draw

我正在尝试绘制多个2D对象,并且能够在OpenGL ES for iOS中通过触摸移动它们。我开始绘制一个正方形的教程,但我不知道如何添加触摸来移动它。我希望能够加载多个对象并通过触摸在屏幕上移动它们。

#import "HelloGLKitViewController.h"
typedef struct {
float Position[3];
float Color[4];
} Vertex;

const Vertex Vertices[] = {
{{1, -1, 0}, {1, 0, 0, 1}},
{{1, 1, 0}, {0, 1, 0, 1}},
{{-1, 1, 0}, {0, 0, 1, 1}},
{{-1, -1, 0}, {0, 0, 0, 1}}
};

const GLubyte Indices[] = {
0, 1, 2,
2, 3, 0
};

GLuint _vertexBuffer;
GLuint _indexBuffer;


@interface HelloGLKitViewController ()

@property (strong, nonatomic) EAGLContext *context;
@property (strong, nonatomic) GLKBaseEffect *effect;



@end

@implementation HelloGLKitViewController



- (void)viewDidLoad {
[super viewDidLoad];

self.context = [[EAGLContext alloc]     initWithAPI:kEAGLRenderingAPIOpenGLES2];

if (!self.context) {
    NSLog(@"Failed to create ES context");
}

GLKView *view = (GLKView *)self.view;
view.context = self.context;

[self setupGL];
}

- (void)viewDidUnload
{
[super viewDidUnload];

if ([EAGLContext currentContext] == self.context) {
    [EAGLContext setCurrentContext:nil];
}
self.context = nil;

[self tearDownGL];
}

#pragma mark - GLKViewDelegate

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {

glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
[self.effect prepareToDraw];

glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));

glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}

#pragma mark - GLKViewControllerDelegate

- (void)update {


float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 4.0f, 10.0f);
self.effect.transform.projectionMatrix = projectionMatrix;

GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -6.0f);
self.effect.transform.modelviewMatrix = modelViewMatrix;
}



- (void)setupGL {

[EAGLContext setCurrentContext:self.context];
self.effect = [[GLKBaseEffect alloc] init];

glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);

}

- (void)tearDownGL {

[EAGLContext setCurrentContext:self.context];

glDeleteBuffers(1, &_vertexBuffer);
glDeleteBuffers(1, &_indexBuffer);
self.effect = nil;


}

@end

1 个答案:

答案 0 :(得分:0)

对于您发布的代码,您需要制作另一个GLKBaseEffect,并根据您收到的内容独立更新每个矩阵的modelviewMatrix属性。