1

我的主要活动中有一个搜索栏,我希望将其用作 GLSurfaceView 中片段着色器中控件的变量。

在 GLSurfaceView 视图中的 Activity 和 glGetUniformLocation 之间共享搜索栏值/变量的最佳方法是什么。

简单的片段着色器:

precision mediump float; 
uniform sampler2D u_Texture0;
varying vec2 v_TexCoordinate; 

uniform float myOffsetVariable;

void main() 
{ 
 vec4 baseColor;
 baseColor = texture2D(u_Texture0, v_TexCoordinate);
 vec4 offsetColor = baselcolor * myOffsetVariable;
 gl_FragColor = vec4(offsetColor,1.0);
}

主要活动:

public class GLActivity extends Activity {
    GLSurfaceView mView;
    private float mOffset = 0.0f;
    private SeekBar mOffsetSeekBar;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);   
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);
    mView = (GLSurfaceView)findViewById(R.id.glview);
    mView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    mView.setZOrderOnTop(true);
    mView.setEGLContextClientVersion(2);
    mView.setRenderer(new GLLayer(this));


    mOffsetSeekBar = (SeekBar)findViewById(R.id.inOffset);
    final TextView OffsetSeekBarValue = (TextView)findViewById(R.id.OffsetSeekBarValue);
    mOffsetSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){ 
           @Override 
           public void onProgressChanged(SeekBar seekBar, int progress, 
             boolean fromUser) { 
            // TODO Auto-generated method stub 
            OffsetSeekBarValue.setText(String.valueOf(progress));
            mOffset= (float)progress;
           } 
           @Override 
           public void onStartTrackingTouch(SeekBar seekBar) { 
            // TODO Auto-generated method stub 
           } 
           @Override 
           public void onStopTrackingTouch(SeekBar seekBar) { 
            // TODO Auto-generated method stub 
           } 
    });
    mOffsetSeekBar.setProgress(50);
    }
    }
    .......

GLSurfaceView 类

public class GLLayer  implements GLSurfaceView.Renderer {
private float mOffset = (float) 0.0;
....
@Override
public void onDrawFrame(GL10 glUnused ) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    final String vertexShader = getVertexShader();
    final String fragmentShader = getFragmentShader();

    final int vertexShaderHandle = ShaderHelper.compileShader(
            GLES20.GL_VERTEX_SHADER, vertexShader);
    final int fragmentShaderHandle = ShaderHelper.compileShader(
            GLES20.GL_FRAGMENT_SHADER, fragmentShader);

    mProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle,
            fragmentShaderHandle, new String[] { "a_Position",
                    "a_TexCoordinate" });

    // Set our per-vertex lighting program.
    GLES20.glUseProgram(mProgramHandle);

    // capture values from seek bar variables SOP & Sat

    // TODO Auto-generated method stub 


    int mOffsetVariable = GLES20.glGetUniformLocation( mProgramHandle, "myOffsetVariable" );
    GLES20.glUniform1f(mOffsetVariable, mOffset);


    // Set program handles for cube drawing.
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle,
            "u_MVPMatrix");
    mTextureUniformHandle0 = GLES20.glGetUniformLocation(mProgramHandle,
            "u_Texture0");
    mTextureUniformHandle1 = GLES20.glGetUniformLocation(mProgramHandle,
            "u_Texture1");
    mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle,
            "a_Position");
    mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle,
            "a_TexCoordinate");

    /**
     * First texture map
     */
    // Set the active texture0 unit to texture unit 0.
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

    // Bind the texture to this unit.
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle0);

    // Tell the texture uniform sampler to use this texture in the shader by
    // binding to texture unit 0.
    GLES20.glUniform1i(mTextureUniformHandle0, 0);

    /**
     * Second texture map
     */
    // Set the active texture1 unit to texture unit 1.
    GLES20.glActiveTexture(GLES20.GL_TEXTURE1);

    // Bind the texture to this unit.
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle1);

    // Tell the texture uniform sampler to use this texture in the shader by
    // binding to texture unit 1.
    GLES20.glUniform1i(mTextureUniformHandle1, 1);

    // Draw some cubes.
    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -3.2f);
    Matrix.rotateM(mModelMatrix, 0, 0.0f, 1.0f, 1.0f, 0.0f);
    drawCube();
} ......

谢谢,让我知道是否需要提供更多信息。一个

4

0 回答 0