簡體   English   中英

在OpenGL中從着色器讀取多個紋理單位的問題

[英]Problem with reading multiple texture units from a Shader in OpenGL

我正在嘗試在着色器中讀取兩種不同的紋理,一種用於常規紋理化,一種用於凹凸貼圖。 但是,兩個Sampler2D都從同一紋理單元讀取。 我將統一設置為0和1,並將紋理綁定到其各自的單元,如下所示:

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texMgr->GetTexture("stone")->texture);

glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texMgr->GetTexture("bump")->texture);

在渲染循環中,我將制服設置如下:

shaderMgr->activeProgram()->setUniform1f("tex", 0);
shaderMgr->activeProgram()->setUniform1f("norm", 1);

最后是我的着色器代碼:

varying vec4 colorVarying;
varying vec4 normalVarying;
varying vec3 lightDirVarying;
varying vec2 textureCoordinateVarying;

uniform sampler2D tex;
uniform sampler2D norm;

void main() {    
    vec4 texColor = texture2D(tex, textureCoordinateVarying);
    vec4 normColor = texture2D(norm, textureCoordinateVarying);
    vec3 newNormal = vec3(2.0 * normColor.x - 1.0, 2.0 * normColor.y - 1.0, 2.0 * normColor.z - 1.0);
    vec3 normal = normalize(normalVarying.xyz + newNormal);
    float diff = max(0.0, dot(normal, normalize(lightDirVarying)));

    gl_FragColor = texColor * (diff + 0.3);
}

每次繪制對象時,我只會在我的renderloop中調用glActiveTexture,並且只調用一次glActiveTexture(GL_TEXTURE1)(在初始化過程中)。

glActiveTexture(GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, object->tex->texture);

第一個紋理一切正常,但第二個紋理(凹凸)未顯示。 我試過了:

  • 將ARB添加到所有內容中,我不確定是否很重要。 (如果有什么用,我在OSX 10.6上)
  • 每次切換紋理單位時添加glEnable(GL_TEXTURE_2D)。
  • 無需在程序之間切換,僅使用一個着色器運行,並在初始化時設置制服
  • 每次切換紋理單位時,重置紋理參數。
  • 不切換TextureUnits,僅在開始時對其進行初始化
  • 開始一個新項目,僅復制並運行相關代碼,並使其盡可能地小

所有這些都無濟於事,范數采樣器僅讀取GL_TEXTURE0單元。

我已經搜索了幾個小時,但仍然沒有找到答案。 我不知道我在做什么錯,為什么我的范數采樣器無法從正確的GL_TEXTURE1單元讀取。

shaderMgr->activeProgram()->setUniform1f("tex", 0);

如果這是調用glUniform1f,那是錯誤的,您必須使用glUniform1i設置紋理采樣器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM