纹理翻转和颠倒

时间:2016-01-03 09:37:44

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

我试图在简单的方块上绘制一个简单的纹理。

然而,看到纹理图像翻转和颠倒。我想我的所有纹理定义都是正确的,所以我不知道是什么问题。

这是我的纹理加载代码:

final int[] textureObjectIds = new int[1];
        GLES20.glGenTextures(1 , textureObjectIds , 0);
        if (textureObjectIds[0] == 0){
            Logger.Log( TAG , "Unable to generate new texture object");
        }

        //define options for decoding
        final BitmapFactory.Options options = new BitmapFactory.Options();

        options.inScaled = true ;



        final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources() , resourceId , options); //decode texture resource to bitmap
        if (bitmap == null){
            Logger.Log(TAG, "Resource ID " + resourceId + " Could not be decoded");
            GLES20.glDeleteTextures(1 , textureObjectIds , 0);
            return 0;
        }

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureObjectIds[0]); //bind texture to our texture object

        // define magnify and minimize filters
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D , GLES20.GL_TEXTURE_MIN_FILTER , GLES20.GL_LINEAR_MIPMAP_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D , GLES20.GL_TEXTURE_MAG_FILTER , GLES20.GL_LINEAR);

        //GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D , GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        //GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D , GLES20.GL_TEXTURE_WRAP_T , GLES20.GL_CLAMP_TO_EDGE);

        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); // load bitmap data to texture

        bitmap.recycle(); // release recycle

        GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D , 0); //unbind from the texture

        return textureObjectIds[0];

我的方块和纹理的坐标:

private static final float TEXTURE_FLOOR  = 0.0f;
private static final float TEXTURE_CEIL= 2.0f;

private static final float[] VERTEX_DATA  = {
                                                                        //Coordinates : X , Y , Z , S , T
                                                                        //Triangle1
                                                                        -0.5f , -0.5f   , 0.0f , TEXTURE_FLOOR , TEXTURE_CEIL  ,
                                                                         0.5f , -0.5f   , 0.0f , TEXTURE_CEIL  , TEXTURE_CEIL ,
                                                                        -0.5f ,  0.5f   , 0.0f , TEXTURE_FLOOR , TEXTURE_FLOOR ,

                                                                        //Triangle2
                                                                         0.5f  , -0.5f  , 0.0f , TEXTURE_CEIL , TEXTURE_CEIL ,
                                                                         0.5f  ,  0.5f  , 0.0f , TEXTURE_CEIL  , TEXTURE_FLOOR ,
                                                                        -0.5f  ,  0.5f  , 0.0f , TEXTURE_FLOOR  , TEXTURE_FLOOR
                                                                      };

来自代码的更多信息:

private static final int     POSITION_COMPONENT_COUNT               = 3 ;
private static final int     TEXTURE_COORDINATES_COMPONENT_COUNT    = 2 ;
private static final int     STRIDE                                 =  (POSITION_COMPONENT_COUNT + TEXTURE_COORDINATES_COMPONENT_COUNT)
                                                                        * BYTES_PER_FLOAT ;

vertexArray.setVertexAttribPointer(0 , textureShaderProgram.getPositionAttributeLocation() , POSITION_COMPONENT_COUNT , STRIDE);
vertexArray.setVertexAttribPointer(POSITION_COMPONENT_COUNT, textureShaderProgram.getTextureCoordinatesAttributeLocation(), TEXTURE_COORDINATES_COMPONENT_COUNT, STRIDE);

其中:

public void setVertexAttribPointer(int dataOffset , int attributeLocation , int componentCount , int stride){
    floatBuffer.position(0);
    GLES20.glVertexAttribPointer(attributeLocation, componentCount, GLES20.GL_FLOAT, false, stride, floatBuffer);
    GLES20.glEnableVertexAttribArray(attributeLocation);
    floatBuffer.position(0);

带有waze图标的问题示例图片: waze

1 个答案:

答案 0 :(得分:2)

您的纹理坐标已关闭。有关纹理坐标的工作原理,请参考How do opengl texture coordinates work?,并正确设置它们。

除此之外,您忘记在dataOffset函数中使用setVertexAttribPointer参数。

public void setVertexAttribPointer(int dataOffset , int attributeLocation , int componentCount , int stride) {
  floatBuffer.position(0);
  GLES20.glVertexAttribPointer(attributeLocation, componentCount, GLES20.GL_FLOAT, false, stride, floatBuffer);
  GLES20.glEnableVertexAttribArray(attributeLocation);
  floatBuffer.position(0);
}

使顶点位置值用于纹理坐标。您应该将floatBuffer设置为从第一个纹理坐标开始,即dataOffset

public void setVertexAttribPointer(int dataOffset , int attributeLocation , int componentCount , int stride) {
    floatBuffer.position(dataOffset);
    GLES20.glVertexAttribPointer(attributeLocation, componentCount, GLES20.GL_FLOAT, false, stride, floatBuffer);
    GLES20.glEnableVertexAttribArray(attributeLocation);
    floatBuffer.position(0);
}