相同的着色器在Nvidia和ATI卡上表现不同

时间:2014-05-01 22:11:39

标签: c++ qt opengl glsl ati

我和一位朋友正在开发一个编辑器(类似CAD),用于我们未来的游戏。 我们正在使用Qt框架和OpenGL。 我们遇到的问题是,在他的带有集成nVidia卡的笔记本电脑上,阴影正在按预期工作并呈现良好状态。在带有集成ATI卡的笔记本电脑上,以及带有Radeon HD5850的桌面上,phong照明的表现略有不同。有更多的亮点和黑点,图像看起来不太好。此外,我们使用香椿着色器在边缘周围绘制轮廓并限制颜色可以具有的阴影量。

香椿着色器使用2次渲染 - 第一次渲染将对象渲染为黑色,略大于原始对象(稍微偏移其法线方向上的每个顶点)以制作轮廓,然后第二次渲染使对象正常渲染(仅限制)阴影谱,所以它看起来更像漫画。)

我们的2台电脑上的图像是一样的。我上面提到的第一个区别,第二个区别是剪影在我的朋友计算机上应该伸展,所以它在对象周围形成一个均匀的轮廓,但在我的计算机上略微向上移动,在顶部,底部没有线。

另一个是phong照明,照亮了编辑对象的立方体。再次,在我的朋友计算机上渲染得很好,但我的几乎全黑或全白。

第一张图片(nVidia卡):

Monkey1

第二张图片(ATI卡):

Monkey2

我知道代码很长,可能问题出现在一些Qt设置中,而不是在着色器中,但是如果你看到任何让你觉得不好的东西,请回答。

phong着色的代码如下

    #version 400

    in vec4 aVertex;
    in vec4 aNormal;
    in vec2 aTexCoord;

    uniform mat4 uPVM;
    uniform mat4 uViewModel;
    uniform mat4 uNormal;
    uniform int  uLightsOn;

    out vec2 vTexCoord;
    out vec3 vNormal;
    flat out vec3 mEye;
    flat out vec3 mLightDirection;

    flat out vec4 mAxisColor;

    void main(void)
    {
        if(uLightsOn == 1) {
            mEye = (uViewModel * aVertex).xyz;
            mLightDirection = vec4(2.0,-2.0,1.0,0.0).xyz;
            vNormal = (uNormal * aNormal).xyz;
        }

        gl_Position = uPVM * aVertex;
        vTexCoord = aTexCoord;
        mAxisColor = aNormal;
    }

phong片段着色器:

    #version 400

    uniform sampler2D uTexture0;
    uniform int  uLightsOn;
    uniform vec3 uHighlightColor;
    uniform int uTextured;
    uniform int uAxisRender;

    in vec2 vTexCoord;
    in vec3 vNormal;
    flat in vec3 mEye;
    flat in vec3 mLightDirection;

    out vec4 fragColor;

    flat in vec4 mAxisColor;

    struct TMaterial {
        vec4 diffuse;
        vec4 ambient;
        vec4 specular;
        float shininess;
    };

    TMaterial material;

    void setup() {
        // setupMaterials

        material.ambient = vec4(0.4);
        material.diffuse = vec4(0.9);
        material.specular = vec4(0.0);
        material.shininess = 0.3;

    }

    void main(void)
    {
        setup();

        vec3 finalHighlightColor = uHighlightColor;

        if(finalHighlightColor.x <= 0.0) finalHighlightColor.x = 0.1;
        if(finalHighlightColor.y <= 0.0) finalHighlightColor.y = 0.1;
        if(finalHighlightColor.z <= 0.0) finalHighlightColor.z = 0.1;

        if(uLightsOn == 0) {
            if(uAxisRender == 1) fragColor = mAxisColor;
            else fragColor = vec4(finalHighlightColor,1.0);
            return;
        }

        vec4 diffuse;
        vec4 spec = vec4(0.0);
        vec4 ambient;

        vec3 L = normalize(mLightDirection - mEye);
        vec3 E = normalize(-mEye);
        vec3 R = normalize(reflect(-L,vNormal));

        ambient = material.ambient;

        float intens = max(dot(vNormal,L), 0.0);

        diffuse = clamp( material.diffuse * intens  , 0.0, 1.0 );
        if(intens > 0.0) spec = clamp ( material.specular * pow(max(dot(R,E),0.0),material.shininess) , 0.0, 1.0 );

        if(uTextured == 1) fragColor = (ambient + diffuse + spec) * texture(uTexture0,vTexCoord);
        else fragColor = (ambient + diffuse + spec) * vec4(finalHighlightColor,1.0);

    }

香椿着色器:

    #version 400

    in vec4 aVertex;
    in vec4 aNormal;
    in vec2 aTexCoord;

    uniform mat4 uPV;
    uniform mat4 uM;
    uniform mat4 uN;
    uniform vec3 uLightPosition;
    uniform vec3 uCameraPosition;
    uniform int  uSilhouetteMode;
    uniform float uOffset;
    // if this uniform is passed, all the toon rendering is going off and only simple axis are rendered
    // last data in aNormal are colors of those axis if everything was ser properly.
    uniform int  uAxisRendering;
    flat out vec4 fAxisColor;

    out vec4 vNormal;
    out vec2 vTexCoord;
    out vec3 vDirectionToCamera;
    out vec3 vDirectionToLight;


    void silhouetteMode() {
        gl_Position = uPV * uM * vec4(aVertex.xyz + aNormal.xyz * uOffset,1.0f);
    }

    void toonMode() {
        vec4 worldPosition = uM * aVertex;

        vDirectionToCamera = uCameraPosition - worldPosition.xyz;
        vDirectionToLight = uLightPosition - worldPosition.xyz;
        vNormal = uN * aNormal;

        gl_Position = uPV * worldPosition;
    }

    void axisMode() {
        fAxisColor = aNormal;

        gl_Position = uPV * uM * aVertex;
    }

    void main(void)
    {
        vTexCoord = aTexCoord;

        if(uSilhouetteMode == 1) {
            silhouetteMode();
        } else {
            if(uAxisRendering == 1) axisMode();
            else toonMode();
        }
    }

和片段着色器

    #version 400

    uniform sampler2D uTexture;
    uniform vec3 uBaseColor;
    uniform float uNumShades;
    uniform int  uSilhouetteMode;

    uniform int  uAxisRendering;
    flat in vec4 fAxisColor;

    in vec4 vNormal;
    in vec2 vTexCoord;
    in vec3 vDirectionToCamera;
    in vec3 vDirectionToLight;

    out vec4 outFragColor;

    void main(void)
    {

        if(uSilhouetteMode == 1) {
            outFragColor = vec4(uBaseColor,1.0);
            return;
        }

        if(uAxisRendering == 1) {
            outFragColor = fAxisColor;
            return;
        }

        float l_ambient = 0.1;
        float l_diffuse = clamp(dot(vDirectionToLight,vNormal.xyz),0.0,1.0);
        float l_specular;
        vec3 halfVector = normalize(vDirectionToCamera + vDirectionToLight);

        if(dot(vDirectionToLight,vNormal.xyz) > 0.0) {
            l_specular = pow(clamp(dot(halfVector,vNormal.xyz),0.0,1.0),64.0);
        } else {
            l_specular = 0.0;
        }

        float intensity = l_ambient + l_diffuse + l_specular;
        float shadeIntesity = ceil(intensity * uNumShades)/ uNumShades;

        outFragColor = vec4(texture(uTexture,vTexCoord).xyz * shadeIntesity * uBaseColor,1.0);
    }

最后,我们的OpenGLWindow初始化(在Qt中)

    OpenGLWindow::OpenGLWindow(QWindow *parent) :
        QWindow(parent),m_animating(false), m_initialized(false), m_animationTimer(NULL)
    {
        setSurfaceType(QWindow::OpenGLSurface);

        QSurfaceFormat format;
        format.setDepthBufferSize( 24 );
        format.setMajorVersion( 4 );
        format.setMinorVersion( 0 );
        format.setSamples( 4 );
        format.setProfile( QSurfaceFormat::CoreProfile );
        setFormat( format );
        create();

        if(!m_context) {
            m_context = new QOpenGLContext(this);
            m_context->setFormat(requestedFormat());
            m_context->create();
            m_context->makeCurrent(this);

            initializeOpenGLFunctions();
        }

        m_animationTimer = new QTimer(this);
        connect(m_animationTimer, SIGNAL(timeout()), this, SLOT(renderer()));
        m_animationTimer->setInterval(16);
    }

1 个答案:

答案 0 :(得分:0)

在我看来,nVidia图像似乎是使用alpha而AMD不是。我也看不到

format.setAlpha(true);
在您的Qt设置中

可能就是这样。