简体   繁体   中英

access arrays in shader in OpenGL ES2.0

This is a very strange problem!! and it should be easy to solve.

What I do is just go through a array, and add the array data together (something like this).

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);
    }
} 

The code above doesn't work, but if Change the kernel[0] to 1.0, that will work.

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);
    }
} 

So I guess there is some problem when I access the array!!! why?

I'v found the problem, it seems I cannot assign the array values when declare it. I have to write some code like this:

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);
      } 
    } 
 }

Alternatively, you can use a single vec4 and access its x , y , z , w values.

I guess this is fragment shader so the most possible cause of it not working may be lack of precision for float type. In fragment shader you must explicitly specify precision for float type:

precision mediump float;

Could you please describe misbehavior of shader in more detail? Also, full code of shader may shade some light on your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM