Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 阻止OpenGL清除_Android_Opengl Es - Fatal编程技术网

Android 阻止OpenGL清除

Android 阻止OpenGL清除,android,opengl-es,Android,Opengl Es,我在Android中有一个OpenGL壁纸服务,我试图实现的是在场景中添加(绘制)一些东西,而不会丢失我已经绘制的东西。目前我只处理三角形等基本体,但仍然无法实现我的目标。这是我的密码: 渲染器: @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // Set the background color to black ( rgba ). gl.glClearColor(0.0f, 0.0f,

我在Android中有一个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);
}
方形:

@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°旋转后创建一个圆。

OpenGL ES(或实际上是EGL)的工作方式是,在一个帧被“交换”后,帧的内容是未定义的。如果只想进行更新,则需要将交换行为设置为EGL_BUFFER_PRESERVED。我认为在onSurfaceCreated中调用下面的代码应该可以实现这一点(未经测试):


我正在使用javax.microedition.khronos.opengles包中的GL10类。GLSURFACHEVIEW有一些类似于您提到的方法,但不是那个。甚至谷歌也不知道它可能在哪里。你可以更具体的pls吗?你可以在中找到这些函数是的,首先它需要API lvl 17。。。在我看来,这对像阻止清理这样的小事来说真的很重要。。。它应该是某个地方的一个时间参数。。。不要告诉我,另一种方法是记住我的对象所创建的所有路径,并在每个帧中绘制它们。。。。必须有一种更简单的方法,你认为这是错误的方法-标志不是关于防止清除,而是关于复制前一帧的结果。由于显示器驱动程序正在使用它,因此无法在其上绘制。解决这个问题的一种方法是将你正在绘制的内容渲染成一个纹理,然后针对每一帧将该纹理渲染到屏幕上。对我来说,这仍然是不必要的,但感谢一个解决方案,它看起来很简单。对于2D图形,我想知道使用画布是否是一个更好的选择。。。