Android OpenGLES 2.0纹理闪烁

时间:2014-07-16 11:18:17

标签: android opengl-es opengl-es-2.0

我的纹理有问题。我正在玩玩家和背景游戏。背景不断移动,用户可以移动玩家。当玩家移动时,有时背景开始闪烁或有时背景纹理是玩家纹理。我无法截屏,因为这是即时的,我没有时间来解决这个问题。对不起,英语。 这是textureloader

public int loadTexture(Context context, int resource,int index){

        BitmapFactory.Options bo = new BitmapFactory.Options();
        Bitmap tex = BitmapFactory.decodeResource(context.getResources(), resource, bo);
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,texture[index]);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, tex, 0);
        tex.recycle();
        return texture[index];
    }

渲染

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
         GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,texture);

          int mPositionHandle = 
                GLES20.glGetAttribLocation(riGraphicTools.sp_Image, "vPosition");

          GLES20.glEnableVertexAttribArray(mPositionHandle);
          GLES20.glVertexAttribPointer(mPositionHandle, 3,
                                         GLES20.GL_FLOAT, false,
                                         0, vertexBuffer);

          int mTexCoordLoc = GLES20.glGetAttribLocation(riGraphicTools.sp_Image, 
                               "a_texCoord" );

          GLES20.glEnableVertexAttribArray ( mTexCoordLoc );

          GLES20.glVertexAttribPointer ( mTexCoordLoc, 2, GLES20.GL_FLOAT,
                        false,
                        0, uvBuffer);

          int mtrxhandle = GLES20.glGetUniformLocation(riGraphicTools.sp_Image, 
                             "uMVPMatrix");

          GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, m, 0);

          int mSamplerLoc = GLES20.glGetUniformLocation (riGraphicTools.sp_Image, 
                              "s_texture" );

          GLES20.glUniform1i ( mSamplerLoc, 0);

          GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length,
                   GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

          GLES20.glDisableVertexAttribArray(mPositionHandle);
          GLES20.glDisableVertexAttribArray(mTexCoordLoc);

程序

public static final String vs_SolidColor =
        "uniform    mat4        uMVPMatrix;" +
        "attribute  vec4        vPosition;" +
        "void main() {" +
        "  gl_Position = uMVPMatrix * vPosition;" +
        "}";

    public static final String fs_SolidColor =
        "precision mediump float;" +
        "void main() {" +
        "  gl_FragColor = vec4(0.5,0,0,1);" +
        "}"; 

    /* SHADER Image
     * 
     * This shader is for rendering 2D images straight from a texture
     * No additional effects.
     * 
     */
    public static final String vs_Image =
        "uniform mat4 uMVPMatrix;" +
        "attribute vec4 vPosition;" +
        "attribute vec2 a_texCoord;" +
        "varying vec2 v_texCoord;" +
        "void main() {" +
        "  gl_Position = uMVPMatrix * vPosition;" +
        "  v_texCoord = a_texCoord;" +
        "}";

    public static final String fs_Image =
        "precision mediump float;" +
        "varying vec2 v_texCoord;" +
        "uniform sampler2D s_texture;" +
        "void main() {" +
        "  gl_FragColor = texture2D( s_texture, v_texCoord );" +
        "}"; 

1 个答案:

答案 0 :(得分:0)

您为背景和玩家'0'传递相同的纹理单元。这可能是获得相同纹理的问题。关于闪烁,您可能将Z轴上的播放器渲染为与背景相同。在背景上绘制播放器,在z轴上有一些差异。例如。你可以将背景的z缓冲区设置为0.0f,将玩家的z轴设置为0.002f。

资料来源: http://en.wikipedia.org/wiki/Z-fighting