1、修改 MyAbstractRenderer 类
添加三个成员变量
//宽高比
protected float ratio;
//屏幕宽
protected int width;
//屏幕高
protected int height;
修改 onSurfaceChanged 方法
public void onSurfaceChanged(GL10 gl, int width, int height) {
//openGL基于状态机
/*单位矩阵
* [1,0,0,0]
* [0,1,0,0]
* [0,0,1,0]
* [0,0,0,1]
*/
this.width =width;
this.height =height;
//视口
gl.glViewport(0, 0, width, height);
//矩阵模式
//透视投影:有深度 (投影矩阵)
//正投影:没有深度
gl.glMatrixMode(GL10.GL_PROJECTION);//(投影矩阵)
//原则:在操作矩阵前先加载单位矩阵 然后才干别的事情
//将当前的用户坐标系的原点移到了屏幕中心:类似于一个复位操作
gl.glLoadIdentity();
//平截头体
//左侧(宽高比),右侧(宽高比),下侧,上侧,近平面、远平面
ratio=(float)width/(float)height;
gl.glFrustumf(-ratio, ratio,-1f, 1f, 3, 7);
}
2、创建 MyScissorRenderer 类
public class MyScissorRenderer extends MyAbstractRenderer {
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
//设置清屏色(背景)
gl.glClearColor(0, 0, 0, 1);
//启用顶点缓冲区
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}
@Override
protected void draw(GL10 gl) {
//启用剪裁
gl.glEnable(GL10.GL_SCISSOR_TEST);
float z =1.9999999f;//在模拟器上2f 可以使用 、而使用1.99f是因为某些机子在使用2f不在平截头体内
float y =1f;
float[] coords={
-ratio,y,z,
ratio,y,z,
-ratio,-y,z,
ratio,-y,z,
};
//颜色数组
float[][] colors={
{1f,0f,0f,1f},
{0f,1f,0f,1f},
{0f,0f,1f,1f},
{1f,1f,0f,1f},
{0f,1f,1f,1f},
{1f,0f,1f,1f},
};
int setp =20;
for(int i=0;i<6;i++){
//设置剪裁区(绘制区域)
gl.glScissor(i*setp, i*setp, width-i*setp*2, height-i*setp*2);
//设置颜色
gl.glColor4f(colors[i][0], colors[i][1], colors[i][2], colors[i][3]);
ByteBuffer cbb =BufferUtil.arr2ByteBuffer(coords);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cbb);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, coords.length/3);
}
}
}
3、运行结果