2011-07-23 4 views
1

Voici mon GL Code init:Android OpenGL Texture non rendu

gl.glEnable(GL10.GL_TEXTURE_2D); 
    gl.glShadeModel(GL10.GL_SMOOTH); 

    gl.glEnable(GL10.GL_BLEND); 
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 

    gl.glDisable(GL10.GL_DEPTH_TEST); 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glLoadIdentity(); 

    gl.glViewport(0, 0, width, height); 

    float left = 0; 
    float right = width; 
    float top = height; 
    float bottom = 0; 
    gl.glOrthof(left, right, top, bottom, -1, 1); 

    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    gl.glLoadIdentity(); 

    textures.put("ball", new Texture(gl, BitmapFactory.decodeResource(context.getResources(), R.drawable.ball))); 

Et voici ma classe Texture

public class Texture 
{ 
int[] texture = new int[1]; 

static int real_width = 0; 
static int real_height = 0; 

Texture(GL10 gl, Bitmap bmp) 
{ 
    real_width = bmp.getWidth(); 
    real_height = bmp.getHeight(); 

    gl.glGenTextures(1, texture, 0); 

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST); 
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); 

    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0); 
    bmp.recycle(); 

    int error = gl.glGetError(); 
    if (error != GL10.GL_NO_ERROR) 
    { 
     Log.e("ROLL", "GL Texture Load Error: " + GLU.gluErrorString(error)); 
    } 
} 

public void draw(GL10 gl, float x, float y) 
{ 
    draw(gl, x, y, real_width, real_height); 
} 

public void draw(GL10 gl, float x, float y, float w, float h) 
{ 
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 

    float[] vertices = { x + w, y + h, x, y + h, x, y, x + w, y }; 
    float[] textureCoords = { 1, 1, 0, 1, 0, 0, 1, 0 }; 

    ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4); 
    byteBuf.order(ByteOrder.nativeOrder()); 
    FloatBuffer vertexBuffer = byteBuf.asFloatBuffer(); 
    vertexBuffer.put(vertices); 
    vertexBuffer.position(0); 

    byteBuf = ByteBuffer.allocateDirect(textureCoords.length * 4); 
    byteBuf.order(ByteOrder.nativeOrder()); 
    FloatBuffer textureBuffer = byteBuf.asFloatBuffer(); 
    textureBuffer.put(textureCoords); 
    textureBuffer.position(0); 

    gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); 

    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 

    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4); 

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

} 
} 

Le problème que je reçois est où rien est tiré, mais une boîte blanche dans la la forme et la position correctes de l'endroit où la texture devrait être.

L'image est de taille 50x50 et je pensais que ce serait dû à la puissance de 2 chose, mais ce problème se produit également sur l'émulateur.

En outre, j'obtiens une erreur dans le journal et il dit: "GL Texture Load Error: valeur non valide", mais je ne sais pas ce que cela signifie.

Enfin, je dessine la texture de ce type:

gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

textures.get("ball").draw(gl, 10, 10); 
+0

Où est le code que vous utilisez pour charger la texture? –

Répondre

2

De glTexImage2D:

  • GL_INVALID_VALUE est généré si internalFormat est pas 1, 2, 3, 4 ou une de l'accepté résolution et formater les constantes symboliques.
  • GL_INVALID_VALUE est généré si la largeur ou la hauteur est inférieure à 0 ou supérieure à 2 + GL_MAX_TEXTURE_SIZE.
  • GL_INVALID_VALUE est généré si les textures non-power-of-two ne sont pas prises en charge et que la largeur ou la hauteur ne peut pas être représentée par 2k + 2 border pour une valeur entière de k.
  • GL_INVALID_VALUE est générée si la frontière est de 0 ou 1.

Je suppose que le format interne serait erroné. Qu'en est-il d'essayer de le spécifier pour le vôtre. Plus de documentation sur GLUtils vous pouvez trouver there.

Mais comme je le vois d'abord, vous n'avez pas de texture avant de le charger dans GL.

gl.glGenTextures(1, texture, 0); 
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]); 

gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST); 
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); 

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0); 
bmp.recycle(); 
+0

Merci! Après avoir ajouté cette liaison, cela fonctionne! –

1

i résolu à faire:

  1. déplacer toutes les images à res/drawable-nodpi
  2. échelle à une puissance de 2, sous Linux mogrify fera le travail (trouver -exec mogrify -. échelle '512x512! {} \;)

si vous chargez des textures de souces exteral ce code fera la conversion au pouvoir le plus proche de 2 bitmap redimensionnée

public class ScalePowerof2 { // some devices require to scale texture bitmaps to power of 2 size 'ScalePower2.scalePowerof2(Bitmpap)' does the job 
private static boolean isPowerof2(int v) { // is v a 2^n ? 
    for (int i=1; i!=0; i<<=1) if (v==i) return true; 
    return false; 
} 
private static int closestPowerof2(int v) { // n | 2^n <= v 
    for (int i=1; i!=0; i<<=1) if (i>=v) return i; 
    return 0; 
} 
public static Bitmap scalePowerof2(Bitmap bmp) { 
    Bitmap b=null; 
    if (bmp!=null) { 
     int w=bmp.getWidth(), h=bmp.getHeight(); 
     if (isPowerof2(w) & isPowerof2(h)) return bmp; // ok, both 2^n nothing to do 
     w=closestPowerof2(w); h=closestPowerof2(h); // new 2^n values 
     b=Bitmap.createScaledBitmap(bmp, w, h, false); 
    } 
    return b; 
} 

}