我有一個GLKView的子類,我想圍繞x軸旋轉它。我試圖在raywenderlich的教程中應用該方法,但沒有結果。不同的是,我有一個GLKView,他碰巧用GLKViewController(+它的委託)做所有事情。所以我沒有委託的更新方法等。我幾乎沒有OpenGL的經驗(剛開始),所以我希望我在這裏遇到一些啓發。如何圍繞x軸旋轉GLKView?
ViewController.m
EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
BlogCell *view = [[BlogCell alloc] initWithFrame:CGRectMake(0, 200, 320, 80) context:context];
view.drawableMultisample = GLKViewDrawableMultisample4X;
view.context = context;
view.delegate = view;
[view setupGL];
[self.view addSubview:view];
MyGLKViewSubclass.m
- (void)setupGL {
[EAGLContext setCurrentContext:self.context];
glEnable(GL_CULL_FACE);
self.effect = [[GLKBaseEffect alloc] init];
NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
GLKTextureLoaderOriginBottomLeft,
nil];
NSError * error;
NSString *path = [[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"];
GLKTextureInfo * info = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if (info == nil) {
NSLog(@"Error loading file: %@", [error localizedDescription]);
}
self.effect.texture2d0.name = info.name;
self.effect.texture2d0.enabled = true;
glGenVertexArraysOES(1, &vertexArray);
glBindVertexArrayOES(vertexArray);
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);
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition,
3,
GL_FLOAT,
GL_FALSE,
sizeof(Vertex),
(const GLvoid *) offsetof(Vertex, Position)
);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, TexCoord));
glBindVertexArrayOES(0);
rotMatrix = GLKMatrix4Identity;
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
self.contentScaleFactor = 2.0;
self.opaque = NO;
self.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
[self.effect prepareToDraw];
glBindVertexArrayOES(vertexArray);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}
這是我想給我的旋轉物體
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
CGPoint lastLoc = [touch previousLocationInView:[touch view]];
CGPoint diff = CGPointMake(lastLoc.x - location.x, lastLoc.y - location.y);
float rotX = -1 * GLKMathDegreesToRadians(diff.y/2.0);
float rotY = -1 * GLKMathDegreesToRadians(diff.x/2.0);
GLKVector3 xAxis = GLKVector3Make(1, 0, 0);
rotMatrix = GLKMatrix4Rotate(rotMatrix, rotX, xAxis.x, xAxis.y, xAxis.z);
GLKVector3 yAxis = GLKVector3Make(0, 1, 0);
rotMatrix = GLKMatrix4Rotate(rotMatrix, rotY, yAxis.x, yAxis.y, yAxis.z);
[self update];
}
謝謝你的回答。我嘗試了你的代碼,不幸的是無濟於事。當我需要重繪我的GLKView時,什麼對我有用就是使用'[self display ]'。順便說一下,我將委託設置爲GLKView本身,因此當我調用它的'display'方法時,它的委託方法將被調用。這是一個壞方法嗎? –
displayLink運行循環與GLKViewController/GLKView組合提供的運行循環相同。使用CADisplayLink,您可以查看自上次刷新以來發生的週期數的細化視圖;但是,這些信息與我認識的每個人都無關。使用GLKViewController,我可以設置給定視頻每秒的幀數;以15 fps播放的視頻不需要以60 fps渲染。能夠減少fps使得一個更快的應用程序比沒有。 –