纹理上的OpenGL ES 2.0着色器不起作用 [英] OpenGL ES 2.0 Shader on Texture not working

查看:115
本文介绍了纹理上的OpenGL ES 2.0着色器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此网站复制了示例点击

I copied the example from this site click

在修复一些小问题并扩展着色器以达到我的目的后,它运行良好.现在,我想将纹理移动/平移到屏幕的右侧.

It is working well after fixing some minor things and extending the shader for my purpose. Now i want to move/translate the texture to the right side of the screen.

在这种情况下,我必须在代码和着色器中添加一个ModelViewProjection矩阵,对吗?

For that case i have to add a ModelViewProjection Matrix to my code and as well to the shader, right?

完成此操作后,纹理不再显示:-(我做错了什么?

After doing this the Texture is not showing up anymore :-( What am I doing wrong?

所以代码一直有效,直到我从以下位置更改着色器gl_position为止:

So the code is working until i change in my shader gl_position from :

gl_Position= a_positiongl_Position= a_position * ModelViewProjectionMatrix

没有错误,也没有纹理...

There is no error but also no texture...

ModelViewProjection的输出是:[0.0,0.0,0.0,0.0] ...为什么?

The output for the ModelViewProjection is : [0.0,0.0,0.0,0.0] ... why?

这是源代码:

活动:

//Activity
public class MainActivity extends Activity {
    private MainView mView;
    //private WakeLock mWL;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mView = new MainView(this);
        setContentView(mView);

    }

    @Override
    protected void onPause() {

        mView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mView.onResume();

    }
}

SurfaceView

SurfaceView

    //View
    class MainView extends GLSurfaceView {
        CameraRenderer mRenderer;

        MainView(Context context) {
            super(context);
            mRenderer = new CameraRenderer(this);
            setEGLContextClientVersion(2);
            setRenderer(mRenderer);
            //setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        }

        public void surfaceCreated(SurfaceHolder holder) {
            super.surfaceCreated(holder);
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            mRenderer.close();
            super.surfaceDestroyed(holder);
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            super.surfaceChanged(holder, format, w, h);
        }
    }

CameraRenderer:

CameraRenderer:

    public class CameraRenderer  implements GLSurfaceView.Renderer,
        SurfaceTexture.OnFrameAvailableListener {

    private final String vss = "uniform mat4 uMVPMatrix;\n"
            + "attribute vec2 vPosition;\n"
            + "attribute vec2 vTexCoord;\n"
            + ""
            + "varying vec2 texCoord;\n"
            + "vec4 Distort(vec4 p){                    \n"
            + " float BarrelPower = 1.0;            \n"
            + " vec2 v = p.xy / p.w;                \n"
            + "     float radius = length(v);           \n"
            + " if (radius > 0.0){                  \n"
            + "     float theta = atan(v.y,v.x);    \n"
            + "         radius = pow(radius, BarrelPower);\n"
            + "         v.x = radius * cos(theta);      \n"
            + "         v.y = radius * sin(theta);      \n"
            + "         p.xy = v.xy * p.w;              \n"
            + " }"
            + "                                         \n"
            + " return p;                               \n"
            + " }                                       \n"
            + "void main() {\n"

            +" vec4 a_position = vec4 ( vPosition.x, vPosition.y, 0.0, 1.0 );"
            + "vec4 test = uMVPMatrix * a_position; \n"
            + "texCoord = vTexCoord;\n"
            + "gl_Position = test;\n"
            + "}";

    private final String fss = "#extension GL_OES_EGL_image_external : require\n"
            + "precision mediump float;\n"
            + "uniform samplerExternalOES sTexture;\n"
            + "varying vec2 texCoord;\n" + "void main() {\n"
            + "  gl_FragColor = texture2D(sTexture,texCoord);\n" + "}";

    private int[] hTex;
    private FloatBuffer pVertexRight;
    private FloatBuffer pTexCoordRight;

    private int hProgram;

    private Camera mCamera;
    private SurfaceTexture mSTexture;

    private boolean mUpdateST = false;

    private MainView mView;

    private float[] mMVPMatrix = new float[16];
    private float[] mProjMatrix = new float[16];
    private float[] mMMatrix = new float[16];
    private float[] mVMatrix = new float[16];

    private int muMVPMatrixHandle;
    private int maPositionHandle;
    private int maTextureHandle;

    CameraRenderer(MainView view) {

        mView = view;

        float[] vtmpRight = { 0.35f, -0.35f, -0.35f, -0.35f, 0.35f, 0.35f,
                -0.35f, 0.35f };
        float[] ttmpRight = { 0.35f, 0.35f, 0.0f, 0.35f, 0.35f, 0.0f, 0.0f,
                0.0f };

        pVertexRight = ByteBuffer.allocateDirect(8 * 4)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        pVertexRight.put(vtmpRight);
        pVertexRight.position(0);
        pTexCoordRight = ByteBuffer.allocateDirect(8 * 4)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        pTexCoordRight.put(ttmpRight);
        pTexCoordRight.position(0);

    }

    public void close() {
        mUpdateST = false;
        mSTexture.release();
        mCamera.stopPreview();

        mCamera = null;
        deleteTex();
    }

    public void onSurfaceCreated(GL10 unused,
            javax.microedition.khronos.egl.EGLConfig config) {
        // String extensions = GLES20.glGetString(GLES20.GL_EXTENSIONS);
        // Log.i("mr", "Gl extensions: " + extensions);
        // Assert.assertTrue(extensions.contains("OES_EGL_image_external"));

        initTex();
        mSTexture = new SurfaceTexture(hTex[0]);
        mSTexture.setOnFrameAvailableListener(this);

        mCamera = Camera.open();
        try {
            mCamera.setPreviewTexture(mSTexture);

            Log.i("GLES20", "setPreviewTexture success");

        } catch (IOException ioe) {
        }

        GLES20.glClearColor(1.0f, 1.0f, 0.0f, 1.0f);

        hProgram = loadShader(vss, fss);



    }

    public void onDrawFrame(GL10 unused) {

        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

        synchronized (this) {
            if (mUpdateST) {
                mSTexture.updateTexImage();
                mUpdateST = false;
            }
        }

        GLES20.glUseProgram(hProgram);

        int ph = GLES20.glGetAttribLocation(hProgram, "vPosition");



        int tch = GLES20.glGetAttribLocation(hProgram, "vTexCoord");
        int th = GLES20.glGetUniformLocation(hProgram, "sTexture");

        muMVPMatrixHandle = GLES20.glGetUniformLocation(hProgram, "uMVPMatrix");



        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, hTex[0]);
        GLES20.glUniform1i(th, 0);

        GLES20.glVertexAttribPointer(ph, 2, GLES20.GL_FLOAT, false, 4 * 2,
                pVertexRight);

        GLES20.glVertexAttribPointer(tch, 2, GLES20.GL_FLOAT, false, 4 * 2,
                pTexCoordRight);
        GLES20.glEnableVertexAttribArray(ph);
        GLES20.glEnableVertexAttribArray(tch);

        Matrix.setRotateM(mMMatrix, 0, 0, 0, 0, 1.0f);
        Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
        Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv( muMVPMatrixHandle, 1, false, mMVPMatrix, 0 );
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

        // right
        GLES20.glFlush();

    }

    public void onSurfaceChanged(GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);

        Camera.Parameters param = mCamera.getParameters();

        List<Size> psize = param.getSupportedPreviewSizes();
        if (psize.size() > 0) {
            int i;
            for (i = 0; i < psize.size(); i++) {
                if (psize.get(i).width < width || psize.get(i).height < height)
                    break;
            }
            if (i > 0)
                i--;

            param.setPreviewSize(psize.get(i).width, psize.get(i).height);
            Log.i("mr", "ssize: " + psize.get(i).width + ", "
                    + psize.get(i).height);
        }



        mCamera.setParameters(param);
        mCamera.startPreview();



    }

    private void initTex() {
        hTex = new int[1];
        GLES20.glGenTextures(1, hTex, 0);
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, hTex[0]);
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
                GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
    }

    private void deleteTex() {
        GLES20.glDeleteTextures(1, hTex, 0);
    }

    public synchronized void onFrameAvailable(SurfaceTexture st) {

        mUpdateST = true;
        // mView.requestRender();
    }

    private static int loadShader(String vss, String fss) {
        int vshader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
        GLES20.glShaderSource(vshader, vss);
        GLES20.glCompileShader(vshader);
        int[] compiled = new int[1];
        GLES20.glGetShaderiv(vshader, GLES20.GL_COMPILE_STATUS, compiled, 0);
        if (compiled[0] == 0) {
            Log.e("Shader", "Could not compile vshader");
            Log.v("Shader",
                    "Could not compile vshader:"
                            + GLES20.glGetShaderInfoLog(vshader));
            GLES20.glDeleteShader(vshader);
            vshader = 0;
        } else {

            Log.i("GLES20", "compile vertex success");

        }

        int fshader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
        GLES20.glShaderSource(fshader, fss);
        GLES20.glCompileShader(fshader);
        GLES20.glGetShaderiv(fshader, GLES20.GL_COMPILE_STATUS, compiled, 0);
        if (compiled[0] == 0) {
            Log.e("Shader", "Could not compile fshader");
            Log.v("Shader",
                    "Could not compile fshader:"
                            + GLES20.glGetShaderInfoLog(fshader));
            GLES20.glDeleteShader(fshader);
            fshader = 0;
        } else {
            Log.i("GLES20", "compile fragment success");

        }

        int program = GLES20.glCreateProgram();
        GLES20.glAttachShader(program, vshader);
        GLES20.glAttachShader(program, fshader);
        GLES20.glLinkProgram(program);

        return program;
    }


}

推荐答案

我看不到将矩阵 mMVPMatrix 中的值设置为统一的 uMVPMatrix 的位置.在绘制数组之前,您应该执行以下操作:

I cant's see where you set the values from the matrix mMVPMatrix to the uniform uMVPMatrix. You should do something like this before drawing the arrays:

GLES20.glUniformMatrix4fv( muMVPMatrixHandle, 1, false, mMVPMatrix, 0 );

@derhass @ reto-koradi感谢您纠正我的答案

@derhass @reto-koradi thank you for correcting my answer

这篇关于纹理上的OpenGL ES 2.0着色器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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