![](/img/trans.png)
[英]Best way to flip a CAEAGLLayer for presentation in OpenGL ES 2
[英]OpenGL drawing messed up when I resize CAEAGLLayer
我試圖在我的應用程序中實現一些基本的OpenGL,並且這樣做,我使用了SDK附帶的GLPaint示例作為起點。 我的代碼本身可以正常工作,但是一旦將所有內容移到我的應用程序中,同一代碼就會失敗。
根據我的嘗試,問題可能是(1)原始圖元在初次繪制時顯得很變形,然后在隨后的原始圖元被繪制時浮動到屏幕頂部,或者(2)這些原始圖元根本不出現。
我將其范圍縮小到我的應用程序要求調整CAEAGLLayer大小的事實。 它位於UIScrollView中,可以隨時移動和調整大小(收縮,拖動等)。如GLPaint示例中所示,我每次查看視圖調整大小時(通過layoutSubviews),都會吹掉幀緩沖區並重新創建它,但這並不工作。 (由於GLPaint示例從不調整大小,因此我懷疑示例代碼的這一部分自從不需要使用以來就從未起作用。)
僅當我將CAEAGLLayer設置為1024 x 768的恆定大小時,代碼才能按預期工作:
self.layer.frame = CGRectMake(0, 0, 1024, 768);
一旦我將該行更改為:
self.layer.frame = self.bounds;
問題又回來了。 我其余的layoutSubviews方法看起來像這樣:
[EAGLContext setCurrentContext:context];
[self destroyFramebuffer];
[self createFramebuffer];
if (needsClearView) {
[self clearView];
needsClearView = NO;
}
與GLPaint示例相比,destroyFramebuffer和createFramebuffer方法基本不變。 引用視圖大小的唯一其他代碼是initWithCoder中的以下行:
glOrthof(0, self.bounds.size.width, 0, self.bounds.size.height, -1, 1);
glViewport(0, 0, self.bounds.size.width, self.bounds.size.height);
我嘗試過在此處更改值,並將這些行移動到其他位置,但是我嘗試過的任何方法都沒有用。 有人對我有什么建議嗎?
前幾天,我遇到了同樣的問題,並且弄清楚了。 在重新創建幀緩沖區之前,您確實需要使用新大小調用glOrthof
和glViewport
。 我在layoutSubviews
調用中做到了:
-(void)layoutSubviews
{
[EAGLContext setCurrentContext:context];
glMatrixMode(GL_PROJECTION);
CGRect frame = self.bounds;
CGFloat scaleFactor = self.contentScaleFactor;
glLoadIdentity();
glOrthof(0, frame.size.width * scaleFactor, 0, frame.size.height * scaleFactor, -1, 1);
glViewport(0, 0, frame.size.width * scaleFactor, frame.size.height * scaleFactor);
glMatrixMode(GL_MODELVIEW);
[self destroyFramebuffer];
[self createFramebuffer];
// Clear the framebuffer the first time it is allocated
if (needsErase) {
[self erase];
needsErase = NO;
}
}
確保首先調用glLoadIdentity
,因為glOrthof
與當前矩陣相乘。
在VC中使用PaintingView並更改其框架時,我遇到了同樣的問題。 通過在更改框架之前在視圖上應用CGAffineTransform來解決此問題。
即:
paintingView.transform = CGAffineTransformMakeScale(0.5, 0.5);
paintingView.frame = CGRectMake(paintingView.frame.origin.x, paintingView.frame.origin.y, paintingView.frame.size.width/2, paintingView.frame.size.height/2);
我發現CAEAGLLayer不滿意,除非它的bounds.width
是8的倍數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.