0
votes

I've have problem with textures. I'm making game with player and background. Background moves constantly and user can move player. While player moves,sometimes backgrounds starts flashing or sometimes background texture is player texture. I can't take screenshot because this is instantly and i don't have time to catch this problem. Sorry for english. This is 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];
    }

Render

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);

Programs

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

1 Answers

0
votes

You are passing the same texture unit for both the background and the player '0'. That could be a problem for getting the same texture. About the flickering, you might be rendering the player on the Z-axis as same as the background. Draw the player over the background with some difference in the z-axis. Eg. you could set the background's z-buffer to 0.0f, and player's z-axis to 0.002f.

Sources : http://en.wikipedia.org/wiki/Z-fighting