texSubImage2D不会更新纹理

时间:2016-12-15 08:19:58

标签: android c++ bitmap opengl-es

我想使用OpenGL ES绘制帧动画,所以我使用glTexSubImage2D来更新纹理。

此函数初始化纹理:

 private void initTexture()
    {
        textures=new int[1];
        GLES20.glGenTextures(1,textures,0);
        //Bitmap bitmap= BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/xiaobizi/xiaobizi_0001.png");
        Bitmap bitmap= BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/aa.png");
        ratio=(float)bitmap.getHeight()/(float)bitmap.getWidth();
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,textures[0]);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
        bitmap.recycle();
    }

此功能可以保存和删除位图:

 mTextureMap.remove(mPathList.get(messageTag));
            mTextureMap.put(mPathList.get(saveCount),BitmapFactory.decodeFile(mPathList.get(saveCount)));

private void drawMagic()
    {

        GLES20.glEnable(GLES20.GL_BLEND);
        GLES20.glBlendFunc(GLES20.GL_ONE,GLES20.GL_ONE_MINUS_SRC_ALPHA);

        GLES20.glUseProgram(programMagic);

        int mPositionHandleFace = GLES20.glGetAttribLocation(programMagic, "vPosition");

        GLES20.glEnableVertexAttribArray(mPositionHandleFace);

        GLES20.glVertexAttribPointer(mPositionHandleFace, 2, GLES20.GL_FLOAT, false, 0, 0);
        int mTexCoordLocFace = GLES20.glGetAttribLocation(programMagic, "a_texCoord" );

        // Enable generic vertex attribute array
        GLES20.glEnableVertexAttribArray ( mTexCoordLocFace );

        // Prepare the texturecoordinates

        GLES20.glVertexAttribPointer(mTexCoordLocFace, 2, GLES20.GL_FLOAT, false, 0, 0);
        // Get handle to shape's transformation matrix
        int mTransHandle = GLES20.glGetUniformLocation(programMagic, "transMatrix");
        int mScaleHandle=GLES20.glGetUniformLocation(programMagic,"scaleMatrix");
        int mRotateXHandle=GLES20.glGetUniformLocation(programMagic,"rotateXMatrix");
        int mRotateZHandle=GLES20.glGetUniformLocation(programMagic,"rotateZMatrix");
        int mRotateYHandle=GLES20.glGetUniformLocation(programMagic,"rotateYMatrix");

        //control matrix
        GLES20.glUniformMatrix4fv(mTransHandle,1,false,transMatrix,0);
        GLES20.glUniformMatrix4fv(mScaleHandle,1,false,scaleMatrix,0);
        GLES20.glUniformMatrix4fv(mRotateXHandle,1,false,rotateXMatrix,0);
        GLES20.glUniformMatrix4fv(mRotateZHandle,1,false,rotateZMatrix,0);
        GLES20.glUniformMatrix4fv(mRotateYHandle,1,false,rotateYMatrix,0);
        int mtrxhandleFace = GLES20.glGetUniformLocation(programMagic, "uMVPMatrix");
        // Apply the projection and view transformation
        //GLES20.glUniformMatrix4fv(mtrxhandleFace, 1, false, mGLTextureTransformMatrix, 0);
        GLES20.glUniformMatrix4fv(mtrxhandleFace, 1, false, mSecondTransFormMatrix, 0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,textures[0]);

        if(i>=mTotalCount)
        {
            i=i-mTotalCount;
        }
        // update texture

        GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D,0,0,0,mTextureMap.get(mPathList.get(i)));
        int mSamplerLocFace = GLES20.glGetUniformLocation (programMagic, "s_texture" );
        // Set the sampler texture unit to 0, where we have saved the texture.
        GLES20.glUniform1i ( mSamplerLocFace, 0);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        // Disable vertex array
        GLES20.glDisableVertexAttribArray(mPositionHandleFace);
        GLES20.glDisableVertexAttribArray(mTexCoordLocFace);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,0);
        mDecodeHandler.sendEmptyMessage(i);
        i++;
    }

这是我的drawFrame函数。 在 initTexture 函数中,如果位图属于帧动画 texSubImage2D 工作,纹理总是会更新,但是如果我传递不属于帧动画的位图,函数 texSubImage2D 无效。在这种情况下,纹理总是我在函数 initTexture 中传递的位图没有更新。 如果我在opengl dawing时调用该函数,纹理是黑色的,但它的形状可以正确显示,只有背景为黑色。 我在网上搜索了很长时间。但没用。请帮助或尝试提出一些如何解决此问题的想法。谢谢。

0 个答案:

没有答案