2012-12-01 51 views
0

我正在使用GLSL進行紋理處理。 我如何處理GLSL的兩個紋理? 一個人建議我在我的GLSL中做兩個samplers2D ... 但是GLSL怎麼知道哪個samplers2D應該使用? (我不是在說混合紋理......)GLSL - 兩種紋理(不混合)

我聽說我應該使用glBindTexture。 我該怎麼做?用glBindTexture?任何人都有這樣的例子嗎?

的openGL 3.3


編輯:

我有這樣的:

uniform Sampler2D texture1; 
uniform Sampler2D texture2; 

我需要繪製兩個對象,使用紋理,這樣怎麼能GLSL知道他是否應該使用texture1或紋理2根據我想繪製的對象。這是我的問題。

+0

[OpenGL Wiki有一篇關於如何工作的文章](http://www.opengl.org/wiki/Sampler_%28GLSL% 29個#Binding_textures_to_samplers)。 –

+0

@NicolBolas你知道他們爲什麼在這些例子中使用GL_TEXTURE0 + x而不是'GL_TEXTURE0','GL_TEXTURE2'等。 – user1118321

+0

@ user1118321由於'GL_TEXTURE#'只能達到31,而*實際*限制可能要高得多。你應該總是使用'GL_TEXTURE0 + x'。 –

回答

2

您需要將每個紋理綁定到不同的紋理單元,然後使用多紋理座標。它會看起來像這樣(假設你已經有紋理):

glActiveTexture (GL_TEXTURE0); // First texture is going into texture unit 0 
glBindTexture (GL_TEXTURE_2D, tex0); 
glEnable (GL_TEXTURE_2D); 

glActiveTexture (GL_TEXTURE1); // Second texture is going into texture unit 1 
glBindTexture (GL_TEXTURE2D, tex1); 
glEnable (GL_TEXTURE_2D); 

glUseProgram (yourGLSLProgramID); 
glUniform1i (sampler1Location, 0); // tell glsl that sampler 1 is sampling the texture in texture unit 0 
glUniform1i (sampler2Location, 1); // tell glsl that sampler 2 is sampling the texture in texture unit 1 
///... set the rest of your uniforms... 

glBegin (GL_QUADS); 
    glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0); 
    glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0); 
    glVertexCoord2f(0.0, 0.0); 

    glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0); 
    glMultiTexCoord2f(GL_TEXTURE1, 1.0, 0.0); 
    glVertexCoord2f(width, 0.0); 

    glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0); 
    glMultiTexCoord2f(GL_TEXTURE1, 1.0, 1.0); 
    glVertexCoord2f(width, height); 

    glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0); 
    glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0); 
    glVertexCoord2f(0.0, height); 
glEnd(); 
+1

謝謝,但我需要使用openGL 3.3。 – Spamdark

+2

好的,所以唯一的區別是不會使用'glBegin()'/'glEnd()'。相反,你會使用各種紋理單元的座標數組。 – user1118321

+0

請參閱[這個答案](http://stackoverflow.com/questions/1524736/glmultitexcoord-in-opengl-es)關於如何使用'glTexCoordPointer()'而不是'glMultiTexCoord()'的信息。 – user1118321