opengl如何画出一个球_OpenGL-Controlling and Monitoring the Pipeline
全球圖形學領域教育的領先者、自研引擎的倡導者、底層技術研究領域的技術公開者,東漢書院在致力于使得更多人群具備內核級競爭力的道路上,將帶給小伙伴們更多的公開技術教學和視頻,感謝一路以來有你的支持。我們正在用實際行動來幫助小伙伴們構建一套成體系的圖形學知識架構,你在我們這里獲得的不止于那些毫無意義的代碼,我們這里更多的是代碼背后的故事,以及精準、透徹的理解。我們不會扔給人們一本書或者給個思路讓人們去自學,我們是親自來設計出好的資源,讓人們明白到底背后還有哪些細節。
這里插播一個引擎大賽的消息,感興趣的同學可以看一眼,這也是東漢書院的立項使命:
瘋狂的程序員:自研引擎大賽?zhuanlan.zhihu.com大賽官方主頁
東漢書院-自己動手寫游戲引擎?edu.battlefire.cn參賽作品列表
瘋狂的程序員:參賽作品1-NaturalEngine?zhuanlan.zhihu.com瘋狂的程序員:參賽作品2-Colble離線室內渲染器和Juziimo實時外觀渲染器?zhuanlan.zhihu.comChapter 12. Controlling and Monitoring the Pipeline
What You’ll Learn in This Chapter
- How to ask OpenGL about the progress of your commands down the graphics pipeline.
- How to measure the time taken for your commands to execute.
- How to synchronize your application with OpenGL and how to synchronize multiple OpenGL contexts with each other.
你將會在本章學到啥
- 如何詢問OpenGL你的指令在圖形管線中的執行進度
- 如何計算出你的指令花費了多少時間
- 如何處理OpenGL與你的程序的同步問題以及如何同步多個OpenGL的渲染上下文
This chapter is about the OpenGL pipeline and how it executes your commands. As your application makes OpenGL function calls, work is placed in the OpenGL pipeline and makes its way down it one stage at a time. This takes time, and you can measure that span. This allows you to tune your application’s complexity to match the performance of the graphics system and to measure and control latency, which is important for real-time applications. In this chapter, you’ll also learn how to synchronize your application’s execution to that of OpenGL commands you’ve issued and even how to synchronize multiple OpenGL contexts with each other.
本章是介紹OpenGL的管線的以及它如何執行你的指令。當你的程序調用OpenGL的API的時候,這些指令都會在OpenGL的管線里得到執行并且每次執行一個階段。這個操作是耗費時間的,你可以計算出花費了多少時間。 這可以讓你去測試你程序的復雜度然后去很好適配你的圖形系統的性能,并且掌握并處理好延遲,這對于實時應用程序來說是非常重要的。在本章中,你將同樣會學到如何去同步你程序發送給OpenGL的那些指令甚至能學到 如何去同步多個OpenGL的渲染上下文。
Queries
Queries are a mechanism to ask OpenGL what’s happening in the graphics pipeline. There’s plenty of information that OpenGL can tell you; you just need to know what to ask—and how to ask the question.
Queries是一種查詢OpenGL圖形管線里正在發生什么的一種機制。你可以查詢到很多東西,你需要做的就是知道可以查詢什么以及如何去查詢。
Remember your early days in school? The teacher wanted you to raise your hand before asking a question. This was almost like reserving your place in line for asking the question—the teacher didn’t know yet what your question was going to be, but she knew that you had something to ask. OpenGL is similar. Before we can ask a question, we have to reserve a spot so that OpenGL knows the question is coming. Questions in OpenGL are represented by query objects, and much like any other object in OpenGL, query objects must be reserved, or generated. To do this, call glGenQueries(), passing it the number of queries you want to reserve and the address of a variable (or array) where you would like the names of the query objects to be placed:
回想起在學校里的那些日子會想到啥?老師們會希望你在提問之前先舉手。你這么做就非常類似于在一個隊列里面占據一個問問題的位置,這時候老師還不知道你想問什么,但是他們知道你有東西要問。OpenGL是類似的。 在你問問題之前,你必須讓我們知道你想要問問題。OpenGL里面使用query object來表現問題的,就跟OpenGL里面的其他object一樣,query object必須實現申明然后生成。我們可以調用glGenQueries,傳入你希望 預留多少個query object以及傳入一個地址,告訴OpenGL你希望那些query object名字被寫進哪里:
void glGenQueries(GLsizei n,GLuint *ids);The function reserves some query objects for you and gives you their names so that you can refer to them later. You can generate as many query objects you need in one go:
這個函數預留了一些query object并且給出了他們的名字,你可以在后面的代碼中使用這些名字來引用這些query object。你可以一次性生成很多個query object:
GLuint one_query; GLuint ten_queries[10]; glGenQueries(1, &one_query); glGenQueries(10, ten_queries);In this example, the first call to glGenQueries() generates a single query object and returns its name in the variable one_query. The second call to glGenQueries() generates ten query objects and returns ten names in the array ten_queries. In total, 11 query objects have been created, and OpenGL has reserved 11 unique names to represent them. It is very unlikely, but still possible, that OpenGL will not be able to create a query for you; in this case it returns 0 as the name of the query. A well-written application always checks that glGenQueries() returns a non-zero value for the name of each requested query object. If there is a failure, OpenGL keeps track of the reason, and you can find that out by calling glGetError().
在本例子中,第一個對glGenQueries的調用中生成了一個query object并且把它的名字寫入了one_query這個變量里面。第二次對glGenQueries的調用生成了10個query object并且把10個名字寫入了ten_queries 變量里面。你總共生成了11個query object,OpenGL為你預留了11個獨一無二的名字來表示這些query object。當name里面的值是0的時候,就表示OpenGL不能給你創建query object。一個良好的編程習慣就是 在調用了glGenQueries之后,去檢查一下返回值是不是非0。如果這個操作失敗了,OpenGL會保留一份錯誤原因,你可以通過glGetError()來獲取到為什么失敗了。
Each query object reserves a small but measurable amount of resources from OpenGL. These resources must be returned to OpenGL because, if they are not, OpenGL may run out of space for queries and fail to generate more for the application later. To return the resources to OpenGL, call glDeleteQueries():
每一個query object會從OpenGL里面占有一份小的,但是可以度量的資源。這些資源必須最后回收給OpenGL,因為如果它們沒有被回收的話,那么OpenGL可能會沒有內存了并且無法為應用程序創建更多的queries。 我們可以調用glDeleteQueries來把query object的資源回收給OpenGL。
void glDeleteQueries(GLsizei n,const GLuint *ids);This works similarly to glGenQueries()—it takes the number of query objects to delete and the address of a variable or array holding their names:
這個跟glGenQueries類似,它會接受兩個參數,第一個是你想回收多少query object,第二個參數就是具體的這些你想回收的query object的存放位置。
glDeleteQueries(10, ten_queries); glDeleteQueries(1, &one_query);After the queries are deleted, they are essentially gone for good. The names of the queries can’t be used again unless they are given back to you by another call to glGenQueries().
當你的queries被刪除了之后,他們基本上都是沒問題的,這些名字是不會被重用的,直到你下次調用glGenQueries的時候再次獲得它們。
我們核心關注和討論的領域是引擎的底層技術以及商業化方面的信息,可能并不適合初級入門的同學。另外官方維護兩個公眾號,第一個公眾號是關于我們企業自身產品的信息與動態的公眾號,如果對我們自身信息與動態感興趣的同學,可以關注圖形之心。
除此之外,我們為了更頻繁的發布一些咨詢與文章,我們維護的第二個公眾號是“內核觀察”,內核觀察提供的主要是一些與我們無關的咨詢與文章。
只言片語,無法描繪出整套圖形學領域的方方面面,只有成體系的知識結構,才能夠充分理解和掌握一門科學,這是藝術。我們已經為你準備好各式各樣的內容了,東漢書院,等你來玩。
總結
以上是生活随笔為你收集整理的opengl如何画出一个球_OpenGL-Controlling and Monitoring the Pipeline的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: datatable使用_使用Stream
- 下一篇: 房地产备案是什么意思住建局(房地产备案是