溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Chapter 3. Following the Pipeline(模仿工廠的流水線)

發(fā)布時(shí)間:2020-07-25 17:05:02 來(lái)源:網(wǎng)絡(luò) 閱讀:249 作者:萌谷王 欄目:游戲開(kāi)發(fā)

周一到周五,每天一篇,北京時(shí)間早上7點(diǎn)準(zhǔn)時(shí)更新~,中英文對(duì)照,一邊學(xué)編程一邊彈吉他,做一個(gè)奇葩碼農(nóng)!

請(qǐng)不要懷疑翻譯是否有問(wèn)題,我們的翻譯工程師是藍(lán)翔畢業(yè)的呢!

What You’ll Learn in This Chapter(你將會(huì)學(xué)到啥)

What each of the stages in the OpenGL pipeline does(渲染管線的每個(gè)階段都干了什么)
How to connect your shaders to the fixed-function pipeline stages(如何把你的shader與硬件中那些固定的功能進(jìn)行拼接,湊成完整的渲染管線)
How to create a program that uses every stage of the graphics pipeline simultaneously(如何創(chuàng)建那些可以在每個(gè)渲染階段都并行的GPU程序)
In this chapter, we will walk all the way along the OpenGL pipeline from start to finish(在本章中,我們將把整個(gè)OpenGL的渲染管線走一遍), providing insight into each of the stages(深入的了解每個(gè)步驟), which include fixed-function blocks and(這就包含了固定功能部分和可編程的shader部分) programmable shader blocks. You have already read a whirlwind introduction to the vertex and fragment shader stages(你已經(jīng)快速的看過(guò)了vertex和fragment shader的處理階段的介紹了). However, the application that you constructed simply drew a single triangle at a fixed position(然而,你構(gòu)建的那個(gè)程序只是在固定的地方畫了一個(gè)三角形而已). If we want to render anything interesting with OpenGL(如果我們想使用OpenGL渲染點(diǎn)有趣的東西的話), we’re going to have to learn a lot more about the pipeline and all of the things you can do with it(我們需要去學(xué)習(xí)關(guān)于渲染管線的知識(shí)以及你可以用它來(lái)干些啥). This chapter introduces every part of the pipeline(本章節(jié)介紹渲染管線的每一個(gè)部分), hooks them up to one another, and provides an example shader for each stage(把他們一個(gè)一個(gè)串起來(lái),并且提供每個(gè)階段的樣本shader代碼)

Passing Data to the Vertex Shader(給vertex shader傳遞數(shù)據(jù))

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階段是第一個(gè)OpenGL渲染管線中的處理階段,而且它必須存在,否則后面都沒(méi)得玩了). However, before the vertex shader runs, a fixed-function stage known as vertex fetching, or sometimes vertex pulling, is run(在vertex shader執(zhí)行之前,會(huì)執(zhí)行一個(gè)叫做vertex fetching或者叫做vertex pulling的操作). This automatically provides inputs to the vertex shader.(這個(gè)操作自動(dòng)的把輸入數(shù)據(jù)輸送給vertex shader)

Vertex Attributes(頂點(diǎn)屬性,實(shí)際上有些名詞你可以不用翻譯,老外怎么叫你就怎么叫就得了,翻譯出來(lái)還別扭,就像這個(gè)單詞,純屬直譯)

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中獲取數(shù)據(jù)的方法就是定義全局變量,并用in或者out標(biāo)識(shí)符去修飾). You were briefly introduced to the out qualifier in Chapter 2(你已經(jīng)在第二章中見(jiàn)識(shí)到了out修飾符了), “Our First OpenGL Program,” when Listing 2.4 used it to output a color from the fragment shader(在Listing2.4中,它被用來(lái)指出從fragment shader中輸出一個(gè)顏色數(shù)據(jù)). At the start of the OpenGL pipeline, we use the in keyword to bring inputs into the vertex shader(在OpenGL渲染管線的開(kāi)始階段、我們使用in這個(gè)關(guān)鍵字來(lái)給vertex shader弄點(diǎn)輸入數(shù)據(jù)). Between stages, in and out can be used to form conduits from shader to shader and pass data between them(在渲染管線的各個(gè)階段之間,in和out可以形成一個(gè)用于數(shù)據(jù)傳輸?shù)墓艿?. 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(現(xiàn)在,我們先來(lái)看看vertex shader的輸入數(shù)據(jù)以及當(dāng)你用in去修飾一個(gè)變量時(shí),到底會(huì)發(fā)生什么). This marks the variable as an input to the vertex shader(這個(gè)in就標(biāo)記了這個(gè)變量時(shí)vertex shader的輸入數(shù)據(jù)), which means that it is essentially an input to the OpenGL graphics pipeline(也就是說(shuō),它是一個(gè)基本的OpenGL渲染管線的輸入數(shù)據(jù)). It is automatically filled in by the fixed-function vertex fetch stage(這個(gè)變量會(huì)在vertex fetch階段自動(dòng)被賦值). The variable becomes known as a vertex attribute(這個(gè)變量通常被稱為頂點(diǎn)屬性)

Vertex attributes are how vertex data is introduced into the OpenGL pipeline(頂點(diǎn)屬性們定義了數(shù)據(jù)如何被傳送給OpenGL渲染管線). To declare a vertex attribute, you declare a variable in the vertex shader using the in storage qualifier(你只需要使用in修飾符去修飾一個(gè)vertex shader中的變量,就申明了一個(gè)頂點(diǎn)屬性). An example of this is shown in Listing 3.1, where we declare thevariable offset as an input attribute(在Listing3.1中,我們申明了變量offset作為一個(gè)輸入的屬性)

#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加了一個(gè)輸入變量叫offset). As it is an input to the first shader in the pipeline, it will be filled automatically by the vertex fetch stage(它作為渲染管線的第一個(gè)階段,這些輸入的數(shù)據(jù)會(huì)被vertex fetch的過(guò)程初始化). 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告訴這個(gè)階段,如何去給這些變量賦值). 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是一個(gè)指針,指向輸入的數(shù)據(jù)). You may have noticed the layout (location = 0) code in the declaration of the offset attribute(你可能已經(jīng)注意到了location=0這句代碼了). This is a layout qualifier, which we have used to set the location of the vertex attribute to zero(這是一個(gè)layout的修飾符,這里我們執(zhí)行了頂點(diǎn)屬性的位置為0). This location is the value we’ll pass in index to refer to the attribute(這里的位置就是指我們將會(huì)傳遞的那個(gè)index參數(shù))

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(每一次我們調(diào)用glVertexAttrib系列函數(shù),它就會(huì)刷新我們傳遞給vertex shader的那些變量). We can use this approach to animate our one triangle(我們可以通過(guò)這種方式,讓我們的三角形動(dòng)起來(lái)). Listing 3.2 shows an updated version of our rendering function that updates the value of offset in each frame(Listing3.2展示了我們新版本的代碼,新版本的代碼會(huì)每一幀都去更新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(當(dāng)我們執(zhí)行本程序的時(shí)候,我們的三角形就會(huì)沿著一個(gè)橢圓做運(yùn)動(dòng)了)

本日的翻譯就到這里,明天見(jiàn),拜拜~~

第一時(shí)間獲取最新橋段,請(qǐng)關(guān)注東漢書院以及圖形之心公眾號(hào)

東漢書院,等你來(lái)玩哦

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI