1、修改 Draw.java 新增方法
/*
* 画圆
*/
public static void drawCircle(GL10 gl ,float r){
List<Float> pos =new ArrayList<Float>();
float x =0 ,y =0 ,z =0;
for(float angle =0; angle <Math.PI *2 ;angle =(float)(angle +Math.PI /32)){
x =(float) (r *Math.cos(angle));
y =(float) (r *Math.sin(angle));
pos.add(x);
pos.add(y);
pos.add(z);
}
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, BufferUtil.list2ByteBuffer(pos));
gl.glDrawArrays(GL10.GL_LINES, 0, pos.size()/3);
}
/*
* 画点
*/
public static void drawPoint(GL10 gl ,float []pos,float size){
gl.glPointSize(size);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, BufferUtil.arr2ByteBuffer(pos));
gl.glDrawArrays(GL10.GL_POINTS, 0, pos.length/3);
}
/*
* 画点
* 抗锯齿
*/
public static void drawSmoothPoint(GL10 gl ,float []pos,float size){
gl.glEnable(GL10.GL_POINT_SMOOTH);
gl.glHint(GL10.GL_POINT_SMOOTH_HINT, GL10.GL_NICEST);//效果最好
// gl.glEnable(GL10.GL_LINE_SMOOTH);
// gl.glHint(GL10.GL_LINE_SMOOTH_HINT, GL10.GL_FASTEST);//速度最快
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glPointSize(size);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, BufferUtil.arr2ByteBuffer(pos));
gl.glDrawArrays(GL10.GL_POINTS, 0, pos.length/3);
gl.glDisable(GL10.GL_POINT_SMOOTH);
gl.glDisable(GL10.GL_BLEND);
}
2、新建 MyCirclePointRenderer.java
public class MyCirclePointRenderer extends MyAbstractRenderer {
private float angle =0;
private float angleStep =(float) (Math.PI /180);
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
//设置清屏色(背景)
gl.glClearColor(0, 0, 0, 1);
//启用顶点缓冲区
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}
protected void draw(GL10 gl) {
float r =0.5f;
float x =0,y =0;
float pointSize= 15;
//绘制红圆
gl.glPushMatrix();
gl.glColor4f(1, 0, 0, 1);
Draw.drawCircle(gl, r);
x =(float) (r *Math.cos(angle));
y =(float) (r *Math.sin(angle));
Draw.drawSmoothPoint(gl,new float[] {x,y,0},pointSize);
gl.glPopMatrix();
//绘制绿圆
gl.glPushMatrix();
gl.glRotatef(90, 1, 0, 0);
gl.glRotatef(45, 0, 1, 0);
gl.glColor4f(0, 1, 0, 1);
Draw.drawCircle(gl, r);
x =(float) (r *Math.cos(angle +Math.PI /2));
y =(float) (r *Math.sin(angle +Math.PI /2));
Draw.drawSmoothPoint(gl,new float[] {x,y,0},pointSize);
gl.glPopMatrix();
//绘制蓝圆
gl.glPushMatrix();
gl.glRotatef(90, 1, 0, 0);
gl.glRotatef(-45, 0, 1, 0);
gl.glColor4f(0, 0, 1, 1);
Draw.drawCircle(gl, r);
x =(float) (r *Math.cos(angle +Math.PI));
y =(float) (r *Math.sin(angle +Math.PI));
Draw.drawSmoothPoint(gl,new float[] {x,y,0},pointSize);
gl.glPopMatrix();
//每一帧 旋转一个角度
angle +=angleStep;
}
}
3、运行效果