opengl如何画出一个球_OpenGL-Controlling and Monitoring the Pipeline
全球圖形學(xué)領(lǐng)域教育的領(lǐng)先者、自研引擎的倡導(dǎo)者、底層技術(shù)研究領(lǐng)域的技術(shù)公開者,東漢書院在致力于使得更多人群具備內(nèi)核級競爭力的道路上,將帶給小伙伴們更多的公開技術(shù)教學(xué)和視頻,感謝一路以來有你的支持。我們正在用實(shí)際行動來幫助小伙伴們構(gòu)建一套成體系的圖形學(xué)知識架構(gòu),你在我們這里獲得的不止于那些毫無意義的代碼,我們這里更多的是代碼背后的故事,以及精準(zhǔn)、透徹的理解。我們不會扔給人們一本書或者給個思路讓人們?nèi)プ詫W(xué),我們是親自來設(shè)計(jì)出好的資源,讓人們明白到底背后還有哪些細(xì)節(jié)。
這里插播一個引擎大賽的消息,感興趣的同學(xué)可以看一眼,這也是東漢書院的立項(xiàng)使命:
瘋狂的程序員:自研引擎大賽?zhuanlan.zhihu.com大賽官方主頁
東漢書院-自己動手寫游戲引擎?edu.battlefire.cn參賽作品列表
瘋狂的程序員:參賽作品1-NaturalEngine?zhuanlan.zhihu.com瘋狂的程序員:參賽作品2-Colble離線室內(nèi)渲染器和Juziimo實(shí)時外觀渲染器?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.
你將會在本章學(xué)到啥
- 如何詢問OpenGL你的指令在圖形管線中的執(zhí)行進(jìn)度
- 如何計(jì)算出你的指令花費(fèi)了多少時間
- 如何處理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的管線的以及它如何執(zhí)行你的指令。當(dāng)你的程序調(diào)用OpenGL的API的時候,這些指令都會在OpenGL的管線里得到執(zhí)行并且每次執(zhí)行一個階段。這個操作是耗費(fèi)時間的,你可以計(jì)算出花費(fèi)了多少時間。 這可以讓你去測試你程序的復(fù)雜度然后去很好適配你的圖形系統(tǒng)的性能,并且掌握并處理好延遲,這對于實(shí)時應(yīng)用程序來說是非常重要的。在本章中,你將同樣會學(xué)到如何去同步你程序發(fā)送給OpenGL的那些指令甚至能學(xué)到 如何去同步多個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圖形管線里正在發(fā)生什么的一種機(jī)制。你可以查詢到很多東西,你需要做的就是知道可以查詢什么以及如何去查詢。
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:
回想起在學(xué)校里的那些日子會想到啥?老師們會希望你在提問之前先舉手。你這么做就非常類似于在一個隊(duì)列里面占據(jù)一個問問題的位置,這時候老師還不知道你想問什么,但是他們知道你有東西要問。OpenGL是類似的。 在你問問題之前,你必須讓我們知道你想要問問題。OpenGL里面使用query object來表現(xiàn)問題的,就跟OpenGL里面的其他object一樣,query object必須實(shí)現(xiàn)申明然后生成。我們可以調(diào)用glGenQueries,傳入你希望 預(yù)留多少個query object以及傳入一個地址,告訴OpenGL你希望那些query object名字被寫進(jìn)哪里:
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:
這個函數(shù)預(yù)留了一些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的調(diào)用中生成了一個query object并且把它的名字寫入了one_query這個變量里面。第二次對glGenQueries的調(diào)用生成了10個query object并且把10個名字寫入了ten_queries 變量里面。你總共生成了11個query object,OpenGL為你預(yù)留了11個獨(dú)一無二的名字來表示這些query object。當(dāng)name里面的值是0的時候,就表示OpenGL不能給你創(chuàng)建query object。一個良好的編程習(xí)慣就是 在調(diào)用了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,因?yàn)槿绻鼈儧]有被回收的話,那么OpenGL可能會沒有內(nèi)存了并且無法為應(yīng)用程序創(chuàng)建更多的queries。 我們可以調(diào)用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類似,它會接受兩個參數(shù),第一個是你想回收多少query object,第二個參數(shù)就是具體的這些你想回收的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().
當(dāng)你的queries被刪除了之后,他們基本上都是沒問題的,這些名字是不會被重用的,直到你下次調(diào)用glGenQueries的時候再次獲得它們。
我們核心關(guān)注和討論的領(lǐng)域是引擎的底層技術(shù)以及商業(yè)化方面的信息,可能并不適合初級入門的同學(xué)。另外官方維護(hù)兩個公眾號,第一個公眾號是關(guān)于我們企業(yè)自身產(chǎn)品的信息與動態(tài)的公眾號,如果對我們自身信息與動態(tài)感興趣的同學(xué),可以關(guān)注圖形之心。
除此之外,我們?yōu)榱烁l繁的發(fā)布一些咨詢與文章,我們維護(hù)的第二個公眾號是“內(nèi)核觀察”,內(nèi)核觀察提供的主要是一些與我們無關(guān)的咨詢與文章。
只言片語,無法描繪出整套圖形學(xué)領(lǐng)域的方方面面,只有成體系的知識結(jié)構(gòu),才能夠充分理解和掌握一門科學(xué),這是藝術(shù)。我們已經(jīng)為你準(zhǔn)備好各式各樣的內(nèi)容了,東漢書院,等你來玩。
總結(jié)
以上是生活随笔為你收集整理的opengl如何画出一个球_OpenGL-Controlling and Monitoring the Pipeline的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: datatable使用_使用Stream
- 下一篇: 房地产备案是什么意思住建局(房地产备案是