2012-07-08 52 views
1

我是OpenGL ES 2.0的新手,所以請耐心等待......我想將BOOL標誌傳遞給我的片段着色器,以便在在我的應用程序中發生了某個觸摸事件,它以不同的方式渲染gl_FragColor。我嘗試過使用vec2屬性,並將「.x」值僞裝成我的「BOOL」,但它看起來像OpenGL將值從0.0減至1.0,然後着色器才能獲取它的值。所以即使在我的應用程序中,我已經將它設置爲0.0,而着色器在做它的事情,最終的值將達到1.0。任何建議將非常感激。在iOS/iPhone上發送布爾值到片段着色器OpenGL ES 2.0

VertexAttrib代碼:

// set up context, shaders, use program etc. 

[filterProgram addAttribute:@"inputBrushMode"]; 
inputBrushModeAttribute = [filterProgram attributeIndex:@"inputBrushMode"]; 

bMode[0] = 0.0; 
bMode[1] = 0.0; 

glVertexAttribPointer(inputBrushModeAttribute, 2, GL_FLOAT, 0, 0, bMode); 

當前頂點着色器代碼:

... 
attribute vec2 inputBrushMode; 
varying highp float brushMode; 

void main() 
{ 
    gl_Position = position; 
    ... 
    brushMode = inputBrushMode.x; 
} 

當前片段着色器代碼:

... 
varying highp float brushMode; 

void main() 
{ 
    if(brushMode < 0.5) { 
     // render the texture 
     gl_FragColor = texture2D(inputImageTexture, textureCoordinate); 
    } else { 
     // cover things in yellow funk 
     gl_FragColor = vec4(1,1,0,1); 
    } 
} 

提前致謝。

+2

「它看起來像在OpenGL的着色器之前,從0.0的值標準化爲1.0獲取它的阿霍德」首先,你爲什麼不使用統一?爲什麼使用頂點屬性?其次,您提供此屬性的設置代碼在哪裏?你正確使用'glVertexAttribPointer'嗎? – 2012-07-08 04:44:05

+0

我的印象是,制服的價值不能在以後改變,這是不是真的?我很確定我正確使用'glVertexAttribPointer' - 我將添加該代碼。謝謝。 – taber 2012-07-08 04:59:30

+0

您可以隨時更改統一值(除了在批次中間)。 – Tim 2012-07-08 05:21:51

回答

7

改爲創建bool作爲glUniform(1.0或0.0)。請用glUniform1f(GLint location, GLfloat v0)來設定它的值。在着色器,檢查它的價值,像這樣:

if (my_uniform < 0.5) { 
    // FALSE 
} else { 
    // TRUE 
} 
+4

爲什麼不只是'if(my_uniform == 0.0){' – 2013-04-29 20:33:35

+3

由於浮點精度。 – 2014-03-22 05:15:49

+1

如果手動將其設置爲0.0或1.0,則不應該遇到精度問題。如果你在通過數學運算之前對浮子進行數學運算,那就是當你處於舍入誤差的風險時。 – 2017-01-27 14:46:21

相關問題