嗨,我是GLSL的新手,我遇到了一些問題。GLSL 2.0顏色和紋理
我想創建一對GLSL着色器來使用顏色或紋理,但我必須做錯了什麼。 問題是,如果將uUseTexture設置爲0(應該指示顏色),它不起作用(對象不着色)。我知道着色代碼單獨工作,任何暗示爲什麼它不能使用if語句?
下面是代碼:
//片段
precision mediump float;
uniform int uUseTexture;
uniform sampler2D uSampler;
varying vec4 vColor;
varying vec2 vTextureCoord;
void main(void) {
if(uUseTexture == 1) {
gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
} else {
gl_FragColor = vColor;
}
}
//頂點
attribute vec3 aVertexPosition;
attribute vec4 aVertexColor;
attribute vec2 aTextureCoord;
uniform int uUseTexture;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec4 vColor;
varying vec2 vTextureCoord;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
if(uUseTexture == 1) {
vTextureCoord = aTextureCoord;
} else {
vColor = aVertexColor;
}
這個'vec2(vTextureCoord.s,vTextureCoord.t)'應該是'vTextureCoord.st'。你應該看看GLSL中的[swizzle masks]是如何工作的(http://www.opengl.org/wiki/GLSL_Types#Swizzling)。 – 2012-02-26 21:40:06
你的意思是說,如果你用'if(true)'替換'if(uUseTexture == 1)',那麼相同的着色器代碼可以工作嗎? – rotoglup 2012-02-26 21:43:20