2014-02-06 53 views
0

我在Android中有一个OpenGL WallpaperService,我想要实现的是向场景添加(绘制)某些内容,而不会丢失已经绘制的内容。目前,我只与三角形等原始图形工作,但仍然无法实现我的目标。这里是我的代码:防止OpenGL清除

渲染:

@Override 
public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
    // Set the background color to black (rgba). 
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); 
    // Enable Smooth Shading, default not really needed. 
    gl.glShadeModel(GL10.GL_SMOOTH); 
    // Depth buffer setup. 
    gl.glClearDepthf(1.0f); 
    // Enables depth testing. 
    gl.glEnable(GL10.GL_DEPTH_TEST); 
    // The type of depth testing to do. 
    gl.glDepthFunc(GL10.GL_LEQUAL); 
    // Really nice perspective calculations. 
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, 
      GL10.GL_NICEST); 
} 

@Override 
public void onSurfaceChanged(GL10 gl, int width, int height) { 
    // Sets the current view port to the new size. 
    gl.glViewport(0, 0, width, height); 
    // Select the projection matrix 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    // Reset the projection matrix 
    gl.glLoadIdentity(); 
    // Calculate the aspect ratio of the window 
    GLU.gluPerspective(gl, 45.0f, 
      (float) width/(float) height, 
      0.1f, 100.0f); 
    // Select the modelview matrix 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    // Reset the modelview matrix 
    gl.glLoadIdentity(); 
} 

@Override 
public void onDrawFrame(GL10 gl) { 
    // Clears the screen and depth buffer. 
    //gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    // Replace the current matrix with the identity matrix 
    gl.glLoadIdentity(); 
    // Translates 10 units into the screen. 
    gl.glTranslatef(0, 0, -10); 

    // SQUARE A 
    // Save the current matrix. 
    gl.glPushMatrix(); 
    // Rotate square A counter-clockwise. 
    gl.glRotatef(angle, 0, 0, 1); 
    // Draw square A. 
    square.draw(gl); 
    // Restore the last matrix. 
    gl.glPopMatrix(); 

    // SQUARE B 
    // Save the current matrix 
    gl.glPushMatrix(); 
    // Rotate square B before moving it, making it rotate around A. 
    gl.glRotatef(-angle, 0, 0, 1); 
    // Move square B. 
    gl.glTranslatef(2, 0, 0); 
    // Scale it to 50% of square A 
    gl.glScalef(.5f, .5f, .5f); 
    // Draw square B. 
    square.draw(gl); 

    // SQUARE C 
    // Save the current matrix 
    gl.glPushMatrix(); 
    // Make the rotation around B 
    gl.glRotatef(-angle, 0, 0, 1); 
    gl.glTranslatef(2, 0, 0); 
    // Scale it to 50% of square B 
    gl.glScalef(.5f, .5f, .5f); 
    // Rotate around it's own center. 
    gl.glRotatef(angle*10, 0, 0, 1); 
    // Draw square C. 
    square.draw(gl); 

    // Restore to the matrix as it was before C. 
    gl.glPopMatrix(); 
    // Restore to the matrix as it was before B. 
    gl.glPopMatrix(); 

    // Increse the angle. 
    angle++; 
} 

广场:

public Square() { 
    // a float is 4 bytes, therefore we multiply the number if 
    // vertices with 4. 
    ByteBuffer vbb = ByteBuffer.allocateDirect(mVertices.length * 4); 
    vbb.order(ByteOrder.nativeOrder()); 
    mVertexBuffer = vbb.asFloatBuffer(); 
    mVertexBuffer.put(mVertices); 
    mVertexBuffer.position(0); 

    // short is 2 bytes, therefore we multiply the number if 
    // vertices with 2. 
    ByteBuffer ibb = ByteBuffer.allocateDirect(mIndices.length * 2); 
    ibb.order(ByteOrder.nativeOrder()); 
    mIndexBuffer = ibb.asShortBuffer(); 
    mIndexBuffer.put(mIndices); 
    mIndexBuffer.position(0); 
} 

/** 
* This function draws our square on screen. 
* @param gl 
*/ 
public void draw(GL10 gl) { 
    // Counter-clockwise winding. 
    gl.glFrontFace(GL10.GL_CCW); 
    // Enable face culling. 
    gl.glEnable(GL10.GL_CULL_FACE); 
    // What faces to remove with the face culling. 
    gl.glCullFace(GL10.GL_BACK); 

    // Enabled the vertices buffer for writing and to be used during 
    // rendering. 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    // Specifies the location and data format of an array of vertex 
    // coordinates to use when rendering. 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer); 

    gl.glDrawElements(GL10.GL_TRIANGLES, mIndices.length, 
      GL10.GL_UNSIGNED_SHORT, mIndexBuffer); 

    // Disable the vertices buffer. 
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    // Disable face culling. 
    gl.glDisable(GL10.GL_CULL_FACE); 
} 

现在它显示3个格在空间中旋转各地的不同点。但是我不想清除它,所以它可以在360°旋转后创建一个圆。

回答

0

OpenGL ES(或实际上是EGL)的工作原理是,在一个帧被“交换”之后,帧的内容是未定义的。如果只想执行更新,则需要将交换行为设置为EGL_BUFFER_PRESERVED。我想调用onSurfaceCreated下面的代码应该启用此(未测试):

eglSurfaceAttrib(eglGetCurrentDisplay(), eglGetCurrentSurface(), EGL_SWAP_BEHAVIOUR, EGL_BUFFER_PRESERVED); 
+0

我从javax.microedition.khronos.opengles包GL10类的工作。 GLSurfaceView有几种类似于你提到的方法,但不是那个。即使谷歌不知道它可能在哪里。你能指定更多吗? – arenaq

+0

你可以在[android.opengl.EGL14](http://developer.android.com/reference/android/opengl/EGL14.html)找到这些函数。 –

+0

是的,首先它需要API lvl 17 ...在我看来,对于像防止清理这样的小事来说真的很顽固......它应该像某个时间参数一样......不要告诉我,另一种方式是记住我的对象所做的所有路径并将它们绘制每一帧......必须有更简单的方法 – arenaq