2010-12-22 16 views
1

Ich habe eine sehr grundlegende Aktivität im Moment. Es erstellt eine GLSurfaceView und setzt den Renderer. Das Problem ist alles, was ich sehe, ist rot, das ist von GlClearColor, und keine Textur. Nicht einmal eine weiße Fläche. Auch glGetError() meldet nichts. HierAndroid OpenGL ES nicht zeichnen Textur

ist der Renderer:

public class MyRenderer implements Renderer { 

    public MyRenderer(Context context) 
    { 
     mContext = context; 
    } 

    public void onDrawFrame(GL10 gl) { 
     gl.glClearColor(1.0f, 0.0f, 0.0f, 1.0f); 
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

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

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

     gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertex); 
     gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoords); 
     gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); 

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

    public void onSurfaceChanged(GL10 gl, int width, int height) { 
     gl.glViewport(0, 0, width, height); 

     gl.glMatrixMode(GL10.GL_PROJECTION); 
     gl.glLoadIdentity(); 
     gl.glOrthof(-160.0f, 160.0f, -240.0f, 240.0f, 0.1f, 1.0f); 

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

    public void onSurfaceCreated(GL10 gl, EGLConfig config) { 

     float vertexBuffer[] = { 
       -160.0f, -240.0f, 
       -160.0f, 240.0f, 
       160.0f, -240.0f, 
       160.0f, 240.0f 
     }; 

     vertex = ByteBuffer.allocateDirect(8 * 4).asFloatBuffer().put(vertexBuffer); 

     float texCoordsBuffer[] = { 
       0.0f, 0.0f, 
       0.0f, 480.0f/512.0f, 
       320.0f/512.0f, 0.0f, 
       320.0f/512.0f, 480.0f/512.0f 
     }; 

     texCoords = ByteBuffer.allocateDirect(8 * 4).asFloatBuffer().put(texCoordsBuffer); 

      BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inDensity = 240; // needed so that the image will be 512x512 
     Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image, options); 
     int width = bitmap.getWidth(); 
     int height = bitmap.getHeight(); 

     Log.i(TAG, "Bitmap:{w:" + width + " h:" + height + "}"); 

     gl.glEnable(GL10.GL_TEXTURE_2D); 

     texture = new int[1]; 

     gl.glGenTextures(1, texture, 0); 

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

     gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
     gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 

     GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); 

     bitmap.recycle(); 

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

     } 


    } 

    private Context mContext; 
    private int texture[]; 
    private FloatBuffer vertex; 
    private FloatBuffer texCoords; 
} 

Antwort

1

Es gibt mehrere Probleme mit Ihrem Code:

Sie müssen die Byte-Reihenfolge der Puffer auf native gesetzt:

vertex.order(ByteOrder.nativeOrder()) 

Daten in die Puffer Nach dem Kopieren, stellen Sie die Position auf 0:

vertex.position(0) 

(Haben beide für texCoord als auch).

Es würde wahrscheinlich auch helfen, Ihre nahe Clipping-Ebene auf -1.0 statt .1 (in glOrthof) zu setzen.

0

// erforderlich, weil das Bild nicht 512x512

sein

haben OpenGL die Textur machen, die Texturgröße muss eine Macht sein von 2, dh 64x64, 128x32 oder 256x1024

+0

512 ist eine Potenz von 2. 2^9 = 512 – NebulaFox

+1

ja aber es wird gesagt, es wird nicht 512x512 .. so habe ich gesagt, was auch immer es sein wird, sollte es eine Leistung von 2 –

+0

wenn ich don 't inDensity = 240, das Bild wird nicht 512x512 sein – NebulaFox