[英]Binding an OpenGL Texture Sampler
我對如何將紋理綁定到GLSL中的插槽感到困惑。 這是我的着色器:
#version 330
layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 texCoords;
out vec4 ex_texCoords;
uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;
void main(void)
{
gl_Position = (ProjectionMatrix * ViewMatrix * ModelMatrix) * in_Position;
ex_texCoords = texCoords;
}
#version 330
in vec4 ex_texCoords;
out vec4 out_Color;
uniform sampler2D textureSampler;
void main(void)
{
out_Color = texture(textureSampler2, ex_texCoords.xy);
}
當我設置矩陣時,我需要查詢統一位置,然后在該位置手動設置值,執行以下操作:
modelMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ModelMatrix");
...
glUniformMatrix4fv(modelMatrixUniformLocation, 1, GL_FALSE, modelMatrix.data);
但是,據我所知,我不需要這樣做來初始化我的紋理采樣器。 在設置時,我似乎需要做的就是:
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);
glTexImage2D(...);
所以,我的問題是,為什么我不需要為矩陣做我需要做的紋理處理呢? OpenGL運行時如何“知道”我綁定的紋理應該是我定義的采樣器? 如果我想要多個紋理,我該怎么做呢?
OpenGL不知道要使用哪個采樣器,你必須使用glActiveTexture(GL_TEXTUREi)
(其中i
從0
到GL_MAX_TEXTURES
) glActiveTexture(GL_TEXTUREi)
綁定它。 您的代碼有效,因為默認采樣器是GL_TEXTURE0
。 顯然你很幸運,着色器鏈接器將textureSampler
設置為0。
此外,OpenGL API版本3及更高版本中還有一項新功能 - glGenSamplers , glBindSampler 。 它允許您使用與紋理無關的采樣器配置! 您可以閱讀http://www.g-truc.net/post-0267.html了解更多詳情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.