如何向GLSL中傳入多個紋理

2021-07-16 07:03:15 字數 1916 閱讀 2453

如何向glsl中傳入多個紋理

如下程式,我們在glsl的fragment著色程式中定義了3個sample2d作為紋理引數。

[cpp]view plain

copy

uniform sampler2d basemap;  

uniform sampler2d reflectmap;  

uniform sampler2d refractmap;  

在主程式中,我們生成3個紋理id

[cpp]view plain

copy

glbindtexture(gl_texture_2d, baseid);  

glteximage2d(gl_texture_2d, 0, gl_rgb, width, height, 0, gl_rgb, gl_unsigned_byte, basedata);  

glbindtexture(gl_texture_2d, reflectionid);  

glteximage2d(gl_texture_2d, 0, gl_rgb, width, height, 0, gl_rgb, gl_unsigned_byte, reflectiondata);  

glbindtexture(gl_texture_2d, refractionid);  

glteximage2d(gl_texture_2d, 0, gl_rgb, width, height, 0, gl_rgb, gl_unsigned_byte, refractiondata);  

gluseprogram(shaderid);  

// 設定紋理引數的數值,這裡是關鍵,很多時候我們會以為要把紋理id作為sampler2d引數的值傳給glsl,

// 我們可能會這樣寫gluniform1i(texloc, baseid,但這做法是錯的,glsl的sample2d 只接受紋理單元的索引號,gl_texture0+i

// 還有乙個要注意的地方就是要用gluniform1i函式,而不要用gluniform1ui();

glint texloc;  

texloc = glgetuniformlocation(shaderid, "basemap"

);  

gluniform1i(texloc, 0); //gl_texture0,

//這裡我覺得是opengl做得最不人性化的地方,你只能輸入0,1,2來代表紋理單元的索引,不直觀,讓人摸不著頭腦。

texloc = glgetuniformlocation(shaderid, "reflectmap"

);  

gluniform1i(texloc, 1); //gl_texture1

texloc = glgetuniformlocation(shaderid, "refractmap"

);  

gluniform1i(texloc, 2); //gl_texture2

then in further down in my draw() function:  

// 把紋理id和紋理單元繫結在一起。

glactivetexture(gl_texture0);  

glbindtexture(gl_texture_2d, baseid);  

glactivetexture(gl_texture1);  

glbindtexture(gl_texture_2d, reflectionid);  

glactivetexture(gl_texture2);  

glbindtexture(gl_texture_2d, refractionid);  

// 用了glsl,glenable(gl_texture_2d);gldisable(gl_texture_2d);就不起作用了,一切由著色**來控制。

Mybatis中為Mapper中傳入多個值

1.通過順序 select from user where name and dept 在 中的數字代表了傳遞引數的順序,一般不建議使用 2.通過 param public user selecttest param username string name,param deptid int dep...

組合語言 向視訊記憶體中傳入字元

程式設計序,在螢幕的中間分別顯示綠色 綠底紅色 白底藍色的字串 yantaiuniversity assume cs codeseg,ds datasg datasg segment db yantaiuniversity datasg ends codeseg segment start mov ...

怎樣向awk中傳入shell變數值

假設當前目錄下有a.log b.log二個檔案,利用awk列印出其檔名,指令碼如下 bin bash for file in log doawk beginend file done 在這裡最讓人混淆的是單引號和雙引號 我們可以利用bash x test.sh來執行指令碼,其會將指令碼中的shell...