2013-03-29 152 views
0

我想繪製一個正方形併爲其添加紋理。我剛剛開始使用OpenGL,我正在按照本教程http://blog.uncle.se/2012/02/opengl-es-tutorial-for-android-part-vi-textures/OpenGL ES Android位圖紋理不顯示

這裏是代碼爲我的畫方塊程序:

public void draw(float[] mvpMatrix) { 
    // Add program to OpenGL environment 
    GLES20.glUseProgram(mProgram); 

    // get handle to vertex shader's vPosition member 
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); 

    // Enable a handle to the triangle vertices 
    GLES20.glEnableVertexAttribArray(mPositionHandle); 

    // Prepare the triangle coordinate data 
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, 
           GLES20.GL_FLOAT, false, 
           vertexStride, vertexBuffer); 

    // get handle to fragment shader's vColor member 
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); 

    // Set color for drawing the square 
    GLES20.glUniform4fv(mColorHandle, 1, color, 0); 

    /** now trying to add the texture */ 

    Bitmap bitmap = BitmapFactory.decodeResource(chamRenderer.context.getResources(), 
      R.drawable.ic_launcher); 

    // Create an int array with the number of textures we want, 
    // in this case 1. 
    int[] textures = new int[1]; 
    // Tell OpenGL to generate textures. 
    GLES20.glGenTextures(1, textures, 0); 
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]); 
    // Scale up if the texture if smaller. 
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, 
      GLES20.GL_TEXTURE_MAG_FILTER, 
      GLES20.GL_LINEAR); 
    // scale linearly when image smalled than texture 
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, 
      GLES20.GL_TEXTURE_MIN_FILTER, 
      GLES20.GL_LINEAR); 

    // get handle to shape's transformation matrix 
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); 
    Renderer.checkGlError("glGetUniformLocation"); 

    // Apply the projection and view transformation 
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); 
    Renderer.checkGlError("glUniformMatrix4fv"); 

    // Draw the square 
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, 
          GLES20.GL_UNSIGNED_SHORT, drawListBuffer); 

    // Disable vertex array 
    GLES20.glDisableVertexAttribArray(mPositionHandle); 
} 

藍色顯示效果細膩,但他們具有質感,沒有錯誤,只是沒有他們沒有希望。就像我說的,我現在非常喜歡OpenGL。謝謝:)

回答

0

如果我正在閱讀這個權利,它顯示藍色和白色方塊(看起來像瓷磚),但不是紋理?紋理是存儲在位圖變量中的圖像。創建位圖後您不做任何事情。看起來你還沒有完成教程;代碼是他們用來渲染它不在你的代碼中。查看說明UV映射的部分,並查看該代碼並確保包含該代碼。