我可以在哪里更改着色器制服并调用 GL 函数? [英] Where i can change shader uniforms and call GL Functions?

查看:90
本文介绍了我可以在哪里更改着色器制服并调用 GL 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想用 GLSurfaceView 编写简单的 android 应用程序,但我有一个问题.我写了一个简单的着色器程序蚂蚁把一个浮点参数放在那里.这是我的

I am just trying to write simple android app with GLSurfaceView but i have one problem. I wrote simple shader program ant put there one float parameter. This is my

顶点着色器:

        attribute vec2 vPosition;
        attribute vec2 vTexCoord;
        varying vec2 texCoord;

        void main(){
           texCoord = vTexCoord;
           gl_Position = vec4 ( vPosition.x, vPosition.y, 0.0, 1.0 );
        }

片段着色器:

        precision mediump float;
        uniform samplerExternalOES sTexture;
        uniform float myParam;
        varying vec2 texCoord;

        void main(){
          gl_FragColor = texture2D(sTexture,texCoord);
          gl_FragColor.r = gl_FragColor.r*myParam.x;
        }

一切都很好.

我获得了访问 myParam 的句柄(int paramHandle - 我的班级成员.):

I got the handle to have access to myParam (int paramHandle - member of my class.):

        public void onSurfaceCreated(....){
          ....
          paramHandle= GLES20.glGetUniformLocation(shaderProgram,"myParam");
          ....
        }

现在我想改变 myParam 并观察屏幕上的图像会发生什么.对于这个任务,我需要调用:

Now i want to variate myParam and watch what happens whit the image on the screen. For this task i need to call:

 GLES20.glUseProgram(myShaderProgram);
 GLES20.glUniform1f(paramHandle, new_value);

所以:如果我尝试更改这些函数中的 myParam 值:

So: if i try to change myParam value inside on of these functions:

onSurfaceCreated(...)、onSurfaceChanged(...) 或在 onDrawFrame(...)

我可以在屏幕上看到这个变化,所以我觉得 myParam 值被改变了.

i can see this changes on the screen, so i feel that myParam value was changed.

但是如果我想使用按钮设置 maParam 值,例如在单击按钮时调用函数 setMyParamValue:

    public void setMyParamValue(float newValue){
      GLES20.glUseProgram(myShaderProgram);
      GLES20.glUniform1f(paramHandle, new_value);
    }

好像什么都没发生.

有人可以解释一下它是如何工作的吗?为什么我不能在我的函数中设置统一值?

Can someone explain me how it works? Why i cannot set uniform value inside my function?

推荐答案

呈现发生在与传递 UI 事件的线程不同的线程上.openGL 调用应该只发生在渲染线程中.

Rendering happens on the different thread than where the UI events are passed from. openGL calls should only happen from the render thread.

您应该将这些调用转发到传递给 queueEvent 的可运行对象:

You should forward those calls to a runnable that you pass to queueEvent:

public void setMyParamValue(final float newValue){
    queueEvent(new Runnable(){
        public void run(){
           GLES20.glUseProgram(myShaderProgram);
           GLES20.glUniform1f(paramHandle, new_value);
        }
    });
}

run() 中的代码将在渲染线程中执行.

The code inside run() will get executed in the render thread.

这篇关于我可以在哪里更改着色器制服并调用 GL 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆