2012-07-20 230 views
1

我開始使用OpenGL-ES爲Android編寫遊戲,並完成了使用glDrawTexfOES擴展的繪製代碼。當我在模擬器上測試它時,它工作正常,但是在我的Samsung Galaxy S2上測試它,似乎所有的紋理都被繪製成白色。Android繪製紋理擴展繪製白色紋理

爲了確保我沒有犯任何錯誤,我從教程中複製了源代碼並使用相同的結果運行它。我正在使用的教程代碼可以看到here

我的紋理是.PNG格式和兩個冪,我從R.drawable文件夾中加載,儘管我已經嘗試了一些其他位置,例如drawable-nodpi,正如我所看到的那樣。

我也檢查了我讀過的glGenTextures的結果可以給某些手機的奇數值,但似乎給出了正確的值(1,2,3 ..)。

有人知道爲什麼會發生這種情況,或者建議我可以做一些其他檢查來弄清楚什麼是錯的?

這是我上面鏈接的示例代碼的一個稍作修改的版本,以保持簡單。

public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) { 

    gl10.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); 
    // Set the background colour to black (rgba). 
    gl10.glClearColor(0.0f, 0.0f, 0.0f, 1); 
    // Enable Flat Shading. 
    gl10.glShadeModel(GL10.GL_FLAT); 
    // We don't need to worry about depth testing! 
    gl10.glDisable(GL10.GL_DEPTH_TEST); 
    // Set OpenGL to optimise for 2D Textures 
    gl10.glEnable(GL10.GL_TEXTURE_2D); 
    // Disable 3D specific features. 
    gl10.glDisable(GL10.GL_DITHER); 
    gl10.glDisable(GL10.GL_LIGHTING); 

    gl10.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE); 
    // Initial clear of the screen. 
    gl10.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

    // Test for draw texture 

    // Test for device specific extensions 
    String extensions = gl10.glGetString(GL10.GL_EXTENSIONS); 
    boolean drawTexture = extensions.contains("draw_texture"); 
    Log.i("OpenGL Support - ver.:", 
      gl10.glGetString(GL10.GL_VERSION) + " renderer:" + 
      gl10.glGetString(GL10.GL_RENDERER) + " : " + 
      (drawTexture ? "good to go!" : "forget it!!")); 

    // LOAD TEXTURE 

    mTextureName = new int[1]; 

    // Generate Texture ID 
    gl10.glGenTextures(1, mTextureName, 0); 
    assert gl10.glGetError() == GL10.GL_NO_ERROR; 

    // Bind texture id/target (we want 2D of course) 
    gl10.glBindTexture(GL10.GL_TEXTURE_2D, mTextureName[0]); 

    // Open and input stream and read the image 
    InputStream is = mContext.getResources().openRawResource(R.drawable.asteroid); 
    Bitmap bitmap; 
    try { 
     bitmap = BitmapFactory.decodeStream(is); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    // Build our crop region to be the size of the bitmap (ie full image) 
    mCrop = new int[4]; 
    mCrop[0] = 0; 
    mCrop[1] = imageHeight = bitmap.getHeight(); 
    mCrop[2] = imageWidth = bitmap.getWidth(); 
    mCrop[3] = -bitmap.getHeight(); 


    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); 
    assert gl10.glGetError() == GL10.GL_NO_ERROR; 

    bitmap.recycle(); 
} 

public void onSurfaceChanged(GL10 gl10, int i, int i1) { 

    gl10.glViewport(0, 0, i, i1); 
    /* 
    * Set our projection matrix. This doesn't have to be done each time we 
    * draw, but usually a new projection needs to be set when the viewport 
    * is resized. 
    */ 
    float ratio = (float) i/i1; 
    gl10.glMatrixMode(GL10.GL_PROJECTION); 
    gl10.glLoadIdentity(); 
    gl10.glFrustumf(-ratio, ratio, -1, 1, 1, 10); 
} 


public void onDrawFrame(GL10 gl) { 
    // Just clear the screen and depth buffer. 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

    // Begin drawing 
    //-------------- 
    // These function calls can be experimented with for various effects such as transparency 
    // although certain functionality maybe device specific. 
    gl.glShadeModel(GL10.GL_FLAT); 
    gl.glEnable(GL10.GL_BLEND); 
    gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA); 
    gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000); 

    // Setup correct projection matrix 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glPushMatrix(); 
    gl.glLoadIdentity(); 
    gl.glOrthof(0.0f, mWidth, 0.0f, mHeight, 0.0f, 1.0f); 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    gl.glPushMatrix(); 
    gl.glLoadIdentity(); 

    gl.glEnable(GL10.GL_TEXTURE_2D); 

    // Draw all Textures 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureName[0]); 
    ((GL11)gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, mCrop, 0); 
    ((GL11Ext)gl).glDrawTexfOES(0, 0, 0, imageWidth, imageHeight); 

    // Finish drawing 
    gl.glDisable(GL10.GL_BLEND); 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glPopMatrix(); 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    gl.glPopMatrix(); 
} 
+0

在銀河上試過glGetError? – Tim 2012-07-20 18:42:38

+0

似乎沒有引發任何錯誤,只是返回0(GL_NO_ERROR)我試過的任何地方。 – Greg 2012-07-21 07:59:02

回答

1

您是否試圖將drawable-nodpi和其他地方的所有圖像?

我不認爲這可能很重要,但在附加渲染之前嘗試這些行。

glSurfaceView.setZOrderOnTop(true); 
glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 
glSurfaceView.getHolder().setFormat(PixelFormat.RGBA_8888); 

該錯誤可能與透明度和PNG格式有關。

如果它不起作用,您可以粘貼與GLSurfaceView和Renderer相關的代碼嗎?

謝謝!

+0

我已經嘗試了只能在drawable-nodpi中顯示圖像,並且它會得到相同的結果。還添加了他們的線條,並沒有改變。 我已經從上面的例子中添加了一個簡單渲染類的代碼,GLSurfaceView默認只是修改後的渲染器。 – Greg 2012-07-21 13:45:27

+0

代碼似乎沒問題。你可以在glTexParameteriv調用之前告訴我mCrop的4個值嗎? 謝謝。 – charlypu 2012-07-21 14:02:16

+0

數值爲[0,64,64,-64] 感謝您的幫助! – Greg 2012-07-21 18:39:37