2010-08-17 91 views
7

我想使用GLSurfaceView在Android 2.2上呈現簡單的紋理四元組。我正在加載一個BMP圖像(128x128)與BitmapFactory.decodeResource() - 這似乎工作。但是,無論何時我嘗試使用GLUtils.glTexImage2D將此位圖放入OpenGL紋理中,我都會收到OpenGL錯誤:glGetError()將返回1280,GL_INVALID_ENUM。我究竟做錯了什麼? 這是我的渲染代碼:Android Opengl ES:GLUtils.glTexImage2D導致GL_INVALID_ENUM

public class MyRenderer implements GLSurfaceView.Renderer { 
    Context context; 
    int texId; 

    public MyRenderer(Context c) { 
     this.context = c; 
    } 

    @Override 
    public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
     gl.glEnable(GL10.GL_TEXTURE_2D); 
     this.texId = loadTexture(gl);  
    } 

    int loadTexture(GL10 gl) {  
     int[] tmp = new int[1]; 
     gl.glGenTextures(1, tmp, 0); 
     int id = tmp[0]; 

     Bitmap bmp = BitmapFactory.decodeResource(this.context.getResources(), R.drawable.myimage); 
     gl.glGetError(); 
     GLUtils.texImage2D(id, 0, bmp, 0); 
     int err = gl.glGetError(); 
     if (err != 0) { 
      // err == 1280, prints "invalid enum": 
      System.err.println(GLU.gluErrorString(err)); 
     } 

     return id; 
    } 

    @Override 
    public void onSurfaceChanged(GL10 gl, int width, int height) {  
     gl.glViewport(0, 0, width, height); 
     gl.glMatrixMode(GL10.GL_PROJECTION); 
     gl.glLoadIdentity(); 
     GLU.gluOrtho2D(gl, 0, width, height, 0); 
     gl.glMatrixMode(GL10.GL_MODELVIEW); 
     gl.glLoadIdentity();    
    } 

    @Override 
    public void onDrawFrame(GL10 gl) { 
     // ... 
    } 
} 

回答

4

它可能應該是這樣的:

gl.glBindTexture(GL10.GL_TEXTURE_2D, id); 
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0); 
+0

D'哦!你是對的 – 2010-08-17 11:02:46