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

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

周一到周五,每天一篇,北京時間早上7點準時更新~

多倫網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、成都響應式網(wǎng)站建設等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)自2013年創(chuàng)立以來到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選創(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)學會了使用使用緩沖區(qū)為shader輸入數(shù)據(jù),你也可以為你的vertex shader定義多個輸入的屬性。結合這些所學的東西,就意味著你可以同時給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的兩個輸入數(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

如果你生成了一個有多組輸入數(shù)據(jù)的GPU程序,那么你可以通過下面的API來獲取某一個輸入數(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.

第一個參數(shù)是GPU程序,第二個參數(shù)是頂點屬性的名字。在我們清單5.6的代碼中,名字這個參數(shù)如果傳position,那么該API會返回0,如果傳color,那么該API會返回1. 如果你傳了一個不存在的名字,那么該API返回-1.當然,如果你在shader代碼中去定義了屬性的綁定位置,那么你總會拿到跟你設置的那些位置一致的返回值。如果你不去在shader里 設置屬性的綁定位置,那么OpenGL將會為你分配這些位置,你通過這個API就可以獲取到那些位置。有兩種方法可以讓你給shader中的屬性傳輸數(shù)據(jù),一種是separate,另一種叫interleaved。 當使用separate方式傳輸數(shù)據(jù)的時候,你的數(shù)據(jù)會存放在不同的緩沖區(qū)對象里,或者存儲在同一個緩沖區(qū)對象里的不同位置。比如你希望給shader中的兩個屬性傳輸數(shù)據(jù),那么你可以創(chuàng)建兩個緩沖區(qū)對象, 然后調用glVertexArrayVertexBuffer去將它們各自綁定到不同的緩沖區(qū)的綁定節(jié)點,然后通過glVertexArrayAttribBingding分別為它們指定兩個它們使用的索引。 另外,你也可以把數(shù)據(jù)放在同一個緩沖區(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方式為頂點屬性傳入數(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ù)塊。當然,使用AOS也是可以的,我們來看看下面這樣的結構體。

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的兩個屬性輸入數(shù)據(jù)。很明顯,如果我們有一組這樣的結構體類型的數(shù)據(jù),我們使用的是AOS的數(shù)據(jù)塊。我們使用glVertexArrayVertexBuffer來設置的時候,我們 需要用到它的stride參數(shù)。stride參數(shù)告訴OpenGL兩個vertex之間的間隔,如果這個參數(shù)我們寫0,那么所有的頂點都使用的是同一個數(shù)據(jù)。使用這個數(shù)據(jù)結構是很簡單的,我們直接使用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的多個屬性輸入數(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.
當執(zhí)行完畢了清單5.8的代碼之后,你就可以拿著它去畫畫了。在使用glVertexArrayAttribFormat設置好了緩沖區(qū)之后,你可以調用glVertexArrayAttribBinding去修改當前的緩沖區(qū)對象。 如果你想渲染很多內(nèi)存相似,卻存儲在不同緩沖區(qū)里的幾何形體時,你可以簡單調用glVertexArrayAttribBinding去切換輸入緩沖區(qū),然后直接調用繪圖函數(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里需要使用大量的頂點屬性。隨著學習的深入,我們時常需要經(jīng)常用到四五個頂點屬性,或許更多。手工的去干這個活很麻煩。另外, 直接在應用程序里手寫這些幾何數(shù)據(jù),會讓你內(nèi)分泌失調。因此,從文件加載模型這個選擇看起來非常不錯。模型的種類有很多,很多建模軟件都支持一些比較常見的模型格式。

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:

為了學習的需要,我們來使用一個后綴叫SBM的模型格式。該格式的完整格式描述在附錄B。我們教程的框架代碼提供加載該格式的代碼,sb7::object。你可以通過下面的代碼加載一個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

如果加載成功,你之后就可以渲染它了。在加載的時候,這個類會配置好VAO以及所有的頂點屬性。這個類還包括了渲染的API接口,里面綁定了VAO之后,然后調用了繪圖函數(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.

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

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

第一時間獲取最新橋段,請關注東漢書院以及圖形之心公眾號

東漢書院,等你來玩哦

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

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

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

廣告

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

成都app開發(fā)公司