剪刀测试可限制屏幕的某一部分更新,其他部分不更新。实例代码如下:
public class MyRenderer implements Renderer { public MyRenderer(Context ctx) { } @Override public void onDrawFrame(GL10 gl) { // Clear blue window gl.glClearColor(0.0f, 0.0f, 1.0f, 0.0f); gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // Now set scissor to smaller red sub region gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f); gl.glScissor(100, 100, 600, 400); gl.glEnable(GL10.GL_SCISSOR_TEST); gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // Finally, an even smaller green rectangle gl.glClearColor(0.0f, 1.0f, 0.0f, 0.0f); gl.glScissor(200, 200, 400, 200); gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // Turn scissor back off for next render gl.glDisable(GL10.GL_SCISSOR_TEST); } @Override public void onSurfaceChanged(GL10 gl, int w, int h) { float aspectRatio; float windowWidth; float windowHeight; // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions gl.glViewport(0, 0, w, h); // Reset coordinate system gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) aspectRatio = (float)w / (float)h; if (w <= h) { windowWidth = 100; windowHeight = 100 / aspectRatio; gl.glOrthof(-100.0f, 100.0f, -windowHeight, windowHeight, 1.0f, -1.0f); } else { windowWidth = 100 * aspectRatio; windowHeight = 100; gl.glOrthof(-windowWidth, windowWidth, -100.0f, 100.0f, 1.0f, -1.0f); } gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // TODO Auto-generated method stub gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Enable Smooth Shading, default not really needed. gl.glShadeModel(GL10.GL_SMOOTH); } }