2011-12-29 31 views
2

我試圖在OPEN GL 2.0中實現可滾動圖像的網格。我已經使用Canvas Drawing實現了視圖,但出於性能原因,我決定轉換到OGL。 在我的實現中,在每一幀我繪製一個位圖對象列表,每個位圖是一個緩存的圖像縮略圖行。 現在我該如何將這些位圖轉換爲可以與OGL一起使用的紋理?OPEN GL如何將位圖對象轉換爲可繪製紋理

回答

4
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID); 
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); 

..where textureID是紋理的唯一ID(通常從glGenTextures()獲得性,或以其他方式只是保持自己的分配每個新的紋理一個新的ID號碼的系統)。 bitmap是Bitmap對象。


在我的結構類使用率的一個例子:

public class Texture { 

protected String name; 
protected int textureID = -1; 
protected String filename; 

public Texture(String filename){ 
    this.filename = filename; 
} 

public void loadTexture(GL10 gl, Context context){ 

    String[] filenamesplit = filename.split("\\."); 

    name = filenamesplit[filenamesplit.length-2]; 

    int[] textures = new int[1]; 
    //Generate one texture pointer... 
    //GLES20.glGenTextures(1, textures, 0); 

    // texturecount is just a public int in MyActivity extends Activity 
    // I use this because I have issues with glGenTextures() not working     
    textures[0] = ((MyActivity)context).texturecount; 
    ((MyActivity)context).texturecount++; 

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]); 

    //Create Nearest Filtered Texture 
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); 
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); 

    //Different possible texture parameters, e.g. GLES20.GL_CLAMP_TO_EDGE 
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT); 
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT); 

    Bitmap bitmap = FileUtil.openBitmap(name, context); 

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); 

    bitmap.recycle(); 

    textureID = textures[0]; 

} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public int getTextureID() { 
    return textureID; 
} 

public void setTextureID(int textureID) { 
    this.textureID = textureID; 
} 


}