OpenGL 渲染上下文-context


context理解  

  OpenGL Context,中文解釋就是OpenGL的上下文。OpenGL只是圖形API,它沒有窗口的支持,我們一般使用glut或glfw來創建窗口,然后在這個窗口中繪制。所以上下文的意思也就是OpenGL的作用范圍,在這里可以先簡單的理解為就是這個窗口。也就是說,如果我們要使用OpenGL,需要先為它創建一個窗口。當然OpenGL的Context不只是這個窗口,這個窗口我們可以理解為OpenGL的default framebuffer,所以Context還包含關於這個framebuffer的一些參數設置信息,具體內容可以查看OpenGL的Context的結構體。

  所以,OpenGL的Context記錄了OpenGL渲染需要的所有信息,它是一個大的結構體,它里面記錄了當前繪制使用的顏色、是否有光照計算以及開啟的光源等非常多我們使用OpenGL函數調用設置的狀態和狀態屬性。在OpenGL 3.0版本之前,OpenGL創建Context都是一致的,隨着升級會新增一些內容(例如從OpenGL1.1升級到1.5,會新增一些狀態變量或者屬性,並添加一些設置這些內容的函數),整體上來說沒有什么大的變化。但是從OpenGL 3.0開始,OpenGL為了擺脫歷史的“包袱”,想要徹底的廢棄掉之前的許多特性,但是無奈市面上已經有大量依賴OpenGL之前版本的代碼,導致OpenGL維護小組的這一想法難以付諸實施,於是在OpenGL 3.1開始引入了OpenGL Context的一些分類,比如引入了CoreProfile等概念,之后隨着版本發展到3.3,一切算是確定下來。

  簡單的來說,到了OpenGL3.3之后,OpenGL的context profile分為了兩個版本,core pfofile和compatibility profile,前者表示刪除任何標記為deprecated(棄用)的功能,后者則表示不刪除任何功能。context除了core profile(核心渲染模式)和compatibility profile(立即渲染模式)外,還有一種模式:foward compatibility,這個表示所有標記為deprecated的函數都禁用,這個模式只對opengl3.0及以上的版本有效。但這個選項對OpenGL 3.2+ compatibility Profile Context沒有任何作用。

Forward compatibility

 

A context, of version 3.0 or greater, can be created with the "forward compatibility" bit set. This will cause, for the given profile, all functionality marked "deprecated" to be removed. You can combine the forward compatibility bit with core and compatibility contexts.

 

For 3.0, this means that all deprecated functionality will no longer be available. This simulates the 3.1 experience.

 

For 3.1, this means that any remaining deprecated functionality (things deprecated in 3.0 but not removed in 3.1) will be removed. Basically, wide-lines. Also, you're not likely to see implementations offer ARB_compatibility if you pass forward compatibility.

 

For 3.2+ compatibility, it should mean nothing at all. Since no functionality is marked deprecated in the compatibility profile, the forward compatibility bit removes nothing.

 

For 3.2+ core, it again means that all functionality that is still deprecated (wide-lines) will be removed.

 

Recommendation: You should use the forward compatibility bit only if you need compatibility with MacOS. That API requires the forward compatibility bit to create any core profile context.

 

context指定方式

  在學習OpenGL的圖形界面的框架中我們一般使用glut和glfw,glut有兩個版本,分別是原始的glut和freeglut,其中glut是由xxx編寫,不過現在已經不維護了,而freeglut,由Mark Kilgard編寫維護,2015年更新到3.0版本,基本也處於停滯更新的狀態;glfw則是另一個輕量級的圖形界面框架,托管在 www.glfw.org,現在非常的活躍,如果新入門可以優先考慮glfw,相關的教程網上也比較多。

  注意在這兩個圖形界面框架中,對於forward compatibility都是按一個選項來配置的,也就是說forward compatibility針對的是標記為deprecated的功能,而不是profile。

  在glut指定context的方式:

glutInitContextVersion(3,3);
glutInitContextProfile(GLUT_CORE_PROFILE);
//glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);  //設置forward compatibility

 

  glfw的context指定方式位:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);  //設置forward compatibility

  其中最后一行在macOS中必須指定。

線程私有

  OpenGL的繪制命令都是作用在當前的Context上,這個Current Context是一個線程私有(thread-local)的變量,也就是說如果我們在線程中繪制,那么需要為每個線程制定一個Current Context的,而且多個線程不能同時指定同一個Context為Current Context。

  

參考資料

https://www.khronos.org/opengl/wiki/OpenGL_Context

https://blog.csdn.net/csxiaoshui/article/details/79032464


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM