John
John

Reputation: 727

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?

Upvotes: 0

Views: 190

Answers (2)

John
John

Reputation: 727

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

Upvotes: 1

keaukraine
keaukraine

Reputation: 5364

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.

Upvotes: 0

Related Questions