【问题标题】:access arrays in shader in OpenGL ES2.0在 OpenGL ES2.0 的着色器中访问数组
【发布时间】:2013-09-24 15:48:40
【问题描述】:

这是一个很奇怪的问题!!应该很容易解决。

我所做的只是遍历一个数组,然后将数组数据相加(类似这样)。

float kernel[] = float[5] (1.0, 1.0, 1.0, 1.0,1.0);
for(int i=-2;i<=2;i++) {            
    for(int j=-2; j<=2; j++){           
        color += kernel[0] * texture2D(image,  outUV);
    }
} 

上面的代码不起作用,但是如果将内核[0]更改为1.0,那将起作用。

float kernel[] = float[5] (1.0, 1.0, 1.0, 1.0,1.0);
for(int i=-2;i<=2;i++) {            
    for(int j=-2; j<=2; j++){           
        color +=  1.0 * texture2D(image,  outUV);
    }
} 

所以我想当我访问数组时有一些问题!!!为什么?

【问题讨论】:

  • 您可以使用一个vec4 并访问其x,y,z,w 值。

标签: android opengl-es-2.0


【解决方案1】:

我发现了问题,在声明它时似乎我无法分配数组值。我必须写一些这样的代码:

float kernel[5] ;

void main(){
    kernel[0]=1.0;
    kernel[1]=2.0;
    ...
    for(int i=-2;i<=2;i++) {            
      for(int j=-2; j<=2; j++){           
         color +=  kernel[i+2]*kernel[j+2] * texture2D(image,  outUV);
      } 
    } 
 }

【讨论】:

  • 但是等等,你没有在代码中使用color += kernel[0]...,还有color += 1.0...,请检查你的代码摘录。
  • @keaukraine 感谢您的回答,我接受我自己的回答,直到明天。
【解决方案2】:

或者,您可以使用单个 vec4 并访问其 x,y,z,w 值。

我猜这是片段着色器,所以它无法正常工作的最可能原因可能是浮点类型缺乏精度。在片段着色器中,您必须明确指定浮点类型的精度:

precision mediump float;

您能否更详细地描述着色器的不当行为?此外,完整的着色器代码可能会对您的问题有所帮助。

【讨论】:

    猜你喜欢
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多