我刚刚将代码改为使用单独的着色器,而不是传递布尔型统一变量来决定使用哪种算法。不幸的是,在进行了大量测试之后,我发现其中一个属性(halo)没有被传递到新的着色器中。然而,它使用的另一个属性(position) 已经 被传递了。
以下是缩略代码:
Java code:
// Attributes
protected static int position = 0;
protected static int colour = 1;
protected static int texture = 2;
protected static int halo = 3;
protected static int normal = 4;
protected static int program1;
protected static int program2;
...
// Linking shader1
GLES20.glBindAttribLocation(program1, position, "position");
GLES20.glBindAttribLocation(program1, colour, "colour");
GLES20.glBindAttribLocation(program1, texture, "texCoord");
GLES20.glBindAttribLocation(program1, normal, "normal");
GLES20.glLinkProgram(program1);
...
// Linking shader2
GLES20.glBindAttribLocation(program2, position, "position");
GLES20.glBindAttribLocation(program2, halo, "halo");
GLES20.glLinkProgram(program2);
...
GLES20.glUseProgram(program1);
GLES20.glVertexAttribPointer(
position,
3,
GLES20.GL_FLOAT,
false,
0,
buffer);
...
//Render with program1
...
GLES20.glUseProgram(program2);
GLES20.glVertexAttribPointer(
halo,
1,
GLES20.GL_FLOAT,
false,
0,
doHaloBuffer);
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glDisable(GLES20.GL_DEPTH_TEST);
...
// Using lines for testing purposes
GLES20.glDrawElements(GLES20.GL_LINE_LOOP, haloIndexCount, GLES20.GL_UNSIGNED_SHORT, haloIndexBuffer);
...
片段着色器只是简单的“渲染纹理和颜色”的着色器。
shader1.vsh:
attribute vec3 position;
attribute vec4 colour;
attribute vec2 texCoord;
attribute vec3 normal;
...
varying vec2 fragTexCoord;
varying vec4 fragColour;
...
// All attributes used at some point
shader2.vsh:
attribute vec3 position;
attribute float halo;
varying vec4 fragColour;
...
vec4 colour = vec4(1.0, 1.0, 0.0, 1.0);
if(halo > 0.5){
colour.g = 0.0;
...
}
fragColour = colour;
...
如果我将
halo > 0.5
更改为halo == 0.0
,或者交换上述语句中的绿色值,就会渲染红色,否则会渲染黄色。我尝试将输入缓冲区更改为全1.0进行测试,但没有任何变化。似乎halo没有被传递。以前,我将两个着色器合并,并使用布尔统一变量来决定运行哪个代码,它可以正常工作。没有其他变化;输入缓冲区相同,计数相同,只是现在使用了不同的分离着色器。有什么想法吗?
program
时设置了属性两次。但是你使用的program1
和program2
没有设置任何属性... - Chris Dodd