我正在ios 5上开发一个opengl es 2.0应用程序。我将纹理绘制为背景。当我旋转设备时,我需要旋转图层并更改图层的尺寸。 问题是我可以改变图层的边界,但图层分辨率是相同的。 我的意思是,如果我有一个边界为1024 * 768的图层并且我将新图层边界定义为768 * 1024,则图层大小会发生变化,但是当我将768 * 1024纹理绘制为背景时,图像会变形。
我使用函数canvasView.bounds = newCanvasRect;
和canvasView.frame = newCanvasRect;
来更改图层大小(其中canvasView是图层,newCanvasRect是新边界)。
有什么想法吗?
答案 0 :(得分:1)
每次设备旋转新的高度和宽度时,您必须销毁并重建帧缓冲区。使用layoutsubviews来触发所有这些,当方向改变时调用它:
- (void)layoutSubviews {
NSLog(@"layoutSubviews");
[EAGLContext setCurrentContext:_context];
[self destroyFramebuffer];
[self createFramebuffer];
}
- (void)destroyFramebuffer {
NSLog(@"destroy buffer");
glDeleteFramebuffers(1, &viewFramebuffer);
viewFramebuffer = 0;
glDeleteRenderbuffers(1, &viewRenderbuffer);
viewRenderbuffer = 0;
glDeleteRenderbuffers(1, &depthRenderbuffer);
depthRenderbuffer = 0;
}
- (BOOL)createFramebuffer {
NSLog(@"creating buffer");
glGenFramebuffers(1, &viewFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
glGenRenderbuffers(1, &viewRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
// setup the renderbuffer to the context
[[EAGLContext currentContext] renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, viewRenderbuffer);
// Allocate color buffer backing based on the current layer size (get the current layer size)
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
NSLog(@"backing width: %d height: %d",backingWidth, backingHeight);
// set up the depth buffer
glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, backingWidth, backingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
[self setupView];
return YES;
}
然后使用backingWidth和backingHeight重新创建投影矩阵。