sunjinbo
sunjinbo

Reputation: 2187

How to bind a vec2&vec3 variable with OpengGL fragment shader?

I'm devloping a android OpenGL ES app, here is my fragment shader snippet:

 uniform sampler2D inputImageTexture;
 varying highp vec2 textureCoordinate;

 uniform lowp vec2 vignetteCenter;
 uniform lowp vec3 vignetteColor;
 uniform highp float vignetteStart;
 uniform highp float vignetteEnd;

 void main()
 {
     lowp vec4 sourceImageColor = texture2D(inputImageTexture, textureCoordinate);
     lowp float d = distance(textureCoordinate, vec2(vignetteCenter.x, vignetteCenter.y));
     lowp float percent = smoothstep(vignetteStart, vignetteEnd, d);
     gl_FragColor = vec4(mix(sourceImageColor.rgb, vignetteColor, percent), sourceImageColor.a);
 }

My problem is that I want to bind a variable with GLSL's vignetteCenter and vignetteColor above, I don't know whick kind of Java Type object correspond to vec2 and vec3? GLES20.glUniform2f or GLES20.glUniform2fv, which one should I use?

Upvotes: 1

Views: 1067

Answers (1)

Edd
Edd

Reputation: 1370

GLES20.glUniform2f will allow you to assign ONE vector of 2 floats to your uniform variable.

GLES20.glUniform2fv will allow you to assign N vectors of 2 floats to your uniform variable.

So, given two floats f1 and f2, you can either use

GLES20.glUniform2f(myVariablePosition,f1,f2);

or

float[] myVector = {f1,f2};
GLES20.glUniform2f(myVariablePosition,1,myVector);

where 1 there means only 1 vector of 2 floats is passed.

If you wanted to pass an array of vectors of 2 floats then you would do:

float[] myVector = {f1,f2,....,f(N*2)};
GLES20.glUniform2f(myVariablePosition,N,myVector);

Upvotes: 2

Related Questions