public class MyActivity extends Activity {
private MyRenderer render;
private MyGLSurfaceView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new MyGLSurfaceView(this);
render = new MyRenderer();
view.setEGLConfigChooser(5, 6, 5, 0, 16, 4);
view.setRenderer(render);
view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);// 脏渲染,命令渲染
setContentView(view);
}
class MyGLSurfaceView extends GLSurfaceView {
public MyGLSurfaceView(Context context) {
super(context);
}
public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
class MyRenderer implements GLSurfaceView.Renderer {
private float xrotate, yrotate;
private float ratio = 0;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
gl.glClearColor(0f, 0f, 0f, 1.0f);// clearColor
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// vertex array
}
@Override
public void onSurfaceChanged(GL10 gl, int w, int h) {// 设置视口
gl.glViewport(0, 0, w, h);
ratio = (float) w / h;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glColor4f(0f, 0f, 1f, 1f);
// 模型视图矩阵
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, 5, 0, 0, 0, 0, 1, 0);
// 旋转
gl.glRotatef(xrotate, 1, 0, 0);
gl.glRotatef(yrotate, 0, 1, 0);
List coordsList = new ArrayList();
float Rinner = 0.2f;// 内环半径
float Rring = 0.3f;// 环半径
int count = 20;
float alphaStep = (float) (2 * Math.PI / count);
float alpha = 0;
float x0, y0, z0, x1, y1, z1;
int count0 = 20;
float betaStep = (float) (2 * Math.PI) / count0;
float beta = 0;
for (int i = 0; i < count; i++) {
alpha = i * alphaStep;
for (int j = 0; j <= count0; j++) {
beta = j * betaStep;
x0 = (float) (Math.cos(alpha) * (Rinner + Rring * (1 + Math.cos(beta))));
y0 = (float) (Math.sin(alpha) * (Rinner + Rring * (1 + Math.cos(beta))));
z0 = (float) (-Rring * Math.sin(beta));
x1 = (float) (Math.cos(alpha + alphaStep) * (Rinner + Rring * (1 + Math.cos(beta))));
y1 = (float) (Math.sin(alpha + alphaStep) * (Rinner + Rring * (1 + Math.cos(beta))));
z1 = (float) (-Rring * Math.sin(beta));
coordsList.add(x0);
coordsList.add(y0);
coordsList.add(z0);
coordsList.add(x1);
coordsList.add(y1);
coordsList.add(z1);
}
}
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, list2FloatBuffer(coordsList));
gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, coordsList.size() / 3);
}
}
private FloatBuffer list2FloatBuffer(List list) {
ByteBuffer ibb = ByteBuffer.allocateDirect(list.size() * 4);
ibb.order(ByteOrder.nativeOrder());
FloatBuffer fbb = ibb.asFloatBuffer();
for (Float f : list) {
fbb.put(f);
}
fbb.position(0);
return fbb;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
float step = 5f;
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
render.xrotate -= step;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
render.xrotate += step;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
render.yrotate += step;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
render.yrotate -= step;
}
// 请求渲染,和脏渲染配合使用
view.requestRender();
return super.onKeyDown(keyCode, event);
}
}