2012-03-15 35 views
1

我最近使用Mono for Android將我自己的2D渲染引擎移植到Android 除了我根本無法繪製紋理,所有紋理看起來都像空。如何使用Mono for Android在OpenGLES中正確加載和繪製紋理

所以我開始了一個新的乾淨的項目來測試它,並且我根據java的示例代碼編寫了它,我發現的是我無法繪製紋理。 這裏是我用於測試的代碼:

class GLView1 : AndroidGameView 
{ 
    public GLView1(Context context) 
     : base(context) 
    { 
     this.GLContextVersion = OpenTK.Graphics.GLContextVersion.Gles1_1; 
    } 

    // This gets called when the drawing surface is ready 
    protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 

     // Run the render loop 
     Run(); 
    } 

    bool initialized = false; 
    int[] textureID = new int[1]; 
    // This gets called on each frame render 
    protected override void OnRenderFrame(FrameEventArgs e) 
    { 
     base.OnRenderFrame(e); 
     if (!initialized) 
     { 
      initialized = true; 
      GL.Enable(All.Texture2D); 
      GL.GenTextures(1, textureID); 
      GL.ShadeModel(All.Smooth); 
      GL.BindTexture(All.Texture2D, textureID[0]); 

      Bitmap bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Icon); 
      Android.Opengl.GLUtils.TexImage2D(Android.Opengl.GLES10.GlTexture2d, 0, Android.Opengl.GLES10.GlRgba, bitmap, 0); 
      bitmap.Recycle(); 

      GL.TexParameter(All.Texture2D, All.TextureMinFilter, (int)All.Nearest); 
      GL.TexParameter(All.Texture2D, All.TextureMagFilter, (int)All.Linear); 
     } 
     GL.ClearColor(0.5f, 0.5f, 0.5f, 1.0f); 
     GL.Clear((uint)All.ColorBufferBit); 

     GL.BindTexture(All.Texture2D, textureID[0]); 

     GL.EnableClientState(All.VertexArray); 
     GL.EnableClientState(All.TextureCoordArray); 
     GL.FrontFace(All.Cw); 

     GL.VertexPointer(2, All.Float, 0, square_vertices); 
     GL.TexCoordPointer(2, All.Float, 0, uv); 

     GL.DrawArrays(All.TriangleStrip, 0, 4); 

     SwapBuffers(); 
    } 

    float[] uv ={ 
        0,1, 
        0,0, 
        1,1, 
        1,0, 
       }; 

    float[] square_vertices = { 
     -0.5f, -0.5f, 
     0.5f, -0.5f, 
     -0.5f, 0.5f, 
     0.5f, 0.5f, 
    }; 

    byte[] square_colors = { 
     255, 255, 0, 255, 
     0, 255, 255, 255, 
     0,  0, 0, 0, 
     255, 0, 255, 255, 
    }; 
} 

我在我的Android設備上所看到的僅僅是一個白色的大廣場。 PS:我再次用Android模擬器測試過它,它似乎代碼在android模擬器中工作,但在我的真實設備上它仍然顯示一個大的白色方塊。

我在這裏做錯了什麼?

+0

你有着色器?看起來你正在請求一個gles2.0上下文,我相信它不支持任何固定的管道。 – Tim 2012-03-15 19:34:27

+0

好吧,着色器工作正常,編譯良好,頂點着色器正確地將座標轉換爲屏幕空間,並且我可以通過片段着色器控制輸出顏色,只是顏色= texture2D(SAMPLER0,TexCoord.st)相等(0,0 ,0,0)。對於上面的示例代碼,如果我使用GLES1_1上下文是相同的 – liiir1985 2012-03-15 19:37:57

+0

我認爲你需要用glGetError(或單聲道等價物)開始尋找gl錯誤。你有什麼真的不像gles2.0代碼。 glEnable(GL_TEXTURE_2D)在opengles2.0上是非法的,glEnableClientState/glVertexPointer在2.0中不是真正的函數。您需要使用等效的着色器,即glEnableVertexAttribArray/glVertexAttribPointer。 – Tim 2012-03-15 19:42:11

回答

0

這可能是BitmapFactory.decodeResource()的問題。我遇到了同樣的問題,但是當我使用openRawResource()以流的形式打開資源,然後使用BitmapFactory.decodeStream()加載位圖時,紋理突然出現。

進一步看看BitmapFactory.decodeResource(),它似乎根據目標設備的像素密度進行縮放。看到這個:http://blog.poweredbytoast.com/loading-opengl-textures-in-android

這將導致所有2倍大小的整齊的紋理的大小滑動,以至於硬件加速無法識別(模擬器不關心非2次冪的大小)。所以,使用上面的鏈接代碼,複製在這裏爲您的方便:

BitmapFactory.Options opts = new BitmapFactory.Options(); 
opts.inScaled = false;