您好,登錄后才能下訂單哦!
周一到周五,每天一篇,北京時間早上7點準時更新~,中英文對照,一邊學編程一邊彈吉他,做一個奇葩碼農!
請不要懷疑翻譯是否有問題,我們的翻譯工程師是藍翔畢業的呢!
What You’ll Learn in This Chapter(你將會學到啥)
What each of the stages in the OpenGL pipeline does(渲染管線的每個階段都干了什么)
How to connect your shaders to the fixed-function pipeline stages(如何把你的shader與硬件中那些固定的功能進行拼接,湊成完整的渲染管線)
How to create a program that uses every stage of the graphics pipeline simultaneously(如何創建那些可以在每個渲染階段都并行的GPU程序)
In this chapter, we will walk all the way along the OpenGL pipeline from start to finish(在本章中,我們將把整個OpenGL的渲染管線走一遍), providing insight into each of the stages(深入的了解每個步驟), which include fixed-function blocks and(這就包含了固定功能部分和可編程的shader部分) programmable shader blocks. You have already read a whirlwind introduction to the vertex and fragment shader stages(你已經快速的看過了vertex和fragment shader的處理階段的介紹了). However, the application that you constructed simply drew a single triangle at a fixed position(然而,你構建的那個程序只是在固定的地方畫了一個三角形而已). If we want to render anything interesting with OpenGL(如果我們想使用OpenGL渲染點有趣的東西的話), we’re going to have to learn a lot more about the pipeline and all of the things you can do with it(我們需要去學習關于渲染管線的知識以及你可以用它來干些啥). This chapter introduces every part of the pipeline(本章節介紹渲染管線的每一個部分), hooks them up to one another, and provides an example shader for each stage(把他們一個一個串起來,并且提供每個階段的樣本shader代碼)
Passing Data to the Vertex Shader(給vertex shader傳遞數據)
The vertex shader is the first programmable stage in the OpenGL pipeline and has the distinction of being the only mandatory stage in the graphics pipeline(vertex shader階段是第一個OpenGL渲染管線中的處理階段,而且它必須存在,否則后面都沒得玩了). However, before the vertex shader runs, a fixed-function stage known as vertex fetching, or sometimes vertex pulling, is run(在vertex shader執行之前,會執行一個叫做vertex fetching或者叫做vertex pulling的操作). This automatically provides inputs to the vertex shader.(這個操作自動的把輸入數據輸送給vertex shader)
Vertex Attributes(頂點屬性,實際上有些名詞你可以不用翻譯,老外怎么叫你就怎么叫就得了,翻譯出來還別扭,就像這個單詞,純屬直譯)
In GLSL, the mechanism for getting data in and out of shaders is to declare global variables with the in and out storage qualifiers(在GLSL中,往shader中傳入或者從shader中獲取數據的方法就是定義全局變量,并用in或者out標識符去修飾). You were briefly introduced to the out qualifier in Chapter 2(你已經在第二章中見識到了out修飾符了), “Our First OpenGL Program,” when Listing 2.4 used it to output a color from the fragment shader(在Listing2.4中,它被用來指出從fragment shader中輸出一個顏色數據). At the start of the OpenGL pipeline, we use the in keyword to bring inputs into the vertex shader(在OpenGL渲染管線的開始階段、我們使用in這個關鍵字來給vertex shader弄點輸入數據). Between stages, in and out can be used to form conduits from shader to shader and pass data between them(在渲染管線的各個階段之間,in和out可以形成一個用于數據傳輸的管道). We’ll get to that shortly(我們將很快的接觸到那一款). For now, consider the input to the vertex shader and what happens if you declare a variable with an in storage qualifier(現在,我們先來看看vertex shader的輸入數據以及當你用in去修飾一個變量時,到底會發生什么). This marks the variable as an input to the vertex shader(這個in就標記了這個變量時vertex shader的輸入數據), which means that it is essentially an input to the OpenGL graphics pipeline(也就是說,它是一個基本的OpenGL渲染管線的輸入數據). It is automatically filled in by the fixed-function vertex fetch stage(這個變量會在vertex fetch階段自動被賦值). The variable becomes known as a vertex attribute(這個變量通常被稱為頂點屬性)
Vertex attributes are how vertex data is introduced into the OpenGL pipeline(頂點屬性們定義了數據如何被傳送給OpenGL渲染管線). To declare a vertex attribute, you declare a variable in the vertex shader using the in storage qualifier(你只需要使用in修飾符去修飾一個vertex shader中的變量,就申明了一個頂點屬性). An example of this is shown in Listing 3.1, where we declare thevariable offset as an input attribute(在Listing3.1中,我們申明了變量offset作為一個輸入的屬性)
#version 450 core
// 'offset' is an input vertex attribute
layout (location = 0) in vec4 offset;
void main(void)
{
const vec4 vertices[3] = vec4[3](vec4(0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4(0.25, 0.25, 0.5, 1.0));
// Add 'offset' to our hard-coded vertex position
gl_Position = vertices[gl_VertexID] + offset;
}
Listing 3.1: Declaration of a vertex attribute
In Listing 3.1, we have added the variable offset as an input to the vertex shader(在Listing3.1中我們給vertex shader加了一個輸入變量叫offset). As it is an input to the first shader in the pipeline, it will be filled automatically by the vertex fetch stage(它作為渲染管線的第一個階段,這些輸入的數據會被vertex fetch的過程初始化). We can tell this stage what to fill the variable with by using one of the many variants of the vertex attribute functions, glVertexAttrib()(我們可以使用glVertexAttrib系列的API告訴這個階段,如何去給這些變量賦值). The prototype for glVertexAttrib4fv(), which we use in this example, is(在我們的案例中,我們使用的API的定義是:)
void glVertexAttrib4fv(GLuint index, const GLfloat * v);
Here, the parameter index is used to reference the attribute (這里,index被用于指定屬性的索引)and v is a pointer to the new data to put into the attribute(v是一個指針,指向輸入的數據). You may have noticed the layout (location = 0) code in the declaration of the offset attribute(你可能已經注意到了location=0這句代碼了). This is a layout qualifier, which we have used to set the location of the vertex attribute to zero(這是一個layout的修飾符,這里我們執行了頂點屬性的位置為0). This location is the value we’ll pass in index to refer to the attribute(這里的位置就是指我們將會傳遞的那個index參數)
Each time we call one of the glVertexAttrib() functions (of which there are many), it will update the value of the vertex attribute that is passed to the vertex shader(每一次我們調用glVertexAttrib系列函數,它就會刷新我們傳遞給vertex shader的那些變量). We can use this approach to animate our one triangle(我們可以通過這種方式,讓我們的三角形動起來). Listing 3.2 shows an updated version of our rendering function that updates the value of offset in each frame(Listing3.2展示了我們新版本的代碼,新版本的代碼會每一幀都去更新offset變量)
// Our rendering function
virtual void render(double currentTime)
{
const GLfloat color[] = {
(float)sin(currentTime) 0.5f + 0.5f,
(float)cos(currentTime) 0.5f + 0.5f,
0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, color);
// Use the program object we created earlier for rendering
glUseProgram(rendering_program);
GLfloat attrib[] = { (float)sin(currentTime) 0.5f,
(float)cos(currentTime) 0.6f,
0.0f, 0.0f };
// Update the value of input attribute 0
glVertexAttrib4fv(0, attrib);
// Draw one triangle
glDrawArrays(GL_TRIANGLES, 0, 3);
}
Listing 3.2: Updating a vertex attribute
When we run the program with the rendering function of Listing 3.2, the triangle will move in a smooth oval shape around the window(當我們執行本程序的時候,我們的三角形就會沿著一個橢圓做運動了)
本日的翻譯就到這里,明天見,拜拜~~
第一時間獲取最新橋段,請關注東漢書院以及圖形之心公眾號
東漢書院,等你來玩哦
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。