欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

UsingMultipleVertexShaderInputs-創(chuàng)新互聯(lián)

周一到周五,每天一篇,北京時(shí)間早上7點(diǎn)準(zhǔn)時(shí)更新~

多倫網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)自2013年創(chuàng)立以來到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

As you have learned, you can get OpenGL to feed data into your vertex shaders and use data you’ve placed in buffer objects. You can also declare multiple inputs to your vertex shaders, and assign each one a unique location that can be used to refer to it. Combining these things together means that you can get OpenGL to provide data to multiple vertex shader inputs simultaneously. Consider the input declarations to a vertex shader shown in Listing 5.6.

你已經(jīng)學(xué)會(huì)了使用使用緩沖區(qū)為shader輸入數(shù)據(jù),你也可以為你的vertex shader定義多個(gè)輸入的屬性。結(jié)合這些所學(xué)的東西,就意味著你可以同時(shí)給shader輸入多組數(shù)據(jù)。 讓我們來看看清單5.6的代碼

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
Listing 5.6: Declaring two inputs to a vertex shader

清單5.6申明了vertex shader的兩個(gè)輸入數(shù)據(jù)

If you have a linked program object whose vertex shader has multiple inputs, you can determine the locations of those inputs by calling

如果你生成了一個(gè)有多組輸入數(shù)據(jù)的GPU程序,那么你可以通過下面的API來獲取某一個(gè)輸入數(shù)據(jù)的綁定位置。

GLint glGetAttribLocation(GLuint program,const GLchar * name);
Here, program is the name of the program object containing the vertex shader and name is the name of the vertex attribute. In our example declarations of Listing 5.6, passing "position" to glGetAttribLocation() will cause it to return 0, and passing "color" will cause it to return 1. Passing something that is not the name of a vertex shader input will cause glGetAttribLocation() to return ?1. Of course, if you always specify locations for your vertex attributes in your shader code, then glGetAttribLocation() should return whatever you specified. If you don’t specify locations in shader code, OpenGL will assign locations for you, and those locations will be returned by glGetAttribLocation(). There are two ways to connect vertex shader inputs to your application’s data, referred to as separate attributes and interleaved attributes. When attributes are separate, they are located either in different buffers or at least at different locations in the same buffer. For example, if you want to feed data into two vertex attributes, you could create two buffer objects, bind each to a different vertex buffer binding with a call to glVertexArrayVertexBuffer(), and then specify the two indices of the two vertex buffer binding points that you used when you call glVertexArrayAttribBinding() for each. Alternatively, you could place the data at different offsets within the same buffer, bind it to a single vertex buffer binding with one call to glVertexArrayVertexBuffer(), and then call glVertexArrayAttribBinding() for both attributes, passing the same binding index to each. Listing 5.7 shows this approach.

第一個(gè)參數(shù)是GPU程序,第二個(gè)參數(shù)是頂點(diǎn)屬性的名字。在我們清單5.6的代碼中,名字這個(gè)參數(shù)如果傳position,那么該API會(huì)返回0,如果傳color,那么該API會(huì)返回1. 如果你傳了一個(gè)不存在的名字,那么該API返回-1.當(dāng)然,如果你在shader代碼中去定義了屬性的綁定位置,那么你總會(huì)拿到跟你設(shè)置的那些位置一致的返回值。如果你不去在shader里 設(shè)置屬性的綁定位置,那么OpenGL將會(huì)為你分配這些位置,你通過這個(gè)API就可以獲取到那些位置。有兩種方法可以讓你給shader中的屬性傳輸數(shù)據(jù),一種是separate,另一種叫interleaved。 當(dāng)使用separate方式傳輸數(shù)據(jù)的時(shí)候,你的數(shù)據(jù)會(huì)存放在不同的緩沖區(qū)對(duì)象里,或者存儲(chǔ)在同一個(gè)緩沖區(qū)對(duì)象里的不同位置。比如你希望給shader中的兩個(gè)屬性傳輸數(shù)據(jù),那么你可以創(chuàng)建兩個(gè)緩沖區(qū)對(duì)象, 然后調(diào)用glVertexArrayVertexBuffer去將它們各自綁定到不同的緩沖區(qū)的綁定節(jié)點(diǎn),然后通過glVertexArrayAttribBingding分別為它們指定兩個(gè)它們使用的索引。 另外,你也可以把數(shù)據(jù)放在同一個(gè)緩沖區(qū)的不同位置,然后使用跟上面相同的方法去做這件事。清單5.7展示了如何做到這些操作

GLuint buffer[2];
GLuint vao;
static const GLfloat positions[] = { ... };
static const GLfloat colors[] = { ... };
// Create the vertex array object
glCreateVertexArrays(1, &vao)
// Get create two buffers
glCreateBuffers(2, &buffer[0]);
// Initialize the first buffer
glNamedBufferStorage(buffer[0], sizeof(positions), positions, 0);
// Bind it to the vertex array - offset zero, stride = sizeof(vec3)
glVertexArrayVertexBuffer(vao, 0, buffer[0], 0, sizeof(vmath::vec3));
// Tell OpenGL what the format of the attribute is
glVertexArrayAttribFormat(vao, 0, 3, GL_FLOAT, GL_FALSE, 0);
// Tell OpenGL which vertex buffer binding to use for this attribute
glVertexArrayAttribBinding(vao, 0, 0);
// Enable the attribute
glEnableVertexArrayAttrib(vao, 0);
// Perform similar initialization for the second buffer
glNamedBufferStorage(buffer[1], sizeof(colors), colors, 0);
glVertexArrayVertexBuffer(vao, 1, buffer[1], 0, sizeof(vmath::vec3));
glVertexArrayAttribFormat(vao, 1, 3, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(vao, 1, 1);
glEnableVertexAttribArray(1);
Listing 5.7: Multiple separate vertex attributes

清單5.7:通過separate方式為頂點(diǎn)屬性傳入數(shù)據(jù)

In both cases of separate attributes, we have used tightly packed arrays of data to feed both attributes. This is effectively structure-of-arrays (SoA) data. We have a set of tightly packed, independent arrays of data. However, it’s also possible to use an array of-structures (AoS) form of data. Consider how the following structure might represent a single vertex:

兩種separate的數(shù)據(jù)傳輸方式我們都用到了非常緊湊的數(shù)據(jù)塊。當(dāng)然,使用AOS也是可以的,我們來看看下面這樣的結(jié)構(gòu)體。

struct vertex
{
// Position
float x;
float y;
float z;
// Color
float r;
float g;
float b;
};
Now we have two inputs to our vertex shader (position and color) interleaved together in a single structure. Clearly, if we make an array of these structures, we have an AoS layout for our data. To represent this with calls to glVertexArrayVertexBuffer(), we have to use its stride parameter. The stride parameter tells OpenGL how far apart in bytes the beginning of each vertex’s data is. If we leave it as 0, OpenGL will use the same data for every vertex. However, to use the vertex structure declared above, we can simply use sizeof(vertex) for the stride parameter and everything will work out. Listing 5.8 shows the code to do this

現(xiàn)在我們使用interleaved方式為shader的兩個(gè)屬性輸入數(shù)據(jù)。很明顯,如果我們有一組這樣的結(jié)構(gòu)體類型的數(shù)據(jù),我們使用的是AOS的數(shù)據(jù)塊。我們使用glVertexArrayVertexBuffer來設(shè)置的時(shí)候,我們 需要用到它的stride參數(shù)。stride參數(shù)告訴OpenGL兩個(gè)vertex之間的間隔,如果這個(gè)參數(shù)我們寫0,那么所有的頂點(diǎn)都使用的是同一個(gè)數(shù)據(jù)。使用這個(gè)數(shù)據(jù)結(jié)構(gòu)是很簡單的,我們直接使用sizeof(vertex)來賦值 給stride參數(shù)。清單5.8展示了剛才討論的這種方式的樣本代碼

GLuint vao;
GLuint buffer;
static const vertex vertices[] = { ... };
// Create the vertex array object
glCreateVertexArrays(1, &vao);
// Allocate and initialize a buffer object
glCreateBuffers(1, &buffer);
glNamedBufferStorage(buffer, sizeof(vertices), vertices, 0);
// Set up two vertex attributes - first positions
glVertexArrayAttribBinding(vao, 0, 0);
glVertexArrayAttribFormat(vao, 0, 3, GL_FLOAT, GL_FALSE, offsetof(vertex,x));
glEnableVertexArrayAttrib(0);
// Now colors
glVertexArrayAttribBinding(vao, 1, 0);
glVertexArrayAttribFormat(vao, 1, 3, GL_FLOAT, GL_FALSE, offsetof(vertex,
r));
glEnableVertexArrayAttrib(1);
// Finally, bind our one and only buffer to the vertex array object
glVertexArrayVertexBuffer(vao, 0, buffer);
Listing 5.8: Multiple interleaved vertex attributes

清單5.8:使用interleaved方式為shader的多個(gè)屬性輸入數(shù)據(jù)

After executing the code in Listing 5.8, you can bind the vertex array object and start pulling data from the buffers bound to it. After the vertex format information has been set up with calls to glVertexArrayAttribFormat(), you can change the vertex buffers that are bound with further calls to glVertexArrayAttribBinding(). If you want to render a lot of geometry stored in different buffers but with similar vertex formats, simply call glVertexArrayAttribBinding() to switch buffers and start drawing from them.
當(dāng)執(zhí)行完畢了清單5.8的代碼之后,你就可以拿著它去畫畫了。在使用glVertexArrayAttribFormat設(shè)置好了緩沖區(qū)之后,你可以調(diào)用glVertexArrayAttribBinding去修改當(dāng)前的緩沖區(qū)對(duì)象。 如果你想渲染很多內(nèi)存相似,卻存儲(chǔ)在不同緩沖區(qū)里的幾何形體時(shí),你可以簡單調(diào)用glVertexArrayAttribBinding去切換輸入緩沖區(qū),然后直接調(diào)用繪圖函數(shù)。

Loading Objects from Files(從文件加載物體)

As you can see, you could potentially use a large number of vertex attributes in a single vertex shader. As we progress through various techniques, you will see that we’ll regularly use four or five vertex attributes, and possibly more. Filling buffers with data to feed all of these attributes and then setting up the vertex array object and all of the vertex attribute pointers can be a chore. Further, encoding all of your geometry data directly in your application isn’t practical for anything but the simplest models. Therefore, it makes sense to store model data in files and load it into your application. There are plenty of model file formats out there, and most modeling programs support several of the more common formats.

如你所見,你可能在shader里需要使用大量的頂點(diǎn)屬性。隨著學(xué)習(xí)的深入,我們時(shí)常需要經(jīng)常用到四五個(gè)頂點(diǎn)屬性,或許更多。手工的去干這個(gè)活很麻煩。另外, 直接在應(yīng)用程序里手寫這些幾何數(shù)據(jù),會(huì)讓你內(nèi)分泌失調(diào)。因此,從文件加載模型這個(gè)選擇看起來非常不錯(cuò)。模型的種類有很多,很多建模軟件都支持一些比較常見的模型格式。

For the purpose of this book, we have devised a simple object file definition called an .SBM file, which stores the information we need without being either too simple or too overly engineered. Complete documentation for the format is found in Appendix B, “The SBM File Format.” The sb7 framework also includes a loader for this model format, called sb7::object. To load an object file, create an instance of sb7::object and call its load function as follows:

為了學(xué)習(xí)的需要,我們來使用一個(gè)后綴叫SBM的模型格式。該格式的完整格式描述在附錄B。我們教程的框架代碼提供加載該格式的代碼,sb7::object。你可以通過下面的代碼加載一個(gè)sbm的模型
sb7::object my_object;
my_object.load("filename.sbm");
If this operation is successful, the model will be loaded into the instance of sb7::object and you will be able to render it. During loading, the class will create and set up the object’s vertex array object and then configure all of the vertex attributes contained in the model file. The class also includes a render function that binds the object’s vertex array object and calls the appropriate drawing command. For example, calling

如果加載成功,你之后就可以渲染它了。在加載的時(shí)候,這個(gè)類會(huì)配置好VAO以及所有的頂點(diǎn)屬性。這個(gè)類還包括了渲染的API接口,里面綁定了VAO之后,然后調(diào)用了繪圖函數(shù)。比如下面這樣

my_object.render();
will render a single copy of the object with the current shaders. In many of the examples in the remainder of this book, we’ll simply use our object loader to load object files (several of which are included with the book’s source code) and render them.

上面的接口就會(huì)使用當(dāng)前的shader去渲染該模型。在本書剩余的內(nèi)容中,我們將使用這個(gè)模型類去加載并渲染模型。

本日的翻譯就到這里,明天見,拜拜~~

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

東漢書院,等你來玩哦

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文題目:UsingMultipleVertexShaderInputs-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://chinadenli.net/article38/deedsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、微信小程序搜索引擎優(yōu)化、網(wǎng)站排名標(biāo)簽優(yōu)化、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司