OpenGL學習日記-2015.3.13——多執行個體渲染,opengl-2015.3.13
執行個體化(instancing)或者多執行個體渲染(instancd rendering)是一種連續執行多條相同渲染命令的方法。並且每個命令的所產生的渲染結果都會有輕微的差異。是一種非常有效,實用少量api調用來渲染大量幾何體的方法。OpenGL提供多種機制,允許著色器對不同渲染執行個體賦予不同的頂點屬性。幾個簡單的多執行個體渲染命令: 1、void glDrawArraysInstanced( GLenum mode, GLint first, GLsizei count, GLsizei primCount ) 該函數是glDrawArrays()的多執行個體版本,參數完全等價,只是多了個primCount,該參數用於設定渲染執行個體個數。 2、void glDrawElementsInstanced(GLenum mode, GLsizei count, GLenum type, void* indices, GLsizei primcount ) 該函數是glDrawElements()的多執行個體版本,同樣只是多了個primCount參數而已,同樣是用於設定渲染執行個體個數。 3、void glDrawElementsInstancedBaseVertex( GLenum mode, GLsizei count, GLenum type, const void* indices, GLsizei instanceCount, GLuint baseVertex ) 該函數是glDrawElementsBaseVertex()的多執行個體版本,instanceCount表示渲染的執行個體數目。
多執行個體渲染頂點屬性控制: 1、void glVertexAttribDivisor( GLenum index, GLuint divisor ) index對應於著色器中輸入變數的location。divisor:表示頂點屬性的更新頻率,每隔多少個執行個體將重新設定執行個體的該屬性,例如設定為1,那麼每個執行個體的屬性都不一樣,設定為2則每兩個執行個體相同,3則每三個執行個體改變屬性。而該屬性的屬性數組大小將為(instance/divisor),instance為之前設定的渲染執行個體數(primCount),假設在多執行個體渲染中改變執行個體的顏色,設divisor為2,instance為100,顏色數組至少為100/2 = 50組rgba資料,才能保證每個執行個體都有自己的顏色值,不然將是黑漆漆的。最後如果divisor設定為0,將代表是非執行個體化,渲染的結果是,所有執行個體都是黑漆漆的,可能這個黑漆漆的結果也不是必然的,我猜想的是這時候著色器的輸入變數vec4 color為預設的(0.0,0.0,0.0,1.0)並沒有設定它的值,所以是黑色的。
頂點著色器分析:#version 410
//輸入變數position,頂點座標 layout (location = 0) in vec4 position;//normal頂點法線計算 layout (location = 1) in vec3 normal;//頂點顏色 layout (location = 2) in vec4 color;
//特別注意,這裡設定的mat4類型的輸入變數,location為3,但是一個mat4類型會佔據連續的4個位置//因此model_matrix佔據了3,4,5,6四個索引位置。 layout (location = 3) in mat4 model_matrix;
//在程式渲染過程是常量的視圖矩陣,和投影矩陣(just這個程式是常量) uniform mat4 view_matrix; uniform mat4 projection_matrix; //輸入變數,一個簡單的結構體,法線,和顏色。 out VERTEX { vec3 normal; vec4 color; } vertex;
void main(void) { //計算模型視圖矩陣 mat4 model_view_matrix = view_matrix * model_matrix; //計算頂點座標 gl_Position = projection_matrix * (model_view_matrix * position); //計演算法線和顏色,並輸出 vertex.normal = mat3(model_view_matrix) * normal; vertex.color = color; }
片元著色器分析:#version 410
//片元著色器的輸出 layout (location = 0) out vec4 color;
//和頂點著色器幾乎一樣的片元著色器輸入,in/out必須是匹配的。 in VERTEX { vec3 normal; vec4 color; } vertex;
void main(void) { //結合器線計算最終的顏色 color = vertex.color * (0.1 + abs(vertex.normal.z)) + vec4(0.8, 0.9, 0.7, 1.0) * pow(abs(vertex.normal.z), 40.0); }
應用程式代碼:代碼並沒有太多的分析,主要是用了前面幾個函數,進行多執行個體渲染的設定,代碼在關鍵的地方都有或多或少的注釋。在這裡把儘可能把代碼貼全。代碼中用到了vbm格式的模型資料,大概是書者自己定義的一種格式,在原始碼中vbm管理類曆經變遷,新版本還不能讀舊版本的資料。在這裡順便提供第八版原始碼的:http://www.opengl-redbook.com/ 應該可以輕易下到。感謝作者們的辛勤勞動
#include "instancing_1.h"#include "vutils.h"#include "vmath.h"#include "vbm_ch3_intancing1.h"namespace instancing1{float shade_aspect = 800 / 600;GLuint color_buffer;GLuint model_matrix_buffer;GLuint render_prog;GLuint model_matrix_loc;GLuint view_matrix_loc;GLuint projection_matrix_loc;VBObject object;#define INSTANCE_COUNT 100GLenum gl_err = 0;static const GLubyte* errorStr = NULL;};using namespace instancing1;void instancing1Init(){int n;//建立著色器程式render_prog = glCreateProgram();//著色器字串static const char render_vs[] ="#version 410\n""\n""// 'position' and 'normal' are regular vertex attributes\n""layout (location = 0) in vec4 position;\n""layout (location = 1) in vec3 normal;\n""\n""// Color is a per-instance attribute\n""layout (location = 2) in vec4 color;\n""\n""// model_matrix will be used as a per-instance transformation\n""// matrix. Note that a mat4 consumes 4 consecutive locations, so\n""// this will actually sit in locations, 3, 4, 5, and 6.\n""layout (location = 3) in mat4 model_matrix;\n""\n""// The view matrix and the projection matrix are constant across a draw\n""uniform mat4 view_matrix;\n""uniform mat4 projection_matrix;\n""\n""// The output of the vertex shader (matched to the fragment shader)\n""out VERTEX\n""{\n"" vec3 normal;\n"" vec4 color;\n""} vertex;\n""\n""// Ok, go!\n""void main(void)\n""{\n"" // Construct a model-view matrix from the uniform view matrix\n"" // and the per-instance model matrix.\n"" mat4 model_view_matrix = view_matrix * model_matrix;\n""\n"" // Transform position by the model-view matrix, then by the\n"" // projection matrix.\n"" gl_Position = projection_matrix * (model_view_matrix * position);\n"" // Transform the normal by the upper-left-3x3-submatrix of the\n"" // model-view matrix\n"" vertex.normal = mat3(model_view_matrix) * normal;\n"" // Pass the per-instance color through to the fragment shader.\n"" vertex.color = color;\n""}\n";static const char render_fs[] ="#version 410\n""\n""layout (location = 0) out vec4 color;\n""\n""in VERTEX\n""{\n"" vec3 normal;\n"" vec4 color;\n""} vertex;\n""\n""void main(void)\n""{\n"" color = vertex.color * (0.1 + abs(vertex.normal.z)) + vec4(0.8, 0.9, 0.7, 1.0) * pow(abs(vertex.normal.z), 40.0);\n""}\n";//shader建立,編譯,裝載vglAttachShaderSource( render_prog, GL_VERTEX_SHADER, render_vs );vglAttachShaderSource( render_prog, GL_FRAGMENT_SHADER, render_fs );//串連著色器成為可用程式glLinkProgram(render_prog);//啟用著色器glUseProgram(render_prog);//擷取視圖矩陣位置view_matrix_loc = glGetUniformLocation(render_prog, "view_matrix");//擷取投影矩陣位置projection_matrix_loc = glGetUniformLocation(render_prog, "projection_matrix");//載入vbm模型檔案//@pram 1路徑、2頂點location 3法線location、4紋理location(顏色)object.LoadFromVBM("../8edlib/media/armadillo_low.vbm", 0, 1, 2);//綁定頂點數組object.BindVertexArray();//擷取頂點著色器in變數locationint position_loc = glGetAttribLocation( render_prog, "position" );int normal_loc = glGetAttribLocation( render_prog, "normal" );int color_loc = glGetAttribLocation( render_prog, "color" );int matrix_loc = glGetAttribLocation( render_prog, "model_matrix");//每個執行個體的顏色數組vmath::vec4 colors[INSTANCE_COUNT];for (n = 0; n < INSTANCE_COUNT; n++ ){float a = float(n) / 4.0f;float b = float(n) / 5.0f;float c = float(n) / 6.0f;colors[n][0] = 0.5f + 0.25f * (sinf(a + 1.0f) + 1.0f );colors[n][1] = 0.5f + 0.25f * (sinf(b + 2.0f) + 1.0f );colors[n][2] = 0.5f + 0.25f * (sinf(c + 3.0f) + 1.0f );colors[n][3] = 1.0f;}//緩衝對象glGenBuffers(1, &color_buffer);glBindBuffer(GL_ARRAY_BUFFER, color_buffer);//3個工作。glBufferData( GL_ARRAY_BUFFER, sizeof(colors), colors, GL_DYNAMIC_DRAW );//動態改變用於繪製的資料glVertexAttribPointer( color_loc, 4, GL_FLOAT, GL_FALSE, 0, NULL );glEnableVertexAttribArray( color_loc );//!!!啟用頂點屬性多執行個體屬性,每個執行個體讀取一次顏色值glVertexAttribDivisor( color_loc, 1);//模型矩陣資料glGenBuffers(1, &model_matrix_buffer );glBindBuffer( GL_ARRAY_BUFFER, model_matrix_buffer );//NULL保留記憶體glBufferData( GL_ARRAY_BUFFER, INSTANCE_COUNT * sizeof(vmath::mat4), NULL, GL_DYNAMIC_DRAW );for (int i = 0; i < 4; i++ ){glVertexAttribPointer(matrix_loc + i, 4, GL_FLOAT, GL_FALSE, sizeof(vmath::mat4), (void *)(sizeof(vmath::vec4)* i));glEnableVertexAttribArray( matrix_loc + i );glVertexAttribDivisor( matrix_loc + i, 1 );}//解除vao綁定,至於為什麼要這句呢,我的理解是:這是個好習慣,用完之後吧OpenGL還原預設狀態。glBindVertexArray(0);}static inline int min( int a, int b ){return a < b ? a : b;}void instancing1Display(){float t = float(GetTickCount() & 0x3FFF) / float(0x3FFF);int n;//清屏glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );//glEnable( GL_CULL_FACE );glEnable( GL_DEPTH_TEST );glDepthFunc( GL_LEQUAL );//綁定vbo,修改資料glBindBuffer( GL_ARRAY_BUFFER, model_matrix_buffer );//擷取當前vbo的OpenGL指標vmath::mat4* matrices = (vmath::mat4*)glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY );for (n = 0; n < INSTANCE_COUNT; n++ ){float a = 50.0f * float(n) / 4.0f;float b = 50.0f * float(n) / 5.0f;float c = 50.0f * float(n) / 6.0f;matrices[n] = vmath::rotate(a + t * 360.0f, 1.0f, 0.0f, 0.0f) * vmath::rotate(a + t * 360.0f, 0.0f, 1.0f, 0.0f) * vmath::rotate(a + t * 360.0f, 0.0f, 0.0f, 1.0f) * vmath::translate(10.0f + a, 40.0f + b, 50.0f + c);//平移,再旋轉}//資料操作完畢,解除映射!必須glUnmapBuffer( GL_ARRAY_BUFFER );//確認啟用需要的著色器程式glUseProgram( render_prog );//產生視圖矩陣和投影矩陣vmath::mat4 view_matrix(vmath::translate(0.0f, 0.0f, -1500.0f) * vmath::rotate(t * 360.0f * 2.0f, 0.0f, 1.0f, 0.0f));vmath::mat4 projection_matrix(vmath::frustum(-1.0f, 1.0f, -shade_aspect, shade_aspect, 1.0f, 5000.0f));//設定視圖矩陣和投影矩陣glUniformMatrix4fv( view_matrix_loc, 1, GL_FALSE, view_matrix );//這次你就沒寫錯參數,能不能不坑爹,之前我都以為自己智商出問題了。glUniformMatrix4fv( projection_matrix_loc, 1, GL_FALSE, projection_matrix );//渲染多個執行個體object.Render( 0, INSTANCE_COUNT ); //能不能不坑爹。。後面的vbm居然不相容前面的vbm.....GLenum error = glGetError();//const GLubyte* errorStr = gluErrorString(error);//這個幾個意思。。。lookat只是產生了一個矩陣而已。。。//vmath::lookat( vmath::vec3(0.0f, 0.0f, 0.0f), vmath::vec3(1.0f, 0.0f, 0.0f), vmath::vec3(0.0f, 1.0f, 0.0f) ); //攝像機?????????/}void instancing1Update(float){}
另一個多執行個體渲染執行個體,紋理打包,著色器內建變數gl_InstanceID使用:執行個體計數器gl_InstanceID:當前執行個體的索引值可以再頂點著色器中通過內建變數gl_InstanceID變數獲得。該變數被聲明為一個整數,初始為0,每個執行個體被渲染之後,他會加1.他總是存在於頂點著色器中,即使當前沒有啟用多執行個體特性,此時他的值保持0.gl_InstanceID可以作為uniform數組的索引使用,也可以作為紋理尋找的參數,或者作為某個分析函數的輸入,等等。新執行個體分析:新樣本實現了上一個執行個體的相同畫面,只是程式的實現方式不一樣。在這裡用到了紋理緩衝對象,對於我又是一個未使用過的特性,在接受新東西的時候總不會覺得乏味。...................雖然不覺得乏味,但還是會困,明天還得上班QAQ,To Be Continue~~~~~