2

在将我们的源代码从 OpenInventor 9.8 升级到 10.4.2 时,我遇到在 10.4.2 中片段着色器中计算的某些颜色始终为黑色,而在 9.8 中一切正常。通常我们使用自己计算的纹理,但出于调试目的,我使用了 OpenInventor 示例中的示例纹理:

SoSwitch* root = new SoSwitch;
// Fill root with geometry
root->addChild(...)

SoSeparator* localRoot = new SoSeparator;
SoFragmentShader* fragShader = new SoFragmentShader;
SoShaderProgram* shaderProgram = new SoShaderProgram;

SoTexture2 texture = new SoTexture2;
texture->filename = "pathToImage\image.png"

SoTextureUnit textureUnit = new SoTextureUnit;
texture Unit->unit = 1;

localRoot->addChild(textureUnit);
localRoot->addChild(texture);

fragShader->addShaderParameter1i("myTexture", 1);
shaderProgram->shaderObject.set1Value(1, fragShader);

root->addChild(localRoot);
root->addChild(shaderProgram);

这是适用于 9.8 的片段着色器:

#version 120
uniform sampler2D myTexture;
in vec3 coord; // Computed in vertex-shader
int main() {
    gl_FragColor = texture2D(myTexture, coord.xy);
    // For Debugging:
    // gl_FragColor = vec4(coord.xy, 0, 1);
}

这是不适用于 10.4.2 的片段着色器:

#version 410 compatibility
//!oiv_include <Inventor/oivShaderState.h>
//!oiv_include <Inventor/oivShapeAttribute.h>
//!oiv_include <Inventor/oivShaderVariables.h>
uniform sampler2D myTexture;
in vec3 coord;
int main() {
    OivFragmentOutput(texture(myTexture, coord.xy)); // Is the same as gl_FragColor = 
    // For Debugging:
    // gl_FragColor = vec4(coord.xy, 0, 1);
}

查看器保持完全黑色,因此我假设调用texture返回始终为零。取消注释gl_FragColor = vec4(coord.xy, 0, 1);给出相同的结果。因此,我假设coord计算正确。

当我们从版本#120 跳转到#410 时,我可以想象我需要做一些其他的事情来让texture我们的片段着色器工作。是否有任何相关的变化GLSL。我需要做什么才能让着色器工作?

如果相关,以下是一些系统信息:

  • 操作系统:Windows 10
  • 显卡:NVIDIA Quadro K2200
4

2 回答 2

3

这里的问题在于您的场景图中,因为 纹理textureUnit 节点都位于 SoSeparator 下,并且对位于 SoSeparator localRoot之外的 shaderProgam可见。请将这些节点移出localRoot 并将它们作为子节点添加到根节点以正确呈现。

它在 Open Inventor 9.8 中为您工作,因为自 Open Inventor 10 以来修复了一个错误。希望这对您有所帮助,并让我们知道问题是否已为您解决。

今后,如有任何问题,请随时联系 Open Inventor 支持 (FRBOR.3d_hotline@thermofisher.com)。

于 2020-02-26T19:46:41.823 回答
2

通过邮件,Open Inventor Support 提出了另一个同样有效的解决方案:

代替

SoSeparator* localRoot = new SoSeparator;

SoGroup* localRoot = new SoGroup;
于 2020-02-27T15:38:15.693 回答