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);
我的問題與位智圖標
爲什麼你的紋理座標的範圍是0到2?對於你想要實現的,他們應該在0到1的範圍內。http://stackoverflow.com/questions/5532595/how-do-opengl-texture-coordinates-work – Reigertje
圖像看起來不像紋理座標從[0,2]。更像[-0.5,0.5]。你能爲紋理座標顯示'glVertexAttribPointer'嗎?對我來說,這看起來好像你將使用數組的第0和第1個元素而不是第4個和第5個元素。 – BDL
@Reigertje我將紋理座標更改爲0-1和-0.5-0.5的範圍,但我得到了相同的結果 – yanish