2011-01-03 12 views
0

Ich versuche eine Android-App zu bekommen, um einfachen OpenGL-Code zu zeichnen, den ich hier gefunden habe. http://www.iphonemobilephones.com/opengl-es-from-the-ground-up-part-4-let-there-be-light.html Für das Leben von mir will es nicht die Zeichnung für irgendeine Form, wo ich mehrere Dreiecke habe. Wenn ich ein einzelnes Dreieck habe, funktioniert es gut.Android- und OpenGL-Dreiecke werden nicht angezeigt

Ich hatte gehofft, dass jemand, der mehr über OpenGL weiß, so freundlich wäre, um zu sehen, ob es irgendwelche Fehler gibt, die auf Anhieb offensichtlich sind.

Danke, mj

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);  
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY); 

    gl.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);  
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    gl.glRotatef(1.0f, 1.0f, 1.0f, 0f);   


    float vertices[]= { 
      0f, -0.525731f, 0.850651f,    // vertices[0] 
      0.850651f, 0f, 0.525731f,    // vertices[1] 
      0.850651f, 0f, -0.525731f,    // vertices[2] 
      -0.850651f, 0f, -0.525731f,   // vertices[3] 
      -0.850651f, 0f, 0.525731f,    // vertices[4] 
      -0.525731f, 0.850651f, 0f,    // vertices[5] 
      0.525731f, 0.850651f, 0f,    // vertices[6] 
      0.525731f, -0.850651f, 0f,    // vertices[7] 
      -0.525731f, -0.850651f, 0f,   // vertices[8] 
      0f, -0.525731f, -0.850651f,   // vertices[9] 
      0f, 0.525731f, -0.850651f,    // vertices[10] 
      0f, 0.525731f, 0.850651f    // vertices[11] 
     }; 

    FloatBuffer v = FloatBufferFromFloatArray(vertices, 12*3); 

    short icosahedronFaces[] = { 
     1, 2, 6, 
     1, 7, 2, 
     3, 4, 5, 
     4, 3, 8, 
     6, 5, 11, 
     5, 6, 10, 
     9, 10, 2, 
     10, 9, 3, 
     7, 8, 9, 
     8, 7, 0, 
     11, 0, 1, 
     0, 11, 4, 
     6, 2, 10, 
     1, 6, 11, 
     3, 5, 10, 
     5, 4, 11, 
     2, 7, 9, 
     7, 1, 0, 
     3, 9, 8, 
     4, 8, 0, 
    }; 

    ShortBuffer i = ShortBufferFromShortArray(icosahedronFaces, 60); 

    float normals[] = { 
     0.000000f, -0.417775f, 0.675974f, 
     0.675973f, 0.000000f, 0.417775f, 
     0.675973f, -0.000000f, -0.417775f, 
     -0.675973f, 0.000000f, -0.417775f, 
     -0.675973f, -0.000000f, 0.417775f, 
     -0.417775f, 0.675974f, 0.000000f, 
     0.417775f, 0.675973f, -0.000000f, 
     0.417775f, -0.675974f, 0.000000f, 
     -0.417775f, -0.675974f, 0.000000f, 
     0.000000f, -0.417775f, -0.675973f, 
     0.000000f, 0.417775f, -0.675974f, 
     0.000000f, 0.417775f, 0.675973f 
    }; 

    FloatBuffer n = FloatBufferFromFloatArray(normals, 3*12); 

    float colors[] = { 
     1.0f, 0.0f, 0.0f, 1.0f, 
     1.0f, 0.5f, 0.0f, 1.0f, 
     1.0f, 1.0f, 0.0f, 1.0f, 
     0.5f, 1.0f, 0.0f, 1.0f, 
     0.0f, 1.0f, 0.0f, 1.0f, 
     0.0f, 1.0f, 0.5f, 1.0f, 
     0.0f, 1.0f, 1.0f, 1.0f, 
     0.0f, 0.5f, 1.0f, 1.0f, 
     0.0f, 0.0f, 1.0f, 1.0f, 
     0.5f, 0.0f, 1.0f, 1.0f, 
     1.0f, 0.0f, 1.0f, 1.0f, 
     1.0f, 0.0f, 0.5f, 1.0f 
    }; 

    FloatBuffer c = FloatBufferFromFloatArray(colors, 4*12); 

    gl.glVertexPointer(12, GL10.GL_FLOAT, 0, v); 
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, c); 
    gl.glNormalPointer(GL10.GL_FLOAT, 0, n);   
    gl.glDrawElements(GL10.GL_TRIANGLES, 60, GL10.GL_UNSIGNED_SHORT, i);   


// helper functions below as well as the surface created function 

private ShortBuffer ShortBufferFromShortArray(short[] array, int length) 
{ 
    ByteBuffer vbb = ByteBuffer.allocateDirect(length * 4); 
    vbb.order(ByteOrder.nativeOrder()); 
    ShortBuffer fb = vbb.asShortBuffer(); 

    fb.put(array); 
    fb.position(0); 

    return fb; 
} 

private FloatBuffer FloatBufferFromFloatArray(float[] array, int length) 
{ 
    ByteBuffer vbb = ByteBuffer.allocateDirect(length * 4); 
    vbb.order(ByteOrder.nativeOrder()); 
    FloatBuffer fb = vbb.asFloatBuffer(); 

    fb.put(array); 
    fb.position(0); 

    return fb; 
} 

@Override 
public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
    // 
    //gl.glEnable(GL10.GL_DEPTH_TEST); 
    //gl.glMatrixMode(GL10.GL_PROJECTION); 

    gl.glMatrixMode(GL10.GL_MODELVIEW); 

    gl.glEnable(GL10.GL_LIGHTING); 
    gl.glEnable(GL10.GL_LIGHT0); 

    // Define the ambient component of the first light 
    float[] light0Ambient = {0.1f, 0.1f, 0.1f, 1.0f};   
    gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, FloatBufferFromFloatArray(light0Ambient, 4)); 

    // Define the diffuse component of the first light 
    float[] light0Diffuse = {0.7f, 0.7f, 0.7f, 1.0f}; 
    gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, FloatBufferFromFloatArray(light0Diffuse, 4)); 

    // Define the specular component and shininess of the first light 
    float[] light0Specular = {0.7f, 0.7f, 0.7f, 1.0f}; 
    float light0Shininess = 0.4f; 
    //gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, FloatBufferFromFloatArray(light0Specular, 4));   

    // Define the position of the first light 
    float[] light0Position = {1.0f, 0.0f, 0.0f, 0.0f}; 
    gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, FloatBufferFromFloatArray(light0Position, 4)); 

    // Define a direction vector for the light, this one points correct down the Z axis 
    float[] light0Direction = {0.0f, 0.0f, -1.0f}; 
    //gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPOT_DIRECTION, FloatBufferFromFloatArray(light0Direction, 3)); 

    // Define a cutoff angle. This defines a 90° field of vision, since the cutoff 
    // is number of degrees to each side of an imaginary line drawn from the light's 
    // position along the vector supplied in GL_SPOT_DIRECTION above 
    //gl.glLightf(gl.GL_LIGHT0, gl.GL_SPOT_CUTOFF, 180.0f); 

    //gl.glEnable(GL10.GL_CULL_FACE); 
    // which is the front? the one which is drawn counter clockwise 
    //gl.glFrontFace(GL10.GL_CCW); 
    // which one should NOT be drawn 
    //gl.glCullFace(GL10.GL_BACK); 

    initShape(); 

    //gl.glScalef(1.0f, 1.0f, 1.0f); 
} 
+0

Möglicherweise möchten Sie Ihren Code verkürzen und in Verbesserungen einer nach dem anderen hinzufügen, dann wird es einfacher zu beheben. –

+0

Ich habe schon große Brocken ausgeschnitten. Ich habe einfach alle Farben sowie die Normalen entfernt. Ich zeichne nur einfache Dreiecke und immer noch nichts. :( –

+1

In 'ShortBufferFromShortArray' wird unnötigerweise zuviel Speicher zugewiesen, da ein short 16 Bits oder 2 Bytes lang ist,' allocateDirect (length * 2) 'würde dir etwas Speicher sparen. –

Antwort

2

es illegal ist 12 als erstes Argument übergeben von glVertexPointer

+1

yep, Bens Recht, du solltest 3 benutzen. Als Randnotiz solltest du dir * BufferFrom * Array() Funktionen statisch final machen :) – dimsuz

+0

ihr Jungs rockt !!!!!!!!!!!!!!!!!!!!!!!!!!! –

Verwandte Themen