1. 程式人生 > >OpenGL 渲染上下文-context

OpenGL 渲染上下文-context

rec wiki lfw RKE exp bili can 函數 mean

context理解  

  OpenGL在渲染的時候需要一個Context,這個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中必須指定。

  

參考資料

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

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

OpenGL 渲染上下文-context