GLSL ES1.0 并将纹理单元索引传递给片段着色器?
GLSL ES1.0 and passing texture-unit indices to the fragment shader?
正在尝试将 vertex/frag 着色器从 glsl 330 转换为 glsl es1.0
(基本上退后一步,因为原始应用程序是为桌面版本的 OpenGL3.0 编写的,但 webGL2.0 仍然没有得到某些浏览器的完全支持,如 IE 或 Safari;据我所知)。
我知道 1.0 使用的是 attribute/varying 而不是 in/out,但我遇到了一个问题不能使用带有变化的整数。有一个每顶点整数值数组,表示该顶点的纹理单元索引。我看不到将该信息传达给片段着色器的方法。如果我将值作为浮点数发送,它将开始插值。对吗?
#version 330 //for openGL 3.3
//VERTEX shader
//---------------------------------------------------------------------------------
//uniform variables stay constant for the whole glDraw call
uniform mat4 ProjViewModelMatrix;
uniform mat4 NormalsMatrix;
uniform vec4 DefaultColor;
uniform vec4 LightColor;
uniform vec3 LightPosition;
uniform float LightIntensity;
uniform bool ExcludeFromLight;
//---------------------------------------------------------------------------------
//non-uniform variables get fed per vertex from the buffers
layout (location=0) in vec3 VertexCoord;
layout (location=1) in vec4 VertexColor;
layout (location=2) in vec3 VertexNormal;
layout (location=3) in vec2 VertexUVcoord;
layout (location=4) in int vertexTexUnit;
//---------------------------------------------------------------------------------
//Output variables to fragment shader
out vec4 thisColor;
out vec2 vertexUVcoord;
flat out int TexUnitIdx; // <------ PROBLEM
out float VertLightIntensity;
//---------------------------------------------------------------------------------
void main ()
{ /* ... blah ... */ }
需要翻译的伴随片段着色器如下所示
#version 330 //for openGL 3.3
//FRAGMENT shader
//---------------------------------------------------------------------------------
//uniform variables
uniform bool useTextures; //If no textures, don't bother reading the TextureUnit array
uniform vec4 AmbientColor; //Background illumination
uniform sampler2D TextureUnit[6]; //Allow up to 6 texture units per draw call
//---------------------------------------------------------------------------------
//non-uniform variables
in vec2 vertexUVcoord;
in vec4 thisColor;
flat in int TexUnitIdx; // <------ PROBLEM
in float VertLightIntensity;
//---------------------------------------------------------------------------------
//Output color to graphics card
out vec4 pixelColor;
//---------------------------------------------------------------------------------
void main ()
{ /* ... blah ... */ }
GLSL ES 1.0 中没有基于整数的属性
您当然可以传入浮点数(并以无符号字节形式提供)。调用 gl.vertexAttribPointer
时传入 false
作为规范化标志
另一方面,GLSL ES 1.0 和 GLSL ES 3.00 都不允许索引采样器数组。
来自spec
12.30 Dynamic Indexing
...
Indexing of arrays of samplers by constant-index-expressions is supported in GLSL ES 1.00. A constant-index-expression
is an expression formed from constant-expressions and certain loop indices, defined for
a subset of loop constructs. Should this functionality be included in GLSL ES 3.00?
RESOLUTION: No. Arrays of samplers may only be indexed by constant-integral-expressions.
"Should this functionality be included in GLSL ES 3.00?" 意味着 GLES ES 3.00
中应该包含采样器的 动态索引
我引用了 GLSL ES 3.00 规范,因为它也引用了 GLSL ES 1.0 规范。
因此,您必须编写代码,使您的独立代码成为常量索引表达式。
attribute float TexUnitNdx;
...
uniform sampler2D TextureUnit[6];
vec4 getValueFromSamplerArray(float ndx, vec2 uv) {
if (ndx < .5) {
return texture2D(TextureUnit[0], uv);
} else if (ndx < 1.5) {
return texture2D(TextureUnit[1], uv);
} else if (ndx < 2.5) {
return texture2D(TextureUnit[2], uv);
} else if (ndx < 3.5) {
return texture2D(TextureUnit[3], uv);
} else if (ndx < 4.5) {
return texture2D(TextureUnit[4], uv);
} else {
return texture2D(TextureUnit[5], uv);
}
}
vec4 color = getValueFromSamplerArray(TexUnitNdx, someTexCoord);
或类似的东西。将你的 ifs 安排成二进制搜索可能会更快。
正在尝试将 vertex/frag 着色器从 glsl 330 转换为 glsl es1.0
(基本上退后一步,因为原始应用程序是为桌面版本的 OpenGL3.0 编写的,但 webGL2.0 仍然没有得到某些浏览器的完全支持,如 IE 或 Safari;据我所知)。
我知道 1.0 使用的是 attribute/varying 而不是 in/out,但我遇到了一个问题不能使用带有变化的整数。有一个每顶点整数值数组,表示该顶点的纹理单元索引。我看不到将该信息传达给片段着色器的方法。如果我将值作为浮点数发送,它将开始插值。对吗?
#version 330 //for openGL 3.3
//VERTEX shader
//---------------------------------------------------------------------------------
//uniform variables stay constant for the whole glDraw call
uniform mat4 ProjViewModelMatrix;
uniform mat4 NormalsMatrix;
uniform vec4 DefaultColor;
uniform vec4 LightColor;
uniform vec3 LightPosition;
uniform float LightIntensity;
uniform bool ExcludeFromLight;
//---------------------------------------------------------------------------------
//non-uniform variables get fed per vertex from the buffers
layout (location=0) in vec3 VertexCoord;
layout (location=1) in vec4 VertexColor;
layout (location=2) in vec3 VertexNormal;
layout (location=3) in vec2 VertexUVcoord;
layout (location=4) in int vertexTexUnit;
//---------------------------------------------------------------------------------
//Output variables to fragment shader
out vec4 thisColor;
out vec2 vertexUVcoord;
flat out int TexUnitIdx; // <------ PROBLEM
out float VertLightIntensity;
//---------------------------------------------------------------------------------
void main ()
{ /* ... blah ... */ }
需要翻译的伴随片段着色器如下所示
#version 330 //for openGL 3.3
//FRAGMENT shader
//---------------------------------------------------------------------------------
//uniform variables
uniform bool useTextures; //If no textures, don't bother reading the TextureUnit array
uniform vec4 AmbientColor; //Background illumination
uniform sampler2D TextureUnit[6]; //Allow up to 6 texture units per draw call
//---------------------------------------------------------------------------------
//non-uniform variables
in vec2 vertexUVcoord;
in vec4 thisColor;
flat in int TexUnitIdx; // <------ PROBLEM
in float VertLightIntensity;
//---------------------------------------------------------------------------------
//Output color to graphics card
out vec4 pixelColor;
//---------------------------------------------------------------------------------
void main ()
{ /* ... blah ... */ }
GLSL ES 1.0 中没有基于整数的属性
您当然可以传入浮点数(并以无符号字节形式提供)。调用 gl.vertexAttribPointer
false
作为规范化标志
另一方面,GLSL ES 1.0 和 GLSL ES 3.00 都不允许索引采样器数组。
来自spec
12.30 Dynamic Indexing
...
Indexing of arrays of samplers by constant-index-expressions is supported in GLSL ES 1.00. A constant-index-expression is an expression formed from constant-expressions and certain loop indices, defined for a subset of loop constructs. Should this functionality be included in GLSL ES 3.00?
RESOLUTION: No. Arrays of samplers may only be indexed by constant-integral-expressions.
"Should this functionality be included in GLSL ES 3.00?" 意味着 GLES ES 3.00
中应该包含采样器的 动态索引我引用了 GLSL ES 3.00 规范,因为它也引用了 GLSL ES 1.0 规范。
因此,您必须编写代码,使您的独立代码成为常量索引表达式。
attribute float TexUnitNdx;
...
uniform sampler2D TextureUnit[6];
vec4 getValueFromSamplerArray(float ndx, vec2 uv) {
if (ndx < .5) {
return texture2D(TextureUnit[0], uv);
} else if (ndx < 1.5) {
return texture2D(TextureUnit[1], uv);
} else if (ndx < 2.5) {
return texture2D(TextureUnit[2], uv);
} else if (ndx < 3.5) {
return texture2D(TextureUnit[3], uv);
} else if (ndx < 4.5) {
return texture2D(TextureUnit[4], uv);
} else {
return texture2D(TextureUnit[5], uv);
}
}
vec4 color = getValueFromSamplerArray(TexUnitNdx, someTexCoord);
或类似的东西。将你的 ifs 安排成二进制搜索可能会更快。