我正在使用2D紋理創建動畫萬花筒的應用程序。它開始顯示給NSOpenGLView。我添加了一個選項來切換到全屏模式。如何在Mac OS中共享NSOpenGLView和全屏上下文之間的紋理?
我寧願使用OS 10.5的方法來全屏,所以我可以支持10.5,但我想我會面臨同樣的問題,如果我使用的方法在「創建一個全屏應用程序Mac OS X v10.6「。
NSOpenGLContext init方法initWithFormat:shareContext:將一個可選指針指向「另一個OpenGL圖形上下文,其紋理名稱空間和顯示列表要與接收器共享」。
的代碼我使用創建全屏OpenGL上下文很簡單:
NSOpenGLPixelFormatAttribute myAttributes[] = // Pixel Format Attributes for the FullScreen NSOpenGLContext
{
NSOpenGLPFAFullScreen, // specify full-screen OpenGL context
NSOpenGLPFAScreenMask,
CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay), // specify main display
NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) 32,
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute) 0,
NSOpenGLPFAStencilSize, 0,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
0
};
NSOpenGLPixelFormat *myPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:myAttributes]; // Create FullScreen NSOpenGLContext with these attributes
NSOpenGLContext *fullScreenContext = [[NSOpenGLContext alloc] initWithFormat:myPixelFormat shareContext: myContext]; // Create an NSOpenGLContext with the FullScreen pixel format.
然而,它似乎並沒有工作。
我的應用程序只使用一個紋理,所以我開始不使用glGenTextures和glBindTexture。相反,我只是使用單個當前紋理作爲上下文。
我試着改變我的代碼來創建一個帶有glGenTextures的命名紋理,希望我可以在切換到全屏上下文但沒有骰子後綁定命名的紋理。兩種方法都會得到相同的結果 - 紋理在全屏幕上下文中不起作用。
我沒有得到任何錯誤,但使用紋理繪圖不會做任何事情。
我每次切換到全屏模式時都被迫重新創建紋理。這很糟糕,因爲紋理可能非常大(取決於用戶加載的圖像的大小,直至最大紋理大小)。這迫使我將兩個非常大的對象副本加載到VRAM中。更糟糕的是,在某些情況下,我必須進行字節調整才能使字節順序正確。這需要花費幾秒鐘的時間才能進入全屏模式,這會導致「懷孕暫停」。
是否有一些技巧我錯過了在NSOpenGLView和全屏OpenGL上下文之間共享紋理?
理論上你應該只需要爲每個上下文創建一次紋理(只要你每次都不刪除/重新創建一個上下文)。您還可以測試GLuint是否使用glIsTexture引用了有效的紋理 – vmpstr 2011-02-28 15:00:14