2015-09-14 448 views
1

我嘗試在OpenGL ES 2.0中編寫一個小程序。但我發現我很難檢查着色語言中的變量。如何在頂點着色器中打印浮點值

例如,我想知道頂點着色器中的值。我會將該值傳遞給片段着色器,並將其值設爲glFragColor中的紅色。但我發現它很難通過價值。如果我使用varying來聲明該值,那麼值將會改變。

這裏是代碼,日誌是我想打印的值。

public static final String VERTEX_SHADER = 
      "attribute vec4 position;\n" + 
        "attribute vec2 inputTextureCoordinate;\n" + 
        "\n" + 
        "uniform float texelWidthOffset; \n" + 
        "uniform float texelHeightOffset; \n" + 
        "\n" + 
        "varying vec2 centerTextureCoordinate;\n" + 
        "varying vec2 oneStepLeftTextureCoordinate;\n" + 
        "varying vec2 twoStepsLeftTextureCoordinate;\n" + 
        "varying vec2 oneStepRightTextureCoordinate;\n" + 
        "varying vec2 twoStepsRightTextureCoordinate;\n" + 
        "varying float log;\n" + 
        "\n" + 
        "void main()\n" + 
        "{\n" + 
        "log = -0.1;\n" + 
        "gl_Position = position;\n" + 
        "vec2 firstOffset;\n" + 
        "vec2 secondOffset;\n" + 
//     "if (sqrt(pow(position.x, 2) + pow(position.y, 2)) < 0.2) {\n" + 
//     "log = -position.x;\n" + 
        "if (position.x < 0.3) {\n" + 
        "log = 0.7;\n" + 
        "firstOffset = vec2(3.0 * texelWidthOffset, 3.0 * texelHeightOffset);\n" + 
        "secondOffset = vec2(3.0 * texelWidthOffset, 3.0 * texelHeightOffset);\n" + 
        "} else {\n" + 
        "firstOffset = vec2(texelWidthOffset, texelHeightOffset);\n" + 
        "secondOffset = vec2(texelWidthOffset, texelHeightOffset);\n" + 
        "log = -0.1;\n" + 
        "}\n" + 
        "\n" + 
        "centerTextureCoordinate = inputTextureCoordinate;\n" + 
        "oneStepLeftTextureCoordinate = inputTextureCoordinate - firstOffset;\n" + 
        "twoStepsLeftTextureCoordinate = inputTextureCoordinate - secondOffset;\n" + 
        "oneStepRightTextureCoordinate = inputTextureCoordinate + firstOffset;\n" + 
        "twoStepsRightTextureCoordinate = inputTextureCoordinate + secondOffset;\n" + 
        "}\n"; 





    public static final String FRAGMENT_SHADER = 
      "precision highp float;\n" + 
        "\n" + 
        "uniform sampler2D inputImageTexture;\n" + 
        "\n" + 
        "varying vec2 centerTextureCoordinate;\n" + 
        "varying vec2 oneStepLeftTextureCoordinate;\n" + 
        "varying vec2 twoStepsLeftTextureCoordinate;\n" + 
        "varying vec2 oneStepRightTextureCoordinate;\n" + 
        "varying vec2 twoStepsRightTextureCoordinate;\n" + 
        "varying float log;\n" + 
        "\n" + 
        "void main()\n" + 
        "{\n" + 
        "if (log != -0.1) {\n" + 
        "gl_FragColor.rgba = vec4(log, 0.0, 0.0, 1.0);\n" + 
//     "return;\n" + 
//     "}\n" + 
        "} else { \n" + 
        "lowp vec4 fragmentColor;\n" + 
        "fragmentColor = texture2D(inputImageTexture, centerTextureCoordinate) * 0.2;\n" + 
        "fragmentColor += texture2D(inputImageTexture, oneStepLeftTextureCoordinate) * 0.2;\n" + 
        "fragmentColor += texture2D(inputImageTexture, oneStepRightTextureCoordinate) * 0.2;\n" + 
        "fragmentColor += texture2D(inputImageTexture, twoStepsLeftTextureCoordinate) * 0.2;\n" + 
        "fragmentColor += texture2D(inputImageTexture, twoStepsRightTextureCoordinate) * 0.2;\n" + 
        "\n" + 
        "gl_FragColor = fragmentColor;\n" + 
//     "gl_FragColor.rgba = vec4(0.0, 1.0, 0.0, 1.0);\n" + 
//     "}\n" + 
        "}\n"; 

或者有沒有更好的方法來做到這一點。

回答

2

在比較浮點值,而不是這樣做:

if (log != -0.1)

您應該允許的值小三角形/公差佔浮點精度和最終值「改變」,你可能作爲變化傳遞它。

所以,你應該這樣做:

if (abs(log - (-0.1)) >= 0.0001)

這裏我選擇的0.0001是有點任意的......它是一個很小的值...

==另一個例子

相反的:

if (log == 0.7)

if (abs(log - 0.7) <= 0.0001)

但是這裏也可能有另外一個問題:

  1. 頂點着色器執行每3個頂點的所有三角形(或四邊形)
  2. 因此對於中您可以爲每個頂點設置不同的值(-0.10.7log
  3. 現在問題是,在片段着色器中,GPU將在3個日誌值之間進行插值,具體取決於它所呈現的像素......因此最終您可以在屏幕上顯示的[-0.1,0.7]間隔中獲得任何值: - (

爲了避免這種問題,我個人使用#ifdef在我的着色器能夠正常和調試模式之間切換他們,並與按鍵在兩者之間可以切換。我從不嘗試根據if測試混合正常和調試顯示,特別是當測試基於頂點位置時。

所以你的情況我會首先創建着色器的特定調試版本,然後用0.01.0作爲值log,這樣你會看到什麼是紅色的漸變,越紅的顏色,越靠近你是想要測試的情況。