2016-01-03 64 views
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

+0

为什么你的纹理坐标的范围是0到2?对于你想要实现的,他们应该在0到1的范围内。http://stackoverflow.com/questions/5532595/how-do-opengl-texture-coordinates-work – Reigertje

+0

图像看起来不像纹理坐标从[0,2]。更像[-0.5,0.5]。你能为纹理坐标显示'glVertexAttribPointer'吗?对我来说,这看起来好像你将使用数组的第0和第1个元素而不是第4个和第5个元素。 – BDL

+0

@Reigertje我将纹理坐标更改为0-1和-0.5-0.5的范围,但我得到了相同的结果 – yanish

回答

2

你的纹理坐标处于关闭状态。请参考How do opengl texture coordinates work?了解纹理坐标的工作方式,并正确设置它们。

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

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