opengl glsl for循环中使用texture2D导致的问题及for循环注意事项

通常情况下,for循环中使用texture2D,不会出现什么问题,但是安卓的情况比较复杂,部分机型还是出现了问题。现把问题记录一下,可能对其他人有帮助。

  • 机型 : nova 7 5G

  • 系统 : Android 10.0

  • 现象 : 画面闪屏

  • 问题代码段

    ... ...
    for(int i=radius;i>=1;i--)
    {
    	vec2 curBottomCoordinate    = uv+vec2(0.0, float(i))*unit_uv;
    	vec2 curTopCoordinate       = uv+vec2(0.0, float(-i))*unit_uv;
    	sum+=texture2D(Img,curBottomCoordinate).rgb*t_weight[i];
    	sum+=texture2D(Img,curTopCoordinate).rgb*t_weight[i];
    	sum_weight+=t_weight[i]*2.0;
    }
    ... ...
    
  • 问题反馈
    shader不符合spec,提示 “ Some texture functions (non-“Lod” and non-“Grad” versions) may require implicit derivatives. Implicit derivatives are undefined within non-uniform control flow and for vertex texture fetches”

  • 知识点备忘
    以下摘自参考文献,仅供参考。并不是所有平台均不适用。
    在使用 for 循环时也有一些约束,如循环变量的值必须是编译时已知。如下:

    for (int i = 0; i < 3; i++) {
    	sum += i;
    }
    

    在 GLSL 中使用循环时一定要注意:只有一个循环变量,循环变量必须使用简单的语句来增减(如 i++, i–, i+=constant, i-=constant等),循环终止条件也必须是循环变量和常量的简单比较,在循环内部不能改变循环变量的值。
    以下代码是 GLSL 中不支持的循环用法的示例(这里存在疑惑,并不是在所有平台都不适用):

    float myArr[4];
    for (int i = 0; i < 3; i++) {
      	// 错误, [ ]中只能为常量或 uniform 变量,不能为整数量变量(如:i,j,k)
    	sum += myArr[i]; 
    }
    ...
    uniform int loopIter;
    // 错误, 循环变量 loopIter 的值必须是编译时已知
    for (int i = 0; i < loopIter; i++) {
    	sum += i;
    }
    

参考:
GLSL 详解(基础篇)

要实现OpenGL扇形纵向循环扫描效果,可以使用GLSL shader来编写。 首先,我们需要在vertex shader传递顶点坐标和纹理坐标,然后在fragment shader计算扇形的颜色。 以下是一个例子: Vertex Shader: ``` #version 330 layout (location = 0) in vec3 position; layout (location = 1) in vec2 texCoord; out vec2 TexCoord; void main() { gl_Position = vec4(position, 1.0); TexCoord = texCoord; } ``` Fragment Shader: ``` #version 330 in vec2 TexCoord; out vec4 FragColor; uniform sampler2D textureSampler; uniform float time; uniform float speed; uniform float radius; void main() { vec2 center = vec2(0.5, 0.5); float dist = length(TexCoord - center); float angle = atan(TexCoord.y - center.y, TexCoord.x - center.x); float offset = time * speed; float sectorAngle = 2.0 * acos(radius / dist); float sectorIndex = floor((angle + offset) / sectorAngle); float sectorStart = (sectorIndex * sectorAngle - offset); float sectorEnd = ((sectorIndex + 1.0) * sectorAngle - offset); float sectorMid = (sectorStart + sectorEnd) / 2.0; float sectorWidth = abs(sectorEnd - sectorStart); float sectorDist = abs(dist - radius); float sectorIntensity = 1.0 - smoothstep(0.0, sectorWidth, sectorDist); vec4 texColor = texture(textureSampler, TexCoord); FragColor = vec4(texColor.rgb * sectorIntensity, texColor.a); } ``` 在这个shader,我们使用了time和speed来控制扇形的扫描速度,radius来控制扇形的半径大小。我们使用了floor函数来计算当前像素所处的扇形的编号,根据编号计算出该扇形的起始角度和结束角度。然后,我们计算出该像素距离扇形心的距离和该像素所处的角度,根据这些信息来计算该像素所处扇形的颜色强度。 注意,在使用这个shader时,需要将time和speed的值不断更新,并且将radius设置为一个合适的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值